Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support xor in selectors #62

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions R/000-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,13 @@ class(`PlRDataType`) <- c("PlRDataType__bundle", "savvy_neopolars__sealed")
}
}

`PlRExpr__meta_selector_xor` <- function(self) {
function(`other`) {
`other` <- .savvy_extract_ptr(`other`, "PlRExpr")
.savvy_wrap_PlRExpr(.Call(savvy_PlRExpr__meta_selector_xor__impl, `self`, `other`))
}
}

`PlRExpr_abs` <- function(self) {
function() {
.savvy_wrap_PlRExpr(.Call(savvy_PlRExpr_abs__impl, `self`))
Expand Down Expand Up @@ -2907,6 +2914,7 @@ class(`PlRDataType`) <- c("PlRDataType__bundle", "savvy_neopolars__sealed")
e$`_meta_selector_add` <- `PlRExpr__meta_selector_add`(ptr)
e$`_meta_selector_and` <- `PlRExpr__meta_selector_and`(ptr)
e$`_meta_selector_sub` <- `PlRExpr__meta_selector_sub`(ptr)
e$`_meta_selector_xor` <- `PlRExpr__meta_selector_xor`(ptr)
e$`abs` <- `PlRExpr_abs`(ptr)
e$`add` <- `PlRExpr_add`(ptr)
e$`agg_groups` <- `PlRExpr_agg_groups`(ptr)
Expand Down
5 changes: 5 additions & 0 deletions R/expr-meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ expr_meta__selector_sub <- function(other) {
wrap()
}

expr_meta__selector_xor <- function(other) {
self$`_rexpr`$`_meta_selector_xor`(other$`_rexpr`) |>
wrap()
}

expr_meta__as_selector <- function() {
self$`_rexpr`$`_meta_as_selector`() |>
wrap()
Expand Down
19 changes: 19 additions & 0 deletions R/selectors.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ selector__and <- function(other) {
}
}

selector__xor <- function(other) {
if (is_column(other)) {
colname <- other$meta$output_name()
other <- cs$by_name(colname)
}
if (is_polars_selector(other)) {
wrap_to_selector(
self$meta$`_as_selector`()$meta$`_selector_xor`(other),
name = "xor",
parameters = list(
self = self,
other = other
)
)
} else {
self$as_expr()$xor(other)
}
}

selector__as_expr <- function() {
self$`_rexpr` |>
wrap()
Expand Down
6 changes: 6 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@ SEXP savvy_PlRExpr__meta_selector_sub__impl(SEXP self__, SEXP c_arg__other) {
return handle_result(res);
}

SEXP savvy_PlRExpr__meta_selector_xor__impl(SEXP self__, SEXP c_arg__other) {
SEXP res = savvy_PlRExpr__meta_selector_xor__ffi(self__, c_arg__other);
return handle_result(res);
}

SEXP savvy_PlRExpr_abs__impl(SEXP self__) {
SEXP res = savvy_PlRExpr_abs__ffi(self__);
return handle_result(res);
Expand Down Expand Up @@ -2636,6 +2641,7 @@ static const R_CallMethodDef CallEntries[] = {
{"savvy_PlRExpr__meta_selector_add__impl", (DL_FUNC) &savvy_PlRExpr__meta_selector_add__impl, 2},
{"savvy_PlRExpr__meta_selector_and__impl", (DL_FUNC) &savvy_PlRExpr__meta_selector_and__impl, 2},
{"savvy_PlRExpr__meta_selector_sub__impl", (DL_FUNC) &savvy_PlRExpr__meta_selector_sub__impl, 2},
{"savvy_PlRExpr__meta_selector_xor__impl", (DL_FUNC) &savvy_PlRExpr__meta_selector_xor__impl, 2},
{"savvy_PlRExpr_abs__impl", (DL_FUNC) &savvy_PlRExpr_abs__impl, 1},
{"savvy_PlRExpr_add__impl", (DL_FUNC) &savvy_PlRExpr_add__impl, 2},
{"savvy_PlRExpr_agg_groups__impl", (DL_FUNC) &savvy_PlRExpr_agg_groups__impl, 1},
Expand Down
1 change: 1 addition & 0 deletions src/rust/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ SEXP savvy_PlRExpr__meta_as_selector__ffi(SEXP self__);
SEXP savvy_PlRExpr__meta_selector_add__ffi(SEXP self__, SEXP c_arg__other);
SEXP savvy_PlRExpr__meta_selector_and__ffi(SEXP self__, SEXP c_arg__other);
SEXP savvy_PlRExpr__meta_selector_sub__ffi(SEXP self__, SEXP c_arg__other);
SEXP savvy_PlRExpr__meta_selector_xor__ffi(SEXP self__, SEXP c_arg__other);
SEXP savvy_PlRExpr_abs__ffi(SEXP self__);
SEXP savvy_PlRExpr_add__ffi(SEXP self__, SEXP c_arg__rhs);
SEXP savvy_PlRExpr_agg_groups__ffi(SEXP self__);
Expand Down
19 changes: 9 additions & 10 deletions src/rust/src/expr/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,15 @@ impl PlRExpr {
Ok(out.into())
}

// TODO: enable after polars update
// fn meta_selector_xor(&self, other: &PlRExpr) -> Result<Self> {
// let out = self
// .inner
// .clone()
// .meta()
// ._selector_xor(other.inner.clone())
// .map_err(RPolarsErr::from)?;
// Ok(out.into())
// }
fn _meta_selector_xor(&self, other: &PlRExpr) -> Result<Self> {
let out = self
.inner
.clone()
.meta()
._selector_xor(other.inner.clone())
.map_err(RPolarsErr::from)?;
Ok(out.into())
}

fn _meta_as_selector(&self) -> Result<Self> {
Ok(self.inner.clone().meta()._into_selector().into())
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-selectors.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ test_that("'minus' operator works", {
)
})

test_that("'xor' operator works", {
df <- pl$DataFrame(foo = "a", bar = "b", foo3 = 3)
expect_named(
df$select(cs$string()$xor(cs$contains("foo"))),
c("bar", "foo3")
)
})

test_that("can use selectors in expressions", {
df <- pl$DataFrame(
dt = as.Date(c("1999-12-31", "2024-1-1", "2010-7-5")),
Expand Down
Loading