From 2d44903299c97f3d4d753922a91ac97c4961a9a0 Mon Sep 17 00:00:00 2001 From: sorhawell Date: Fri, 4 Aug 2023 15:42:15 +0200 Subject: [PATCH] rework all bianry operators --- R/expr__expr.R | 51 +++++++++++++++++++--------------- man/nanoarrow.Rd | 8 +++--- man/wrap_e.Rd | 2 +- src/rust/src/lazy/dsl.rs | 60 ++++++++++++++++++++-------------------- 4 files changed, 64 insertions(+), 57 deletions(-) diff --git a/R/expr__expr.R b/R/expr__expr.R index 2ffc6b94f..2f7d9d287 100644 --- a/R/expr__expr.R +++ b/R/expr__expr.R @@ -98,7 +98,7 @@ wrap_e_legacy = function(e, str_to_lit = TRUE) { } } -#' DEPRECATED wrap as literal +#' wrap as literal #' @description use robj_to!(Expr) on rust side or rarely wrap_e on R-side #' This function is only kept for reference #' @param e an Expr(polars) or any R expression @@ -188,7 +188,7 @@ wrap_elist_result = function(elist, str_to_lit = TRUE) { #' pl$lit(5)$add(pl$lit(10)) #' +pl$lit(5) # unary use resolves to same as pl$lit(5) Expr_add = function(other) { - .pr$Expr$add(self, wrap_e(other) |> result() |> unwrap("in $add()")) + .pr$Expr$add(self, other) |> unwrap("in $add()") } #' @export #' @rdname Expr_add @@ -210,7 +210,7 @@ Expr_add = function(other) { #' pl$lit(5) / pl$lit(10) #' pl$lit(5)$div(pl$lit(10)) Expr_div = function(other) { - .pr$Expr$div(self, wrap_e(other) |> result() |> unwrap("in $div()")) + .pr$Expr$div(self, other) |> unwrap("in $div()") } #' @export #' @rdname Expr_div @@ -230,7 +230,7 @@ Expr_div = function(other) { #' pl$lit(5)$sub(pl$lit(10)) #' -pl$lit(5) Expr_sub = function(other) { - .pr$Expr$sub(self, wrap_e(other) |> result() |> unwrap("in $sub")) + .pr$Expr$sub(self, other) |> unwrap("in $sub()") } #' @export #' @rdname Expr_sub @@ -253,7 +253,7 @@ Expr_sub = function(other) { #' pl$lit(5) * pl$lit(10) #' pl$lit(5)$mul(pl$lit(10)) Expr_mul = Expr_mul = function(other) { - .pr$Expr$mul(self, wrap_e(other) |> result() |> unwrap("in $mul")) + .pr$Expr$mul(self, other) |> unwrap("in $mul()") } #' @export @@ -292,7 +292,7 @@ Expr_is_not = "use_extendr_wrapper" #' pl$lit(5) < pl$lit(10) #' pl$lit(5)$lt(pl$lit(10)) Expr_lt = function(other) { - .pr$Expr$lt(self, wrap_e(other) |> result() |> unwrap("in $lt()")) + .pr$Expr$lt(self, other) |> unwrap("in $lt()") } #' @export #' @details @@ -313,7 +313,7 @@ Expr_lt = function(other) { #' pl$lit(2) > pl$lit(1) #' pl$lit(2)$gt(pl$lit(1)) Expr_gt = function(other) { - .pr$Expr$gt(self, wrap_e(other) |> result() |> unwrap("in $gt()")) + .pr$Expr$gt(self, other) |> unwrap("in $gt()") } #' @export #' @details @@ -334,7 +334,7 @@ Expr_gt = function(other) { #' pl$lit(2) == pl$lit(2) #' pl$lit(2)$eq(pl$lit(2)) Expr_eq = function(other) { - .pr$Expr$eq(self, wrap_e(other) |> result() |> unwrap("in $eq()")) + .pr$Expr$eq(self, other) |> unwrap("in $eq()") } #' @export #' @details @@ -356,7 +356,7 @@ Expr_eq = function(other) { #' pl$lit(1) != pl$lit(2) #' pl$lit(1)$neq(pl$lit(2)) Expr_neq = function(other) { - .pr$Expr$neq(self, wrap_e(other) |> result() |> unwrap("in $neq()" )) + .pr$Expr$neq(self, other) |> unwrap("in $neq()" ) } #' @export #' @details @@ -377,7 +377,7 @@ Expr_neq = function(other) { #' pl$lit(2) <= pl$lit(2) #' pl$lit(2)$lt_eq(pl$lit(2)) Expr_lt_eq = function(other) { - .pr$Expr$lt_eq(self, wrap_e(other) |> result() |> unwrap("in $lt_eq()" )) + .pr$Expr$lt_eq(self, other) |> unwrap("in $lt_eq()" ) } #' @export #' @details @@ -399,7 +399,7 @@ Expr_lt_eq = function(other) { #' pl$lit(2) >= pl$lit(2) #' pl$lit(2)$gt_eq(pl$lit(2)) Expr_gt_eq = function(other) { - .pr$Expr$gt_eq(self, wrap_e(other) |> result() |> unwrap("in $gt_eq()" )) + .pr$Expr$gt_eq(self, other) |> unwrap("in $gt_eq()" ) } #' @export #' @details @@ -900,9 +900,11 @@ Expr_reverse = function() { #' @examples #' pl$lit(TRUE) & TRUE #' pl$lit(TRUE)$and(pl$lit(TRUE)) -Expr_and = "use_extendr_wrapper" +Expr_and = function(other) { + .pr$Expr$and(self, other) |> unwrap("in $and()") +} #' @export -"&.Expr" = function(e1, e2) wrap_e(e1)$and(wrap_e(e2)) +"&.Expr" = function(e1, e2) result(wrap_e(e1)$and(e2)) |> unwrap("using the '&'-operator") #' Or @@ -918,9 +920,11 @@ Expr_and = "use_extendr_wrapper" #' @examples #' pl$lit(TRUE) | FALSE #' pl$lit(TRUE)$or(pl$lit(TRUE)) -Expr_or = "use_extendr_wrapper" +Expr_or = function(other) { + .pr$Expr$or(self, other) |> unwrap("in $or()") +} #' @export -"|.Expr" = function(e1, e2) wrap_e(e1)$or(wrap_e(e2)) +"|.Expr" = function(e1, e2) result(wrap_e(e1)$or(e2)) |> unwrap("using the '|'-operator") #' Xor @@ -934,7 +938,9 @@ Expr_or = "use_extendr_wrapper" #' @usage Expr_xor(other) #' @examples #' pl$lit(TRUE)$xor(pl$lit(FALSE)) -Expr_xor = "use_extendr_wrapper" +Expr_xor = function(other) { + .pr$Expr$xor(self, other) |> unwrap("in $xor()") +} @@ -1011,8 +1017,7 @@ Expr_cast = function(dtype, strict = TRUE) { #' pl$lit(2) %**% (pl$col("a")) #' )$get_column("a")$to_r() == (-1:3)^2 Expr_rpow = function(base) { - if (!inherits(base, "Expr")) base <- pl$lit(base) - expr = .pr$Expr$pow(base, self) + result(wrap_e(base)$pow(self)) |> unwrap("in $rpow()") } #' @rdname Expr_rpow @@ -1023,7 +1028,7 @@ Expr_rpow = function(base) { #' @rdname Expr_rpow #' @export -"%**%.Expr" = function(e1, e2) wrap_e(e1)$rpow(e2) +"%**%.Expr" = function(e1, e2) result(wrap_e(e1)$rpow(e2))|> unwrap("using the '**'-operator") #' Square root @@ -2446,10 +2451,10 @@ Expr_limit = function(n = 10) { #' pl$lit(2)^(pl$col("a")) #' )$get_column("literal")$to_r() == 2^(-1:3) Expr_pow = function(exponent) { - .pr$Expr$pow(self, wrap_e(exponent)) + .pr$Expr$pow(self, exponent) |> unwrap("in $pow()") } #' @export -"^.Expr" = function(e1, e2) wrap_e(e1)$pow(e2) +"^.Expr" = function(e1, e2) result(wrap_e(e1)$pow(e2)) |> unwrap("using '^'-operator") #' is_in @@ -2468,7 +2473,9 @@ Expr_pow = function(exponent) { #' pl$col("a")$is_in(pl$lit(NA_real_)) #' )$to_data_frame()[[1L]] #' -Expr_is_in = "use_extendr_wrapper" +Expr_is_in = function(other) { + .pr$Expr$is_in(self, other) |> unwrap("in $is_in()") +} ## TODO contribute polars, do not panic on by pointing to non positive values #' Repeat by diff --git a/man/nanoarrow.Rd b/man/nanoarrow.Rd index 3ecaf02a4..7af2018a2 100644 --- a/man/nanoarrow.Rd +++ b/man/nanoarrow.Rd @@ -16,13 +16,13 @@ \alias{as_record_batch_reader.DataFrame} \title{polars to nanoarrow and arrow} \usage{ -\method{as_nanoarrow_array_stream}{DataFrame}(x, ..., schema = NULL) +as_nanoarrow_array_stream.DataFrame(x, ..., schema = NULL) -\method{infer_nanoarrow_schema}{DataFrame}(x, ...) +infer_nanoarrow_schema.DataFrame(x, ...) -\method{as_arrow_table}{DataFrame}(x, ...) +as_arrow_table.DataFrame(x, ...) -\method{as_record_batch_reader}{DataFrame}(x, ..., schema = NULL) +as_record_batch_reader.DataFrame(x, ..., schema = NULL) } \arguments{ \item{x}{a polars DataFrame} diff --git a/man/wrap_e.Rd b/man/wrap_e.Rd index 66e2c4346..e99531819 100644 --- a/man/wrap_e.Rd +++ b/man/wrap_e.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/expr__expr.R \name{wrap_e} \alias{wrap_e} -\title{DEPRECATED wrap as literal} +\title{wrap as literal} \usage{ wrap_e(e, str_to_lit = TRUE) } diff --git a/src/rust/src/lazy/dsl.rs b/src/rust/src/lazy/dsl.rs index 5e07e81f3..c12c4a689 100644 --- a/src/rust/src/lazy/dsl.rs +++ b/src/rust/src/lazy/dsl.rs @@ -146,45 +146,45 @@ impl Expr { } //expr binary comparisons - pub fn gt(&self, other: &Expr) -> Self { - self.0.clone().gt(other.0.clone()).into() + pub fn gt(&self, other: Robj) -> RResult { + Ok(self.0.clone().gt(robj_to!(PLExpr, other)?).into()) } - pub fn gt_eq(&self, other: &Expr) -> Self { - self.0.clone().gt_eq(other.0.clone()).into() + pub fn gt_eq(&self, other: Robj) -> RResult { + Ok(self.0.clone().gt_eq(robj_to!(PLExpr, other)?).into()) } - pub fn lt(&self, other: &Expr) -> Self { - self.0.clone().lt(other.0.clone()).into() + pub fn lt(&self, other: Robj) -> RResult { + Ok(self.0.clone().lt(robj_to!(PLExpr, other)?).into()) } - pub fn lt_eq(&self, other: &Expr) -> Self { - self.0.clone().lt_eq(other.0.clone()).into() + pub fn lt_eq(&self, other: Robj) -> RResult { + Ok(self.0.clone().lt_eq(robj_to!(PLExpr, other)?).into()) } - pub fn neq(&self, other: &Expr) -> Self { - self.0.clone().neq(other.0.clone()).into() + pub fn neq(&self, other: Robj) -> RResult { + Ok(self.0.clone().neq(robj_to!(PLExpr, other)?).into()) } - pub fn eq(&self, other: &Expr) -> Self { - self.0.clone().eq(other.0.clone()).into() + pub fn eq(&self, other: Robj) -> RResult { + Ok(self.0.clone().eq(robj_to!(PLExpr, other)?).into()) } //logical operators - fn and(&self, other: &Expr) -> Self { - self.0.clone().and(other.0.clone()).into() + fn and(&self, other: Robj) -> RResult { + Ok(self.0.clone().and(robj_to!(PLExpr, other)?).into()) } - fn or(&self, other: &Expr) -> Self { - self.0.clone().or(other.0.clone()).into() + fn or(&self, other: Robj) -> RResult { + Ok(self.0.clone().or(robj_to!(PLExpr, other)?).into()) } - fn xor(&self, other: &Expr) -> Self { - self.0.clone().xor(other.0.clone()).into() + fn xor(&self, other: Robj) -> RResult { + Ok(self.0.clone().xor(robj_to!(PLExpr, other)?).into()) } - fn is_in(&self, other: &Expr) -> Self { - self.0.clone().is_in(other.0.clone()).into() + fn is_in(&self, other: Robj) -> RResult { + Ok(self.0.clone().is_in(robj_to!(PLExpr, other)?).into()) } //any not translated expr from expr/expr.py @@ -1410,8 +1410,8 @@ impl Expr { self.clone().0.dt().offset_by(by).into() } - pub fn pow(&self, exponent: &Expr) -> Self { - self.0.clone().pow(exponent.0.clone()).into() + pub fn pow(&self, exponent: Robj) -> RResult { + Ok(self.0.clone().pow(robj_to!(PLExpr, exponent)?).into()) } pub fn repeat_by(&self, by: &Expr) -> Self { @@ -1590,21 +1590,21 @@ impl Expr { } //binary arithmetic expressions - pub fn add(&self, other: &Expr) -> Self { - self.0.clone().add(other.0.clone()).into() + pub fn add(&self, other: Robj) -> RResult { + Ok(self.0.clone().add(robj_to!(PLExpr, other)?).into()) } //binary arithmetic expressions - pub fn sub(&self, other: &Expr) -> Self { - self.0.clone().sub(other.0.clone()).into() + pub fn sub(&self, other: Robj) -> RResult { + Ok(self.0.clone().sub(robj_to!(PLExpr, other)?).into()) } - pub fn mul(&self, other: &Expr) -> Self { - self.0.clone().mul(other.0.clone()).into() + pub fn mul(&self, other: Robj) -> RResult { + Ok(self.0.clone().mul(robj_to!(PLExpr, other)?).into()) } - pub fn div(&self, other: &Expr) -> Self { - self.0.clone().div(other.0.clone()).into() + pub fn div(&self, other: Robj) -> RResult { + Ok(self.0.clone().div(robj_to!(PLExpr, other)?).into()) } //unary