Skip to content

Commit

Permalink
Fix MERGE/SET github issue apache#235
Browse files Browse the repository at this point in the history
Fixed github issue apache#235 for when MERGE and SET were used together.

FX: MERGE (n:node {name: 'Jason'}) SET n.name = 'Lisa' RETURN n

The issue is that the above command would cause a crash when the
target of the MERGE command existed. Meaning, it found a matching
tuple to return to the SET command.

The tuple generated for that particular case was not being sent up
to the SET command correctly, this was fixed.

Added a regression test.
  • Loading branch information
jrgemignani committed Jun 30, 2022
1 parent fae55d2 commit 6fbdfa8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
32 changes: 31 additions & 1 deletion regress/expected/cypher_merge.out
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,35 @@ SELECT * FROM cypher('cypher_merge', $$MATCH (n) RETURN n$$) AS (a agtype);
{"id": 281474976710702, "label": "", "properties": {}}::vertex
(1 row)

--
-- MERGE/SET test
-- Node does exist, then set (github issue #235)
SELECT * FROM cypher('cypher_merge', $$ CREATE (n:node {name: 'Jason'}) RETURN n $$) AS (n agtype);
n
------------------------------------------------------------------------------------
{"id": 2533274790395905, "label": "node", "properties": {"name": "Jason"}}::vertex
(1 row)

SELECT * FROM cypher('cypher_merge', $$ MATCH (n:node) RETURN n $$) AS (n agtype);
n
------------------------------------------------------------------------------------
{"id": 2533274790395905, "label": "node", "properties": {"name": "Jason"}}::vertex
(1 row)

SELECT * FROM cypher('cypher_merge', $$ MERGE (n:node {name: 'Jason'}) SET n.name = 'Lisa' RETURN n $$) AS (n agtype);
n
-----------------------------------------------------------------------------------
{"id": 2533274790395905, "label": "node", "properties": {"name": "Lisa"}}::vertex
(1 row)

SELECT * FROM cypher('cypher_merge', $$ MATCH (n:node) RETURN n $$) AS (n agtype);
n
-----------------------------------------------------------------------------------
{"id": 2533274790395905, "label": "node", "properties": {"name": "Lisa"}}::vertex
(1 row)

-- Node doesn't exist, is created, then set
-- to be added -jrg
--clean up
SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtype);
a
Expand All @@ -818,7 +847,7 @@ SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtyp
* Clean up graph
*/
SELECT drop_graph('cypher_merge', true);
NOTICE: drop cascades to 8 other objects
NOTICE: drop cascades to 9 other objects
DETAIL: drop cascades to table cypher_merge._ag_label_vertex
drop cascades to table cypher_merge._ag_label_edge
drop cascades to table cypher_merge.e
Expand All @@ -827,6 +856,7 @@ drop cascades to table cypher_merge.e_new
drop cascades to table cypher_merge."Person"
drop cascades to table cypher_merge."City"
drop cascades to table cypher_merge."BORN_IN"
drop cascades to table cypher_merge.node
NOTICE: graph "cypher_merge" has been dropped
drop_graph
------------
Expand Down
10 changes: 10 additions & 0 deletions regress/sql/cypher_merge.sql
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,16 @@ SELECT * FROM cypher('cypher_merge', $$MATCH (n) OPTIONAL MATCH (n)-[:e]->(m) ME
-- validate only 1 vertex exits
SELECT * FROM cypher('cypher_merge', $$MATCH (n) RETURN n$$) AS (a agtype);

--
-- MERGE/SET test
-- Node does exist, then set (github issue #235)
SELECT * FROM cypher('cypher_merge', $$ CREATE (n:node {name: 'Jason'}) RETURN n $$) AS (n agtype);
SELECT * FROM cypher('cypher_merge', $$ MATCH (n:node) RETURN n $$) AS (n agtype);
SELECT * FROM cypher('cypher_merge', $$ MERGE (n:node {name: 'Jason'}) SET n.name = 'Lisa' RETURN n $$) AS (n agtype);
SELECT * FROM cypher('cypher_merge', $$ MATCH (n:node) RETURN n $$) AS (n agtype);

-- Node doesn't exist, is created, then set
-- to be added -jrg

--clean up
SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtype);
Expand Down
3 changes: 2 additions & 1 deletion src/backend/executor/cypher_merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ static TupleTableSlot *exec_cypher_merge(CustomScanState *node)
*/
css->found_a_path = true;

return node->ss.ps.lefttree->ps_ResultTupleSlot;
econtext->ecxt_scantuple = ExecProject(node->ss.ps.lefttree->ps_ProjInfo);
return ExecProject(node->ss.ps.ps_ProjInfo);
}
else if (TupIsNull(slot) && css->found_a_path)
{
Expand Down

0 comments on commit 6fbdfa8

Please sign in to comment.