Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# dbplyr (development version)

* `sql_translator()` now checks for duplicated definitions (@krlmlr, #1374).

# dbplyr 2.4.0

## Breaking changes
Expand Down
8 changes: 3 additions & 5 deletions R/backend-.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ base_scalar <- sql_translator(
}
},
log10 = sql_prefix("LOG10", 1),
round = sql_prefix("ROUND", 2),
round = function(x, digits = 0L) {
sql_expr(ROUND(!!x, !!as.integer(digits)))
},
sign = sql_prefix("SIGN", 1),
sin = sql_prefix("SIN", 1),
sqrt = sql_prefix("SQRT", 1),
Expand All @@ -152,10 +154,6 @@ base_scalar <- sql_translator(
tanh = function(x) sql_expr((!!sql_exp(2, x) - 1L) / (!!sql_exp(2, x) + 1L)),
coth = function(x) sql_expr((!!sql_exp(2, x) + 1L) / (!!sql_exp(2, x) - 1L)),

round = function(x, digits = 0L) {
sql_expr(ROUND(!!x, !!as.integer(digits)))
},

`if` = function(cond, if_true, if_false = NULL) {
sql_if(enquo(cond), enquo(if_true), enquo(if_false))
},
Expand Down
8 changes: 8 additions & 0 deletions R/translate-sql-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ sql_translator <- function(...,
funs <- c(list2(...), .funs)
if (length(funs) == 0) return(.parent)

if (anyDuplicated(names(funs))) {
bullets <- unique(names(funs)[duplicated(names(funs))])
cli_abort(c(
"Duplicate names in {.fun sql_translator}",
set_names(bullets, "*")
))
}

list2env(funs, copy_env(.parent))
}

Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/translate-sql-helpers.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# duplicates throw an error

Code
sql_translator(round = function(x) x, round = function(y) y)
Condition
Error in `sql_translator()`:
! Duplicate names in `sql_translator()`
* round

# output of print method for sql_variant is correct

Code
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-translate-sql-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ test_that("missing window functions create a warning", {
)
})

test_that("duplicates throw an error", {
expect_snapshot(error = TRUE, {
sql_translator(round = function(x) x, round = function(y) y)
})
})

test_that("missing aggregate functions filled in", {
local_con(simulate_dbi())
sim_scalar <- sql_translator()
Expand Down