From be1092ce7dafd5ba0f0eca773a7bdc16a4eabd1c Mon Sep 17 00:00:00 2001 From: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> Date: Fri, 4 Aug 2023 10:19:32 +0200 Subject: [PATCH] Add method `$clone()` for `LazyFrame` (#347) --- NEWS.md | 3 ++- R/extendr-wrappers.R | 2 ++ R/lazyframe__lazy.R | 11 +++++++++++ man/LazyFrame_clone.Rd | 17 +++++++++++++++++ src/rust/src/lazy/dataframe.rs | 4 ++++ tests/testthat/test-lazy.R | 9 +++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 man/LazyFrame_clone.Rd diff --git a/NEWS.md b/NEWS.md index 43a77589a..266116115 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,7 +2,8 @@ ## What's changed -- New method `$explode()` for `DataFrame` and `LazyFrame`. +- New method `$explode()` for `DataFrame` and `LazyFrame` (#314). +- New method `$clone()` for `LazyFrame` (#347). # polars 0.7.0 diff --git a/R/extendr-wrappers.R b/R/extendr-wrappers.R index 4d3f23af9..bf7c0336f 100644 --- a/R/extendr-wrappers.R +++ b/R/extendr-wrappers.R @@ -951,6 +951,8 @@ LazyFrame$schema <- function() .Call(wrap__LazyFrame__schema, self) LazyFrame$explode <- function(columns, dotdotdot_args) .Call(wrap__LazyFrame__explode, self, columns, dotdotdot_args) +LazyFrame$clone_see_me_macro <- function() .Call(wrap__LazyFrame__clone_see_me_macro, self) + #' @export `$.LazyFrame` <- function (self, name) { func <- LazyFrame[[name]]; environment(func) <- environment(); func } diff --git a/R/lazyframe__lazy.R b/R/lazyframe__lazy.R index 2acd0d6b5..c5b318db9 100644 --- a/R/lazyframe__lazy.R +++ b/R/lazyframe__lazy.R @@ -947,3 +947,14 @@ LazyFrame_explode = function(columns = list(), ...) { .pr$LazyFrame$explode(self, columns, dotdotdot_args) |> unwrap("in explode():") } + +#' Clone a LazyFrame +#' +#' This makes a very cheap deep copy/clone of an existing `LazyFrame`. +#' @return A LazyFrame +#' @examples +#' pl$LazyFrame(mtcars)$clone() + +LazyFrame_clone = function() { + .pr$LazyFrame$clone_see_me_macro(self) +} diff --git a/man/LazyFrame_clone.Rd b/man/LazyFrame_clone.Rd new file mode 100644 index 000000000..46f990824 --- /dev/null +++ b/man/LazyFrame_clone.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/lazyframe__lazy.R +\name{LazyFrame_clone} +\alias{LazyFrame_clone} +\title{Clone a LazyFrame} +\usage{ +LazyFrame_clone() +} +\value{ +A LazyFrame +} +\description{ +This makes a very cheap deep copy/clone of an existing \code{LazyFrame}. +} +\examples{ +pl$LazyFrame(mtcars)$clone() +} diff --git a/src/rust/src/lazy/dataframe.rs b/src/rust/src/lazy/dataframe.rs index 7f935c5da..ea875b77e 100644 --- a/src/rust/src/lazy/dataframe.rs +++ b/src/rust/src/lazy/dataframe.rs @@ -401,6 +401,10 @@ impl LazyFrame { } Ok(self.0.clone().explode(columns).into()) } + + pub fn clone_see_me_macro(&self) -> LazyFrame { + self.clone() + } } #[derive(Clone)] diff --git a/tests/testthat/test-lazy.R b/tests/testthat/test-lazy.R index 2afd5da37..20d0c1f9f 100644 --- a/tests/testthat/test-lazy.R +++ b/tests/testthat/test-lazy.R @@ -646,3 +646,12 @@ test_that("with_row_count", { lf = pl$LazyFrame(mtcars) expect_identical(lf$with_row_count("idx", 42)$select(pl$col("idx"))$collect()$to_data_frame()$idx, as.double(42:(41+nrow(mtcars)))) }) + +test_that("cloning", { + pf = pl$LazyFrame(iris) + + # deep copy clone rust side object, hence not same mem address + pf2 = pf$clone() + expect_identical(pf$collect()$to_data_frame(), pf2$collect()$to_data_frame()) + expect_different(pl$mem_address(pf), pl$mem_address(pf2)) +})