Skip to content
Permalink
Browse files
SET refactor and feature upgrade
This patch utilizes some of the ideas from the partial CREATE
refactor for the transaction bugs (AGE2-337 and AGE2-345) to bring
SET (and REMOVE) up to date.

Additionally, this refactor enabled allowing multiple items to now
be set all at once.

Added regression tests for both SET and REMOVE.
  • Loading branch information
jrgemignani committed Sep 17, 2021
1 parent bfdcfae commit 23ec457c951553994efa7ae9111d7761118a0270
Showing 6 changed files with 461 additions and 104 deletions.
@@ -363,6 +363,98 @@ SELECT remove_test();
{"id": 3377699720527873, "label": "test_7", "properties": {}}::vertex
(19 rows)

--
-- Updating Multiple Fields
--
SELECT * FROM cypher('cypher_remove', $$MATCH (n) RETURN n$$) AS (a agtype);
a
------------------------------------------------------------------------------------
{"id": 281474976710657, "label": "", "properties": {}}::vertex
{"id": 281474976710658, "label": "", "properties": {}}::vertex
{"id": 281474976710659, "label": "", "properties": {}}::vertex
{"id": 281474976710660, "label": "", "properties": {}}::vertex
{"id": 281474976710661, "label": "", "properties": {}}::vertex
{"id": 281474976710662, "label": "", "properties": {}}::vertex
{"id": 844424930131969, "label": "test_1", "properties": {}}::vertex
{"id": 844424930131971, "label": "test_1", "properties": {}}::vertex
{"id": 844424930131970, "label": "test_1", "properties": {"a": 0, "j": 5}}::vertex
{"id": 1125899906842625, "label": "test_2", "properties": {}}::vertex
{"id": 1125899906842626, "label": "test_2", "properties": {"a": 0}}::vertex
{"id": 1125899906842627, "label": "test_2", "properties": {}}::vertex
{"id": 1407374883553282, "label": "test_3", "properties": {}}::vertex
{"id": 1407374883553281, "label": "test_3", "properties": {}}::vertex
{"id": 1970324836974593, "label": "test_4", "properties": {}}::vertex
{"id": 1970324836974594, "label": "test_4", "properties": {}}::vertex
{"id": 2533274790395905, "label": "test_5", "properties": {"k": 3}}::vertex
{"id": 2814749767106561, "label": "test_6", "properties": {"j": 5}}::vertex
{"id": 3377699720527873, "label": "test_7", "properties": {}}::vertex
(19 rows)

SELECT * FROM cypher('cypher_remove', $$MATCH (n) REMOVE n.i, n.j, n.k RETURN n$$) AS (a agtype);
a
-----------------------------------------------------------------------------
{"id": 281474976710657, "label": "", "properties": {}}::vertex
{"id": 281474976710658, "label": "", "properties": {}}::vertex
{"id": 281474976710659, "label": "", "properties": {}}::vertex
{"id": 281474976710660, "label": "", "properties": {}}::vertex
{"id": 281474976710661, "label": "", "properties": {}}::vertex
{"id": 281474976710662, "label": "", "properties": {}}::vertex
{"id": 844424930131969, "label": "test_1", "properties": {}}::vertex
{"id": 844424930131971, "label": "test_1", "properties": {}}::vertex
{"id": 844424930131970, "label": "test_1", "properties": {"a": 0}}::vertex
{"id": 1125899906842625, "label": "test_2", "properties": {}}::vertex
{"id": 1125899906842626, "label": "test_2", "properties": {"a": 0}}::vertex
{"id": 1125899906842627, "label": "test_2", "properties": {}}::vertex
{"id": 1407374883553282, "label": "test_3", "properties": {}}::vertex
{"id": 1407374883553281, "label": "test_3", "properties": {}}::vertex
{"id": 1970324836974593, "label": "test_4", "properties": {}}::vertex
{"id": 1970324836974594, "label": "test_4", "properties": {}}::vertex
{"id": 2533274790395905, "label": "test_5", "properties": {}}::vertex
{"id": 2814749767106561, "label": "test_6", "properties": {}}::vertex
{"id": 3377699720527873, "label": "test_7", "properties": {}}::vertex
(19 rows)

SELECT * FROM cypher('cypher_remove', $$MATCH (n) RETURN n$$) AS (a agtype);
a
-----------------------------------------------------------------------------
{"id": 281474976710657, "label": "", "properties": {}}::vertex
{"id": 281474976710658, "label": "", "properties": {}}::vertex
{"id": 281474976710659, "label": "", "properties": {}}::vertex
{"id": 281474976710660, "label": "", "properties": {}}::vertex
{"id": 281474976710661, "label": "", "properties": {}}::vertex
{"id": 281474976710662, "label": "", "properties": {}}::vertex
{"id": 844424930131969, "label": "test_1", "properties": {}}::vertex
{"id": 844424930131971, "label": "test_1", "properties": {}}::vertex
{"id": 844424930131970, "label": "test_1", "properties": {"a": 0}}::vertex
{"id": 1125899906842625, "label": "test_2", "properties": {}}::vertex
{"id": 1125899906842626, "label": "test_2", "properties": {"a": 0}}::vertex
{"id": 1125899906842627, "label": "test_2", "properties": {}}::vertex
{"id": 1407374883553282, "label": "test_3", "properties": {}}::vertex
{"id": 1407374883553281, "label": "test_3", "properties": {}}::vertex
{"id": 1970324836974593, "label": "test_4", "properties": {}}::vertex
{"id": 1970324836974594, "label": "test_4", "properties": {}}::vertex
{"id": 2533274790395905, "label": "test_5", "properties": {}}::vertex
{"id": 2814749767106561, "label": "test_6", "properties": {}}::vertex
{"id": 3377699720527873, "label": "test_7", "properties": {}}::vertex
(19 rows)

SELECT * FROM cypher('cypher_remove', $$CREATE ()-[:edge_multi_property { i: 5, j: 20}]->()$$) AS (a agtype);
a
---
(0 rows)

SELECT * FROM cypher('cypher_remove', $$MATCH ()-[e:edge_multi_property]-() RETURN e$$) AS (a agtype);
a
---------------------------------------------------------------------------------------------------------------------------------------------------------
{"id": 3659174697238529, "label": "edge_multi_property", "end_id": 281474976710664, "start_id": 281474976710663, "properties": {"i": 5, "j": 20}}::edge
(1 row)

SELECT * FROM cypher('cypher_remove', $$MATCH ()-[e:edge_multi_property]-() REMOVE e.i, e.j RETURN e$$) AS (a agtype);
a
------------------------------------------------------------------------------------------------------------------------------------------
{"id": 3659174697238529, "label": "edge_multi_property", "end_id": 281474976710664, "start_id": 281474976710663, "properties": {}}::edge
(1 row)

--Errors
SELECT * FROM cypher('cypher_remove', $$REMOVE n.i$$) AS (a agtype);
ERROR: REMOVE cannot be the first clause in a Cypher query
@@ -376,16 +468,12 @@ SELECT * FROM cypher('cypher_remove', $$MATCH (n) REMOVE wrong_var.i$$) AS (a ag
ERROR: undefined reference to variable wrong_var in REMOVE clause
LINE 1: SELECT * FROM cypher('cypher_remove', $$MATCH (n) REMOVE wro...
^
SELECT * FROM cypher('cypher_remove', $$MATCH (n) REMOVE n.i = 3, n.j = 5 $$) AS (a agtype);
ERROR: REMOVE clause does not yet support updating more than one property
LINE 1: SELECT * FROM cypher('cypher_remove', $$MATCH (n) REMOVE n.i...
^
--
-- Clean up
--
DROP FUNCTION remove_test;
SELECT drop_graph('cypher_remove', true);
NOTICE: drop cascades to 12 other objects
NOTICE: drop cascades to 13 other objects
DETAIL: drop cascades to table cypher_remove._ag_label_vertex
drop cascades to table cypher_remove._ag_label_edge
drop cascades to table cypher_remove.test_1
@@ -398,6 +486,7 @@ drop cascades to table cypher_remove.test_5
drop cascades to table cypher_remove.test_6
drop cascades to table cypher_remove.e
drop cascades to table cypher_remove.test_7
drop cascades to table cypher_remove.edge_multi_property
NOTICE: graph "cypher_remove" has been dropped
drop_graph
------------
@@ -340,6 +340,30 @@ SELECT set_test();
{"id": 1407374883553284, "label": "other_v", "properties": {"i": 7, "k": 10}}::vertex
(10 rows)

--
-- Updating multiple fieds
--
SELECT * FROM cypher('cypher_set', $$MATCH (n) SET n.i = 3, n.j = 5 RETURN n $$) AS (a agtype);
a
-------------------------------------------------------------------------------------------------------------------
{"id": 281474976710659, "label": "", "properties": {"i": 3, "j": 5, "y": 2}}::vertex
{"id": 281474976710657, "label": "", "properties": {"i": 3, "j": 5, "t": 150}}::vertex
{"id": 281474976710658, "label": "", "properties": {"i": 3, "j": 5, "t": 150, "y": 1}}::vertex
{"id": 844424930131969, "label": "v", "properties": {"i": 3, "j": 5, "t": 150}}::vertex
{"id": 844424930131971, "label": "v", "properties": {"i": 3, "j": 5, "t": 150}}::vertex
{"id": 844424930131970, "label": "v", "properties": {"a": 0, "i": 3, "j": 5, "t": 150, "y": 99, "z": 99}}::vertex
{"id": 1407374883553281, "label": "other_v", "properties": {"i": 3, "j": 5, "k": 10}}::vertex
{"id": 1407374883553282, "label": "other_v", "properties": {"i": 3, "j": 5, "k": 10}}::vertex
{"id": 1407374883553283, "label": "other_v", "properties": {"i": 3, "j": 5, "k": 10}}::vertex
{"id": 1407374883553284, "label": "other_v", "properties": {"i": 3, "j": 5, "k": 10}}::vertex
(10 rows)

SELECT * FROM cypher('cypher_set', $$MATCH (n)-[m]->(n) SET m.y = n.y RETURN n, m$$) AS (a agtype, b agtype);
a | b
-------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------
{"id": 844424930131970, "label": "v", "properties": {"a": 0, "i": 3, "j": 5, "t": 150, "y": 99, "z": 99}}::vertex | {"id": 1125899906842629, "label": "e", "end_id": 844424930131970, "start_id": 844424930131970, "properties": {"j": 34, "y": 99}}::edge
(1 row)

--Errors
SELECT * FROM cypher('cypher_set', $$SET n.i = NULL$$) AS (a agtype);
ERROR: SET cannot be the first clause in a Cypher query
@@ -349,21 +373,138 @@ SELECT * FROM cypher('cypher_set', $$MATCH (n) SET wrong_var.i = 3$$) AS (a agty
ERROR: undefined reference to variable wrong_var in SET clause
LINE 1: ...ELECT * FROM cypher('cypher_set', $$MATCH (n) SET wrong_var....
^
SELECT * FROM cypher('cypher_set', $$MATCH (n) SET n.i = 3, n.j = 5 $$) AS (a agtype);
ERROR: SET clause does not yet support updating more than one property
LINE 1: SELECT * FROM cypher('cypher_set', $$MATCH (n) SET n.i = 3, ...
^
--
-- SET refactor regression tests
--
-- INSERT INTO
CREATE TABLE tbl (result agtype);
SELECT * FROM cypher('cypher_set', $$CREATE (u:vertices) $$) AS (result agtype);
result
--------
(0 rows)

SELECT * FROM cypher('cypher_set', $$CREATE (u:begin)-[:edge]->(v:end) $$) AS (result agtype);
result
--------
(0 rows)

SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) return u $$) AS (result agtype);
result
-------------------------------------------------------------------------
{"id": 1688849860263937, "label": "vertices", "properties": {}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:begin)-[:edge]->(v:end) return u, v $$) AS (u agtype, v agtype);
u | v
----------------------------------------------------------------------+--------------------------------------------------------------------
{"id": 1970324836974593, "label": "begin", "properties": {}}::vertex | {"id": 2533274790395905, "label": "end", "properties": {}}::vertex
(1 row)

INSERT INTO tbl (SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) SET u.i = 7 return u $$) AS (result agtype));
INSERT INTO tbl (SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) SET u.i = 13 return u $$) AS (result agtype));
SELECT * FROM tbl;
result
--------------------------------------------------------------------------------
{"id": 1688849860263937, "label": "vertices", "properties": {"i": 7}}::vertex
{"id": 1688849860263937, "label": "vertices", "properties": {"i": 13}}::vertex
(2 rows)

SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) return u $$) AS (result agtype);
result
--------------------------------------------------------------------------------
{"id": 1688849860263937, "label": "vertices", "properties": {"i": 13}}::vertex
(1 row)

BEGIN;
SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) SET u.i = 1, u.j = 3, u.k = 5 return u $$) AS (result agtype);
result
-----------------------------------------------------------------------------------------------
{"id": 1688849860263937, "label": "vertices", "properties": {"i": 1, "j": 3, "k": 5}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) return u $$) AS (result agtype);
result
-----------------------------------------------------------------------------------------------
{"id": 1688849860263937, "label": "vertices", "properties": {"i": 1, "j": 3, "k": 5}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) SET u.i = 2, u.j = 4, u.k = 6 return u $$) AS (result agtype);
result
-----------------------------------------------------------------------------------------------
{"id": 1688849860263937, "label": "vertices", "properties": {"i": 2, "j": 4, "k": 6}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) return u $$) AS (result agtype);
result
-----------------------------------------------------------------------------------------------
{"id": 1688849860263937, "label": "vertices", "properties": {"i": 2, "j": 4, "k": 6}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) SET u.i = 3, u.j = 6, u.k = 9 return u $$) AS (result agtype);
result
-----------------------------------------------------------------------------------------------
{"id": 1688849860263937, "label": "vertices", "properties": {"i": 3, "j": 6, "k": 9}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) return u $$) AS (result agtype);
result
-----------------------------------------------------------------------------------------------
{"id": 1688849860263937, "label": "vertices", "properties": {"i": 3, "j": 6, "k": 9}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:begin)-[:edge]->(v:end) SET u.i = 1, v.i = 2, u.j = 3, v.j = 4 return u, v $$) AS (u agtype, v agtype);
u | v
------------------------------------------------------------------------------------+----------------------------------------------------------------------------------
{"id": 1970324836974593, "label": "begin", "properties": {"i": 1, "j": 3}}::vertex | {"id": 2533274790395905, "label": "end", "properties": {"i": 2, "j": 4}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:begin)-[:edge]->(v:end) return u, v $$) AS (u agtype, v agtype);
u | v
------------------------------------------------------------------------------------+----------------------------------------------------------------------------------
{"id": 1970324836974593, "label": "begin", "properties": {"i": 1, "j": 3}}::vertex | {"id": 2533274790395905, "label": "end", "properties": {"i": 2, "j": 4}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:begin)-[:edge]->(v:end) SET u.i = 2, v.i = 1, u.j = 4, v.j = 3 return u, v $$) AS (u agtype, v agtype);
u | v
------------------------------------------------------------------------------------+----------------------------------------------------------------------------------
{"id": 1970324836974593, "label": "begin", "properties": {"i": 2, "j": 4}}::vertex | {"id": 2533274790395905, "label": "end", "properties": {"i": 1, "j": 3}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:begin)-[:edge]->(v:end) return u, v $$) AS (u agtype, v agtype);
u | v
------------------------------------------------------------------------------------+----------------------------------------------------------------------------------
{"id": 1970324836974593, "label": "begin", "properties": {"i": 2, "j": 4}}::vertex | {"id": 2533274790395905, "label": "end", "properties": {"i": 1, "j": 3}}::vertex
(1 row)

END;
SELECT * FROM cypher('cypher_set', $$MATCH (u:vertices) return u $$) AS (result agtype);
result
-----------------------------------------------------------------------------------------------
{"id": 1688849860263937, "label": "vertices", "properties": {"i": 3, "j": 6, "k": 9}}::vertex
(1 row)

SELECT * FROM cypher('cypher_set', $$MATCH (u:begin)-[:edge]->(v:end) return u, v $$) AS (u agtype, v agtype);
u | v
------------------------------------------------------------------------------------+----------------------------------------------------------------------------------
{"id": 1970324836974593, "label": "begin", "properties": {"i": 2, "j": 4}}::vertex | {"id": 2533274790395905, "label": "end", "properties": {"i": 1, "j": 3}}::vertex
(1 row)

--
-- Clean up
--
DROP TABLE tbl;
DROP FUNCTION set_test;
SELECT drop_graph('cypher_set', true);
NOTICE: drop cascades to 5 other objects
NOTICE: drop cascades to 9 other objects
DETAIL: drop cascades to table cypher_set._ag_label_vertex
drop cascades to table cypher_set._ag_label_edge
drop cascades to table cypher_set.v
drop cascades to table cypher_set.e
drop cascades to table cypher_set.other_v
drop cascades to table cypher_set.vertices
drop cascades to table cypher_set.begin
drop cascades to table cypher_set.edge
drop cascades to table cypher_set."end"
NOTICE: graph "cypher_set" has been dropped
drop_graph
------------

0 comments on commit 23ec457

Please sign in to comment.