Skip to content

[Cypher] EXISTS/COUNT/COLLECT re-parse their body once per outer row; correlate the AST instead of the text #5657

Closed as not planned

Description

@lvca

Recorded while closing #5626 (PR #5642), and called out as out-of-scope there by both the PR and its review.

ExistsExpression, CountExpression and CollectExpression keep their body as a String and run it through context.getDatabase().query("opencypher", ...) once per outer row. Each of those calls lexes, parses, semantically validates, plans and executes the body from scratch. A 10k-row outer query with an EXISTS { } guard pays 10k parses of the same body text.

It is not cached, and it cannot be with the current shape: what runs is not the body as written but the body after CorrelatedSubqueryRewriter.correlate() has spliced the outer row into it - an injected MATCH (v), an id(v) = $param condition merged into an existing WHERE, a leading WITH $param AS v - so the text differs row by row and misses CypherStatementCache every time.

#5626 removed the reason the AST was not available: the three expressions now carry their body as a CypherStatement (getParsedSubquery()), built off the sub-tree ANTLR already produced.

Suggested fix. Correlate the body as an AST rather than as text: bind the outer row into parameters against the parsed statement and execute that, so the body is parsed once per query rather than once per row.

The second prize is the larger one. CorrelatedSubqueryRewriter decides where a clause begins, where a WHERE body ends, and whether a keyword is a clause or an identifier, all by scanning the text with a keyword table and a bracket-depth counter. Every one of these came out of that scan getting it wrong:

Its own Javadoc still records two known blind spots: a keyword inside a comment is read as a clause, and WITH 1 AS set RETURN set is lexically indistinguishable from SET n.x = 1. Both are unfixable by a text scan and both disappear if the decision is made on the parse tree.

Note the failure mode this class of bug has: all three expressions catch Exception and answer with their neutral value - false, 0, [] - logging at FINE. A corrupted body does not raise; it silently answers "no". That is how #5464 stayed invisible, and it is worth deciding separately (see the sibling issue) whether a subquery that fails should keep being reported as a subquery that does not match.

Activity

  1. lvca commented on Aug 1, 2026

    @lvca
    MemberAuthor

    Folded into #5656, which now carries all three #5626 follow-ups together - they are three consequences of one design decision (a subquery body held as text and run as a standalone statement per row) rather than three unrelated defects. This one is section 2 there.

  2. added this to the 26.8.1 milestone on Aug 1, 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions