Repository navigation
fix(sparql): an anonymous [] is one node across ; and , lists - #2270
Conversation
On the triple-only DELETE WHERE path the WHERE patterns and the delete templates shared one blank-node namer, which numbers each anonymous [] it meets. Lowering each triple's WHERE side and then its template named one [] twice, so the template read a variable nothing bound and deleted nothing. Each side now has its own namer; both meet the [] in the same order and so name them alike. Fixes #2266
jdorety
left a comment
There was a problem hiding this comment.
Blocking: with this change, DELETE WHERE { [] ex:p ?o ; ex:q ?z } deletes triples from unrelated subjects.
The parser copies an anonymous [] subject into each triple of a ; or , list (subject.clone(), fluree-db-sparql/src/parse/query/term.rs:916, :1471). Each copy is a separate BlankNodeValue::Anon, and the namer gives each one its own variable. So the pattern matches "something with ex:p" and, separately, "something with ex:q". It should match one node that has both.
Verified on main and on this branch (retracted triple counts):
| Data | DELETE WHERE |
Correct | main | PR |
|---|---|---|---|---|
ex:a ex:p "one" . ex:b ex:q "two" |
{ [] ex:p ?o ; ex:q ?z } |
0 | 0 | 2 |
ex:a ex:p "one" ; ex:q "two" |
same | 2 | 0 | 2 |
ex:a ex:p ex:x . ex:b ex:p ex:y |
{ [] ex:p ex:x , ex:y } |
0 | 0 | 2 |
first row's data in <g> |
{ GRAPH <g> { [] ex:p ?o ; ex:q ?z } } |
0 | 2 | 2 |
The namer change is correct and still needed. On its own, it brings the no-GRAPH path's existing over-deletion from the GRAPH path with it. SELECT ?o ?z WHERE { [] ex:p ?o ; ex:q ?z } is also wrong on main, for the same reason.
Fix: in parse_blank_node, give [] a reserved label, the same way [ p o ] already gets reserved_blank_label("bnpl", …). Every copy then refers to the same node. That fixes DELETE WHERE with and without GRAPH, and SELECT. Please add the rows above as regression tests (including the rows where the correct answer is 2), plus a SELECT twin.
| ns, | ||
| vars, | ||
| &mut bnode_vars, | ||
| &mut delete_bnodes, |
There was a problem hiding this comment.
Naming each side's []s in order is correct, but it assumes every Anon is a different node. The parser breaks that assumption for [] p o ; q z and [] p o1 , o2 by copying the subject, so this now matches two unrelated subjects and deletes from both. Details in the review summary.
aaj3f
left a comment
There was a problem hiding this comment.
@bplatz nice find and a clean fix; no pushback at a design level on the diagnosis or the test coverage. My one headline note is a design question rather than a must: the two namers agree because both sides walk the triples in step, and the GRAPH path already has the by-construction version of this (rewrite_blank_nodes_to_vars, name each blank node once, lower both sides from it). Claude tried that on the triple-only path and it passes your end-to-end test, so it might be worth taking now, before RDF 1.2 triple terms give one side something to name that the other doesn't. Details below; good to merge either way.
Full review below:
✅ Approve.
This is a nice catch, and a nasty one to have had in place: a [] in a triple-only DELETE WHERE matched and silently deleted nothing, and it has been that way since the v4 baseline (the anonymous arm counted from the same shared namer before and after 6762ed7ce renamed it to _:[]{N}). The diagnosis is exactly right, the fix is at the right spot, and both new tests go red with the shared namer restored, which Claude confirmed on a box. The one note below is a question about making the two sides agree by construction (the GRAPH path's rewrite-once) instead of by walking in step; it's optional. On SPARQL ↔ JSON-LD parity I agree there's no twin to write: JSON-LD refuses blank nodes in delete templates (parse/jsonld.rs:722) and has no DELETE WHERE shorthand, so there's no JSON-LD form of this bug.
Adherence to repo commitments:
- Patterns/abstractions: ✔ stays inside
lower_delete_whereand reusesBlankNodeVarNamer; the GRAPH path'srewrite_blank_nodes_to_varswould be the shared step (optional note). - Performance (speed first, memory second): ✔ lowering-time only, once per request; no engine or hot-path change, no performance-degradation risk.
- Deployment targets: ✔ n/a — host-neutral lowering code with no I/O, threads or clocks; same behavior embedded, in the server and on wasm32.
- Testing: ✔ a unit test pinning one variable per
[]on both sides, and agrp_transactend-to-end test over the triple-only path (subject, object, several in one pattern); both fail under the mutation. - Conventions: ✔ self-describing title and multi-line commit body; CI fmt/clippy green on the head.
Verified at branch HEAD (919b8f736): Claude ran the two new tests on a box (pass), reverted the fix to one shared namer (both fail: left: None at lower_sparql_update.rs:2634, retract_count 0 vs 2 at it_transact_update.rs:3303), and ran every delete_where unit and grp_transact test (8 and 6 pass); CI test, clippy, testsuite-sparql and wasm32 are green on this SHA.
Approving so you can merge when ready, but maybe worth a look at the rewrite-once question first.
| .collect(); | ||
|
|
||
| let mut bnode_vars = BlankNodeVarNamer::new(); | ||
| // One namer per side: each numbers the anonymous `[]`s it meets, and both |
There was a problem hiding this comment.
Optional (design). This is more of a question than a suggestion. The two namers agree because both sides walk the triples in the same order and both refuse quoted triples and triple terms, which is true today, but it's an agreement held by convention across three functions (subject_to_unresolved_delete_where, object_to_unresolved_delete_where, lower_triple_to_delete_template_delete_where) rather than by construction.
The place I'd expect it to slip is already on the roadmap: both sides refuse RDF 1.2 triple terms today as "deferred", and the day one side learns to lower a blank node inside one (or the two learn it differently), the counters drift and this exact bug comes back with no compile error.
The GRAPH path already has the by-construction version: rewrite_blank_nodes_to_vars names each blank node once on the pattern, and both sides lower from that. Claude tried that here on a box, at this head with the namers put back to one shared namer (i.e. the bug) plus one line before the triple-only loop:
let expanded_pattern = rewrite_blank_nodes_to_vars(&expanded_pattern);and your end-to-end test plus every delete_where test in grp_transact passed (6/6). Your unit test was the only failure, and only because the rewritten names are ?_:[]N, so its starts_with("_:[]") filter counted 0 of 4 (a contains would do). With the rewrite in place, the bnodes parameter on those three helpers could go entirely, and the two DELETE WHERE paths would share one blank-node step. It's minor and non-blocking, and your version is correct, but if you agree it's the sturdier shape I'd rather see it folded in now than have the next triple-term change rediscover it.
A `;` or `,` list copies its subject into each triple. For `[]` each copy was a separate anonymous blank node, so `[] ex:p ?o ; ex:q ?z` matched "something with ex:p" and, separately, "something with ex:q". DELETE WHERE deleted from unrelated subjects (with and without GRAPH), SELECT returned the cross product, and INSERT DATA minted one node per triple. The parser now gives each `[]` a reserved `-anon<N>` label, as it already does for `[ p o ]`, so every copy names the one node. With every blank node labeled, `BlankNodeValue` collapses to a `label` field on `BlankNode`, and the lowering's anonymous counters go: DELETE WHERE's namer becomes a stateless `bnode_var_name`, and template `[]`s take the parser's label.
[] in DELETE WHERE deletes what it matches[] is one node across ; and , lists
Each puts a [] subject across a ; list inside GRAPH, so it waited on [] being one node across the list, not on GRAPH-variable semantics.
Ports main's changes to next's shapes: - `[]` is one node across `;`/`,` lists (#2270): next's DELETE WHERE lowering (lists, triple terms) names blank nodes by label, so its triple-only lane, which still shared one counter between the WHERE side and the template, stops dropping `[]` deletes. A param's blank node is always a stored node, so `stored_node_iri` can't fail. - Push applies each commit's graph lifecycle and keeps next's link flakes; large graph drops filter pooled graphs through the same set. - SHACL graph sources and the large-drop test use `GraphId`, not u16; `genesis_ledger` is awaited; CLI map commands keep next's routing with main's error context.
Fixes #2266
An anonymous
[]named a different node in each triple it appeared in. Two symptoms:DELETE WHERE { ex:s ex:p [] }with noGRAPHblock matched and deleted nothing (SPARQLDELETE WHERE: an anonymous[]deletes nothing #2266).[] ex:p ?o ; ex:q ?zand[] ex:p ex:x , ex:ymatched each triple against a different node:DELETE WHERE { [] ex:p ?o ; ex:q ?z }ex:pandex:qon different subjectsGRAPH <g><g>SELECT ?o ?z WHERE { [] ex:p ?o ; ex:q ?z }ex:pandex:qon different subjectsINSERT DATA { [] ex:p 1 ; ex:q 2 }Cause
[]was the one blank node with no label. The parser copies the subject into every triple of a;or,list, and each copy of[]was a separateBlankNodeValue::Anon. Every consumer then minted a fresh variable or node for each copy. The no-GRAPHDELETE WHEREpath went further: it numbered anonymous nodes once for the WHERE side and again for the template, so the template read a variable nothing bound.Fix
The parser gives each
[]a reserved-anon<N>label, as it already does for[ p o ](-bnpl<N>). The copies a list makes all carry one label, so every consumer treats them as one node. User labels can't start with-, so these labels can't collide with one a user writes.Every blank node now has a label, so
BlankNodeValuecollapses into alabelfield onBlankNode, and the anonymous counters downstream go away:DELETE WHERE: the stateful namer becomesbnode_var_name, a function of the label. The WHERE side and the template agree by construction, not by walking the triples in the same order. Thebnodesparameter on the three helpers is gone.[]takes the parser's label, soBlankNodeCounter::next()is gone. The counter now mints only the-annannotation reifier labels.A DELETE-side blank node with a parser-reserved label is reported without the label (
Blank node is not allowed in …), since the user never wrote it.Tests
[]in a;or,list share one label, and separate[]s don't.-anonjoins the reserved-label checks.[]'s DELETE template reads the variable its WHERE pattern binds: as subject, as object, several in one pattern, and across a;copy.grp_transact:DELETE WHEREover each row of the table above, with and withoutGRAPH, including the shapes where the right answer is to delete 2.DELETE WHERE: an anonymous[]deletes nothing #2266 test.grp_query_sparql: the SELECT twin, including that[]stays out ofSELECT *.The new end-to-end tests fail against the previous source:
DELETE WHEREdeletes 2 where it should delete 0, INSERT DATA creates 4 nodes where it should create 2, and SELECT returns the cross product.There is no JSON-LD twin: the bug is in SPARQL's
[]syntax and;/,lists, which JSON-LD doesn't have.