Neo4j: WITH does not limit scope to property

Created on 7 Sep 2017  ยท  3Comments  ยท  Source: neo4j/neo4j

  • Neo4j version: 3.2.1
  • Operating system: Ubuntu 16.04
  • API/Driver: web UI

Steps to reproduce

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.

Expected behavior

Neither query should compile.

Actual behavior

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"
       ^
3.2 cypher question

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.

All 3 comments

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 *
Was this page helpful?
0 / 5 - 0 ratings

Related issues

noorbakerally picture noorbakerally  ยท  3Comments

Shib4 picture Shib4  ยท  5Comments

cybersam picture cybersam  ยท  3Comments

LWprogramming picture LWprogramming  ยท  4Comments

BrainAnnex picture BrainAnnex  ยท  4Comments