Repository navigation
fix(transact): read a JSON-LD transaction's keys as the expander resolves them - #2414
Conversation
…lves them Some checks a JSON-LD transaction gets before it is parsed resolved its compact keys with a copy of the context rules of their own. That copy applied a node's @context and nothing else, and read the top-level context without the parser's rule that a lone @base is also the @vocab, so for some documents it named a key differently from the parser. The checks now read the expanded document, where every key is the IRI the parser writes, resolved by the expander itself. An ordinary transaction is checked on the expansion the parser already makes, so it does no extra work. A document with edge annotations is checked as written, on one more expansion, after the lowering, so a document the lowering refuses keeps its reason. An IRI in an update's values is read the same way, and bulk import checks every expansion it makes. The expander keeps an @annotation (or @edge) on a value object, expanded as a node, as it does on a node reference; it was dropped.
aaj3f
left a comment
There was a problem hiding this comment.
@bplatz yes to reading the expansion: checking the IRIs the expander actually resolves closes the reserved-key gaps the old copy of the context rules left open, under a lone @base, scoped contexts and prefix-assembled imports. The one thing I'd hold the merge for is that the port leaves out 16a635b8e's where check: the old whole-document walk covered an update's where, so after this PR an update can read f:reifies* there, which main refused before. There's also a 3% cost on ordinary bulk imports with a cheap fix, below.
Full review below:
✅ Approving to unblock: please get the where/values check and the import fast path in before merging. Reading the expansion is the right fix: the checks now see a reserved key however the context spells it, which the old copy of the rules didn't under a lone @base, a scoped context or a prefix-assembled import. The two musts are the where/values check this port left behind from 16a635b8e, which main had, and a 3% import cost with a cheap way out.
Before merging:
fluree-db-transact/src/parse/jsonld.rs:146: run the query side's check over an update'swhereandvalues, asnext's16a635b8edoes. Without it, an update'swherecan now readf:reifies*.fluree-db-transact/src/import.rs:1603: move the ordinary-import check from its own walk intoImportSink::emit_triple. The walk costs every ordinary JSON-LD bulk import about 3%.
Notes:
- Performance, every row Claude measured (release builds, quiet box, three alternating rounds, per-round medians):
parse_transactionon 2,000 ordinary nodes −8.1%, on 2,000 annotated edges +33.6%, on 2,000 nodes with one annotated edge +47.4%; an end-to-end insert of 2,000 ordinary nodes −2.0% (one round a tie, so noise), of 2,000 annotated edges +11.4%; a serial bulk import of 100k ordinary NDJSON lines +3.0%, of 100k annotated lines +19.3%. The ordinary-import row is must 2. The annotated rows are the second expansion, whichef33f6cacremoves, and I'd like them stated in the body. - This PR is the class fix for "a key's IRI resolved by a second copy of the context rules", applied to the checks. Two instances stay outside it on
main, neither from this PR: the lowering's own copy (the@vocabcomment above, whichef33f6cacfixes onnext), and a lone@basein a bulk import, which writes the relativenamewhere a transaction writeshttp://example.org/name. Not this PR; the point fixes are right here. - Documents that changed side: newly refused are the reserved-key spellings the old copy missed, plus an update whose
valuesbinds a reserved IRI used only as an object. SPARQL refuses thatVALUEStoo, so that's the right side. Newly accepted are a reserved key inside a@jsonterm's payload, or with anullor[]value (Claude confirmed none writes a reserved flake), and thewherecase, which is the must. - The body doesn't say this ports
next's16a635b8e, andmerge-treeagainstnextconflicts in all four code files.nexthas that commit plusef33f6cac, so the merger takesnext's side throughout. A "Merging intonext" section like #2408's would save whoever does it a read. - Merge order: this merges cleanly onto current
main(with my #2227, which touchedimport.rsandparse/jsonld.rs) and alongside #2408 (both editparse_value_object) and #2425. Claude built all three merged on a box, and this PR's test,grp_importand thefluree-graph-json-ldunit tests pass there.
Adherence: Patterns ⚠️ · Performance ✖ · Deployment targets ✔ · Testing ⚠️ · Conventions ⚠️ · verification
- Patterns/abstractions:
⚠️ The checks now read the parser's own expansion, which is the shared mechanism, instead of a copy of its rules. The port leaves out thewherecheck that16a635b8eroutes through the query side's existing scan. - Performance (speed first, memory second): ✖ Ordinary bulk import is +3.0% from the always-on walk (must 2, recoverable in the sink). Annotated writes are +11.4% per insert and +19.3% per import, from the second expansion, which
ef33f6cacrecovers. Ordinary transactions got faster. Measured, every row in Notes. - Deployment targets: ✔ n/a: parse-time code that runs the same in every host, with no I/O, threads or clocks.
- Testing:
⚠️ The unit test runs in CI and fails under each of three mutations (the walk made a no-op, thevalueschecks removed, the expander hunk reverted). The import checks and an update'swherehave no test. - Conventions:
⚠️ Multi-line commit, CI green. The comments atjsonld.rs:136-141and:156-157describe the old order, and the body doesn't name the port or the cost.
Verified at f0f8c6e72: CI green on this SHA (test 16,198 passed, clippy, fmt, testsuite-sparql, wasm32); on a box Claude ran 48 transaction, update, SPARQL-update, import and query probes at the base and this head, mutation-checked the test three ways, timed release builds of both trees, and built main with #2408, #2414 and #2425 merged.
Approving so you can merge once the where check and the import fast path are in.
| // parser resolves: an ordinary document on the expansion the parser makes | ||
| // anyway, an annotated one as written, before the lowering adds | ||
| // statements of its own. | ||
| let annotated = super::edge_annotations::document_has_annotation_keys(json); |
There was a problem hiding this comment.
Blocking: this port drops 16a635b8e's where/values check, which main had through the old whole-document walk.
The old firewall walked the whole update document, so a where that named a reserved predicate was refused with the rest. This port replaces it with checks on what the parser expands: the data clauses, the templates and the values cells. An update's where never goes through the expander (it's parsed by the query parser), so nothing checks it now. next's 16a635b8e, which this ports, covers it: it adds fluree_db_query::parse::reject_reserved_iris_in_clause and runs it over an update's where and values in parse_rooted. That hunk didn't come across.
Claude ran this on a box, against a ledger holding one annotated edge:
{"@context": {"ex": "http://example.org/"},
"where": {"@id": "?r", "https://ns.flur.ee/db#reifiesSubject": {"@id": "?s"}},
"insert": {"@id": "ex:x", "ex:saw": {"@id": "?s"}}}On the base it's refused ("resolves to a system-controlled predicate"). At this head, and on main with this PR merged, it's accepted and writes ex:x ex:saw ex:a, the reifier bundle's subject copied into an ordinary triple. The compact f:reifiesSubject spelling behaves the same. A JSON-LD query refuses that where (fluree-db-query/src/parse/mod.rs:106), and SPARQL refuses naming f:reifies* in a pattern, so this would leave an update's where as the one surface that reads the bundle.
The fix is next's: the reject_reserved_iris_in_clause wrapper in fluree-db-query/src/parse/mod.rs, its call for where and values when txn_type is Update, and next's test case ("where": {"@id": "?s", "x": "?o"} under a term naming f:reifiesSubject). Your values-cell checks can stay or go once that's in. The txn-meta half of 16a635b8e isn't needed on main: txn-meta already refuses an f: predicate other than f:message and f:author (Claude checked the sidecar and the envelope forms).
| )?; | ||
| Ok(()) | ||
| } else { | ||
| crate::parse::edge_annotations::refuse_reserved_iris(expanded) |
There was a problem hiding this comment.
Blocking (performance): the always-on walk costs every ordinary JSON-LD bulk import about 3%.
For a chunk with no annotation, refuse_reserved now walks every expansion the import makes. On a 20,000-node expansion that walk takes 5.5–5.6% of the expansion's own time. The base skipped any chunk whose text lacked reifies. Some check has to run, since a prefix-assembled IRI defeats that text gate, but it doesn't need a pass of its own.
Claude timed release builds of the base and this head on a quiet box, in three alternating rounds. A serial bulk import of 100k ordinary NDJSON lines is +3.4% / +3.0% / +2.5% (mean +3.0%), and the per-round minimums agree (+3.7% / +2.9% / +2.6%). Ordinary transactions got faster: parse_transaction −8.1%, end-to-end insert −2.0%.
Every triple already reaches ImportSink::emit_triple, so a reserved-predicate test on the predicate's Sid there (is_reserved_reifies_predicate, which fails on the namespace code for nearly every triple) should do the same job without the walk. It wants a flag set only for a JSON-LD chunk the lowering didn't touch: a lowered chunk writes the bundle's f:reifies* slots through that sink and keeps its as-written check, and a Turtle import stays permissive for export round-trips. Commenting here because import_sink.rs is not in this diff.
The annotated rows (+33.6% / +47.4% parse_transaction, +11.4% per insert, +19.3% per import) come from the second expansion. I think they're a fair price for closing these gaps, but the body's "one more expansion" should state them. Is next's ef33f6cac, which removes them (and fixes the @vocab lowering issue below), meant for main too?
There was a problem hiding this comment.
Addressed in 8b13cab. The PR body now states the annotated-write cost; ef33f6cac stays on next.
| if needs_lowering { | ||
| let top_ctx = crate::parse::edge_annotations::top_level_context(&doc)?; | ||
| crate::parse::edge_annotations::run_user_authored_reifies_firewall(&doc, &top_ctx)?; | ||
| let refused = refuse_reserved_in_chunk(&doc, shared.as_ref()); |
There was a problem hiding this comment.
This is more of a suggestion than a must. The import side is where the old check was narrowest: it ran only on a chunk whose text contained reifies, so a reserved IRI assembled from a prefix, or a term an NDJSON line's shared context scopes to one, wasn't checked. At this head both are refused (Claude checked each on a box). But the PR's test drives parse_transaction only, so nothing would catch refuse_reserved_in_chunk or the per-expansion check regressing. A grp_import test with one of each would pin them. It's minor and non-blocking, but if you agree it's right, I'd rather see it folded in now than lost in the backlog.
| @@ -139,8 +139,11 @@ fn parse_rooted( | |||
| // clone, and only when an `@annotation` / `@edge` / `@reifies` block is | |||
There was a problem hiding this comment.
So minor, but the comment just above the new one (:136-141) still says "The firewall always runs on the original document … it must not be gated", and :156-157 and the name lower_edge_annotations_after_firewall describe the old order. Worth a pass while you're here, since the next reader will trust the old paragraph.
| if update { | ||
| super::edge_annotations::lower_delete_annotation_blocks(&mut lowered)?; | ||
| } | ||
| super::edge_annotations::lower_edge_annotations_after_firewall( |
There was a problem hiding this comment.
Your code didn't introduce this, but it's the same shape this PR fixes, one step over. The lowering still names the edge's predicate by its own reading of the context: it writes the key as written into f:reifiesPredicate. Under {"@vocab": "http://example.org/"}, "knows": {"@id": "http://example.org/b", "@annotation": {"http://example.org/conf": 0.9}} writes the base edge as http://example.org/knows but the reifier's f:reifiesPredicate as the relative IRI knows, so @reifies can't find the annotation. Claude reproduced that on the base and at this head; with a prefix, a term, a full IRI or a lone @base it attaches. next's ef33f6cac fixes exactly this by lowering on the expansion. Is that one meant to come to main as well? If not, a @vocab-term annotation is worth a refusal on main rather than a link that silently points nowhere.
There was a problem hiding this comment.
Addressed in 3d347a3. ef33f6cac stays on next; on main the lowering now resolves the key as the expander resolves a property key, so a @vocab key links (an annotation-rooted query finds it, and a delete retracts it).
|
For whichever of this PR and #2408 lands second: #2408 adds a refusal in |
The checks this branch moved onto the expansion read what the parser expands: an insert, delete or upsert's data and an update's templates. An update's `where` and `values` are parsed by the query parser instead, so nothing checked them for reserved predicates any more. The compact-form walk they replaced had covered the whole document, so an update could again name `f:reifiesSubject` in its `where` and copy a reifier bundle's subject into an ordinary triple, which a JSON-LD query and SPARQL both refuse. `fluree_db_query::parse::reject_reserved_iris_in_clause` exposes the scan a query's clauses get, and parsing an update runs it over `where` and `values` against the document's context. The test refuses a `where` naming the reserved IRI directly and through a term.
A bulk import refused the `f:reifies*` predicates in a JSON-LD chunk no annotation lowering touched by walking every expansion it made, a pass of its own over each document that cost an ordinary import about 3%. The import sink now does it: for such a chunk it tests each statement's predicate by its Sid, which fails on the namespace code for nearly every one, and keeps the first refusal as it does any other the sink makes. Every statement already reaches the sink, in the default graph, a named graph or a list, so the check sees what the walk saw. A lowered chunk keeps its check of the document as written, since the lowering writes those predicates itself, and a Turtle import still carries them as exported.
A bulk import's check used to run only on a chunk whose text held `reifies`, so a reserved IRI assembled from a prefix, or a term an NDJSON file's shared context defines, slipped past it. Both are now refused by the IRI the expander resolves, and nothing pinned that on the import path, where the check now runs in the sink. The new test imports one file of each kind, neither containing the text `reifies`, and expects the refusal. With the sink's check off, both imports go through and write the reserved predicate.
This branch moved the reserved-predicate check to read the document as written after the annotation lowering has run, so a document the lowering refuses keeps its reason. Several comments still described the old order, the check running first on the original document, and `lower_edge_annotations_after_firewall` was named for it. The lowering without the check is now `lower_edge_annotations_unchecked`, documented for a caller that checks the document as written itself. The parser's comments, the module's, and the lowering's notes on its synthesized `f:reifies*` IRIs say what holds now, and a wrapper's unparseable `@context` is said to be refused where it is read.
The expander refuses a value object whose `@value` is an array or a map without `@type: @json` (or `@vector` for an array), and one typed `@id` whose `@value` is not a string. Neither message said where the value object was, so in a bulk import's chunk, which expands many nodes as one document, the error named no node. Both now come as `JsonLdError::InvalidValueObject`, whose message leads with the value object's place in the document: the keys, as written, and array indexes from the root, as a JSON array, such as `["@graph",1,"ex:p"]`. The reasons are unchanged.
…er does The annotation lowering wrote the edge's key as written into the reifier's `f:reifiesPredicate`, as an `@id`. An `@id` resolves against `@base`, while the edge's own key resolves through a term, a prefix, then `@vocab`. Under `@vocab`, `"knows"` wrote the edge under `http://example.org/knows` but named the relative `knows` in the reifier, so the annotation linked to no edge: an annotation-rooted query did not find it, and a delete of the annotated edge did not retract it. The lowering now resolves a forward key, and a key in a `@reverse` map, as the expander resolves a property key, against the node's own context, in an insert and in a delete. A key none of those rules resolves is left as written, so `@base` resolves it as before. A term defined with `@reverse` already names its resolved IRI.
Addressed in c23afa7.
Addressed in 8b13cab (the check is in the sink's
Stated in the PR body.
Both are in the PR body now.
Left on |
Brings #2414 and #2415. #2414 ported next's `16a635b8e` to main, and next also has `ef33f6cac`, which lowers annotations on the expansion. next's side is kept in `parse/jsonld.rs` and `parse/edge_annotations.rs`; main's duplicate `reject_reserved_iris_in_clause` and its `values`-cell checks are dropped (next's `values` already get the query side's check, and on next `refuse_reserved_predicate` covers `rdf:reifies` too). What main adds on top comes across: - Bulk import: next still walked every expansion for reserved predicates. A chunk with no lowering is now checked by the import sink, by each statement's predicate Sid, with no pass of its own; a lowered chunk keeps the walk before its lowering. On next the sink refuses `rdf:reifies` as well as the `f:reifies*` slots (`is_annotation_predicate`), as next's walk did, and the import test gains an `rdf:reifies` case. main's `refuse_reserved_in_chunk` is unused here and dropped. - The expander's value-object refusals carry their location (`InvalidValueObject`), with next's reason text. - main's `@vocab` annotation test passes on next, whose lowering reads the expansion; its genesis is awaited. #2415: the per-row store ids join next's `term_only` scan state, and next's independent-count call of `make_right_scan` passes none. On next the fused chain answers `DESCRIBE ?x WHERE { ex:a ex:knows ?x }`, so that check asserts the fused chain's stamp, not per-row probes.
Some checks a JSON-LD transaction gets before it is parsed resolved its compact keys with a copy of the context rules of their own. That copy applied a node's
@contextand nothing else, and read the top-level context without the parser's rule that a lone@baseis also the@vocab. For some documents it named a key differently from the parser.Fix: the checks read the expanded document instead, where every key is the IRI the parser writes, resolved by the expander itself. A context rule the expander applies (a property's or a type's scoped context, a node's context) is then applied to the check by construction. This ports
next's16a635b8etomain.parse_transaction+34% to +47%);nextremoves it by lowering on the expansion (ef33f6cac), which stays onnext.whereandvaluesgo to the query parser, not the expander, and get the check a query's clauses get. An IRI in avaluescell is also read as the parser resolves it.f:reifies*as exported.@annotation(or@edge) on a value object, expanded as a node, as it does on a node reference. It was dropped.No change to what a valid document writes.
Also in this PR
@vocabkey links to its edge. The lowering wrote the edge's key as written into the reifier'sf:reifiesPredicate, which resolves as an@id, against@base. Under@vocab,"knows"wrote the edge ashttp://example.org/knowsand the link as the relativeknows, so@reifiesdid not find the annotation and a delete of the annotated edge left it. The lowering now resolves the key as the expander resolves a property key. This predates this PR;nextfixes it as part ofef33f6cac.@valuewithout@json, and of an@id-typed value that is not a string, now lead with the value object's place in the document (["@graph",1,"ex:p"]), so a bulk import's error points at the node.Tests
parse::jsonld::tests::checks_read_keys_as_the_expander_resolves_them: reserved keys under a lone@base, a node'scontext, a property's and a type's scoped context, an annotation's body, an update'svalues, and an update'swhere(directly and through a term). Each is refused with the reserved-predicate message.it_import::import_refuses_a_reserved_predicate_however_it_is_spelled: a bulk import refuses a reserved predicate assembled from a prefix, and one an NDJSON file's shared context defines, in files without the textreifies. It fails with the sink's check off.it_edge_annotations::an_annotation_on_a_vocab_key_links_to_its_edge: under@vocab, an annotation-rooted query finds the edge, and a delete retracts the annotation. It fails with the key written as written.expand::tests::a_refused_value_object_names_where_it_is.When merging into next
nexthas16a635b8e, which this ports, andef33f6cac, which lowers annotations on the expansion. Takenext's side inimport.rs,import_sink.rs,parse/edge_annotations.rsandparse/jsonld.rs. Check two things survive:next's import still walks each expansion;JsonLdError::InvalidValueObjectand its location inexpand.rs.