From d935545dcf417c63549a0a44d8056ee1dcc86b9e Mon Sep 17 00:00:00 2001 From: Anthony Sena Date: Thu, 8 Aug 2024 13:35:01 -0400 Subject: [PATCH] CAST(@a AS DATE) handling for Snowflake - fixes #372 --- inst/csv/replacementPatterns.csv | 4 ++++ tests/testthat/test-translate-snowflake.R | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/inst/csv/replacementPatterns.csv b/inst/csv/replacementPatterns.csv index 8a14f1cc..890b4d6d 100644 --- a/inst/csv/replacementPatterns.csv +++ b/inst/csv/replacementPatterns.csv @@ -1287,6 +1287,10 @@ snowflake,"HASHBYTES('MD5',@a)","MD5(@a)" snowflake,"CONVERT(VARBINARY, @a, 1)","CAST(CONCAT('x', @a) AS BIT(32))" snowflake,"CONVERT(DATE, @a)","TO_DATE(@a, 'yyyymmdd')" snowflake,"CONVERT(VARCHAR,@date,112)","TO_CHAR(@date, 'YYYYMMDD')" +snowflake,"CAST('@a' AS DATE)","TO_DATE('@a', 'YYYYMMDD')" +snowflake,"CAST('@a' + @b AS DATE)","TO_DATE('@a' + @b, 'YYYYMMDD')" +snowflake,"CAST(@a + '@b' AS DATE)","TO_DATE(@a + '@b', 'YYYYMMDD')" +snowflake,"CAST(CONCAT(@a) AS DATE)","TO_DATE(CONCAT(@a), 'YYYYMMDD')" snowflake,GETDATE(),CURRENT_DATE snowflake,+ '@a',|| '@a' snowflake,'@a' +,'@a' || diff --git a/tests/testthat/test-translate-snowflake.R b/tests/testthat/test-translate-snowflake.R index 47e73f14..2eeb8e46 100644 --- a/tests/testthat/test-translate-snowflake.R +++ b/tests/testthat/test-translate-snowflake.R @@ -210,5 +210,9 @@ test_that("translate sql server -> snowflake bitwise and", { expect_equal_ignore_spaces(sql, "SELECT BITAND((a+b) , c/123) FROM table ;") }) +test_that("translate sql server -> Snowflake CAST(AS DATE)", { + sql <- translate("CAST('20000101' AS DATE);", targetDialect = "snowflake") + expect_equal_ignore_spaces(sql, "TO_DATE('20000101', 'YYYYMMDD');") +}) # rJava::J('org.ohdsi.sql.SqlTranslate')$setReplacementPatterns('inst/csv/replacementPatterns.csv')