Skip to content

Commit

Permalink
Compatibility with R >= 4.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Dec 8, 2024
1 parent fdb662f commit c043baa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,20 @@ unique_ptr<TableRef> duckdb::EnvironmentScanReplacement(ClientContext &context,
auto table_name_symbol = cpp11::safe[Rf_install](input.table_name.c_str());
SEXP df;
SEXP rho = db_wrapper->env;
#if defined(R_VERSION) && R_VERSION >= R_Version(4, 5, 0)
df = cpp11::safe[R_getVarEx](table_name_symbol, rho, Rboolean::TRUE, R_NilValue);
#else
while(rho != R_EmptyEnv) {
df = cpp11::safe[Rf_findVarInFrame3](rho, table_name_symbol, TRUE);
if (df != R_UnboundValue) {
break;
}
rho = ENCLOS(rho);
}
if (!df) {
return nullptr;
}
if (TYPEOF(df) == PROMSXP) {
df = cpp11::safe[Rf_eval](df, rho);
}
#endif
if (!Rf_inherits(df, "data.frame")) {
return nullptr;
}
Expand All @@ -79,8 +80,7 @@ unique_ptr<TableRef> duckdb::EnvironmentScanReplacement(ClientContext &context,
// TODO: do utf conversion
auto table_function = make_uniq<TableFunctionRef>();
vector<duckdb::unique_ptr<ParsedExpression>> children;
children.push_back(
make_uniq<ConstantExpression>(Value::POINTER((uintptr_t)df)));
children.push_back(make_uniq<ConstantExpression>(Value::POINTER((uintptr_t)df)));
table_function->function = make_uniq<FunctionExpression>("r_dataframe_scan", std::move(children));
return std::move(table_function);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-scan.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ test_that("Data frame scan fetches from the correct environment", {
expect_equal(dbGetQuery(con, "FROM x"), x)
})

test_that("Function hides data frame", {
con <- dbConnect(duckdb(environment_scan = TRUE))
on.exit(dbDisconnect(con))

x <- data.frame(a = 1)

fun <- function() {
x <- function() {}
dbGetQuery(con, "FROM x")
}

expect_error(fun())
expect_equal(dbGetQuery(con, "FROM x"), x)
})

test_that("Database tables take precedence", {
con <- dbConnect(duckdb(environment_scan = TRUE))
on.exit(dbDisconnect(con))
Expand Down

0 comments on commit c043baa

Please sign in to comment.