Skip to content

Commit a54eca1

Browse files
committed
chore: Use built-in rfuns extension
1 parent 7bb5817 commit a54eca1

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

Diff for: R/meta.R

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
pre_code_cache <- collections::queue()
22
code_cache <- collections::queue()
3+
ext_cache <- collections::dict()
34
macro_cache <- collections::dict()
45
df_cache <- collections::dict()
56
rel_cache <- collections::dict()
67

78
meta_clear <- function() {
89
pre_code_cache$clear()
910
code_cache$clear()
11+
ext_cache$clear()
1012
macro_cache$clear()
1113
df_cache$clear()
1214
rel_cache$clear()
@@ -26,7 +28,8 @@ meta_replay <- function(add_pre_code = TRUE) {
2628
if (add_pre_code) {
2729
con_exprs <- list(
2830
expr(duckdb <- asNamespace("duckdb")),
29-
expr(con <- DBI::dbConnect(duckdb::duckdb())),
31+
expr(drv <- duckdb::duckdb()),
32+
expr(con <- DBI::dbConnect(drv)),
3033
expr(experimental <- !!(Sys.getenv("DUCKPLYR_EXPERIMENTAL") == "TRUE"))
3134
)
3235
con_code <- map(con_exprs, constructive::deparse_call)
@@ -100,6 +103,22 @@ meta_eval <- function() {
100103
eval(parse(text = code))
101104
}
102105

106+
meta_ext_register <- function(name = "rfuns") {
107+
if (ext_cache$has(name)) {
108+
return(invisible())
109+
}
110+
111+
stopifnot(identical(name, "rfuns"))
112+
113+
ext_install_expr <- expr(invisible(
114+
duckdb$rapi_load_rfuns(drv@database_ref)
115+
))
116+
meta_pre_record(constructive::deparse_call(ext_install_expr))
117+
118+
ext_cache$set(name, TRUE)
119+
invisible()
120+
}
121+
103122
meta_macro_register <- function(name) {
104123
macro <- duckplyr_macros[name]
105124
if (is.na(macro)) {
@@ -110,6 +129,12 @@ meta_macro_register <- function(name) {
110129
return(invisible())
111130
}
112131

132+
# Register functions from the rfuns extension
133+
# Can't use '^"r_' because of the way the macro is defined
134+
if (grepl('"r_', macro)) {
135+
meta_ext_register()
136+
}
137+
113138
macro_expr <- expr(invisible(
114139
DBI::dbExecute(con, !!paste0('CREATE MACRO "', names(macro), '"', macro))
115140
))

Diff for: R/relational-duckdb.R

+10-7
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ get_default_duckdb_connection <- function() {
1515
}
1616

1717
duckplyr_macros <- c(
18-
"<" = "(x, y) AS x < y",
19-
"<=" = "(x, y) AS x <= y",
20-
">" = "(x, y) AS x > y",
21-
">=" = "(x, y) AS x >= y",
22-
"==" = "(x, y) AS x = y",
23-
"!=" = "(x, y) AS x <> y",
18+
"<" = '(x, y) AS "r_base::<"(x, y)',
19+
"<=" = '(x, y) AS "r_base::<="(x, y)',
20+
">" = '(x, y) AS "r_base::>"(x, y)',
21+
">=" = '(x, y) AS "r_base::>="(x, y)',
22+
"==" = '(x, y) AS "r_base::=="(x, y)',
23+
"!=" = '(x, y) AS "r_base::!="(x, y)',
2424
#
2525
"___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",
2626
#
@@ -56,11 +56,14 @@ duckplyr_macros <- c(
5656
)
5757

5858
create_default_duckdb_connection <- function() {
59-
con <- DBI::dbConnect(duckdb::duckdb())
59+
drv <- duckdb::duckdb()
60+
con <- DBI::dbConnect(drv)
6061

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

65+
duckdb$rapi_load_rfuns(drv@database_ref)
66+
6467
for (i in seq_along(duckplyr_macros)) {
6568
sql <- paste0('CREATE MACRO "', names(duckplyr_macros)[[i]], '"', duckplyr_macros[[i]])
6669
DBI::dbExecute(con, sql)

0 commit comments

Comments
 (0)