Skip to content

Commit 13ee660

Browse files
committed
handle list inputs
1 parent f69a4ad commit 13ee660

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

r/R/dplyr-funcs-conditional.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,15 @@ parse_from_to_mapping <- function(x, from, to) {
8989
query <- vector("list", n)
9090
value <- vector("list", n)
9191
for (i in seq_len(n)) {
92+
from_i <- from[[i]]
9293
# Handle NA specially: use is.na() since x == NA returns NA in Arrow
93-
if (is.na(from[[i]])) {
94+
if (length(from_i) == 1 && is.na(from_i)) {
9495
query[[i]] <- call_binding("is.na", x)
96+
} else if (length(from_i) > 1) {
97+
# Multiple values: use %in% to match any
98+
query[[i]] <- call_binding("%in%", x, from_i)
9599
} else {
96-
query[[i]] <- x == from[[i]]
100+
query[[i]] <- x == from_i
97101
}
98102
value[[i]] <- Expression$scalar(to[[i]])
99103
}

r/tests/testthat/test-dplyr-funcs-conditional.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,14 @@ test_that("replace_values()", {
581581
tbl
582582
)
583583

584+
# from/to with list of vectors - multiple values map to single replacement
585+
compare_dplyr_binding(
586+
.input |>
587+
mutate(result = replace_values(chr, from = list(c("a", "b"), "c"), to = c("AB", "C"))) |>
588+
collect(),
589+
tbl
590+
)
591+
584592
# unmatched values kept
585593
compare_dplyr_binding(
586594
.input |>
@@ -630,6 +638,11 @@ test_that("replace_values()", {
630638
)
631639

632640
# validation errors
641+
expect_arrow_eval_error(
642+
replace_values(chr, "A"),
643+
"Each argument to replace_values\\(\\) must be a two-sided formula",
644+
class = "validation_error"
645+
)
633646
expect_arrow_eval_error(
634647
replace_values(chr, "a" ~ "A", from = "b"),
635648
"Can't use both `...` and `from`/`to` in replace_values\\(\\)",
@@ -659,6 +672,14 @@ test_that("recode_values()", {
659672
tbl
660673
)
661674

675+
# from/to with list of vectors - multiple values map to single replacement
676+
compare_dplyr_binding(
677+
.input |>
678+
mutate(result = recode_values(chr, from = list(c("a", "b"), "c"), to = c("AB", "C"))) |>
679+
collect(),
680+
tbl
681+
)
682+
662683
# custom default
663684
compare_dplyr_binding(
664685
.input |>
@@ -697,6 +718,11 @@ test_that("recode_values()", {
697718
"`\\.\\.\\.` can't be empty",
698719
class = "validation_error"
699720
)
721+
expect_arrow_eval_error(
722+
recode_values(chr, "A"),
723+
"Each argument to recode_values\\(\\) must be a two-sided formula",
724+
class = "validation_error"
725+
)
700726
expect_arrow_eval_error(
701727
recode_values(chr, "a" ~ "A", from = "b"),
702728
"Can't use both `...` and `from`/`to` in recode_values\\(\\)",

0 commit comments

Comments
 (0)