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

fix use polars without library(polars) #355

Merged
merged 8 commits into from
Aug 8, 2023
Merged
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Suggests:
arrow,
bench,
bit64,
callr,
data.table,
knitr,
lubridate,
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- New lazy function translated: `concat_str()` to concatenate several columns
into one (#349).
- New stat functions `pl$cov()`, `pl$rolling_cov()` `pl$corr()`, `pl$rolling_corr()` (#351).
- Fix bug to allow using polars without library(polars) (#355).

# polars 0.7.0

Expand Down
2 changes: 2 additions & 0 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ test_print_string <- function(s) invisible(.Call(wrap__test_print_string, s))

test_robj_to_expr <- function(robj) .Call(wrap__test_robj_to_expr, robj)

test_wrong_call_pl_lit <- function(robj) .Call(wrap__test_wrong_call_pl_lit, robj)

RPolarsErr <- new.env(parent = emptyenv())

RPolarsErr$new <- function() .Call(wrap__RPolarsErr__new)
Expand Down
2 changes: 1 addition & 1 deletion man/pl_corr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/pl_cov.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/pl_rolling_corr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/pl_rolling_cov.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/rust/src/rlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ fn test_robj_to_expr(robj: Robj) -> RResult<Expr> {
robj_to!(Expr, robj)
}

#[extendr]
fn test_wrong_call_pl_lit(robj: Robj) -> RResult<Robj> {
Ok(R!("pl$lit({{robj}})")?) // this call should have been polars::pl$lit(...
}

extendr_module! {
mod rlib;
fn concat_df;
Expand Down Expand Up @@ -302,4 +307,5 @@ extendr_module! {
fn test_robj_to_u32;
fn test_print_string;
fn test_robj_to_expr;
fn test_wrong_call_pl_lit;
}
12 changes: 6 additions & 6 deletions src/rust/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,18 +637,18 @@ fn internal_rust_wrap_e(robj: Robj, str_to_lit: bool) -> RResult<Robj> {
};
match robj.rtype() {
ExternalPtr if robj.inherits("Expr") => Ok(robj),
ExternalPtr if robj.inherits("WhenThen") | robj.inherits("WhenThenThen") => {
unpack(R!("polars:::result({{robj}}$otherwise(pl$lit(NULL)))"))
}
ExternalPtr if robj.inherits("WhenThen") | robj.inherits("WhenThenThen") => unpack(R!(
"polars:::result({{robj}}$otherwise(polars::pl$lit(NULL)))"
)),
ExternalPtr if robj.inherits("When") => {
rerr().plain("Cannot use a When-statement as Expr without a $then()")
}
_h @ Logicals | _h @ List | _h @ Doubles | _h @ Integers => {
unpack(R!("polars:::result(pl$lit({{robj}}))"))
unpack(R!("polars:::result(polars::pl$lit({{robj}}))"))
}
_ if str_to_lit => unpack(R!("polars:::result(pl$lit({{robj}}))")),
_ if str_to_lit => unpack(R!("polars:::result(polars::pl$lit({{robj}}))")),

_ => unpack(R!("polars:::result(pl$col({{robj}}))")),
_ => unpack(R!("polars:::result(polars::pl$col({{robj}}))")),
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-without_library_polars.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test_that("without library(polars)", {
# calling sort("mpg") triggers rust to call pl$lit() which will be available even though
# polars is not added to serach with search() library(polars)
skip_if_not_installed("callr")
# positive test:
# Will work because robj_to! now calls polars::pl$lit and polars::pl$col
expect_identical(
callr::r(\() {
polars::pl$DataFrame(mtcars)$sort("mpg")$to_list()
}),
polars::pl$DataFrame(mtcars)$sort("mpg")$to_list()
)

# Negative control:
# This will fail because test_wrong_call_pl_lit just uses pl$col and pl$lit
expect_false(
callr::r(\() polars:::test_wrong_call_pl_lit(42) |> polars:::is_ok())
)

# Positive-Negative control
# This works because library(polars) puts polars in search()
expect_true(polars:::test_wrong_call_pl_lit(42) |> polars:::is_ok())
})