Repository navigation
Conversation
ReviewThis appears to break existing tests, widens the behaviour change to durations without mentioning anything about it, and leaves the whitespace in the lexical form of the result term. Were the unit tests run before making the PR? I see failures on my end. The tests sort of imply that rejecting surrounding whitespace was deliberate behaviour rather than an accident, so the PR should state the case for changing it and update or delete those tests, rather than leaving the contradiction for CI to find. Scope is wider than the titleThe removed clause also matched The result term keeps the whitespace
Collapsing XML whitespace ( Minor
|
Remove leading and trailing XML whitespace (#x9, #xA, #xD, #x20) before
casting a string to a numeric, boolean or duration datatype, so the
result has the canonical lexical form ("1"^^xsd:integer, not
" 1 "^^xsd:integer).
Update TestExpressions2 xsd_cast_10..13, which asserted the old
rejection, add duration coverage, and drop the query-level test.
|
Thanks for the careful review, you're right on all of it. I hadn't run the existing TestExpressions2 cases before opening this, and that's on me. Pushed 6b3cd59:
Ran |
| // Cast by lexical form with checking. | ||
| private static NodeValue castByLex(String lex, XSDDatatype castType) { | ||
| if ( isWhitespaceCollapsed(castType) ) | ||
| // These datatypes have the whitespace facet "collapse" so leading |
There was a problem hiding this comment.
Maybe simply the comment,
// Remove empty white space around values
There was a problem hiding this comment.
Done in b4eda4b, the comment in castByLex now just reads "// Remove empty white space around values". Thanks for the suggestion.
| throw exception("Can not cast '"+lex+"' to a "+castType); | ||
| } | ||
|
|
||
| private static boolean isWhitespaceCollapsed(XSDDatatype castType) { |
There was a problem hiding this comment.
From Andy's comment, it sounds like there's already a method in Jena to perform the white space removal. Is it possible to use that instead of this new method and isXMLWhitespace?
There was a problem hiding this comment.
Good call, switched to XML10Char.trim in d3b9b3d and removed trimXMLWhitespace and isXMLWhitespace from CastXSD. TestCastXSD and TestExpressions2 still pass.
Replace the private trimXMLWhitespace and isXMLWhitespace helpers in CastXSD with the existing XML10Char.trim, which trims the XML 1.0 S production characters.
|
The code looks fine to me at this point. Here are some results from comparing URI patterns that are affected by this PR. In the table below,
|
|
Thanks for putting this comparison together, it lines up with what the PR is meant to change. The rows that match main are out of scope on purpose: date and time casts go through |
Background
It addresses the behavior reported in issue #4298.
Summary of the fix
CastXSD.castByLexused to reject a string with leading or trailing whitespace when casting to a type whose whiteSpace facet iscollapse. Soxsd:integer(' 1')raised an error, whilexsd:date(' 2026-01-01 ')already worked because date and time casts go throughXSDFuncOp.dateTimeCast.XPath and XQuery Functions and Operators 3.1, section 19.2 (casting from xs:string and xs:untypedAtomic), says the input string is first processed according to the whiteSpace facet of the target type. The numeric types,
xsd:booleanand the duration types all usecollapse, so surrounding whitespace should be stripped before the lexical form is checked.With this change
castByLexstrips leading and trailing XML whitespace (#x9, #xA, #xD, #x20) before it validates and builds the term, for the same set of datatypes the old check covered. The result is built from the stripped string, soxsd:integer(' 1 ')gives"1"^^xsd:integerandstr()of it is"1". Whitespace inside the value is still an error, for examplexsd:integer('1 2').Behavior change for durations:
xsd:duration,xsd:dayTimeDurationandxsd:yearMonthDurationextendXSDAbstractDateTimeTypeand are cast throughcastByLex, so they pick up the same rule.xsd:duration(' P1D ')used to be an error and now casts to"P1D"^^xsd:duration.' P 1D 'still fails.Date and time casts other than durations are unchanged by this PR.
Verification
Tests for the jena CastXSD change live in
jena-arq/src/test/java/org/apache/jena/sparql/expr/TestCastXSD.java, includingcast_duration_whitespacefor the three duration types.xsd_cast_10toxsd_cast_13inTestExpressions2used to assert that surrounding whitespace is rejected; they now assert that the cast succeeds, andxsd_cast_14checks that'1 2'still fails. The tests injena-arq/src/test/java/org/apache/jena/sparql/expr/TestCastXSD.javafail without the fix in the jena CastXSD change (checked by reverting the source changes).mvn -pl jena-arq -am -Dtest=TestCastXSD,TestExpressions2 testpasses, as does a wider run overorg.apache.jena.sparql.expr.Test*,org.apache.jena.sparql.function.Test*,Scripts_ARQandScripts_SPARQL.Fixes #4298
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.