Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added datediff() and dateadd() for hours/minutes/seconds for Spark, Oracle, PostGres, RedShift #347

Prev Previous commit
Next Next commit
Add datediff translations for postGres
TomWhite-MedStar committed Oct 2, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit bef6747d70010d4be26034f3e2f83c5f0af79896
3 changes: 3 additions & 0 deletions inst/csv/replacementPatterns.csv
Original file line number Diff line number Diff line change
@@ -174,6 +174,9 @@ postgresql,"DATEADD(month,@months,@date)",(@date + @months*INTERVAL'1 month')
postgresql,"DATEADD(yy,@years,@date)",(@date + @years*INTERVAL'1 year')
postgresql,"DATEADD(yyyy,@years,@date)",(@date + @years*INTERVAL'1 year')
postgresql,"DATEADD(year,@years,@date)",(@date + @years*INTERVAL'1 year')
postgresql,"DATEDIFF(second,@start, @end)",EXTRACT(EPOCH FROM (@end - @start))
postgresql,"DATEDIFF(minute,@start, @end)",(EXTRACT(EPOCH FROM (@end - @start)) / 60)
postgresql,"DATEDIFF(hour,@start, @end)",(EXTRACT(EPOCH FROM (@end - @start)) / 3600)
postgresql,"DATEDIFF(d,@start, @end)",(CAST(@end AS DATE) - CAST(@start AS DATE))
postgresql,"DATEDIFF(dd,@start, @end)",(CAST(@end AS DATE) - CAST(@start AS DATE))
postgresql,"DATEDIFF(day,@start, @end)",(CAST(@end AS DATE) - CAST(@start AS DATE))
17 changes: 17 additions & 0 deletions tests/testthat/test-translate-postgresql.R
Original file line number Diff line number Diff line change
@@ -49,6 +49,23 @@ test_that("translate sql server -> PostgreSQL date diff (month)", {
expect_equal_ignore_spaces(sql, "SELECT (extract(year from age(CAST(drug_era_end_date AS DATE), CAST(drug_era_start_date AS DATE)))*12 + extract(month from age(CAST(drug_era_end_date AS DATE), CAST(drug_era_start_date AS DATE)))) FROM drug_era;")
})

test_that("translate sql server -> PostgreSQL date diff (hour, minute, second)", {
sql <- translate("SELECT DATEDIFF(hour,drug_exposure_start_datetime,drug_exposure_end_datetime) FROM drug_exposure;",
targetDialect = "postgresql"
)
expect_equal_ignore_spaces(sql, "SELECT (EXTRACT(EPOCH FROM (drug_exposure_end_datetime - drug_exposure_start_datetime)) / 3600) FROM drug_exposure;")

sql <- translate("SELECT DATEDIFF(minute,drug_exposure_start_datetime,drug_exposure_end_datetime) FROM drug_exposure;",
targetDialect = "postgresql"
)
expect_equal_ignore_spaces(sql, "SELECT (EXTRACT(EPOCH FROM (drug_exposure_end_datetime - drug_exposure_start_datetime)) / 60) FROM drug_exposure;")

sql <- translate("SELECT DATEDIFF(second,drug_exposure_start_datetime,drug_exposure_end_datetime) FROM drug_exposure;",
targetDialect = "postgresql"
)
expect_equal_ignore_spaces(sql, "SELECT EXTRACT(EPOCH FROM (drug_exposure_end_datetime - drug_exposure_start_datetime)) FROM drug_exposure;")
})

test_that("translate sql server -> Postgres WITH SELECT", {
sql <- translate("WITH cte1 AS (SELECT a FROM b) SELECT c FROM cte1;",
targetDialect = "postgresql"