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

chore: Use built-in rfuns extension to implement equality and inequality operators, improve translation for NA and %in% #148

Merged
merged 5 commits into from
May 5, 2024
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
27 changes: 26 additions & 1 deletion R/meta.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
pre_code_cache <- collections::queue()
code_cache <- collections::queue()
ext_cache <- collections::dict()
macro_cache <- collections::dict()
df_cache <- collections::dict()
rel_cache <- collections::dict()

meta_clear <- function() {
pre_code_cache$clear()
code_cache$clear()
ext_cache$clear()
macro_cache$clear()
df_cache$clear()
rel_cache$clear()
Expand All @@ -26,7 +28,8 @@ meta_replay <- function(add_pre_code = TRUE) {
if (add_pre_code) {
con_exprs <- list(
expr(duckdb <- asNamespace("duckdb")),
expr(con <- DBI::dbConnect(duckdb::duckdb())),
expr(drv <- duckdb::duckdb()),
expr(con <- DBI::dbConnect(drv)),
expr(experimental <- !!(Sys.getenv("DUCKPLYR_EXPERIMENTAL") == "TRUE"))
)
con_code <- map(con_exprs, constructive::deparse_call)
Expand Down Expand Up @@ -100,6 +103,22 @@ meta_eval <- function() {
eval(parse(text = code))
}

meta_ext_register <- function(name = "rfuns") {
if (ext_cache$has(name)) {
return(invisible())
}

stopifnot(identical(name, "rfuns"))

ext_install_expr <- expr(invisible(
duckdb$rapi_load_rfuns(drv@database_ref)
))
meta_pre_record(constructive::deparse_call(ext_install_expr))

ext_cache$set(name, TRUE)
invisible()
}

meta_macro_register <- function(name) {
macro <- duckplyr_macros[name]
if (is.na(macro)) {
Expand All @@ -110,6 +129,12 @@ meta_macro_register <- function(name) {
return(invisible())
}

# Register functions from the rfuns extension
# Can't use '^"r_' because of the way the macro is defined
if (grepl('"r_', macro)) {
meta_ext_register()
}

macro_expr <- expr(invisible(
DBI::dbExecute(con, !!paste0('CREATE MACRO "', names(macro), '"', macro))
))
Expand Down
20 changes: 13 additions & 7 deletions R/relational-duckdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ get_default_duckdb_connection <- function() {
}

duckplyr_macros <- c(
"<" = "(x, y) AS x < y",
"<=" = "(x, y) AS x <= y",
">" = "(x, y) AS x > y",
">=" = "(x, y) AS x >= y",
"==" = "(x, y) AS x = y",
"!=" = "(x, y) AS x <> y",
# https://github.com/duckdb/duckdb-r/pull/156
"___null" = "() AS CAST(NULL AS BOOLEAN)",

"<" = '(x, y) AS "r_base::<"(x, y)',
"<=" = '(x, y) AS "r_base::<="(x, y)',
">" = '(x, y) AS "r_base::>"(x, y)',
">=" = '(x, y) AS "r_base::>="(x, y)',
"==" = '(x, y) AS "r_base::=="(x, y)',
"!=" = '(x, y) AS "r_base::!="(x, y)',
#
"___divide" = "(x, y) AS CASE WHEN y = 0 THEN CASE WHEN x = 0 THEN CAST('NaN' AS double) WHEN x > 0 THEN CAST('+Infinity' AS double) ELSE CAST('-Infinity' AS double) END ELSE CAST(x AS double) / y END",
#
Expand Down Expand Up @@ -56,11 +59,14 @@ duckplyr_macros <- c(
)

create_default_duckdb_connection <- function() {
con <- DBI::dbConnect(duckdb::duckdb())
drv <- duckdb::duckdb()
con <- DBI::dbConnect(drv)

DBI::dbExecute(con, "set memory_limit='1GB'")
DBI::dbExecute(con, paste0("pragma temp_directory='", tempdir(), "'"))

duckdb$rapi_load_rfuns(drv@database_ref)

for (i in seq_along(duckplyr_macros)) {
sql <- paste0('CREATE MACRO "', names(duckplyr_macros)[[i]], '"', duckplyr_macros[[i]])
DBI::dbExecute(con, sql)
Expand Down
6 changes: 5 additions & 1 deletion R/relational.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ rel_translate <- function(

switch(typeof(expr),
character = ,
logical = ,
integer = ,
double = relexpr_constant(expr),
# https://github.com/duckdb/duckdb-r/pull/156
logical = if (is.na(expr)) relexpr_function("___null", list()) else relexpr_constant(expr),
#
symbol = {
if (as.character(expr) %in% names_forbidden) {
Expand Down Expand Up @@ -188,6 +189,9 @@ rel_translate <- function(
tryCatch(
{
values <- eval(expr[[3]], envir = env)
if (length(values) == 0) {
return(relexpr_constant(FALSE))
}
consts <- map(values, do_translate, in_window = in_window)
ops <- map(consts, list, do_translate(expr[[2]]))
cmp <- map(ops, relexpr_function, name = "___eq_na_matches_na")
Expand Down
78 changes: 78 additions & 0 deletions tests/testthat/test-as_duckplyr_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,32 @@ test_that("as_duckplyr_df() and mutate(c = NA_character_, d = c %in% NA_characte
})


test_that("as_duckplyr_df() and mutate(d = a %in% NULL)", {
# Data
test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3))

# Run
pre <- test_df %>% as_duckplyr_df() %>% mutate(d = a %in% NULL)
post <- test_df %>% mutate(d = a %in% NULL) %>% as_duckplyr_df()

# Compare
expect_identical(pre, post)
})


test_that("as_duckplyr_df() and mutate(d = a %in% integer())", {
# Data
test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3))

# Run
pre <- test_df %>% as_duckplyr_df() %>% mutate(d = a %in% integer())
post <- test_df %>% mutate(d = a %in% integer()) %>% as_duckplyr_df()

# Compare
expect_identical(pre, post)
})


test_that("as_duckplyr_df() and mutate(d = NA_real_, e = is.na(d))", {
# Data
test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3))
Expand Down Expand Up @@ -1625,6 +1651,58 @@ test_that("as_duckplyr_df() and mutate(c = .data$b)", {
expect_identical(pre, post)
})


test_that("as_duckplyr_df() and mutate(d = NA)", {
# Data
test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3))

# Run
pre <- test_df %>% as_duckplyr_df() %>% mutate(d = NA)
post <- test_df %>% mutate(d = NA) %>% as_duckplyr_df()

# Compare
expect_identical(pre, post)
})


test_that("as_duckplyr_df() and mutate(d = NA_integer_)", {
# Data
test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3))

# Run
pre <- test_df %>% as_duckplyr_df() %>% mutate(d = NA_integer_)
post <- test_df %>% mutate(d = NA_integer_) %>% as_duckplyr_df()

# Compare
expect_identical(pre, post)
})


test_that("as_duckplyr_df() and mutate(d = NA_real_)", {
# Data
test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3))

# Run
pre <- test_df %>% as_duckplyr_df() %>% mutate(d = NA_real_)
post <- test_df %>% mutate(d = NA_real_) %>% as_duckplyr_df()

# Compare
expect_identical(pre, post)
})


test_that("as_duckplyr_df() and mutate(d = NA_character_)", {
# Data
test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3))

# Run
pre <- test_df %>% as_duckplyr_df() %>% mutate(d = NA_character_)
post <- test_df %>% mutate(d = NA_character_) %>% as_duckplyr_df()

# Compare
expect_identical(pre, post)
})

test_that("as_duckplyr_df() and n_groups()", {
withr::local_envvar(DUCKPLYR_FORCE = "FALSE")

Expand Down
Loading
Loading