Skip to content

Commit ca694f6

Browse files
committed
implement when all/any and add tests and build docs
1 parent 9577ca4 commit ca694f6

6 files changed

Lines changed: 96 additions & 4 deletions

File tree

r/R/dplyr-funcs-conditional.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ register_bindings_conditional <- function() {
9999
out
100100
})
101101

102+
register_binding("dplyr::when_any", function(..., na_rm = FALSE, size = NULL) {
103+
if (!is.null(size)) {
104+
arrow_not_supported("`when_any()` with `size` specified")
105+
}
106+
args <- list2(...)
107+
if (na_rm) {
108+
args <- lapply(args, function(x) call_binding("coalesce", x, FALSE))
109+
}
110+
Reduce("|", args)
111+
})
112+
113+
register_binding("dplyr::when_all", function(..., na_rm = FALSE, size = NULL) {
114+
if (!is.null(size)) {
115+
arrow_not_supported("`when_all()` with `size` specified")
116+
}
117+
args <- list2(...)
118+
if (na_rm) {
119+
args <- lapply(args, function(x) call_binding("coalesce", x, TRUE))
120+
}
121+
Reduce("&", args)
122+
})
123+
102124
register_binding(
103125
"dplyr::case_when",
104126
function(..., .default = NULL, .ptype = NULL, .size = NULL) {

r/R/dplyr-funcs-doc.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#'
2222
#' The `arrow` package contains methods for 38 `dplyr` table functions, many of
2323
#' which are "verbs" that do transformations to one or more tables.
24-
#' The package also has mappings of 224 R functions to the corresponding
24+
#' The package also has mappings of 226 R functions to the corresponding
2525
#' functions in the Arrow compute library. These allow you to write code inside
2626
#' of `dplyr` methods that call R functions, including many in packages like
2727
#' `stringr` and `lubridate`, and they will get translated to Arrow and run
@@ -214,6 +214,8 @@
214214
#' * [`if_else()`][dplyr::if_else()]
215215
#' * [`n()`][dplyr::n()]
216216
#' * [`n_distinct()`][dplyr::n_distinct()]
217+
#' * [`when_all()`][dplyr::when_all()]
218+
#' * [`when_any()`][dplyr::when_any()]
217219
#'
218220
#' ## hms
219221
#'

r/man/acero.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r/man/read_json_arrow.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r/man/schema.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,69 @@ test_that("external objects are found when they're not in the global environment
517517
tibble(x = c("a", "b"), x2 = c("foo", NA))
518518
)
519519
})
520+
521+
test_that("when_any()", {
522+
# combines with OR
523+
compare_dplyr_binding(
524+
.input |>
525+
mutate(result = when_any(lgl, false)) |>
526+
collect(),
527+
tbl
528+
)
529+
530+
# na_rm=TRUE treats NA as FALSE
531+
compare_dplyr_binding(
532+
.input |>
533+
mutate(result = when_any(lgl, false, na_rm = TRUE)) |>
534+
collect(),
535+
tbl
536+
)
537+
538+
# works in filter()
539+
compare_dplyr_binding(
540+
.input |>
541+
filter(when_any(int > 5, dbl > 3)) |>
542+
collect(),
543+
tbl
544+
)
545+
546+
# size not supported
547+
expect_arrow_eval_error(
548+
when_any(lgl, false, size = 10),
549+
"`when_any\\(\\)` with `size` specified not supported in Arrow",
550+
class = "arrow_not_supported"
551+
)
552+
})
553+
554+
test_that("when_all()", {
555+
# combines with AND
556+
compare_dplyr_binding(
557+
.input |>
558+
mutate(result = when_all(lgl, false)) |>
559+
collect(),
560+
tbl
561+
)
562+
563+
# na_rm=TRUE treats NA as TRUE
564+
compare_dplyr_binding(
565+
.input |>
566+
mutate(result = when_all(lgl, false, na_rm = TRUE)) |>
567+
collect(),
568+
tbl
569+
)
570+
571+
# works in filter()
572+
compare_dplyr_binding(
573+
.input |>
574+
filter(when_all(int > 5, dbl > 3)) |>
575+
collect(),
576+
tbl
577+
)
578+
579+
# size not supported
580+
expect_arrow_eval_error(
581+
when_all(lgl, false, size = 10),
582+
"`when_all\\(\\)` with `size` specified not supported in Arrow",
583+
class = "arrow_not_supported"
584+
)
585+
})

0 commit comments

Comments
 (0)