Unexpected behavior combining collections in Cypher

拥有回忆 提交于 2019-12-23 14:04:11

问题


Using http://console.neo4j.org as a sandbox, I have come across the following unexpected behavior:

Statement 1 - Returns 1 row with a collection containing Neo Node

MATCH (n:Crew) 
WHERE n.name="Neo" 
WITH COLLECT(n) AS c1
WITH c1+[] AS c2
RETURN c2

Statement 2 - Returns 0 rows (unexpected)

MATCH (n:Crew) 
WHERE n.name="Neo" 
WITH COLLECT(n) AS c1
MATCH (n:Crew) 
WHERE n.name="NoOne"
WITH c1+COLLECT(n) AS c2
RETURN c2

Statement 3 - Returns 1 row containing an empty collection

MATCH (n:Crew) 
WHERE n.name="NoOne"
WITH COLLECT(n) AS c1
RETURN c1

I fail to see why Statement 2 is not returning the same result as Statement 1, because it should return a collection containing the Neo node, just like in Statement 1.
Statement 3 shows that the second MATCH in Statement 2 should be resulting in an empty collection.

Is this behavior expected in Cypher? If that's the case, I'd be happy about a small explanation to help me understand this behavior.


回答1:


I've run into this exact behavior before, and it is very frustrating. The issue is with the second MATCH clause in Query 2: if an existing result row (in this case, your single row with c1) doesn't return any results for a MATCH, that row will be dropped completely after that MATCH clause, even though that MATCH on its own (without the pre-existing result row) returns an empty collection. If you convert it to an OPTIONAL MATCH you'll be able to keep your result row when there are no matches.

UPDATE: See below for a more thorough analysis, but the tl,dr is that the second COLLECT(n) in Statement 2 does return an empty list, just like in Statement 3; however, the whole clause WITH c1+COLLECT(n) AS c2 returns no rows, because there are no rows with a c1 value after the second MATCH.




回答2:


I can't quite think of the right explanation for why the 2nd query doesn't do what you expect, but if you have multiple optional matches that you want to chain together then you could use an OPTIONAL MATCH to do that:

OPTIONAL MATCH (n:Crew) 
WHERE n.name="Neo" 
WITH COLLECT(n) AS c1
OPTIONAL MATCH (n:Crew) 
WHERE n.name="NoOne"
WITH c1+COLLECT(n) AS c2
RETURN c2


来源:https://stackoverflow.com/questions/39873935/unexpected-behavior-combining-collections-in-cypher

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!