Skip to content

Commit da40aea

Browse files
Move is_float_zero to ScalarValue
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 881738e commit da40aea

3 files changed

Lines changed: 41 additions & 41 deletions

File tree

‎datafusion/common/src/float_semantics.rs‎

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

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

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-
122107
/// The value that DataFusion hashes in place of a float. Values that DataFusion
123108
/// treats as equal hash the same, so this is the value [`normalize_float`]
124109
/// returns.
@@ -255,30 +240,6 @@ mod tests {
255240
assert!(float_gt(f64::NAN, f64::INFINITY) && !float_gt(1.0_f64, 1.0));
256241
}
257242

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-
282243
#[test]
283244
fn normalize_floats_rewrites_negative_zero() {
284245
let array: ArrayRef =

‎datafusion/common/src/scalar/mod.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,22 @@ impl ScalarValue {
27822782
}
27832783
}
27842784

2785+
/// Whether this is a float `0.0` or `-0.0`, including one inside a
2786+
/// dictionary. The two zeros are equal but sort apart (see
2787+
/// [`compare_floats`]), so a sort boundary at a zero has to account for
2788+
/// both.
2789+
///
2790+
/// [`compare_floats`]: crate::float_semantics::compare_floats
2791+
pub fn is_float_zero(&self) -> bool {
2792+
match self {
2793+
ScalarValue::Float16(Some(v)) => f32::from(*v) == 0.0,
2794+
ScalarValue::Float32(Some(v)) => *v == 0.0,
2795+
ScalarValue::Float64(Some(v)) => *v == 0.0,
2796+
ScalarValue::Dictionary(_, inner) => inner.is_float_zero(),
2797+
_ => false,
2798+
}
2799+
}
2800+
27852801
/// whether this value is null or not.
27862802
pub fn is_null(&self) -> bool {
27872803
match self {
@@ -12077,6 +12093,30 @@ mod tests {
1207712093
assert_eq!(arr.num_columns(), 0);
1207812094
}
1207912095

12096+
#[test]
12097+
fn is_float_zero_matches_both_zeros() {
12098+
for v in [
12099+
ScalarValue::Float64(Some(0.0)),
12100+
ScalarValue::Float64(Some(-0.0)),
12101+
ScalarValue::Float32(Some(-0.0)),
12102+
ScalarValue::Float16(Some(f16::NEG_ZERO)),
12103+
ScalarValue::Dictionary(
12104+
Box::new(DataType::Int32),
12105+
Box::new(ScalarValue::Float64(Some(-0.0))),
12106+
),
12107+
] {
12108+
assert!(v.is_float_zero(), "{v:?}");
12109+
}
12110+
for v in [
12111+
ScalarValue::Float64(None),
12112+
ScalarValue::Float64(Some(f64::MIN_POSITIVE)),
12113+
ScalarValue::Float32(Some(f32::NAN)),
12114+
ScalarValue::Int32(Some(0)),
12115+
] {
12116+
assert!(!v.is_float_zero(), "{v:?}");
12117+
}
12118+
}
12119+
1208012120
#[test]
1208112121
fn normalize_folds_negative_zero() {
1208212122
assert_eq!(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ 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;
2928
use datafusion_common::utils::memory::RecordBatchMemoryTracker;
3029
use datafusion_common::{
3130
HashMap, Result, ScalarValue, internal_datafusion_err, internal_err,
@@ -636,7 +635,7 @@ impl TopK {
636635
// `-0.0` rows that belong in the TopK. For a zero threshold, use a
637636
// non-strict bound that keeps both zeros and do not filter on any
638637
// later key.
639-
let float_zero = is_float_zero(value);
638+
let float_zero = value.is_float_zero();
640639

641640
// Create the appropriate operator based on sort order
642641
let op = match (sort_expr.options.descending, float_zero) {

0 commit comments

Comments
 (0)