Skip to content

ARROW-18195: [C++] Fix case_when produces bad data when condition has nulls - #15131

Merged
pitrou merged 5 commits into
apache:masterfrom
wjones127:ARROW-18195-case-when-na
Jan 4, 2023
Merged

pitrou merged 5 commits into
apache:masterfrom
wjones127:ARROW-18195-case-when-na

Conversation

@wjones127

@wjones127 wjones127 commented Dec 30, 2022 •

Copy link
Copy Markdown
Member

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).

@github-actions

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

⚠️ Ticket has not been started in JIRA, please click 'Start Progress'.

@wjones127
wjones127 force-pushed the ARROW-18195-case-when-na branch from 524c904 to 755e9b3 Compare December 30, 2022 19:22
@wjones127 wjones127 changed the title ARROW-18195: [C++] Fix case_when uses wrong condition on last value ARROW-18195: [C++] Fix case_when produces bad data when condition has nulls Dec 30, 2022
@wjones127

Copy link
Copy Markdown
Member Author

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  TRUE

Created on 2022-12-30 with reprex v2.0.2

@wjones127
wjones127 force-pushed the ARROW-18195-case-when-na branch from 755e9b3 to 5611d2d Compare December 30, 2022 19:33
@wjones127
wjones127 marked this pull request as ready for review December 30, 2022 19:33
@pitrou

pitrou commented Jan 3, 2023

Copy link
Copy Markdown
Member

Hmm, it is worrying that we let this slip through. Do you think there would be an easy way to test this more thoroughly?

@wjones127

Copy link
Copy Markdown
Member Author

I think the ifelse kernel has a more thorough tests that we can adapt for case when:

TYPED_TEST(TestIfElseBaseBinary, IfElseBaseBinaryRand) {
using ArrayType = typename TypeTraits<TypeParam>::ArrayType;
using OffsetType = typename TypeTraits<TypeParam>::OffsetType::c_type;
auto type = TypeTraits<TypeParam>::type_singleton();
random::RandomArrayGenerator rand(/*seed=*/0);
int64_t len = 1000;
// this is to check the BitBlockCount::AllSet/ NoneSet code paths
ASSERT_OK_AND_ASSIGN(auto temp1, MakeArrayFromScalar(BooleanScalar(true), 64));
ASSERT_OK_AND_ASSIGN(auto temp2, MakeArrayFromScalar(BooleanScalar(false), 64));

@wjones127

Copy link
Copy Markdown
Member Author

@pitrou I've written a new test function that exercises all code paths within ExecArrayCaseWhen. I validated it hits all branches by making sure it hit relevant breakpoints in lldb. This includes whether there is an else value and whether there are nulls in the condition.

@pitrou

pitrou commented Jan 4, 2023

Copy link
Copy Markdown
Member

Thanks @wjones127 . I've enhanced this a bit so that we can random-test more input types for better coverage (including nested types).

@pitrou
pitrou merged commit c6eb4aa into apache:master Jan 4, 2023
@wjones127
wjones127 deleted the ARROW-18195-case-when-na branch January 4, 2023 19:15
EpsilonPrime pushed a commit to EpsilonPrime/arrow that referenced this pull request Jan 5, 2023
… 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>
@ursabot

ursabot commented Jan 5, 2023

Copy link
Copy Markdown

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.
Conbench compare runs links:
[Finished ⬇️0.0% ⬆️0.0%] ec2-t3-xlarge-us-east-2
[Failed ⬇️0.0% ⬆️0.0%] test-mac-arm
[Finished ⬇️16.33% ⬆️0.0%] ursa-i9-9960x
[Failed ⬇️0.0% ⬆️0.0%] ursa-thinkcentre-m75q
Buildkite builds:
[Finished] c6eb4aad ec2-t3-xlarge-us-east-2
[Failed] c6eb4aad test-mac-arm
[Finished] c6eb4aad ursa-i9-9960x
[Failed] c6eb4aad ursa-thinkcentre-m75q
[Finished] c45ce810 ec2-t3-xlarge-us-east-2
[Failed] c45ce810 test-mac-arm
[Finished] c45ce810 ursa-i9-9960x
[Failed] c45ce810 ursa-thinkcentre-m75q
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

@ursabot

ursabot commented Jan 5, 2023

Copy link
Copy Markdown

['Python', 'R'] benchmarks have high level of regressions.
ursa-i9-9960x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants