Skip to content

xsd:boolean(...) cast of "NaN"^^xsd:double returns true #4239

Description

@ting668

Version

6.2.0

What happened?

Reporter: ting668

Environment

  • Apache Jena Version: 6.2.0
  • Java Version: 21.0.12+8-LTS

Description

While testing Apache Jena using an intuitive approach inspired by attribute constraints, I found that the numeric-to-boolean constructor returns true for a NaN double: xsd:boolean("NaN"^^xsd:double) evaluates to true, so a FILTER on the cast accepts a row it should reject.

Under the numeric-to-boolean cast, NaN and zero are false and non-zero values are true: 1.0 (as xsd:double or xsd:float) is true, while "NaN"^^xsd:double and "0.0"^^xsd:double are false.

How to Reproduce and Expected Behavior

Note: The queries below are a minimized, simplified example reproducing the bug.

Buggy query (with the default graph loaded with the N-Triples data below):

SELECT ?s WHERE { ?s <urn:pub:p> ?v . FILTER(<http://www.w3.org/2001/XMLSchema#boolean>(?v)) } ORDER BY ?s

Data (default graph, N-Triples):

<urn:pub:nanDouble> <urn:pub:p> "NaN"^^<http://www.w3.org/2001/XMLSchema#double> .
<urn:pub:zeroDouble> <urn:pub:p> "0.0"^^<http://www.w3.org/2001/XMLSchema#double> .
<urn:pub:oneDouble> <urn:pub:p> "1.0"^^<http://www.w3.org/2001/XMLSchema#double> .
<urn:pub:nanFloat> <urn:pub:p> "NaN"^^<http://www.w3.org/2001/XMLSchema#float> .
<urn:pub:oneFloat> <urn:pub:p> "1.0"^^<http://www.w3.org/2001/XMLSchema#float> .
<urn:pub:background> <urn:pub:other> <urn:pub:value> .

Expected behavior: only the 1.0 values cast to true:

[{"s":"<urn:pub:oneDouble>"},{"s":"<urn:pub:oneFloat>"}]

Actual behavior: the row carrying "NaN"^^xsd:double is also returned:

[{"s":"<urn:pub:nanDouble>"},{"s":"<urn:pub:oneDouble>"},{"s":"<urn:pub:oneFloat>"}]

In the same run the "NaN"^^xsd:float value was not accepted; the wrong true result was observed for the xsd:double NaN.

This suggests that the boolean cast of an xsd:double does not apply the specified mapping of NaN to false, so a value that should be rejected passes the filter.

Relevant output and stacktrace

Are you interested in making a pull request?

None

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugconfirmedReport confirmed (project use only)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions