Skip to content

fix(sparql): an anonymous [] is one node across ; and , lists - #2270

Merged
bplatz merged 5 commits into
mainfrom
fix/delete-where-anon-blank-node
Nov 11, 2026
Merged

bplatz merged 5 commits into
mainfrom
fix/delete-where-anon-blank-node

Conversation

@bplatz

@bplatz bplatz commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2266

An anonymous [] named a different node in each triple it appeared in. Two symptoms:

Statement Data Before Now
DELETE WHERE { [] ex:p ?o ; ex:q ?z } ex:p and ex:q on different subjects deletes 2 0
same, inside GRAPH <g> same, in <g> deletes 2 0
SELECT ?o ?z WHERE { [] ex:p ?o ; ex:q ?z } ex:p and ex:q on different subjects cross product only rows where one node has both
INSERT DATA { [] ex:p 1 ; ex:q 2 } — 2 nodes 1 node

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 separate BlankNodeValue::Anon. Every consumer then minted a fresh variable or node for each copy. The no-GRAPH DELETE WHERE path 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 BlankNodeValue collapses into a label field on BlankNode, and the anonymous counters downstream go away:

  • DELETE WHERE: the stateful namer becomes bnode_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. The bnodes parameter on the three helpers is gone.
  • Templates: [] takes the parser's label, so BlankNodeCounter::next() is gone. The counter now mints only the -ann annotation 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

  • Parser: the copies of [] in a ; or , list share one label, and separate []s don't. -anon joins the reserved-label checks.
  • Lowering: each []'s DELETE template reads the variable its WHERE pattern binds: as subject, as object, several in one pattern, and across a ; copy.
  • grp_transact:
  • grp_query_sparql: the SELECT twin, including that [] stays out of SELECT *.

The new end-to-end tests fail against the previous source: DELETE WHERE deletes 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.

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
@bplatz bplatz added the bug Something isn't working as expected label Nov 6, 2026
@bplatz
bplatz requested a review from a team November 6, 2026 04:01

@jdorety jdorety left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

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.

Addressed in 71e2855

@aaj3f aaj3f 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.

@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_where and reuses BlankNodeVarNamer; the GRAPH path's rewrite_blank_nodes_to_vars would 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 a grp_transact end-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

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 (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.

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.

Addressed in 71e2855

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.
@bplatz bplatz changed the title fix(sparql): an anonymous [] in DELETE WHERE deletes what it matches fix(sparql): an anonymous [] is one node across ; and , lists Nov 11, 2026
Each puts a [] subject across a ; list inside GRAPH, so it waited on [] being
one node across the list, not on GRAPH-variable semantics.
@bplatz
bplatz merged commit 8308ba1 into main Nov 11, 2026
19 checks passed
@bplatz
bplatz deleted the fix/delete-where-anon-blank-node branch November 11, 2026 10:35
bplatz added a commit that referenced this pull request Nov 11, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SPARQL DELETE WHERE: an anonymous [] deletes nothing

3 participants