Pattern Comprehension - possible bug handling null variables

There is a strange behavior in Pattern Comprehension when using a null value in the where clause.

It can be reproduced with following examples from the Movies database:

This cypher query results in several Actors in the "coActors" Pattern Comprehension result:

OPTIONAL MATCH (tom)-[:ACTED_IN]->(movie:Movie)
WITH tom, movie, null as customValue
RETURN tom, movie.title, movie.released, [(movie)<-[:ACTED_IN]-(coActor:Person) WHERE coActor.name <> tom.name | coActor.name ] as coActors

If I add the condition OR coalesce(customValue,"") = "" to the WHERE-clause, wich should be true but that even doesn`t matter because it is OR-related, coActors returns always null (not even an empty array).

OPTIONAL MATCH (tom)-[:ACTED_IN]->(movie:Movie)
WITH tom, movie, null as customValue
RETURN tom, movie.title, movie.released, [(movie)<-[:ACTED_IN]-(coActor:Person) WHERE coActor.name <> tom.name OR coalesce(customValue,"") = "" | coActor.name ] as coActors

No matter what kind of operation is done with that variable, output is always null. Even OR customValue is null doesn't work. but OR null is null works.

Best
Reiner

You're getting this because tom isn't guaranteed to be a :Person node (it's matching to all nodes in your db), so it isn't guaranteed to have a name property.

You can add a coalesce() around the tom.name to remedy the issue:

WHERE coActor.name <> coalesce(tom.name, '')

It looks like you copied and pasted a snippet from another query, where tom was already matched to Tom Hanks, but since tom isn't bound to anything, and no label is supplied, it's matching to all nodes in your db, including nodes that are not :Persons.

Sorry, but that doesn`t touch the problem. My example might not have been perfect. Try this slightly modified example on the movie graph and you will see that coActors stays null. I now used pure MATCH and applied :Person label to tom and your proposed coalesce statement. The problem only occurs when using a null variable (here customValue) from outside the pattern comprehension within it. If removing OR customValue is null , it works

MATCH (tom:Person)-[:ACTED_IN]->(movie:Movie)
WHERE tom.name = "Tom Hanks"
WITH tom, movie, null as customValue
RETURN tom, movie.title, movie.released, 
[(movie)<-[:ACTED_IN]-(coActor:Person) 
   WHERE coActor.name <> coalesce(tom.name, '')  
   OR customValue is null 
| coActor.name ] as coActors

You also can try OR coalesce(customValue,'') = '' or any other statement involving customValue - this all should result in populated coActors as it is OR-related but instead always returns null.

Even if using just OR customValue returns null where it should throw an "expected Boolean" error in my opinion.

Now that is a weird one.

This looks like a Cypher bug to me, I'll raise the issue internally.

1 Like