Skip to content

Commit

Permalink
Translatable n_distinct() can only work with single expression
Browse files Browse the repository at this point in the history
Fixes #101. Fixes #133.
  • Loading branch information
hadley committed Jan 10, 2019
1 parent 6cacb7a commit 1b58b68
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dbplyr (development version)

* Translation of `n_distinct()` now only supports a single argument
(#101, #133).

* Functions that are only available in a windowed (`mutate()`) query now
throw an error when called in a aggregate (`summarise()`) query (#129)

Expand Down
10 changes: 4 additions & 6 deletions R/translate-sql-base.r
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,8 @@ base_agg <- sql_translator(
# last = sql_prefix("LAST_VALUE", 1),
# nth = sql_prefix("NTH_VALUE", 2),

n_distinct = function(...) {
vars <- sql_vector(list(...), parens = FALSE, collapse = ", ")
build_sql("COUNT(DISTINCT ", vars, ")")
n_distinct = function(x) {
build_sql("COUNT(DISTINCT ", x, ")")
}
)

Expand Down Expand Up @@ -294,9 +293,8 @@ base_win <- sql_translator(
n = function() {
win_over(sql("COUNT(*)"), win_current_group())
},
n_distinct = function(...) {
vars <- sql_vector(list(...), parens = FALSE, collapse = ", ")
win_over(build_sql("COUNT(DISTINCT ", vars, ")"), win_current_group())
n_distinct = function(x) {
win_over(build_sql("COUNT(DISTINCT ", x, ")"), win_current_group())
},

# Cumulative function are like recycled aggregates except that R names
Expand Down
6 changes: 0 additions & 6 deletions tests/testthat/test-translate-odbc.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ test_that("custom aggregators translated correctly", {
expect_equal(trans(sd(x)), sql("STDDEV_SAMP(`x`)"))
expect_equal(trans(count()), sql("COUNT(*)"))
expect_equal(trans(n()), sql("COUNT(*)"))
expect_equal(trans(n_distinct(x)), sql("COUNT(DISTINCT `x`)"))


})

test_that("custom window functions translated correctly", {
Expand All @@ -35,9 +32,6 @@ test_that("custom window functions translated correctly", {
expect_equal(trans(sd(x, na.rm = TRUE)), sql("STDDEV_SAMP(`x`) OVER ()"))
expect_equal(trans(count()), sql("COUNT(*) OVER ()"))
expect_equal(trans(n()), sql("COUNT(*) OVER ()"))
expect_equal(trans(n_distinct(x)), sql("COUNT(DISTINCT `x`) OVER ()"))


})

test_that("queries translate correctly", {
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/test-translate.r
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ test_that("%in% with empty vector", {
expect_equal(translate_sql(x %in% !!integer()), sql('FALSE'))
})

test_that("n_distinct can take multiple values", {
test_that("n_distinct(x) translated to COUNT(distinct, x)", {
expect_equal(
translate_sql(n_distinct(x), window = FALSE),
sql('COUNT(DISTINCT "x")')
)
expect_equal(
translate_sql(n_distinct(x, y), window = FALSE),
sql('COUNT(DISTINCT "x", "y")')
translate_sql(n_distinct(x), window = TRUE),
sql('COUNT(DISTINCT "x") OVER ()')
)
expect_error(translate_sql(n_distinct(x, y), window = FALSE), "unused argument")
})

test_that("na_if is translated to NULLIF (#211)", {
Expand Down

0 comments on commit 1b58b68

Please sign in to comment.