Skip to content

Reject a Cypher read clause written after a write clause, which was silently reordered - #1886

Merged
bplatz merged 10 commits into
mainfrom
fix/cypher-clause-order-hoist
Nov 17, 2026
Merged

bplatz merged 10 commits into
mainfrom
fix/cypher-clause-order-hoist

Conversation

@aaj3f

@aaj3f aaj3f commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1873

Update, 2026-11-17

Brought up to date with main, and zonotope's four review points addressed: the guard tracks a bool, it_smoke.rs drops the lint allow for plain raw strings, the doc's "how many failed loudly" sentence matches its table, and the deliberately rejected MATCH (n) SET n.a = 1 WITH n SET n.b = 2 is 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 after main reworked the Cypher write path: CREATE (n:P {id: "c1"}) WITH n SET n.nm = "a" used to error, and now commits nothing, not even the CREATE. 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.

Update is 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 = 1 is handed to the engine as WITH n MERGE (n) SET n.x = 1 — a WITH projecting a variable nothing has bound yet.

It is three symptoms, not one, and only two of them are loud

Measured on a5fdba900, asserting on receipt.flake_count:

MERGE (n:P {id:"x"}) WITH n SET n.nm = "b"
  -> ERROR, blaming a policy that is not involved

MERGE (n:P {name:"Zed"}) WITH n.name AS nm SET n.marker = nm
  -> COMMITTED, 4 flakes: one marker per PRE-MERGE name, written 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.

MATCH (n) SET n.a = 1 WITH n SET n.b = 2
  -> worked (2 flakes) — see "the surprising row" below

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 reach transact_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 = 2 has no MERGE, so detect_sequential returns None and the statement never routes to the sequential driver — a guard in validate_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 Update exists the ordering is gone. MATCH (a) WITH a MERGE (b) SET b.x = 1 commits correctly today (3 flakes) and its AST is [Match, With] / [Merge, Set] — structurally identical to the hoisted MATCH (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_body and parse_procedure_call collect 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-change alone 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.

Written Before Now Write instead
MERGE (n) WITH n SET n.x = 1 error blaming a policy that was not involved clear error MATCH … WITH … MERGE … SET …, or split in two
MERGE (n {name:"Z"}) WITH n.name AS nm SET n.marker = nm committed wrong data clear error same
MATCH (n) SET n.a = 1 WITH n WHERE n.a = 1 SET n.b = 2 committed nothing, silently clear error split in two
CREATE (n:P {id: 1}) WITH n SET n.nm = "a" committed nothing, silently — not even the CREATE clear error CREATE (n:P {id: 1}) SET n.nm = "a"
MATCH (n) SET n.a = 1 WITH n SET n.b = 2 worked clear error MATCH (n) SET n.a = 1 SET n.b = 2

The 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 through SET and WHERE, 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:

`WITH` after a write clause is not supported — the clause order would be silently rearranged so that every read runs before every write, which changes what the statement does. Put the reads before the first write (`MATCH … WITH … MERGE … SET …`), or split this into two statements.

The guard found a hoisted statement in our own test suite

delete_in_chain_rejected_clearly (it_cypher_seq_writes.rs) used MERGE (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 = 1 specifically — the AST-identical case a kind-based guard would have broken.

Non-vacuity proven from post-cargo fmt source: 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 Update is fluree_db_cypher::ast, which SPARQL and JSON-LD never construct. SPARQL UPDATE's AST is UpdateOperation (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 a Vec per clause. No IR change, no plan change, no per-row cost.

Gates

cargo fmt --all --check clean; cargo clippy --all-targets --no-deps 0 warnings on fluree-db-cypher and fluree-db-api (recompile confirmed, not reported clean from cache); cargo test -p fluree-db-cypher -p fluree-db-api 65 binaries, 3773 passed, 0 failed, 119 ignored.

--all-features clippy cannot run on this host — it pulls fluree-search-* → cxx, which fails against the local C++ toolchain. CI is the first place that variant executes. testsuite-sparql is untouched and its separate fmt/clippy pair was not run.

Deferred

Making the interleaved form actually work needs Update to 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

`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.
@aaj3f aaj3f added bug Something isn't working as expected breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section P2 Scheduled: real but not urgent area:transact Staging, commit, import/bulk-import, novelty, retraction semantics labels Sep 18, 2026

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

I have a few minor nits, but 👍🏾

Comment thread docs/query/cypher.md Outdated
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:

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.

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.

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.

Addressed in 6330b44

Comment thread docs/query/cypher.md

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

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.

I don't think there's a test for the last one, but it's called out explicitly here.

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.

Addressed in 3cd7931

Comment thread fluree-db-cypher/src/parse/stmt.rs Outdated
// `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;

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.

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.

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.

Addressed in 43df393

Comment thread fluree-db-cypher/tests/it_smoke.rs Outdated
@@ -1,3 +1,5 @@
#![allow(clippy::needless_raw_string_hashes)]

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.

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"

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.

Addressed in 355e00a

@aaj3f aaj3f mentioned this pull request Oct 13, 2026
@bplatz

bplatz commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Brought up to date with main, and the four review points are addressed (each thread has its commit). The description has an Update, 2026-11-17 note at the top. On current main, CREATE … WITH … SET now commits nothing instead of erroring, so it's added to the table and the tests.

@bplatz
bplatz merged commit 0c827a4 into main Nov 17, 2026
21 checks passed
@bplatz
bplatz deleted the fix/cypher-clause-order-hoist branch November 17, 2026 17:31
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 P2 Scheduled: real but not urgent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A WITH after a MERGE is hoisted to before it, so MERGE … WITH … SET writes nothing and reports a misleading policy error

3 participants