Skip to content

Commit

Permalink
Fixed translation of DATEADD() for DuckDB when number to add is an …
Browse files Browse the repository at this point in the history
…expression instead of a verbatim number. Fixes #340
  • Loading branch information
Schuemie authored and Schuemie committed Jun 22, 2023
1 parent 83941cd commit c409364
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
SqlRender 1.15.1
================

Bugfixes:

1. Fixed translation of `DATEADD()` for DuckDB when number to add is an expression instead of a verbatim number.


SqlRender 1.15.0
================

Expand Down
24 changes: 12 additions & 12 deletions inst/csv/replacementPatterns.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1175,18 +1175,18 @@ duckdb,TRY_CAST(@a),CAST(@a)
duckdb,"IIF(@condition, @whentrue, @whenfalse)","CASE WHEN @condition THEN @whentrue ELSE @whenfalse END"
duckdb,"ROUND(@a,@b)","ROUND(CAST(@a AS NUMERIC),@b)"
duckdb,"HASHBYTES('MD5',@a)","MD5(@a)"
duckdb,"DATEADD(second,@seconds,@datetime)",(@datetime + INTERVAL'@seconds second')
duckdb,"DATEADD(minute,@minutes,@datetime)",(@datetime + INTERVAL'@minutes minute')
duckdb,"DATEADD(hour,@hours,@datetime)",(@datetime + INTERVAL'@hours hour')
duckdb,"DATEADD(d,@days,@date)",(@date + INTERVAL'@days day')
duckdb,"DATEADD(dd,@days,@date)",(@date + INTERVAL'@days day')
duckdb,"DATEADD(day,@days,@date)",(@date + INTERVAL'@days day')
duckdb,"DATEADD(m,@months,@date)",(@date + INTERVAL'@months month')
duckdb,"DATEADD(mm,@months,@date)",(@date + INTERVAL'@months month')
duckdb,"DATEADD(month,@months,@date)",(@date + INTERVAL'@months month')
duckdb,"DATEADD(yy,@years,@date)",(@date + INTERVAL'@years year')
duckdb,"DATEADD(yyyy,@years,@date)",(@date + INTERVAL'@years year')
duckdb,"DATEADD(year,@years,@date)",(@date + INTERVAL'@years year')
duckdb,"DATEADD(second,@seconds,@datetime)",(@datetime + TO_SECONDS(@seconds))
duckdb,"DATEADD(minute,@minutes,@datetime)",(@datetime + TO_MINUTES(@minutes))
duckdb,"DATEADD(hour,@hours,@datetime)",(@datetime + TO_HOURS(@hours))
duckdb,"DATEADD(d,@days,@date)",(@date + TO_DAYS(@days))
duckdb,"DATEADD(dd,@days,@date)",(@date + TO_DAYS(@days))
duckdb,"DATEADD(day,@days,@date)",(@date + TO_DAYS(@days))
duckdb,"DATEADD(m,@months,@date)",(@date + TO_MONTHS(@months))
duckdb,"DATEADD(mm,@months,@date)",(@date + TO_MONTHS(@months))
duckdb,"DATEADD(month,@months,@date)",(@date + TO_MONTHS(@months))
duckdb,"DATEADD(yy,@years,@date)",(@date + TO_YEAR(@years))
duckdb,"DATEADD(yyyy,@years,@date)",(@date + TO_YEAR(@years))
duckdb,"DATEADD(year,@years,@date)",(@date + TO_YEAR(@years))
duckdb,INTERVAL'@(-?[0-9]+)a.0 @b',INTERVAL'@a @b'
duckdb,"DATEDIFF(d,@start, @end)","(CONVERT(DATE, @end) - CAST(@start AS DATE))"
duckdb,"DATEDIFF(dd,@start, @end)",(CAST(@end AS DATE) - CAST(@start AS DATE))
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-translate-duckdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test_that("translate sql server -> DuckDB add months", {
sql <- translate("DATEADD(mm,2,date)", targetDialect = "duckdb")
expect_equal_ignore_spaces(
sql,
"(date + INTERVAL'2 month')"
"(date + TO_MONTHS(2))"
)
})

Expand Down Expand Up @@ -224,6 +224,6 @@ test_that("translate sql server -> DuckDB add days with period", {
sql <- translate("DATEADD(DAY, -2.0, date)", targetDialect = "duckdb")
expect_equal_ignore_spaces(
sql,
"(date + INTERVAL'-2 day')"
"(date + TO_DAYS(-2.0))"
)
})

0 comments on commit c409364

Please sign in to comment.