Skip to content

Commit

Permalink
More robust detection of string concatenation for BigQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
schuemie committed May 8, 2024
1 parent 6e13c66 commit 0ff607b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Bugfixes:

2. Fixed translation of `ALTER TABLE ALTER COLUMN` on PostgreSQL.

3. More robust detection of string concatenation for BigQuery.


SqlRender 1.17.0
================
Expand Down
25 changes: 17 additions & 8 deletions inst/csv/replacementPatterns.csv
Original file line number Diff line number Diff line change
Expand Up @@ -726,15 +726,24 @@ bigquery,"DATEADD(yy,@years,@date)","DATE_ADD(@date, INTERVAL @years YEAR)"
bigquery,"DATEADD(yyyy,@years,@date)","DATE_ADD(@date, INTERVAL @years YEAR)"
bigquery,"DATEADD(year,@years,@date)","DATE_ADD(@date, INTERVAL @years YEAR)"
bigquery,INTERVAL @(-?[0-9]+)a.0,INTERVAL @a
bigquery,@(([a-z]+\(.*\))|([a-z0-9_]+))a + '@b',"CONCAT(@a, '@b')"
bigquery,'@a' + @(([a-z]+\(.*\))|([a-z0-9_]+))b,"CONCAT('@a', @b)"
bigquery,CAST(@a AS VARCHAR(@b)) + @(([a-z]+\(.*\))|([a-z0-9_]+))c,"CONCAT(CAST(@a AS VARCHAR(@b)),@c)"
bigquery,@(([a-z]+\(.*\))|([a-z0-9_]+))a + CAST(@b AS VARCHAR(@c)),"CONCAT(@a, CAST(@b AS VARCHAR(@c))"
bigquery,CAST(@a AS VARCHAR) + @(([a-z]+\(.*\))|([a-z0-9_]+))b,"CONCAT(CAST(@a AS VARCHAR), @b)"
bigquery,@(([a-z]+\(.*\))|([a-z0-9_]+))a + CAST(@b AS VARCHAR),"CONCAT(@a, CAST(@b AS VARCHAR))"
bigquery,"CONCAT(@a, @b) + @(([a-z]+\(.*\))|([a-z0-9_]+))c","CONCAT(@a, @b, @c)"
bigquery,"@(([a-z]+\(.*\))|([a-z0-9_]+))a + CONCAT(@b, @c)","CONCAT(@a, @b, @c)"
bigquery,CAST(@a AS VARCHAR) + @b(@c),"CONCAT(CAST(@a AS VARCHAR), @b(@c))"
bigquery,@([a-z]+)a(@b) + CAST(@c AS VARCHAR),"CONCAT(@a(@b), CAST(@c AS VARCHAR))"
bigquery,CAST(@a AS VARCHAR(@n)) + @b(@c),"CONCAT(CAST(@a AS VARCHAR(@n)), @b(@c))"
bigquery,@([a-z]+)a(@b) + CAST(@c AS VARCHAR(@n)),"CONCAT(@a(@b), CAST(@c AS VARCHAR(@n)))"
bigquery,'@a' + @b(@c),"CONCAT('@a', @b(@c))"
bigquery,@([a-z]+)a(@b) + '@c',"CONCAT(@a(@b), '@c')"
bigquery,'@a' + @b FROM,"CONCAT('@a', @b) FROM"
bigquery,@([a-z0-9_]+)a + '@b',"CONCAT(@a, '@b')"
bigquery,CAST(@a AS VARCHAR) + @b FROM,"CONCAT(CAST(@a AS VARCHAR), @b) FROM"
bigquery,@([a-z0-9_]+)a + CAST(@b AS VARCHAR),"CONCAT(@a, CAST(@b AS VARCHAR))"
bigquery,CAST(@a AS VARCHAR(@n)) + @b FROM,"CONCAT(CAST(@a AS VARCHAR(@n)), @b) FROM"
bigquery,@([a-z0-9_]+)a + CAST(@b AS VARCHAR(@n)),"CONCAT(@a, CAST(@b AS VARCHAR(@n)))"
bigquery,CONCAT(@a) + @b(@c),"CONCAT(@a, @b(@c))"
bigquery,@([a-z]+)a(@b) + CONCAT(@c),"CONCAT(@a(@b), @c)"
bigquery,CONCAT(@a) + @b FROM,"CONCAT(@a, @b) FROM"
bigquery,@([a-z0-9_]+)a + CONCAT(@b),"CONCAT(@a, @b)"
bigquery,"CONCAT(@a, CONCAT(@b, @c))","CONCAT(@a, @b, @c)"
bigquery,"CONCAT(CONCAT(@a, @b), @c)","CONCAT(@a, @b, @c)"
bigquery,"CONCAT(CONCAT(@a, @b, @c))","CONCAT(@a, @b, @c)"
bigquery,"STDEV(@a)","STDDEV(@a)"
bigquery,"HASHBYTES('MD5',@a)","md5(@a)"
Expand Down
19 changes: 10 additions & 9 deletions tests/testthat/test-translate-bigquery.R
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,6 @@ test_that("translate sql server -> bigquery DATEFROMPARTS", {
expect_equal_ignore_spaces(sql, "select DATE(2019,1,30)")
})

# test_that("translate sql server -> bigquery offset literal", {
# sql <- translate("create table test_that (\"offset\" STRING);", targetDialect = "bigquery")
# expect_equal_ignore_spaces(sql, "create table test_that (offset STRING);")
# })

test_that("translate sql server -> bigquery EOMONTH()", {
sql <- translate("select eomonth(payer_plan_period_start_date)", targetDialect = "bigquery")
expect_equal_ignore_spaces(
Expand Down Expand Up @@ -467,7 +462,7 @@ test_that("translate sql server -> String concatenation", {
)
expect_equal_ignore_spaces(
sql,
"select CONCAT(last_name, ', ', first_name) from my_table;"
"select CONCAT(last_name, ', ', first_name) FROM my_table;"
)
})

Expand All @@ -477,7 +472,7 @@ test_that("translate sql server -> String concatenation", {
)
expect_equal_ignore_spaces(
sql,
"select CONCAT(first_name, CAST(middle_initial AS STRING), last_name) from my_table;"
"select CONCAT(first_name, CAST(middle_initial AS STRING), last_name) FROM my_table;"
)
})

Expand All @@ -487,7 +482,7 @@ test_that("translate sql server -> String concatenation", {
)
expect_equal_ignore_spaces(
sql,
"select CONCAT(first_name, CAST(middle_initial AS STRING), last_name) from my_table;"
"select CONCAT(first_name, CAST(middle_initial AS STRING), last_name) FROM my_table;"
)
})

Expand All @@ -497,7 +492,7 @@ test_that("translate sql server -> String concatenation", {
)
expect_equal_ignore_spaces(
sql,
"select subgroup_id, CONCAT('Persons aged ', CONCAT(cast(age_low as STRING), 'to ', cast(age_high as STRING), 'with gender = '), gender_name) from subgroups;"
"select subgroup_id, CONCAT('Persons aged ', CAST(age_low AS STRING), CONCAT('to ', cast(age_high as STRING)), 'with gender = ', gender_name ) FROM subgroups;"
)
})

Expand Down Expand Up @@ -549,3 +544,9 @@ test_that("translate sql server -> bigquery quotes", {
sql <- translate("SELECT \"a\" from t;", targetDialect = "bigquery")
expect_equal_ignore_spaces(sql, "select `a` from t;")
})

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)")
})

0 comments on commit 0ff607b

Please sign in to comment.