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 list of strings for column references in R API #14

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
4 changes: 2 additions & 2 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ rapi_unregister_arrow <- function(conn, name) {
invisible(.Call(`_duckdb_rapi_unregister_arrow`, conn, name))
}

rapi_expr_reference <- function(name, table) {
.Call(`_duckdb_rapi_expr_reference`, name, table)
rapi_expr_reference <- function(rnames) {
.Call(`_duckdb_rapi_expr_reference`, rnames)
}

rapi_expr_constant <- function(val) {
Expand Down
11 changes: 7 additions & 4 deletions R/relational.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# expressions

#' Create a column reference expression
#' @param name the column name to be referenced
#' @param names the column name to be referenced, could be a list to refer to schema.table.column etc.
#' @param table the optional table name or a relation object to be referenced
#' @return a column reference expression
#' @noRd
#' @examples
#' col_ref_expr <- expr_reference("some_column_name")
#' col_ref_expr2 <- expr_reference("some_column_name", "some_table_name")
expr_reference <- function(name, table = "") {
expr_reference <- function(names, table = NULL) {
if (inherits(table, "duckdb_relation")) {
table <- rel_alias(table)
names <- c(rel_alias(table), names)
}
rapi_expr_reference(name, table)
if (is.character(table) && !identical(table, "")) {
names <- c(table, names)
}
rapi_expr_reference(names)
}

#' Create a constant expression
Expand Down
8 changes: 4 additions & 4 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ extern "C" SEXP _duckdb_rapi_unregister_arrow(SEXP conn, SEXP name) {
END_CPP11
}
// relational.cpp
SEXP rapi_expr_reference(std::string name, std::string table);
extern "C" SEXP _duckdb_rapi_expr_reference(SEXP name, SEXP table) {
SEXP rapi_expr_reference(r_vector<r_string> rnames);
extern "C" SEXP _duckdb_rapi_expr_reference(SEXP rnames) {
BEGIN_CPP11
return cpp11::as_sexp(rapi_expr_reference(cpp11::as_cpp<cpp11::decay_t<std::string>>(name), cpp11::as_cpp<cpp11::decay_t<std::string>>(table)));
return cpp11::as_sexp(rapi_expr_reference(cpp11::as_cpp<cpp11::decay_t<r_vector<r_string>>>(rnames)));
END_CPP11
}
// relational.cpp
Expand Down Expand Up @@ -381,7 +381,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_duckdb_rapi_execute_arrow", (DL_FUNC) &_duckdb_rapi_execute_arrow, 2},
{"_duckdb_rapi_expr_constant", (DL_FUNC) &_duckdb_rapi_expr_constant, 1},
{"_duckdb_rapi_expr_function", (DL_FUNC) &_duckdb_rapi_expr_function, 4},
{"_duckdb_rapi_expr_reference", (DL_FUNC) &_duckdb_rapi_expr_reference, 2},
{"_duckdb_rapi_expr_reference", (DL_FUNC) &_duckdb_rapi_expr_reference, 1},
{"_duckdb_rapi_expr_set_alias", (DL_FUNC) &_duckdb_rapi_expr_set_alias, 2},
{"_duckdb_rapi_expr_tostring", (DL_FUNC) &_duckdb_rapi_expr_tostring, 1},
{"_duckdb_rapi_expr_window", (DL_FUNC) &_duckdb_rapi_expr_window, 9},
Expand Down
21 changes: 11 additions & 10 deletions src/relational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ external_pointer<T> make_external_prot(const string &rclass, SEXP prot, ARGS &&.
}
// DuckDB Expressions

[[cpp11::register]] SEXP rapi_expr_reference(std::string name, std::string table) {
if (name.size() == 0) {
stop("expr_reference: Zero length name");
}
if (!table.empty()) {
auto res = make_external<ColumnRefExpression>("duckdb_expr", name, table);
res->alias = name; // TODO does this really make sense here?
return res;
} else {
return make_external<ColumnRefExpression>("duckdb_expr", name);
[[cpp11::register]] SEXP rapi_expr_reference(r_vector<r_string> rnames) {
if (rnames.size() == 0) {
stop("expr_reference: Zero length name vector");
}
duckdb::vector<std::string> names;
for (auto name : rnames) {
if (name.size() == 0) {
stop("expr_reference: Zero length name");
}
names.push_back(name);
}
return make_external<ColumnRefExpression>("duckdb_expr", names);;
}

[[cpp11::register]] SEXP rapi_expr_constant(sexp val) {
Expand Down
Loading