GH-4235, GH-4239: NaN in numeric comparison and in the xsd:boolean cast - #4246
Open
anishmehta24 wants to merge 2 commits into
Open
anishmehta24 wants to merge 2 commits into
anishmehta24 wants to merge 2 commits into
Conversation
castToBoolean tested for NaN with NodeValue.sameValueAs(nv, nvDoubleNaN),
which is never true because NaN is not the same value as anything, so
xsd:boolean("NaN"^^xsd:double) fell through to true. Use the effective
boolean value of the number, which is the same rule as the XPath cast:
zero, -0.0 and NaN are false, everything else true.
XPath numeric comparison defines every ordering comparison with NaN as false, and NaN as not equal to anything, but compareNumeric used Double.compare/Float.compare, in which NaN is greater than every other value, so FILTER(?v > 1e0) accepted NaN. Value comparison (compare, used by <, <=, >, >=) now reports NaN as not comparable, sameValueAs returns false for NaN of any numeric type, and ordering (compareAlways, used by ORDER BY) keeps the Java total order with NaN after every other number.
4 tasks
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.
GitHub issues resolved #4235, #4239
Pull request Description:
Two coupled NaN fixes, one commit each (this PR replaces #4244, which afs noted overlapped with it).
GH-4239 (
cf55ae2d)CastXSD.castToBooleantested for NaN withNodeValue.sameValueAs(nv, nvDoubleNaN), which is never true because NaN is not the same value as anything, soxsd:boolean("NaN"^^xsd:double)fell through totrue. The float case only passed by accident ("NaN"^^xsd:floatis a different term, so it missed the fast path and hitDouble.compare(NaN, NaN) == 0). The numeric branch now returnsXSDFuncOp.effectiveBooleanValue(nv), which is exactly the XPath cast rule quoted above the method: zero, -0.0 and NaN are false, every other number true.GH-4235 (
ce7b01c6)compareNumericusesDouble.compare/Float.compare, where NaN is greater than everything, soFILTER(?v > 1e0)accepted NaN. XPath numeric comparison defines every ordering comparison involving NaN as false,=false and!=true. InNVCompare:compareByValue(behindNodeValue.compare:<,<=,>,>=) returnsCMP_INDETERMINATEfor a numeric comparison involving NaN, so the operator raisesExprNotComparableExceptionand the filter is an error. The same-term fast path no longer returnsCMP_EQUALfor NaN (NaN >= NaNmust not be true).sameValueAsis false whenever either side is NaN, so"NaN"^^xsd:double = "NaN"^^xsd:floatis false and!=true (already the case for two NaN terms of the same datatype viasameExceptNaN, which now shares anisNaNhelper).compareWithOrdering(compareAlways, ORDER BY) keeps NaN's place after every other number exactly as before, rather than falling through to term ordering.The two are coupled: making
sameValueAsstrict about NaN is what exposes the accidental float-NaN cast result, so the cast fix has to land with or before the comparison fix.Tests:
TestCastXSD.cast_to_boolean_06a/06b/06c;TestExpressions2.nan_cmp_01..12;TestNodeValue.testCompareNaN_value/testCompareNaN_ordering. Nine of them fail onmain. The fulljena-arqmodule passes (16629 tests; the single error isTestLangJsonLD_prefixesfetching a remote context, unrelated).By submitting this pull request, I acknowledge that I am making a contribution to the Apache Software Foundation under the terms and conditions of the Contributor's Agreement.
🤖 Generated with Claude Code