From 43f9fb535cb793ca4db638cffe5114873de19f66 Mon Sep 17 00:00:00 2001 From: edward-burn <9583964+edward-burn@users.noreply.github.com> Date: Fri, 5 Apr 2024 17:32:27 +0100 Subject: [PATCH] updates --- NEWS.md | 1 + R/backend-.R | 3 ++- tests/testthat/test-backend-.R | 7 +++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0f1748db9..ff8f0c3cc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,5 @@ # dbplyr (development version) +* The translation of `stringr::str_like()` now defaults to use case-sensitive LIKE when argument `ignore_case` is set as FALSE instead of when this argument is set as TRUE.(@edward-burn, #1488) # dbplyr 2.5.0 diff --git a/R/backend-.R b/R/backend-.R index b3c2f845e..c7a576cfc 100644 --- a/R/backend-.R +++ b/R/backend-.R @@ -280,12 +280,13 @@ base_scalar <- sql_translator( str_trim = sql_str_trim, str_c = sql_paste(""), str_sub = sql_str_sub("SUBSTR"), + # https://docs.getdbt.com/sql-reference/like is typically case sensitive str_like = function(string, pattern, ignore_case = TRUE) { if (isTRUE(ignore_case)) { cli_abort(c( "Backend does not support case insensitve {.fn str_like}.", i = "Set ignore_case = FALSE for case sensitive match.", - i = "Note, using `tolower()` to cast string and pattern to lower case would also achieve a case insenitive match." + i = "Or use `tolower()` on both arguments to achieve a case insensitive match." )) } else { sql_expr(!!string %LIKE% !!pattern) diff --git a/tests/testthat/test-backend-.R b/tests/testthat/test-backend-.R index 741305e2e..06b86d361 100644 --- a/tests/testthat/test-backend-.R +++ b/tests/testthat/test-backend-.R @@ -101,9 +101,12 @@ test_that("lead and lag translate n to integers", { # strings ----------------------------------------------------------------- -test_that("can translate case insensitive like", { +test_that("can only translate case sensitive str_like", { local_con(simulate_dbi()) - test_translate_sql(str_like(x, "abc", ignore_case = FALSE)) + expect_equal(test_translate_sql(str_like(x, "abc", ignore_case = FALSE)), + sql("`x` LIKE 'abc'")) + expect_equal(test_translate_sql(str_like(x, "ABC", ignore_case = FALSE)), + sql("`x` LIKE 'ABC'")) expect_snapshot( test_translate_sql(str_like(x, "abc", ignore_case = TRUE)), error = TRUE