fix: order -NaN below +NaN in Literal comparison - #861
Open
LuciferYang wants to merge 2 commits into
Open
Conversation
CompareFloat returned lhs_is_negative <=> rhs_is_negative for the both-NaN case, so -NaN compared as greater than +NaN. That contradicts the adjacent "-NAN < NAN" comment and the FloatSpecialValuesComparison / DoubleSpecialValuesComparison tests, which assert the total ordering -NaN < -Infinity < ... < +Infinity < +NaN. Swap the operands so a negative sign bit sorts below a positive one. The existing NaN tests only covered same-sign pairs (qNaN vs sNaN), so the mixed-sign case was unexercised; add FloatSignedNaNComparison and DoubleSignedNaNComparison to cover it.
LuciferYang
marked this pull request as draft
July 29, 2026 12:57
LuciferYang
marked this pull request as ready for review
July 29, 2026 13:05
Contributor
Author
|
cc @wgtmac FYI |
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Fixes NaN total-ordering behavior in Literal::operator<=> so that -NaN sorts below +NaN, aligning implementation with the documented and tested ordering and adding missing mixed-sign NaN coverage (Fixes #860).
Changes:
- Corrected NaN sign-bit comparison in
CompareFloatto order-NaN < +NaN. - Added new float/double tests covering mixed-sign NaN comparisons.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/iceberg/test/literal_test.cc | Adds regression tests for signed NaN ordering for float and double. |
| src/iceberg/expression/literal.cc | Fixes NaN ordering logic to sort negative-sign NaNs below positive-sign NaNs. |
Comment on lines
+220
to
+221
| auto neg_nan = Literal::Float(-std::numeric_limits<float>::quiet_NaN()); | ||
| auto pos_nan = Literal::Float(std::numeric_limits<float>::quiet_NaN()); |
Comment on lines
+447
to
+449
| // A negative sign bit sorts below a positive one (-NaN < +NaN), so a | ||
| // negative operand must compare as less. | ||
| return rhs_is_negative <=> lhs_is_negative; |
std::numeric_limits<T>::quiet_NaN() does not guarantee a sign bit, so build the mixed-sign NaN operands with std::copysign to keep the test deterministic across platforms.
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.
What
Literal::operator<=>orders a negative NaN as greater than a positive NaN, the opposite of the total ordering documented and tested in this file.CompareFloatreturnslhs_is_negative <=> rhs_is_negativefor the both-NaN case, so-NaN <=> +NaNistrue <=> false=greater. The adjacent comment says "-NAN < NAN", andFloatSpecialValuesComparison/DoubleSpecialValuesComparisonassert-NaN < -Infinity < ... < +Infinity < +NaN, both of which this branch contradicts.Fixes #860.
How
Swap the operands so a negative sign bit sorts below a positive one:
return rhs_is_negative <=> lhs_is_negative;Testing
The existing
FloatNaNComparison/DoubleNaNComparisontests only cover same-sign NaN pairs (qNaN vs sNaN, which are equivalent), so the mixed-sign case was unexercised. AddedFloatSignedNaNComparisonandDoubleSignedNaNComparisonasserting-NaN < +NaNand the reverse. Verified fail-without (the new tests reportgreater/lessswapped) / pass-with. Fullexpression_testpasses (495 tests).