-
Notifications
You must be signed in to change notification settings - Fork 24
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: Implement n_distinct()
for multiple arguments using duckdb structs
#122
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
54a7ce0
implement n_distinct() for multiple arguments using duckdb structs
lschneiderbauer dee31f7
remove wrong `na.rm = TRUE` check: the implemented bevaviour coincide…
lschneiderbauer f7e1f65
more `n_distinct()` tests: check computation results
lschneiderbauer 5731d99
`n_distinct()`: simplify SQL by using duckdb's row() function and get…
lschneiderbauer 45ab3d2
replace `cli::cli_abort()` by `stop()`
lschneiderbauer 05f20b2
Strict
krlmlr eb0eeb4
compat: do not use pipe operator in tests
lschneiderbauer 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 |
---|---|---|
|
@@ -206,6 +206,9 @@ test_that("aggregators translated correctly", { | |
|
||
expect_equal(translate(str_flatten(x, ","), window = FALSE), sql(r"{STRING_AGG(x, ',')}")) | ||
expect_equal(translate(str_flatten(x, ","), window = TRUE), sql(r"{STRING_AGG(x, ',') OVER ()}")) | ||
|
||
expect_equal(translate(n_distinct(x), window = FALSE), sql(r"{COUNT(DISTINCT row(x))}")) | ||
expect_equal(translate(n_distinct(x), window = TRUE), sql(r"{COUNT(DISTINCT row(x)) OVER ()}")) | ||
}) | ||
|
||
test_that("two variable aggregates are translated correctly", { | ||
|
@@ -218,8 +221,54 @@ test_that("two variable aggregates are translated correctly", { | |
|
||
expect_equal(translate(cor(x, y), window = FALSE), sql(r"{CORR(x, y)}")) | ||
expect_equal(translate(cor(x, y), window = TRUE), sql(r"{CORR(x, y) OVER ()}")) | ||
|
||
expect_equal(translate(n_distinct(x, y), window = FALSE), sql(r"{COUNT(DISTINCT row(x, y))}")) | ||
expect_equal(translate(n_distinct(x, y), window = TRUE), sql(r"{COUNT(DISTINCT row(x, y)) OVER ()}")) | ||
}) | ||
|
||
test_that("n_distinct() computations are correct", { | ||
skip_if_no_R4() | ||
skip_if_not_installed("dplyr") | ||
skip_if_not_installed("dbplyr") | ||
con <- dbConnect(duckdb()) | ||
on.exit(dbDisconnect(con, shutdown = TRUE)) | ||
tbl <- dplyr::tbl | ||
summarize <- dplyr::summarize | ||
pull <- dplyr::pull | ||
|
||
duckdb_register(con, "df", data.frame(x = c(1, 1, 2, 2), y = c(1, 2, 2, 2))) | ||
duckdb_register(con, "df_na", data.frame(x = c(1, 1, 2, NA, NA), y = c(1, 2, NA, 2, NA))) | ||
|
||
df <- tbl(con, "df") | ||
df_na <- tbl(con, "df_na") | ||
|
||
expect_error( | ||
pull(summarize(df, n = n_distinct(x, na.rm = TRUE)), n) | ||
) | ||
|
||
# single column is working as usual | ||
expect_equal( | ||
pull(summarize(df, n = n_distinct(x)), n), | ||
2 | ||
) | ||
|
||
expect_equal( | ||
pull(summarize(df_na, n = n_distinct(x)), n), | ||
3 | ||
) | ||
|
||
# two columns return correct results | ||
expect_equal( | ||
pull(summarize(df, n = n_distinct(x, y)), n), | ||
3 | ||
) | ||
|
||
# two columns containing NAs return correct results | ||
expect_equal( | ||
pull(summarize(df_na, n = n_distinct(x, y)), n), | ||
5 | ||
) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to add a test that uses a window. |
||
|
||
|
||
|
||
|
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.
@mgirlich: Can you please help me review this part of the code?
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.
Basically looks good to me. This also works for diverse user input, e.g.
n_distinct(x, a / (b - 1))
orn_distinct(!!x)
.One minor thing: you might want to check for empty dots.