Skip to content

Commit 8604cc2

Browse files
committed
Support Set Operations in Cypher
1 parent 2fc607b commit 8604cc2

File tree

4 files changed

+267
-3
lines changed

4 files changed

+267
-3
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ REGRESS = new_cypher \
9090
expr \
9191
cypher_create \
9292
cypher_match \
93-
order_by
93+
order_by \
94+
cypher_setop
9495

9596
srcdir=`pwd`
9697
POSTGIS_DIR ?= postgis_dir

regress/expected/cypher_setop.out

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright (C) 2024 PostGraphDB
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
LOAD 'postgraph';
18+
SET search_path TO postgraph;
19+
CREATE GRAPH set_op;
20+
NOTICE: graph "set_op" has been created
21+
create_graph
22+
--------------
23+
24+
(1 row)
25+
26+
USE GRAPH set_op;
27+
use_graph
28+
-----------
29+
30+
(1 row)
31+
32+
CREATE ();
33+
--
34+
(0 rows)
35+
36+
MATCH (n) RETURN n UNION MATCH (n) RETURN n;
37+
n
38+
--------------------------------------------------------
39+
{"id": 281474976710657, "label": "", "properties": {}}
40+
(1 row)
41+
42+
MATCH (n) RETURN n UNION ALL MATCH (n) RETURN n;
43+
n
44+
--------------------------------------------------------
45+
{"id": 281474976710657, "label": "", "properties": {}}
46+
{"id": 281474976710657, "label": "", "properties": {}}
47+
(2 rows)
48+
49+
MATCH (n) RETURN n UNION RETURN 1;
50+
ERROR: UNION could not convert type gtype to vertex
51+
LINE 1: MATCH (n) RETURN n UNION RETURN 1;
52+
^
53+
MATCH (n) RETURN n UNION RETURN NULL;
54+
ERROR: UNION could not convert type gtype to vertex
55+
LINE 1: MATCH (n) RETURN n UNION RETURN NULL;
56+
^
57+
RETURN [1,2,3] UNION RETURN 1;
58+
?column?
59+
-----------
60+
[1, 2, 3]
61+
1
62+
(2 rows)
63+
64+
RETURN NULL UNION RETURN NULL;
65+
?column?
66+
----------
67+
68+
(1 row)
69+
70+
RETURN NULL UNION ALL RETURN NULL;
71+
?column?
72+
----------
73+
74+
75+
(2 rows)
76+
77+
MATCH (n) RETURN n UNION MATCH (n) RETURN n UNION MATCH (n) RETURN n;
78+
n
79+
--------------------------------------------------------
80+
{"id": 281474976710657, "label": "", "properties": {}}
81+
(1 row)
82+
83+
MATCH (n) RETURN n UNION ALL MATCH (n) RETURN n UNION ALL MATCH(n) RETURN n;
84+
n
85+
--------------------------------------------------------
86+
{"id": 281474976710657, "label": "", "properties": {}}
87+
{"id": 281474976710657, "label": "", "properties": {}}
88+
{"id": 281474976710657, "label": "", "properties": {}}
89+
(3 rows)
90+
91+
MATCH (n) RETURN n UNION ALL MATCH (n) RETURN n UNION MATCH(n) RETURN n;
92+
n
93+
--------------------------------------------------------
94+
{"id": 281474976710657, "label": "", "properties": {}}
95+
(1 row)
96+
97+
MATCH (n) RETURN n UNION MATCH (n) RETURN n UNION ALL MATCH(n) RETURN n;
98+
n
99+
--------------------------------------------------------
100+
{"id": 281474976710657, "label": "", "properties": {}}
101+
{"id": 281474976710657, "label": "", "properties": {}}
102+
(2 rows)
103+
104+
RETURN NULL UNION ALL RETURN NULL UNION ALL RETURN NULL;
105+
?column?
106+
----------
107+
108+
109+
110+
(3 rows)
111+
112+
RETURN NULL UNION ALL RETURN NULL UNION RETURN NULL;
113+
?column?
114+
----------
115+
116+
(1 row)
117+
118+
RETURN NULL UNION RETURN NULL UNION ALL RETURN NULL;
119+
?column?
120+
----------
121+
122+
123+
(2 rows)
124+
125+
RETURN 1.0::int UNION RETURN 1::float UNION ALL RETURN 2.0::float;
126+
ERROR: syntax error at or near "int"
127+
LINE 1: RETURN 1.0::int UNION RETURN 1::float UNION ALL RETURN 2.0::...
128+
^
129+
RETURN 1.0::int UNION RETURN 1.0::float UNION ALL RETURN 1::int;
130+
ERROR: syntax error at or near "int"
131+
LINE 1: RETURN 1.0::int UNION RETURN 1.0::float UNION ALL RETURN 1::...
132+
^
133+
RETURN 1.0::float UNION RETURN 1::int UNION RETURN 1::float;
134+
ERROR: syntax error at or near "float"
135+
LINE 1: RETURN 1.0::float UNION RETURN 1::int UNION RETURN 1::float;
136+
^
137+
-- Intersect
138+
MATCH (n) RETURN n INTERSECT MATCH (m) RETURN m;
139+
n
140+
--------------------------------------------------------
141+
{"id": 281474976710657, "label": "", "properties": {}}
142+
(1 row)
143+
144+
MATCH (n) RETURN n INTERSECT ALL MATCH (m) RETURN m;
145+
n
146+
--------------------------------------------------------
147+
{"id": 281474976710657, "label": "", "properties": {}}
148+
(1 row)
149+
150+
-- Except
151+
MATCH (n) RETURN n EXCEPT MATCH (m) RETURN m;
152+
n
153+
---
154+
(0 rows)
155+
156+
MATCH (n) RETURN n EXCEPT ALL MATCH (m) RETURN m;
157+
n
158+
---
159+
(0 rows)
160+
161+
-- Operator Precedence
162+
RETURN 2.0::int UNION (RETURN 1::float UNION ALL RETURN 1.0::float);
163+
ERROR: syntax error at or near "int"
164+
LINE 1: RETURN 2.0::int UNION (RETURN 1::float UNION ALL RETURN 1.0:...
165+
^
166+
(RETURN 2.0::int UNION RETURN 1::float ) UNION ALL RETURN 1.0::float;
167+
ERROR: syntax error at or near "int"
168+
LINE 1: (RETURN 2.0::int UNION RETURN 1::float ) UNION ALL RETURN 1....
169+
^
170+
-- Errors
171+
MATCH (n) RETURN n UNION ALL MATCH (m) RETURN n;
172+
ERROR: could not find rte for n
173+
LINE 1: MATCH (n) RETURN n UNION ALL MATCH (m) RETURN n;
174+
^
175+
DROP GRAPH set_op;
176+
ERROR: syntax error at or near ";"
177+
LINE 1: DROP GRAPH set_op;
178+
^

regress/sql/cypher_setop.sql

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (C) 2024 PostGraphDB
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
LOAD 'postgraph';
19+
SET search_path TO postgraph;
20+
21+
CREATE GRAPH set_op;
22+
23+
USE GRAPH set_op;
24+
25+
CREATE ();
26+
27+
28+
MATCH (n) RETURN n UNION MATCH (n) RETURN n;
29+
30+
MATCH (n) RETURN n UNION ALL MATCH (n) RETURN n;
31+
32+
MATCH (n) RETURN n UNION RETURN 1;
33+
34+
MATCH (n) RETURN n UNION RETURN NULL;
35+
36+
RETURN [1,2,3] UNION RETURN 1;
37+
38+
RETURN NULL UNION RETURN NULL;
39+
40+
RETURN NULL UNION ALL RETURN NULL;
41+
42+
MATCH (n) RETURN n UNION MATCH (n) RETURN n UNION MATCH (n) RETURN n;
43+
44+
MATCH (n) RETURN n UNION ALL MATCH (n) RETURN n UNION ALL MATCH(n) RETURN n;
45+
46+
MATCH (n) RETURN n UNION ALL MATCH (n) RETURN n UNION MATCH(n) RETURN n;
47+
48+
MATCH (n) RETURN n UNION MATCH (n) RETURN n UNION ALL MATCH(n) RETURN n;
49+
50+
RETURN NULL UNION ALL RETURN NULL UNION ALL RETURN NULL;
51+
52+
RETURN NULL UNION ALL RETURN NULL UNION RETURN NULL;
53+
54+
RETURN NULL UNION RETURN NULL UNION ALL RETURN NULL;
55+
56+
RETURN 1.0::int UNION RETURN 1::float UNION ALL RETURN 2.0::float;
57+
58+
RETURN 1.0::int UNION RETURN 1.0::float UNION ALL RETURN 1::int;
59+
60+
RETURN 1.0::float UNION RETURN 1::int UNION RETURN 1::float;
61+
62+
63+
-- Intersect
64+
65+
MATCH (n) RETURN n INTERSECT MATCH (m) RETURN m;
66+
67+
MATCH (n) RETURN n INTERSECT ALL MATCH (m) RETURN m;
68+
69+
70+
-- Except
71+
MATCH (n) RETURN n EXCEPT MATCH (m) RETURN m;
72+
73+
MATCH (n) RETURN n EXCEPT ALL MATCH (m) RETURN m;
74+
75+
-- Operator Precedence
76+
77+
RETURN 2.0::int UNION (RETURN 1::float UNION ALL RETURN 1.0::float);
78+
79+
(RETURN 2.0::int UNION RETURN 1::float ) UNION ALL RETURN 1.0::float;
80+
81+
82+
-- Errors
83+
MATCH (n) RETURN n UNION ALL MATCH (m) RETURN n;
84+
85+
DROP GRAPH set_op;

src/backend/parser/cypher_gram.y

+2-2
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ cypher_stmt:
10871087
{
10881088
$$ = $2;
10891089
}
1090-
/* | cypher_stmt UNION all_or_distinct cypher_stmt
1090+
| cypher_stmt UNION all_or_distinct cypher_stmt
10911091
{
10921092
$$ = list_make1(make_set_op(SETOP_UNION, $3, $1, $4));
10931093
}
@@ -1098,7 +1098,7 @@ cypher_stmt:
10981098
| cypher_stmt EXCEPT all_or_distinct cypher_stmt
10991099
{
11001100
$$ = list_make1(make_set_op(SETOP_EXCEPT, $3, $1, $4));
1101-
}*/
1101+
}
11021102
;
11031103

11041104
/*****************************************************************************

0 commit comments

Comments
 (0)