Skip to content

GH-4235, GH-4239: NaN in numeric comparison and in the xsd:boolean cast - #4246

Open
anishmehta24 wants to merge 2 commits into
apache:mainfrom
anishmehta24:gh-4235-nan-comparison
Open

anishmehta24 wants to merge 2 commits into
apache:mainfrom
anishmehta24:gh-4235-nan-comparison

Conversation

@anishmehta24

@anishmehta24 anishmehta24 commented Sep 20, 2026

Copy link
Copy Markdown

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.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. The float case only passed by accident ("NaN"^^xsd:float is a different term, so it missed the fast path and hit Double.compare(NaN, NaN) == 0). The numeric branch now returns XSDFuncOp.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) compareNumeric uses Double.compare/Float.compare, where NaN is greater than everything, so FILTER(?v > 1e0) accepted NaN. XPath numeric comparison defines every ordering comparison involving NaN as false, = false and != true. In NVCompare:

  • compareByValue (behind NodeValue.compare: <, <=, >, >=) returns CMP_INDETERMINATE for a numeric comparison involving NaN, so the operator raises ExprNotComparableException and the filter is an error. The same-term fast path no longer returns CMP_EQUAL for NaN (NaN >= NaN must not be true).
  • sameValueAs is false whenever either side is NaN, so "NaN"^^xsd:double = "NaN"^^xsd:float is false and != true (already the case for two NaN terms of the same datatype via sameExceptNaN, which now shares an isNaN helper).
  • 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 sameValueAs strict 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 on main. The full jena-arq module passes (16629 tests; the single error is TestLangJsonLD_prefixes fetching a remote context, unrelated).


  • Tests are included.
  • Documentation change and updates are provided for the Apache Jena website (not needed)
  • Commits have been squashed to remove intermediate development commit messages (one commit per issue).
  • Key commit messages start with the issue number (GH-xxxx)

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

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.
@anishmehta24 anishmehta24 changed the title GH-4235: A numeric comparison involving NaN is not true GH-4235, GH-4239: NaN in numeric comparison and in the xsd:boolean cast Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FILTER(?v > 1e0) accepts "NaN"^^xsd:double (numeric > comparison with NaN returns true)

1 participant