Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

str_like case sensitivity #1490

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dbplyr (development version)

* Corrected translation of `stringr::str_like()` to use case-sensitive
`LIKE` when argument `ignore_case` is set as `FALSE` (@edward-burn, #1488).

* `clock::add_years()` translates to correct SQL on Spark (@ablack3, #1510).

* Translations for `as.double()` and `as.character()` with Teradata previously
Expand Down
11 changes: 8 additions & 3 deletions R/backend-.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,16 @@ base_scalar <- sql_translator(
str_trim = sql_str_trim,
str_c = sql_paste(""),
str_sub = sql_str_sub("SUBSTR"),
str_like = function(string, pattern, ignore_case = TRUE) {
# https://docs.getdbt.com/sql-reference/like is typically case sensitive (#1490)
str_like = function(string, pattern, ignore_case = FALSE) {
if (isTRUE(ignore_case)) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
sql_expr(!!string %LIKE% !!pattern)
cli_abort(c(
"Backend does not support case insensitive {.fn str_like}.",
i = "Set {.code ignore_case = FALSE} for case sensitive match.",
i = "Use {.fn tolower} on both arguments to achieve a case insensitive match."
))
} else {
cli::cli_abort("Backend only supports case insensitve {.fn str_like}.")
sql_expr(!!string %LIKE% !!pattern)
}
},

Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/_snaps/backend-.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
Error in `x$id`:
! $ operator is invalid for atomic vectors

# can translate case insensitive like
# can only translate case sensitive str_like

Code
test_translate_sql(str_like(x, "abc", ignore_case = FALSE))
test_translate_sql(str_like(x, "abc", ignore_case = TRUE))
Condition
Error in `str_like()`:
! Backend only supports case insensitve `str_like()`.
! Backend does not support case insensitive `str_like()`.
i Set `ignore_case = FALSE` for case sensitive match.
i Use `tolower()` on both arguments to achieve a case insensitive match.

# default raw escapes translated correctly

Expand Down
13 changes: 10 additions & 3 deletions tests/testthat/test-backend-.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,18 @@ 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"))
expect_snapshot(
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
)
})
Expand Down
Loading