-
Notifications
You must be signed in to change notification settings - Fork 2.4k
IN LIST: treat signed zeros as equal #25186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
geoffreyclaude
wants to merge
1
commit into
apache:main
Choose a base branch
from
geoffreyclaude:codex/in-list-signed-zero
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1407,9 +1407,9 @@ fn fsl_values_row_number(list_size: i32, array_len: usize) -> Result<Int32Array> | |
| Ok(PrimitiveArray::new(rows_number.into(), None)) | ||
| } | ||
|
|
||
| /// Replace `-0.0` with `+0.0` in any `Float16`, `Float32`, or `Float64` array. | ||
| /// For non-float arrays returns the input unchanged. NaN payloads are | ||
| /// preserved. | ||
| /// Replace `-0.0` with `+0.0` in any `Float16`, `Float32`, or `Float64` array, | ||
| /// including dictionary-wrapped floats. For other arrays, returns the input | ||
| /// unchanged. NaN payloads are preserved. | ||
| /// | ||
| /// Arrow's comparison kernels (`arrow::compute::kernels::cmp::eq` etc.) and | ||
| /// row-encoding (`arrow::row::RowConverter`) use IEEE 754 totalOrder | ||
|
|
@@ -1430,6 +1430,17 @@ pub fn normalize_float_zero(array: &ArrayRef) -> ArrayRef { | |
| const NEG_ZERO_F32_BITS: u32 = (-0.0_f32).to_bits(); | ||
| const NEG_ZERO_F64_BITS: u64 = (-0.0_f64).to_bits(); | ||
| match array.data_type() { | ||
| DataType::Dictionary(_, value_type) | ||
| if is_float_or_dictionary_float(value_type) => | ||
| { | ||
| let dictionary = array.as_any_dictionary(); | ||
| let values = normalize_float_zero(dictionary.values()); | ||
| if Arc::ptr_eq(&values, dictionary.values()) { | ||
| Arc::clone(array) | ||
| } else { | ||
| dictionary.with_values(values) | ||
| } | ||
| } | ||
| DataType::Float32 => { | ||
| let arr: &Float32Array = array.as_primitive::<Float32Type>(); | ||
| if !arr | ||
|
|
@@ -1439,8 +1450,13 @@ pub fn normalize_float_zero(array: &ArrayRef) -> ArrayRef { | |
| { | ||
| return Arc::clone(array); | ||
| } | ||
| let normalized: Float32Array = | ||
| arr.unary(|v| if v.to_bits() << 1 == 0 { 0.0_f32 } else { v }); | ||
| let normalized: Float32Array = arr.unary(|v| { | ||
| if v.to_bits() == NEG_ZERO_F32_BITS { | ||
| 0.0_f32 | ||
| } else { | ||
| v | ||
| } | ||
| }); | ||
| Arc::new(normalized) | ||
| } | ||
| DataType::Float64 => { | ||
|
|
@@ -1452,8 +1468,13 @@ pub fn normalize_float_zero(array: &ArrayRef) -> ArrayRef { | |
| { | ||
| return Arc::clone(array); | ||
| } | ||
| let normalized: Float64Array = | ||
| arr.unary(|v| if v.to_bits() << 1 == 0 { 0.0_f64 } else { v }); | ||
| let normalized: Float64Array = arr.unary(|v| { | ||
| if v.to_bits() == NEG_ZERO_F64_BITS { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only |
||
| 0.0_f64 | ||
| } else { | ||
| v | ||
| } | ||
| }); | ||
| Arc::new(normalized) | ||
| } | ||
| DataType::Float16 => { | ||
|
|
@@ -1466,8 +1487,8 @@ pub fn normalize_float_zero(array: &ArrayRef) -> ArrayRef { | |
| return Arc::clone(array); | ||
| } | ||
| let normalized: Float16Array = arr.unary(|v| { | ||
| if v.to_bits() << 1 == 0 { | ||
| half::f16::from_bits(0) | ||
| if v.to_bits() == NEG_ZERO_F16_BITS { | ||
| half::f16::ZERO | ||
| } else { | ||
| v | ||
| } | ||
|
|
@@ -1478,22 +1499,38 @@ pub fn normalize_float_zero(array: &ArrayRef) -> ArrayRef { | |
| } | ||
| } | ||
|
|
||
| fn is_float_or_dictionary_float(mut data_type: &DataType) -> bool { | ||
| while let DataType::Dictionary(_, value_type) = data_type { | ||
| data_type = value_type; | ||
| } | ||
| data_type.is_floating() | ||
| } | ||
|
|
||
| /// Replace `-0.0` with `+0.0` in `Float16`, `Float32`, or `Float64` scalar | ||
| /// values. Other variants are returned unchanged. See [`normalize_float_zero`] | ||
| /// for context. | ||
| pub fn normalize_float_zero_scalar(scalar: ScalarValue) -> ScalarValue { | ||
| match scalar { | ||
| ScalarValue::Float32(Some(v)) if v.to_bits() << 1 == 0 => { | ||
| ScalarValue::Float32(Some(0.0)) | ||
| /// values, including dictionary-wrapped floats. Other variants are returned | ||
| /// unchanged. See [`normalize_float_zero`] for context. | ||
| pub fn normalize_float_zero_scalar(mut scalar: ScalarValue) -> ScalarValue { | ||
| let mut value = &mut scalar; | ||
| while let ScalarValue::Dictionary(_, dictionary_value) = value { | ||
| value = dictionary_value.as_mut(); | ||
| } | ||
|
|
||
| match value { | ||
| ScalarValue::Float32(Some(value)) if value.to_bits() == (-0.0_f32).to_bits() => { | ||
| *value = 0.0 | ||
| } | ||
| ScalarValue::Float64(Some(v)) if v.to_bits() << 1 == 0 => { | ||
| ScalarValue::Float64(Some(0.0)) | ||
| ScalarValue::Float64(Some(value)) if value.to_bits() == (-0.0_f64).to_bits() => { | ||
| *value = 0.0 | ||
| } | ||
| ScalarValue::Float16(Some(v)) if v.to_bits() << 1 == 0 => { | ||
| ScalarValue::Float16(Some(half::f16::from_bits(0))) | ||
| ScalarValue::Float16(Some(value)) | ||
| if value.to_bits() == half::f16::NEG_ZERO.to_bits() => | ||
| { | ||
| *value = half::f16::ZERO; | ||
| } | ||
| other => other, | ||
| _ => {} | ||
| } | ||
|
|
||
| scalar | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
@@ -1503,9 +1540,9 @@ mod tests { | |
| use super::*; | ||
| use crate::ScalarValue::Null; | ||
| use arrow::{ | ||
| array::{Float64Array, Int32Array}, | ||
| array::{DictionaryArray, Float64Array, Int8Array, Int32Array}, | ||
| buffer::NullBuffer, | ||
| datatypes::Int32Type, | ||
| datatypes::{Float64Type, Int8Type, Int32Type}, | ||
| }; | ||
| #[cfg(feature = "sql")] | ||
| use sqlparser::ast::Ident; | ||
|
|
@@ -1534,6 +1571,49 @@ mod tests { | |
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn normalize_float_zero_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( | ||
| keys.clone(), | ||
| Arc::new(Float64Array::from(vec![-0.0, nan, 1.0])), | ||
| )?); | ||
|
|
||
| let normalized = normalize_float_zero(&array); | ||
| assert!(!Arc::ptr_eq(&normalized, &array)); | ||
| let dictionary = normalized.as_dictionary::<Int8Type>(); | ||
| assert_eq!(dictionary.keys(), &keys); | ||
| let values = dictionary.values().as_primitive::<Float64Type>(); | ||
| assert_eq!(values.value(0).to_bits(), 0.0_f64.to_bits()); | ||
| assert_eq!(values.value(1).to_bits(), nan.to_bits()); | ||
| assert_eq!(values.value(2), 1.0); | ||
|
|
||
| let without_negative_zero: ArrayRef = Arc::new(DictionaryArray::try_new( | ||
| Int8Array::from(vec![0, 1]), | ||
| Arc::new(Float64Array::from(vec![0.0, nan])), | ||
| )?); | ||
| assert!(Arc::ptr_eq( | ||
| &normalize_float_zero(&without_negative_zero), | ||
| &without_negative_zero | ||
| )); | ||
|
|
||
| let scalar = ScalarValue::Dictionary( | ||
| Box::new(DataType::Int8), | ||
| Box::new(ScalarValue::Float64(Some(-0.0))), | ||
| ); | ||
| let ScalarValue::Dictionary(_, value) = normalize_float_zero_scalar(scalar) | ||
| else { | ||
| unreachable!() | ||
| }; | ||
| let ScalarValue::Float64(Some(value)) = *value else { | ||
| unreachable!() | ||
| }; | ||
| assert_eq!(value.to_bits(), 0.0_f64.to_bits()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_bisect_linear_left_and_right() -> Result<()> { | ||
| let arrays: Vec<ArrayRef> = vec![ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only
NEG_ZERO_F32_BITSneeds a change, so compare directly against it