Run these two queries:
1.
MATCH (n:Person)
WITH n.name AS name
WHERE n.age > 25
RETURN *
2.
MATCH (n:Person)
WITH n.name AS name
WITH name AS name
WHERE n.age > 25
RETURN *
These queries should behave the same, as only difference is the WITH name AS name line, which in theory is an identity operation.
Neither query should compile.
The first query compiles. For the simple dataset of
CREATE (p:Person {name: 'John', age: 32})
it returns
โโโโโโโโ
โ"name"โ
โโโโโโโโก
โ"John"โ
โโโโโโโโ
The second query fails (correctly):
Variable `n` not defined (line 4, column 7 (offset: 61))
"WHERE n.age > 25"
^
I believe the behavior you observed is correct, not a bug.
The WHERE is a part of the WITH, not something separate from it, so at the point that the WITH is being executed, both it and its associated WHERE clause still have access to variables before the WITH. They should both have access to the same scope.
I agree though it can be confusing, but to me the 1st query should be correct.
In the same kind of constructions, you have
MATCH (n:Person) RETURN n.name AS name ORDER BY n.age DESC
@szarnyasg This is working according to design. The WITH clause is a horizon, and only keeps the explicitly projected fields. Use WITH * to get the semantics outlined in the description:
MATCH (n:Person)
WITH *, n.name AS name
WITH name AS name
WHERE n.age > 25
RETURN *
Most helpful comment
I believe the behavior you observed is correct, not a bug.
The WHERE is a part of the WITH, not something separate from it, so at the point that the WITH is being executed, both it and its associated WHERE clause still have access to variables before the WITH. They should both have access to the same scope.