Repository navigation
fix(query): a stored JSON value is the term of its "…"^^rdf:JSON constant - #2430
Conversation
…tant Main has been red since 88c3cf1: #2227 and #2214 merged two minutes apart, each green on its own CI, and together two of #2227's tests fail on the rdf:JSON case (it_ill_typed_literals:: a_valid_typed_literal_is_one_term_on_every_write_path and bulk_import_keeps_an_ill_typed_literal). #2214 made a stored JSON value an rdf:JSON literal and lowers a SPARQL typed literal in an expression as STRDT, so `"[1,2]"^^rdf:JSON` in a FILTER is a string typed rdf:JSON. sameTerm and the term-identity step of `=` compared the two by representation, so the same term (one lexical form, one datatype) no longer matched itself. same_json_term treats a JSON value and a string typed rdf:JSON with the same text as one term, in sameTerm and in rdf_term_equal. Lowering, storage and writes are unchanged, and the comparison stays lexical: `"[1, 2]"^^rdf:JSON` is not the stored `[1,2]`, and a plain string is not a JSON value.
bplatz
left a comment
There was a problem hiding this comment.
Approving. Please review the inline comments before merging.
| let plain = Expression::Const(FlakeValue::String("[1,2]".into())); | ||
| for (other, same) in [ | ||
| (typed("[1,2]"), true), | ||
| (typed("[1, 2]"), false), |
There was a problem hiding this comment.
"[1, 2]"^^rdf:JSON should match the stored [1,2]: every write path stores JSON canonicalized, and datatypes.md says a FILTER literal is canonicalized before it is compared. Canonicalize a typed rdf:JSON constant once at lowering: an rdf:JSON arm in lower_typed_literal through coerce_string_value, falling back to the string for invalid JSON as the WKT arm does, and FlakeValue::Json added to lower_filter_value's pass-through list. The constant is then the stored term by plain equality, = stops re-parsing the STRDT string on every row (coerce_numeric_operand runs canonicalize_json on it today), and this case should expect true.
There was a problem hiding this comment.
Agreed, and thanks for pointing at datatypes.md:272 -- I'd been reading "keep it lexical" as the conservative choice here, but it was actually contradicting what the docs already promise. Done in 6d024b6f9: lower_typed_literal has an rdf:JSON arm through coerce_string_value (falling back to the string for text that isn't JSON, same as the WKT arm), and lower_filter_value passes FlakeValue::Json through. So "[1, 2]"^^rdf:JSON lowers once to Const(Json("[1,2]")), it's the stored term by plain equality, and = no longer goes through coerce_numeric_operand's canonicalize_json on every row. a_json_constant_lowers_to_its_canonical_value in fluree-db-sparql/src/lower/mod.rs pins the lowering.
This test case expects true now. It builds its constant with STRDT directly, though, so it's the eval_str_dt change from your other comment (537bce525) that flips it, not the lowering.
One thing this turned up that I hadn't realized: since the arm sits in lower_typed_literal, it also covers the triple-pattern object and the VALUES row, and on main both ?s ex:p "[1,2]"^^rdf:JSON and VALUES ?v { "[1,2]"^^rdf:JSON } matched nothing, canonical spelling included (a string typed rdf:JSON against a stored JSON value). They lower the way JSON-LD's where and values already did (coerce_value_by_datatype) now, and every_spelling_of_a_json_constant_names_the_stored_value (it_json_canonicalization.rs) pins both spellings in all three positions, before and after the first index.
| /// ([`json_literal`]); a typed constant or `STRDT` carries it as a string | ||
| /// typed `rdf:JSON`. Derived equality tells the two apart; term identity | ||
| /// must not. | ||
| pub(crate) fn same_json_term(a: &ComparableValue, b: &ComparableValue) -> bool { |
There was a problem hiding this comment.
Optional: this bridges the two representations for sameTerm and = only. Binding-level identity (object_binding::same_term, join and DISTINCT keys) has no bridge, and to_binding keeps a STRDT result as FlakeValue::String, so BIND(STRDT("[1,2]", rdf:JSON) AS ?v) and the stored [1,2] are still two terms there. Once typed constants lower to Json (see the comment on the test), only an explicit STRDT/str-dt needs this helper; minting canonical Json for a valid rdf:JSON string in to_binding would close the gap for bindings too, or it can be a follow-up issue.
There was a problem hiding this comment.
Folded in here (537bce525), though I put it one step earlier than to_binding: eval_str_dt now builds a valid rdf:JSON literal as its canonical FlakeValue::Json, through the same coerce_string_value the lowering uses. My worry with doing it only in to_binding was that the inline and the bound form of one STRDT would disagree -- STR(STRDT("[1, 2]", rdf:JSON)) would read "[1, 2]" while STR(?v) after the BIND read "[1,2]". Built in STRDT, it's one term everywhere: sameTerm and = see it by plain equality, and to_binding mints Lit { Json, rdf:JSON } with no special case, so object_binding::same_term, the join key and the DISTINCT key all match the stored value. That also answers the explicit-STRDT question the way the docs do: STRDT("[1, 2]", rdf:JSON) is the stored [1,2], same as the typed constant. STR of one now reads canonical, which I've added to datatypes.md next to the other consequences of canonical form.
The helper stays, but for a narrower case than I wrote it for: text that isn't JSON. An import keeps "not json"^^rdf:JSON as a FlakeValue::Json, while the constant or STRDT carries that text as a string, so same_json_term is still what keeps sameTerm(?v, "not json"^^rdf:JSON) true against it. I've rewritten its doc comment to say that and added the row to the unit test.
every_spelling_of_a_json_constant_names_the_stored_value pins the join (BIND(STRDT(…) AS ?v) ?s ex:items ?v), the DISTINCT (a UNION of the stored and the bound value counts 1) and the JSON-LD bind twin, in both spellings, before and after the first index. #2214's pins (it_string_filter_pushdown, it_typed_filter_pushdown, it_literal_identity) all pass unchanged.
One cost note, fwiw: an explicit STRDT/str-dt still canonicalizes once per row. That's the parse = was already paying in coerce_numeric_operand, it just happens in STRDT now, but it does mean a constant (str-dt "[1,2]" rdf:JSON) on the JSON-LD side isn't lowered once the way the SPARQL literal now is. I don't think it's hot-path for JSON values, but folding a constant str-dt at JSON-LD lowering would be small if you'd rather have it here.
| /// (which lowers to `STRDT`) are one term, for `sameTerm` and `=`; other | ||
| /// text, and a plain string, are not. | ||
| #[test] | ||
| fn a_stored_json_value_is_the_term_of_its_typed_constant() { |
There was a problem hiding this comment.
Please add a JSON-LD twin: #2227's integration tests in it_ill_typed_literals check this through SPARQL only. A JSON-LD filter writes the constant as (str-dt "[1,2]" rdf:JSON), so (sameTerm ?v (str-dt "[1,2]" rdf:JSON)) and the = form against the stored value pin the same answer on that surface.
There was a problem hiding this comment.
Good call, added in 0aad544ca. For the rdf:JSON case, a_valid_typed_literal_is_one_term_on_every_write_path now also counts (sameTerm ?v (str-dt "…" rdf:JSON)) and (= ?v (str-dt "…" rdf:JSON)) over all ten write paths, default and named graphs as the SPARQL query does, with SPARQL = alongside, and each in both "[1,2]" and "[1, 2]". The JSON-LD filter and bind forms are in every_spelling_of_a_json_constant_names_the_stored_value (it_json_canonicalization.rs) too, before and after the first index.
To check the twin pins something the SPARQL check doesn't: with the lowering fix in and the STRDT change and the helper reverted, SPARQL sameTerm on "[1,2]" still passes (the constant is a JSON value now), and the JSON-LD sameTerm fails on that same spelling.
I kept the twin to rdf:JSON on purpose. I tried it for all three of #2227's cases first, and the XSD ones don't hold on the str-dt surface: (sameTerm ?v (str-dt "42" xsd:integer)) counts 0 of the ten (only = coerces it), and (= ?v (str-dt "2026-10-28T12:00:00Z" xsd:dateTime)) counts 0 too, because STRDT builds those as strings typed with the datatype. This PR didn't introduce that, and fixing it means STRDT building the value for every datatype a write coerces, which changes what something like STR(STRDT("01", xsd:integer)) reads, so I didn't want to slip it in here without agreeing on it first.
SPARQL lowered `"…"^^rdf:JSON` to a string typed rdf:JSON: STRDT in an
expression, and a string object in a triple pattern or a VALUES row. Every
write path stores JSON canonical, and the datatypes doc says a literal in a
where, a delete or a FILTER is canonicalized before it is compared. On main:
- `?s ex:p "[1,2]"^^rdf:JSON` and `VALUES ?v { "[1,2]"^^rdf:JSON }` found
nothing, in either spelling;
- `"[1, 2]"^^rdf:JSON` in a FILTER was not the stored `[1,2]`;
- `=` parsed the constant again on every row (coerce_numeric_operand
canonicalizes a string typed rdf:JSON to try it as a number).
lower_typed_literal now coerces an rdf:JSON literal as a write does
(coerce_string_value), and falls back to the string for text that is not
JSON, as the WKT arm does. lower_filter_value passes the JSON value
through. The constant is now the stored term by plain equality in all
three positions, which is how JSON-LD's where and values already lower it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
STRDT kept its text as a string typed rdf:JSON. A JSON literal built at
run time was therefore a different term from the stored value wherever
bindings are compared: `BIND(STRDT("[1,2]", rdf:JSON) AS ?v) ?s ex:p ?v`
joined nothing, and a DISTINCT over the stored value and the bound one
counted two. same_json_term bridged sameTerm and `=` only, and only for
the canonical spelling.
eval_str_dt now builds a valid rdf:JSON literal as its canonical JSON
value, the term a write of it stores, as SPARQL's typed constant now
lowers. sameTerm, `=`, a join, DISTINCT and STR all see that one term.
`STRDT("[1, 2]", rdf:JSON)` and JSON-LD's `(str-dt "[1, 2]" rdf:JSON)`
are the stored `[1,2]`, and STR of either reads `"[1,2]"`, as STR of the
stored value does. Text that is not JSON stays a string, which a
transaction still refuses.
same_json_term is left with one case: text that is not JSON, which an
import keeps as a JSON value while a constant carries it as a string. Its
doc comment says so, and the unit test gains that row and the
non-canonical spelling, which is now the same term.
it_json_canonicalization pins every form on both surfaces, in both
spellings, before and after the first index: SPARQL's typed literal and
STRDT and JSON-LD's str-dt under sameTerm and `=`, a triple-pattern
object, a VALUES row, a BIND into a join and into a DISTINCT, and STR.
The datatypes doc lists it with the other consequences of canonical form.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
a_valid_typed_literal_is_one_term_on_every_write_path checked the rdf:JSON case through SPARQL's sameTerm only. A JSON-LD filter writes the constant with str-dt. For rdf:JSON the test now also counts `(sameTerm ?v (str-dt "…" rdf:JSON))` and `(= ?v (str-dt "…" rdf:JSON))`, and SPARQL's `=`, over every path's write, in the spellings "[1,2]" and "[1, 2]". Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks @bplatz -- all three are folded in here rather than left for later: the typed constant lowers to its canonical value ( |
|
Filed the XSD side of this as #2450, so it doesn't get lost: |
Main has been red since
88c3cf134. #2227 (b38268f33, 16:40Z) and #2214 (88c3cf134, 16:42Z) merged two minutes apart. Each was green on its own CI, neither ran against the other, and there was no textual conflict. Together, two of #2227's tests fail, both on therdf:JSONcase:it_ill_typed_literals::a_valid_typed_literal_is_one_term_on_every_write_path:rdf:JSON: every path, one term,[[0,0]]instead of[[10,1]]it_ill_typed_literals::bulk_import_keeps_an_ill_typed_literal:rdf:JSON: the valid literal, as its value,falseinstead oftrueThe failure also turns up on every open PR's
testjob, since CI tests each PR merged with main.Cause
Each PR is right on its own terms:
"[1,2]"^^rdf:JSONas a JSON value on every write path, and checks it withsameTerm(?v, "[1,2]"^^rdf:JSON).rdf:JSONliteral (21215f0cd). It also lowers a SPARQL typed literal in an expression asSTRDT, as reviewed there, so the constant is a string typedrdf:JSON.sameTermand the term-identity step of=then compared aJsonvalue against aStringtypedrdf:JSONby representation, so one RDF term no longer matched itself.The same split ran wider than those two tests. A SPARQL
rdf:JSONliteral lowered to a string everywhere, and so didSTRDT(…, rdf:JSON), while every write path stores the canonical JSON value. On main:?s ex:p "[1,2]"^^rdf:JSONandVALUES ?v { "[1,2]"^^rdf:JSON }match nothing, canonical spelling included;BIND(STRDT("[1,2]", rdf:JSON) AS ?v) ?s ex:p ?vjoins nothing, and aDISTINCTover the stored value and the bound one counts two;"[1, 2]"^^rdf:JSONis not the stored[1,2], althoughdocs/concepts/datatypes.mdsays a literal in awhere, adeleteor aFILTERis canonicalized before it is compared.Fix
A JSON literal a query names or builds is now the canonical JSON value a write stores, so it is the stored term by plain equality:
lower_typed_literal(fluree-db-sparql/src/lower/term.rs) coerces"…"^^rdf:JSONas a write does, once, at lowering, andlower_filter_valuepasses the JSON value through. That covers aFILTERorBINDconstant, a triple-pattern object and aVALUESrow, which JSON-LD'swhereandvaluesalready lowered this way.=no longer parses the constant again on every row.STRDT(…, rdf:JSON)and JSON-LD'sstr-dt.eval_str_dt(fluree-db-query/src/eval/string.rs) builds the canonical JSON value, so a bound one joins the stored value andDISTINCTcounts the two once.rdf:JSONin both, which a transaction still refuses.same_json_term(eval/value.rs), whichsameTermand=use, now covers only that text, which an import keeps as a JSON value.What a query sees:
"[1,2]"^^rdf:JSON,"[1, 2]"^^rdf:JSON,STRDT("[1, 2]", rdf:JSON)and(str-dt "[1, 2]" rdf:JSON)are each the stored[1,2], on both surfaces, undersameTermand=, in a pattern, aVALUESrow and aBIND.STR(STRDT("[1, 2]", rdf:JSON))is"[1,2]", asSTRof the stored value is. Before this it was the text as written.datatypes.mdlists this with the other consequences of canonical form."[1,2]"is not a JSON value:sameTermis false and=is a type error.Evidence
origin/mainand pass with this.a_json_constant_lowers_to_its_canonical_value(fluree-db-sparql/src/lower/mod.rs): the lowering, in aFILTER, a triple pattern and aVALUESrow, and the string fallback.a_stored_json_value_is_the_term_of_its_typed_constant(eval/rdf.rs):sameTermand=for both spellings, another value, a plain string, and text that is not JSON.every_spelling_of_a_json_constant_names_the_stored_value(it_json_canonicalization.rs): every form above, both spellings, both surfaces, before and after the first index.a_valid_typed_literal_is_one_term_on_every_write_path(it_ill_typed_literals.rs) now has a JSON-LD twin forrdf:JSON:(sameTerm ?v (str-dt …))and(= ?v (str-dt …)), with SPARQL=alongside, over all ten write paths, in both spellings.fluree-db-query(1,903 tests) andfluree-db-sparql(700) suites; influree-db-api,grp_transact,grp_queryandgrp_query_sparqlin full, plusit_json_canonicalization,it_typed_literal_index,it_literal_identity,it_typed_filter_pushdownandit_string_filter_pushdown(1,589 tests).A note on #2214's description
While checking this, I found one line in #2214's change list no longer describes main: "a bound
"…"^^rdf:JSONis refused on insert". Since #2227, a bound valid JSON constant inserts as its canonical value, because #2227's write path parses it. A bound'not json'is refused. This PR keeps both.🤖 Generated with Claude Code