Repository navigation
fix: treat -0.0 and 0.0 as equal in short float IN lists (alternate) #27181
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ae79ee2
fix: normalize signed zeros in InListExpr so IN agrees with = for floats
alamb fc0e495
test: drop custom assertion message in simplify_inlist_signed_zero
alamb ac752db
refactor: normalize floats without copying and share the helper with `=`
alamb fa32325
docs: link filter docs to InListExpr and float_semantics, rename to C…
alamb 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
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
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
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 |
|---|---|---|
|
|
@@ -324,6 +324,7 @@ impl PhysicalExpr for InListExpr { | |
| fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> { | ||
| let num_rows = batch.num_rows(); | ||
| let value = self.expr.evaluate(batch)?; | ||
| let value = value.normalize(); // so 0.0 == -0.0 | ||
|
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. the fix is basically to sprinkle calls to |
||
| let r = match &self.static_filter { | ||
| Some(filter) => { | ||
| match value { | ||
|
|
@@ -373,7 +374,8 @@ impl PhysicalExpr for InListExpr { | |
|
|
||
| // Helper: compare value against a single list expression | ||
| let compare_one = |expr: &Arc<dyn PhysicalExpr>| -> Result<BooleanArray> { | ||
| match expr.evaluate(batch)? { | ||
| let list_value = expr.evaluate(batch)?.normalize(); // 0.0 == -0.0 | ||
|
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. here is the other call |
||
| match list_value { | ||
| ColumnarValue::Array(array) => { | ||
| if lhs_supports_arrow_eq | ||
| && supports_arrow_eq(array.data_type()) | ||
|
|
@@ -1137,6 +1139,78 @@ mod tests { | |
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn in_list_float_signed_zero() -> Result<()> { | ||
| // `IN` must agree with `=`: `-0.0` and `+0.0` are equal, whichever side | ||
| // of the comparison they appear on and whichever filter is used. | ||
| let schema = Schema::new(vec![ | ||
| Field::new("a", DataType::Float64, true), | ||
| Field::new("b", DataType::Float64, true), | ||
| ]); | ||
| let a = Float64Array::from(vec![Some(0.0), Some(-0.0), Some(1.0), None]); | ||
| let b = Float64Array::from(vec![Some(-0.0), Some(0.0), Some(-0.0), Some(0.0)]); | ||
| let col_a = col("a", &schema)?; | ||
| let batch = RecordBatch::try_new( | ||
| Arc::new(schema.clone()), | ||
| vec![Arc::new(a), Arc::new(b)], | ||
| )?; | ||
| let expected = vec![Some(true), Some(true), Some(false), None]; | ||
| let expected_negated = vec![Some(false), Some(false), Some(true), None]; | ||
|
|
||
| // Short literal lists use the branchless filter | ||
| let list = || vec![lit(-0.0f64), lit(2.0f64)]; | ||
| in_list!( | ||
| batch, | ||
| list(), | ||
| &false, | ||
| expected.clone(), | ||
| Arc::clone(&col_a), | ||
| &schema | ||
| ); | ||
| in_list!( | ||
| batch, | ||
| list(), | ||
| &true, | ||
| expected_negated, | ||
| Arc::clone(&col_a), | ||
| &schema | ||
| ); | ||
|
|
||
| // Longer literal lists use the hash set filter | ||
| let list: Vec<Arc<dyn PhysicalExpr>> = std::iter::once(lit(0.0f64)) | ||
| .chain((2..22).map(|i| lit(i as f64))) | ||
| .collect(); | ||
| in_list!( | ||
| batch, | ||
| list, | ||
| &false, | ||
| expected.clone(), | ||
| Arc::clone(&col_a), | ||
| &schema | ||
| ); | ||
|
|
||
| // A column in the list is compared at runtime | ||
| let expr = make_in_list_with_columns( | ||
| Arc::clone(&col_a), | ||
| vec![col("b", &schema)?], | ||
| false, | ||
| ); | ||
| let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?; | ||
| assert_eq!( | ||
| as_boolean_array(&result), | ||
| &BooleanArray::from(expected.clone()) | ||
| ); | ||
|
|
||
| // Lists built directly from an array are normalized as well | ||
| let haystack: ArrayRef = Arc::new(Float64Array::from(vec![-0.0])); | ||
| let expr = | ||
| InListExpr::try_new_from_array(Arc::clone(&col_a), haystack, false, &schema)?; | ||
| let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?; | ||
| assert_eq!(as_boolean_array(&result), &BooleanArray::from(expected)); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn in_list_bool() -> Result<()> { | ||
| let schema = Schema::new(vec![Field::new("a", DataType::Boolean, true)]); | ||
|
|
||
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
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
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
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.
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.
Given how much of @geoffreyclaude 's obsession has gone into performance, I wanted to minimize any impact of this fix, so this is a version that avoids a clone and reuses the allocation when needed
I could have just changed
normalize_floatsto take an owned array but that makes for a larger diff. I am happy to do so if reviewers prefer