Skip to content

Apache DataFusion SQL Query Engine

Short-circuit evaluation for `CaseWhen` (#2068)

Yijie Shenff110d6c682e
diff --git a/datafusion-physical-expr/src/expressions/case.rs b/datafusion-physical-expr/src/expressions/case.rs
index 3bcb78a977..5e6ea9191d 100644
--- a/datafusion-physical-expr/src/expressions/case.rs
+++ b/datafusion-physical-expr/src/expressions/case.rs
@@ -20,7 +20,7 @@ use std::{any::Any, sync::Arc};
use crate::expressions::try_cast;
use crate::PhysicalExpr;
use arrow::array::{self, *};
-use arrow::compute::{eq, eq_utf8};
+use arrow::compute::{and, eq, eq_utf8, is_null, not, or, or_kleene};
use arrow::datatypes::{DataType, Schema};
use arrow::record_batch::RecordBatch;
use datafusion_common::{DataFusionError, Result};
@@ -323,39 +323,44 @@ impl CaseExpr {
let base_value = expr.evaluate(batch)?;
let base_type = expr.data_type(&batch.schema())?;
let base_value = base_value.into_array(batch.num_rows());
-
- // start with the else condition, or nulls
- let mut current_value: Option<ArrayRef> = if let Some(e) = &self.else_expr {
- // keep `else_expr`'s data type and return type consistent
- let expr = try_cast(e.clone(), &*batch.schema(), return_type.clone())
- .unwrap_or_else(|_| e.clone());
- Some(expr.evaluate(batch)?.into_array(batch.num_rows()))
- } else {
- Some(new_null_array(&return_type, batch.num_rows()))
- };
-
- // walk backwards through the when/then expressions
- for i in (0..self.when_then_expr.len()).rev() {
- let i = i as usize;
-
- let when_value = self.when_then_expr[i].0.evaluate(batch)?;
+ let base_nulls = is_null(base_value.as_ref())?;
+
+ // start with nulls as default output
+ let mut current_value = new_null_array(&return_type, batch.num_rows());
+ // We only consider non-null values while comparing with whens
+ let mut remainder = not(&base_nulls)?;
+ for i in 0..self.when_then_expr.len() {
+ let when_value = self.when_then_expr[i]
+ .0
+ .evaluate_selection(batch, &remainder)?;
let when_value = when_value.into_array(batch.num_rows());
+ // build boolean array representing which rows match the "when" value
+ let when_match = array_equals(&base_type, when_value, base_value.clone())?;
- let then_value = self.when_then_expr[i].1.evaluate(batch)?;
+ let then_value = self.when_then_expr[i]
+ .1
+ .evaluate_selection(batch, &when_match)?;
let then_value = then_value.into_array(batch.num_rows());
- // build boolean array representing which rows match the "when" value
- let when_match = array_equals(&base_type, when_value, base_value.clone())?;
+ current_value =
+ if_then_else(&when_match, then_value, current_value, &return_type)?;
+
+ remainder = and(&remainder, &or_kleene(&not(&when_match)?, &base_nulls)?)?;
+ }
- current_value = Some(if_then_else(
- &when_match,
- then_value,
- current_value.unwrap(),
- &return_type,
- )?);
+ if let Some(e) = &self.else_expr {
+ // keep `else_expr`'s data type and return type consistent
+ let expr = try_cast(e.clone(), &*batch.schema(), return_type.clone())
+ .unwrap_or_else(|_| e.clone());
+ // null and unmatched tuples should be assigned else value
+ remainder = or(&base_nulls, &remainder)?;
+ let else_ = expr
+ .evaluate_selection(batch, &remainder)?
+ .into_array(batch.num_rows());
+ current_value = if_then_else(&remainder, else_, current_value, &return_type)?;
}
- Ok(ColumnarValue::Array(current_value.unwrap()))
+ Ok(ColumnarValue::Array(current_value))
}
/// This function evaluates the form of CASE where each WHEN expression is a boolean
@@ -368,20 +373,13 @@ impl CaseExpr {
fn case_when_no_expr(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
let return_type = self.when_then_expr[0].1.data_type(&batch.schema())?;
- // start with the else condition, or nulls
- let mut current_value: Option<ArrayRef> = if let Some(e) = &self.else_expr {
- let expr = try_cast(e.clone(), &*batch.schema(), return_type.clone())
- .unwrap_or_else(|_| e.clone());
- Some(expr.evaluate(batch)?.into_array(batch.num_rows()))
- } else {
- Some(new_null_array(&return_type, batch.num_rows()))
- };
-
- // walk backwards through the when/then expressions
- for i in (0..self.when_then_expr.len()).rev() {
- let i = i as usize;
-
- let when_value = self.when_then_expr[i].0.evaluate(batch)?;
+ // start with nulls as default output
+ let mut current_value = new_null_array(&return_type, batch.num_rows());
+ let mut remainder = BooleanArray::from(vec![true; batch.num_rows()]);
+ for i in 0..self.when_then_expr.len() {
+ let when_value = self.when_then_expr[i]
+ .0
+ .evaluate_selection(batch, &remainder)?;
let when_value = when_value.into_array(batch.num_rows());
let when_value = when_value
.as_ref()
@@ -389,23 +387,38 @@ impl CaseExpr {
.downcast_ref::<BooleanArray>()
.expect("WHEN expression did not return a BooleanArray");
- let then_value = self.when_then_expr[i].1.evaluate(batch)?;
+ let then_value = self.when_then_expr[i]
+ .1
+ .evaluate_selection(batch, when_value)?;
let then_value = then_value.into_array(batch.num_rows());
- current_value = Some(if_then_else(
- when_value,
- then_value,
- current_value.unwrap(),
- &return_type,
- )?);
+ current_value =
+ if_then_else(when_value, then_value, current_value, &return_type)?;
+
+ // 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)?)?,
+ )?;
+ }
+
+ if let Some(e) = &self.else_expr {
+ // keep `else_expr`'s data type and return type consistent
+ let expr = try_cast(e.clone(), &*batch.schema(), return_type.clone())
+ .unwrap_or_else(|_| e.clone());
+ let else_ = expr
+ .evaluate_selection(batch, &remainder)?
+ .into_array(batch.num_rows());
+ current_value = if_then_else(&remainder, else_, current_value, &return_type)?;
}
- Ok(ColumnarValue::Array(current_value.unwrap()))
+ Ok(ColumnarValue::Array(current_value))
}
}
impl PhysicalExpr for CaseExpr {
- /// Return a reference to Any that can be used for downcasting
+ /// Return a reference to Any that can be used for down-casting
fn as_any(&self) -> &dyn Any {
self
}
@@ -455,10 +468,11 @@ pub fn case(
#[cfg(test)]
mod tests {
use super::*;
- use crate::expressions::binary;
use crate::expressions::col;
use crate::expressions::lit;
+ use crate::expressions::{binary, cast};
use arrow::array::StringArray;
+ use arrow::datatypes::DataType::Float64;
use arrow::datatypes::*;
use datafusion_common::ScalarValue;
use datafusion_expr::Operator;
@@ -523,6 +537,39 @@ mod tests {
Ok(())
}
+ #[test]
+ fn case_with_expr_divide_by_zero() -> Result<()> {
+ let batch = case_test_batch1()?;
+ let schema = batch.schema();
+
+ // CASE a when 0 THEN float64(null) ELSE 25.0 / cast(a, float64) END
+ let when1 = lit(ScalarValue::Int32(Some(0)));
+ let then1 = lit(ScalarValue::Float64(None));
+ let else_value = binary(
+ lit(ScalarValue::Float64(Some(25.0))),
+ Operator::Divide,
+ cast(col("a", &schema)?, &batch.schema(), Float64)?,
+ &batch.schema(),
+ )?;
+
+ let expr = case(
+ Some(col("a", &schema)?),
+ &[(when1, then1)],
+ Some(else_value),
+ )?;
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows());
+ let result = result
+ .as_any()
+ .downcast_ref::<Float64Array>()
+ .expect("failed to downcast to Int32Array");
+
+ let expected = &Float64Array::from(vec![Some(25.0), None, None, Some(5.0)]);
+
+ assert_eq!(expected, result);
+
+ Ok(())
+ }
+
#[test]
fn case_without_expr() -> Result<()> {
let batch = case_test_batch()?;
@@ -558,6 +605,47 @@ mod tests {
Ok(())
}
+ #[test]
+ fn case_without_expr_divide_by_zero() -> Result<()> {
+ let batch = case_test_batch1()?;
+ let schema = batch.schema();
+
+ // CASE WHEN a > 0 THEN 25.0 / cast(a, float64) ELSE float64(null) END
+ let when1 = binary(
+ col("a", &schema)?,
+ Operator::Gt,
+ lit(ScalarValue::Int32(Some(0))),
+ &batch.schema(),
+ )?;
+ let then1 = binary(
+ lit(ScalarValue::Float64(Some(25.0))),
+ Operator::Divide,
+ cast(col("a", &schema)?, &batch.schema(), Float64)?,
+ &batch.schema(),
+ )?;
+ let x = lit(ScalarValue::Float64(None));
+
+ let expr = case(None, &[(when1, then1)], Some(x))?;
+ let result = expr.evaluate(&batch)?.into_array(batch.num_rows());
+ let result = result
+ .as_any()
+ .downcast_ref::<Float64Array>()
+ .expect("failed to downcast to Int32Array");
+
+ let expected = &Float64Array::from(vec![Some(25.0), None, None, Some(5.0)]);
+
+ assert_eq!(expected, result);
+
+ Ok(())
+ }
+
+ fn case_test_batch1() -> Result<RecordBatch> {
+ let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
+ let a = Int32Array::from(vec![Some(1), Some(0), None, Some(5)]);
+ let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)])?;
+ Ok(batch)
+ }
+
#[test]
fn case_without_expr_else() -> Result<()> {
let batch = case_test_batch()?;
diff --git a/datafusion-physical-expr/src/physical_expr.rs b/datafusion-physical-expr/src/physical_expr.rs
index 25885b1ab5..79c2269f8a 100644
--- a/datafusion-physical-expr/src/physical_expr.rs
+++ b/datafusion-physical-expr/src/physical_expr.rs
@@ -16,6 +16,7 @@
// under the License.
use arrow::datatypes::{DataType, Schema};
+use arrow::error::Result as ArrowResult;
use arrow::record_batch::RecordBatch;
@@ -24,7 +25,12 @@ use datafusion_common::Result;
use datafusion_expr::ColumnarValue;
use std::fmt::{Debug, Display};
+use arrow::array::{
+ make_array, Array, ArrayRef, BooleanArray, MutableArrayData, UInt64Array,
+};
+use arrow::compute::{take, SlicesIterator};
use std::any::Any;
+use std::sync::Arc;
/// Expression that can be evaluated against a RecordBatch
/// A Physical expression knows its type, nullability and how to evaluate itself.
@@ -38,4 +44,117 @@ pub trait PhysicalExpr: Send + Sync + Display + Debug {
fn nullable(&self, input_schema: &Schema) -> Result<bool>;
/// Evaluate an expression against a RecordBatch
fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue>;
+ /// Evaluate an expression against a RecordBatch after first applying a
+ /// validity array
+ fn evaluate_selection(
+ &self,
+ batch: &RecordBatch,
+ selection: &BooleanArray,
+ ) -> Result<ColumnarValue> {
+ if selection.iter().all(|b| b == Some(true)) {
+ return self.evaluate(batch);
+ }
+ let mut indices = vec![];
+ for (i, b) in selection.iter().enumerate() {
+ if let Some(true) = b {
+ indices.push(i as u64);
+ }
+ }
+ let indices = UInt64Array::from_iter_values(indices);
+ let tmp_columns = batch
+ .columns()
+ .iter()
+ .map(|c| take(c.as_ref(), &indices, None))
+ .collect::<ArrowResult<Vec<Arc<dyn Array>>>>()?;
+
+ let tmp_batch = RecordBatch::try_new(batch.schema(), tmp_columns)?;
+ let tmp_result = self.evaluate(&tmp_batch)?;
+ if let ColumnarValue::Array(a) = tmp_result {
+ let result = scatter(selection, a.as_ref())?;
+ Ok(ColumnarValue::Array(result))
+ } else {
+ Ok(tmp_result)
+ }
+ }
+}
+
+/// Scatter `truthy` array by boolean mask. When the mask evaluates `true`, next values of `truthy`
+/// are taken, when the mask evaluates `false` values null values are filled.
+///
+/// # Arguments
+/// * `mask` - Boolean values used to determine where to put the `truthy` values
+/// * `truthy` - All values of this array are to scatter according to `mask` into final result.
+fn scatter(mask: &BooleanArray, truthy: &dyn Array) -> Result<ArrayRef> {
+ let truthy = truthy.data();
+
+ let mut mutable = MutableArrayData::new(vec![&*truthy], true, mask.len());
+
+ // the SlicesIterator slices only the true values. So the gaps left by this iterator we need to
+ // fill with falsy values
+
+ // keep track of how much is filled
+ let mut filled = 0;
+ // keep track of current position we have in truthy array
+ let mut true_pos = 0;
+
+ SlicesIterator::new(mask).for_each(|(start, end)| {
+ // the gap needs to be filled with nulls
+ if start > filled {
+ mutable.extend_nulls(start - filled);
+ }
+ // fill with truthy values
+ let len = end - start;
+ mutable.extend(0, true_pos, true_pos + len);
+ true_pos += len;
+ filled = end;
+ });
+ // the remaining part is falsy
+ if filled < truthy.len() {
+ mutable.extend_nulls(truthy.len() - filled);
+ }
+
+ let data = mutable.freeze();
+ Ok(make_array(data))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use arrow::array::Int32Array;
+ use datafusion_common::Result;
+
+ #[test]
+ fn scatter_int() -> Result<()> {
+ let truthy = Arc::new(Int32Array::from(vec![1, 10, 11, 100]));
+ let mask = BooleanArray::from(vec![true, true, false, false, true]);
+
+ // the output array is expected to be the same length as the mask array
+ let expected =
+ Int32Array::from_iter(vec![Some(1), Some(10), None, None, Some(11)]);
+ let result = scatter(&mask, truthy.as_ref())?;
+ let result = result.as_any().downcast_ref::<Int32Array>().unwrap();
+
+ assert_eq!(&expected, result);
+ Ok(())
+ }
+
+ #[test]
+ fn scatter_boolean() -> Result<()> {
+ let truthy = Arc::new(BooleanArray::from(vec![false, false, false, true]));
+ let mask = BooleanArray::from(vec![true, true, false, false, true]);
+
+ // the output array is expected to be the same length as the mask array
+ let expected = BooleanArray::from_iter(vec![
+ Some(false),
+ Some(false),
+ None,
+ None,
+ Some(false),
+ ]);
+ let result = scatter(&mask, truthy.as_ref())?;
+ let result = result.as_any().downcast_ref::<BooleanArray>().unwrap();
+
+ assert_eq!(&expected, result);
+ Ok(())
+ }
}