Repository navigation
ARROW-18195: [C++] Fix case_when produces bad data when condition has nulls - #15131
Conversation
|
|
524c904 to
755e9b3
Compare
|
Double checked, and @thisisnic's repros are all fixed by this: library(dplyr, warn.conflicts = FALSE)
library(arrow, warn.conflicts = FALSE)
# Specific conditions where this happens: a table with one NA and 64 or more non-NA values
test_df = tibble::tibble(x = c(NA, rep("foo", 64)))
test_arrow = arrow_table(test_df)
# the non-arrow version; all the final values are 1
test_df %>%
mutate(y = case_when(x == 'foo' ~ 1, is.na(x) ~ NA_real_)) %>%
tail()
#> # A tibble: 6 × 2
#> x y
#> <chr> <dbl>
#> 1 foo 1
#> 2 foo 1
#> 3 foo 1
#> 4 foo 1
#> 5 foo 1
#> 6 foo 1
# the arrow version; the final values is NA
test_arrow %>%
mutate(y = case_when(x == 'foo' ~ 1, is.na(x) ~ NA_real_)) %>%
collect() %>%
tail()
#> # A tibble: 6 × 2
#> x y
#> <chr> <dbl>
#> 1 foo 1
#> 2 foo 1
#> 3 foo 1
#> 4 foo 1
#> 5 foo 1
#> 6 foo 1
# it's fine if there are less than 65 values in the table (i.e. but still contains an NA)
test_arrow[1:64,] %>%
mutate(y = case_when(x == 'foo' ~ 1, is.na(x) ~ NA_real_)) %>%
collect() %>%
tail()
#> # A tibble: 6 × 2
#> x y
#> <chr> <dbl>
#> 1 foo 1
#> 2 foo 1
#> 3 foo 1
#> 4 foo 1
#> 5 foo 1
#> 6 foo 1
# everything is fine when the comparison is being done on doubles and return value is char
test_df2 = tibble::tibble(x = c(NA, rep(1, 64)))
test_arrow2 = arrow_table(test_df2)
test_arrow2 %>%
mutate(y = case_when(x == 1 ~ "winning", is.na(x) ~ NA_character_)) %>%
collect() %>%
tail()
#> # A tibble: 6 × 2
#> x y
#> <dbl> <chr>
#> 1 1 winning
#> 2 1 winning
#> 3 1 winning
#> 4 1 winning
#> 5 1 winning
#> 6 1 winning
# also breaks when source value is boolean and target value is double
test_df3 = tibble::tibble(x = c(NA, rep(TRUE, 64)))
test_arrow3 = arrow_table(test_df3)
test_arrow3 %>%
mutate(y = case_when(x == TRUE ~ 1, is.na(x) ~ NA_real_)) %>%
collect() %>%
tail()
#> # A tibble: 6 × 2
#> x y
#> <lgl> <dbl>
#> 1 TRUE 1
#> 2 TRUE 1
#> 3 TRUE 1
#> 4 TRUE 1
#> 5 TRUE 1
#> 6 TRUE 1
# also broken for when target is integer
test_df4 = tibble::tibble(x = c(NA, rep(TRUE, 64)))
test_arrow4 = arrow_table(test_df4)
test_arrow4 %>%
mutate(y = case_when(x == TRUE ~ 1L, is.na(x) ~ 2L)) %>%
collect() %>%
tail()
#> # A tibble: 6 × 2
#> x y
#> <lgl> <int>
#> 1 TRUE 1
#> 2 TRUE 1
#> 3 TRUE 1
#> 4 TRUE 1
#> 5 TRUE 1
#> 6 TRUE 1
# broken for logical to logical
test_df5 = tibble::tibble(x = c(NA, rep(TRUE, 64)))
test_arrow5 = arrow_table(test_df5)
test_arrow5 %>%
mutate(y = case_when(x == TRUE ~ TRUE, is.na(x) ~ FALSE)) %>%
collect() %>%
tail()
#> # A tibble: 6 × 2
#> x y
#> <lgl> <lgl>
#> 1 TRUE TRUE
#> 2 TRUE TRUE
#> 3 TRUE TRUE
#> 4 TRUE TRUE
#> 5 TRUE TRUE
#> 6 TRUE TRUECreated on 2022-12-30 with reprex v2.0.2 |
755e9b3 to
5611d2d
Compare
|
Hmm, it is worrying that we let this slip through. Do you think there would be an easy way to test this more thoroughly? |
|
I think the ifelse kernel has a more thorough tests that we can adapt for case when: arrow/cpp/src/arrow/compute/kernels/scalar_if_else_test.cc Lines 508 to 518 in 63b91cc |
|
@pitrou I've written a new test function that exercises all code paths within |
|
Thanks @wjones127 . I've enhanced this a bit so that we can random-test more input types for better coverage (including nested types). |
… nulls (apache#15131) When the condition has nulls, we were going into a branch that did not advance the `offset`. On subsequent blocks (each 64 values), the `mask` (which tells us which positions have already been set by previous cases) was always already set, so the condition was always evaluated as False regardless of its true values. So the first 64 values would be correct, but every value afterwards would skip the condition with nulls and use the next condition (or the else). Lead-authored-by: Will Jones <willjones127@gmail.com> Co-authored-by: Antoine Pitrou <antoine@python.org> Signed-off-by: Antoine Pitrou <antoine@python.org>
|
Benchmark runs are scheduled for baseline = c45ce81 and contender = c6eb4aa. c6eb4aa is a master commit associated with this PR. Results will be available as each benchmark for each run completes. |
|
['Python', 'R'] benchmarks have high level of regressions. |
When the condition has nulls, we were going into a branch that did not advance the
offset. On subsequent blocks (each 64 values), themask(which tells us which positions have already been set by previous cases) was always already set, so the condition was always evaluated as False regardless of its true values. So the first 64 values would be correct, but every value afterwards would skip the condition with nulls and use the next condition (or the else).