Skip to content

Commit 7dd1c6a

Browse files
authored
feat: Support IEEE 754 negative zero semantics (apache#22835)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes apache#22826 . - Closes apache#22490 - Closes apache#11108 ## Rationale for this change SQL (per PG and IEEE 754) treats `+0.0` and `-0.0` as equal in `=`, `IS DISTINCT FROM`, DISTINCT, GROUP BY, UNION/INTERSECT/EXCEPT, equi-joins, and `array_*` set ops. DataFusion treated them as distinct because: - Arrow's `cmp::eq`/`gt`/`lt` use IEEE 754 totalOrder for floats — `arrow-ord-58.3.0/src/cmp.rs:71-75` explicitly says *"please normalize zeros before calling this kernel"*. - Arrow's `RowConverter` row-encodes floats with totalOrder; ±0 produce different bytes. - DataFusion's primitive float hashing used raw `to_bits()` / `to_ne_bytes()`, so ±0 hashed to different buckets. ## What changes are included in this PR? **Helper** in `datafusion/common/src/utils/mod.rs`: - `normalize_float_zero(&ArrayRef) -> ArrayRef` — rewrites `-0.0 → +0.0` for Float16/32/64 via `PrimitiveArray::unary`; `Arc::clone` for non-float. NaN payloads preserved (`bits << 1 == 0` matches only ±0). - `normalize_float_zero_scalar(ScalarValue) -> ScalarValue` — symmetric for scalars. **Applied at six boundary sites** where DataFusion hands float data to Arrow: | Site | Fixes | |---|---| | `physical-expr-common/src/datum.rs::apply_cmp` | BinaryExpr `=`, `<`, `>`, `IS DISTINCT FROM` | | `physical-plan/src/joins/utils.rs::eq_dyn_null` | HashJoin row equality → INNER JOIN, INTERSECT, EXCEPT | | `physical-plan/src/aggregates/group_values/row.rs::intern` | Multi-column row-encoded GROUP BY | | `functions-nested/src/set_ops.rs::general_array_distinct` | `array_distinct` | | `functions-nested/src/set_ops.rs::generic_set_lists` | `array_union`, `array_intersect` | | `functions-nested/src/except.rs::general_except` | `array_except` | **Float hash macros** normalize for consistency: - `datafusion/common/src/hash_utils.rs::hash_float_value!` — `create_hashes`, hash joins, shuffle. - `datafusion/physical-plan/src/aggregates/group_values/single_group_by/primitive.rs::hash_float!` — single-column primitive GROUP BY fast path. **Single-column primitive GROUP BY / DISTINCT** (`GroupValuesPrimitive::intern`, `PrimitiveGroupValueBuilder::{append_val,vectorized_append,equal_to,vectorized_equal_to_*}`) canonicalize the input via a new default-identity `canonicalize` method on the local `HashValue` trait (float override only). Trait visibility lifted to `pub` so the multi-column file can use it.
1 parent c0dc571 commit 7dd1c6a

13 files changed

Lines changed: 611 additions & 32 deletions

File tree

‎datafusion/common/src/hash_utils.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,16 @@ macro_rules! hash_float_value {
188188
($(($t:ty, $i:ty)),+) => {
189189
$(impl HashValue for $t {
190190
fn hash_one(&self, state: &RandomState) -> u64 {
191-
state.hash_one(<$i>::from_ne_bytes(self.to_ne_bytes()))
191+
// +0.0 and -0.0 differ only in the sign bit but compare equal
192+
// under IEEE 754; normalize -0.0 → +0.0 so Hash agrees with Eq.
193+
let bits = <$i>::from_ne_bytes(self.to_ne_bytes());
194+
let bits = if bits << 1 == 0 { 0 } else { bits };
195+
state.hash_one(bits)
192196
}
193197
fn hash_write(&self, hasher: &mut impl Hasher) {
194-
hasher.write(&self.to_ne_bytes())
198+
let bits = <$i>::from_ne_bytes(self.to_ne_bytes());
199+
let bits: $i = if bits << 1 == 0 { 0 } else { bits };
200+
hasher.write(&bits.to_ne_bytes())
195201
}
196202
})+
197203
};

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,95 @@ fn fsl_values_row_number(list_size: i32, array_len: usize) -> Result<Int32Array>
13861386
Ok(PrimitiveArray::new(rows_number.into(), None))
13871387
}
13881388

1389+
/// Replace `-0.0` with `+0.0` in any `Float16`, `Float32`, or `Float64` array.
1390+
/// For non-float arrays returns the input unchanged. NaN payloads are
1391+
/// preserved.
1392+
///
1393+
/// Arrow's comparison kernels (`arrow::compute::kernels::cmp::eq` etc.) and
1394+
/// row-encoding (`arrow::row::RowConverter`) use IEEE 754 totalOrder
1395+
/// semantics, which treats `-0.0` and `+0.0` as distinct. SQL semantics
1396+
/// (PostgreSQL / IEEE 754 equality) require them to compare equal, so
1397+
/// callers normalize before invoking those kernels.
1398+
///
1399+
/// The common case - no `-0.0` present - is allocation-free: a single
1400+
/// read-only scan of the underlying buffer (auto-vectorizable to an
1401+
/// OR-reduction) decides whether to fall through to the rewriting path.
1402+
/// Only arrays that actually contain `-0.0` pay for a new buffer.
1403+
pub fn normalize_float_zero(array: &ArrayRef) -> ArrayRef {
1404+
use arrow::array::{Float16Array, Float32Array, Float64Array};
1405+
use arrow::datatypes::{Float16Type, Float32Type, Float64Type};
1406+
// -0.0 has only the sign bit set; no other finite or NaN value shares
1407+
// this bit pattern, so a strict-equality scan reliably gates the rewrite.
1408+
const NEG_ZERO_F16_BITS: u16 = half::f16::NEG_ZERO.to_bits();
1409+
const NEG_ZERO_F32_BITS: u32 = (-0.0_f32).to_bits();
1410+
const NEG_ZERO_F64_BITS: u64 = (-0.0_f64).to_bits();
1411+
match array.data_type() {
1412+
DataType::Float32 => {
1413+
let arr: &Float32Array = array.as_primitive::<Float32Type>();
1414+
if !arr
1415+
.values()
1416+
.iter()
1417+
.any(|v| v.to_bits() == NEG_ZERO_F32_BITS)
1418+
{
1419+
return Arc::clone(array);
1420+
}
1421+
let normalized: Float32Array =
1422+
arr.unary(|v| if v.to_bits() << 1 == 0 { 0.0_f32 } else { v });
1423+
Arc::new(normalized)
1424+
}
1425+
DataType::Float64 => {
1426+
let arr: &Float64Array = array.as_primitive::<Float64Type>();
1427+
if !arr
1428+
.values()
1429+
.iter()
1430+
.any(|v| v.to_bits() == NEG_ZERO_F64_BITS)
1431+
{
1432+
return Arc::clone(array);
1433+
}
1434+
let normalized: Float64Array =
1435+
arr.unary(|v| if v.to_bits() << 1 == 0 { 0.0_f64 } else { v });
1436+
Arc::new(normalized)
1437+
}
1438+
DataType::Float16 => {
1439+
let arr: &Float16Array = array.as_primitive::<Float16Type>();
1440+
if !arr
1441+
.values()
1442+
.iter()
1443+
.any(|v| v.to_bits() == NEG_ZERO_F16_BITS)
1444+
{
1445+
return Arc::clone(array);
1446+
}
1447+
let normalized: Float16Array = arr.unary(|v| {
1448+
if v.to_bits() << 1 == 0 {
1449+
half::f16::from_bits(0)
1450+
} else {
1451+
v
1452+
}
1453+
});
1454+
Arc::new(normalized)
1455+
}
1456+
_ => Arc::clone(array),
1457+
}
1458+
}
1459+
1460+
/// Replace `-0.0` with `+0.0` in `Float16`, `Float32`, or `Float64` scalar
1461+
/// values. Other variants are returned unchanged. See [`normalize_float_zero`]
1462+
/// for context.
1463+
pub fn normalize_float_zero_scalar(scalar: ScalarValue) -> ScalarValue {
1464+
match scalar {
1465+
ScalarValue::Float32(Some(v)) if v.to_bits() << 1 == 0 => {
1466+
ScalarValue::Float32(Some(0.0))
1467+
}
1468+
ScalarValue::Float64(Some(v)) if v.to_bits() << 1 == 0 => {
1469+
ScalarValue::Float64(Some(0.0))
1470+
}
1471+
ScalarValue::Float16(Some(v)) if v.to_bits() << 1 == 0 => {
1472+
ScalarValue::Float16(Some(half::f16::from_bits(0)))
1473+
}
1474+
other => other,
1475+
}
1476+
}
1477+
13891478
#[cfg(test)]
13901479
mod tests {
13911480
use std::sync::Arc;

‎datafusion/functions-nested/src/except.rs‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use arrow::buffer::{NullBuffer, OffsetBuffer};
2727
use arrow::compute::take;
2828
use arrow::datatypes::{DataType, FieldRef};
2929
use arrow::row::{RowConverter, SortField};
30-
use datafusion_common::utils::{ListCoercion, take_function_args};
30+
use datafusion_common::utils::{ListCoercion, normalize_float_zero, take_function_args};
3131
use datafusion_common::{HashSet, Result, internal_err};
3232
use datafusion_expr::{
3333
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
@@ -169,16 +169,21 @@ fn general_except<OffsetSize: OffsetSizeTrait>(
169169
) -> Result<GenericListArray<OffsetSize>> {
170170
let converter = RowConverter::new(vec![SortField::new(l.value_type())])?;
171171

172+
// Normalize -0.0 → +0.0 so RowConverter (IEEE 754 totalOrder) groups
173+
// ±0 together for both the rhs lookup set and the lhs probe.
174+
let l_values_norm = normalize_float_zero(l.values());
175+
let r_values_norm = normalize_float_zero(r.values());
176+
172177
// Only convert the visible portion of the values array. For sliced
173178
// ListArrays, values() returns the full underlying array but only
174179
// elements between the first and last offset are referenced.
175180
let l_first = l.offsets()[0].as_usize();
176181
let l_len = l.offsets()[l.len()].as_usize() - l_first;
177-
let l_values = converter.convert_columns(&[l.values().slice(l_first, l_len)])?;
182+
let l_values = converter.convert_columns(&[l_values_norm.slice(l_first, l_len)])?;
178183

179184
let r_first = r.offsets()[0].as_usize();
180185
let r_len = r.offsets()[r.len()].as_usize() - r_first;
181-
let r_values = converter.convert_columns(&[r.values().slice(r_first, r_len)])?;
186+
let r_values = converter.convert_columns(&[r_values_norm.slice(r_first, r_len)])?;
182187

183188
let mut offsets = Vec::<OffsetSize>::with_capacity(l.len() + 1);
184189
offsets.push(OffsetSize::usize_as(0));
@@ -223,11 +228,11 @@ fn general_except<OffsetSize: OffsetSizeTrait>(
223228
} else if OffsetSize::IS_LARGE {
224229
let indices =
225230
UInt64Array::from(indices.into_iter().map(|i| i as u64).collect::<Vec<_>>());
226-
take(l.values().as_ref(), &indices, None)?
231+
take(l_values_norm.as_ref(), &indices, None)?
227232
} else {
228233
let indices =
229234
UInt32Array::from(indices.into_iter().map(|i| i as u32).collect::<Vec<_>>());
230-
take(l.values().as_ref(), &indices, None)?
235+
take(l_values_norm.as_ref(), &indices, None)?
231236
};
232237

233238
Ok(GenericListArray::<OffsetSize>::new(

‎datafusion/functions-nested/src/set_ops.rs‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use arrow::datatypes::DataType::{LargeList, List, Null};
2828
use arrow::datatypes::{DataType, Field, FieldRef};
2929
use arrow::row::{RowConverter, SortField};
3030
use datafusion_common::cast::{as_large_list_array, as_list_array};
31-
use datafusion_common::utils::ListCoercion;
31+
use datafusion_common::utils::{ListCoercion, normalize_float_zero};
3232
use datafusion_common::{
3333
Result, assert_eq_or_internal_err, exec_err, internal_err, utils::take_function_args,
3434
};
@@ -351,21 +351,28 @@ fn generic_set_lists<OffsetSize: OffsetSizeTrait>(
351351

352352
let converter = RowConverter::new(vec![SortField::new(l.value_type())])?;
353353

354+
// Normalize -0.0 → +0.0 so RowConverter (which uses IEEE 754 totalOrder
355+
// and treats ±0 as distinct) groups them together. Use the normalized
356+
// arrays for both row conversion and the final output values.
357+
let l_values_norm = normalize_float_zero(l.values());
358+
let r_values_norm = normalize_float_zero(r.values());
359+
354360
// Only convert the visible portion of the values array. For sliced
355361
// ListArrays, values() returns the full underlying array but only
356362
// elements between the first and last offset are referenced.
357363
let l_first = l.offsets()[0].as_usize();
358364
let l_len = l.offsets()[l.len()].as_usize() - l_first;
359-
let rows_l = converter.convert_columns(&[l.values().slice(l_first, l_len)])?;
365+
let l_values = l_values_norm.slice(l_first, l_len);
366+
let rows_l = converter.convert_columns(&[Arc::clone(&l_values)])?;
360367

361368
let r_first = r.offsets()[0].as_usize();
362369
let r_len = r.offsets()[r.len()].as_usize() - r_first;
363-
let rows_r = converter.convert_columns(&[r.values().slice(r_first, r_len)])?;
370+
let r_values = r_values_norm.slice(r_first, r_len);
371+
let rows_r = converter.convert_columns(&[Arc::clone(&r_values)])?;
364372

365-
// Combine the *sliced* value arrays so 0-based indices from the row
366-
// converter map directly into the concatenated array.
367-
let l_values = l.values().slice(l_first, l_len);
368-
let r_values = r.values().slice(r_first, r_len);
373+
// Indices from the row converter are 0-based in the per-side slice;
374+
// concatenating those same slices lets indices map directly into the
375+
// combined values array.
369376
let combined_values = concat(&[l_values.as_ref(), r_values.as_ref()])?;
370377
let r_offset = l_len;
371378

@@ -558,13 +565,18 @@ fn general_array_distinct<OffsetSize: OffsetSizeTrait>(
558565

559566
let converter = RowConverter::new(vec![SortField::new(dt.clone())])?;
560567

568+
// Normalize -0.0 → +0.0 so RowConverter (which uses IEEE 754 totalOrder
569+
// and treats ±0 as distinct) groups them together, and so the output
570+
// carries the canonical sign.
571+
let values_norm = normalize_float_zero(array.values());
572+
561573
// Only convert the visible portion of the values array. For sliced
562574
// ListArrays, values() returns the full underlying array but only
563575
// elements between the first and last offset are referenced.
564576
let first_offset = value_offsets[0].as_usize();
565577
let visible_len = value_offsets[array.len()].as_usize() - first_offset;
566578
let rows =
567-
converter.convert_columns(&[array.values().slice(first_offset, visible_len)])?;
579+
converter.convert_columns(&[values_norm.slice(first_offset, visible_len)])?;
568580

569581
let mut indices: Vec<usize> = Vec::with_capacity(rows.num_rows());
570582
let mut seen = HashSet::new();
@@ -593,19 +605,19 @@ fn general_array_distinct<OffsetSize: OffsetSizeTrait>(
593605
}
594606

595607
// Gather distinct values in a single pass, using the computed `indices`.
596-
// Indices are absolute positions in array.values() (first_offset was added
597-
// back when collecting them), so we can take directly from the full values.
608+
// Indices are absolute positions in the (normalized) values array, so we
609+
// can take directly from the full values.
598610
// Use UInt64Array for LargeList to support values arrays exceeding u32::MAX.
599611
let final_values = if indices.is_empty() {
600612
new_empty_array(&dt)
601613
} else if OffsetSize::IS_LARGE {
602614
let indices =
603615
UInt64Array::from(indices.into_iter().map(|i| i as u64).collect::<Vec<_>>());
604-
take(array.values().as_ref(), &indices, None)?
616+
take(values_norm.as_ref(), &indices, None)?
605617
} else {
606618
let indices =
607619
UInt32Array::from(indices.into_iter().map(|i| i as u32).collect::<Vec<_>>());
608-
take(array.values().as_ref(), &indices, None)?
620+
take(values_norm.as_ref(), &indices, None)?
609621
};
610622

611623
Ok(Arc::new(GenericListArray::<OffsetSize>::try_new(

‎datafusion/physical-expr-common/src/datum.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use arrow::compute::kernels::cmp::{
2323
};
2424
use arrow::compute::{SortOptions, ilike, like, nilike, nlike};
2525
use arrow::error::ArrowError;
26+
use datafusion_common::utils::{normalize_float_zero, normalize_float_zero_scalar};
2627
use datafusion_common::{Result, ScalarValue};
2728
use datafusion_common::{arrow_datafusion_err, assert_or_internal_err, internal_err};
2829
use datafusion_expr_common::columnar_value::ColumnarValue;
@@ -84,7 +85,22 @@ pub fn apply_cmp(
8485
}
8586
};
8687

87-
apply(lhs, rhs, |l, r| Ok(Arc::new(f(l, r)?)))
88+
// Arrow's comparison kernels use IEEE 754 totalOrder semantics for
89+
// floats, which treats `-0.0` and `+0.0` as distinct. Normalize float
90+
// operands so SQL semantics (`+0.0 == -0.0`) hold. No-op for
91+
// non-float types.
92+
let lhs = normalize_cmp_input(lhs);
93+
let rhs = normalize_cmp_input(rhs);
94+
apply(&lhs, &rhs, |l, r| Ok(Arc::new(f(l, r)?)))
95+
}
96+
}
97+
98+
fn normalize_cmp_input(cv: &ColumnarValue) -> ColumnarValue {
99+
match cv {
100+
ColumnarValue::Array(a) => ColumnarValue::Array(normalize_float_zero(a)),
101+
ColumnarValue::Scalar(s) => {
102+
ColumnarValue::Scalar(normalize_float_zero_scalar(s.clone()))
103+
}
88104
}
89105
}
90106

‎datafusion/physical-plan/src/aggregates/group_values/multi_group_by/primitive.rs‎

Lines changed: 17 additions & 8 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::aggregates::group_values::HashValue;
1819
use crate::aggregates::group_values::multi_group_by::{
1920
GroupColumn, Nulls, nulls_equal_to,
2021
};
@@ -51,6 +52,7 @@ pub struct PrimitiveGroupValueBuilder<T: ArrowPrimitiveType, const NULLABLE: boo
5152
impl<T, const NULLABLE: bool> PrimitiveGroupValueBuilder<T, NULLABLE>
5253
where
5354
T: ArrowPrimitiveType,
55+
T::Native: HashValue,
5456
{
5557
/// Create a new `PrimitiveGroupValueBuilder`
5658
pub fn new(data_type: DataType) -> Self {
@@ -91,7 +93,9 @@ where
9193
} else {
9294
unsafe { *array_values.get_unchecked(rhs_row) }
9395
};
94-
if left.is_eq(right) {
96+
// `left` was already canonicalized on append; canonicalize the
97+
// input so ±0 (and any future equivalence class) compares equal.
98+
if left.is_eq(right.canonicalize()) {
9599
cmp_buf[i / 8] |= 1 << (i % 8);
96100
}
97101
}
@@ -133,7 +137,7 @@ where
133137
continue;
134138
}
135139

136-
if !self.group_values[lhs_row].is_eq(array.value(rhs_row)) {
140+
if !self.group_values[lhs_row].is_eq(array.value(rhs_row).canonicalize()) {
137141
equal_to_results.set_bit(idx, false);
138142
}
139143
}
@@ -142,6 +146,8 @@ where
142146

143147
impl<T: ArrowPrimitiveType, const NULLABLE: bool> GroupColumn
144148
for PrimitiveGroupValueBuilder<T, NULLABLE>
149+
where
150+
T::Native: HashValue,
145151
{
146152
fn equal_to(&self, lhs_row: usize, array: &ArrayRef, rhs_row: usize) -> bool {
147153
// Perf: skip null check (by short circuit) if input is not nullable
@@ -154,7 +160,8 @@ impl<T: ArrowPrimitiveType, const NULLABLE: bool> GroupColumn
154160
// Otherwise, we need to check their values
155161
}
156162

157-
self.group_values[lhs_row].is_eq(array.as_primitive::<T>().value(rhs_row))
163+
self.group_values[lhs_row]
164+
.is_eq(array.as_primitive::<T>().value(rhs_row).canonicalize())
158165
}
159166

160167
fn append_val(&mut self, array: &ArrayRef, row: usize) -> Result<()> {
@@ -165,10 +172,12 @@ impl<T: ArrowPrimitiveType, const NULLABLE: bool> GroupColumn
165172
self.group_values.push(T::default_value());
166173
} else {
167174
self.nulls.append(false);
168-
self.group_values.push(array.as_primitive::<T>().value(row));
175+
self.group_values
176+
.push(array.as_primitive::<T>().value(row).canonicalize());
169177
}
170178
} else {
171-
self.group_values.push(array.as_primitive::<T>().value(row));
179+
self.group_values
180+
.push(array.as_primitive::<T>().value(row).canonicalize());
172181
}
173182

174183
Ok(())
@@ -214,15 +223,15 @@ impl<T: ArrowPrimitiveType, const NULLABLE: bool> GroupColumn
214223
self.group_values.push(T::default_value());
215224
} else {
216225
self.nulls.append(false);
217-
self.group_values.push(arr.value(row));
226+
self.group_values.push(arr.value(row).canonicalize());
218227
}
219228
}
220229
}
221230

222231
(true, Nulls::None) => {
223232
self.nulls.append_n(rows.len(), false);
224233
for &row in rows {
225-
self.group_values.push(arr.value(row));
234+
self.group_values.push(arr.value(row).canonicalize());
226235
}
227236
}
228237

@@ -234,7 +243,7 @@ impl<T: ArrowPrimitiveType, const NULLABLE: bool> GroupColumn
234243

235244
(false, _) => {
236245
for &row in rows {
237-
self.group_values.push(arr.value(row));
246+
self.group_values.push(arr.value(row).canonicalize());
238247
}
239248
}
240249
}

0 commit comments

Comments
 (0)