diff --git a/NEWS.md b/NEWS.md index d61372cac..95daf05e4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,13 +1,11 @@ # dbplyr (development version) -## Minor improvements and bug fixes +* `lead()` translation coerces `n` to an integer. * `sql_translator()` now checks for duplicated definitions (@krlmlr, #1374). * `reframe()` now gives an informative error that it isn't supported (#1148). -## Backend specific improvements - * MySQL/MariaDB: * Fix translation of `as.integer()` for MySQL (@krlmlr, #1375). * New `simulate_mariadb()` (@krlmlr, #1375). diff --git a/R/backend-.R b/R/backend-.R index d7481e6f7..2fb679427 100644 --- a/R/backend-.R +++ b/R/backend-.R @@ -446,7 +446,7 @@ base_win <- sql_translator( lead = function(x, n = 1L, default = NA, order_by = NULL) { win_over( - sql_expr(LEAD(!!x, !!n, !!default)), + sql_expr(LEAD(!!x, !!as.integer(n), !!default)), win_current_group(), order_by %||% win_current_order(), win_current_frame() diff --git a/R/backend-redshift.R b/R/backend-redshift.R index 0b0200224..735085ebb 100644 --- a/R/backend-redshift.R +++ b/R/backend-redshift.R @@ -81,7 +81,7 @@ sql_translation.RedshiftConnection <- function(con) { # https://docs.aws.amazon.com/redshift/latest/dg/r_WF_LEAD.html lead = function(x, n = 1L, order_by = NULL) { win_over( - sql_expr(LEAD(!!x, !!n)), + sql_expr(LEAD(!!x, !!as.integer(n))), win_current_group(), order_by %||% win_current_order(), win_current_frame() diff --git a/tests/testthat/test-backend-.R b/tests/testthat/test-backend-.R index eeb208dac..db1808f28 100644 --- a/tests/testthat/test-backend-.R +++ b/tests/testthat/test-backend-.R @@ -78,6 +78,15 @@ test_that("can translate subsetting", { }) +# window ------------------------------------------------------------------ + +test_that("lead and lag translate n to integers", { + local_con(simulate_dbi()) + + expect_equal(test_translate_sql(lead(x, 1)), sql("LEAD(`x`, 1, NULL) OVER ()")) + expect_equal(test_translate_sql(lag(x, 1)), sql("LAG(`x`, 1, NULL) OVER ()")) +}) + # strings ----------------------------------------------------------------- test_that("can translate case insensitive like", {