fix: treat -0.0 and +0.0 as equal in comparisons and IN list#22628
Open
sweb wants to merge 2 commits into
Open
fix: treat -0.0 and +0.0 as equal in comparisons and IN list#22628sweb wants to merge 2 commits into
sweb wants to merge 2 commits into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Which issue does this PR close?
Rationale for this change
Per IEEE 754 default semantics,
-0.0 == +0.0(and-0.0 < +0.0is false). PostgreSQL, DuckDB, and Python all follow this. DataFusion currently treats-0.0as strictly less than+0.0because arrow-rs' comparison kernels intentionally use totalOrder semantics. This produces surprising results inWHEREfilters,INlists, andIS [NOT] DISTINCT FROM, especially when-0.0is produced by arithmetic on a column (e.g.x * -1wherex = 0.0).See also https://github.com/apache/arrow-rs/blob/58.3.0/arrow-ord/src/cmp.rs#L66-L80
This was debugged, replicated and further explored using Claude Code. However, the result was adjusted and further improved.
What changes are included in this PR?
normalize_neg_zero/normalize_neg_zero_array/normalize_neg_zero_scalarindatafusion-physical-expr-common::datum. These rewrite-0.0to+0.0for float inputs and pass arrays through unchanged (no allocation) when no-0.0is present.apply_cmpso all comparison operators (=,<>,<,<=,>,>=, distinct / not-distinct, like / ilike) inherit IEEE 754 zero semantics.InListExprfor both the dynamic comparator path and the per-list-expression normalization. For the primitive static filter (which hashes viaOrderedFloat), inserting0.0now also inserts-0.0(and vice versa) so set membership matches the normalized comparison semantics.Are these changes tested?
Yes:
datum.rscover float normalization and check for passthrough when no-0.0is there, also dictionaries.Are there any user-facing changes?
Yes, comparisons against
-0.0change with this.Since this introduces an extra step for comparisons, this also has performance implications - I tried to reduce this by checking for float first and it reduces the performance hit but I did not get very consistent benchmark results. Please check whether IEEE 754 behavior for
-0.0is desirable for DataFusion and whether this line of implementation fits.