From 012e555b61f0e83762a7f0112f5ba682d2c2affb Mon Sep 17 00:00:00 2001 From: Jarod Meng Date: Fri, 26 Apr 2024 09:31:43 +0800 Subject: [PATCH] Add paste and paste0 translation --- NEWS.md | 1 + R/dbplyr-sql.R | 2 ++ tests/testthat/test-translate_sql.R | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/NEWS.md b/NEWS.md index 7e91ce3..800e51d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # RPresto 1.4.6.9000 * Fixed a bug whereby nested CTEs result in nested WITH. (#261) +* Fixed `paste()` and `paste0()` translation (#266) # RPresto 1.4.6 diff --git a/R/dbplyr-sql.R b/R/dbplyr-sql.R index ab6c375..ea4df51 100644 --- a/R/dbplyr-sql.R +++ b/R/dbplyr-sql.R @@ -69,6 +69,8 @@ presto_window_functions <- function() { } return(dbplyr::sql_translator( .parent = dbplyr::base_win, + paste = dbplyr::sql_paste_infix(" ", "||", function(x) dbplyr::sql_expr(cast(!!x %as% varchar))), + paste0 = dbplyr::sql_paste_infix("", "||", function(x) dbplyr::sql_expr(cast(!!x %as% varchar))), all = dbplyr::win_recycled("bool_and"), any = dbplyr::win_recycled("bool_or"), n_distinct = dbplyr::win_absent("n_distinct"), diff --git a/tests/testthat/test-translate_sql.R b/tests/testthat/test-translate_sql.R index 584616c..e0382cc 100644 --- a/tests/testthat/test-translate_sql.R +++ b/tests/testthat/test-translate_sql.R @@ -425,3 +425,22 @@ with_locale(test.locale(), test_that)("first(), last(), and nth() work", { dbplyr::sql('NTH_VALUE("x", 2) IGNORE NULLS OVER (ORDER BY "y")') ) }) + +with_locale(test.locale(), test_that)("paste() works", { + s <- setup_mock_dplyr_connection()[["db"]] + + expect_equal( + dbplyr::translate_sql(paste(a, b, c), con = s$con), + dbplyr::sql("\"a\" || ' ' || \"b\" || ' ' || \"c\"") + ) + + expect_equal( + dbplyr::translate_sql(paste(a, b, c, sep = "-"), con = s$con), + dbplyr::sql("\"a\" || '-' || \"b\" || '-' || \"c\"") + ) + + expect_equal( + dbplyr::translate_sql(paste0(a, b, c), con = s$con), + dbplyr::sql("\"a\" || \"b\" || \"c\"") + ) +})