From 6e13c6631df846301fda69e53638822b246dde29 Mon Sep 17 00:00:00 2001 From: schuemie Date: Wed, 10 Apr 2024 08:28:53 +0200 Subject: [PATCH] Fixed for ALTER TABLE ALTER COLUMN for Postgres and SQLite --- DESCRIPTION | 6 +++--- NEWS.md | 10 ++++++++++ inst/csv/replacementPatterns.csv | 4 ++-- tests/testthat/test-translate-postgresql.R | 4 ++++ tests/testthat/test-translate-sqlite.R | 9 +++++++++ 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ee602902..5bb55fb6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: SqlRender Type: Package Title: Rendering Parameterized SQL and Translation to Dialects -Version: 1.17.0 -Date: 2024-03-20 +Version: 1.17.1 +Date: 2024-04-09 Authors@R: c( person("Martijn", "Schuemie", , "schuemie@ohdsi.org", role = c("aut", "cre")), person("Marc", "Suchard", role = c("aut")) @@ -27,5 +27,5 @@ Suggests: rmarkdown, shiny, shinydashboard -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Encoding: UTF-8 diff --git a/NEWS.md b/NEWS.md index 1f809b71..38241cdb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,13 @@ +SqlRender 1.17.1 +================ + +Bugfixes: + +1. For SQLite, now translating `ALTER TABLE ALTER COLUMN BIGINT`to dummy statement (`SELECT 0;`), since all integer types are the same on SQLite. + +2. Fixed translation of `ALTER TABLE ALTER COLUMN` on PostgreSQL. + + SqlRender 1.17.0 ================ diff --git a/inst/csv/replacementPatterns.csv b/inst/csv/replacementPatterns.csv index 31ef0014..8b7a72af 100644 --- a/inst/csv/replacementPatterns.csv +++ b/inst/csv/replacementPatterns.csv @@ -242,7 +242,7 @@ postgresql,SELECT DISTINCT TOP @([0-9]+)rows @a;,SELECT DISTINCT @a LIMIT @rows; postgresql,(SELECT DISTINCT TOP @([0-9]+)rows @a),(SELECT DISTINCT @a LIMIT @rows) postgresql,"WITH @cte AS (SELECT @s1 '@literal' @s2)","WITH @cte AS (SELECT @s1 CAST('@literal' as TEXT) @s2)" postgresql,UPDATE STATISTICS @a;,ANALYZE @a; -postgresql,"ALTER TABLE @table ALTER COLUMN @a @b;","ALTER TABLE @table ALTER COLUMN @a TYPE @b;" +postgresql,"ALTER TABLE @table ALTER COLUMN @([^ ]+)a @b;","ALTER TABLE @table ALTER COLUMN @a TYPE @b;" postgresql,"ALTER TABLE @table ADD @a, @b;","ALTER TABLE @table @a, @b;" postgresql," @b, @c;"," @b, @c;" postgresql,"ALTER TABLE @table ADD @a;","ALTER TABLE @table @a;" @@ -906,7 +906,7 @@ sqlite,IN (SELECT @a) UNION,IN ((SELECT @a)) UNION sqlite,(SELECT @a) UNION,SELECT @a UNION sqlite,UNION (@a),UNION @a sqlite,UNION ALL (@a),UNION ALL @a -sqlite,"ALTER TABLE @table ALTER COLUMN @a @b;","ALTER TABLE @table ALTER COLUMN @a TYPE @b;" +sqlite,"ALTER TABLE @table ALTER COLUMN @a BIGINT;","SELECT 0;" sqlite,"ALTER TABLE @table ADD @a, @b;","ALTER TABLE @table ADD @a; ALTER TABLE @table ADD @b;" hive,...@([0-9]+|y)a,xxx@a hive,TRY_CAST(@a),CAST(@a) diff --git a/tests/testthat/test-translate-postgresql.R b/tests/testthat/test-translate-postgresql.R index 702b547e..a189b96c 100644 --- a/tests/testthat/test-translate-postgresql.R +++ b/tests/testthat/test-translate-postgresql.R @@ -282,5 +282,9 @@ test_that("translate sql server -> postgresql ALTER TABLE ADD CONSTRAINT", { expect_equal_ignore_spaces(sql, "ALTER TABLE cdm.MEASUREMENT ADD CONSTRAINT xpk_MEASUREMENT PRIMARY KEY (measurement_id);") }) +test_that("translate sql server -> postgresql ALTER TABLE ALTER COLUMN", { + sql <- translate("ALTER TABLE my_table ALTER COLUMN a BIGINT;", targetDialect = "postgresql") + expect_equal_ignore_spaces(sql, "ALTER TABLE my_table ALTER COLUMN a TYPE BIGINT;") +}) diff --git a/tests/testthat/test-translate-sqlite.R b/tests/testthat/test-translate-sqlite.R index 1c1c8de4..1faed002 100644 --- a/tests/testthat/test-translate-sqlite.R +++ b/tests/testthat/test-translate-sqlite.R @@ -272,3 +272,12 @@ test_that("translate sql server -> sqlite ALTER TABLE ADD COLUMN", { sql <- translate("ALTER TABLE my_table ADD COLUMN a INT;", targetDialect = "sqlite") expect_equal_ignore_spaces(sql, "ALTER TABLE my_table ADD COLUMN a INT;") }) + +test_that("translate sql server -> sqlite ALTER TABLE ALTER COLUMN", { + sql <- translate("ALTER TABLE my_table ALTER COLUMN a BIGINT;", targetDialect = "sqlite") + expect_equal_ignore_spaces(sql, "SELECT 0;") +}) + + + +