Skip to content

Cypher: Update cannot represent a read clause between two write clauses, so interleaved statements are rejected rather than executed #1889

Open

Description

@aaj3f

Follow-up from #1873. That fix takes the right immediate call — reject a read clause written after a write clause, because the parser was silently hoisting it in front of every write and executing a different statement. This is the other half: making the interleaved form actually run.

What is in the way

Update (fluree-db-cypher/src/ast/stmt.rs:109) is two vectors:

pub struct Update {
    pub read_clauses: Vec<ReadClause>,
    pub write_clauses: Vec<WriteClause>,
    pub return_clause: Option<ReturnClause>,
    pub span: SourceSpan,
}

There is nowhere to record that a clause came after a write, so the parser's loop (parse/stmt.rs:47-120) buckets by keyword and the interleaving is gone by the time the struct exists. Every downstream consumer then assumes reads-then-writes, and most of them are right to, because that is what the type says.

Making MATCH … MERGE … WITH … SET … work means giving Update a real clause sequence — one Vec<Clause> where Clause is read-or-write — and then teaching both lowerings to walk it in order. That is not a small change:

  • fluree-db-transact/src/lower_cypher_update.rs builds a single Txn from read_clauses then write_clauses (lower_read_clauses, then the write loop). Ordered clauses mean interleaving where-pattern accumulation with write-template emission, and bound_vars narrowing would have to move with it rather than happening once.
  • fluree-db-api/src/cypher_seq.rs stages clause-by-clause already, which is the closer fit — but read_prefix_query (:1503) builds the row table from update.read_clauses as a block, up front, and detect_sequential (:88) gates on read_clauses being non-empty. Both assume the split.
  • validate_members (:1338) and the visible_vars walk (:1436) both iterate the two vectors independently.

Why it is worth doing eventually

MATCH … SET … WITH … SET … and MERGE … WITH … SET … are ordinary openCypher, and Neo4j runs them. A user porting a multi-stage write hits the rejection immediately, and the workaround — split into two statements — gives up atomicity, which is a real cost rather than a cosmetic one.

It would also let MERGE … WITH n SET … work, which is the shape #1873 was filed about and is the natural way to express "upsert this, then derive something from it".

Why it is not in #1886

Two reasons, and the second is the one that decided it. It is a large change to a type every Cypher write path depends on, riding a P2 bug fix. And the rejection has to land first regardless: until the sequence exists, the statements are being silently miscompiled, and one shape commits wrong data while another commits nothing. Rejecting is correct on its own timeline.

Scope note

This is Cypher-only. 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 and so no ordering to lose; JSON-LD has no clause-sequence grammar. Verified by grep — read_clauses/write_clauses appear nowhere in fluree-db-sparql/src/.

Activity

  1. added
    enhancement
    P3
    area:transact
    on Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area:transactenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions