From b9423dc91f089317eec76428de41dd4a845f1a0a Mon Sep 17 00:00:00 2001 From: schuemie Date: Tue, 24 Sep 2024 13:53:04 +0200 Subject: [PATCH] When creating emulated temp tables (Oracle, Spark, BigQuery), will first attempt to drop (if exist). --- DESCRIPTION | 4 ++-- NEWS.md | 8 ++++++++ inst/csv/replacementPatterns.csv | 9 ++++++--- tests/testthat/test-translate-bigquery.R | 15 +++++++++++++++ tests/testthat/test-translate-oracle.R | 10 ++++++++++ tests/testthat/test-translate-spark.R | 15 +++++++++++++++ 6 files changed, 56 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e3eb4115..2c7f048e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: SqlRender Type: Package Title: Rendering Parameterized SQL and Translation to Dialects -Version: 1.18.1 -Date: 2024-08-20 +Version: 1.19.0 +Date: 2024-09-24 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 0aa1fe96..87ab02b8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +SqlRender 1.19.0 +================ + +Changes: + +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. + + SqlRender 1.18.1 ================ diff --git a/inst/csv/replacementPatterns.csv b/inst/csv/replacementPatterns.csv index 890b4d6d..2649cd48 100644 --- a/inst/csv/replacementPatterns.csv +++ b/inst/csv/replacementPatterns.csv @@ -119,7 +119,9 @@ oracle,WITH @a AS @b INSERT INTO @c SELECT @d;,INSERT INTO @c WITH @a AS @b SELE oracle,SELECT @a INTO @b FROM @c;,CREATE TABLE @b AS\nSELECT\n@a\nFROM\n@c; oracle,SELECT @a INTO @b;,CREATE TABLE @b AS\nSELECT\n@a; oracle,##, +oracle,CREATE TABLE #@([^\s]+)table,DROP TABLE IF EXISTS %temp_prefix%%session_id%table;\nCREATE TABLE %temp_prefix%%session_id%table oracle,#@([^\s]+)table.@([^\s]+)field,%session_id%@table.@field +oracle,"DROP TABLE IF EXISTS #@table;",BEGIN\n EXECUTE IMMEDIATE 'TRUNCATE TABLE %temp_prefix%%session_id%@table';\n EXECUTE IMMEDIATE 'DROP TABLE %temp_prefix%%session_id%@table';\nEXCEPTION\n WHEN OTHERS THEN\n IF SQLCODE != -942 THEN\n RAISE;\n END IF;\nEND; oracle,#,%temp_prefix%%session_id% oracle,"CREATE INDEX @a ON @b (@c,@d) WHERE @e;","CREATE INDEX @a ON @b (CASE WHEN @e THEN @c END, CASE WHEN @e THEN @d END);" oracle,,## @@ -130,7 +132,6 @@ oracle,(SELECT DISTINCT TOP @([0-9]+)rows @a),(SELECT DISTINCT @a FETCH FIRST @r oracle,"IF OBJECT_ID('@table', 'U') IS NULL CREATE TABLE @table (@definition);",BEGIN\n EXECUTE IMMEDIATE 'CREATE TABLE @table (@definition)';\nEXCEPTION\n WHEN OTHERS THEN\n IF SQLCODE != -955 THEN\n RAISE;\n END IF;\nEND; oracle,"IF OBJECT_ID('tempdb..#@table', 'U') IS NOT NULL DROP TABLE #@table;",BEGIN\n EXECUTE IMMEDIATE 'TRUNCATE TABLE %temp_prefix%%session_id%@table';\n EXECUTE IMMEDIATE 'DROP TABLE %temp_prefix%%session_id%@table';\nEXCEPTION\n WHEN OTHERS THEN\n IF SQLCODE != -942 THEN\n RAISE;\n END IF;\nEND; oracle,"IF OBJECT_ID('@table', 'U') IS NOT NULL DROP TABLE @table;",BEGIN\n EXECUTE IMMEDIATE 'TRUNCATE TABLE @table';\n EXECUTE IMMEDIATE 'DROP TABLE @table';\nEXCEPTION\n WHEN OTHERS THEN\n IF SQLCODE != -942 THEN\n RAISE;\n END IF;\nEND; -oracle,"DROP TABLE IF EXISTS #@table;",BEGIN\n EXECUTE IMMEDIATE 'TRUNCATE TABLE %temp_prefix%%session_id%@table';\n EXECUTE IMMEDIATE 'DROP TABLE %temp_prefix%%session_id%@table';\nEXCEPTION\n WHEN OTHERS THEN\n IF SQLCODE != -942 THEN\n RAISE;\n END IF;\nEND; oracle,"DROP TABLE IF EXISTS @table;",BEGIN\n EXECUTE IMMEDIATE 'TRUNCATE TABLE @table';\n EXECUTE IMMEDIATE 'DROP TABLE @table';\nEXCEPTION\n WHEN OTHERS THEN\n IF SQLCODE != -942 THEN\n RAISE;\n END IF;\nEND; oracle,"FROM @a AS @b;","FROM @a NESTED @b;" oracle,"NESTED @a AS @b;","@a @b;" @@ -806,6 +807,7 @@ bigquery,DEFAULT @([0-9]+)a,"" bigquery,DEFAULT \"@a\","" bigquery,"DEFAULT CURRENT_DATE()","" bigquery,TRUNCATE TABLE @a;,DELETE FROM @a WHERE True; +bigquery,CREATE TABLE #@([^\s]+)table,DROP TABLE IF EXISTS %temp_prefix%%session_id%table;\nCREATE TABLE %temp_prefix%%session_id%table bigquery,#@([^\s]+)table.@([^\s]+)field,%session_id%@table.@field bigquery,#,%temp_prefix%%session_id% bigquery,CREATE INDEX @index_name ON @table_col_cond;,-- bigquery does not support indexes @@ -1058,7 +1060,6 @@ spark,TRY_CAST(@a),CAST(@a) spark,"IIF(@condition, @whentrue, @whenfalse)","CASE WHEN @condition THEN @whentrue ELSE @whenfalse END" spark,tempdb..#@table +,%temp_prefix%%session_id% || spark,#@([^\s]+)table.@([^\s]+)field,%session_id%@table.@field -spark,#,%temp_prefix%%session_id% spark,"--HINT BUCKET(@a, @b)", spark,"--HINT PARTITION(@a @b)", 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;" @@ -1081,6 +1082,8 @@ spark,SELECT @a INTO @b GROUP BY @c;,CREATE TABLE @b\nUSING DELTA\n AS\nSELECT\n spark,"IF OBJECT_ID('@table', 'U') IS NULL CREATE TABLE @table\nUSING DELTA\nAS\n@definition;",CREATE TABLE IF NOT EXISTS @table\nUSING DELTA\nAS\n@definition; spark,"IF OBJECT_ID('@table', 'U') IS NULL CREATE TABLE @table (@definition);",CREATE TABLE IF NOT EXISTS @table (@definition); spark,"IF OBJECT_ID('@table', 'U') IS NOT NULL DROP TABLE @table;",DROP TABLE IF EXISTS @table; +spark,CREATE TABLE #@([^\s]+)table,DROP TABLE IF EXISTS %temp_prefix%%session_id%table;\nCREATE TABLE %temp_prefix%%session_id%table +spark,#,%temp_prefix%%session_id% spark,\"@a\",`@a` spark,+ '@a',|| '@a' spark,'@a' +,'@a' || @@ -1351,7 +1354,7 @@ snowflake,UPDATE STATISTICS @a;, snowflake,"--HINT BUCKET(@a, @b)", snowflake,"--HINT PARTITION(@a @b)", snowflake,"--HINT DISTRIBUTE_ON_KEY(@key)", -snowflake,#,%temp_prefix%%session_id% +snowflake,#,%session_id% snowflake,(@a & @b),"BITAND(@a, @b)" synapse,...@([0-9]+|y)a,xxx@a synapse,VARCHAR(MAX),VARCHAR(8000) diff --git a/tests/testthat/test-translate-bigquery.R b/tests/testthat/test-translate-bigquery.R index 781f4745..1522aff8 100644 --- a/tests/testthat/test-translate-bigquery.R +++ b/tests/testthat/test-translate-bigquery.R @@ -549,3 +549,18 @@ test_that("translate sql server -> bigquery RIGHT with implicit concat", { sql <- translate("RIGHT('0' + CAST(p.month_of_birth AS VARCHAR), 2)", targetDialect = "bigquery") expect_equal_ignore_spaces(sql, "SUBSTR(CONCAT('0', cast(p.month_of_birth as STRING)),-2)") }) + +test_that("translate sql server -> bigquery create temp table", { + sql <- translate("CREATE TABLE #temp (x INT);", targetDialect = "bigquery", tempEmulationSchema = "ts") + expect_equal_ignore_spaces(sql, sprintf("DROP TABLE IF EXISTS ts.%stable;\nCREATE TABLE ts.%stable (x INT64);", getTempTablePrefix(), getTempTablePrefix())) +}) + +test_that("translate sql server -> bigquery select into temp table", { + sql <- translate("SELECT * INTO #temp FROM my_table;", targetDialect = "bigquery", tempEmulationSchema = "ts") + expect_equal_ignore_spaces(sql, sprintf("DROP TABLE IF EXISTS ts.%stable;\nCREATE TABLE ts.%stable AS\nSELECT\n* \nFROM\nmy_table;", getTempTablePrefix(), getTempTablePrefix())) +}) + +test_that("translate sql server -> bigquery create temp table if not exists", { + sql <- translate("CREATE TABLE IF NOT EXISTS #temp (x INT);", targetDialect = "bigquery", tempEmulationSchema = "ts") + expect_equal_ignore_spaces(sql, sprintf("create table if not exists ts.%stemp (x INT64);", getTempTablePrefix())) +}) diff --git a/tests/testthat/test-translate-oracle.R b/tests/testthat/test-translate-oracle.R index 6638f63b..706304fe 100644 --- a/tests/testthat/test-translate-oracle.R +++ b/tests/testthat/test-translate-oracle.R @@ -561,3 +561,13 @@ test_that("translate sql server -> oracle bitwise and", { sql <- translate("SELECT ((a+b) & c/123) FROM table;", targetDialect = "oracle") expect_equal_ignore_spaces(sql, "SELECT BITAND((a+b) , c/123) FROM table ;") }) + +test_that("translate sql server -> oracle create temp table", { + sql <- translate("CREATE TABLE #temp (x INT);", targetDialect = "oracle", tempEmulationSchema = "ts") + expect_equal_ignore_spaces(sql, sprintf("BEGIN\n EXECUTE IMMEDIATE 'TRUNCATE TABLE ts.%stable';\n EXECUTE IMMEDIATE 'DROP TABLE ts.%stable';\nEXCEPTION\n WHEN OTHERS THEN\n IF SQLCODE != -942 THEN\n RAISE;\n END IF;\nEND;\nCREATE TABLE ts.%stable (x INT);", getTempTablePrefix(), getTempTablePrefix(), getTempTablePrefix())) +}) + +test_that("translate sql server -> oracle select into temp table", { + sql <- translate("SELECT * INTO #temp FROM my_table;", targetDialect = "oracle", tempEmulationSchema = "ts") + expect_equal_ignore_spaces(sql, sprintf("BEGIN\n EXECUTE IMMEDIATE 'TRUNCATE TABLE ts.%stable';\n EXECUTE IMMEDIATE 'DROP TABLE ts.%stable';\nEXCEPTION\n WHEN OTHERS THEN\n IF SQLCODE != -942 THEN\n RAISE;\n END IF;\nEND;\nCREATE TABLE ts.%stable AS\nSELECT\n* \nFROM\nmy_table ;", getTempTablePrefix(), getTempTablePrefix(), getTempTablePrefix())) +}) diff --git a/tests/testthat/test-translate-spark.R b/tests/testthat/test-translate-spark.R index ef180b98..6af23a1c 100644 --- a/tests/testthat/test-translate-spark.R +++ b/tests/testthat/test-translate-spark.R @@ -453,3 +453,18 @@ test_that("translate sql server -> spark cast string as date", { sql <- translate("SELECT CAST('20191201' AS DATE);", targetDialect = "spark") expect_equal_ignore_spaces(sql, "SELECT IF(try_cast('20191201' AS DATE) IS NULL, to_date(cast('20191201' AS STRING), 'yyyyMMdd'), try_cast('20191201' AS DATE));") }) + +test_that("translate sql server -> spark create temp table", { + sql <- translate("CREATE TABLE #temp (x INT);", targetDialect = "spark", tempEmulationSchema = "ts") + expect_equal_ignore_spaces(sql, sprintf("DROP TABLE IF EXISTS ts.%stable;\nCREATE TABLE ts.%stable \nUSING DELTA\n AS\nSELECT\nCAST(NULL AS int) AS x WHERE 1 = 0;", getTempTablePrefix(), getTempTablePrefix())) +}) + +test_that("translate sql server -> spark select into temp table", { + sql <- translate("SELECT * INTO #temp FROM my_table;", targetDialect = "spark", tempEmulationSchema = "ts") + expect_equal_ignore_spaces(sql, sprintf("DROP TABLE IF EXISTS ts.%stable;\nCREATE TABLE ts.%stable \nUSING DELTA\nAS\nSELECT\n* \nFROM\nmy_table;", getTempTablePrefix(), getTempTablePrefix())) +}) + +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())) +})