neo4j

Neo4j keeping bound variables after unwind with comprehension list that returns null


I found this old bug listing in github, and mainly seeking confirmation that this is a feature. Is there a workaround? https://github.com/neo4j/neo4j/issues/5851

MATCH (rememberMe)
WITH rememberMe
UNWIND [(rememberMe)-[:hates]->(icecream) | icecream]
 +  [(rememberMe)-[:vomits]->(cookies) | cookies]
 AS noSuchThing
RETURN rememberMe

rememberMe is lost when noSuchThing is null

Is there a way to get around this? Can't seem to get the CASE WHEN NULL to work with sum of comprehension list.


Solution

  • Currently the UNWIND behavior on an empty list is expected. UNWIND emits a row per element in a list, so if there are no list elements, no rows will be emitted, effectively wiping out the original row.

    As you mentioned in the documentation there is a workaround using CASE to use a [null] list in place of an empty list so you retain a single list element and the original row remains.

    For your case, you'll want to slow down a bit, you're trying to do many things at once, so separate the creation of the list from the UNWIND and you should be able to apply the workaround:

    MATCH (rememberMe)
    WITH rememberMe, [(rememberMe)-[:hates]->(icecream) | icecream]
     +  [(rememberMe)-[:vomits]->(cookies) | cookies]
     AS list
    UNWIND CASE WHEN size(list) = 0 THEN [null] ELSE list END as noSuchThing
    RETURN rememberMe