Skip to content

Commit

Permalink
On DataBricks, a translation of DATEADD() now returns a DATE if t…
Browse files Browse the repository at this point in the history
…he input was a `DATE`, to be consistent with other platforms. Updating copyright year
  • Loading branch information
Admin_mschuemi authored and Admin_mschuemi committed Jan 30, 2025
1 parent a0dad78 commit e2315f2
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 19 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre")),
person("Marc", "Suchard", role = c("aut"))
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
================

Expand Down
2 changes: 1 addition & 1 deletion R/HelperFunctions.R
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down
2 changes: 1 addition & 1 deletion R/RenderSql.R
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down
2 changes: 1 addition & 1 deletion R/ShinyApps.R
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down
2 changes: 1 addition & 1 deletion R/SparkSql.R
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down
2 changes: 1 addition & 1 deletion R/SqlRender.R
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down
1 change: 1 addition & 0 deletions SqlRender.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 172808d3-be61-43ad-8098-ce800f3e8439

RestoreWorkspace: No
SaveWorkspace: No
Expand Down
2 changes: 1 addition & 1 deletion extras/PackageMaintenance.R
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down
2 changes: 2 additions & 0 deletions inst/csv/replacementPatterns.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion java/org/ohdsi/sql/BigQuerySparkTranslate.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion java/org/ohdsi/sql/JarChecksum.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion java/org/ohdsi/sql/MainClass.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion java/org/ohdsi/sql/SqlRender.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion java/org/ohdsi/sql/SqlSplit.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion java/org/ohdsi/sql/SqlTranslate.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion java/org/ohdsi/sql/StringUtils.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>org.ohdsi.sql</groupId>
<artifactId>SqlRender</artifactId>
<packaging>jar</packaging>
<version>1.19.1-SNAPSHOT</version>
<version>1.19.2-SNAPSHOT</version>
<name>SqlRender</name>
<scm>
<connection>scm:git:https://github.com/OHDSI/SqlRender</connection>
Expand Down
21 changes: 18 additions & 3 deletions tests/testthat/test-translate-spark.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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;")
})

0 comments on commit e2315f2

Please sign in to comment.