From e978fafa1014d566ba7e3fe836a9ccd91f17c1da Mon Sep 17 00:00:00 2001 From: Thomas Hulst Date: Thu, 9 Nov 2023 14:39:08 +0000 Subject: [PATCH 1/5] add str_replace and str_replace_all for Oracle backend --- R/backend-oracle.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/R/backend-oracle.R b/R/backend-oracle.R index dcae5b1d2..b2b05f5d4 100644 --- a/R/backend-oracle.R +++ b/R/backend-oracle.R @@ -133,6 +133,16 @@ sql_translation.Oracle <- function(con) { paste0 = sql_paste_infix("", "||", function(x) sql_expr(cast(!!x %as% text))), str_c = sql_paste_infix("", "||", function(x) sql_expr(cast(!!x %as% text))), + # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/REGEXP_REPLACE.html + # 4th argument is starting position (default: 1 => first char of string) + # 5th argument is occurrence (default: 0 => match all occurrences) + str_replace = function(string, pattern, replacement){ + sql_expr(regexp_replace(!!string, !!pattern, !!replacement, 1L, 1L)) + }, + str_replace_all = function(string, pattern, replacement){ + sql_expr(regexp_replace(!!string, !!pattern, !!replacement)) + }, + # lubridate -------------------------------------------------------------- today = function() sql_expr(TRUNC(CURRENT_TIMESTAMP)), now = function() sql_expr(CURRENT_TIMESTAMP) From c1b015a3f4eda8c2fcbb04f98b988519d9936e8e Mon Sep 17 00:00:00 2001 From: Thomas Hulst Date: Fri, 22 Dec 2023 12:46:50 +0000 Subject: [PATCH 2/5] added snapshot tests and news bullet --- NEWS.md | 3 +++ tests/testthat/_snaps/backend-oracle.md | 18 ++++++++++++++++++ tests/testthat/test-backend-oracle.R | 8 ++++++++ 3 files changed, 29 insertions(+) diff --git a/NEWS.md b/NEWS.md index 45c2e46f0..f89bdb99c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # dbplyr (development version) +* Oracle (@thomashulst, #1402) + * Added support for `str_replace()` and `str_replace_all()` via `REGEXP_REPLACE()` + * Functions qualified with the base namespace are now also translated, e.g. `base::paste0(x, "_1")` is now translated (@mgirlich, #1022). diff --git a/tests/testthat/_snaps/backend-oracle.md b/tests/testthat/_snaps/backend-oracle.md index 72e3f07bb..0c38c1465 100644 --- a/tests/testthat/_snaps/backend-oracle.md +++ b/tests/testthat/_snaps/backend-oracle.md @@ -1,3 +1,21 @@ +# string functions translate correctly + + Code + mutate(lf, x = str_replace(x, "y", "z")) + Output + + SELECT REGEXP_REPLACE(`x`, 'y', 'z', 1, 1) AS `x` + FROM `df` + +--- + + Code + mutate(lf, x = str_replace_all(x, "y", "z")) + Output + + SELECT REGEXP_REPLACE(`x`, 'y', 'z') AS `x` + FROM `df` + # queries translate correctly Code diff --git a/tests/testthat/test-backend-oracle.R b/tests/testthat/test-backend-oracle.R index 5c299600f..c5892b824 100644 --- a/tests/testthat/test-backend-oracle.R +++ b/tests/testthat/test-backend-oracle.R @@ -16,6 +16,14 @@ test_that("paste and paste0 translate correctly", { expect_equal(test_translate_sql(str_c(x, y)), sql("`x` || `y`")) }) + +test_that("string functions translate correctly", { + lf <- lazy_frame(x = "yy", con = simulate_oracle()) + + expect_snapshot(lf |> mutate(x = str_replace(x,"y","z"))) + expect_snapshot(lf |> mutate(x = str_replace_all(x,"y","z"))) +}) + test_that("queries translate correctly", { mf <- lazy_frame(x = 1, con = simulate_oracle()) expect_snapshot(mf %>% head()) From 7dc7227ebbc086fca53169c8ce9047155a9e6fcb Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Fri, 22 Dec 2023 07:15:22 -0600 Subject: [PATCH 3/5] Fix bullet format --- NEWS.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 348b2f6c7..297a7f42a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,7 @@ # dbplyr (development version) -* Oracle (@thomashulst, #1402) - * Added support for `str_replace()` and `str_replace_all()` via `REGEXP_REPLACE()` +* Oracle: Added support for `str_replace()` and `str_replace_all()` via + `REGEXP_REPLACE()` (@thomashulst, #1402). * Allow additional arguments to be passed from `compute()` all the way to `sql_query_save()`-method (@rsund). From 54404365383c4db107438688b9e22e876d58b2fa Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Fri, 22 Dec 2023 07:24:32 -0600 Subject: [PATCH 4/5] Add snapshot tests --- tests/testthat/_snaps/backend-oracle.md | 4 ++-- tests/testthat/test-backend-oracle.R | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/_snaps/backend-oracle.md b/tests/testthat/_snaps/backend-oracle.md index 55cdf2119..17f09e714 100644 --- a/tests/testthat/_snaps/backend-oracle.md +++ b/tests/testthat/_snaps/backend-oracle.md @@ -1,7 +1,7 @@ # string functions translate correctly Code - mutate(lf, x = str_replace(x, "y", "z")) + lf %>% mutate(x = str_replace(x, "y", "z")) Output SELECT REGEXP_REPLACE(`x`, 'y', 'z', 1, 1) AS `x` @@ -10,7 +10,7 @@ --- Code - mutate(lf, x = str_replace_all(x, "y", "z")) + lf %>% mutate(x = str_replace_all(x, "y", "z")) Output SELECT REGEXP_REPLACE(`x`, 'y', 'z') AS `x` diff --git a/tests/testthat/test-backend-oracle.R b/tests/testthat/test-backend-oracle.R index c5892b824..b2a94d3f5 100644 --- a/tests/testthat/test-backend-oracle.R +++ b/tests/testthat/test-backend-oracle.R @@ -20,8 +20,8 @@ test_that("paste and paste0 translate correctly", { test_that("string functions translate correctly", { lf <- lazy_frame(x = "yy", con = simulate_oracle()) - expect_snapshot(lf |> mutate(x = str_replace(x,"y","z"))) - expect_snapshot(lf |> mutate(x = str_replace_all(x,"y","z"))) + expect_snapshot(lf %>% mutate(x = str_replace(x,"y","z"))) + expect_snapshot(lf %>% mutate(x = str_replace_all(x,"y","z"))) }) test_that("queries translate correctly", { From 6ca59037129e8fe7c7bafd5d8b0030300b0554c7 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Fri, 22 Dec 2023 07:26:31 -0600 Subject: [PATCH 5/5] Tweak snapshot tests --- tests/testthat/_snaps/backend-oracle.md | 15 ++++----------- tests/testthat/test-backend-oracle.R | 8 +++++--- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/tests/testthat/_snaps/backend-oracle.md b/tests/testthat/_snaps/backend-oracle.md index 17f09e714..e50731905 100644 --- a/tests/testthat/_snaps/backend-oracle.md +++ b/tests/testthat/_snaps/backend-oracle.md @@ -1,20 +1,13 @@ # string functions translate correctly Code - lf %>% mutate(x = str_replace(x, "y", "z")) + test_translate_sql(str_replace(col, "pattern", "replacement")) Output - - SELECT REGEXP_REPLACE(`x`, 'y', 'z', 1, 1) AS `x` - FROM `df` - ---- - + REGEXP_REPLACE(`col`, 'pattern', 'replacement', 1, 1) Code - lf %>% mutate(x = str_replace_all(x, "y", "z")) + test_translate_sql(str_replace_all(col, "pattern", "replacement")) Output - - SELECT REGEXP_REPLACE(`x`, 'y', 'z') AS `x` - FROM `df` + REGEXP_REPLACE(`col`, 'pattern', 'replacement') # queries translate correctly diff --git a/tests/testthat/test-backend-oracle.R b/tests/testthat/test-backend-oracle.R index b2a94d3f5..9824c8245 100644 --- a/tests/testthat/test-backend-oracle.R +++ b/tests/testthat/test-backend-oracle.R @@ -18,10 +18,12 @@ test_that("paste and paste0 translate correctly", { test_that("string functions translate correctly", { - lf <- lazy_frame(x = "yy", con = simulate_oracle()) + local_con(simulate_oracle()) - expect_snapshot(lf %>% mutate(x = str_replace(x,"y","z"))) - expect_snapshot(lf %>% mutate(x = str_replace_all(x,"y","z"))) + expect_snapshot({ + test_translate_sql(str_replace(col, "pattern", "replacement")) + test_translate_sql(str_replace_all(col, "pattern", "replacement")) + }) }) test_that("queries translate correctly", {