Apache DataFusion SQL Query Engine
refactor: Improve `float_semantics::normalize`, consolidate ownership (#27202)
Andrew Lamb01f4796b21b6
diff --git a/datafusion/common/src/float_semantics.rs b/datafusion/common/src/float_semantics.rsindex fb120ed523..7ebbabe9f6 100644--- a/datafusion/common/src/float_semantics.rs+++ b/datafusion/common/src/float_semantics.rs@@ -7,7 +7,7 @@//! Arrow's comparison kernels work on the bit pattern of a float,//! i.e. IEEE 754 totalOrder. DataFusion deviates from that in one//! respect: `+0.0` and `-0.0` are equal, hash the same and fall into the same-//! group. See [`normalize_float`], [`normalize_floats`] and+//! group. See [`normalize_float`], [`normalize`] and//! [`ScalarValue::normalize`]. Ordering still follows totalOrder, so//! [`compare_floats`] sorts `-0.0` before `+0.0`.//!@@ -15,7 +15,7 @@//! helpers rather than inspecting float bits itself://!//! - Equality and grouping: normalize first ([`normalize_float`],-//! [`normalize_floats`], [`ScalarValue::normalize`]), then compare bits or+//! [`normalize`], [`ScalarValue::normalize`]), then compare bits or//! call Arrow's `eq`, `RowConverter` or `make_comparator`. Hash//! [`hash_input`].//! - Ordering: use [`compare_floats`], [`float_lt`] or [`float_gt`]. Sorting@@ -119,11 +119,14 @@ pub fn hash_input<T: FloatSemantics>(v: T) -> T {/// Callers use this before handing float data to Arrow kernels that compare/// bit patterns, so that `+0.0 = -0.0` holds.///-/// The common case - nothing to normalize - is allocation-free: a single-/// read-only scan of the values buffer (auto-vectorizable to an OR-reduction)-/// decides whether to fall through to the rewriting path. Only arrays that-/// contain values to normalize pay for a new buffer.-pub fn normalize_floats(array: &ArrayRef) -> ArrayRef {+/// Optimized for the common case of nothing to normalize, by searching for such+/// values in a vectorized way. If noting needs normalization, the original+/// array is returned unchanged.+///+/// If the values do require normalization, attempts to rewrite in place when+/// possible (when the underlying buffers are not shared). Thus, passing+/// `Arc::clone(&array)` therefore always copies if normalization is required.+pub fn normalize(array: ArrayRef) -> ArrayRef {match array.data_type() {DataType::Float16 => normalize_primitive::<Float16Type>(array),DataType::Float32 => normalize_primitive::<Float32Type>(array),@@ -133,14 +136,14 @@ pub fn normalize_floats(array: &ArrayRef) -> ArrayRef {let children = data.child_data().iter()- .map(|child| normalize_floats(&make_array(child.clone())).to_data())+ .map(|child| normalize(make_array(child.clone())).to_data()).collect::<Vec<_>>();if children.iter().zip(data.child_data()).all(|(new, old)| new.ptr_eq(old)){- return Arc::clone(array);+ return array;}make_array(data.into_builder()@@ -149,20 +152,39 @@ pub fn normalize_floats(array: &ArrayRef) -> ArrayRef {.expect("rewriting float leaves preserves the array layout"),)}- _ => Arc::clone(array),+ _ => array,}}-fn normalize_primitive<T>(array: &ArrayRef) -> ArrayRef+/// Like [`normalize`], but borrows the array and so always copies the values+/// buffer when there is something to normalize.+#[deprecated(+ since = "57.0.0",+ note = "use `datafusion_common::float_semantics::normalize` instead"+)]+pub fn normalize_floats(array: &ArrayRef) -> ArrayRef {+ normalize(Arc::clone(array))+}++/// Takes an owned array and tries to reuse its allocation when possible+fn normalize_primitive<T>(array: ArrayRef) -> ArrayRefwhereT: ArrowPrimitiveType,T::Native: FloatSemantics,{let arr = array.as_primitive::<T>();if !arr.values().iter().any(|v| v.needs_normalization()) {- return Arc::clone(array);+ return array;}- Arc::new(arr.unary::<_, T>(normalize_float))+ // 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@@ -246,23 +268,20 @@ mod tests {#[test]fn normalize_floats_rewrites_negative_zero() {- let array: ArrayRef =- Arc::new(Float64Array::from(vec![Some(-0.0), None, Some(1.0)]));- let result = normalize_floats(&array);- let result = result.as_primitive::<Float64Type>();- assert_eq!(result.value(0).to_bits(), 0.0_f64.to_bits());- assert!(result.is_null(1));- assert_eq!(result.value(2), 1.0);+ let floats = &[Some(5.0), Some(-0.0), None, Some(2.0)];+ let (array, _) = float64_array_fixture(floats);+ let result = normalize(Arc::clone(&array));+ assert_float64_values(&result, &[Some(5.0), Some(0.0), None, Some(2.0)]);let array: ArrayRef = Arc::new(Float32Array::from(vec![-0.0_f32]));- let result = normalize_floats(&array);+ let result = normalize(Arc::clone(&array));assert_eq!(result.as_primitive::<Float32Type>().value(0).to_bits(),0.0_f32.to_bits());let array: ArrayRef = Arc::new(Float16Array::from(vec![f16::NEG_ZERO]));- let result = normalize_floats(&array);+ let result = normalize(Arc::clone(&array));assert_eq!(result.as_primitive::<Float16Type>().value(0).to_bits(),f16::ZERO.to_bits()@@ -272,10 +291,38 @@ mod tests {#[test]fn normalize_floats_reuses_normalized_input() {let array: ArrayRef = Arc::new(Float64Array::from(vec![0.0, 1.0, f64::NAN]));- assert!(Arc::ptr_eq(&normalize_floats(&array), &array));+ assert!(Arc::ptr_eq(&normalize(Arc::clone(&array)), &array));let array: ArrayRef = Arc::new(Int32Array::from(vec![1, 2]));- assert!(Arc::ptr_eq(&normalize_floats(&array), &array));+ assert!(Arc::ptr_eq(&normalize(Arc::clone(&array)), &array));+ }++ #[test]+ fn normalize_floats_rewrites_in_place_when_unique() {+ let floats = &[Some(5.0), Some(-0.0), None, Some(2.0)];+ let expected = &[Some(5.0), Some(0.0), None, Some(2.0)];+ let (array, values_ptr) = float64_array_fixture(floats);+ let (result, result_values_ptr) = normalize_fixture(array);+ assert_eq!(result_values_ptr, values_ptr);+ assert_float64_values(&result, expected);++ // A shared buffer is copied and the original is left as it is+ let (array, values_ptr) = float64_array_fixture(floats);+ let (result, result_values_ptr) = normalize_fixture(Arc::clone(&array));+ assert_ne!(result_values_ptr, values_ptr);+ assert_float64_values(&result, expected);+ assert_float64_values(&array, floats);+ }++ #[test]+ fn normalize_floats_copies_shared_slice() {+ let floats = &[Some(5.0), Some(-0.0), None, Some(2.0)];+ let (parent, values_ptr) = float64_array_fixture(floats);+ let slice = parent.slice(1, 3);+ let (result, result_values_ptr) = normalize_fixture(slice);+ assert_ne!(result_values_ptr, values_ptr.wrapping_add(1));+ assert_float64_values(&result, &[Some(0.0), None, Some(2.0)]);+ assert_float64_values(&parent, floats);}#[test]@@ -285,7 +332,7 @@ mod tests {None,]);let array: ArrayRef = Arc::new(list);- let result = normalize_floats(&array);+ let result = normalize(Arc::clone(&array));assert!(!Arc::ptr_eq(&result, &array));let values = result.as_list::<i32>().value(0);assert_eq!(@@ -297,11 +344,11 @@ mod tests {let list =ListArray::from_iter_primitive::<Int32Type, _, _>(vec![Some(vec![Some(1)])]);let array: ArrayRef = Arc::new(list);- assert!(Arc::ptr_eq(&normalize_floats(&array), &array));+ assert!(Arc::ptr_eq(&normalize(Arc::clone(&array)), &array));}#[test]- fn normalize_floats_in_dictionary_arrays_and_scalars() -> Result<()> {+ fn normalize_in_dictionary_arrays_and_scalars() -> Result<()> {let nan = f64::from_bits(0x7ff8_0000_0000_0001);let keys = Int8Array::from(vec![Some(0), Some(1), None, Some(2)]);let array: ArrayRef = Arc::new(DictionaryArray::try_new(@@ -309,7 +356,7 @@ mod tests {Arc::new(Float64Array::from(vec![-0.0, nan, 1.0])),)?);- let normalized = normalize_floats(&array);+ let normalized = normalize(Arc::clone(&array));let dictionary = normalized.as_dictionary::<Int8Type>();assert_eq!(dictionary.keys(), &keys);let values = dictionary.values().as_primitive::<Float64Type>();@@ -317,7 +364,10 @@ mod tests {assert_eq!(values.value(1).to_bits(), nan.to_bits());assert_eq!(values.value(2), 1.0);- assert!(Arc::ptr_eq(&normalize_floats(&normalized), &normalized));+ assert!(Arc::ptr_eq(+ &normalize(Arc::clone(&normalized)),+ &normalized+ ));assert_eq!(ScalarValue::try_from_array(&array, 0)?.normalize(),@@ -395,4 +445,37 @@ mod tests {assert_eq!(child.value(1).to_bits(), nan.to_bits());assert!(child.is_null(2));}++ /// Creates a float64 array and returns it with a pointer to its values+ fn float64_array_fixture(input: &[Option<f64>]) -> (ArrayRef, *const f64) {+ let array: ArrayRef = Arc::new(Float64Array::from(input.to_vec()));+ let values_ptr = array.as_primitive::<Float64Type>().values().as_ptr();+ (array, values_ptr)+ }++ /// Calls `normalize` and returns the normalized array and a pointer to its values+ fn normalize_fixture(array: ArrayRef) -> (ArrayRef, *const f64) {+ let result = normalize(array);+ let values_ptr = result.as_primitive::<Float64Type>().values().as_ptr();+ (result, values_ptr)+ }++ /// Asserts that the values in a float64 array match the expected values+ fn assert_float64_values(array: &ArrayRef, expected: &[Option<f64>]) {+ let array = array.as_primitive::<Float64Type>();+ // Note don't use the eq on FloatArray to ensure we are comparing using+ // bitwise equality (not normalized equality).+ assert_eq!(array.len(), expected.len());+ assert_eq!(+ array.null_count(),+ expected.iter().filter(|value| value.is_none()).count()+ );+ for (index, (actual, expected)) in array.iter().zip(expected).enumerate() {+ assert_eq!(+ actual.map(f64::to_bits),+ expected.map(f64::to_bits),+ "value at index {index}"+ );+ }+ }}diff --git a/datafusion/common/src/scalar/mod.rs b/datafusion/common/src/scalar/mod.rsindex fa7d4e3278..22ee94cd95 100644--- a/datafusion/common/src/scalar/mod.rs+++ b/datafusion/common/src/scalar/mod.rs@@ -39,6 +39,7 @@ use crate::cast::{as_uint16_array, as_uint32_array, as_uint64_array, as_union_array,};use crate::error::{_exec_err, _internal_err, _not_impl_err, DataFusionError, Result};+use crate::float_semantics::{self, normalize_float};use crate::format::DEFAULT_CAST_OPTIONS;use crate::hash_utils::create_hashes;use crate::scalar::consts::{@@ -1267,6 +1268,17 @@ macro_rules! eq_array_float {}};}+/// Applies [`float_semantics::normalize`] to a typed array, keeping the+/// original `Arc` when nothing needed to change+fn normalize_array<A: Array + From<ArrayData> + 'static>(array: Arc<A>) -> Arc<A> {+ let normalized = float_semantics::normalize(Arc::clone(&array) as ArrayRef);+ if std::ptr::addr_eq(Arc::as_ptr(&array), Arc::as_ptr(&normalized)) {+ array+ } else {+ Arc::new(downcast_array(normalized.as_ref()))+ }+}+impl ScalarValue {/// Create a [`Result<ScalarValue>`] with the provided value and datatype///@@ -2762,43 +2774,90 @@ impl ScalarValue {)}- /// Applies DataFusion's floating-point equality semantics to this value,- /// including floats inside nested and encoded values. Negative zero becomes- /// positive zero; nulls, NaN payloads and other values are preserved.+ /// Applies DataFusion's [floating-point equality semantics] to this value///- /// See [`crate::float_semantics`] for the shared normalization rules.- pub fn normalize(mut self) -> Self {- use crate::float_semantics::{normalize_float, normalize_floats};-- fn normalize_array<A: Array + From<ArrayData> + 'static>(array: &mut Arc<A>) {- let original: ArrayRef = Arc::clone(array) as ArrayRef;- let normalized = normalize_floats(&original);- if !Arc::ptr_eq(&original, &normalized) {- *array = Arc::new(downcast_array(normalized.as_ref()));+ /// [floating-point equality semantics]: crate::float_semantics+ pub fn normalize(self) -> Self {+ match self {+ ScalarValue::Float16(Some(v)) => {+ ScalarValue::Float16(Some(normalize_float(v)))}- }-- fn normalize(scalar: &mut ScalarValue) {- match scalar {- ScalarValue::Float16(Some(value)) => *value = normalize_float(*value),- ScalarValue::Float32(Some(value)) => *value = normalize_float(*value),- ScalarValue::Float64(Some(value)) => *value = normalize_float(*value),- ScalarValue::FixedSizeList(array) => normalize_array(array),- ScalarValue::List(array) => normalize_array(array),- ScalarValue::LargeList(array) => normalize_array(array),- ScalarValue::ListView(array) => normalize_array(array),- ScalarValue::LargeListView(array) => normalize_array(array),- ScalarValue::Struct(array) => normalize_array(array),- ScalarValue::Map(array) => normalize_array(array),- ScalarValue::Union(Some((_, value)), _, _)- | ScalarValue::Dictionary(_, value)- | ScalarValue::RunEndEncoded(_, _, value) => normalize(value),- _ => {}+ ScalarValue::Float32(Some(v)) => {+ ScalarValue::Float32(Some(normalize_float(v)))+ }+ ScalarValue::Float64(Some(v)) => {+ ScalarValue::Float64(Some(normalize_float(v)))+ }+ ScalarValue::FixedSizeList(array) => {+ ScalarValue::FixedSizeList(normalize_array(array))+ }+ ScalarValue::List(array) => ScalarValue::List(normalize_array(array)),+ ScalarValue::LargeList(array) => {+ ScalarValue::LargeList(normalize_array(array))+ }+ ScalarValue::ListView(array) => ScalarValue::ListView(normalize_array(array)),+ ScalarValue::LargeListView(array) => {+ ScalarValue::LargeListView(normalize_array(array))}+ ScalarValue::Struct(array) => ScalarValue::Struct(normalize_array(array)),+ ScalarValue::Map(array) => ScalarValue::Map(normalize_array(array)),+ ScalarValue::Union(Some((type_id, value)), fields, mode) => {+ ScalarValue::Union(+ Some((type_id, Box::new(value.normalize()))),+ fields,+ mode,+ )+ }+ ScalarValue::Dictionary(key_type, value) => {+ ScalarValue::Dictionary(key_type, Box::new(value.normalize()))+ }+ ScalarValue::RunEndEncoded(run_ends, values, value) => {+ ScalarValue::RunEndEncoded(run_ends, values, Box::new(value.normalize()))+ }+ // Everything else holds no float, or is a null float+ other @ (ScalarValue::Null+ | ScalarValue::Boolean(_)+ | ScalarValue::Float16(None)+ | ScalarValue::Float32(None)+ | ScalarValue::Float64(None)+ | ScalarValue::Decimal32(..)+ | ScalarValue::Decimal64(..)+ | ScalarValue::Decimal128(..)+ | ScalarValue::Decimal256(..)+ | ScalarValue::Int8(_)+ | ScalarValue::Int16(_)+ | ScalarValue::Int32(_)+ | ScalarValue::Int64(_)+ | ScalarValue::UInt8(_)+ | ScalarValue::UInt16(_)+ | ScalarValue::UInt32(_)+ | ScalarValue::UInt64(_)+ | ScalarValue::Utf8(_)+ | ScalarValue::Utf8View(_)+ | ScalarValue::LargeUtf8(_)+ | ScalarValue::Binary(_)+ | ScalarValue::BinaryView(_)+ | ScalarValue::FixedSizeBinary(..)+ | ScalarValue::LargeBinary(_)+ | ScalarValue::Date32(_)+ | ScalarValue::Date64(_)+ | ScalarValue::Time32Second(_)+ | ScalarValue::Time32Millisecond(_)+ | ScalarValue::Time64Microsecond(_)+ | ScalarValue::Time64Nanosecond(_)+ | ScalarValue::TimestampSecond(..)+ | ScalarValue::TimestampMillisecond(..)+ | ScalarValue::TimestampMicrosecond(..)+ | ScalarValue::TimestampNanosecond(..)+ | ScalarValue::IntervalYearMonth(_)+ | ScalarValue::IntervalDayTime(_)+ | ScalarValue::IntervalMonthDayNano(_)+ | ScalarValue::DurationSecond(_)+ | ScalarValue::DurationMillisecond(_)+ | ScalarValue::DurationMicrosecond(_)+ | ScalarValue::DurationNanosecond(_)+ | ScalarValue::Union(None, ..)) => other,}-- normalize(&mut self);- self}/// whether this value is null or not.diff --git a/datafusion/common/src/utils/mod.rs b/datafusion/common/src/utils/mod.rsindex e09d703d91..5f14150bcd 100644--- a/datafusion/common/src/utils/mod.rs+++ b/datafusion/common/src/utils/mod.rs@@ -1516,10 +1516,10 @@ fn fsl_values_row_number(list_size: i32, array_len: usize) -> Result<Int32Array>/// Replace `-0.0` with `+0.0` in any `Float16`, `Float32`, or `Float64` array.#[deprecated(since = "56.0.0",- note = "use `datafusion_common::float_semantics::normalize_floats` instead"+ note = "use `datafusion_common::float_semantics::normalize` instead")]pub fn normalize_float_zero(array: &ArrayRef) -> ArrayRef {- crate::float_semantics::normalize_floats(array)+ crate::float_semantics::normalize(Arc::clone(array))}pub use crate::float_semantics::has_float_leaf;diff --git a/datafusion/expr-common/src/columnar_value.rs b/datafusion/expr-common/src/columnar_value.rsindex 3a2c6a18de..7ef3087f08 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;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, so that+ /// `-0.0` becomes `+0.0`. Values without floats are returned unchanged.+ ///+ /// [float equality semantics]: datafusion_common::float_semantics+ pub fn normalize(self) -> Self {+ match self {+ ColumnarValue::Array(array) => {+ ColumnarValue::Array(float_semantics::normalize(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.@@ -454,10 +468,39 @@ impl fmt::Display for ColumnarValue {mod tests {use super::*;use arrow::{- array::{Date64Array, Int32Array, StructArray},- datatypes::{Field, Fields, TimeUnit},+ array::{AsArray, Date64Array, Float64Array, Int32Array, StructArray},+ datatypes::{Field, Fields, Float64Type, TimeUnit},};+ #[test]+ fn normalize_rewrites_negative_zero() {+ let array: ArrayRef = Arc::new(Float64Array::from(vec![-0.0, 1.0]));+ let ColumnarValue::Array(normalized) = ColumnarValue::Array(array).normalize()+ else {+ panic!("expected an array");+ };+ let normalized = normalized.as_primitive::<Float64Type>();+ assert_eq!(normalized.value(0).to_bits(), 0.0_f64.to_bits());+ assert_eq!(normalized.value(1), 1.0);++ let scalar = ColumnarValue::Scalar(ScalarValue::Float64(Some(-0.0)));+ let ColumnarValue::Scalar(ScalarValue::Float64(Some(normalized))) =+ scalar.normalize()+ else {+ panic!("expected a float scalar");+ };+ assert_eq!(normalized.to_bits(), 0.0_f64.to_bits());++ // Non-float values are returned as they are+ let array: ArrayRef = Arc::new(Int32Array::from(vec![1, 2]));+ let ColumnarValue::Array(normalized) =+ ColumnarValue::Array(Arc::clone(&array)).normalize()+ else {+ panic!("expected an array");+ };+ assert!(Arc::ptr_eq(&normalized, &array));+ }+#[test]fn into_array_of_size() {// Array casediff --git a/datafusion/functions-nested/src/set_ops.rs b/datafusion/functions-nested/src/set_ops.rsindex 3fe5b86d57..da4430e7df 100644--- a/datafusion/functions-nested/src/set_ops.rs+++ b/datafusion/functions-nested/src/set_ops.rs@@ -15,7 +15,7 @@ use arrow::datatypes::DataType::{LargeList, List, Null};use arrow::datatypes::{DataType, Field, FieldRef};use arrow::row::{RowConverter, SortField};use datafusion_common::cast::{as_large_list_array, as_list_array};-use datafusion_common::float_semantics::normalize_floats;+use datafusion_common::float_semantics;use datafusion_common::utils::{ListCoercion, offset_span};use datafusion_common::{Result, assert_eq_or_internal_err, exec_err, internal_err, utils::take_function_args,@@ -322,9 +322,9 @@ pub(crate) fn normalize_visible_values<OffsetSize: OffsetSizeTrait>() -> ArrayRef {let (first, len) = offset_span(array.offsets());if first == 0 && len == array.values().len() {- normalize_floats(array.values())+ float_semantics::normalize(Arc::clone(array.values()))} else {- normalize_floats(&array.values().slice(first, len))+ float_semantics::normalize(array.values().slice(first, len))}}diff --git a/datafusion/physical-expr-common/src/datum.rs b/datafusion/physical-expr-common/src/datum.rsindex fae4dcb6d9..cd4d84ab43 100644--- a/datafusion/physical-expr-common/src/datum.rs+++ b/datafusion/physical-expr-common/src/datum.rs@@ -10,7 +10,7 @@ use arrow::compute::kernels::cmp::{};use arrow::compute::{SortOptions, ilike, like, nilike, nlike};use arrow::error::ArrowError;-use datafusion_common::float_semantics::{has_float_leaf, normalize_floats};+use datafusion_common::float_semantics::{self, has_float_leaf};use datafusion_common::{Result, ScalarValue};use datafusion_common::{arrow_datafusion_err, assert_or_internal_err, internal_err};use datafusion_expr_common::columnar_value::ColumnarValue;@@ -76,19 +76,12 @@ pub fn apply_cmp(// floats, which treats `-0.0` and `+0.0` as distinct. Normalize float// operands so SQL semantics (`+0.0 == -0.0`) hold. No-op for// non-float types.- let lhs = normalize_cmp_input(lhs);- let rhs = normalize_cmp_input(rhs);+ let lhs = lhs.clone().normalize();+ let rhs = rhs.clone().normalize();apply(&lhs, &rhs, |l, r| Ok(Arc::new(f(l, r)?)))}}-fn normalize_cmp_input(cv: &ColumnarValue) -> ColumnarValue {- match cv {- ColumnarValue::Array(a) => ColumnarValue::Array(normalize_floats(a)),- ColumnarValue::Scalar(s) => ColumnarValue::Scalar(s.clone().normalize()),- }-}-/// Applies a binary [`Datum`] comparison operator `op` to `lhs` and `rhs` for nested type like/// List, FixedSizeList, LargeList, Struct, Union, Map, or a dictionary of a nested typepub fn apply_cmp_for_nested(@@ -134,7 +127,7 @@ pub fn compare_with_eq(fn normalize_nested_float_zero(array: &dyn Array) -> Option<ArrayRef> {has_float_leaf(array.data_type())- .then(|| normalize_floats(&make_array(array.to_data())))+ .then(|| float_semantics::normalize(make_array(array.to_data())))}/// Compare on nested type List, Struct, and so ondiff --git a/datafusion/physical-expr/src/expressions/in_list.rs b/datafusion/physical-expr/src/expressions/in_list.rsindex f1d772ae26..82277bd907 100644--- a/datafusion/physical-expr/src/expressions/in_list.rs+++ b/datafusion/physical-expr/src/expressions/in_list.rs@@ -19,7 +19,6 @@ use arrow::compute::kernels::boolean::{not, or_kleene};use arrow::compute::kernels::cmp::eq as arrow_eq;use arrow::datatypes::*;-use datafusion_common::float_semantics::normalize_floats;use datafusion_common::tree_node::TreeNode;use datafusion_common::{DFSchema, Result, ScalarValue, assert_or_internal_err, exec_err,@@ -72,13 +71,6 @@ fn supports_arrow_eq(dt: &DataType) -> bool {}}-fn normalize_in_list_value(value: ColumnarValue) -> ColumnarValue {- match value {- ColumnarValue::Array(array) => ColumnarValue::Array(normalize_floats(&array)),- ColumnarValue::Scalar(scalar) => ColumnarValue::Scalar(scalar.normalize()),- }-}-/// Evaluates the list of expressions into an array, flattening any dictionariesfn evaluate_list(list: &[Arc<dyn PhysicalExpr>],@@ -378,12 +370,12 @@ impl PhysicalExpr for InListExpr {// comparator for unsupported types (nested, RunEndEncoded, etc.).// Normalize the left side once for the whole list. Doing this// outside `compare_one` avoids rescanning it for every item.- let value = normalize_in_list_value(value).into_array(num_rows)?;+ let value = value.normalize().into_array(num_rows)?;let lhs_supports_arrow_eq = supports_arrow_eq(value.data_type());// Helper: compare value against a single list expressionlet compare_one = |expr: &Arc<dyn PhysicalExpr>| -> Result<BooleanArray> {- match normalize_in_list_value(expr.evaluate(batch)?) {+ match expr.evaluate(batch)?.normalize() {ColumnarValue::Array(array) => {if lhs_supports_arrow_eq&& supports_arrow_eq(array.data_type())diff --git a/datafusion/physical-expr/src/expressions/in_list/array_static_filter.rs b/datafusion/physical-expr/src/expressions/in_list/array_static_filter.rsindex d2822982c8..6373c2a554 100644--- a/datafusion/physical-expr/src/expressions/in_list/array_static_filter.rs+++ b/datafusion/physical-expr/src/expressions/in_list/array_static_filter.rs@@ -8,7 +8,7 @@ use arrow::compute::SortOptions;use arrow::datatypes::DataType;use arrow::util::bit_iterator::BitIndexIterator;use datafusion_common::Result;-use datafusion_common::float_semantics::{has_float_leaf, normalize_floats};+use datafusion_common::float_semantics::{self, has_float_leaf};use datafusion_common::hash_utils::{RandomState, with_hashes};use hashbrown::HashTable;@@ -42,7 +42,7 @@ impl ArrayStaticFilter {}// Hashing treats both signed zeros alike; the comparator must do so too.- let in_array = normalize_floats(&in_array);+ let in_array = float_semantics::normalize(in_array);let state = RandomState::default();let table = Self::build_haystack_table(&in_array, &state)?;@@ -129,7 +129,7 @@ impl StaticFilter for ArrayStaticFilter {}if has_float_leaf(v.data_type()) {- let normalized = normalize_floats(&make_array(v.to_data()));+ let normalized = float_semantics::normalize(make_array(v.to_data()));self.find_needles_in_haystack(normalized.as_ref(), negated)} else {self.find_needles_in_haystack(v, negated)diff --git a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rsindex bf784ea5d1..26200b4a03 100644--- a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs+++ b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs@@ -32,7 +32,7 @@ use arrow::datatypes::{BinaryViewType, DataType, Field, Int8Type, Int16Type, Int32Type, Int64Type, Schema,SchemaRef, StringViewType, UInt8Type, UInt16Type, UInt32Type, UInt64Type,};-use datafusion_common::float_semantics::{has_float_leaf, normalize_floats};+use datafusion_common::float_semantics::{self, has_float_leaf};use datafusion_common::hash_utils::RandomState;use datafusion_common::hash_utils::create_hashes;use datafusion_common::{Result, not_impl_err};@@ -1057,7 +1057,11 @@ impl<const STREAMING: bool> GroupValues for GroupValuesColumn<STREAMING> {let normalized: Option<Vec<ArrayRef>> = cols.iter().any(|col| col.data_type().is_nested() && has_float_leaf(col.data_type()))- .then(|| cols.iter().map(normalize_floats).collect());+ .then(|| {+ cols.iter()+ .map(|col| float_semantics::normalize(Arc::clone(col)))+ .collect()+ });let cols = normalized.as_deref().unwrap_or(cols);// `try_new` and the reset points in `emit` / `clear_shrink` keepdiff --git a/datafusion/physical-plan/src/aggregates/group_values/row.rs b/datafusion/physical-plan/src/aggregates/group_values/row.rsindex 27740e7b39..29b5eaa66b 100644--- a/datafusion/physical-plan/src/aggregates/group_values/row.rs+++ b/datafusion/physical-plan/src/aggregates/group_values/row.rs@@ -12,7 +12,7 @@ use arrow::compute::cast;use arrow::datatypes::{DataType, SchemaRef};use arrow::row::{RowConverter, Rows, SortField};use datafusion_common::Result;-use datafusion_common::float_semantics::normalize_floats;+use datafusion_common::float_semantics;use datafusion_common::hash_utils::RandomState;use datafusion_common::hash_utils::create_hashes;use datafusion_execution::memory_pool::proxy::VecAllocExt;@@ -112,7 +112,10 @@ impl GroupValues for GroupValuesRows {// Normalize -0.0 → +0.0 so RowConverter (IEEE 754 totalOrder) and// primitive hashing both group ±0 together. No-op for non-float// columns.- let normalized_cols: Vec<ArrayRef> = cols.iter().map(normalize_floats).collect();+ let normalized_cols: Vec<ArrayRef> = cols+ .iter()+ .map(|col| float_semantics::normalize(Arc::clone(col)))+ .collect();let cols = normalized_cols.as_slice();// Convert the group keys into the row formatdiff --git a/datafusion/physical-plan/src/joins/asof_join.rs b/datafusion/physical-plan/src/joins/asof_join.rsindex 04cd8b4bc9..a42e1588bb 100644--- a/datafusion/physical-plan/src/joins/asof_join.rs+++ b/datafusion/physical-plan/src/joins/asof_join.rs@@ -66,7 +66,7 @@ use arrow::array::{use arrow::buffer::NullBuffer;use arrow::compute::{SortOptions, interleave};use arrow::datatypes::{Schema, SchemaRef};-use datafusion_common::float_semantics::normalize_floats;+use datafusion_common::float_semantics;use datafusion_common::stats::Precision;use datafusion_common::tree_node::TreeNodeRecursion;use datafusion_common::utils::memory::RecordBatchMemoryCounter;@@ -902,7 +902,7 @@ impl InputCursor {// Arrow's comparator distinguishes -0.0 from +0.0, while SQL does// not. Normalizing once here is cheaper than fixing every row-level// comparison and lets the cached comparator use the arrays directly.- self.match_array = Some(normalize_floats(&match_array));+ self.match_array = Some(float_semantics::normalize(match_array));self.key_batch_id += 1;self.batch = Some(batch);}diff --git a/datafusion/physical-plan/src/joins/hash_join/exec.rs b/datafusion/physical-plan/src/joins/hash_join/exec.rsindex ffa77f5589..3dc705d34e 100644--- a/datafusion/physical-plan/src/joins/hash_join/exec.rs+++ b/datafusion/physical-plan/src/joins/hash_join/exec.rs@@ -66,7 +66,7 @@ use arrow::record_batch::RecordBatch;use arrow::util::bit_util;use arrow_schema::{DataType, Schema};use datafusion_common::config::ConfigOptions;-use datafusion_common::float_semantics::normalize_floats;+use datafusion_common::float_semantics;use datafusion_common::stats::Precision;use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeRecursion};use datafusion_common::utils::memory::{@@ -3343,8 +3343,10 @@ fn build_in_memory(// Rewrite float `-0.0` to `+0.0` once here, so the per probe batch key// comparison does not rescan the build side. Arrays without `-0.0`, and// non-float keys, are shared rather than copied.- let normalized_values: Vec<ArrayRef> =- left_values.iter().map(normalize_floats).collect();+ let normalized_values: Vec<ArrayRef> = left_values+ .iter()+ .map(|values| float_semantics::normalize(Arc::clone(values)))+ .collect();// Join keys that are plain columns share the buffers of `batch`, any other// expression evaluates to new arrays that are kept for the whole join.diff --git a/datafusion/physical-plan/src/joins/piecewise_merge_join/classic_join.rs b/datafusion/physical-plan/src/joins/piecewise_merge_join/classic_join.rsindex 5a771808b2..47b23bb664 100644--- a/datafusion/physical-plan/src/joins/piecewise_merge_join/classic_join.rs+++ b/datafusion/physical-plan/src/joins/piecewise_merge_join/classic_join.rs@@ -16,7 +16,7 @@ use arrow::{};use arrow_schema::{DataType, Schema, SchemaRef, SortOptions};use datafusion_common::NullEquality;-use datafusion_common::float_semantics::normalize_floats;+use datafusion_common::float_semantics;use datafusion_common::{Result, ScalarValue, internal_err};use datafusion_execution::{RecordBatchStream, SendableRecordBatchStream};use datafusion_expr::{ColumnarValue, JoinType, Operator};@@ -776,7 +776,7 @@ fn buffered_extreme(let last = values.slice(end - 1, 1);// `apply_cmp` normalizes `-0.0` only in flat float scalars, not inside a// `ScalarValue::Dictionary`, so normalize the key before taking it.- let extreme = normalize_floats(&last);+ let extreme = float_semantics::normalize(last);Ok(Some(ColumnarValue::Scalar(ScalarValue::try_from_array(&extreme, 0,)?)))diff --git a/datafusion/physical-plan/src/joins/utils.rs b/datafusion/physical-plan/src/joins/utils.rsindex e6024d177a..2def26225d 100644--- a/datafusion/physical-plan/src/joins/utils.rs+++ b/datafusion/physical-plan/src/joins/utils.rs@@ -55,7 +55,7 @@ use arrow::datatypes::{use arrow_ord::ord::{DynComparator, make_comparator};use arrow_schema::{DataType, SortOptions, TimeUnit};use datafusion_common::cast::as_boolean_array;-use datafusion_common::float_semantics::normalize_floats;+use datafusion_common::float_semantics;use datafusion_common::hash_utils::RandomState;use datafusion_common::hash_utils::create_hashes;use datafusion_common::stats::Precision;@@ -2455,7 +2455,7 @@ pub(super) fn equal_rows_arr(}/// Same as [`equal_rows_arr`], but `left_arrays` must already have float-/// `-0.0` rewritten to `+0.0` (see `normalize_floats`), so a comparator+/// `-0.0` rewritten to `+0.0` (see [`float_semantics::normalize`]), so a comparator/// built here skips that scan on the left side. Hash join keeps its build/// keys in that form, which saves rescanning the whole build side for every/// probe batch.@@ -2723,17 +2723,14 @@ impl JoinKeyComparator {.map(|((l, r), opts)| {// `make_comparator` uses IEEE 754 totalOrder for floats and// treats `-0.0` / `+0.0` as distinct. Normalize float arrays- // so SMJ / piecewise-merge equi-keys honor SQL equality;- // no-op (Arc::clone) for non-floats and for float arrays- // that contain no `-0.0`. `normalize_floats` preserves- // null positions, so the original null masks below remain- // valid. A left side that is already normalized is used as is.+ // to honor SQL equality. Normalizing keeps null positions, so+ // the null masks of `l` and `r` below stay valid.let l_norm = if normalize_left {- normalize_floats(l)+ float_semantics::normalize(Arc::clone(l))} else {Arc::clone(l)};- let r_norm = normalize_floats(r);+ let r_norm = float_semantics::normalize(Arc::clone(r));let inner = make_comparator(l_norm.as_ref(), r_norm.as_ref(), *opts)?;if null_equality == NullEquality::NullEqualsNothing {let ln = l.logical_nulls().filter(|n| n.null_count() > 0);