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

r_base::sum() handling na.rm = #75

Merged
merged 8 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
set(EXTENSION_SOURCES src/rfuns_extension.cpp
src/add.cpp
src/relop.cpp
src/dispatch.cpp)
src/dispatch.cpp
src/sum.cpp)

build_static_extension(${TARGET_NAME} ${EXTENSION_SOURCES})
build_loadable_extension(${TARGET_NAME} " " ${EXTENSION_SOURCES})
Expand Down
22 changes: 22 additions & 0 deletions duckdb-rfuns-r/R/aggregate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
rfuns_sum <- function(x, na.rm = TRUE) {
con <- local_duckdb_con()

in_df <- tibble::tibble(x = x)
in_rel <- duckdb:::rel_from_df(con, in_df)

exprs <- list(
duckdb:::expr_function(
"r_base::sum",
list(
duckdb:::expr_reference("x"),
duckdb:::expr_constant(TRUE)
)
)
)

agg <- duckdb:::rel_aggregate(in_rel, list(), exprs)

withr::with_options(list(duckdb.materialize_message = FALSE), {
duckdb:::rel_to_altrep(agg)[, 1][]
})
}
8 changes: 8 additions & 0 deletions duckdb-rfuns-r/tests/testthat/_snaps/sum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# r_base::sum(<VARCHAR>

Code
rfuns_sum("HufflePuff")
Condition
Error:
! {"exception_type":"Binder","exception_message":"No function matches the given name and argument types 'r_base::sum(VARCHAR, BOOLEAN)'. You might need to add explicit type casts.\n\tCandidate functions:\n\tr_base::sum(INTEGER, BOOLEAN) -> INTEGER\n\tr_base::sum(DOUBLE, BOOLEAN) -> DOUBLE\n","name":"r_base::sum","candidates":"r_base::sum(INTEGER, BOOLEAN) -> INTEGER,r_base::sum(DOUBLE, BOOLEAN) -> DOUBLE","call":"r_base::sum(VARCHAR, BOOLEAN)","error_subtype":"NO_MATCHING_FUNCTION"}

24 changes: 24 additions & 0 deletions duckdb-rfuns-r/tests/testthat/test-sum.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
test_that("r_base::sum(<INTEGER>)", {
expect_equal(rfuns_sum(1:10), 55)
expect_equal(rfuns_sum(c(1:10, NA)), 55)

expect_equal(rfuns_sum(c(1:10, NA), na.rm = TRUE), 55)

# TODO: should be NA
expect_equal(rfuns_sum(c(1:10, NA), na.rm = FALSE), 55)
})


test_that("r_base::sum(<DOUBLE>)", {
expect_equal(rfuns_sum(c(1.1, 2.2, 3.3)), 6.6)
expect_equal(rfuns_sum(c(1.1, 2.2, 3.3, NA)), 6.6)

expect_equal(rfuns_sum(c(1.1, 2.2, 3.3, NA), na.rm = TRUE), 6.6)

# TODO: should be NA
expect_equal(rfuns_sum(c(1.1, 2.2, 3.3, NA), na.rm = FALSE), 6.6)
})

test_that("r_base::sum(<VARCHAR>", {
expect_snapshot(error = TRUE, rfuns_sum("HufflePuff"))
})
3 changes: 3 additions & 0 deletions src/include/rfuns_extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ ScalarFunctionSet base_r_lte();
ScalarFunctionSet base_r_gt();
ScalarFunctionSet base_r_gte();

// sum
AggregateFunctionSet base_r_sum();

ScalarFunctionSet binary_dispatch(ScalarFunctionSet fn) ;

} // namespace rfuns
Expand Down
3 changes: 3 additions & 0 deletions src/rfuns_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ static void register_rfuns(DatabaseInstance &instance) {
register_binary(instance, base_r_lte());
register_binary(instance, base_r_gt());
register_binary(instance, base_r_gte());

// sum
ExtensionUtil::RegisterFunction(instance, base_r_sum());
}
} // namespace rfuns

Expand Down
36 changes: 36 additions & 0 deletions src/sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "rfuns_extension.hpp"

#include "duckdb/parser/parsed_data/create_aggregate_function_info.hpp"

#include <math.h>
#include <climits>
#include "duckdb/core_functions/aggregate/sum_helpers.hpp"
#include "duckdb/core_functions/aggregate/distributive_functions.hpp"

namespace duckdb {
namespace rfuns {

unique_ptr<FunctionData> BindRSum(ClientContext &context, AggregateFunction &function, vector<unique_ptr<Expression>> &arguments) {
function = SumFun::GetFunctions().GetFunctionByArguments(context, {arguments[0]->return_type});
return nullptr;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: handle arguments[1] here, i.e. if it is TRUE go through the data and if we find a null, then bind to a function that returns a null somehow.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can assume a constant here, duckplyr won't let anything else through.


AggregateFunction RSum(const LogicalType& type) {
return AggregateFunction(
{type, LogicalType::BOOLEAN}, type,
nullptr, nullptr, nullptr, nullptr, nullptr, FunctionNullHandling::DEFAULT_NULL_HANDLING, nullptr,
BindRSum
);
}

AggregateFunctionSet base_r_sum() {
AggregateFunctionSet set("r_base::sum");

set.AddFunction(RSum(LogicalType::INTEGER));
set.AddFunction(RSum(LogicalType::DOUBLE));

return set;
}

}
}
Loading