Skip to content

fix(transact): every transaction refuses an ill-typed literal; bulk import keeps one - #2227

Merged
aaj3f merged 21 commits into
mainfrom
fix/ill-typed-literal-writes
Nov 17, 2026
Merged

aaj3f merged 21 commits into
mainfrom
fix/ill-typed-literal-writes

Conversation

@aaj3f

@aaj3f aaj3f commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Every transaction refuses an ill-typed literal; bulk import keeps one

A literal whose lexical form isn't in its datatype's lexical space, like "landed"^^xsd:integer, was refused by some transactions and stored by others. INSERT DATA, the JSON-LD insert, template constants and VALUES parse a typed literal through fluree_db_core::coerce::coerce_string_value and refuse one that doesn't parse ("Cannot parse 'landed' as integer"). These didn't, and stored it as text under the declared datatype:

  • an update that builds the literal at run time: BIND(STRDT("landed", xsd:integer) AS ?v), STRDT(CONCAT(..), ..), JSON-LD str-dt;
  • an INSERT … WHERE that copies one a ledger already holds;
  • the RDF-text verbs: Turtle insert and upsert, the TriG insert fallback, graph insert, graph sync;
  • a JSON-LD value object typed @json whose string @value is not JSON ({"@value": "not json", "@type": "@json"}).

On the way out, an ill-typed xsd:integer then printed in JSON-LD as a bare string, which reads back as an xsd:string, while an ill-typed xsd:dateTime kept its @type.

Fluree stores typed values rather than lexical forms. That's an existing, documented divergence from RDF 1.1, where a literal is its lexical form plus datatype (docs/audit/burn-down/expression-semantics.md D6, decisions D-11 and D-12 in docs/audit/burn-down/ROADMAP.md). Under that model an ill-typed literal has no value to store, and the insert paths already said so. This PR makes every transaction agree with them, and leaves bulk import as the one lenient path.

Data-integrity fix: bulk import stored wrong rdf:JSON and xsd:duration values

This is a bug on main, in every release since 4.0.0, and it is fixed here (a182d90). A bulk import (fluree create --from, and fluree graph import) indexed each rdf:JSON and xsd:duration value under the wrong string-dictionary entry. A query read back some other text the ledger held: another statement's string, a file name, a hash, different from one import to the next. A JSON-LD or SPARQL result that included such a JSON value failed outright with "Invalid JSON in @JSON value". Ledgers built by transactions were not affected.

The cause: an import numbers strings per chunk and remaps them to the ledger's ids before it builds the index. remap_v2_record (fluree-db-indexer/.../runs/spool.rs) remapped only DecodeKind::StringDict keys. But rdf:JSON (JsonArena) and xsd:duration (Duration) also keep their text in the string dictionary (OType::is_string_keyed), so their keys stayed chunk-local. It now remaps every string-keyed o_type, which closes the class. A string id only reaches a record as LEX_ID or JSON_ID, and both resolve to string-keyed o_types. Every other id an import writes is either global already (predicates, datatypes, numbig and vector handles) or remapped separately (subjects, languages). The same function serves fluree graph import and the renumbering over an existing dictionary (relocalize_sorted_commit). The V1 remaps (the commit rebuild, the incremental resolver) were right.

Ledgers already built or extended by an import with rdf:JSON or xsd:duration values hold wrong index entries. Their commits hold the values as written, and fluree reindex rebuilds the index from the commits through the remap that was always right. So a reindex repairs them; no re-import is needed. I checked that with the bug in place: a bulk import plus a graph import read wrong, and after a reindex both read back as written. Reindex even if the ledger has been written to since. One thing a reindex can't undo: a value an update copied out of such a ledger before the repair (an INSERT … WHERE that bound it) was written with the wrong text.

imports_read_back_json_and_duration_values_as_written (grp_import) bulk-imports three chunks and graph-imports two more into the same ledger. It reads every string-keyed value back by query (strings, language strings, a custom datatype, JSON, durations), before and after a reindex, and it fails without the fix. The parity table that bplatz suggested (below) is what turned this up.

Which paths do what

Write path ill-typed literal
SPARQL INSERT DATA / DELETE DATA, JSON-LD insert, template constants, VALUES refused (unchanged)
JSON-LD value object typed @json (or rdf:JSON) whose string @value is not JSON refused (was stored as written)
SPARQL update with STRDT(..) or STRDT(CONCAT(..), ..), JSON-LD update with str-dt refused (was stored as text)
INSERT … WHERE copying one a ledger holds refused (was copied)
Turtle / TriG / N-Triples insert and upsert, the TriG insert fallback, graph insert, graph sync refused (was stored as text)
DELETE of one a ledger holds, through a bound variable removes it (unchanged)
Bulk import (fluree create --from, fluree graph import; Turtle and JSON-LD) kept as text under its datatype (unchanged, except that an out-of-range "300"^^xsd:byte is now text too; it was stored as the number)

This is a deliberate deviation from RDF 1.1. RDF 1.1 allows an ill-typed literal in a graph (it just denotes no value), so a Turtle document holding "xyz"^^xsd:integer is valid RDF, and a Turtle transaction will now refuse it anyway. That follows from Fluree storing typed values (D6, D-11 above): a transaction is the place a write is checked, and every transaction checks the same way. Bulk import is the lenient path, on purpose. It loads RDF from elsewhere in one pass, and that data often carries ill-typed literals. Failing a whole import on one would be the wrong trade, and a ledger that holds one reads it back with its datatype (below). Push, pull, clone and restore copy commits as they are. compatibility.md now names the deviation under RDF 1.1.

The W3C SPARQL suite is where this shows. Seven tests load data with ill-typed literals on purpose: open-eq-07 through open-eq-12 ("xyz"^^xsd:integer) and eq-bool ("yes"^^xsd:boolean). They exercise how queries compare such literals, and a transaction now refuses their data. The harness now runs every query and update evaluation test twice: once with its data loaded by transaction, so queries read novelty as they always did, and once by bulk import, so they read the index. The seven run over the import only. They're listed in ILL_TYPED_DATA, which is checked both ways like the skip registers. Running over the index is what found the langMatches index-lane bug fixed in #2226. With both loads, every group passes (sparql10_query_eval_tests: 258 passed, 0 failed).

STRDT to rdf:JSON or f:embeddingVector with an invalid form was already refused, but late, by the value/datatype pairing guard ("must pair only with …"). It's now refused up front with the same "Cannot parse '…' as rdf:JSON" error an insert gives, and a valid form is stored as its value. xsd:integer("landed") in a BIND is a type error that leaves the variable unbound, so it writes nothing, as before.

What changed

  • One parser. value_convert::typed_lexical is coerce_string_value with the JSON-LD insert's error (TransactError::Parse). Bulk import's lenient parse is now that parser falling back to the text, and the cross-ledger SHACL decoder (parse_xsd_lexical) delegates to it too. Before, both were hand-kept copies. They agreed on valid forms but drifted on two things: import stored an out-of-range "300"^^xsd:byte as the number, and the decoder read a WKT point and a vector as text. Now import keeps as text exactly what a transaction refuses.
  • Updates. Flake generation runs a literal bound at run time (a string value under a non-string datatype) through it, on assertions only. A valid form is stored as its value, the term INSERT DATA stores. An ill-typed one fails the write. A retraction keeps the value as bound, so a DELETE still removes an ill-typed literal a ledger already holds. Whether a datatype needs parsing at all is decided on its Sid (parses_lexical), so a datatype whose forms are text (xsd:anyURI, a custom one) skips decoding the datatype IRI. Flake generation for 200k bound literals × 21, release profile, instructions:u: a custom datatype 11.11e9 → 3.98e9 (−64%), xsd:anyURI 9.71e9 → 4.03e9 (−58%), and xsd:integer, which parses either way, 8.21e9 → 8.62e9 (+5%, wall time unchanged).
  • RDF text. convert_string_literal now takes a LiteralCheck from each caller, with no default. The transaction sinks (FlakeSink, TemplateSink) pass Strict and return the refusal. The import pipeline (ImportSink, the parallel import) passes Lenient. The choice is made where the write path is known.
  • JSON-LD @json. A string @value on a @json value object is read as a serialized document (documented in compatibility.md), so one that doesn't parse is refused by typed_lexical, as INSERT DATA and Turtle refuse "not json"^^rdf:JSON.
  • Import index fix. See the data-integrity section above.
  • Reading invalid rdf:JSON. format::datatype::{json_document, ill_typed_json_literal} hold the rule for an rdf:JSON value on a JSON output once: the document when the text is JSON, else the typed literal. The JSON-LD, typed JSON and crawl writers use it instead of failing (behavior change 3).
  • Output. The JSON-LD formatter, and the graph crawl's, dropped @type for an "inferable" datatype (xsd:integer, xsd:decimal, xsd:double, xsd:boolean, …) because the JSON number or boolean carries it. A string-backed value prints as a JSON string, so that only holds for xsd:string. datatype::may_print_bare checks the value too. An ill-typed literal a ledger holds, imported or written before this change, now prints as {"@value": "landed", "@type": "xsd:integer"}. The W3C result formats already kept the datatype.

Behavior changes

Each in its own commit with a test that pins it. The first two fall out of the one-parser change; the third follows from import keeping an rdf:JSON literal that isn't JSON:

  1. Bulk import keeps an out-of-range bounded integer as text (5d9f09f). Import used to store "300"^^xsd:byte as the number 300 under xsd:byte. It now keeps it as text, as it keeps every literal a transaction refuses, so it no longer equals a number. Pinned by the "300"^^xsd:byte row of a_transaction_and_an_import_store_one_value_per_literal. compatibility.md has an entry.
  2. The cross-ledger SHACL decoder reads a literal as the write paths store it (1232c8e). parse_xsd_lexical read a WKT point and a vector as text; it now reads them as the geo point and vector both write paths store. An out-of-range integer now fails closed (TranslationFailed), as every other ill-typed literal already did; it used to read as the number. Pinned by a_held_literal_decodes_as_the_write_paths_store_it.
  3. An rdf:JSON literal that isn't JSON reads back as typed text (d2ca74b). Import keeps one as written, but the JSON-LD-flavored outputs parsed every rdf:JSON value and failed the whole query on it ("Invalid JSON in @JSON value"). They now write it the way they write any other ill-typed literal: {"@value": "not json", "@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON"}, never @json. That covers JSON-LD, typed JSON and agent JSON selects (DOM and streamed), the graph crawl and its typed form, and a crawl of the literal's own @value/@type. SPARQL JSON/XML/CSV/TSV, CONSTRUCT and export already wrote "not json"^^rdf:JSON. A valid JSON literal prints exactly as before. Pinned by it_ill_typed_json_reads: an imported ledger holding one is read back through every surface. The invalid case fails on the fourteen JSON-LD-flavored surfaces without the change, and the valid case is pinned to today's output on all of them.

Breaking changes

A transaction that stored an ill-typed literal before now fails, with nothing committed: an update that builds one with STRDT/str-dt, an INSERT … WHERE that copies one, a Turtle, TriG or N-Triples transaction, graph insert or graph sync that carries one, and a JSON-LD @json value object whose string isn't JSON. A caller that needs to keep such data as written can load it by bulk import. A ledger that already holds one keeps it, reads it back with its datatype, and can delete it.

The two behavior changes above. For Rust embedders, fluree_db_transact::value_convert::parse_xsd_lexical now returns Result<FlakeValue, String> (it returned Result<Option<FlakeValue>, String>, with None for an unrecognized datatype, which is now its text).

Release note: Bulk import (fluree create --from, fluree graph import) stored wrong rdf:JSON and xsd:duration values in every release since 4.0.0: queries read back other text, or failed with "Invalid JSON in @JSON value". Fixed. Run fluree reindex on any ledger built or extended by an import that holds such values; it rebuilds the index from the commits, which hold the values as written. Also: every transaction now refuses a typed literal whose lexical form doesn't parse as its datatype ("landed"^^xsd:integer), with the error INSERT DATA and the JSON-LD insert already gave ("Cannot parse 'landed' as integer"). That covers SPARQL and JSON-LD updates that build one with STRDT/str-dt or copy one; Turtle, TriG and N-Triples insert, upsert, graph insert and graph sync, which used to store it as text; and a JSON-LD @json value whose string isn't JSON. Bulk import (fluree create --from, fluree graph import) still keeps such a literal as text under its datatype. In JSON-LD results, an ill-typed literal a ledger holds now carries its @type instead of printing as a plain string, and an rdf:JSON literal whose text isn't JSON reads back as that typed text instead of failing the query.

Tests

it_ill_typed_literals (in grp_transact), for xsd:integer, xsd:dateTime and rdf:JSON:

  • every_transaction_refuses_an_ill_typed_literal: ten paths each refuse with "Cannot parse '…' as …" and commit nothing. They are INSERT DATA, SPARQL STRDT and STRDT(CONCAT(..)), the JSON-LD insert, JSON-LD str-dt, Turtle insert and upsert, the TriG insert fallback, graph insert and graph sync.
  • bulk_import_keeps_an_ill_typed_literal: pinned, so a change to it is deliberate. It keeps the literal as text under its datatype (an rdf:JSON one as written) and stores the valid one beside it as its value.
  • a_valid_typed_literal_is_one_term_on_every_write_path: all ten paths store a valid form as one sameTerm term.
  • a_stored_ill_typed_literal_keeps_its_datatype: a ledger that holds one (carried in by a pushed commit, support::ledger_with_stored_ill_typed_literals) reads it back typed in SPARQL, a JSON-LD select and a JSON-LD crawl. An update can't copy it, and a DELETE removes it.
  • a_transaction_and_an_import_store_one_value_per_literal: the strict/lenient parity table. Valid forms of fifteen datatypes, among them boolean "1", decimal, double INF, float, a BigInt, dateTime, gYear, duration, dayTimeDuration, a WKT point, rdf:JSON, a vector, an in-range xsd:byte, xsd:anyURI and a custom datatype. Each is written by a Turtle transaction (novelty) and by an import (index), and the stored values are compared. Four forms a transaction refuses are each checked to be kept as text by the import.

imports_read_back_json_and_duration_values_as_written (grp_import): the import data-integrity fix, end to end (above).

it_ill_typed_json_reads (grp_transact, 1f2b4e0 for the byte-for-byte pin): an imported rdf:JSON literal, valid and not, read back through SELECT in every result format (DOM and streamed), the graph crawl (plain, typed, and of @value/@type), CONSTRUCT (JSON-LD, Turtle, N-Triples) and export (Turtle, N-Triples, JSON-LD).

Unit tests: a_json_literal_string_must_be_json (parse/jsonld.rs), remap_v2_record_remaps_every_string_keyed_o_type (spool.rs), a_held_literal_decodes_as_the_write_paths_store_it (value_convert.rs), and a_datatype_passed_over_by_its_sid_takes_any_text (generate/flakes.rs). The last pins that the Sid precheck only passes over datatypes coerce_string_value takes as text. Each new test was checked to fail with its fix reverted.

testsuite-sparql loads each test's data both ways (setup_graph_store, Load). Tests that staged an ill-typed literal through a Turtle insert now stage it through a pushed commit (it_typed_literal_index, it_xsd_double_special_values). The ones that pinned Turtle keeping one now pin the refusal, and the RDF-text parity fixture drops its ill-typed line. turtle.md, create.md and graph.md say which path is lenient; concepts/datatypes.md lists the paths that refuse; compatibility.md has the behavior-change entries.

Gates

On an EC2 c7i.8xlarge at d2ca74b3b (main 467f0da4d merged in at 37715147a): cargo fmt --check (both workspaces), clippy --all-features -D warnings, cargo check (default and server native), both wasm32 clippy lanes, and testsuite-sparql clippy are clean. cargo nextest run --workspace --all-features: 15,868 of 15,868 passed. Doctests pass. W3C: 36 of 36 manifests; sparql10 258 passed, 0 failed (24 on the register), sparql11 update 157 / 0, now under both loads. Not run here: workflow-lint, sql-bridge, python and wasm-smoke, which CI runs.

aaj3f added 9 commits October 28, 2026 22:16
…ushed commit

`support::ledger_with_stored_ill_typed_literals` creates a ledger holding
literals whose lexical form is not in their datatype's lexical space, as
text under the datatype: what a write stored before it refused them, or
what bulk import keeps. A placeholder per literal is committed on a sender
ledger, rewritten in the commit, and the line is pushed, so the test does
not depend on any write path still accepting one.
The JSON-LD formatter, and the graph crawl's, printed a literal bare when
its datatype was "inferable" (xsd:integer, xsd:decimal, xsd:double,
xsd:boolean, ...), because the JSON number or boolean carries the type. A
string-backed value prints as a JSON string, which reads back as
xsd:string, so an ill-typed literal a ledger holds ("landed"^^xsd:integer,
or the "inf" text an earlier bulk import stored under xsd:double) came out
as a plain string, while an ill-typed xsd:dateTime kept its @type.

`datatype::may_print_bare` checks the value too: a string-backed value
prints bare only under xsd:string. The W3C result formats already kept
the datatype. The special-doubles test reads the typed `inf` cell.
A literal whose lexical form is not in its datatype's lexical space
("landed"^^xsd:integer) was refused by INSERT DATA and the JSON-LD insert
and stored by other transactions, as text under the declared datatype: an
update that builds it at run time (STRDT, STRDT of CONCAT, JSON-LD
str-dt) or copies a stored one, and the RDF-text verbs (Turtle insert and
upsert, the TriG insert fallback, graph insert, graph sync). Every
transaction now refuses one with the insert's error, "Cannot parse
'landed' as integer".

One parser: `value_convert::typed_lexical` is core's
`coerce_string_value` with the JSON-LD insert's error.
- Flake generation runs a literal bound at run time through it, on
  assertions only, so a valid STRDT form is stored as its value and a
  DELETE still removes an ill-typed literal a ledger holds. A template
  constant was parsed where it was written.
- `convert_string_literal` takes a `LiteralCheck` from each caller. The
  transaction sinks (FlakeSink, TemplateSink) pass Strict and return the
  refusal. Bulk import (ImportSink and the parallel import) passes
  Lenient and keeps such a literal as text, the one lenient write path,
  so a dataset from elsewhere loads in one pass.

Tests that staged an ill-typed literal through a Turtle insert now stage
it through a pushed commit. The RDF-text parity fixture drops its
ill-typed line, and the tests that pinned Turtle keeping one now pin the
refusal. turtle.md and create.md say which path is lenient.
…ture

The wasm32 build has no bulk import, so no caller passes Lenient, and
clippy -D warnings refused the dead variant.
A transaction now refuses an ill-typed literal, and W3C test data carries
some on purpose: open-eq-07 through 12 load "xyz"^^xsd:integer and eq-bool
loads "yes"^^xsd:boolean, to test how queries compare them. Loading that
data by Turtle transaction failed all seven at load. The harness now
writes each data file as one TriG file (the default graph as written, a
named graph as a GRAPH block, directives hoisted) and loads them by bulk
import, the one lenient write path, so the seven keep running.

Import also leaves the data indexed, so the suite now reads the binary
index, which it never did before. That is how the LANG pre-filter bug
merged in from fix/lang-pushdown-operator came to light (dawg-langMatches-2).

The lock picks up tempfile, and smallvec, which main's fluree-db-query
already depends on.
@aaj3f aaj3f added bug Something isn't working as expected breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section area:transact Staging, commit, import/bulk-import, novelty, retraction semantics labels Oct 30, 2026
@aaj3f
aaj3f requested a review from bplatz October 30, 2026 19:11
…runs does

`fluree graph import` (#2232) fills a new graph of an existing ledger
through the bulk import pipeline, so it keeps a typed literal whose
lexical form does not fit its datatype as text under that datatype, as
`fluree create --from` does. `create.md` and `turtle.md` named
`create --from` as the one lenient path; they now name both, and the
graph import section of `graph.md` says it.
@bplatz

bplatz commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

From the #2232 review: fluree graph import is a second bulk path that keeps ill-typed literals, and whichever PR landed second would add it. #2232 has landed, so:

  • Merged main into this branch (a3a5609).
  • 210a5ab names fluree graph import beside fluree create --from as the lenient path in create.md and turtle.md, and says so in the graph-import section of graph.md. I checked it on this branch with main merged: a graph import keeps "landed"^^xsd:integer and "landed"^^xsd:dateTime as text under their datatypes.

The "Which paths do what" table in the description could take it in its bulk-import row: Bulk import (fluree create --from, fluree graph import; Turtle and JSON-LD).

@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; this looks right. Feedback inline, plus two docs gaps outside the diff:

  • concepts/datatypes.md ("Text That Is Not a Value of Its Datatype") still says Turtle transactions keep an ill-typed literal.
  • No compatibility.md behavior-change entry for this breaking change: the refusals, and JSON-LD output now carrying @type on a stored one.

- **Directives apply in document order.** A `@prefix` or `@base` applies to what follows it, in the default graph and in blocks alike; redefining a prefix later does not change the IRIs before it. A block label is resolved under the declarations in force where the block appears.
- **Relative IRIs resolve against the base.** `<x>` resolves against the `@base` in force. With no base, a relative reference in the default graph is a parse error, while one in a `GRAPH` block, as its label or in its contents, is kept exactly as written: ledger configuration names a ledger by its id, and an id such as `org/governance:main` is a relative reference (`f:ledger <org/governance:main>`). `<#txn-meta>` with no base in force names the commit-metadata block, as always.
- **Literals are stored as written.** A lexical form that does not fit its datatype (`"abc"^^xsd:integer`) is kept, with its declared datatype, on every endpoint, as insert always did.
- **A typed literal must parse as its datatype.** A lexical form that does not fit its datatype (`"abc"^^xsd:integer`) fails the transaction with "Cannot parse 'abc' as integer", on every endpoint, as it does in a JSON-LD insert or `INSERT DATA`. The import pipeline (`fluree create --from`, and `fluree graph import` into a new graph of an existing ledger) is the exception: it keeps such a literal as text under its declared datatype, so a dataset from elsewhere loads in one pass (see [`create`](../cli/create.md)). The upsert endpoint below is a transaction, so it refuses one.

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.

A JSON-LD insert still keeps one for rdf:JSON: {"@value": "not json", "@type": "@json"} (or the full IRI) commits as written (parse/jsonld.rs:2294, documented at datatypes.md:266), where INSERT DATA and a Turtle insert refuse it. Worth naming as the exception here and in the table, with a follow-up to align it. Under JSON-LD 1.1 a string @value under @json is a JSON string, so storing it as one may be the better fix than refusing it.

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 catch, that was the one write still keeping one, and the title says "every transaction", so I'd rather fix it than name it as the exception. I went with refusing it, not storing it as a JSON string, in 9d3be9d. {"@value": "not json", "@type": "@json"} (or the full rdf:JSON IRI) now goes through typed_lexical, as "not json"^^rdf:JSON does in INSERT DATA and Turtle, fails with "Cannot parse 'not json' as rdf:JSON", and commits nothing.

My reasoning is the deviation you wrote up in 5c98bd1: a string @value on a @json value object is a serialized document, so "[1,2]" is the array. Under that reading, storing a string that doesn't parse as a JSON string would make one form mean two things depending on its content ("5" is the number, "five" would be the string), and a typo in a serialized object would quietly become a string instead of an error. The text it stored before couldn't be read back either: a query returning it fails with "Invalid JSON in @JSON value". Moving every string @value to JSON-LD 1.1's reading would be cleaner in principle, but it changes what every valid string write stores, so I'd rather that be its own decision than ride on this PR. Happy to talk it through if you see it differently.

rdf:JSON is now a case in the three write-path tests of it_ill_typed_literals (every_transaction_refuses_an_ill_typed_literal covers it on all ten paths), and datatypes.md:266 no longer says it's stored as written. So no exception left to name in turtle.md or the table.

Bulk import still keeps an invalid one as written, as it keeps every other ill-typed literal. But a ledger holding one couldn't return it from a JSON-LD, typed or agent JSON query: the whole query failed with "Invalid JSON in @JSON value". So it now reads back the way any other ill-typed literal does, as {"@value": "not json", "@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON"} (not @json, since it isn't a JSON value), in d2ca74b. SPARQL results, CONSTRUCT and export already wrote "not json"^^rdf:JSON. it_ill_typed_json_reads reads an imported one back through every output, and pins a valid one to today's output.

LiteralCheck::Lenient => lenient_value(value, dt_iri),
// The language tag rides on the flake's metadata; the value is text.
LiteralCheck::Strict if dt_iri == rdf::LANG_STRING => FlakeValue::String(value.to_string()),
LiteralCheck::Strict => typed_lexical(value, dt_iri)?,

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.

Strict and lenient are now two parsers kept in step by hand; they agree on every valid form today. A table test through a Turtle transaction and an import (boolean "1", decimal, double INF, gYear, duration, a WKT point, rdf:JSON, a vector, an in-range xsd:byte, a custom datatype) would keep them from drifting. Or lenient_value could be coerce_string_value(..) falling back to the text, with rdf:JSON and vectors special-cased.

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 I did both. lenient_value is now coerce_string_value falling back to the text. An rdf:JSON form is kept as written; a non-vector falls to text, and the pairing guard refuses it as before. So the two can't drift: whatever a transaction stores, an import stores the same value, and only what a transaction refuses is kept as text. That's 5d9f09f. The cross-ledger SHACL decoder (parse_xsd_lexical) was a third hand-kept copy, so it delegates now too, in 1232c8e.

a_transaction_and_an_import_store_one_value_per_literal is the table. It has your rows plus float, a BigInt, dateTime, dayTimeDuration and anyURI. Each is written once by a Turtle transaction (so it's read from novelty) and once by an import (read from the index), and the stored values are compared. Four forms a transaction refuses are checked to be kept as text by the import.

Two things fell out of it:

  • The drift you were worried about was already there, in both copies, and each fix is its own commit with a test that pins it. Import stored "300"^^xsd:byte as the number 300, while datatypes.md said import keeps it as text; it's text now (5d9f09f, the table's "300"^^xsd:byte row, plus a compatibility entry). The decoder read a WKT point and a vector as text, where both write paths store a geo point and a vector, and it read an out-of-range integer as the number. It now reads them as stored and fails closed on the integer (1232c8e, a_held_literal_decodes_as_the_write_paths_store_it).
  • More importantly, the import side of the table failed for rdf:JSON and xsd:duration even on valid values, and differently from run to run. remap_v2_record (fluree-db-indexer/.../runs/spool.rs) only remapped DecodeKind::StringDict keys from chunk-local to global dictionary ids. But rdf:JSON and xsd:duration keep their text in the string dictionary too (OType::is_string_keyed), so an imported JSON value read back as whatever string held that id globally: a file name, a content hash. That one's on main, not from this PR, and it goes back to 4.0.0. It also hits fluree graph import, which shares the remap. Since the table can't pass without it, it's fixed here in a182d90, for every string-keyed o_type. I checked the other remap sites too: the V1 ones (commit rebuild, incremental resolver) were already right. There's a unit test plus imports_read_back_json_and_duration_values_as_written, which bulk-imports three chunks, graph-imports two more and reads every value back by query; it fails without the fix. A rebuild from commits was always right, so fluree reindex repairs a ledger an earlier release imported. I checked that with the bug in place, for both commands. The description has a section on it, and the release note leads with it, since anyone with imported JSON or durations will want to reindex.

// written (`value_convert` for RDF text).
if let (TemplateTerm::Var(_), FlakeValue::String(lexical)) = (&template.object, &o) {
if dt != *DT_STRING && dt != *DT_LANG_STRING {
if let Some(dt_iri) = self.decode_sid(&dt) {

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.

Nit: this decodes the datatype IRI, an allocation, per flake for every variable-bound string with a non-xsd:string datatype, such as an INSERT … WHERE copying anyURI or custom-typed values. A check on the Sid first would skip that.

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.

Done in 9e1bb61. parses_lexical decides on the Sid first: an XSD datatype outside the string family, rdf:JSON, geo:wktLiteral or the vector. Anything else (anyURI, a custom datatype) skips the decode. a_datatype_passed_over_by_its_sid_takes_any_text pins that any datatype it passes over is one coerce_string_value takes as text, so the check can't drift from the parser.

I measured it on a c7i.8xlarge in the release profile: flake generation for 200k variable-bound literals, 21 reps, instructions:u per process. Those counts held to within a few hundred between runs, so they're the number to trust here; the box was busy with gates, which made wall time noisy:

  • custom datatype: 11.11e9 → 3.98e9 instructions (−64%)
  • xsd:anyURI: 9.71e9 → 4.03e9 (−58%), about 188 → 115 ns/flake
  • xsd:integer, as a control: 8.21e9 → 8.62e9 (+5%), wall time unchanged

The control parses either way, so the only new work on it is a Sid check of a few instructions. My guess is the +5% is the optimizer laying out materialize_template differently, but I haven't confirmed that. It's also a rare path: a stored integer binds as a number and never gets here. Only a string built by STRDT/str-dt does, or a stored ill-typed one, which is refused anyway. So I don't think it outweighs the win on copies, but happy to dig further if you'd like.

}
match fluree
.create(TEST_LEDGER)
.import(dir.path())

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.

This moves the W3C suite from novelty only to index only. It found a real index-lane bug, but novelty coverage goes. Could it keep both: load by transaction and fall back to import for the seven tests a transaction refuses, or run the suite in both modes?

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 point, kept both, in 58ff5f9. Every query and update evaluation test now runs twice in its subprocess: once with its data loaded by transaction, so queries read novelty as they did before this PR, and once by import, so they read the index. A failure names the load. The seven ill-typed tests are listed in ILL_TYPED_DATA and run over the import only. The list is checked both ways, like the skip registers: a listed test whose data a transaction loads fails, and so does an unlisted test whose data a transaction refuses. With both loads, sparql10_query_eval_tests is 258 passed, 0 failed (the same 24 registered), sparql11_update_tests is 157/0, and all 36 W3C SPARQL manifests pass. It costs a few seconds of suite time.

To check the list is live, I swapped eq-bool for open-eq-02 in it. The suite then failed both ways, each naming the load: "[Transaction load] … open-eq-02 is listed in ILL_TYPED_DATA, but a transaction loaded its data", and for eq-bool, "[Transaction load] … Cannot parse 'yes' as xsd:boolean".

…lues

A bulk import writes each chunk's records with chunk-local dictionary ids
and remaps them to the ledger's ids before it builds the index.
`remap_v2_record` remapped only `DecodeKind::StringDict` keys, but an
`rdf:JSON` or `xsd:duration` value also keeps its text in the string
dictionary (`OType::is_string_keyed`), so its key stayed chunk-local. A
query read back whatever string held that id in the ledger (another
statement's text, a file name, a hash), different from one import to the
next, and a result holding such a JSON value failed with "Invalid JSON in
@JSON value". Every release since 4.0.0 has done this, for
`fluree create --from` and for `fluree graph import`, which shares the
remap, as does the remap that renumbers a sorted commit over an existing
dictionary (`relocalize_sorted_commit`).

`remap_v2_record` now remaps every string-keyed o_type. That is the whole
class: a string id only ever reaches a record as `LEX_ID` or `JSON_ID`,
and the o_type registry resolves both to string-keyed o_types (the string
family, language strings, a custom datatype, `rdf:JSON`, `xsd:duration`).
The other ids an import writes are global already (predicates, datatypes,
numbig and vector handles) or remapped apart (subjects, languages). The
V1 remaps (`remap_record`, the incremental resolver, the commit rebuild)
key on `JSON_ID` and `LEX_ID` and were right.

The commits always held the values as written, and `fluree reindex`
rebuilds through the V1 remap, so a reindex repairs a ledger an earlier
release imported; the compatibility note says so.
`imports_read_back_json_and_duration_values_as_written` bulk-imports three
chunks, graph-imports two more into the same ledger, and reads every
string-keyed value back by query, before and after a reindex; it fails
without the fix.
…he write

A string `@value` on a value object typed `@json` (or `rdf:JSON`) is read
as a serialized JSON document. One that did not parse was stored as
written, where `INSERT DATA` and a Turtle transaction refuse
`"not json"^^rdf:JSON`, and the text it left could not be read back: a
query returning it fails with "Invalid JSON in @JSON value". It is now
parsed by `typed_lexical`, as those transactions parse it, and refused
with their error ("Cannot parse 'not json' as rdf:JSON").

The ill-typed write-path tests take a full datatype IRI and gain an
`rdf:JSON` case, so every path is checked to refuse it, to store a valid
form as one term, and bulk import to keep it as written.
`it_literal_identity` wrote `{"@value": "s", "@type": "@JSON"}`, text that
is not JSON; it now writes the JSON string "s" as a serialized document.
…t it refuses as text

Bulk import's lenient parse was a hand-kept copy of
`coerce_string_value`, the parser every transaction uses. They agreed on
valid forms but not on every form: import stored an out-of-range
`"300"^^xsd:byte` as the number 300, where `datatypes.md` says import
keeps an ill-typed literal as text.

`lenient_value` is now `coerce_string_value` falling back to the text (an
`rdf:JSON` form as written), so the two cannot drift: import stores
everything a transaction stores as the same value, and keeps as text
exactly what a transaction refuses. The one behavior change is that
out-of-range value of a bounded integer type, now text under its
datatype, which no longer equals a number; the compatibility reference
gains an entry for it.

`a_transaction_and_an_import_store_one_value_per_literal` writes a table
of forms through a Turtle transaction (novelty) and an import (index) and
compares what each stored: valid forms of fifteen datatypes, and four
forms a transaction refuses and import keeps, among them
`"300"^^xsd:byte` as text.
The cross-ledger SHACL translator decodes a model ledger's literals with
`parse_xsd_lexical`, a third hand-kept copy of the lexical parser, whose
contract is to match what the same ledger stores. It did not for two
kinds: it read a `geo:wktLiteral` point and an `f:embeddingVector` as
text, where the write paths store a geo point and a vector, and it read an
out-of-range `"300"^^xsd:byte` as the number 300, where a transaction
refuses it and an import keeps it as text.

`parse_xsd_lexical` now delegates to `coerce_string_value` and returns the
value directly (`Result<FlakeValue, String>`, no longer an `Option` whose
`None` meant "store the text"). A point and a vector decode as stored, and
an out-of-range integer fails closed as every other ill-typed literal
already did. An `rdf:JSON` form that is not JSON still decodes as written,
as bulk import keeps it. `a_held_literal_decodes_as_the_write_paths_store_it`
pins each case.
…e index

Loading the W3C data by import moved the suite from novelty to the index:
it found an index-lane bug, but nothing read novelty any more. Each query
and update evaluation test now runs twice in its subprocess, its data
loaded once by transaction (queries read novelty, as before this branch)
and once by bulk import (queries read the index), and a failure names the
load.

The seven tests whose data carries an ill-typed literal on purpose
(`open-eq-07` to `open-eq-12`, `eq-bool`) are listed in `ILL_TYPED_DATA`
and run over the import only, since a transaction refuses their data. The
list is checked both ways, as the skip registers are: a listed test whose
data a transaction loads fails, and so does any other test whose data a
transaction refuses.
…ity notes

`concepts/datatypes.md` still said Turtle transactions keep an ill-typed
literal. It now lists the write paths that refuse one, and says bulk
import (`fluree create --from`, `fluree graph import`) keeps one as text,
an `rdf:JSON` one as written (which a query then cannot return), while an
invalid vector fails an import as it fails a transaction.

`compatibility.md` gains the behavior-change entry for the refusals and
for JSON-LD results carrying `@type` on a stored ill-typed literal, and
notes the deviation under RDF 1.1, which it listed as fully compliant.
Flake generation parses a string bound under a non-string datatype as its
datatype's lexical form, and decoded the datatype's IRI first: an
allocation, then a walk of `coerce_string_value`'s arms and a copy of the
text, per flake, even for a datatype whose forms are text (`xsd:anyURI`,
an application's own), such as an `INSERT ... WHERE` copying stored values.
`parses_lexical` decides on the Sid: an XSD datatype outside the string
family, `rdf:JSON`, `geo:wktLiteral` or `f:embeddingVector`. Other
datatypes skip the decode and the parse, which stored the text as is. A
unit test pins that every datatype it passes over takes any text in
`coerce_string_value`, so the check cannot drift from the parser.

Flake generation for 200k bound literals x 21 reps, release profile,
instructions:u per process (deterministic to a few hundred):
  custom datatype   11.11e9 -> 3.98e9  (-64%)
  xsd:anyURI         9.71e9 -> 4.03e9  (-58%), 188 -> 115 ns/flake
  xsd:integer        8.21e9 -> 8.62e9  (+5%, wall time unchanged; this
                     path parses either way)
…text

Bulk import keeps an `rdf:JSON` literal whose text is not JSON as written,
so a ledger can hold one, but the JSON-LD-flavored outputs parsed every
`rdf:JSON` value and failed the whole query on one ("Invalid JSON in @JSON
value"): JSON-LD, typed JSON and agent JSON selects, DOM and streamed, and
the graph crawl, its typed form, and a crawl of the literal's own `@value`
and `@type`.

It now reads back the way any other ill-typed literal does:
`{"@value": "not json", "@type": <rdf:JSON IRI>}`, never `@json`, which
would declare it a JSON value. `format::datatype` holds the rule once
(`json_document`, `ill_typed_json_literal`) and each of those writers uses
it. A valid JSON literal prints exactly as before. SPARQL JSON, XML, CSV
and TSV results, CONSTRUCT and export already wrote the text with its
datatype.

`it_ill_typed_json_reads` imports a ledger holding one and reads it back
through every surface: SELECT in each result format, DOM and streamed,
the crawl in both forms, CONSTRUCT as JSON-LD, Turtle and N-Triples, and
export as Turtle, N-Triples and JSON-LD. A valid JSON literal is pinned
to its output before this change on every surface, and the invalid one
fails on the fourteen JSON-LD-flavored surfaces without it.
@aaj3f

aaj3f commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, and for doing the graph import half yourself. I merged main again (3771514). #2226 has landed, so the diff is back to just this work. Everything you raised is folded in:

  • concepts/datatypes.md's "Text That Is Not a Value of Its Datatype" now says every transaction refuses one and lists the paths. It also says bulk import (fluree create --from, fluree graph import) keeps one as text, and that an invalid vector fails an import as it fails a transaction (e5db33b).
  • compatibility.md has the behavior-change entry for the refusals and for JSON-LD output carrying @type on a stored one. I also added a deviation note under RDF 1.1, since that section said "fully compliant" (e5db33b). The import fix below and the out-of-range integer each have their own entry (a182d90, 5d9f09f).
  • The inline threads are each answered. The one worth a heads-up: the parity table you suggested turned up a real data-integrity bug on main, since 4.0.0. Bulk import and fluree graph import stored rdf:JSON and xsd:duration values that read back as other strings. It's fixed here too (a182d90), and ledgers that already have them need a fluree reindex. Details are in the value_convert.rs thread and in their own section of the description.
  • One more call that came out of this: an imported rdf:JSON value that isn't JSON (import keeps it as written) used to make every query returning it fail with "Invalid JSON in @JSON value". It now reads back as typed text, the way every other ill-typed literal does: "not json"^^rdf:JSON in SPARQL, and {"@value": "not json", "@type": "…rdf-syntax-ns#JSON"} (the full IRI, not @json) in JSON-LD. Import stays lenient (d2ca74b, pinned across 23 output surfaces in it_ill_typed_json_reads). The description lists it under Behavior changes with the other two.

On your fluree graph import comment: Thanks for catching this and for 210a5ab. The bulk-import row in the description now reads "Bulk import (fluree create --from, fluree graph import; Turtle and JSON-LD)". datatypes.md and compatibility.md name both commands as well. fluree graph import also picks up the import remap fix in a182d90, since it shares remap_v2_record.

…urface

`an_imported_json_literal_reads_back_as_before` compared each surface's
output for a valid JSON literal as parsed JSON, which ignores key order
and whitespace. It now compares bytes: `VALID_OUTPUTS` records, for all 25
surfaces, the output main's formatters give an imported valid JSON
literal, and the test fails unless every recorded output matched a surface.

The recorded bytes are main's: with main's four formatter files in place
of this branch's, the valid case passes byte for byte and the invalid case
fails on the fourteen JSON-LD-flavored surfaces. Changing one surface's
valid output (the streamed typed select writing `@JSON`) fails the test,
naming that surface.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:transact Staging, commit, import/bulk-import, novelty, retraction semantics breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants