Strange behavior of path variables with CALL {} subquery

Hi, I'm getting an error with the following query, and I don't understand why. Is it a bug?

// Get all movies that rdj acted in, including the franchise of each movie
MATCH (person:Person {shortId: "rdj"})

OPTIONAL MATCH _path1 = (person)-[_rel1:ACTED_IN]->(_movie1:Movie)

// If this is uncommented, there is no error:
// WITH person, _movie1, _path1, _rel1

    WITH _movie1
    OPTIONAL MATCH (_movie1)-[:FRANCHISE_IS]->(_moviefranchise1:MovieFranchise)
    RETURN _moviefranchise1 LIMIT 1
WITH person, _movie1, _path1, _rel1, _moviefranchise1 {.uuid, .name} AS _franchise1
WITH person, _movie1, _path1, _rel1, _franchise1 ORDER BY _movie1.year DESC
WITH person, collect(_movie1 {.title, .year, franchise: _franchise1}) AS _movies1

RETURN AS name, _movies1 AS movies ORDER BY

I think you can ignore the second half of my query. The problem is in the first part, with the path variable _path1. If I run the query as shown above, I get this syntax error:

Variable `_path1` not defined (line 10, column 22 (offset: 299))
"    LIMIT 1"

The line in question does not reference the _path1 variable anyways.

If I uncomment the additional WITH statement that is shown commented out, the query runs successfully. But why is that needed?

(Note: I know the _path1 variable is not being used here, but I'm generating these cypher queries programmatically and I found that I need to include the path variable to avoid some edge cases with similar queries when combined with collect())

This is on Neo4j 4.2

P.S. this doesn't really affect me, but I noticed that if I keep the query as written (with WITH still commented out, and change the subquery to

    WITH _movie1, _path1

Then it gives a strange error:

Importing WITH should consist only of simple references to outside variables. Aliasing or expressions are not supported. (line 6, column 5 (offset: 137))
"  WITH _movie1 AS _movie1, _path1 AS _path1"

where the cypher shown in the error message doesn't even match the query I wrote.

That is very strange. I've been playing around with simpler variants of your query, trying to figure out why it might be throwing that error but I've not even very successful, so I think you are right that it seems like a bug.

Could you post it as an issue?

And for the Cypher generator, you could try using WITH * before the CALL {} so that it will definitely pass through everything that's in the current scope.

(I think it should be doing that anyway, but it doesn't seem to be!)

Thanks for checking it out, @mark.needham!


Yep, that's what I've done for now, and it has worked around the problem.


1 Like