Skip to content

Commit

Permalink
add comparison expression to relational api
Browse files Browse the repository at this point in the history
  • Loading branch information
toppyy authored and krlmlr committed Oct 15, 2024
1 parent 9007b30 commit 4552bbd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ rapi_expr_constant <- function(val) {
.Call(`_duckdb_rapi_expr_constant`, val)
}

rapi_expr_comparison <- function(exprs, cmp_op) {
.Call(`_duckdb_rapi_expr_comparison`, exprs, cmp_op)
}

rapi_expr_function <- function(name, args, order_bys, filter_bys) {
.Call(`_duckdb_rapi_expr_function`, name, args, order_bys, filter_bys)
}
Expand Down
9 changes: 9 additions & 0 deletions R/relational.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ expr_reference <- function(names, table = NULL) {
#' const_str_expr <- expr_constant("Hello, World")
expr_constant <- rapi_expr_constant

#' Create a comparison expression
#' @param exprs a vector of size two, the expressions to compare
#' @param cmp_op the comparison operators
#' @return a comparison expression
#' @noRd
#' @examples
#' comp_expr <- expr_comparison(list(expr_constant(-42), expr_constant(42)), ">")
expr_comparison <- rapi_expr_comparison

#' Create a function call expression
#' @param name the function name
#' @param args the a list of expressions for the function arguments
Expand Down
8 changes: 8 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ extern "C" SEXP _duckdb_rapi_expr_constant(SEXP val) {
END_CPP11
}
// relational.cpp
SEXP rapi_expr_comparison(list exprs, std::string cmp_op);
extern "C" SEXP _duckdb_rapi_expr_comparison(SEXP exprs, SEXP cmp_op) {
BEGIN_CPP11
return cpp11::as_sexp(rapi_expr_comparison(cpp11::as_cpp<cpp11::decay_t<list>>(exprs),cpp11::as_cpp<cpp11::decay_t<std::string>>(cmp_op)));
END_CPP11
}
// relational.cpp
SEXP rapi_expr_function(std::string name, list args, list order_bys, list filter_bys);
extern "C" SEXP _duckdb_rapi_expr_function(SEXP name, SEXP args, SEXP order_bys, SEXP filter_bys) {
BEGIN_CPP11
Expand Down Expand Up @@ -432,6 +439,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_duckdb_rapi_execute", (DL_FUNC) &_duckdb_rapi_execute, 3},
{"_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_comparison", (DL_FUNC) &_duckdb_rapi_expr_comparison, 2},
{"_duckdb_rapi_expr_function", (DL_FUNC) &_duckdb_rapi_expr_function, 4},
{"_duckdb_rapi_expr_reference", (DL_FUNC) &_duckdb_rapi_expr_reference, 1},
{"_duckdb_rapi_expr_set_alias", (DL_FUNC) &_duckdb_rapi_expr_set_alias, 2},
Expand Down
16 changes: 16 additions & 0 deletions src/relational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "duckdb/parser/expression/columnref_expression.hpp"
#include "duckdb/parser/expression/constant_expression.hpp"
#include "duckdb/parser/expression/comparison_expression.hpp"
#include "duckdb/parser/expression/function_expression.hpp"
#include "duckdb/parser/expression/comparison_expression.hpp"
#include "duckdb/parser/expression/conjunction_expression.hpp"
Expand Down Expand Up @@ -67,6 +68,21 @@ external_pointer<T> make_external_prot(const string &rclass, SEXP prot, ARGS &&.
return make_external<ConstantExpression>("duckdb_expr", RApiTypes::SexpToValue(val, 0, false));
}

[[cpp11::register]] SEXP rapi_expr_comparison(list exprs, std::string cmp_op) {

ExpressionType expr_type = OperatorToExpressionType(cmp_op);
if (expr_type ==ExpressionType::INVALID) {
stop("expr_comparison: Invalid comparison operator");
}

return make_external<ComparisonExpression>(
"duckdb_expr",
expr_type,
expr_extptr_t(exprs[0])->Copy(),
expr_extptr_t(exprs[1])->Copy()
);
}

[[cpp11::register]] SEXP rapi_expr_function(std::string name, list args, list order_bys, list filter_bys) {
if (name.size() == 0) {
stop("expr_function: Zero length name");
Expand Down

0 comments on commit 4552bbd

Please sign in to comment.