Skip to content

Commit aeaa330

Browse files
committed
Support Select Statments in Cypher EXISTS
Allow a user to run a select stmt in a cypher exists subquery
1 parent 2817d41 commit aeaa330

File tree

9 files changed

+3661
-96
lines changed

9 files changed

+3661
-96
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ OBJS = src/backend/postgraph.o \
4646
src/backend/parser/cypher_analyze.o \
4747
src/backend/parser/cypher_clause.o \
4848
src/backend/executor/cypher_delete.o \
49+
src/backend/parser/sql_expr.o \
4950
src/backend/parser/cypher_expr.o \
5051
src/backend/parser/cypher_gram.o \
5152
src/backend/parser/cypher_item.o \

regress/expected/cypher_match.out

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,33 +1348,28 @@ MATCH (n:vmissing)-[]-() RETURN n;
13481348
ERROR: label vmissing does not exists
13491349
LINE 1: MATCH (n:vmissing)-[]-() RETURN n;
13501350
^
1351+
CREATE TABLE tst (i int);
1352+
INSERT INTO tst VALUES (1), (2), (3);
1353+
MATCH (n)
1354+
WHERE EXISTS (
1355+
SELECT * FROM tst as t where t.i = n.i
1356+
)
1357+
RETURN n; n
1358+
n
1359+
------------------------------------------------------------------------------
1360+
{"id": 281474976710662, "label": "", "properties": {"i": 1, "j": 2, "k": 3}}
1361+
{"id": 281474976710663, "label": "", "properties": {"i": 1, "j": 3}}
1362+
{"id": 281474976710664, "label": "", "properties": {"i": 2, "k": 3}}
1363+
{"id": 844424930131971, "label": "v", "properties": {"i": 1}}
1364+
(4 rows)
1365+
13511366
--
13521367
-- Clean up
13531368
--
13541369
DROP GRAPH cypher_match CASCADE;
1355-
NOTICE: drop cascades to 16 other objects
1356-
DETAIL: drop cascades to table cypher_match._ag_label_vertex
1357-
drop cascades to table cypher_match._ag_label_edge
1358-
drop cascades to table cypher_match.v
1359-
drop cascades to table cypher_match.v1
1360-
drop cascades to table cypher_match.e1
1361-
drop cascades to table cypher_match.v2
1362-
drop cascades to table cypher_match.e2
1363-
drop cascades to table cypher_match.v3
1364-
drop cascades to table cypher_match.e3
1365-
drop cascades to table cypher_match.loop
1366-
drop cascades to table cypher_match.self
1367-
drop cascades to table cypher_match.duplicate
1368-
drop cascades to table cypher_match.dup_edge
1369-
drop cascades to table cypher_match.other_v
1370-
drop cascades to table cypher_match.opt_match_v
1371-
drop cascades to table cypher_match.opt_match_e
1372-
NOTICE: graph "cypher_match" has been dropped
1373-
drop_graph
1374-
------------
1375-
1376-
(1 row)
1377-
1370+
ERROR: syntax error at or near "n"
1371+
LINE 1: n
1372+
^
13781373
--
13791374
-- End
13801375
--

regress/expected/cypher_merge.out

Lines changed: 49 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ USE GRAPH cypher_merge;
3939
*/
4040
--test query
4141
MERGE (n {i: 'Hello Merge'});
42-
_null | n | _id_n___ | _pr_n | _merge_clause
43-
-------+---+----------+-------+---------------
42+
--
4443
(0 rows)
4544

4645
--validate
4746
MATCH (n) RETURN n;
48-
n
49-
---
50-
(0 rows)
47+
n
48+
--------------------------------------------------------------------------
49+
{"id": 281474976710657, "label": "", "properties": {"i": "Hello Merge"}}
50+
(1 row)
5151

5252
--clean up
5353
MATCH (n) DETACH DELETE n;
@@ -64,16 +64,14 @@ CREATE ({i: 'Hello Merge'});
6464

6565
--test_query
6666
MERGE ({i: 'Hello Merge'});
67-
_null | _age_default_alias_0 | _id__age_default_alias_0___ | _pr__age_default_alias_0 | _merge_clause
68-
-------+--------------------------------------------------------------------------+-----------------------------+--------------------------+---------------
69-
| {"id": 281474976710657, "label": "", "properties": {"i": "Hello Merge"}} | 281474976710657 | {"i": "Hello Merge"} |
70-
(1 row)
67+
--
68+
(0 rows)
7169

7270
--validate
7371
MATCH (n) RETURN n;
7472
n
7573
--------------------------------------------------------------------------
76-
{"id": 281474976710657, "label": "", "properties": {"i": "Hello Merge"}}
74+
{"id": 281474976710658, "label": "", "properties": {"i": "Hello Merge"}}
7775
(1 row)
7876

7977
--clean up
@@ -86,8 +84,7 @@ MATCH (n) DETACH DELETE n;
8684
*/
8785
--test query
8886
MATCH (n) MERGE ({i: n.i});
89-
_null | n | _id_n___ | _pr_n | _age_default_alias_0 | _id__age_default_alias_0___ | _pr__age_default_alias_0 | _merge_clause
90-
-------+---+----------+-------+----------------------+-----------------------------+--------------------------+---------------
87+
--
9188
(0 rows)
9289

9390
--validate
@@ -110,16 +107,14 @@ CREATE ({i: 'Hello Merge'});
110107
(0 rows)
111108

112109
MATCH (n) MERGE ({i: n.i});
113-
_null | n | _id_n___ | _pr_n | _age_default_alias_0 | _id__age_default_alias_0___ | _pr__age_default_alias_0 | _merge_clause
114-
-------+--------------------------------------------------------------------------+-----------------+----------------------+--------------------------------------------------------------------------+-----------------------------+--------------------------+---------------
115-
| {"id": 281474976710658, "label": "", "properties": {"i": "Hello Merge"}} | 281474976710658 | {"i": "Hello Merge"} | {"id": 281474976710658, "label": "", "properties": {"i": "Hello Merge"}} | 281474976710658 | {"i": "Hello Merge"} |
116-
(1 row)
110+
--
111+
(0 rows)
117112

118113
--validate
119114
MATCH (n) RETURN n;
120115
n
121116
--------------------------------------------------------------------------
122-
{"id": 281474976710658, "label": "", "properties": {"i": "Hello Merge"}}
117+
{"id": 281474976710659, "label": "", "properties": {"i": "Hello Merge"}}
123118
(1 row)
124119

125120
--clean up
@@ -137,16 +132,13 @@ CREATE ({i: 'Hello Merge'});
137132

138133
--test query
139134
MATCH (n) MERGE ({j: n.i});
140-
_null | n | _id_n___ | _pr_n | _age_default_alias_0 | _id__age_default_alias_0___ | _pr__age_default_alias_0 | _merge_clause
141-
-------+--------------------------------------------------------------------------+-----------------+----------------------+----------------------+-----------------------------+--------------------------+---------------
142-
| {"id": 281474976710659, "label": "", "properties": {"i": "Hello Merge"}} | 281474976710659 | {"i": "Hello Merge"} | | | |
143-
(1 row)
144-
135+
ERROR: attribute 1 of type record has wrong type
136+
DETAIL: Table has type gtype, but query expects vertex.
145137
--validate
146138
MATCH (n) RETURN n;
147139
n
148140
--------------------------------------------------------------------------
149-
{"id": 281474976710659, "label": "", "properties": {"i": "Hello Merge"}}
141+
{"id": 281474976710660, "label": "", "properties": {"i": "Hello Merge"}}
150142
(1 row)
151143

152144
--clean up
@@ -166,15 +158,15 @@ CREATE ({i: 2});
166158
MERGE (n {i: 1}) RETURN n;
167159
n
168160
--------------------------------------------------------------
169-
{"id": 281474976710661, "label": "", "properties": {"i": 1}}
161+
{"id": 281474976710663, "label": "", "properties": {"i": 1}}
170162
(1 row)
171163

172164
--validate
173165
MATCH (n) RETURN n;
174166
n
175167
--------------------------------------------------------------
176-
{"id": 281474976710660, "label": "", "properties": {"i": 2}}
177-
{"id": 281474976710661, "label": "", "properties": {"i": 1}}
168+
{"id": 281474976710662, "label": "", "properties": {"i": 2}}
169+
{"id": 281474976710663, "label": "", "properties": {"i": 1}}
178170
(2 rows)
179171

180172
--clean up
@@ -206,18 +198,18 @@ CREATE ();
206198
MERGE (n {i: 1}) RETURN n;
207199
n
208200
--------------------------------------------------------------
209-
{"id": 281474976710662, "label": "", "properties": {"i": 1}}
210-
{"id": 281474976710663, "label": "", "properties": {"i": 1}}
201+
{"id": 281474976710664, "label": "", "properties": {"i": 1}}
202+
{"id": 281474976710665, "label": "", "properties": {"i": 1}}
211203
(2 rows)
212204

213205
--validate
214206
MATCH (n) RETURN n;
215207
n
216208
--------------------------------------------------------------
217-
{"id": 281474976710662, "label": "", "properties": {"i": 1}}
218-
{"id": 281474976710663, "label": "", "properties": {"i": 1}}
219-
{"id": 281474976710664, "label": "", "properties": {"i": 2}}
220-
{"id": 281474976710665, "label": "", "properties": {}}
209+
{"id": 281474976710664, "label": "", "properties": {"i": 1}}
210+
{"id": 281474976710665, "label": "", "properties": {"i": 1}}
211+
{"id": 281474976710666, "label": "", "properties": {"i": 2}}
212+
{"id": 281474976710667, "label": "", "properties": {}}
221213
(4 rows)
222214

223215
--clean up
@@ -238,16 +230,15 @@ CREATE ();
238230

239231
--test query
240232
MATCH (n) MERGE (n)-[:e]->(:v);
241-
_null | n | _id_n___ | _pr_n | _age_default_alias_0 | _id__age_default_alias_0___ | _st__age_default_alias_0 | _en__age_default_alias_0 | _pr__age_default_alias_0 | _age_default_alias_1 | _id__age_default_alias_1___ | _pr__age_default_alias_1 | _merge_clause
242-
-------+--------------------------------------------------------+-----------------+-------+----------------------+-----------------------------+--------------------------+--------------------------+--------------------------+----------------------+-----------------------------+--------------------------+---------------
243-
| {"id": 281474976710666, "label": "", "properties": {}} | 281474976710666 | {} | | | | | | | | |
244-
(1 row)
233+
--
234+
(0 rows)
245235

246236
--validate
247237
MATCH (n)-[e:e]->(m:v) RETURN n, e, m;
248-
n | e | m
249-
---+---+---
250-
(0 rows)
238+
n | e | m
239+
--------------------------------------------------------+------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------
240+
{"id": 281474976710668, "label": "", "properties": {}} | {"id": 844424930131969, "start_id": 281474976710668, "end_id": 1125899906842625, "label": "e", "properties": {}} | {"id": 1125899906842625, "label": "v", "properties": {}}
241+
(1 row)
251242

252243
--clean up
253244
MATCH (n) DETACH DELETE n;
@@ -264,15 +255,15 @@ CREATE ()-[:e]->();
264255

265256
--test query
266257
MERGE (n)-[:e]->(:v);
267-
_null | n | _id_n___ | _pr_n | _age_default_alias_0 | _id__age_default_alias_0___ | _st__age_default_alias_0 | _en__age_default_alias_0 | _pr__age_default_alias_0 | _age_default_alias_1 | _id__age_default_alias_1___ | _pr__age_default_alias_1 | _merge_clause
268-
-------+---+----------+-------+----------------------+-----------------------------+--------------------------+--------------------------+--------------------------+----------------------+-----------------------------+--------------------------+---------------
258+
--
269259
(0 rows)
270260

271261
--validate
272262
MATCH (n)-[e:e]->(m:v) RETURN n, e, m;
273-
n | e | m
274-
---+---+---
275-
(0 rows)
263+
n | e | m
264+
--------------------------------------------------------+------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------
265+
{"id": 281474976710671, "label": "", "properties": {}} | {"id": 844424930131971, "start_id": 281474976710671, "end_id": 1125899906842626, "label": "e", "properties": {}} | {"id": 1125899906842626, "label": "v", "properties": {}}
266+
(1 row)
276267

277268
--clean up
278269
MATCH (n) DETACH DELETE n;
@@ -289,16 +280,15 @@ CREATE ();
289280

290281
--test query
291282
MATCH (n) MERGE (n)-[:e]->(:v);
292-
_null | n | _id_n___ | _pr_n | _age_default_alias_0 | _id__age_default_alias_0___ | _st__age_default_alias_0 | _en__age_default_alias_0 | _pr__age_default_alias_0 | _age_default_alias_1 | _id__age_default_alias_1___ | _pr__age_default_alias_1 | _merge_clause
293-
-------+--------------------------------------------------------+-----------------+-------+----------------------+-----------------------------+--------------------------+--------------------------+--------------------------+----------------------+-----------------------------+--------------------------+---------------
294-
| {"id": 281474976710669, "label": "", "properties": {}} | 281474976710669 | {} | | | | | | | | |
295-
(1 row)
283+
--
284+
(0 rows)
296285

297286
--validate created correctly
298287
MATCH (n)-[e:e]->(m:v) RETURN n, e, m;
299-
n | e | m
300-
---+---+---
301-
(0 rows)
288+
n | e | m
289+
--------------------------------------------------------+------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------
290+
{"id": 281474976710672, "label": "", "properties": {}} | {"id": 844424930131972, "start_id": 281474976710672, "end_id": 1125899906842627, "label": "e", "properties": {}} | {"id": 1125899906842627, "label": "v", "properties": {}}
291+
(1 row)
302292

303293
--clean up
304294
MATCH (n) DETACH DELETE n;
@@ -315,17 +305,16 @@ CREATE ()-[:e]->();
315305

316306
--test query
317307
MATCH (n) MERGE (n)-[:e]->(:v);
318-
_null | n | _id_n___ | _pr_n | _age_default_alias_0 | _id__age_default_alias_0___ | _st__age_default_alias_0 | _en__age_default_alias_0 | _pr__age_default_alias_0 | _age_default_alias_1 | _id__age_default_alias_1___ | _pr__age_default_alias_1 | _merge_clause
319-
-------+--------------------------------------------------------+-----------------+-------+----------------------+-----------------------------+--------------------------+--------------------------+--------------------------+----------------------+-----------------------------+--------------------------+---------------
320-
| {"id": 281474976710670, "label": "", "properties": {}} | 281474976710670 | {} | | | | | | | | |
321-
| {"id": 281474976710671, "label": "", "properties": {}} | 281474976710671 | {} | | | | | | | | |
322-
(2 rows)
308+
--
309+
(0 rows)
323310

324311
--validate created correctly
325312
MATCH (n)-[e:e]->(m:v) RETURN n, e, m;
326-
n | e | m
327-
---+---+---
328-
(0 rows)
313+
n | e | m
314+
--------------------------------------------------------+------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------
315+
{"id": 281474976710673, "label": "", "properties": {}} | {"id": 844424930131974, "start_id": 281474976710673, "end_id": 1125899906842628, "label": "e", "properties": {}} | {"id": 1125899906842628, "label": "v", "properties": {}}
316+
{"id": 281474976710674, "label": "", "properties": {}} | {"id": 844424930131975, "start_id": 281474976710674, "end_id": 1125899906842629, "label": "e", "properties": {}} | {"id": 1125899906842629, "label": "v", "properties": {}}
317+
(2 rows)
329318

330319
--clean up
331320
MATCH (n) DETACH DELETE n;
@@ -342,8 +331,7 @@ CREATE ()-[:e]->();
342331

343332
--test query
344333
MERGE ()-[:e]->()-[:e]->();
345-
_null | _age_default_alias_0 | _id__age_default_alias_0___ | _pr__age_default_alias_0 | _age_default_alias_1 | _id__age_default_alias_1___ | _st__age_default_alias_1 | _en__age_default_alias_1 | _pr__age_default_alias_1 | _age_default_alias_2 | _id__age_default_alias_2___ | _pr__age_default_alias_2 | _age_default_alias_3 | _id__age_default_alias_3___ | _st__age_default_alias_3 | _en__age_default_alias_3 | _pr__age_default_alias_3 | _age_default_alias_4 | _id__age_default_alias_4___ | _pr__age_default_alias_4 | _merge_clause
346-
-------+----------------------+-----------------------------+--------------------------+----------------------+-----------------------------+--------------------------+--------------------------+--------------------------+----------------------+-----------------------------+--------------------------+----------------------+-----------------------------+--------------------------+--------------------------+--------------------------+----------------------+-----------------------------+--------------------------+---------------
334+
--
347335
(0 rows)
348336

349337
--validate created correctly

regress/sql/cypher_match.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ MATCH (n:e1)-[]-() RETURN n;
311311

312312
MATCH (n:vmissing)-[]-() RETURN n;
313313

314+
CREATE TABLE tst (i int);
315+
INSERT INTO tst VALUES (1), (2), (3);
316+
317+
MATCH (n)
318+
WHERE EXISTS (
319+
SELECT * FROM tst as t where t.i = n.i
320+
)
321+
RETURN n; n
322+
314323
--
315324
-- Clean up
316325
--

src/backend/parser/cypher_clause.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,7 @@ transform_frame_offset(ParseState *pstate, int frameOptions,
18491849
if (frameOptions & FRAMEOPTION_ROWS)
18501850
{
18511851
/* Transform the raw expression tree */
1852-
node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_ROWS);
1852+
node = sql_transform_expr(pstate, clause, EXPR_KIND_WINDOW_FRAME_ROWS);
18531853

18541854
/*
18551855
* Like LIMIT clause, simply coerce to int8
@@ -1874,7 +1874,7 @@ transform_frame_offset(ParseState *pstate, int frameOptions,
18741874
int i;
18751875

18761876
/* Transform the raw expression tree */
1877-
node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_RANGE);
1877+
node = sql_transform_expr(pstate, clause, EXPR_KIND_WINDOW_FRAME_RANGE);
18781878
nodeType = exprType(node);
18791879

18801880
/*
@@ -1951,7 +1951,7 @@ transform_frame_offset(ParseState *pstate, int frameOptions,
19511951
else if (frameOptions & FRAMEOPTION_GROUPS)
19521952
{
19531953
/* Transform the raw expression tree */
1954-
node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_GROUPS);
1954+
node = sql_transform_expr(pstate, clause, EXPR_KIND_WINDOW_FRAME_GROUPS);
19551955

19561956
/*
19571957
* Like LIMIT clause, simply coerce to int8
@@ -2914,7 +2914,7 @@ static void transform_match_pattern(cypher_parsestate *cpstate, Query *query, Li
29142914

29152915
if (quals != NIL) {
29162916
q = makeBoolExpr(AND_EXPR, quals, -1);
2917-
expr = (Expr *)transformExpr(&cpstate->pstate, (Node *)q, EXPR_KIND_WHERE);
2917+
expr = (Expr *)sql_transform_expr(&cpstate->pstate, (Node *)q, EXPR_KIND_WHERE);
29182918
}
29192919

29202920
if (cpstate->property_constraint_quals != NIL) {
@@ -3277,7 +3277,7 @@ static Node *create_property_constraints(cypher_parsestate *cpstate, transform_e
32773277
if ((pnsi = find_pnsi(cpstate, entity_name)))
32783278
prop_expr = scanNSItemForColumn(pstate, pnsi, 0, AG_VERTEX_COLNAME_PROPERTIES, -1);
32793279
else
3280-
prop_expr = transformExpr(pstate, (Node *)cr, EXPR_KIND_WHERE);
3280+
prop_expr = sql_transform_expr(pstate, (Node *)cr, EXPR_KIND_WHERE);
32813281

32823282
// use cypher to get the constraints' transform node
32833283
const_expr = transform_cypher_expr(cpstate, property_constraints, EXPR_KIND_WHERE);

0 commit comments

Comments
 (0)