Skip to content
Merged
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
8 changes: 8 additions & 0 deletions r/R/dplyr-funcs-simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ common_type <- function(exprs) {
}

cast_or_parse <- function(x, type) {
# A null scalar (e.g. a bare `NA`, which is logical) carries no data, so
# skip the value cast and just create a null of the target type. This
# avoids unsupported casts like bool -> date32 (GH-38358). Only Scalars
# have `is_valid`; Arrays (e.g. from the `%in%` binding) take the normal path.
if (inherits(x, "Scalar") && !x$is_valid) {
return(Scalar$create(NULL)$cast(type))
}

to_type_id <- type$id
if (to_type_id %in% c(Type[["DECIMAL32"]], Type[["DECIMAL64"]], Type[["DECIMAL128"]], Type[["DECIMAL256"]])) {
# TODO: determine the minimum size of decimal (or integer) required to
Expand Down
23 changes: 23 additions & 0 deletions r/tests/testthat/test-dplyr-funcs-conditional.R
Original file line number Diff line number Diff line change
Expand Up @@ -876,3 +876,26 @@ test_that("recode_values()", {
class = "arrow_not_supported"
)
})

test_that("if_else with logical NA and a date/timestamp column", {
# GH-38358: a bare `NA` is logical and can't be cast to date32, so
# if_else(bool, bool, date32) had no matching kernel
date_tbl <- tibble::tibble(
date = as.Date(c("2013-01-01", "2013-01-02", "2033-01-03", "2013-01-04")),
ts = as.POSIXct(
c("2013-01-01 01:00:00", "2013-01-02 02:00:00", "2033-01-03 03:00:00", "2013-01-04 04:00:00"),
tz = "UTC"
)
)

compare_dplyr_binding(
.input |>
mutate(
date2 = if_else(date > as.Date("2014-01-01"), NA, date),
date3 = if_else(date > as.Date("2014-01-01"), date, NA),
Comment on lines +894 to +895

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its the NAs here that are the issue?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we're making sure they don't get converted to the default logical NAs instead of type-specific NAs

ts2 = if_else(ts > as.POSIXct("2014-01-01", tz = "UTC"), NA, ts)
) |>
collect(),
date_tbl
)
})
Loading