Skip to content

Commit 47aa24a

Browse files
committed
Fix Issue 1709 - MERGE creates incomplete vertices after the first one (apache#1721)
Fixed issue 1709 where MERGE appears to create incomplete vertices after the first one. However, the real issue was that SET wasn't seeing the newly created tuples from MERGE. This was due to an incorrect cid when performing the tuple insert in MERGE. The issue was that the cid was being overwritten in the function insert_entity_tuple_cid. Once this was corrected, everything worked fine. Added regression tests.
1 parent b9341f2 commit 47aa24a

File tree

3 files changed

+183
-6
lines changed

3 files changed

+183
-6
lines changed

regress/expected/cypher_merge.out

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,15 +1529,140 @@ SELECT * FROM cypher('issue_1691', $$MATCH (u) DELETE u $$) AS (a agtype);
15291529
---
15301530
(0 rows)
15311531

1532+
--
1533+
-- Issue 1709 - MERGE creates incomplete vertices after the first one
1534+
-- This is actually an issue with MERGE not using the correct command id
1535+
--
1536+
SELECT * FROM create_graph('issue_1709');
1537+
NOTICE: graph "issue_1709" has been created
1538+
create_graph
1539+
--------------
1540+
1541+
(1 row)
1542+
1543+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) RETURN u $$) AS (u agtype);
1544+
u
1545+
---
1546+
(0 rows)
1547+
1548+
SELECT * FROM cypher('issue_1709', $$ UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
1549+
MERGE (v:PERSON {first: map.first})
1550+
SET v=map
1551+
RETURN v $$) AS (v agtype);
1552+
v
1553+
-----------------------------------------------------------------------------------------------------
1554+
{"id": 844424930131969, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
1555+
{"id": 844424930131970, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
1556+
(2 rows)
1557+
1558+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) RETURN u $$) AS (u agtype);
1559+
u
1560+
-----------------------------------------------------------------------------------------------------
1561+
{"id": 844424930131969, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
1562+
{"id": 844424930131970, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
1563+
(2 rows)
1564+
1565+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) DELETE u $$) AS (a agtype);
1566+
a
1567+
---
1568+
(0 rows)
1569+
1570+
SELECT * FROM cypher('issue_1709', $$ UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark', middle: 'jim'}, {first: 'jane', last: 'doe'}, {first: 'ned', last: 'flanders'}, {first: 'wanda', last: 'cosmo'}] AS map
1571+
MERGE (v:PERSON {first: map.first})
1572+
SET v=map
1573+
RETURN v $$) AS (v agtype);
1574+
v
1575+
----------------------------------------------------------------------------------------------------------------------
1576+
{"id": 844424930131971, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
1577+
{"id": 844424930131972, "label": "PERSON", "properties": {"last": "stark", "first": "ned", "middle": "jim"}}::vertex
1578+
{"id": 844424930131973, "label": "PERSON", "properties": {"last": "doe", "first": "jane"}}::vertex
1579+
{"id": 844424930131972, "label": "PERSON", "properties": {"last": "flanders", "first": "ned"}}::vertex
1580+
{"id": 844424930131974, "label": "PERSON", "properties": {"last": "cosmo", "first": "wanda"}}::vertex
1581+
(5 rows)
1582+
1583+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) RETURN u $$) AS (u agtype);
1584+
u
1585+
--------------------------------------------------------------------------------------------------------
1586+
{"id": 844424930131971, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
1587+
{"id": 844424930131973, "label": "PERSON", "properties": {"last": "doe", "first": "jane"}}::vertex
1588+
{"id": 844424930131972, "label": "PERSON", "properties": {"last": "flanders", "first": "ned"}}::vertex
1589+
{"id": 844424930131974, "label": "PERSON", "properties": {"last": "cosmo", "first": "wanda"}}::vertex
1590+
(4 rows)
1591+
1592+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) DELETE u $$) AS (a agtype);
1593+
a
1594+
---
1595+
(0 rows)
1596+
1597+
SELECT * FROM cypher('issue_1709', $$ UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
1598+
MERGE (u: PERSON {last: map.last})-[e:KNOWS]->(v:PERSON {first: map.first})
1599+
SET v=map
1600+
RETURN u,e,v $$) AS (u agtype, e agtype, v agtype);
1601+
u | e | v
1602+
-------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------
1603+
{"id": 844424930131975, "label": "PERSON", "properties": {"last": "snow"}}::vertex | {"id": 1125899906842625, "label": "KNOWS", "end_id": 844424930131976, "start_id": 844424930131975, "properties": {}}::edge | {"id": 844424930131976, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
1604+
{"id": 844424930131977, "label": "PERSON", "properties": {"last": "stark"}}::vertex | {"id": 1125899906842626, "label": "KNOWS", "end_id": 844424930131978, "start_id": 844424930131977, "properties": {}}::edge | {"id": 844424930131978, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
1605+
(2 rows)
1606+
1607+
SELECT * FROM cypher('issue_1709', $$ MATCH (u)-[e]->(v) RETURN u,e,v $$) AS (u agtype, e agtype, v agtype);
1608+
u | e | v
1609+
-------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------
1610+
{"id": 844424930131975, "label": "PERSON", "properties": {"last": "snow"}}::vertex | {"id": 1125899906842625, "label": "KNOWS", "end_id": 844424930131976, "start_id": 844424930131975, "properties": {}}::edge | {"id": 844424930131976, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
1611+
{"id": 844424930131977, "label": "PERSON", "properties": {"last": "stark"}}::vertex | {"id": 1125899906842626, "label": "KNOWS", "end_id": 844424930131978, "start_id": 844424930131977, "properties": {}}::edge | {"id": 844424930131978, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
1612+
(2 rows)
1613+
1614+
SELECT * FROM cypher('issue_1709', $$ MATCH ()-[e]->() DELETE e $$) AS (a agtype);
1615+
a
1616+
---
1617+
(0 rows)
1618+
1619+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) DELETE u $$) AS (a agtype);
1620+
a
1621+
---
1622+
(0 rows)
1623+
1624+
SELECT * FROM cypher('issue_1709', $$ UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
1625+
MERGE (u: PERSON {last: map.last})-[e:KNOWS]->(v:PERSON {first: map.first})
1626+
SET u=map SET v=map
1627+
RETURN u,e,v $$) AS (u agtype, e agtype, v agtype);
1628+
u | e | v
1629+
-----------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------
1630+
{"id": 844424930131979, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex | {"id": 1125899906842627, "label": "KNOWS", "end_id": 844424930131980, "start_id": 844424930131979, "properties": {}}::edge | {"id": 844424930131980, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
1631+
{"id": 844424930131981, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex | {"id": 1125899906842628, "label": "KNOWS", "end_id": 844424930131982, "start_id": 844424930131981, "properties": {}}::edge | {"id": 844424930131982, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
1632+
(2 rows)
1633+
1634+
SELECT * FROM cypher('issue_1709', $$ MATCH (u)-[e]->(v) RETURN u,e,v $$) AS (u agtype, e agtype, v agtype);
1635+
u | e | v
1636+
-----------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------
1637+
{"id": 844424930131979, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex | {"id": 1125899906842627, "label": "KNOWS", "end_id": 844424930131980, "start_id": 844424930131979, "properties": {}}::edge | {"id": 844424930131980, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
1638+
{"id": 844424930131981, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex | {"id": 1125899906842628, "label": "KNOWS", "end_id": 844424930131982, "start_id": 844424930131981, "properties": {}}::edge | {"id": 844424930131982, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
1639+
(2 rows)
1640+
1641+
SELECT * FROM cypher('issue_1709', $$ MATCH ()-[e]->() DELETE e $$) AS (a agtype);
1642+
a
1643+
---
1644+
(0 rows)
1645+
1646+
-- clean up
1647+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) DELETE u $$) AS (a agtype);
1648+
a
1649+
---
1650+
(0 rows)
1651+
15321652
--
15331653
-- clean up graphs
15341654
--
1535-
SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtype);
1655+
SELECT * FROM cypher('cypher_merge', $$ MATCH (n) DETACH DELETE n $$) AS (a agtype);
15361656
a
15371657
---
15381658
(0 rows)
15391659

1540-
SELECT * FROM cypher('issue_1630', $$MATCH (n) DETACH DELETE n $$) AS (a agtype);
1660+
SELECT * FROM cypher('issue_1630', $$ MATCH (n) DETACH DELETE n $$) AS (a agtype);
1661+
a
1662+
---
1663+
(0 rows)
1664+
1665+
SELECT * FROM cypher('issue_1709', $$ MATCH (n) DETACH DELETE n $$) AS (a agtype);
15411666
a
15421667
---
15431668
(0 rows)
@@ -1595,6 +1720,18 @@ NOTICE: graph "issue_1691" has been dropped
15951720

15961721
(1 row)
15971722

1723+
SELECT drop_graph('issue_1709', true);
1724+
NOTICE: drop cascades to 4 other objects
1725+
DETAIL: drop cascades to table issue_1709._ag_label_vertex
1726+
drop cascades to table issue_1709._ag_label_edge
1727+
drop cascades to table issue_1709."PERSON"
1728+
drop cascades to table issue_1709."KNOWS"
1729+
NOTICE: graph "issue_1709" has been dropped
1730+
drop_graph
1731+
------------
1732+
1733+
(1 row)
1734+
15981735
--
15991736
-- End
16001737
--

regress/sql/cypher_merge.sql

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,18 +722,58 @@ SELECT * FROM cypher('issue_1691', $$ UNWIND ["foo", "bar", "foo", "foo", "bar"]
722722
SELECT * FROM cypher('issue_1691', $$MATCH ()-[e]->() DELETE e $$) AS (a agtype);
723723
SELECT * FROM cypher('issue_1691', $$MATCH (u) DELETE u $$) AS (a agtype);
724724

725+
--
726+
-- Issue 1709 - MERGE creates incomplete vertices after the first one
727+
-- This is actually an issue with MERGE not using the correct command id
728+
--
729+
SELECT * FROM create_graph('issue_1709');
730+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) RETURN u $$) AS (u agtype);
731+
732+
SELECT * FROM cypher('issue_1709', $$ UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
733+
MERGE (v:PERSON {first: map.first})
734+
SET v=map
735+
RETURN v $$) AS (v agtype);
736+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) RETURN u $$) AS (u agtype);
737+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) DELETE u $$) AS (a agtype);
738+
739+
SELECT * FROM cypher('issue_1709', $$ UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark', middle: 'jim'}, {first: 'jane', last: 'doe'}, {first: 'ned', last: 'flanders'}, {first: 'wanda', last: 'cosmo'}] AS map
740+
MERGE (v:PERSON {first: map.first})
741+
SET v=map
742+
RETURN v $$) AS (v agtype);
743+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) RETURN u $$) AS (u agtype);
744+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) DELETE u $$) AS (a agtype);
745+
746+
SELECT * FROM cypher('issue_1709', $$ UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
747+
MERGE (u: PERSON {last: map.last})-[e:KNOWS]->(v:PERSON {first: map.first})
748+
SET v=map
749+
RETURN u,e,v $$) AS (u agtype, e agtype, v agtype);
750+
SELECT * FROM cypher('issue_1709', $$ MATCH (u)-[e]->(v) RETURN u,e,v $$) AS (u agtype, e agtype, v agtype);
751+
SELECT * FROM cypher('issue_1709', $$ MATCH ()-[e]->() DELETE e $$) AS (a agtype);
752+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) DELETE u $$) AS (a agtype);
753+
754+
SELECT * FROM cypher('issue_1709', $$ UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
755+
MERGE (u: PERSON {last: map.last})-[e:KNOWS]->(v:PERSON {first: map.first})
756+
SET u=map SET v=map
757+
RETURN u,e,v $$) AS (u agtype, e agtype, v agtype);
758+
SELECT * FROM cypher('issue_1709', $$ MATCH (u)-[e]->(v) RETURN u,e,v $$) AS (u agtype, e agtype, v agtype);
759+
SELECT * FROM cypher('issue_1709', $$ MATCH ()-[e]->() DELETE e $$) AS (a agtype);
760+
-- clean up
761+
SELECT * FROM cypher('issue_1709', $$ MATCH (u) DELETE u $$) AS (a agtype);
762+
725763
--
726764
-- clean up graphs
727765
--
728-
SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtype);
729-
SELECT * FROM cypher('issue_1630', $$MATCH (n) DETACH DELETE n $$) AS (a agtype);
766+
SELECT * FROM cypher('cypher_merge', $$ MATCH (n) DETACH DELETE n $$) AS (a agtype);
767+
SELECT * FROM cypher('issue_1630', $$ MATCH (n) DETACH DELETE n $$) AS (a agtype);
768+
SELECT * FROM cypher('issue_1709', $$ MATCH (n) DETACH DELETE n $$) AS (a agtype);
730769

731770
--
732771
-- delete graphs
733772
--
734773
SELECT drop_graph('cypher_merge', true);
735774
SELECT drop_graph('issue_1630', true);
736775
SELECT drop_graph('issue_1691', true);
776+
SELECT drop_graph('issue_1709', true);
737777

738778
--
739779
-- End

src/backend/executor/cypher_utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ HeapTuple insert_entity_tuple_cid(ResultRelInfo *resultRelInfo,
231231
}
232232

233233
// Insert the tuple normally
234-
table_tuple_insert(resultRelInfo->ri_RelationDesc, elemTupleSlot,
235-
GetCurrentCommandId(true), 0, NULL);
234+
table_tuple_insert(resultRelInfo->ri_RelationDesc, elemTupleSlot, cid, 0,
235+
NULL);
236236

237237
// Insert index entries for the tuple
238238
if (resultRelInfo->ri_NumIndices > 0)

0 commit comments

Comments
 (0)