Skip to content

Commit

Permalink
When creating emulated temp tables (Oracle, Spark, BigQuery), will fi…
Browse files Browse the repository at this point in the history
…rst attempt to drop (if exist).
  • Loading branch information
schuemie committed Sep 24, 2024
1 parent 5d8445a commit b9423dc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 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.18.1
Date: 2024-08-20
Version: 1.19.0
Date: 2024-09-24
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.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
================

Expand Down
9 changes: 6 additions & 3 deletions inst/csv/replacementPatterns.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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,##,<hash><hash>
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,<hash><hash>,##
Expand All @@ -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;"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;"
Expand All @@ -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' ||
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-translate-bigquery.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
})
10 changes: 10 additions & 0 deletions tests/testthat/test-translate-oracle.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
})
15 changes: 15 additions & 0 deletions tests/testthat/test-translate-spark.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
})

0 comments on commit b9423dc

Please sign in to comment.