-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: handle scalar predicates in CASE expressions to prevent internal errors for InfallibleExprOrNull eval method #14156
Merged
alamb
merged 3 commits into
apache:main
from
avkirilishin:predicate-did-not-evaluate-to-an-array-14099
Jan 18, 2025
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -344,7 +344,16 @@ impl CaseExpr { | |
fn case_column_or_null(&self, batch: &RecordBatch) -> Result<ColumnarValue> { | ||
let when_expr = &self.when_then_expr[0].0; | ||
let then_expr = &self.when_then_expr[0].1; | ||
if let ColumnarValue::Array(bit_mask) = when_expr.evaluate(batch)? { | ||
|
||
let when_expr_value = when_expr.evaluate(batch)?; | ||
let when_expr_value = match when_expr_value { | ||
ColumnarValue::Scalar(_) => { | ||
ColumnarValue::Array(when_expr_value.into_array(batch.num_rows())?) | ||
} | ||
other => other, | ||
}; | ||
|
||
if let ColumnarValue::Array(bit_mask) = when_expr_value { | ||
let bit_mask = bit_mask | ||
.as_any() | ||
.downcast_ref::<BooleanArray>() | ||
|
@@ -896,6 +905,53 @@ mod tests { | |
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn case_with_scalar_predicate() -> Result<()> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally recommend adding a .slt test for this in addition (and possibly also instead): --- a/datafusion/sqllogictest/test_files/case.slt
+++ b/datafusion/sqllogictest/test_files/case.slt
@@ -235,3 +235,10 @@ SELECT CASE WHEN a < 5 THEN a + b ELSE b - NVL(a, 0) END FROM foo
NULL
NULL
7
+
+# Reproducer for
+# https://github.com/apache/datafusion/issues/14099
+query I
+SELECT - 79 * + 91 * - COUNT ( * ) * + - 2 * + - NULLIF ( - 49, - COALESCE ( - + 69, - COALESCE ( + COALESCE ( - 20, ( - 18 ) * + COUNT ( * ) + - 93, - CASE 51 WHEN + COUNT ( * ) + 28 THEN 0 ELSE + 29 * + CASE ( 50 ) WHEN - ( - ( CASE WHEN NOT + 37 IS NULL THEN + COUNT ( * ) END ) ) THEN NULL WHEN - 46 + 87 * - 28 THEN 85 WHEN - COUNT ( * ) THEN NULL END END ), COUNT ( * ) - 39 ) * + 22 ) / - COUNT ( * ) )
+----
+-704522 |
||
let batch = case_test_batch_nulls()?; | ||
let schema = batch.schema(); | ||
|
||
// SELECT CASE WHEN TRUE THEN load4 END | ||
let when = lit(true); | ||
let then = col("load4", &schema)?; | ||
let expr = generate_case_when_with_type_coercion( | ||
None, | ||
vec![(when, then)], | ||
None, | ||
schema.as_ref(), | ||
)?; | ||
|
||
// many rows | ||
let result = expr | ||
.evaluate(&batch)? | ||
.into_array(batch.num_rows()) | ||
.expect("Failed to convert to array"); | ||
let result = | ||
as_float64_array(&result).expect("failed to downcast to Float64Array"); | ||
let expected = &Float64Array::from(vec![ | ||
Some(1.77), | ||
None, | ||
None, | ||
Some(1.78), | ||
None, | ||
Some(1.77), | ||
]); | ||
assert_eq!(expected, result); | ||
|
||
// one row | ||
let expected = Float64Array::from(vec![Some(1.1)]); | ||
let batch = | ||
RecordBatch::try_new(Arc::clone(&schema), vec![Arc::new(expected.clone())])?; | ||
let result = expr | ||
.evaluate(&batch)? | ||
.into_array(batch.num_rows()) | ||
.expect("Failed to convert to array"); | ||
let result = | ||
as_float64_array(&result).expect("failed to downcast to Float64Array"); | ||
assert_eq!(&expected, result); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn case_expr_matches_and_nulls() -> Result<()> { | ||
let batch = case_test_batch_nulls()?; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is correct, but it effectively expands out the array even when the argument is a constant. We can probably make it more efficient by handling the other case too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a follow up, I have added one in
case
expr constant handling for when<scalar>
#14159