Skip to content

Commit

Permalink
Merge pull request #212 from duckdb/f-sql
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Aug 17, 2024
2 parents 33f2e41 + 612bfe4 commit 74e50cc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ rapi_rel_set_symdiff <- function(rel_a, rel_b) {
.Call(`_duckdb_rapi_rel_set_symdiff`, rel_a, rel_b)
}

rapi_rel_from_sql <- function(con, sql) {
.Call(`_duckdb_rapi_rel_from_sql`, con, sql)
}

rapi_rel_from_table <- function(con, schema_name, table_name) {
.Call(`_duckdb_rapi_rel_from_table`, con, schema_name, table_name)
}
Expand Down
18 changes: 15 additions & 3 deletions R/relational.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ rel_aggregate <- rapi_rel_aggregate
#' Lazily reorder a DuckDB relation object
#' @param rel the DuckDB relation object
#' @param orders a list of DuckDB expressions to order by
#' @param ascending a vector of boolean values describing sort order of expressions. True for ascending.
#' @param ascending a vector of boolean values describing sort order of expressions. True for ascending.
#' @return the now aggregated `duckdb_relation` object
#' @noRd
#' @examples
Expand All @@ -163,7 +163,7 @@ rel_order <- function(rel, orders, ascending = NULL) {
if (length(orders) != length(ascending)) {
stop("length of ascending must equal length of orders")
}

return(rapi_rel_order(rel, orders, ascending))
}

Expand Down Expand Up @@ -385,6 +385,18 @@ rel_to_sql <- rapi_rel_to_sql



#' Create a duckdb table relation from a table name
#' @param sql An SQL query
#' @return a duckdb relation
#' @noRd
#' @examples
#' con <- DBI::dbConnect(duckdb())
#' DBI::dbWriteTable(con, "mtcars", mtcars)
#' rel <- rel_from_sql(con, "SELECT * FROM mtcars")
rel_from_sql <- function(con, sql) {
rapi_rel_from_sql(con@conn_ref, sql)
}

#' Create a duckdb table relation from a table name
#' @param table the table name
#' @return a duckdb relation
Expand All @@ -393,7 +405,7 @@ rel_to_sql <- rapi_rel_to_sql
#' con <- DBI::dbConnect(duckdb())
#' DBI::dbWriteTable(con, "mtcars", mtcars)
#' rel <- rel_from_table(con, "mtcars")
rel_from_table <- function(con, table_name, schema_name="MAIN") {
rel_from_table <- function(con, table_name, schema_name = "MAIN") {
rapi_rel_from_table(con@conn_ref, schema_name, table_name)
}

Expand Down
8 changes: 8 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ extern "C" SEXP _duckdb_rapi_rel_set_symdiff(SEXP rel_a, SEXP rel_b) {
END_CPP11
}
// relational.cpp
SEXP rapi_rel_from_sql(duckdb::conn_eptr_t con, const std::string sql);
extern "C" SEXP _duckdb_rapi_rel_from_sql(SEXP con, SEXP sql) {
BEGIN_CPP11
return cpp11::as_sexp(rapi_rel_from_sql(cpp11::as_cpp<cpp11::decay_t<duckdb::conn_eptr_t>>(con), cpp11::as_cpp<cpp11::decay_t<const std::string>>(sql)));
END_CPP11
}
// relational.cpp
SEXP rapi_rel_from_table(duckdb::conn_eptr_t con, const std::string schema_name, const std::string table_name);
extern "C" SEXP _duckdb_rapi_rel_from_table(SEXP con, SEXP schema_name, SEXP table_name) {
BEGIN_CPP11
Expand Down Expand Up @@ -451,6 +458,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_duckdb_rapi_rel_filter", (DL_FUNC) &_duckdb_rapi_rel_filter, 2},
{"_duckdb_rapi_rel_from_altrep_df", (DL_FUNC) &_duckdb_rapi_rel_from_altrep_df, 3},
{"_duckdb_rapi_rel_from_df", (DL_FUNC) &_duckdb_rapi_rel_from_df, 3},
{"_duckdb_rapi_rel_from_sql", (DL_FUNC) &_duckdb_rapi_rel_from_sql, 2},
{"_duckdb_rapi_rel_from_table", (DL_FUNC) &_duckdb_rapi_rel_from_table, 3},
{"_duckdb_rapi_rel_from_table_function", (DL_FUNC) &_duckdb_rapi_rel_from_table_function, 4},
{"_duckdb_rapi_rel_join", (DL_FUNC) &_duckdb_rapi_rel_join, 5},
Expand Down
10 changes: 9 additions & 1 deletion src/relational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,20 @@ static SEXP result_to_df(duckdb::unique_ptr<QueryResult> res) {
return make_external_prot<RelationWrapper>("duckdb_relation", prot, symdiff);
}

[[cpp11::register]] SEXP rapi_rel_from_sql(duckdb::conn_eptr_t con, const std::string sql) {
if (!con || !con.get() || !con->conn) {
stop("rel_from_table: Invalid connection");
}
auto rel = con->conn->RelationFromQuery(sql);
cpp11::writable::list prot = {};
return make_external_prot<RelationWrapper>("duckdb_relation", prot, std::move(rel));
}

[[cpp11::register]] SEXP rapi_rel_from_table(duckdb::conn_eptr_t con, const std::string schema_name,
const std::string table_name) {
if (!con || !con.get() || !con->conn) {
stop("rel_from_table: Invalid connection");
}
auto desc = make_uniq<TableDescription>();
auto rel = con->conn->Table(schema_name, table_name);
cpp11::writable::list prot = {};
return make_external_prot<RelationWrapper>("duckdb_relation", prot, std::move(rel));
Expand Down

0 comments on commit 74e50cc

Please sign in to comment.