Skip to content

fix(query): a stored JSON value is the term of its "…"^^rdf:JSON constant - #2430

Merged
aaj3f merged 4 commits into
mainfrom
fix/same-term-json-typed-constant
Nov 17, 2026
Merged

aaj3f merged 4 commits into
mainfrom
fix/same-term-json-typed-constant

Conversation

@aaj3f

@aaj3f aaj3f commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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 the rdf:JSON case:

  • 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, false instead of true

The failure also turns up on every open PR's test job, since CI tests each PR merged with main.

Cause

Each PR is right on its own terms:

sameTerm and the term-identity step of = then compared a Json value against a String typed rdf:JSON by representation, so one RDF term no longer matched itself.

The same split ran wider than those two tests. A SPARQL rdf:JSON literal lowered to a string everywhere, and so did STRDT(…, rdf:JSON), while every write path stores the canonical JSON value. On main:

  • ?s ex:p "[1,2]"^^rdf:JSON and VALUES ?v { "[1,2]"^^rdf:JSON } match nothing, canonical spelling included;
  • BIND(STRDT("[1,2]", rdf:JSON) AS ?v) ?s ex:p ?v joins nothing, and a DISTINCT over the stored value and the bound one counts two;
  • "[1, 2]"^^rdf:JSON is not the stored [1,2], although docs/concepts/datatypes.md says a literal in a where, a delete or a FILTER is 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:

  • SPARQL's typed constant. lower_typed_literal (fluree-db-sparql/src/lower/term.rs) coerces "…"^^rdf:JSON as a write does, once, at lowering, and lower_filter_value passes the JSON value through. That covers a FILTER or BIND constant, a triple-pattern object and a VALUES row, which JSON-LD's where and values already lowered this way. = no longer parses the constant again on every row.
  • STRDT(…, rdf:JSON) and JSON-LD's str-dt. eval_str_dt (fluree-db-query/src/eval/string.rs) builds the canonical JSON value, so a bound one joins the stored value and DISTINCT counts the two once.
  • Text that is not JSON stays a string typed rdf:JSON in both, which a transaction still refuses. same_json_term (eval/value.rs), which sameTerm and = 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, under sameTerm and =, in a pattern, a VALUES row and a BIND.
  • STR(STRDT("[1, 2]", rdf:JSON)) is "[1,2]", as STR of the stored value is. Before this it was the text as written. datatypes.md lists this with the other consequences of canonical form.
  • A plain "[1,2]" is not a JSON value: sameTerm is false and = is a type error.

Evidence

  • fix(transact): every transaction refuses an ill-typed literal; bulk import keeps one #2227's two tests fail on origin/main and pass with this.
  • a_json_constant_lowers_to_its_canonical_value (fluree-db-sparql/src/lower/mod.rs): the lowering, in a FILTER, a triple pattern and a VALUES row, and the string fallback.
  • a_stored_json_value_is_the_term_of_its_typed_constant (eval/rdf.rs): sameTerm and = 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 for rdf:JSON: (sameTerm ?v (str-dt …)) and (= ?v (str-dt …)), with SPARQL = alongside, over all ten write paths, in both spellings.
  • Each new test fails with its fix reverted.
  • Locally: the full fluree-db-query (1,903 tests) and fluree-db-sparql (700) suites; in fluree-db-api, grp_transact, grp_query and grp_query_sparql in full, plus it_json_canonicalization, it_typed_literal_index, it_literal_identity, it_typed_filter_pushdown and it_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:JSON is 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

…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.
@aaj3f
aaj3f requested a review from bplatz November 17, 2026 20:55

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Please review the inline comments before merging.

Comment thread fluree-db-query/src/eval/rdf.rs Outdated
let plain = Expression::Const(FlakeValue::String("[1,2]".into()));
for (other, same) in [
(typed("[1,2]"), true),
(typed("[1, 2]"), false),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"[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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

aaj3f and others added 3 commits November 17, 2026 17:55
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]>
@aaj3f

aaj3f commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @bplatz -- all three are folded in here rather than left for later: the typed constant lowers to its canonical value (6d024b6f9), STRDT/str-dt builds that value too so a bound one joins and DISTINCTs as the stored term (537bce525), and the JSON-LD twin (0aad544ca). The second went a step past your suggestion (into STRDT itself rather than to_binding), and the reasoning is in that thread. The lowering fix also turned up that a SPARQL triple pattern or VALUES row with an rdf:JSON literal matched nothing on main, canonical spelling included, so that's covered now too. I've updated the PR body to describe the final shape.

@aaj3f
aaj3f merged commit 1374785 into main Nov 17, 2026
21 checks passed
@aaj3f
aaj3f deleted the fix/same-term-json-typed-constant branch November 17, 2026 23:12
@aaj3f

aaj3f commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Filed the XSD side of this as #2450, so it doesn't get lost: STRDT/str-dt still build an XSD-typed string rather than the value, so sameTerm misses a stored integer or dateTime. Left out of this PR on purpose, since fixing it changes what STR of the result reads.

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.

2 participants