Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions r/R/dplyr-funcs-doc.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
#' * [`floor()`][base::floor()]
#' * [`format()`][base::format()]
#' * [`grepl()`][base::grepl()]
#' * [`gsub()`][base::gsub()]
#' * [`gsub()`][base::gsub()]: multiple patterns or replacements not supported; `pattern` and `replacement` must be length 1 character vectors
#' * [`ifelse()`][base::ifelse()]
#' * [`is.character()`][base::is.character()]
#' * [`is.double()`][base::is.double()]
Expand Down Expand Up @@ -186,7 +186,7 @@
#' Valid values are "s", "ms" (default), "us", "ns".
#' * [`strrep()`][base::strrep()]
#' * [`strsplit()`][base::strsplit()]
#' * [`sub()`][base::sub()]
#' * [`sub()`][base::sub()]: multiple patterns or replacements not supported; `pattern` and `replacement` must be length 1 character vectors
#' * [`substr()`][base::substr()]: `start` and `stop` must be length 1
#' * [`substring()`][base::substring()]
#' * [`sum()`][base::sum()]
Expand Down Expand Up @@ -344,10 +344,10 @@
#' * [`str_length()`][stringr::str_length()]
#' * [`str_like()`][stringr::str_like()]
#' * [`str_pad()`][stringr::str_pad()]
#' * [`str_remove()`][stringr::str_remove()]
#' * [`str_remove_all()`][stringr::str_remove_all()]
#' * [`str_replace()`][stringr::str_replace()]
#' * [`str_replace_all()`][stringr::str_replace_all()]
#' * [`str_remove()`][stringr::str_remove()]: multiple patterns not supported; `pattern` must be a length 1 character vector
#' * [`str_remove_all()`][stringr::str_remove_all()]: multiple patterns not supported; `pattern` must be a length 1 character vector
#' * [`str_replace()`][stringr::str_replace()]: multiple patterns or replacements not supported; `pattern` and `replacement` must be length 1 character vectors
#' * [`str_replace_all()`][stringr::str_replace_all()]: multiple patterns or replacements not supported; `pattern` and `replacement` must be length 1 character vectors
#' * [`str_replace_na()`][stringr::str_replace_na()]
#' * [`str_split()`][stringr::str_split()]: Case-insensitive string splitting and splitting into 0 parts not supported
#' * [`str_starts()`][stringr::str_starts()]
Expand Down
42 changes: 34 additions & 8 deletions r/R/dplyr-funcs-string.R
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,14 @@ register_bindings_string_regex <- function() {
arrow_r_string_replace_function <- function(max_replacements) {
function(pattern, replacement, x, ignore.case = FALSE, fixed = FALSE) {
if (length(pattern) != 1) {
validation_error("`pattern` must be a length 1 character vector")
validation_error(
"Multiple patterns not supported: `pattern` must be a length 1 character vector"
)
}
Comment thread
thisisnic marked this conversation as resolved.
if (length(replacement) != 1) {
validation_error("`replacement` must be a length 1 character vector")
validation_error(
"Multiple replacements not supported: `replacement` must be a length 1 character vector"
)
Comment thread
thisisnic marked this conversation as resolved.
Comment on lines 369 to +372
}
Expression$create(
ifelse(fixed && !ignore.case, "replace_substring", "replace_substring_regex"),
Expand Down Expand Up @@ -407,12 +411,34 @@ register_bindings_string_regex <- function() {
}
}

register_binding("base::sub", arrow_r_string_replace_function(1L))
register_binding("base::gsub", arrow_r_string_replace_function(-1L))
register_binding("stringr::str_replace", arrow_stringr_string_replace_function(1L))
register_binding("stringr::str_replace_all", arrow_stringr_string_replace_function(-1L))
register_binding("stringr::str_remove", arrow_stringr_string_remove_function(1L))
register_binding("stringr::str_remove_all", arrow_stringr_string_remove_function(-1L))
replace_notes <- paste(
"multiple patterns or replacements not supported;",
"`pattern` and `replacement` must be length 1 character vectors"
)
remove_notes <- "multiple patterns not supported; `pattern` must be a length 1 character vector"

register_binding("base::sub", arrow_r_string_replace_function(1L), notes = replace_notes)
register_binding("base::gsub", arrow_r_string_replace_function(-1L), notes = replace_notes)
register_binding(
"stringr::str_replace",
arrow_stringr_string_replace_function(1L),
notes = replace_notes
)
register_binding(
"stringr::str_replace_all",
arrow_stringr_string_replace_function(-1L),
notes = replace_notes
)
register_binding(
"stringr::str_remove",
arrow_stringr_string_remove_function(1L),
notes = remove_notes
)
register_binding(
"stringr::str_remove_all",
arrow_stringr_string_remove_function(-1L),
notes = remove_notes
)

register_binding("stringr::str_replace_na", function(string, replacement = "NA") {
if (!is.character(replacement) || length(replacement) != 1) {
Expand Down
12 changes: 6 additions & 6 deletions r/man/acero.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions r/tests/testthat/test-dplyr-funcs-string.R
Original file line number Diff line number Diff line change
Expand Up @@ -1586,3 +1586,22 @@ test_that("str_replace_na", {
df
)
})

test_that("GH-45314: multiple patterns or replacements give an informative error", {
x <- Expression$field_ref("x")

expect_error(
call_binding("str_replace_all", x, c("F" = "_", "b" = "")),
regexp = "Multiple patterns not supported: `pattern` must be a length 1 character vector"
)

expect_error(
call_binding("gsub", "o", c("u", "a"), x),
regexp = "Multiple replacements not supported: `replacement` must be a length 1 character vector"
)

expect_error(
call_binding("str_remove_all", x, c("F", "b")),
regexp = "Multiple patterns not supported: `pattern` must be a length 1 character vector"
)
})
Loading