Issue 1:
I'm trying to design a single query that creates a node including input/output links with possible linking to existing nodes. Creation stages are separated by cardinality reduction clauses using LIMIT 1. This method prevents creation of certain nodes in preceding CREATE clause, however.

Neo4j version: 3.4.6

Operating system: macOs 10.13.6

API/Driver: Cypher in neo4j browser

Steps to reproduce

Create a structure that next step is going to build upon:

CREATE (m:Move {id: "m1"})
WITH m
UNWIND [{i: 0}, {i: 1}, {i: 2}] as p
CREATE (m)-[:OUT]->(:Position {id: p.i})

Tie to output positions from previous step:

CREATE (m:Move {id: "m2"})

WITH m
UNWIND [{pid: 0}, {pid: 1}] as i
MATCH (p:Position {id: i.pid})
CREATE (p)-[:IN]->(m)

WITH m LIMIT 1
UNWIND [{pid: 2, addr: "street"}] as i
MATCH (p:Position {id: i.pid})
MERGE (p)-[:IS_A]->(a:Place {id: i.addr})
CREATE (p)-[:IN]->(m)

Expected behavior

All positions are linked to m2 by IN edges.

Actual behavior

Position 1 is not linked.

The problem seems to be in cardinality reducing WITH m LIMIT 1 because it prevents creation of the link according to second map in first unwind (just to test – link is created when using WITH m LIMIT 2 but that leads to duplicate link from second unwind of course). I could use WITH distinct m but that is more expensive construct.

I do not think that WITH clause should have this "backward" effect so I consider it a bug for now. I would welcome an explanation if I'm wrong :)

Thanks!

Issue 2:
Neo4j version: 5.8.0-enterprise

Operating system: Ubuntu 20.04

API/Driver: Cypher

Steps to reproduce

Hello! While testing Neo4j in the movie graph, I came across an issue related to duplicated path patterns. Allow me to explain in a clearer manner:

In the second query, the expected result should be 506. This is because all pairs (n0, n1) in the first query, satisfy the pattern defined in the second query.

Similarly, in the third query, the expected result should also be 506. However, currently, it returns 64, which is the number of pairs (n0, n1) that have at least two edges in the movie graph.

The issue here is that there is no constraint requiring the two relationships to be identical, and thus the expected result should still be 506.

While I am uncertain if this behavior is a bug in Neo4j, it is worth noting that other graph databases (such as Memgraph) attempt to address this by returning an exception Redeclaring variable: r1 for the second query.

Would it is possible for you to further confirm and investigate this issue?

One possible solution to address this issue is to implement a step during query compilation that removes duplicated path patterns for each pattern, and another solution is to throw an exception like other GDBs when encountering a redeclared variable for relation.

//:play movie-graph
MATCH (n0)-[r1]-(n1) RETURN COUNT (*)
=> 506
MATCH (n0)-[r1]-(n1), (n1)-[r1]-(n0) RETURN COUNT (*)
=> 0
MATCH (n0)-[]-(n1), (n1)-[]-(n0) RETURN COUNT (*)
=>64

Expected behavior

MATCH (n0)-[r1]-(n1), (n1)-[r1]-(n0) RETURN COUNT (*)
=> 506
MATCH (n0)-[]-(n1), (n1)-[]-(n0) RETURN COUNT (*)
=> 506

or

MATCH (n0)-[r1]-(n1), (n1)-[r1]-(n0) RETURN COUNT (*)
=> Exception: Redeclaring variable: r1.
MATCH (n0)-[]-(n1), (n1)-[]-(n0) RETURN COUNT (*)
=> 506