zihaozeng0021 opened a new issue, #2619:
URL: https://github.com/apache/age/issues/2619

   # MERGE full pattern with property maps does not match pre-existing nodes 
and duplicates them
   
   ### Configuration
   
   Docker Image: `apache/age:release_PG18_1.8.0`
   Image Digest: 
`sha256:47a0b054c3663a3e64a25fc0c5a982f1b58348019274b326a691cdf77f78eb58`
   PostgreSQL Version: `18.6`
   Apache AGE Extension Version: `1.8.0`
   Operating System: WSL2, Ubuntu 24.04.1 LTS
   Installation Method: Docker
   Query Interface: `psql` (`SELECT * FROM cypher('xgdb', $$ ... $$) AS (...)`)
   
   ---
   
   ### Description
   
   A `MERGE` whose full pattern carries property maps
   (`MERGE (src {k: 1})-[rel:SD]->(dest {k: 2})`) does not match pre-existing 
nodes with those
   properties. Instead the engine treats the whole pattern as absent, creates 
two duplicate nodes
   (node count grows from 2 to 4), and creates the edge between the new 
duplicates. The property
   maps are simply not used for matching.
   
   ---
   
   ### Steps to reproduce
   
   1. Start the AGE container and connect with `psql`.
   2. Create two `:Base` vertices with distinct properties, run the 
full-pattern `MERGE`, and
      inspect the graph:
   
   ```sql
   SELECT * FROM cypher('xgdb', $$ CREATE (:Base {k: 1}) $$) AS (r agtype);
   SELECT * FROM cypher('xgdb', $$ CREATE (:Base {k: 2}) $$) AS (r agtype);
   SELECT * FROM cypher('xgdb', $$ MERGE (src {k: 1})-[rel:SD]->(dest {k: 2}) 
ON CREATE SET rel.v = -12 ON MATCH SET rel.v = 34 $$) AS (r agtype);
   SELECT * FROM cypher('xgdb', $$ MATCH (n) RETURN count(n) AS nodes $$) AS 
(nodes agtype);
   SELECT * FROM cypher('xgdb', $$ MATCH ()-[r:SD]->() RETURN count(r) AS 
edges, collect(r.v) AS vs $$) AS (edges agtype, vs agtype);
   ```
   
   ---
   
   ### Expected behaviour
   
   openCypher `MERGE` semantics: when the pattern carries properties, matching 
considers them.
   The existing `k=1` and `k=2` vertices must be found, only the edge is 
created, and the `ON MATCH`
   branch runs: `nodes = 2`, `edges = 1`, `vs = [34]`.
   
   ---
   
   ### Actual behaviour
   
   Both fresh-container runs duplicate the endpoints:
   
   ```text
    nodes
   -------
    4
   (1 row)
   
    edges |  vs
   -------+-------
    1     | [-12]
   (1 row)
   ```
   
   The whole pattern was treated as not present (creation path, `ON CREATE` 
value `-12`). The
   UNWIND two-element variant of the same shape behaves identically (nodes 2 -> 
4, one `S` edge at
   `-12`; Q3). Note the contrast with the within-query ON-branch case:
   there an entity created by the same query is found but takes the wrong ON 
branch; here
   pre-existing entities are not matched at all. Backend alive 
(`RestartCount=0`), no FATAL/PANIC.
   
   ---
   
   ### Minimal reproduction
   
   ```sql
   SELECT * FROM cypher('xgdb', $$ MERGE (src {k: 1})-[rel:SD]->(dest {k: 2}) 
ON CREATE SET rel.v = -12 ON MATCH SET rel.v = 34 $$) AS (r agtype);
   SELECT * FROM cypher('xgdb', $$ MATCH (n) RETURN count(n) AS nodes $$) AS 
(nodes agtype);
   ```
   
   Two pre-existing vertices with `k: 1` and `k: 2`; the count becomes 4 
instead of staying 2.
   
   ---
   
   ### Why this appears to be a bug
   
   Property maps in a `MERGE` pattern are part of the match specification per 
openCypher. Ignoring
   them silently duplicates data on every execution of an idempotent-looking 
statement, which is
   exactly the class of corruption `MERGE` exists to prevent. apache/age#1709 
(open) documents a
   sibling defect (incomplete subsequent-vertex properties in `UNWIND` + 
`MERGE`), but the
   observable behavior differs, so a separate report is warranted.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to