diff --git a/datafusion/common/src/float_semantics.rs b/datafusion/common/src/float_semantics.rs index 04ae9118009a2..01b226eeed7bf 100644 --- a/datafusion/common/src/float_semantics.rs +++ b/datafusion/common/src/float_semantics.rs @@ -153,6 +153,20 @@ pub fn normalize_floats(array: &ArrayRef) -> ArrayRef { } } +/// Like [`normalize_floats`], but takes ownership of the array. +/// +/// This avoids any copying (including arc ref counts) if there is nothing, and +/// will reuse the underlying allocation if possible +pub fn normalize_floats_owned(array: ArrayRef) -> ArrayRef { + match array.data_type() { + DataType::Float16 => normalize_primitive_owned::(array), + DataType::Float32 => normalize_primitive_owned::(array), + DataType::Float64 => normalize_primitive_owned::(array), + dt if has_float_leaf(dt) => normalize_floats(&array), + _ => array, + } +} + fn normalize_primitive(array: &ArrayRef) -> ArrayRef where T: ArrowPrimitiveType, @@ -165,6 +179,28 @@ where Arc::new(arr.unary::<_, T>(normalize_float)) } +/// Like [`normalize_primitive`] except it takes an owned array +/// and tries to reuse the allocation when possible; +fn normalize_primitive_owned(array: ArrayRef) -> ArrayRef +where + T: ArrowPrimitiveType, + T::Native: FloatSemantics, +{ + let arr = array.as_primitive::(); + if !arr.values().iter().any(|v| v.needs_normalization()) { + return array; + } + // Drop the `ArrayRef` so the values buffer is uniquely owned (when nothing + // else refers to it) and can be rewritten in place. + let arr = arr.clone(); + drop(array); + let normalized = match arr.unary_mut(normalize_float) { + Ok(normalized) => normalized, // updated in place + Err(arr) => arr.unary::<_, T>(normalize_float), // needed to copy + }; + Arc::new(normalized) +} + /// Whether `data_type` is a float or contains a float at any depth of its /// nested children. pub fn has_float_leaf(data_type: &DataType) -> bool { @@ -274,6 +310,37 @@ mod tests { assert!(Arc::ptr_eq(&normalize_floats(&array), &array)); } + #[test] + fn normalize_floats_owned_rewrites_in_place_when_unique() { + let array: ArrayRef = Arc::new(Float64Array::from(vec![-0.0, 1.0])); + let values_ptr = array.as_primitive::().values().as_ptr(); + let result = normalize_floats_owned(array); + let result = result.as_primitive::(); + assert_eq!(result.values().as_ptr(), values_ptr); + assert_eq!(result.value(0).to_bits(), 0.0_f64.to_bits()); + + // A shared buffer is copied and the original is left as it is + let array: ArrayRef = Arc::new(Float64Array::from(vec![-0.0, 1.0])); + let result = normalize_floats_owned(Arc::clone(&array)); + let result = result.as_primitive::(); + assert_ne!( + result.values().as_ptr(), + array.as_primitive::().values().as_ptr() + ); + assert_eq!(result.value(0).to_bits(), 0.0_f64.to_bits()); + assert_eq!( + array.as_primitive::().value(0).to_bits(), + (-0.0_f64).to_bits() + ); + + // Nothing to normalize: the input is returned as is + let array: ArrayRef = Arc::new(Float64Array::from(vec![0.0, 1.0])); + assert!(Arc::ptr_eq( + &normalize_floats_owned(Arc::clone(&array)), + &array + )); + } + #[test] fn normalize_floats_rewrites_nested_floats() { let list = ListArray::from_iter_primitive::(vec![ diff --git a/datafusion/expr-common/src/columnar_value.rs b/datafusion/expr-common/src/columnar_value.rs index 3a2c6a18de699..a3b5a36f639b6 100644 --- a/datafusion/expr-common/src/columnar_value.rs +++ b/datafusion/expr-common/src/columnar_value.rs @@ -13,6 +13,7 @@ use arrow::{ datatypes::{DataType, TimeUnit}, util::pretty::pretty_format_columns, }; +use datafusion_common::float_semantics::normalize_floats_owned; use datafusion_common::internal_datafusion_err; use datafusion_common::{ Result, ScalarValue, @@ -113,6 +114,19 @@ impl ColumnarValue { } } + /// Applies DataFusion's [float equality semantics] to this value (e.g. + /// `-0.0` becomes `+0.0`) + /// + /// [float equality semantics]: datafusion_common::float_semantics + pub fn normalize(self) -> Self { + match self { + ColumnarValue::Array(array) => { + ColumnarValue::Array(normalize_floats_owned(array)) + } + ColumnarValue::Scalar(scalar) => ColumnarValue::Scalar(scalar.normalize()), + } + } + /// Convert any [`Self::Scalar`] into an Arrow [`ArrayRef`] with the specified /// number of rows by repeating the same scalar multiple times, /// which is not as efficient as handling the scalar directly. diff --git a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs index af5ad6d43bdad..5cf4e972109c7 100644 --- a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs +++ b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs @@ -4990,17 +4990,19 @@ mod tests { if list_len > THRESHOLD_INLINE_INLIST || retains_short_lists { // Floating-point literals must bypass structural set rewrites. assert_eq!(simplified, expr); - } else { - let actual = create_physical_expr( - &simplified, - &schema, - &ExecutionProps::new(), - &PhysicalPlanningContext::default(), - )? - .evaluate(&batch)? - .into_array(batch.num_rows())?; - assert_eq!(actual.as_boolean(), &BooleanArray::from(expected)); } + + // Regardless of the form the list takes (rewritten or not) + // it must correctly report that `-0.0` matches `0.0`. + let actual = create_physical_expr( + &simplified, + &schema, + &ExecutionProps::new(), + &PhysicalPlanningContext::default(), + )? + .evaluate(&batch)? + .into_array(batch.num_rows())?; + assert_eq!(actual.as_boolean(), &BooleanArray::from(expected)); } } } diff --git a/datafusion/physical-expr/src/expressions/in_list.rs b/datafusion/physical-expr/src/expressions/in_list.rs index bb4293605242e..fc48705a16b63 100644 --- a/datafusion/physical-expr/src/expressions/in_list.rs +++ b/datafusion/physical-expr/src/expressions/in_list.rs @@ -324,6 +324,7 @@ impl PhysicalExpr for InListExpr { fn evaluate(&self, batch: &RecordBatch) -> Result { let num_rows = batch.num_rows(); let value = self.expr.evaluate(batch)?; + let value = value.normalize(); // so 0.0 == -0.0 let r = match &self.static_filter { Some(filter) => { match value { @@ -373,7 +374,8 @@ impl PhysicalExpr for InListExpr { // Helper: compare value against a single list expression let compare_one = |expr: &Arc| -> Result { - match expr.evaluate(batch)? { + let list_value = expr.evaluate(batch)?.normalize(); // 0.0 == -0.0 + match list_value { ColumnarValue::Array(array) => { if lhs_supports_arrow_eq && supports_arrow_eq(array.data_type()) @@ -1137,6 +1139,78 @@ mod tests { Ok(()) } + #[test] + fn in_list_float_signed_zero() -> Result<()> { + // `IN` must agree with `=`: `-0.0` and `+0.0` are equal, whichever side + // of the comparison they appear on and whichever filter is used. + let schema = Schema::new(vec![ + Field::new("a", DataType::Float64, true), + Field::new("b", DataType::Float64, true), + ]); + let a = Float64Array::from(vec![Some(0.0), Some(-0.0), Some(1.0), None]); + let b = Float64Array::from(vec![Some(-0.0), Some(0.0), Some(-0.0), Some(0.0)]); + let col_a = col("a", &schema)?; + let batch = RecordBatch::try_new( + Arc::new(schema.clone()), + vec![Arc::new(a), Arc::new(b)], + )?; + let expected = vec![Some(true), Some(true), Some(false), None]; + let expected_negated = vec![Some(false), Some(false), Some(true), None]; + + // Short literal lists use the branchless filter + let list = || vec![lit(-0.0f64), lit(2.0f64)]; + in_list!( + batch, + list(), + &false, + expected.clone(), + Arc::clone(&col_a), + &schema + ); + in_list!( + batch, + list(), + &true, + expected_negated, + Arc::clone(&col_a), + &schema + ); + + // Longer literal lists use the hash set filter + let list: Vec> = std::iter::once(lit(0.0f64)) + .chain((2..22).map(|i| lit(i as f64))) + .collect(); + in_list!( + batch, + list, + &false, + expected.clone(), + Arc::clone(&col_a), + &schema + ); + + // A column in the list is compared at runtime + let expr = make_in_list_with_columns( + Arc::clone(&col_a), + vec![col("b", &schema)?], + false, + ); + let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?; + assert_eq!( + as_boolean_array(&result), + &BooleanArray::from(expected.clone()) + ); + + // Lists built directly from an array are normalized as well + let haystack: ArrayRef = Arc::new(Float64Array::from(vec![-0.0])); + let expr = + InListExpr::try_new_from_array(Arc::clone(&col_a), haystack, false, &schema)?; + let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?; + assert_eq!(as_boolean_array(&result), &BooleanArray::from(expected)); + + Ok(()) + } + #[test] fn in_list_bool() -> Result<()> { let schema = Schema::new(vec![Field::new("a", DataType::Boolean, true)]); diff --git a/datafusion/physical-expr/src/expressions/in_list/branchless_filter.rs b/datafusion/physical-expr/src/expressions/in_list/branchless_filter.rs index 98c170b9212cd..3488857d4a66d 100644 --- a/datafusion/physical-expr/src/expressions/in_list/branchless_filter.rs +++ b/datafusion/physical-expr/src/expressions/in_list/branchless_filter.rs @@ -29,6 +29,12 @@ //! different NaN values. [`BranchlessFilterType`] defines these safe, //! same-sized mappings and checks their sizes at compile time. //! +//! It is important therefore for [`InListExpr`] to normalize `-0.0` to `+0.0` in +//! both the list and the input before they reach this filter, so signed zeros +//! compare equal, as they do for `=` (see [`datafusion_common::float_semantics`]). +//! +//! [`InListExpr`]: crate::expressions::InListExpr +//! //! The fast path is limited to short lists: //! //! - 16 values for 1-byte types diff --git a/datafusion/physical-expr/src/expressions/in_list/primitive_filter.rs b/datafusion/physical-expr/src/expressions/in_list/primitive_filter.rs index 7ef6d25f87b61..cca8c35af14e2 100644 --- a/datafusion/physical-expr/src/expressions/in_list/primitive_filter.rs +++ b/datafusion/physical-expr/src/expressions/in_list/primitive_filter.rs @@ -321,6 +321,11 @@ where /// Wrapper for f32 that implements Hash and Eq using bit comparison. /// This treats NaN values as equal to each other when they have the same bit pattern. +/// +/// Note: [`InListExpr`] must normalize the values first, see +/// [`datafusion_common::float_semantics`]. +/// +/// [`InListExpr`]: crate::expressions::InListExpr #[derive(Clone, Copy)] struct OrderedFloat32(f32); @@ -346,6 +351,11 @@ impl From for OrderedFloat32 { /// Wrapper for f64 that implements Hash and Eq using bit comparison. /// This treats NaN values as equal to each other when they have the same bit pattern. +/// +/// Note: [`InListExpr`] must normalize the values first, see +/// [`datafusion_common::float_semantics`]. +/// +/// [`InListExpr`]: crate::expressions::InListExpr #[derive(Clone, Copy)] struct OrderedFloat64(f64); diff --git a/datafusion/physical-expr/src/expressions/in_list/strategy.rs b/datafusion/physical-expr/src/expressions/in_list/strategy.rs index bc2ab9d272935..64e170fccaee2 100644 --- a/datafusion/physical-expr/src/expressions/in_list/strategy.rs +++ b/datafusion/physical-expr/src/expressions/in_list/strategy.rs @@ -8,6 +8,7 @@ use arrow::array::{ArrayRef, AsArray}; use arrow::compute::take; use arrow::datatypes::DataType; use datafusion_common::Result; +use datafusion_common::float_semantics::normalize_floats_owned; use super::array_static_filter::ArrayStaticFilter; use super::byte_view_filter::instantiate_byte_view_filter; @@ -21,6 +22,7 @@ pub(super) fn instantiate_static_filter( needle_data_type: &DataType, ) -> Result { let in_array = flatten_dictionary_haystack(in_array)?; + let in_array = normalize_floats_owned(in_array); let filter = if view_types_match(needle_data_type, in_array.data_type()) && let Some(filter) = instantiate_byte_view_filter(&in_array)? diff --git a/datafusion/sqllogictest/test_files/in_list.slt b/datafusion/sqllogictest/test_files/in_list.slt index 8b9e9f58b255e..21122c912e5fc 100644 --- a/datafusion/sqllogictest/test_files/in_list.slt +++ b/datafusion/sqllogictest/test_files/in_list.slt @@ -1147,3 +1147,49 @@ true statement ok DROP TABLE in_list_nulls; + +# IN must agree with = on signed zeros: -0.0 and +0.0 are equal +# https://github.com/apache/datafusion/issues/27121 +statement ok +CREATE TABLE signed_zeros AS +SELECT id, x, y, arrow_cast(x, 'Float16') AS h +FROM (VALUES + (1, -0.0, CAST(0.0 AS FLOAT)), + (2, 0.0, CAST(-0.0 AS FLOAT)), + (3, 5.0, CAST(5.0 AS FLOAT)) +) AS t(id, x, y); + +query IBBBBBBBBBBBBBBB +SELECT + id, + x = 0.0, + -- constant folded with a scalar input + -0.0 IN (0.0, 1.0), + -- dictionary encoded input + arrow_cast(x, 'Dictionary(Int32, Float64)') IN (0.0, 5.0), + -- short literal list: branchless filter + x IN (0.0, 5.0), + x NOT IN (0.0, 5.0), + x IN (-0.0, 7.0), + -- non-column expression and four literals keep the membership filter + -x IN (0.0, -5.0), + x IN (0.0, 1.0, 2.0, 3.0), + x NOT IN (0.0, 1.0, 2.0, 3.0), + -- seventeen literals: hash set filter + x IN (0.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0), + -- column in the list: compared at runtime + x IN (y, 7.0, 8.0, 9.0), + x NOT IN (y, 7.0, 8.0, 9.0), + y IN (CAST(0 AS FLOAT), CAST(9 AS FLOAT)), + y NOT IN (CAST(0 AS FLOAT), CAST(9 AS FLOAT)), + -- nine Float16 literals: bitmap filter + h IN (arrow_cast(0, 'Float16'), arrow_cast(1, 'Float16'), arrow_cast(2, 'Float16'), arrow_cast(3, 'Float16'), arrow_cast(4, 'Float16'), arrow_cast(6, 'Float16'), arrow_cast(7, 'Float16'), arrow_cast(8, 'Float16'), arrow_cast(9, 'Float16')) +FROM signed_zeros +ORDER BY id; +---- +1 true true true true false true true true false true true false true false true +2 true true true true false true true true false true true false true false true +3 false true true true false false true false true false true false false true false + +statement ok +DROP TABLE signed_zeros;