Skip to content

Commit 0a8f085

Browse files
Apply WHEN condition mask in divide_by_zero_protection
1 parent ba698ad commit 0a8f085

1 file changed

Lines changed: 23 additions & 71 deletions

File tree

  • datafusion/physical-expr/src/expressions

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

Lines changed: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use super::{CastExpr, Column, Literal};
2121
use crate::PhysicalExpr;
2222
use crate::expressions::{BinaryExpr, lit, try_cast};
2323
use arrow::array::*;
24+
use arrow::compute::kernels::numeric::div;
2425
use arrow::compute::kernels::zip::zip;
2526
use arrow::compute::{
2627
FilterBuilder, FilterPredicate, is_not_null, not, nullif, prep_null_mask_filter,
@@ -1277,7 +1278,10 @@ impl CaseExpr {
12771278
}
12781279

12791280
fn divide_by_zero_protection(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
1280-
let then_expr = &self.body.when_then_expr[0].1;
1281+
let (when_expr, then_expr) = &self.body.when_then_expr[0];
1282+
1283+
let when_value = when_expr.evaluate(batch)?;
1284+
12811285
let binary = then_expr
12821286
.as_any()
12831287
.downcast_ref::<BinaryExpr>()
@@ -1286,7 +1290,15 @@ impl CaseExpr {
12861290
let numerator = binary.left().evaluate(batch)?;
12871291
let divisor = binary.right().evaluate(batch)?;
12881292

1289-
safe_divide(&numerator, &divisor)
1293+
let num_rows = batch.num_rows();
1294+
let num_array = numerator.into_array(num_rows)?;
1295+
let div_array = divisor.into_array(num_rows)?;
1296+
let condition_array = when_value.into_array(num_rows)?;
1297+
let condition = as_boolean_array(&condition_array)?;
1298+
1299+
let result = safe_divide_with_mask(&num_array, &div_array, condition)?;
1300+
1301+
Ok(ColumnarValue::Array(result))
12901302
}
12911303
}
12921304

@@ -1508,76 +1520,16 @@ fn replace_with_null(
15081520
Ok(with_null)
15091521
}
15101522

1511-
fn safe_divide(
1512-
numerator: &ColumnarValue,
1513-
divisor: &ColumnarValue,
1514-
) -> Result<ColumnarValue> {
1515-
if let ColumnarValue::Scalar(div_scalar) = divisor
1516-
&& is_scalar_zero(div_scalar)
1517-
{
1518-
let data_type = numerator.data_type();
1519-
return match numerator {
1520-
ColumnarValue::Array(arr) => {
1521-
Ok(ColumnarValue::Array(new_null_array(&data_type, arr.len())))
1522-
}
1523-
ColumnarValue::Scalar(_) => Ok(ColumnarValue::Scalar(
1524-
ScalarValue::try_new_null(&data_type)?,
1525-
)),
1526-
};
1527-
}
1528-
1529-
let num_rows = match (numerator, divisor) {
1530-
(ColumnarValue::Array(arr), _) => arr.len(),
1531-
(_, ColumnarValue::Array(arr)) => arr.len(),
1532-
_ => 1,
1533-
};
1534-
1535-
let num_array = numerator.clone().into_array(num_rows)?;
1536-
let div_array = divisor.clone().into_array(num_rows)?;
1537-
1538-
let result = safe_divide_arrays(&num_array, &div_array)?;
1539-
1540-
if matches!(numerator, ColumnarValue::Scalar(_))
1541-
&& matches!(divisor, ColumnarValue::Scalar(_))
1542-
{
1543-
Ok(ColumnarValue::Scalar(ScalarValue::try_from_array(
1544-
&result, 0,
1545-
)?))
1546-
} else {
1547-
Ok(ColumnarValue::Array(result))
1548-
}
1549-
}
1550-
1551-
fn safe_divide_arrays(numerator: &ArrayRef, divisor: &ArrayRef) -> Result<ArrayRef> {
1552-
use arrow::compute::kernels::cmp::eq;
1553-
use arrow::compute::kernels::numeric::div;
1554-
1555-
let zero = ScalarValue::new_zero(divisor.data_type())?.to_scalar()?;
1556-
let zero_mask = eq(divisor, &zero)?;
1557-
1523+
fn safe_divide_with_mask(
1524+
numerator: &ArrayRef,
1525+
divisor: &ArrayRef,
1526+
condition: &BooleanArray,
1527+
) -> Result<ArrayRef> {
1528+
let not_condition = not(condition)?;
15581529
let ones = ScalarValue::new_one(divisor.data_type())?.to_scalar()?;
1559-
let safe_divisor = zip(&zero_mask, &ones, divisor)?;
1560-
1561-
let result = div(&numerator, &safe_divisor)?;
1562-
1563-
Ok(nullif(&result, &zero_mask)?)
1564-
}
1565-
1566-
fn is_scalar_zero(scalar: &ScalarValue) -> bool {
1567-
match scalar {
1568-
ScalarValue::Int8(Some(0))
1569-
| ScalarValue::Int16(Some(0))
1570-
| ScalarValue::Int32(Some(0))
1571-
| ScalarValue::Int64(Some(0))
1572-
| ScalarValue::UInt8(Some(0))
1573-
| ScalarValue::UInt16(Some(0))
1574-
| ScalarValue::UInt32(Some(0))
1575-
| ScalarValue::UInt64(Some(0)) => true,
1576-
ScalarValue::Float16(Some(v)) if v.to_f32() == 0.0 => true,
1577-
ScalarValue::Float32(Some(v)) if *v == 0.0 => true,
1578-
ScalarValue::Float64(Some(v)) if *v == 0.0 => true,
1579-
_ => false,
1580-
}
1530+
let safe_divisor = zip(&not_condition, &ones, &divisor)?;
1531+
let result = div(numerator, &safe_divisor)?;
1532+
Ok(nullif(&result, &not_condition)?)
15811533
}
15821534

15831535
/// Create a CASE expression

0 commit comments

Comments
 (0)