Skip to content
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 case evaluation with NULL #6477

Merged
merged 1 commit into from
May 30, 2023
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 datafusion/core/tests/sqllogictests/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,14 @@ NULL
NULL
4

# issue: https://github.com/apache/arrow-datafusion/issues/6376
query I
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 matches postgres:

postgres=# select case when a = 0 then 123 end from (values(1), (0), (null)) as t(a);
 case
------

  123

(3 rows)

select case when a = 0 then 123 end from (values(1), (0), (null)) as t(a);
----
NULL
123
NULL

# csv_query_sum_cast() {

statement ok
Expand Down
30 changes: 15 additions & 15 deletions datafusion/physical-expr/src/expressions/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::borrow::Cow;
use std::{any::Any, sync::Arc};

use crate::expressions::try_cast;
Expand All @@ -23,7 +24,7 @@ use crate::physical_expr::down_cast_any_ref;
use crate::PhysicalExpr;
use arrow::array::*;
use arrow::compute::kernels::zip::zip;
use arrow::compute::{and, and_kleene, eq_dyn, is_not_null, is_null, not, or, or_kleene};
use arrow::compute::{and, eq_dyn, is_null, not, or, prep_null_mask_filter};
use arrow::datatypes::{DataType, Schema};
use arrow::record_batch::RecordBatch;
use datafusion_common::{cast::as_boolean_array, DataFusionError, Result};
Expand Down Expand Up @@ -139,7 +140,10 @@ impl CaseExpr {
// build boolean array representing which rows match the "when" value
let when_match = eq_dyn(&when_value, base_value.as_ref())?;
// Treat nulls as false
let when_match = and_kleene(&when_match, &is_not_null(&when_match)?)?;
let when_match = match when_match.null_count() {
0 => Cow::Borrowed(&when_match),
_ => Cow::Owned(prep_null_mask_filter(&when_match)),
};

let then_value = self.when_then_expr[i]
.1
Expand Down Expand Up @@ -189,39 +193,35 @@ impl CaseExpr {
let when_value = self.when_then_expr[i]
.0
.evaluate_selection(batch, &remainder)?;
// Treat 'NULL' as false value
let when_value = match when_value {
ColumnarValue::Scalar(value) if value.is_null() => {
continue;
}
_ => when_value,
};
let when_value = when_value.into_array(batch.num_rows());
let when_value = as_boolean_array(&when_value).map_err(|e| {
DataFusionError::Context(
"WHEN expression did not return a BooleanArray".to_string(),
Box::new(e),
)
})?;
// Treat 'NULL' as false value
let when_value = match when_value.null_count() {
0 => Cow::Borrowed(when_value),
_ => Cow::Owned(prep_null_mask_filter(when_value)),
};

let then_value = self.when_then_expr[i]
.1
.evaluate_selection(batch, when_value)?;
.evaluate_selection(batch, &when_value)?;
let then_value = match then_value {
ColumnarValue::Scalar(value) if value.is_null() => {
new_null_array(&return_type, batch.num_rows())
}
_ => then_value.into_array(batch.num_rows()),
};

current_value = zip(when_value, then_value.as_ref(), current_value.as_ref())?;
current_value =
zip(&when_value, then_value.as_ref(), current_value.as_ref())?;

// Succeed tuples should be filtered out for short-circuit evaluation,
// null values for the current when expr should be kept
remainder = and(
&remainder,
&or_kleene(&not(when_value)?, &is_null(when_value)?)?,
)?;
remainder = and(&remainder, &not(&when_value)?)?;
}

if let Some(e) = &self.else_expr {
Expand Down