Skip to content

Commit ad43879

Browse files
committed
Fixing unit tests broken by IRIS PR #377
1 parent e5463e2 commit ad43879

File tree

5 files changed

+115
-114
lines changed

5 files changed

+115
-114
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: SqlRender
22
Type: Package
33
Title: Rendering Parameterized SQL and Translation to Dialects
44
Version: 1.19.0
5-
Date: 2024-09-24
5+
Date: 2024-09-30
66
Authors@R: c(
77
person("Martijn", "Schuemie", , "[email protected]", role = c("aut", "cre")),
88
person("Marc", "Suchard", role = c("aut"))

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Changes:
55

66
1. When creating emulated temp tables (Oracle, Spark, BigQuery), will first attempt to drop (if exist). This is to clean up any orphan tables from a previous (unsuccesful) run.
77

8+
2. Adding tentative support for InterSystems IRIS. Could still be removed.
9+
810
Bugfixes:
911

1012
1. On Snowflake, fixed 'This session does not have a current schema' error when translating legacy `IF OBJECT_ID('tempdb..#table', 'U') IS NOT NULL DROP TABLE #table;`

inst/csv/replacementPatterns.csv

+5-5
Original file line numberDiff line numberDiff line change
@@ -1060,8 +1060,8 @@ spark,TRY_CAST(@a),CAST(@a)
10601060
spark,"IIF(@condition, @whentrue, @whenfalse)","CASE WHEN @condition THEN @whentrue ELSE @whenfalse END"
10611061
spark,tempdb..#@table +,%temp_prefix%%session_id% ||
10621062
spark,#@([^\s]+)table.@([^\s]+)field,%session_id%@table.@field
1063-
spark,"--HINT BUCKET(@a, @b)",
1064-
spark,"--HINT PARTITION(@a @b)",
1063+
spark,"--HINT BUCKET(@a, @b)",""
1064+
spark,"--HINT PARTITION(@a @b)",""
10651065
spark,"HINT DISTRIBUTE_ON_KEY(@key) CREATE TABLE IF NOT EXISTS @table\nUSING DELTA\nAS\n@definition;","HINT DISTRIBUTE_ON_KEY(@key)\nCREATE TABLE IF NOT EXISTS @table\nUSING DELTA\nAS\n@definition;\nOPTIMIZE @table ZORDER BY @key;"
10661066
spark,"HINT DISTRIBUTE_ON_KEY(@key) IF OBJECT_ID('@d', 'U') IS NULL WITH @a AS @b SELECT @c INTO @d FROM @e;",HINT DISTRIBUTE_ON_KEY(@key)\nCREATE TABLE IF NOT EXISTS @d\nUSING DELTA\nAS\nWITH @a AS @b SELECT\n@c\nFROM\n@e;\nOPTIMIZE @d ZORDER BY @key;
10671067
spark,"HINT DISTRIBUTE_ON_KEY(@key) IF OBJECT_ID('@b', 'U') IS NULL SELECT @a INTO @b FROM @c;",HINT DISTRIBUTE_ON_KEY(@key) \nCREATE TABLE IF NOT EXISTS @b\nUSING DELTA\nAS\nSELECT\n@a\nFROM\n@c;\nOPTIMIZE @b ZORDER BY @key;
@@ -1402,9 +1402,9 @@ iris,SELECT @a INTO @b;,CREATE TABLE @b AS SELECT @a;
14021402
iris,SELECT @a INTO #@b;,CREATE GLOBAL TEMPORARY TABLE #@b AS SELECT @a;
14031403
iris,#,%temp_prefix%%session_id%
14041404
iris,UPDATE STATISTICS @a;,TUNE TABLE @a;
1405-
iris,"--HINT BUCKET(@a, @b)", -- haven't looked into this yet, skip it for now
1406-
iris,"--HINT PARTITION(@a @b)", -- haven't looked into this yet, skip it for now
1407-
iris,"--HINT DISTRIBUTE_ON_KEY(@key)", -- haven't looked into this yet, skip it for now
1405+
iris,"--HINT BUCKET(@a, @b)", "-- haven't looked into this yet, skip it for now"
1406+
iris,"--HINT PARTITION(@a @b)", -- "haven't looked into this yet, skip it for now""
1407+
iris,"--HINT DISTRIBUTE_ON_KEY(@key)", -- "haven't looked into this yet, skip it for now""
14081408
iris,"DATEFROMPARTS(@year,@month,@day)","TO_DATE(TO_CHAR(@year,'FM0000')||'-'||TO_CHAR(@month,'FM00')||'-'||TO_CHAR(@day,'FM00'), 'YYYY-MM-DD')"
14091409
iris,"DATETIMEFROMPARTS(@year,@month,@day,@hour,@minute,@second,@ms)","TO_TIMESTAMP(TO_CHAR(@year,'FM0000')||'-'||TO_CHAR(@month,'FM00')||'-'||TO_CHAR(@day,'FM00')||' '||TO_CHAR(@hour,'FM00')||':'||TO_CHAR(@minute,'FM00')||':'||TO_CHAR(@second,'FM00')||'.'||TO_CHAR(@ms,'FM000'), 'YYYY-MM-DD HH24:MI:SS.FF')"
14101410
iris," DATEADD(d, @a, @b) AS"," TO_DATE(DATEADD(d, @a, @b),'YYYY-MM-DD HH:MI:SS') AS"

man/SqlRender-package.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-translate-iris.R

+106-107
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,106 @@
1-
library(testthat)
2-
library(devtools)
3-
library(rJava)
4-
5-
6-
# For debugging: force reload of code & patterns:
7-
# load_all()
8-
# rJava::J('org.ohdsi.sql.SqlTranslate')$setReplacementPatterns('../../inst/csv/replacementPatterns.csv')
9-
10-
11-
expect_equal_ignore_spaces <- function(string1, string2) {
12-
string1 <- gsub("([;()'+-/|*\n])", " \\1 ", string1)
13-
string2 <- gsub("([;()'+-/|*\n])", " \\1 ", string2)
14-
string1 <- gsub(" +", " ", string1)
15-
string2 <- gsub(" +", " ", string2)
16-
expect_equivalent(string1, string2)
17-
}
18-
19-
# tests wrt string concatenation
20-
test_that("translate sql server -> InterSystems IRIS string concatenation", {
21-
sql <- translate("SELECT CONCAT(a, 'b', c)", targetDialect = "iris")
22-
expect_equal_ignore_spaces(sql, "SELECT a || 'b' || c")
23-
})
24-
test_that("translate sql server -> InterSystems IRIS string concatenation", {
25-
sql <- translate("SELECT CONCAT(a, 'b', c, d, e, e, f)", targetDialect = "iris")
26-
expect_equal_ignore_spaces(sql, "SELECT a || 'b' || c || d || e || e || f")
27-
})
28-
test_that("translate sql server -> InterSystems IRIS string +", {
29-
sql <- translate("SELECT CAST(a AS VARCHAR) + CAST(b AS VARCHAR(10)) + CAST(c AS VARCHAR) + 'd'", targetDialect = "iris")
30-
expect_equal_ignore_spaces(sql, "SELECT CAST(a AS VARCHAR) || CAST(b AS varchar(10)) || CAST(c AS VARCHAR) || 'd'")
31-
})
32-
test_that("translate sql server -> InterSystem IRIS string concatenation DOB", {
33-
sql <- translate("SELECT CONCAT(p.year_of_birth, 11, 11)", targetDialect = "iris")
34-
expect_equal_ignore_spaces(sql, "SELECT p.year_of_birth||'-'||11||'-'||11")
35-
})
36-
37-
38-
# build date from parts
39-
test_that("translate sql server -> InterSystems IRIS DATEFROMPARTS()", {
40-
sql <- translate("SELECT DATEFROMPARTS(yyyy, mm, dd)", targetDialect = "iris")
41-
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(TO_CHAR(yyyy,'FM0000')||'-'||TO_CHAR(mm,'FM00')||'-'||TO_CHAR(dd,'FM00'), 'YYYY-MM-DD')")
42-
})
43-
test_that("translate sql server -> InterSystems IRIS DATETIMEFROMPARTS()", {
44-
sql <- translate("SELECT DATETIMEFROMPARTS(yyyy, mm, dd, hh, mi, ss, ms)", targetDialect = "iris")
45-
expect_equal_ignore_spaces(sql, "SELECT TO_TIMESTAMP(TO_CHAR(yyyy,'FM0000')||'-'||TO_CHAR(mm,'FM00')||'-'||TO_CHAR(dd,'FM00')||' '||TO_CHAR(hh,'FM00')||':'||TO_CHAR(mi,'FM00')||':'||TO_CHAR(ss,'FM00')||'.'||TO_CHAR(ms,'FM000'), 'YYYY-MM-DD HH24:MI:SS.FF')")
46-
})
47-
48-
49-
50-
# temp table handling
51-
test_that("translate sql server -> InterSystems IRIS implicit CTAS", {
52-
sql <- translate("SELECT a, b INTO t_new FROM t;", targetDialect = "iris")
53-
expect_equal_ignore_spaces(sql, "CREATE TABLE t_new AS SELECT a, b FROM t;")
54-
})
55-
test_that("translate sql server -> InterSystems IRIS implicit CTTAS", {
56-
sql <- translate("SELECT a, b INTO #t_new FROM t;", targetDialect = "iris")
57-
expect_equal_ignore_spaces(sql, paste("CREATE GLOBAL TEMPORARY TABLE ", getTempTablePrefix(), "t_new AS SELECT a, b FROM t;", sep=""))
58-
})
59-
60-
61-
# test DATEADD() flavours
62-
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
63-
sql <- translate("SELECT DATEADD(d, 1, '2007-07-28') AS dt", targetDialect = "iris")
64-
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(d,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
65-
})
66-
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
67-
sql <- translate("SELECT DATEADD(dd, 1, '2007-07-28') AS dt", targetDialect = "iris")
68-
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(dd,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
69-
})
70-
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
71-
sql <- translate("SELECT DATEADD(day, 1, '2007-07-28') AS dt", targetDialect = "iris")
72-
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(day,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
73-
})
74-
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
75-
sql <- translate("SELECT DATEADD(m, 1, '2007-07-28') AS dt", targetDialect = "iris")
76-
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(m,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
77-
})
78-
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
79-
sql <- translate("SELECT DATEADD(mm, 1, '2007-07-28') AS dt", targetDialect = "iris")
80-
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(mm,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
81-
})
82-
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
83-
sql <- translate("SELECT DATEADD(yy, 1, '2007-07-28') AS dt", targetDialect = "iris")
84-
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(yy,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
85-
})
86-
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
87-
sql <- translate("SELECT DATEADD(yyyy, 1, '2007-07-28') AS dt", targetDialect = "iris")
88-
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(yyyy,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
89-
})
90-
91-
92-
# test reserved words
93-
test_that("translate sql server -> InterSystems IRIS reserved word DOMAIN", {
94-
sql <- translate("SELECT t.domain, 'domain' FROM omopcdm.domain AS t", targetDialect = "iris")
95-
expect_equal_ignore_spaces(sql, "SELECT t.\"DOMAIN\", 'domain' FROM omopcdm.\"DOMAIN\" AS t")
96-
})
97-
test_that("translate sql server -> InterSystems IRIS reserved words for aggregates", {
98-
sql <- translate("SELECT MIN(x) AS min, MAX(x) AS max, COUNT(x) as COUNT FROM t", targetDialect = "iris")
99-
expect_equal_ignore_spaces(sql, "SELECT MIN(x) AS \"MIN\", MAX(x) AS \"MAX\", COUNT(x) as \"COUNT\" FROM t")
100-
})
101-
102-
103-
# test function names
104-
test_that("translate sql server -> InterSystems IRIS function names", {
105-
sql <- translate("SELECT STDEV(x), STDEV_POP(x), STDEV_SAMP(x), EOMONTH(dt) FROM t", targetDialect = "iris")
106-
expect_equal_ignore_spaces(sql, "SELECT STDDEV(x), STDDEV_POP(x), STDDEV_SAMP(x), LAST_DAY(dt) FROM t")
107-
})
1+
library(testthat)
2+
library(rJava)
3+
4+
5+
# For debugging: force reload of code & patterns:
6+
# load_all()
7+
# rJava::J('org.ohdsi.sql.SqlTranslate')$setReplacementPatterns('../../inst/csv/replacementPatterns.csv')
8+
9+
10+
expect_equal_ignore_spaces <- function(string1, string2) {
11+
string1 <- gsub("([;()'+-/|*\n])", " \\1 ", string1)
12+
string2 <- gsub("([;()'+-/|*\n])", " \\1 ", string2)
13+
string1 <- gsub(" +", " ", string1)
14+
string2 <- gsub(" +", " ", string2)
15+
expect_equivalent(string1, string2)
16+
}
17+
18+
# tests wrt string concatenation
19+
test_that("translate sql server -> InterSystems IRIS string concatenation", {
20+
sql <- translate("SELECT CONCAT(a, 'b', c)", targetDialect = "iris")
21+
expect_equal_ignore_spaces(sql, "SELECT a || 'b' || c")
22+
})
23+
test_that("translate sql server -> InterSystems IRIS string concatenation", {
24+
sql <- translate("SELECT CONCAT(a, 'b', c, d, e, e, f)", targetDialect = "iris")
25+
expect_equal_ignore_spaces(sql, "SELECT a || 'b' || c || d || e || e || f")
26+
})
27+
test_that("translate sql server -> InterSystems IRIS string +", {
28+
sql <- translate("SELECT CAST(a AS VARCHAR) + CAST(b AS VARCHAR(10)) + CAST(c AS VARCHAR) + 'd'", targetDialect = "iris")
29+
expect_equal_ignore_spaces(sql, "SELECT CAST(a AS VARCHAR) || CAST(b AS varchar(10)) || CAST(c AS VARCHAR) || 'd'")
30+
})
31+
test_that("translate sql server -> InterSystem IRIS string concatenation DOB", {
32+
sql <- translate("SELECT CONCAT(p.year_of_birth, 11, 11)", targetDialect = "iris")
33+
expect_equal_ignore_spaces(sql, "SELECT p.year_of_birth||'-'||11||'-'||11")
34+
})
35+
36+
37+
# build date from parts
38+
test_that("translate sql server -> InterSystems IRIS DATEFROMPARTS()", {
39+
sql <- translate("SELECT DATEFROMPARTS(yyyy, mm, dd)", targetDialect = "iris")
40+
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(TO_CHAR(yyyy,'FM0000')||'-'||TO_CHAR(mm,'FM00')||'-'||TO_CHAR(dd,'FM00'), 'YYYY-MM-DD')")
41+
})
42+
test_that("translate sql server -> InterSystems IRIS DATETIMEFROMPARTS()", {
43+
sql <- translate("SELECT DATETIMEFROMPARTS(yyyy, mm, dd, hh, mi, ss, ms)", targetDialect = "iris")
44+
expect_equal_ignore_spaces(sql, "SELECT TO_TIMESTAMP(TO_CHAR(yyyy,'FM0000')||'-'||TO_CHAR(mm,'FM00')||'-'||TO_CHAR(dd,'FM00')||' '||TO_CHAR(hh,'FM00')||':'||TO_CHAR(mi,'FM00')||':'||TO_CHAR(ss,'FM00')||'.'||TO_CHAR(ms,'FM000'), 'YYYY-MM-DD HH24:MI:SS.FF')")
45+
})
46+
47+
48+
49+
# temp table handling
50+
test_that("translate sql server -> InterSystems IRIS implicit CTAS", {
51+
sql <- translate("SELECT a, b INTO t_new FROM t;", targetDialect = "iris")
52+
expect_equal_ignore_spaces(sql, "CREATE TABLE t_new AS SELECT a, b FROM t;")
53+
})
54+
test_that("translate sql server -> InterSystems IRIS implicit CTTAS", {
55+
sql <- translate("SELECT a, b INTO #t_new FROM t;", targetDialect = "iris")
56+
expect_equal_ignore_spaces(sql, paste("CREATE GLOBAL TEMPORARY TABLE ", getTempTablePrefix(), "t_new AS SELECT a, b FROM t;", sep=""))
57+
})
58+
59+
60+
# test DATEADD() flavours
61+
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
62+
sql <- translate("SELECT DATEADD(d, 1, '2007-07-28') AS dt", targetDialect = "iris")
63+
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(d,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
64+
})
65+
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
66+
sql <- translate("SELECT DATEADD(dd, 1, '2007-07-28') AS dt", targetDialect = "iris")
67+
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(dd,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
68+
})
69+
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
70+
sql <- translate("SELECT DATEADD(day, 1, '2007-07-28') AS dt", targetDialect = "iris")
71+
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(day,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
72+
})
73+
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
74+
sql <- translate("SELECT DATEADD(m, 1, '2007-07-28') AS dt", targetDialect = "iris")
75+
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(m,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
76+
})
77+
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
78+
sql <- translate("SELECT DATEADD(mm, 1, '2007-07-28') AS dt", targetDialect = "iris")
79+
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(mm,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
80+
})
81+
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
82+
sql <- translate("SELECT DATEADD(yy, 1, '2007-07-28') AS dt", targetDialect = "iris")
83+
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(yy,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
84+
})
85+
test_that("translate sql server -> InterSystems IRIS DATEADD(d, ..)", {
86+
sql <- translate("SELECT DATEADD(yyyy, 1, '2007-07-28') AS dt", targetDialect = "iris")
87+
expect_equal_ignore_spaces(sql, "SELECT TO_DATE(DATEADD(yyyy,1,'2007-07-28'),'YYYY-MM-DD HH:MI:SS') AS dt")
88+
})
89+
90+
91+
# test reserved words
92+
test_that("translate sql server -> InterSystems IRIS reserved word DOMAIN", {
93+
sql <- translate("SELECT t.domain, 'domain' FROM omopcdm.domain AS t", targetDialect = "iris")
94+
expect_equal_ignore_spaces(sql, "SELECT t.\"DOMAIN\", 'domain' FROM omopcdm.\"DOMAIN\" AS t")
95+
})
96+
test_that("translate sql server -> InterSystems IRIS reserved words for aggregates", {
97+
sql <- translate("SELECT MIN(x) AS min, MAX(x) AS max, COUNT(x) as COUNT FROM t", targetDialect = "iris")
98+
expect_equal_ignore_spaces(sql, "SELECT MIN(x) AS \"MIN\", MAX(x) AS \"MAX\", COUNT(x) AS \"COUNT\" FROM t")
99+
})
100+
101+
102+
# test function names
103+
test_that("translate sql server -> InterSystems IRIS function names", {
104+
sql <- translate("SELECT STDEV(x), STDEV_POP(x), STDEV_SAMP(x), EOMONTH(dt) FROM t", targetDialect = "iris")
105+
expect_equal_ignore_spaces(sql, "SELECT STDDEV(x), STDDEV_POP(x), STDDEV_SAMP(x), LAST_DAY(dt) FROM t")
106+
})

0 commit comments

Comments
 (0)