Skip to content

Commit 86b4a41

Browse files
author
zhangli20
committed
fix case_column_or_null with nullable when conditions
1 parent 8fd792f commit 86b4a41

File tree

2 files changed

+32
-3
lines changed
  • datafusion

2 files changed

+32
-3
lines changed

datafusion/physical-expr/src/expressions/case.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,10 @@ impl CaseExpr {
346346
.downcast_ref::<BooleanArray>()
347347
.expect("predicate should evaluate to a boolean array");
348348
// invert the bitmask
349-
let bit_mask = not(bit_mask)?;
349+
let bit_mask = match bit_mask.null_count() {
350+
0 => not(bit_mask)?,
351+
_ => not(&prep_null_mask_filter(bit_mask))?,
352+
};
350353
match then_expr.evaluate(batch)? {
351354
ColumnarValue::Array(array) => {
352355
Ok(ColumnarValue::Array(nullif(&array, &bit_mask)?))
@@ -885,6 +888,32 @@ mod tests {
885888
Ok(())
886889
}
887890

891+
#[test]
892+
fn test_when_null_and_some_cond_else_null() -> Result<()> {
893+
let batch = case_test_batch()?;
894+
let schema = batch.schema();
895+
896+
let when = binary(
897+
Arc::new(Literal::new(ScalarValue::Boolean(None))),
898+
Operator::And,
899+
binary(col("a", &schema)?, Operator::Eq, lit("foo"), &schema)?,
900+
&schema,
901+
)?;
902+
let then = col("a", &schema)?;
903+
904+
// SELECT CASE WHEN (NULL AND a = 'foo') THEN a ELSE NULL END
905+
let expr = Arc::new(CaseExpr::try_new(None, vec![(when, then)], None)?);
906+
let result = expr
907+
.evaluate(&batch)?
908+
.into_array(batch.num_rows())
909+
.expect("Failed to convert to array");
910+
let result = as_string_array(&result);
911+
912+
// all result values should be null
913+
assert_eq!(result.logical_null_count(), batch.num_rows());
914+
Ok(())
915+
}
916+
888917
fn case_test_batch() -> Result<RecordBatch> {
889918
let schema = Schema::new(vec![Field::new("a", DataType::Utf8, true)]);
890919
let a = StringArray::from(vec![Some("foo"), Some("baz"), None, Some("bar")]);

datafusion/sqllogictest/test_files/case.slt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ NULL
5050
6
5151
NULL
5252
NULL
53-
7
53+
NULL
5454

5555
# column or implicit null
5656
query I
@@ -61,7 +61,7 @@ NULL
6161
6
6262
NULL
6363
NULL
64-
7
64+
NULL
6565

6666
# scalar or scalar (string)
6767
query T

0 commit comments

Comments
 (0)