Skip to content

Commit

Permalink
Redshift: paste() and paste0() translations
Browse files Browse the repository at this point in the history
Fixes #458
  • Loading branch information
hadley committed Sep 22, 2020
1 parent 54ea038 commit 175af9c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* New RedShift translations when used with `RPostgres::Redshift()`.
* `str_replace()` errors since there's no Redshift translation,
and `str_replace_all()` uses `REGEXP_REPLACE()` (#446).

* `paste()` and `paste0()` use `||` (#458).

* `sql_translate_env.Microsoft SQL Server()` now uses `sql_try_cast()` instead
of `sql_cast()` for MSSQL version 11+ (2012+) (@DavidPatShuiFong, #380).
Expand Down
10 changes: 10 additions & 0 deletions R/backend-redshift.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ sql_translate_env.RedshiftConnection <- function(con) {

sql_variant(
sql_translator(.parent = postgres$scalar,

# https://stackoverflow.com/questions/56708136
paste = sql_paste_redshift(" "),
paste0 = sql_paste_redshift(""),
str_c = sql_paste_redshift(""),

# https://docs.aws.amazon.com/redshift/latest/dg/REGEXP_REPLACE.html
str_replace = sql_not_supported("str_replace"),
str_replace_all = function(string, pattern, replacement) {
Expand All @@ -17,3 +23,7 @@ sql_translate_env.RedshiftConnection <- function(con) {

#' @export
sql_translate_env.Redshift <- sql_translate_env.RedshiftConnection

sql_paste_redshift <- function(sep) {
sql_paste_infix(sep, "||", function(x) sql_expr(cast(!!x %as% text)))
}
4 changes: 4 additions & 0 deletions tests/testthat/test-backend-redshift.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ test_that("string translations", {

expect_error(translate_sql(str_replace("xx", ".", "a")), "not available")
expect_equal(translate_sql(str_replace_all("xx", ".", "a")), sql("REGEXP_REPLACE('xx', '.', 'a')"))

expect_equal(translate_sql(paste("x", "y")), sql("'x' || ' ' || 'y'"))
expect_equal(translate_sql(paste0("x", "y")), sql("'x' || 'y'"))
expect_equal(translate_sql(str_c("x", "y")), sql("'x' || 'y'"))
})

0 comments on commit 175af9c

Please sign in to comment.