generated from duckdb/extension-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f3073e8
initial r_base::sum() passthrough
romainfrancois 4c395f3
handle sum(na.rm = FALSE)
romainfrancois 74fcc49
some comments
romainfrancois e711e79
can't use duckdb version
romainfrancois c2b303e
BindRSum_dispatch
romainfrancois 76c9be3
tests
romainfrancois 115cce8
more tests
romainfrancois f3861e4
sum(<BOOLEAN>)
romainfrancois File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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][] | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
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; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 isTRUE
go through the data and if we find a null, then bind to a function that returns a null somehow.There was a problem hiding this comment.
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.