Skip to content

Commit 881738e

Browse files
Move the float zero check into float_semantics
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 94ca4bc commit 881738e

3 files changed

Lines changed: 44 additions & 11 deletions

File tree

‎datafusion/common/src/float_semantics.rs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use arrow::datatypes::{
3232
};
3333
use half::f16;
3434

35+
use crate::ScalarValue;
36+
3537
/// The floating point types the functions in this module are defined for:
3638
/// [`f16`](struct@f16), [`f32`] and [`f64`].
3739
pub trait FloatSemantics: Copy {
@@ -104,6 +106,19 @@ pub fn float_gt<T: FloatSemantics>(left: T, right: T) -> bool {
104106
compare_floats(left, right).is_gt()
105107
}
106108

109+
/// Whether `value` is a float `0.0` or `-0.0`, including one inside a
110+
/// dictionary. The two zeros are equal but [`compare_floats`] sorts them
111+
/// apart, so a sort boundary at a zero has to account for both.
112+
pub fn is_float_zero(value: &ScalarValue) -> bool {
113+
match value {
114+
ScalarValue::Float16(Some(v)) => v.to_f32() == 0.0,
115+
ScalarValue::Float32(Some(v)) => *v == 0.0,
116+
ScalarValue::Float64(Some(v)) => *v == 0.0,
117+
ScalarValue::Dictionary(_, inner) => is_float_zero(inner),
118+
_ => false,
119+
}
120+
}
121+
107122
/// The value that DataFusion hashes in place of a float. Values that DataFusion
108123
/// treats as equal hash the same, so this is the value [`normalize_float`]
109124
/// returns.
@@ -240,6 +255,30 @@ mod tests {
240255
assert!(float_gt(f64::NAN, f64::INFINITY) && !float_gt(1.0_f64, 1.0));
241256
}
242257

258+
#[test]
259+
fn is_float_zero_matches_both_zeros() {
260+
for v in [
261+
ScalarValue::Float64(Some(0.0)),
262+
ScalarValue::Float64(Some(-0.0)),
263+
ScalarValue::Float32(Some(-0.0)),
264+
ScalarValue::Float16(Some(f16::NEG_ZERO)),
265+
ScalarValue::Dictionary(
266+
Box::new(DataType::Int32),
267+
Box::new(ScalarValue::Float64(Some(-0.0))),
268+
),
269+
] {
270+
assert!(is_float_zero(&v), "{v:?}");
271+
}
272+
for v in [
273+
ScalarValue::Float64(None),
274+
ScalarValue::Float64(Some(f64::MIN_POSITIVE)),
275+
ScalarValue::Float32(Some(f32::NAN)),
276+
ScalarValue::Int32(Some(0)),
277+
] {
278+
assert!(!is_float_zero(&v), "{v:?}");
279+
}
280+
}
281+
243282
#[test]
244283
fn normalize_floats_rewrites_negative_zero() {
245284
let array: ArrayRef =

‎datafusion/physical-plan/src/topk/mod.rs‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::{SendableRecordBatchStream, stream::RecordBatchStreamAdapter};
2525

2626
use arrow::array::{ArrayRef, RecordBatch, UInt32Array, UInt64Array};
2727
use arrow::datatypes::{FieldRef, Schema, SchemaRef};
28+
use datafusion_common::float_semantics::is_float_zero;
2829
use datafusion_common::utils::memory::RecordBatchMemoryTracker;
2930
use datafusion_common::{
3031
HashMap, Result, ScalarValue, internal_datafusion_err, internal_err,
@@ -349,17 +350,6 @@ pub(crate) fn build_sort_fields(
349350
.collect::<Result<_>>()
350351
}
351352

352-
/// Whether `value` is a floating-point `0.0` or `-0.0`.
353-
fn is_float_zero(value: &ScalarValue) -> bool {
354-
match value {
355-
ScalarValue::Float16(Some(v)) => v.to_f32() == 0.0,
356-
ScalarValue::Float32(Some(v)) => *v == 0.0,
357-
ScalarValue::Float64(Some(v)) => *v == 0.0,
358-
ScalarValue::Dictionary(_, inner) => is_float_zero(inner),
359-
_ => false,
360-
}
361-
}
362-
363353
impl TopK {
364354
/// Create a new [`TopK`] that stores the top `k` values, as
365355
/// defined by the sort expressions in `expr`.

‎datafusion/sqllogictest/test_files/topk.slt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,10 @@ set datafusion.execution.target_partitions = 4;
815815
# The TopK dynamic filter must keep `-0.0` rows when the threshold is `0.0`
816816
# (and vice versa): the heap orders `-0.0` before `0.0`, but comparisons treat
817817
# them as equal. https://github.com/apache/datafusion/issues/26618
818+
#
819+
# The expected rows assume -0.0 sorts before 0.0 (the current order).
820+
# They will change when -0.0 and 0.0 are treated as equal when sorting,
821+
# see https://github.com/apache/datafusion/issues/26138
818822
statement ok
819823
set datafusion.execution.target_partitions = 1;
820824

0 commit comments

Comments
 (0)