Skip to content

Cypher CALL (a) { … } loses its correlation when a WITH inside the body drops the imported variable — returns a cross product #1894

Closed

Description

@aaj3f

Turned up while fixing something else in #1867, and kept out of that PR because it is a different defect that predates it.

A correlated CALL returns a cross product instead of one result per parent row, when a WITH inside the body projects the imported variable away. On four Person nodes:

MATCH (a:Person) CALL (a) { WITH a.name AS n RETURN n AS z } RETURN z
  -> 16 rows   (4 x 4)   — expected 4

Neo4j correlates on the imported a and returns 4, one name per person. We return every name for every person.

Which shapes break, and which do not

This is the useful part, because it is sharply bounded. Same fixture, five shapes:

Body Imported a after the WITH? Rows
WITH a.name AS n RETURN n AS z dropped 16
WITH a.name AS n, 1 AS k RETURN n AS z dropped 16
WITH a AS a RETURN a.name AS z kept 4
WITH a, a.name AS n RETURN n AS z kept 4
RETURN a.name AS z (no WITH) n/a 4

So the correlation is lost exactly when a WITH inside the body narrows the imported variable out of scope — and only then. Carry a through the WITH and it is correct; add an unrelated item alongside the dropped a and it is still broken. 4 × 4 is the signature you would expect if the body were being evaluated over every a for each outer a, rather than over its own.

What I have not established

The why. The table above is a measured description of which shapes break; it is not a traced mechanism, and I would rather not guess at one in an issue. The obvious reading is that the correlation key has to survive into the body's output for the subquery join to have something to join on, and a WITH that projects it away severs that — but I have not followed the lowering of a correlated CALL through a narrowing WITH to confirm it, so treat that as a hypothesis.

It is pre-existing: I first saw it in a test control while working on #1867, then measured it on a tree that has none of #1867's changes and got 16 there too, so it is not something that PR introduced.

Workaround until it is fixed

Carry the imported variable through the WITH:

MATCH (a:Person) CALL (a) { WITH a, a.name AS n RETURN n AS z } RETURN z
  -> 4 rows, correct

Why it is worth fixing rather than documenting

It is a silent wrong answer — too many rows, no error — on a shape that is perfectly ordinary Cypher. Narrowing scope inside a subquery body with WITH is exactly what WITH is for, and nothing about the result tells you it is a cross product unless you already know how many rows to expect.

Activity

  1. added
    bug
    P2
    area:query
    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

    P2area:querybug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions