UNWIND没有在neo4j中返回

时间:2017-04-03 09:17:02

标签: performance neo4j

考虑以下CQL查询

MATCH (n:Label1) WITH n
OPTIONAL MATCH (n)-[r:REL_1]-(:Label2 {id: 5})
WHERE r is NULL OR r.d < 12345 OR (r.d = 12345 OR r.c < 2)
WITH n,r LIMIT 100
WITH COLLECT({n: n, r: r}) AS rows
MERGE (c:Label2 {id: 5})
WITH c,
[b IN rows WHERE  b.r.d IS NULL OR b.r.d < 12345] AS null_less_rows,
[c IN rows WHERE (c.r.d = 12345 AND c.r.c < 2)] AS other_rows
WITH null_less_rows, other_rows, c, null_less_rows+other_rows AS rows, size(null_less_rows+other_rows) AS count
UNWIND null_less_rows AS null_less_row
MERGE(s:Label1 {id: null_less_row.n.id})
MERGE(s)-[:REL_1 {d: 12345, c: 1}]->(c)
WITH DISTINCT other_rows, c, rows, count
UNWIND other_rows AS other_row
MATCH(s:Label1 {id: other_row.n.id})-[str:REL_1]->(c) SET str.c = str.c + 1
WITH rows, count
RETURN rows, count

当我执行查询时,它应该返回行和计数(根据查询)。但不是返回行,而是计算它给出结果语句。

Set 200 properties, created 100 relationships, statement completed in 13 ms.

查询结构是否有问题或者UNWIND子句使用不当有问题。

2 个答案:

答案 0 :(得分:0)

如果other_rows为null或为空,UNWIND将不会产生任何行。

您可以通过以下方式解决问题:

UNWIND case coalesce(size(other_rows),0) when 0 then [null] else other_rows end 
   as other_row

答案 1 :(得分:0)

除了Micheael饥饿答案

UNWIND (CASE other_rows WHEN [] then [{n:{id: -2}}] else other_rows end) AS other_row

当我操作数组的值而不是null时,我需要添加额外的条件,以便它不会抛出任何错误信息。

这两个案例的apllies(other_rows,null_less_rows)