diff --git a/DESCRIPTION b/DESCRIPTION index d42f47ab..df8bb6fc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: SqlRender Type: Package Title: Rendering Parameterized SQL and Translation to Dialects -Version: 1.19.1 -Date: 2024-11-29 +Version: 1.19.2 +Date: 2025-01-30 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 9125046e..bacc997e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +SqlRender 1.19.2 +================ + +Bugfixes: + +1. On DataBricks, a translation of `DATEADD()` now returns a `DATE` if the input was a `DATE`, to be consistent with other platforms. (Requires input field name to end with '_date') + + SqlRender 1.19.1 ================ diff --git a/R/HelperFunctions.R b/R/HelperFunctions.R index 21988423..39d2ad77 100644 --- a/R/HelperFunctions.R +++ b/R/HelperFunctions.R @@ -1,6 +1,6 @@ # @file HelperFunctions.R # -# Copyright 2024 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of SqlRender # diff --git a/R/RenderSql.R b/R/RenderSql.R index 872cc47a..73e4bb94 100644 --- a/R/RenderSql.R +++ b/R/RenderSql.R @@ -1,6 +1,6 @@ # @file RenderSql # -# Copyright 2024 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of SqlRender # diff --git a/R/ShinyApps.R b/R/ShinyApps.R index 067c5f19..41368a26 100644 --- a/R/ShinyApps.R +++ b/R/ShinyApps.R @@ -1,4 +1,4 @@ -# Copyright 2024 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of SqlRender # diff --git a/R/SparkSql.R b/R/SparkSql.R index 327a3421..c1b4a9b0 100644 --- a/R/SparkSql.R +++ b/R/SparkSql.R @@ -1,4 +1,4 @@ -# Copyright 2024 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of SqlRender # diff --git a/R/SqlRender.R b/R/SqlRender.R index 8c46f5cd..04a2909e 100644 --- a/R/SqlRender.R +++ b/R/SqlRender.R @@ -1,4 +1,4 @@ -# Copyright 2024 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of SqlRender # diff --git a/SqlRender.Rproj b/SqlRender.Rproj index 97fa78e0..7ac844d5 100644 --- a/SqlRender.Rproj +++ b/SqlRender.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: 172808d3-be61-43ad-8098-ce800f3e8439 RestoreWorkspace: No SaveWorkspace: No diff --git a/extras/PackageMaintenance.R b/extras/PackageMaintenance.R index f3873c3c..4962246a 100644 --- a/extras/PackageMaintenance.R +++ b/extras/PackageMaintenance.R @@ -1,4 +1,4 @@ -# Copyright 2024 Observational Health Data Sciences and Informatics +# Copyright 2025 Observational Health Data Sciences and Informatics # # This file is part of SqlRender # diff --git a/inst/csv/replacementPatterns.csv b/inst/csv/replacementPatterns.csv index 5e7f4062..2b5fe377 100644 --- a/inst/csv/replacementPatterns.csv +++ b/inst/csv/replacementPatterns.csv @@ -1144,6 +1144,8 @@ spark,UPDATE STATISTICS @a;, spark,"SELECT @columns FROM (@a) @x,(@b) @y;","SELECT @columns FROM (@a) @x cross join (@b) @y;" spark,"ALTER TABLE @table ADD COLUMN @([\w_-]+)column @type DEFAULT @default;","ALTER TABLE @table ADD COLUMN @column @type; \nALTER TABLE @table SET TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported'); \nALTER TABLE @table ALTER COLUMN @column SET DEFAULT @default;" spark,"CAST(@a AS DATE)","IF(try_cast(@a AS DATE) IS NULL, to_date(cast(@a AS STRING), 'yyyyMMdd'), try_cast(@a AS DATE))" +spark,"DATEADD(@part,@amount,@([0-9a-zA-Z_]+_date)date)","CAST(DATEA##(@part,@amount,@date) AS DATE)" +spark,DATEA##,DATEADD sqlite extended,"IIF(@condition, @whentrue, @whenfalse)","CASE WHEN @condition THEN @whentrue ELSE @whenfalse END" sqlite extended,"ROUND(@a,@b)","ROUND(CAST(@a AS REAL),@b)" sqlite extended,+ '@a',|| '@a' diff --git a/java/org/ohdsi/sql/BigQuerySparkTranslate.java b/java/org/ohdsi/sql/BigQuerySparkTranslate.java index 9be9c3b3..e9dee636 100644 --- a/java/org/ohdsi/sql/BigQuerySparkTranslate.java +++ b/java/org/ohdsi/sql/BigQuerySparkTranslate.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright 2024 Observational Health Data Sciences and Informatics + * Copyright 2025 Observational Health Data Sciences and Informatics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/java/org/ohdsi/sql/JarChecksum.java b/java/org/ohdsi/sql/JarChecksum.java index 0437800d..def5faac 100644 --- a/java/org/ohdsi/sql/JarChecksum.java +++ b/java/org/ohdsi/sql/JarChecksum.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright 2024 Observational Health Data Sciences and Informatics + * Copyright 2025 Observational Health Data Sciences and Informatics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/java/org/ohdsi/sql/MainClass.java b/java/org/ohdsi/sql/MainClass.java index 892391f5..dfbce51a 100644 --- a/java/org/ohdsi/sql/MainClass.java +++ b/java/org/ohdsi/sql/MainClass.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright 2024 Observational Health Data Sciences and Informatics + * Copyright 2025 Observational Health Data Sciences and Informatics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/java/org/ohdsi/sql/SqlRender.java b/java/org/ohdsi/sql/SqlRender.java index 02810a00..39a9486b 100644 --- a/java/org/ohdsi/sql/SqlRender.java +++ b/java/org/ohdsi/sql/SqlRender.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright 2024 Observational Health Data Sciences and Informatics + * Copyright 2025 Observational Health Data Sciences and Informatics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/java/org/ohdsi/sql/SqlSplit.java b/java/org/ohdsi/sql/SqlSplit.java index 06c38a2e..f84bb375 100644 --- a/java/org/ohdsi/sql/SqlSplit.java +++ b/java/org/ohdsi/sql/SqlSplit.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright 2024 Observational Health Data Sciences and Informatics + * Copyright 2025 Observational Health Data Sciences and Informatics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/java/org/ohdsi/sql/SqlTranslate.java b/java/org/ohdsi/sql/SqlTranslate.java index 5b732406..a8a51409 100644 --- a/java/org/ohdsi/sql/SqlTranslate.java +++ b/java/org/ohdsi/sql/SqlTranslate.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright 2024 Observational Health Data Sciences and Informatics + * Copyright 2025 Observational Health Data Sciences and Informatics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/java/org/ohdsi/sql/StringUtils.java b/java/org/ohdsi/sql/StringUtils.java index 6aae76ab..bb87daf1 100644 --- a/java/org/ohdsi/sql/StringUtils.java +++ b/java/org/ohdsi/sql/StringUtils.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright 2024 Observational Health Data Sciences and Informatics + * Copyright 2025 Observational Health Data Sciences and Informatics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/pom.xml b/pom.xml index 5e8c4bf4..5d8b1074 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.ohdsi.sql SqlRender jar - 1.19.1-SNAPSHOT + 1.19.2-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 79967d2c..f9e7f386 100644 --- a/tests/testthat/test-translate-spark.R +++ b/tests/testthat/test-translate-spark.R @@ -422,17 +422,17 @@ 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 DATEADD(DAY, 1, some_date) from my_table;") + expect_equal_ignore_spaces(sql, "select CAST(DATEADD(DAY, 1, some_date) AS 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 DATEADD(YEAR, 1, some_date) from my_table;") + expect_equal_ignore_spaces(sql, "select CAST(DATEADD(YEAR, 1, some_date) AS DATE) from my_table;") }) -test_that("translate sql server -> spark DATEADD YEAR with float", { +test_that("translate sql server -> spark CTE", { sql <- translate("WITH cte AS (SELECT * FROM table) SELECT * INTO tmp.table FROM cte;", targetDialect = "spark" ) @@ -468,3 +468,18 @@ test_that("translate sql server -> spark create temp table if not exists", { sql <- translate("CREATE TABLE IF NOT EXISTS #temp (x INT);", targetDialect = "spark", tempEmulationSchema = "ts") expect_equal_ignore_spaces(sql, sprintf("CREATE TABLE IF NOT EXISTS ts.%stemp \nUSING DELTA\n AS\nSELECT\nCAST(NULL AS int) AS x WHERE 1 = 0;", getTempTablePrefix())) }) + +rJava::J('org.ohdsi.sql.SqlTranslate')$setReplacementPatterns('inst/csv/replacementPatterns.csv') + +test_that("translate sql server -> spark DATEADD for DATE column", { + # If field is a date, it should remain a date after DATEADD to be consistent with other platforms: + sql <- translate("SELECT DATEADD(DAY, 1, start_date) FROM table;", targetDialect = "spark") + expect_equal_ignore_spaces(sql, "SELECT CAST(DATEADD(DAY,1,start_date) AS DATE) FROM table;") + + sql <- translate("SELECT DATEADD(DAY, 1, START_DATE) FROM table;", targetDialect = "spark") + expect_equal_ignore_spaces(sql, "SELECT CAST(DATEADD(DAY,1,START_DATE) AS DATE) FROM table;") + + sql <- translate("SELECT DATEADD(DAY, 1, start_datetime) FROM table;", targetDialect = "spark") + expect_equal_ignore_spaces(sql, "SELECT DATEADD(DAY,1,start_datetime) FROM table;") +}) +