3
votes

For illustrating this issue, create thousand nodes labeled z having incrementing numeric attribute zid.

FOREACH (i IN range(1, 1000)| CREATE (z:z { zid: i }));

Now find a node using random zid value between 1 and 1000.

MATCH (n:z { zid: round(rand()*1000)}) 
RETURN n;

The above cypher returns inconsistent results, sometimes no nodes are returned, sometimes multiple nodes are returned.

Tweaking the cypher as follows yields consistent results.

WITH round(rand()*1000) AS x
MATCH (n:z { zid: x })
RETURN x, n;

What is wrong with the first cypher query ?

1
Which values do the multiple nodes have that are returned? No nodes are returned if rand()*1000 evaluates to zero as you didn't create any nodes with id zero.Michael Hunger

1 Answers

3
votes

The reason why you are receiving inconsistent results with the first query has to do with how Neo4j evaluates Cypher queries. The function round(rand()*1000) is evaluated for each of the items within the label index for z when using WHERE or concise syntax. When you use the WITH clause, the function is evaluated once.

That being said, this looks like a bug that is specific to the rand() function.