Repository navigation
Reject a Cypher read clause written after a write clause, which was silently reordered - #1886
Conversation
`Update` is a fixed reads-then-writes shape (`ast/stmt.rs:109`) with
nowhere to record that a clause came after a write, and the parser's
clause loop dispatches purely on keyword. So a read clause written later
was silently hoisted in front of every write and the engine executed a
different statement than the author wrote.
The damage is not uniform, which is why this was filed as one symptom and
is actually three:
MERGE (n:P {id:"x"}) WITH n SET n.nm = "b"
-> errored, blaming a policy that is not involved
MERGE (n:P {name:"Zed"}) WITH n.name AS nm SET n.marker = nm
-> COMMITTED 4 flakes, writing one marker per pre-MERGE name onto the
newly merged node; the hoisted WITH read `n.name` with `n` unbound
MATCH (n) SET n.a = 1 WITH n WHERE n.a = 1 SET n.b = 2
-> COMMITTED 0 flakes; the filter ran before the SET it reads
The second is silent wrong data and the third is the silent write-nothing
the issue title describes. Both reach `transact_cypher`, so Bolt and the
server's Cypher write endpoint have them.
Rejecting in the parser rather than downstream is the load-bearing
choice, for two reasons that are easy to miss:
- It is the only point that still HAS the ordering. By the time `Update`
exists it is gone, and a downstream check sees `[Match, With]` /
`[Merge, Set]` for BOTH the legal `MATCH … WITH … MERGE … SET` (which
commits correctly today) and the hoisted `MATCH … MERGE … WITH … SET`.
They are indistinguishable without source positions.
- It is the only point every write path passes through. The third case
above has no MERGE, so it never routes to the sequential driver and a
guard in `validate_members` would not see it — it would leave the
silent zero-flake shape live while looking fixed.
The guard is positional, not kind-based: it fires only when a read clause
is written after the first write clause, so every reads-then-writes
statement is untouched.
The other two clause loops in this file, `parse_call_body` and
`parse_procedure_call`, collect only read clauses and have no write
bucket, so the hoist cannot occur there and they are not touched.
"the created pattern is not visible to the writer (a policy may hide it)" is false in the case that produced it most often, and it sends people to the wrong subsystem — every repro of #1873 is an unauthenticated in-memory ledger with no policy anywhere. A hidden node is one cause, so it stays named. But the message now also names the other: a read clause whose bindings the MERGE itself was supposed to supply. The parser guard in the previous commit removes the route that made that common, so this is mostly for whatever reaches it next.
Two parse-level tests and two end-to-end, and the pairing matters: a
guard that rejected everything would pass the rejection half alone, so
the still-works half asserts exact `receipt.flake_count` values rather
than the absence of an error. That distinction is not academic here —
two of the five rejected shapes COMMITTED before the fix, so a test
asserting "the transaction succeeded" would have passed against the bug.
Each rejection case records what it did before, including the two that
committed: 4 flakes of wrong data, and 0 flakes silently.
The acceptance half includes `MATCH (a) WITH a MERGE (b) SET b.x = 1`
specifically, because its AST is identical to a hoisted statement's and
only source position separates them — it is the case a kind-based guard
would have broken.
One existing test changed. `delete_in_chain_rejected_clearly` used
`MERGE (a:P {id: 1}) MATCH (b:Old) DELETE b`, which is itself a hoisted
statement, so the new guard rejects it earlier and for a more fundamental
reason than the DELETE deferral it exists to pin. Reordering the reads
ahead of the writes keeps it pointed at its own subject. Worth noting the
guard found a hoisted statement inside the repo's own suite.
`it_smoke.rs` gains the `needless_raw_string_hashes` allow its sibling
Cypher test files already carry, for the same reason they carry it.
A `breaking-change` label tells a migrating user nothing to grep for, so the Deferred entry enumerates all four classes with before/after rather than stating the rule and leaving them to discover which of their statements it hits. Two of the four failed loudly before and two did not: one committed wrong data, one committed nothing at all. The fourth worked, and it is called out explicitly as the surprising row — it worked only because that statement's reordering was unobservable, and the row above it is the same shape with a `WHERE` that silently wrote nothing. An author cannot tell which they have written, which is the argument for rejecting both.
zonotope
left a comment
There was a problem hiding this comment.
I have a few minor nits, but 👍🏾
| it is a **deliberate tightening of a shape that was previously accepted and | ||
| silently miscompiled**: the statement was rearranged so that every read ran | ||
| before every write, which is a different statement. What that cost depended | ||
| on the shape, and only two of the four classes failed loudly: |
There was a problem hiding this comment.
The last sentence doesn't agree with the table. The first entry errored before, the next two committed either incorrect data or nothing, and the last one worked. Only one class failed loudly.
|
|
||
| The last row is the one that will surprise people: it produced the right | ||
| answer, but only because that statement's reordering happened to be | ||
| unobservable. The row above it is the same shape with a `WHERE`, and it |
There was a problem hiding this comment.
I don't think there's a test for the last one, but it's called out explicitly here.
| // `MATCH … MERGE … WITH … SET`. It is also the only point every write | ||
| // path passes through; the sequential driver's own validation never sees | ||
| // the third case above, which does not route to it. | ||
| let mut first_write: Option<SourceSpan> = None; |
There was a problem hiding this comment.
It doesn't look like this value is ever used explicitly besides just checking if it's Some. If that's the case, then I think a boolean value would be both more semantically accurate and simpler.
| @@ -1,3 +1,5 @@ | |||
| #![allow(clippy::needless_raw_string_hashes)] | |||
There was a problem hiding this comment.
I think it would be better to remove the string hashes that don't have quotes below instead of disabling this linter.
For example, r#"MATCH (n:P) RETURN n"# could just be written as r"MATCH (n:P) RETURN n"
…er-hoist # Conflicts: # docs/query/cypher.md # fluree-db-api/tests/it_cypher_seq_writes.rs
|
Brought up to date with |
Fixes #1873
Update, 2026-11-17
Brought up to date with
main, and zonotope's four review points addressed: the guard tracks abool,it_smoke.rsdrops the lintallowfor plain raw strings, the doc's "how many failed loudly" sentence matches its table, and the deliberately rejectedMATCH (n) SET n.a = 1 WITH n SET n.b = 2is now tested, along with the unaffected… SET n.a = 1 SET n.b = 2.Re-measured on current
main, the hoist still does what the table below says. One shape got worse aftermainreworked the Cypher write path:CREATE (n:P {id: "c1"}) WITH n SET n.nm = "a"used to error, and now commits nothing, not even theCREATE. It is added to the table and the tests.The bug, in one sentence
A read clause written after a write clause was silently hoisted in front of every write, so the engine executed a different statement than the author wrote.
Updateis a fixed reads-then-writes shape (fluree-db-cypher/src/ast/stmt.rs:109) with nowhere to record that a clause came after a write, and the parser's clause loop dispatches purely on keyword, never on position.MERGE (n) WITH n SET n.x = 1is handed to the engine asWITH n MERGE (n) SET n.x = 1— aWITHprojecting a variable nothing has bound yet.It is three symptoms, not one, and only two of them are loud
Measured on
a5fdba900, asserting onreceipt.flake_count:The issue is titled "writes nothing", and the third case is exactly that — but it is not the shape the issue describes, and it has no
MERGE. The second case is silent wrong data. Both reachtransact_cypher, so Bolt and the server's Cypher write endpoint have them.Why the guard is in the parser
Two independent reasons, both measured, and the second is easy to miss.
The obvious site structurally cannot see the worst case.
MATCH (n) SET n.a = 1 WITH n WHERE n.a = 1 SET n.b = 2has noMERGE, sodetect_sequentialreturnsNoneand the statement never routes to the sequential driver — a guard invalidate_members(fluree-db-api/src/cypher_seq.rs:1338) would never execute for it. It would have shipped, passed its own tests, closed the issue, and left the silent-write-nothing case live.By the time
Updateexists the ordering is gone.MATCH (a) WITH a MERGE (b) SET b.x = 1commits correctly today (3 flakes) and its AST is[Match, With] / [Merge, Set]— structurally identical to the hoistedMATCH (a) MERGE (b) WITH a SET b.x = 1. Only source position separates them, so any kind-based rule downstream either breaks a working statement or misses a broken one.The parser is the only point that still holds the ordering and the only point every write path crosses. The guard is positional: it fires only when a read clause is written after the first write clause, so every reads-then-writes statement is untouched.
parse_call_bodyandparse_procedure_callcollect only read clauses and have no write bucket, so the hoist cannot occur there; they are not touched.Breaking change — every class that now errors
breaking-changealone gives a migrating user nothing to grep for, so here is the full set. Two of these four work today, and they are the ones that will surprise people.MERGE (n) WITH n SET n.x = 1MATCH … WITH … MERGE … SET …, or split in twoMERGE (n {name:"Z"}) WITH n.name AS nm SET n.marker = nmMATCH (n) SET n.a = 1 WITH n WHERE n.a = 1 SET n.b = 2CREATE (n:P {id: 1}) WITH n SET n.nm = "a"CREATECREATE (n:P {id: 1}) SET n.nm = "a"MATCH (n) SET n.a = 1 WITH n SET n.b = 2MATCH (n) SET n.a = 1 SET n.b = 2The surprising row is the last one, and it is deliberate. It produced the right answer — but only because that statement's reordering happened to be unobservable. The row above it is the same shape with a
WHERE, and it silently wrote nothing. An author cannot tell from the statement which of the two they have written, so both are rejected rather than one being blessed. Narrowing to "provably observable" would need per-statement dataflow throughSETandWHERE, and a wrong "observable" check silently admits a corrupting shape — the failure mode is asymmetric and points at the wide guard.Unaffected: anything that already puts its reads first, and
MATCH (n) SET n.a = 1 SET n.b = 2(no read after the write).The error message
Was: "MERGE re-bind matched no rows — the created pattern is not visible to the writer (a policy may hide it)". That is false for every repro here — they are unauthenticated in-memory ledgers with no policy anywhere — and it sends people to the wrong subsystem. It now names both possible causes.
The new parser error names the reordering and the working form rather than just refusing:
The guard found a hoisted statement in our own test suite
delete_in_chain_rejected_clearly(it_cypher_seq_writes.rs) usedMERGE (a:P {id: 1}) MATCH (b:Old) DELETE b— itself a hoisted statement. It was being rejected for a different, also-valid reason (DELETE in a multi-clause write is deferred), so the flaw was invisible.That cuts both ways and both are worth stating. It widens the real-world impact of this breaking change: the shape occurs naturally enough that it appeared in our own suite without anyone noticing. And it is the strongest available evidence that the hazard is not hypothetical — a statement written by someone who knew this subsystem was silently reordered. I reordered the reads ahead of the writes so it still pins the DELETE deferral it exists for; that is the only existing test whose text changed.
Tests
Two parse-level (
fluree-db-cypher/tests/it_smoke.rs), two end-to-end (fluree-db-api/tests/it_cypher_seq_writes.rs), in delimited blocks naming #1873.Asserted on
receipt.flake_count, not on the absence of an error. Two of the rejected shapes committed before the fix, so a test asserting "the transaction succeeded" would have passed against the bug. Each rejection case records what it did before, including the 4-flakes-of-wrong-data and 0-flakes-silently cases.The rejection and still-works halves are paired, so a guard that rejected everything cannot pass. The acceptance half includes
MATCH (a) WITH a MERGE (b) SET b.x = 1specifically — the AST-identical case a kind-based guard would have broken.Non-vacuity proven from post-
cargo fmtsource: guard disabled, test targets touched, re-run — both rejection tests fail, both acceptance tests still pass.Twin surface: Cypher-local, checked three ways
The hoist is in the Cypher parser and
Updateisfluree_db_cypher::ast, which SPARQL and JSON-LD never construct. SPARQL UPDATE's AST isUpdateOperation(fluree-db-sparql/src/ast/update.rs:77), an enum of fixed-shape operations with no read/write bucketing —grep -rn 'read_clauses|write_clauses' fluree-db-sparql/src/returns nothing, so there is no ordering to lose. And the crate's other two clause loops have no write bucket. Not reachable from another surface.Performance
Nothing at execution. The guard is one
Option<SourceSpan>compared at parse time, once per clause, on a path that already allocates aVecper clause. No IR change, no plan change, no per-row cost.Gates
cargo fmt --all --checkclean;cargo clippy --all-targets --no-deps0 warnings onfluree-db-cypherandfluree-db-api(recompile confirmed, not reported clean from cache);cargo test -p fluree-db-cypher -p fluree-db-api65 binaries, 3773 passed, 0 failed, 119 ignored.--all-featuresclippy cannot run on this host — it pullsfluree-search-*→cxx, which fails against the local C++ toolchain. CI is the first place that variant executes.testsuite-sparqlis untouched and its separate fmt/clippy pair was not run.Deferred
Making the interleaved form actually work needs
Updateto carry a real clause sequence, which is a large change to the AST and both lowerings and should not ride a P2 bug fix.Follow-up: #1889