Skip to content

Commit

Permalink
Coerce n to integer in lead too (#1383)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Oct 27, 2023
1 parent f83e9f0 commit 93e8bcb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
4 changes: 1 addition & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
2 changes: 1 addition & 1 deletion R/backend-.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion R/backend-redshift.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-backend-.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 93e8bcb

Please sign in to comment.