Skip to content

Commit 14d2e7d

Browse files
authored
refactor: move native -0.0 and NaN handling into one float_semantics module (apache#6400)
* refactor: move native -0.0 and NaN handling into one float_semantics module Part of apache#6385. Each native expression that follows one of Spark's float rules carried its own copy of that rule. This moves them onto a new `float_semantics` module in `spark-expr`, with no change in behavior. - `normalize_float`, `canonicalize_nan`, `compare_floats` and `hash_input` are the per-value rules: `NormalizeNaNAndZero`, `Double.equals`, `compareDoubles`, and the `Murmur3Hash`/`XxHash64` input. - `normalize_floats`, `normalize_nested_floats` and `has_float_leaf` normalize arrays. `NormalizeNaNAndZero` and `NormalizeNestedFloats` move into the module, and `NormalizeNaNAndZero::wrap_if_needed` takes over the planner's check in `create_normalized_key_expr`. - `spark_comparator` replaces both `nested_equality` (nested `=` and `IN`) and the comparator local to `array_min`/`array_max`. hll_plus_plus, max_min_by, mode, percentile, array_extrema, array_position, arrays_overlap, nested_comparison, the float hash macros, and the planner's sort keys and range partition bounds now call the module. * Restore hot-loop speed and narrow the float_semantics API - compare_floats uses the if-chain form again, so `.is_eq()` in array_position and percentile compiles to a single comparison. - float_extrema calls new float_lt/float_gt predicates, which compile to the same instructions as the inline tests they replace. The three-way comparison was up to 9x slower in that loop. - spark_equality gives nested `=` and `IN` back the length check that skips comparing the elements of lists of different lengths. - float_semantics is a private module again, re-exporting normalize_floats, canonicalize_nan and the two expressions. - The Parquet variant cast canonicalizes NaN with canonicalize_nan, and gains a test. - Tests share the NaN constants and check the per-value rules in one table, and the max_by doc defers to the module docs. * Return an error, not a panic, for mismatched physical types in the comparator `check_types` compares logical types, so a `Dictionary(_, Float64)` passes as `Float64` and then panicked when `float_comparator` downcast it. The comparator now matches on both sides' types and returns the internal error. `mismatched_types_are_rejected` covers a dictionary on either side, at the top level and inside a list.
1 parent 8458ed2 commit 14d2e7d

19 files changed

Lines changed: 838 additions & 508 deletions

File tree

‎native/core/src/execution/planner.rs‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ use datafusion_comet_proto::{
149149
spark_partitioning::{partitioning::PartitioningStruct, Partitioning as SparkPartitioning},
150150
};
151151
use datafusion_comet_spark_expr::{
152-
jvm_udf::JvmScalarUdfExpr, spark_in_list, ApproxPercentile, ArrayInsert, Avg, AvgDecimal, Cast,
153-
CheckOverflow, Correlation, Covariance, CreateNamedStruct, DecimalRescaleCheckOverflow,
154-
GetArrayStructFields, GetStructField, HllPlusPlus, HllSketchAgg, HllUnionAgg, IfExpr,
155-
ListExtract, MaxMinBy, Mode, NormalizeNaNAndZero, Regr, RegrType, SparkCastOptions, Stddev,
156-
SumDecimal, ToJson, UnboundColumn, Variance, WideDecimalBinaryExpr, WideDecimalOp,
152+
jvm_udf::JvmScalarUdfExpr, normalize_floats, spark_in_list, ApproxPercentile, ArrayInsert, Avg,
153+
AvgDecimal, Cast, CheckOverflow, Correlation, Covariance, CreateNamedStruct,
154+
DecimalRescaleCheckOverflow, GetArrayStructFields, GetStructField, HllPlusPlus, HllSketchAgg,
155+
HllUnionAgg, IfExpr, ListExtract, MaxMinBy, Mode, NormalizeNaNAndZero, Regr, RegrType,
156+
SparkCastOptions, Stddev, SumDecimal, ToJson, UnboundColumn, Variance, WideDecimalBinaryExpr,
157+
WideDecimalOp,
157158
};
158159
use itertools::Itertools;
159160
use jni::objects::{Global, JObject};
@@ -1015,15 +1016,10 @@ impl PhysicalPlanner {
10151016
input_schema: SchemaRef,
10161017
) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> {
10171018
let child = self.create_expr(spark_expr, Arc::clone(&input_schema))?;
1018-
let data_type = child.data_type(input_schema.as_ref())?;
1019-
// Spark may already have normalized a partition or join key.
1020-
if matches!(data_type, DataType::Float32 | DataType::Float64)
1021-
&& child.downcast_ref::<NormalizeNaNAndZero>().is_none()
1022-
{
1023-
Ok(Arc::new(NormalizeNaNAndZero::new(data_type, child)))
1024-
} else {
1025-
Ok(child)
1026-
}
1019+
Ok(NormalizeNaNAndZero::wrap_if_needed(
1020+
child,
1021+
input_schema.as_ref(),
1022+
)?)
10271023
}
10281024

10291025
/// Only constant literals are supported as scan defaults.
@@ -3705,7 +3701,7 @@ impl PhysicalPlanner {
37053701
.iter()
37063702
.map(|scalar_vec| {
37073703
ScalarValue::iter_to_array(scalar_vec.iter().cloned())
3708-
.map(|array| NormalizeNaNAndZero::normalize_array(&array))
3704+
.map(|array| normalize_floats(&array))
37093705
})
37103706
.collect::<Result<Vec<_>, _>>()?;
37113707

‎native/core/src/parquet/cast_column/variant.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use arrow::{
2626
};
2727
use datafusion::common::{DataFusionError, Result as DataFusionResult};
2828
use datafusion_comet_common::SparkError;
29+
use datafusion_comet_spark_expr::canonicalize_nan;
2930
use parquet::variant::{
3031
unshred_variant, ListBuilder, MetadataBuilder, ObjectBuilder, ObjectFieldBuilder, ParentState,
3132
ReadOnlyMetadataBuilder, ValueBuilder, Variant, VariantArray, VariantBuilderExt,
@@ -852,8 +853,8 @@ fn spark_typed_scalar<'m, 'v>(value: Variant<'m, 'v>) -> Variant<'m, 'v> {
852853
.map(Variant::Decimal4)
853854
.unwrap_or(value),
854855
Variant::String(s) => Variant::from(s),
855-
Variant::Float(v) if v.is_nan() => Variant::Float(f32::NAN),
856-
Variant::Double(v) if v.is_nan() => Variant::Double(f64::NAN),
856+
Variant::Float(v) => Variant::Float(canonicalize_nan(v)),
857+
Variant::Double(v) => Variant::Double(canonicalize_nan(v)),
857858
_ => value,
858859
}
859860
}

‎native/core/src/parquet/cast_column/variant/tests.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,3 +980,27 @@ fn normalize_null_parent_ignores_empty_children() {
980980
assert!(output.is_null(0));
981981
assert!(output.as_struct().column(0).is_null(0));
982982
}
983+
984+
/// Spark writes typed floats through `floatToIntBits`/`doubleToLongBits`, so every NaN comes out
985+
/// canonical while `-0.0` keeps its sign.
986+
#[test]
987+
fn typed_scalar_canonicalizes_nan_and_keeps_negative_zero() {
988+
for bits in [0xfff8_0000_0000_0000, 0x7ff0_0000_0000_0001] {
989+
let Variant::Double(v) = spark_typed_scalar(Variant::Double(f64::from_bits(bits))) else {
990+
panic!("expected a double");
991+
};
992+
assert_eq!(v.to_bits(), f64::NAN.to_bits());
993+
}
994+
let Variant::Float(v) = spark_typed_scalar(Variant::Float(f32::from_bits(0xffc0_0000))) else {
995+
panic!("expected a float");
996+
};
997+
assert_eq!(v.to_bits(), f32::NAN.to_bits());
998+
let Variant::Double(v) = spark_typed_scalar(Variant::Double(-0.0)) else {
999+
panic!("expected a double");
1000+
};
1001+
assert_eq!(v.to_bits(), (-0.0f64).to_bits());
1002+
let Variant::Float(v) = spark_typed_scalar(Variant::Float(-0.0)) else {
1003+
panic!("expected a float");
1004+
};
1005+
assert_eq!(v.to_bits(), (-0.0f32).to_bits());
1006+
}

‎native/spark-expr/src/agg_funcs/hll_plus_plus.rs‎

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424
//! results are bit-identical to Spark.
2525
2626
use crate::agg_funcs::hll_plus_plus_const::{BIAS_DATA, RAW_ESTIMATE_DATA, THRESHOLDS};
27+
use crate::float_semantics::normalize_floats;
2728
use crate::hash_funcs::create_xxhash64_hashes;
28-
use crate::math_funcs::internal::normalize_float;
29-
use arrow::array::{
30-
Array, ArrayRef, AsArray, BooleanArray, Float32Array, Float64Array, Int64Array,
31-
};
32-
use arrow::datatypes::{DataType, Field, FieldRef, Float32Type, Float64Type};
29+
use arrow::array::{Array, ArrayRef, AsArray, BooleanArray, Int64Array};
30+
use arrow::datatypes::{DataType, Field, FieldRef};
3331
use datafusion::common::{not_impl_err, Result, ScalarValue};
3432
use datafusion::logical_expr::function::{AccumulatorArgs, StateFieldsArgs};
3533
use datafusion::logical_expr::{
@@ -120,25 +118,6 @@ impl AggregateUDFImpl for HllPlusPlus {
120118
}
121119
}
122120

123-
/// Normalize a float/double column the way Spark's `NormalizeNaNAndZero` does before hashing:
124-
/// every NaN becomes the canonical NaN and `-0.0` becomes `0.0`. Returns the input unchanged for
125-
/// non-floating-point types.
126-
fn normalize_floats(array: &ArrayRef) -> ArrayRef {
127-
match array.data_type() {
128-
DataType::Float32 => {
129-
let normalized: Float32Array =
130-
array.as_primitive::<Float32Type>().unary(normalize_float);
131-
Arc::new(normalized)
132-
}
133-
DataType::Float64 => {
134-
let normalized: Float64Array =
135-
array.as_primitive::<Float64Type>().unary(normalize_float);
136-
Arc::new(normalized)
137-
}
138-
_ => Arc::clone(array),
139-
}
140-
}
141-
142121
/// Hash a value column with Spark's `XxHash64` (seed 42), normalizing floats first, reusing
143122
/// `buf` as scratch to avoid a per-batch allocation. The buffer is re-seeded (not just cleared)
144123
/// because `create_xxhash64_hashes` folds each value into the existing seed.

‎native/spark-expr/src/agg_funcs/max_min_by.rs‎

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use arrow::array::{new_null_array, Array, ArrayRef, AsArray, BooleanArray};
18+
use crate::float_semantics::normalize_floats;
19+
use arrow::array::{new_null_array, Array, ArrayRef, BooleanArray};
1920
use arrow::compute::SortOptions;
20-
use arrow::datatypes::{DataType, Field, FieldRef, Float32Type, Float64Type};
21+
use arrow::datatypes::{DataType, Field, FieldRef};
2122
use arrow::row::{OwnedRow, RowConverter, SortField};
2223
use datafusion::common::{not_impl_err, Result, ScalarValue};
2324
use datafusion::logical_expr::function::{AccumulatorArgs, StateFieldsArgs};
@@ -39,6 +40,13 @@ use std::sync::Arc;
3940
/// buffer and, on a tie in the ordering, the later row wins. Because ties across
4041
/// partitions are processed in an unspecified order, Spark documents the function as
4142
/// non-deterministic when several rows share the extremum ordering.
43+
///
44+
/// # Float orderings
45+
///
46+
/// Spark compares the ordering with `SQLOrderingUtil.compareDoubles`, while the accumulators rank
47+
/// it in Arrow's row format, which orders floats by IEEE 754 total order. The ordering column goes
48+
/// through [`normalize_floats`] first, which makes the two agree. This is verified identical on
49+
/// Spark 3.4 through master.
4250
#[derive(Debug, Clone, PartialEq, Eq)]
4351
pub struct MaxMinBy {
4452
name: String,
@@ -145,56 +153,6 @@ fn extremum_sort_options(is_max: bool) -> SortOptions {
145153
}
146154
}
147155

148-
/// Canonicalize a floating-point ordering column so that Arrow's row-format byte order reproduces
149-
/// Spark's comparison for this aggregate.
150-
///
151-
/// Spark compares the ordering with `SQLOrderingUtil.compareDoubles`/`compareFloats`, wired in via
152-
/// `PhysicalDoubleType.ordering`/`PhysicalFloatType.ordering`. That is
153-
/// `if (x == y) 0 else java.lang.Double.compare(x, y)`, which has two consequences Arrow's row
154-
/// format does not share:
155-
///
156-
/// * `-0.0` and `0.0` tie, because the `x == y` short-circuit is IEEE equality. Arrow encodes
157-
/// floats by flipping the bits off the sign, a total order placing `-0.0` strictly below `0.0`.
158-
/// * every `NaN` is one value and sorts above `+Infinity`, because `Double.compare` goes through
159-
/// `doubleToLongBits`. Arrow uses the raw bits, so a sign-bit-set `NaN` would sort below
160-
/// `-Infinity` instead.
161-
///
162-
/// Folding `-0.0` into `0.0` and every `NaN` into the canonical `NaN` makes the row bytes agree
163-
/// with `compareDoubles` on both counts. This is verified identical on Spark 3.4 through master.
164-
///
165-
/// Note this is the opposite of what `mode` needs: `mode` keys a hash map via
166-
/// `OpenHashSet`'s `equals` (`java.lang.Double.equals`), which distinguishes `-0.0` from `0.0`, so
167-
/// it must *not* fold them. Same two input values, different Spark comparison path, opposite
168-
/// correct behaviour.
169-
fn canonicalize_float_ordering(array: &ArrayRef) -> ArrayRef {
170-
match array.data_type() {
171-
DataType::Float32 => Arc::new(array.as_primitive::<Float32Type>().unary::<_, Float32Type>(
172-
|v| {
173-
if v.is_nan() {
174-
f32::NAN
175-
} else if v == 0.0 {
176-
// `-0.0 == 0.0` in IEEE 754, so this catches negative zero only.
177-
0.0
178-
} else {
179-
v
180-
}
181-
},
182-
)),
183-
DataType::Float64 => Arc::new(array.as_primitive::<Float64Type>().unary::<_, Float64Type>(
184-
|v| {
185-
if v.is_nan() {
186-
f64::NAN
187-
} else if v == 0.0 {
188-
0.0
189-
} else {
190-
v
191-
}
192-
},
193-
)),
194-
_ => Arc::clone(array),
195-
}
196-
}
197-
198156
/// Accumulator that tracks the running `(value, ordering)` pair for the extremum ordering.
199157
#[derive(Debug)]
200158
struct MaxMinByAccumulator {
@@ -231,7 +189,7 @@ impl MaxMinByAccumulator {
231189
return Ok(());
232190
}
233191

234-
let ordering_arr = canonicalize_float_ordering(ordering_arr);
192+
let ordering_arr = normalize_floats(ordering_arr);
235193
let rows = self
236194
.ordering_converter
237195
.convert_columns(&[Arc::clone(&ordering_arr)])?;
@@ -389,7 +347,7 @@ impl MaxMinByGroupsAccumulator {
389347
let value_rows = self
390348
.value_converter
391349
.convert_columns(&[Arc::clone(&values[0])])?;
392-
let ordering_arr = canonicalize_float_ordering(&values[1]);
350+
let ordering_arr = normalize_floats(&values[1]);
393351
let ordering_rows = self
394352
.ordering_converter
395353
.convert_columns(&[Arc::clone(&ordering_arr)])?;

‎native/spark-expr/src/agg_funcs/mode.rs‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* under the License.
1818
*/
1919

20+
use crate::float_semantics::{canonicalize_nan, normalize_float};
2021
use arrow::array::{Array, ArrayRef, AsArray, BooleanArray, StructArray};
2122
use arrow::datatypes::{DataType, Field, FieldRef, Fields, Int64Type};
2223
use datafusion::common::{internal_datafusion_err, not_impl_err, Result, ScalarValue};
@@ -171,21 +172,15 @@ impl AggregateUDFImpl for Mode {
171172
/// equality, which is what Spark's `OpenHashSet` uses. `-0.0` is folded into `0.0` only when
172173
/// `normalize_neg_zero` is set, i.e. only on Spark 4.2.0+ (SPARK-57329); see [`Mode`].
173174
fn normalize_key(value: ScalarValue, normalize_neg_zero: bool) -> ScalarValue {
174-
macro_rules! normalize_float {
175-
($variant:path, $f:expr, $nan:expr) => {
176-
if $f.is_nan() {
177-
$variant(Some($nan))
178-
} else if normalize_neg_zero && $f == 0.0 {
179-
// `-0.0 == 0.0` in IEEE 754, so this catches negative zero only.
180-
$variant(Some(0.0))
181-
} else {
182-
$variant(Some($f))
183-
}
184-
};
185-
}
186175
match value {
187-
ScalarValue::Float32(Some(f)) => normalize_float!(ScalarValue::Float32, f, f32::NAN),
188-
ScalarValue::Float64(Some(f)) => normalize_float!(ScalarValue::Float64, f, f64::NAN),
176+
ScalarValue::Float32(Some(f)) if normalize_neg_zero => {
177+
ScalarValue::Float32(Some(normalize_float(f)))
178+
}
179+
ScalarValue::Float64(Some(f)) if normalize_neg_zero => {
180+
ScalarValue::Float64(Some(normalize_float(f)))
181+
}
182+
ScalarValue::Float32(Some(f)) => ScalarValue::Float32(Some(canonicalize_nan(f))),
183+
ScalarValue::Float64(Some(f)) => ScalarValue::Float64(Some(canonicalize_nan(f))),
189184
other => other,
190185
}
191186
}

‎native/spark-expr/src/agg_funcs/percentile.rs‎

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use crate::float_semantics::compare_floats;
1819
use arrow::array::{
1920
Array, ArrayRef, AsArray, BooleanArray, Float64Array, Float64Builder, ListArray,
2021
};
@@ -27,7 +28,6 @@ use datafusion::logical_expr::{
2728
Accumulator, AggregateUDFImpl, EmitTo, GroupsAccumulator, ReversedUDAF, Signature,
2829
};
2930
use datafusion::physical_expr::expressions::format_state_name;
30-
use std::cmp::Ordering;
3131
use std::mem::{size_of, size_of_val};
3232
use std::sync::Arc;
3333

@@ -297,38 +297,24 @@ fn spark_percentile(values: &mut [f64], percentile: f64) -> Option<f64> {
297297
let lower = position.floor() as usize;
298298
let higher = position.ceil() as usize;
299299

300-
let (_, lower_value, _) = values.select_nth_unstable_by(lower, spark_double_cmp);
300+
let (_, lower_value, _) = values.select_nth_unstable_by(lower, |x, y| compare_floats(*x, *y));
301301
let lower_value = *lower_value;
302302
if lower == higher {
303303
return Some(lower_value);
304304
}
305305

306-
let (_, higher_value, _) = values.select_nth_unstable_by(higher, spark_double_cmp);
306+
let (_, higher_value, _) = values.select_nth_unstable_by(higher, |x, y| compare_floats(*x, *y));
307307
let higher_value = *higher_value;
308-
if spark_double_cmp(&lower_value, &higher_value) == Ordering::Equal {
308+
if compare_floats(lower_value, higher_value).is_eq() {
309309
return Some(lower_value);
310310
}
311311

312312
Some((higher as f64 - position) * lower_value + (position - lower as f64) * higher_value)
313313
}
314314

315-
fn spark_double_cmp(x: &f64, y: &f64) -> Ordering {
316-
if x == y || (x.is_nan() && y.is_nan()) {
317-
Ordering::Equal
318-
} else if x.is_nan() {
319-
Ordering::Greater
320-
} else if y.is_nan() {
321-
Ordering::Less
322-
} else {
323-
x.partial_cmp(y)
324-
.expect("non-NaN values should be comparable")
325-
}
326-
}
327-
328315
#[cfg(test)]
329316
mod tests {
330-
use super::{spark_double_cmp, spark_percentile};
331-
use std::cmp::Ordering;
317+
use super::spark_percentile;
332318

333319
#[test]
334320
fn interpolates_with_full_spark_precision() {
@@ -340,10 +326,10 @@ mod tests {
340326
}
341327

342328
#[test]
343-
fn matches_spark_double_ordering_for_nan_and_zero() {
344-
assert_eq!(spark_double_cmp(&f64::NAN, &1.0), Ordering::Greater);
345-
assert_eq!(spark_double_cmp(&1.0, &f64::NAN), Ordering::Less);
346-
assert_eq!(spark_double_cmp(&f64::NAN, &f64::NAN), Ordering::Equal);
347-
assert_eq!(spark_double_cmp(&-0.0, &0.0), Ordering::Equal);
329+
fn orders_nan_above_every_other_value() {
330+
// A NaN with the sign bit set, which IEEE 754 total order would put first.
331+
let mut values = vec![f64::from_bits(0xfff8_0000_0000_0000), 2.0, 1.0];
332+
assert_eq!(spark_percentile(&mut values, 0.0), Some(1.0));
333+
assert!(spark_percentile(&mut values, 1.0).unwrap().is_nan());
348334
}
349335
}

0 commit comments

Comments
 (0)