From acdd6f36ad44227f13214ddcfe8f17f93583a9cb Mon Sep 17 00:00:00 2001 From: schuemie Date: Tue, 4 Jun 2024 14:26:53 +0200 Subject: [PATCH] Changing Spark translation of `DATEADD()` from `DATE_ADD()` to `DATEADD()` as required by some but not all Databricks instances. Fixes #366 --- DESCRIPTION | 4 +- NEWS.md | 8 ++++ inst/csv/replacementPatterns.csv | 20 +++----- man/loadRenderTranslateSql.Rd | 4 +- pom.xml | 2 +- tests/testthat/test-translate-spark.R | 66 +++++++++++++-------------- 6 files changed, 53 insertions(+), 51 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3225272c..7c4e946c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: SqlRender Type: Package Title: Rendering Parameterized SQL and Translation to Dialects -Version: 1.18.0 -Date: 2024-05-30 +Version: 1.18.1 +Date: 2024-06-04 Authors@R: c( person("Martijn", "Schuemie", , "schuemie@ohdsi.org", role = c("aut", "cre")), person("Marc", "Suchard", role = c("aut")) diff --git a/NEWS.md b/NEWS.md index 544678bd..b47788a5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +SqlRender 1.18.1 +================ + +Changes: + +1. Changing Spark translation of `DATEADD()` from `DATE_ADD()` to `DATEADD()` as required by some but not all Databricks instances. + + SqlRender 1.18.0 ================ diff --git a/inst/csv/replacementPatterns.csv b/inst/csv/replacementPatterns.csv index 514f80bc..f2290f13 100644 --- a/inst/csv/replacementPatterns.csv +++ b/inst/csv/replacementPatterns.csv @@ -1090,19 +1090,13 @@ spark,"HASHBYTES('MD5',@a)","MD5(@a)" spark,"CONVERT(VARBINARY, CONCAT('0x', @a), 1)","CAST(CONCAT('x', @a) AS BIT(32))" spark,"CONVERT(DATE, @a)","TO_DATE(@a, 'yyyy-MM-dd')" spark,"DATEPART(@part, @date)","DATE_PART('@part', @date)" -spark,"DATEADD(second,@seconds,@datetime)","DATE_ADD(second,@seconds,@datetime)" -spark,"DATEADD(minute,@minutes,@datetime)","DATE_ADD(minute,@minutes,@datetime)" -spark,"DATEADD(hour,@hours,@datetime)","DATE_ADD(hour,@hours,@datetime)" -spark,"DATEADD(d,@days,@date)","DATE_ADD(day,@days,@date)" -spark,"DATEADD(dd,@days,@date)","DATE_ADD(day,@days,@date)" -spark,"DATEADD(day,@days,@date)","DATE_ADD(day,@days,@date)" -spark,"DATEADD(m,@months,@date)","DATE_ADD(month,@months,@date)" -spark,"DATEADD(mm,@months,@date)","DATE_ADD(month,@months,@date)" -spark,"DATEADD(month,@months,@date)","DATE_ADD(month,@months,@date)" -spark,"DATEADD(yy,@years,@date)","DATE_ADD(year,@years,@date)" -spark,"DATEADD(yyyy,@years,@date)","DATE_ADD(year,@years,@date)" -spark,"DATEADD(year,@years,@date)","DATE_ADD(year,@years,@date)" -spark,"DATE_ADD(@part,@(-?[0-9]+)a.0,@date)","DATE_ADD(@part,@a,@date)" +spark,"DATEADD(d,@days,@date)","DATEADD(day,@days,@date)" +spark,"DATEADD(dd,@days,@date)","DATEADD(day,@days,@date)" +spark,"DATEADD(m,@months,@date)","DATEADD(month,@months,@date)" +spark,"DATEADD(mm,@months,@date)","DATEADD(month,@months,@date)" +spark,"DATEADD(yy,@years,@date)","DATEADD(year,@years,@date)" +spark,"DATEADD(yyyy,@years,@date)","DATEADD(year,@years,@date)" +spark,"DATEADD(@part,@(-?[0-9]+)a.0,@date)","DATEADD(@part,@a,@date)" spark,INTERVAL @(-?[0-9]+)a.0,INTERVAL @a spark,"DATEDIFF(d,@start, @end)","datediff(day,@start,@end)" spark,"DATEDIFF(dd,@start, @end)","datediff(day,@start,@end)" diff --git a/man/loadRenderTranslateSql.Rd b/man/loadRenderTranslateSql.Rd index deb875f0..74df6de4 100644 --- a/man/loadRenderTranslateSql.Rd +++ b/man/loadRenderTranslateSql.Rd @@ -44,8 +44,8 @@ to the specified dialect \details{ This function looks for a SQL file with the specified name in the inst/sql/ folder of the specified package. If it doesn't find it in that folder, it will try and load the file from the -inst/sql or inst/sql/sql_server folder and use the \code{translate} function to translate it to the -requested dialect. It will subsequently call the \code{render} function with any of the additional +inst/sql or inst/sql/sql_server folder and use the \code{translate} function to translate it to the +requested dialect. It will subsequently call the \code{render} function with any of the additional specified parameters. } \examples{ diff --git a/pom.xml b/pom.xml index 75ac9d43..625388be 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.ohdsi.sql SqlRender jar - 1.18.0-SNAPSHOT + 1.18.1-SNAPSHOT SqlRender scm:git:https://github.com/OHDSI/SqlRender diff --git a/tests/testthat/test-translate-spark.R b/tests/testthat/test-translate-spark.R index 93a9689f..d38d5f0d 100644 --- a/tests/testthat/test-translate-spark.R +++ b/tests/testthat/test-translate-spark.R @@ -54,83 +54,83 @@ test_that("translate sql server -> spark convert date", { }) -test_that("translate sql server -> spark dateadd", { +test_that("translate sql server -> spark DATEADD", { # Need custom translation pattern for negative intervals in Spark - sql <- translate("SELECT dateadd(second, -1 * 2, '2019-01-01 00:00:00')", + sql <- translate("SELECT DATEADD(second, -1 * 2, '2019-01-01 00:00:00')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(second,-1 * 2,'2019-01-01 00:00:00')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(second,-1 * 2,'2019-01-01 00:00:00')") - sql <- translate("SELECT dateadd(minute, -1 * 3, '2019-01-01 00:00:00')", + sql <- translate("SELECT DATEADD(minute, -1 * 3, '2019-01-01 00:00:00')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(minute,-1 * 3,'2019-01-01 00:00:00')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(minute,-1 * 3,'2019-01-01 00:00:00')") - sql <- translate("SELECT dateadd(hour, -1 * 4, '2019-01-01 00:00:00')", + sql <- translate("SELECT DATEADD(hour, -1 * 4, '2019-01-01 00:00:00')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(hour,-1 * 4,'2019-01-01 00:00:00')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(hour,-1 * 4,'2019-01-01 00:00:00')") # Positive intervals have typical translation patterns - sql <- translate("SELECT dateadd(second, 1, '2019-01-01 00:00:00')", + sql <- translate("SELECT DATEADD(second, 1, '2019-01-01 00:00:00')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(second,1,'2019-01-01 00:00:00')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(second,1,'2019-01-01 00:00:00')") - sql <- translate("SELECT dateadd(minute, 1, '2019-01-01 00:00:00')", + sql <- translate("SELECT DATEADD(minute, 1, '2019-01-01 00:00:00')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(minute,1,'2019-01-01 00:00:00')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(minute,1,'2019-01-01 00:00:00')") - sql <- translate("SELECT dateadd(hour, 1, '2019-01-01 00:00:00')", + sql <- translate("SELECT DATEADD(hour, 1, '2019-01-01 00:00:00')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(hour,1,'2019-01-01 00:00:00')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(hour,1,'2019-01-01 00:00:00')") - sql <- translate("SELECT dateadd(d, 1, '2019-01-01')", + sql <- translate("SELECT DATEADD(d, 1, '2019-01-01')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(day,1,'2019-01-01')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(day,1,'2019-01-01')") - sql <- translate("SELECT dateadd(dd, 1, '2019-01-01')", + sql <- translate("SELECT DATEADD(dd, 1, '2019-01-01')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(day,1,'2019-01-01')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(day,1,'2019-01-01')") - sql <- translate("SELECT dateadd(day, 1, '2019-01-01')", + sql <- translate("SELECT DATEADD(day, 1, '2019-01-01')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(day,1,'2019-01-01')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(day,1,'2019-01-01')") - sql <- translate("SELECT dateadd(m, 1, '2019-01-01')", + sql <- translate("SELECT DATEADD(m, 1, '2019-01-01')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(month,1,'2019-01-01')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(month,1,'2019-01-01')") - sql <- translate("SELECT dateadd(mm, 1, '2019-01-01')", + sql <- translate("SELECT DATEADD(mm, 1, '2019-01-01')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(month,1,'2019-01-01')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(month,1,'2019-01-01')") - sql <- translate("SELECT dateadd(month, 1, '2019-01-01')", + sql <- translate("SELECT DATEADD(month, 1, '2019-01-01')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(month,1,'2019-01-01')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(month,1,'2019-01-01')") - sql <- translate("SELECT dateadd(yy, 1, '2019-01-01')", + sql <- translate("SELECT DATEADD(yy, 1, '2019-01-01')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(year,1,'2019-01-01')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(year,1,'2019-01-01')") - sql <- translate("SELECT dateadd(yyyy, 1, '2019-01-01')", + sql <- translate("SELECT DATEADD(yyyy, 1, '2019-01-01')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(year,1,'2019-01-01')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(year,1,'2019-01-01')") - sql <- translate("SELECT dateadd(year, 1, '2019-01-01')", + sql <- translate("SELECT DATEADD(year, 1, '2019-01-01')", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(year,1,'2019-01-01')") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(year,1,'2019-01-01')") }) @@ -422,14 +422,14 @@ test_that("translate sql server -> spark DATEADD DAY with float", { sql <- translate("select DATEADD(DAY, 1.0, some_date) from my_table;", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "select DATE_ADD(day, 1, some_date) from my_table;") + expect_equal_ignore_spaces(sql, "select DATEADD(DAY, 1, some_date) from my_table;") }) test_that("translate sql server -> spark DATEADD YEAR with float", { sql <- translate("select DATEADD(YEAR, 1.0, some_date) from my_table;", targetDialect = "spark" ) - expect_equal_ignore_spaces(sql, "select DATE_ADD(year, 1, some_date) from my_table;") + expect_equal_ignore_spaces(sql, "select DATEADD(YEAR, 1, some_date) from my_table;") }) test_that("translate sql server -> spark DATEADD YEAR with float", {