Skip to content

Commit a4525e3

Browse files
committed
Support ANY/ALL/SOME SQL Subqueries in Cypher
1 parent aeaa330 commit a4525e3

File tree

5 files changed

+111
-5
lines changed

5 files changed

+111
-5
lines changed

regress/expected/cypher_match.out

+57-4
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ MATCH (n)
13541354
WHERE EXISTS (
13551355
SELECT * FROM tst as t where t.i = n.i
13561356
)
1357-
RETURN n; n
1357+
RETURN n;
13581358
n
13591359
------------------------------------------------------------------------------
13601360
{"id": 281474976710662, "label": "", "properties": {"i": 1, "j": 2, "k": 3}}
@@ -1363,13 +1363,66 @@ RETURN n; n
13631363
{"id": 844424930131971, "label": "v", "properties": {"i": 1}}
13641364
(4 rows)
13651365

1366+
MATCH (n)
1367+
WHERE NOT EXISTS (
1368+
SELECT * FROM tst as t where t.i = n.i
1369+
)
1370+
RETURN n;
1371+
n
1372+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
1373+
{"id": 281474976710657, "label": "", "properties": {"int_key": 1, "map_key": {"key": "value"}, "list_key": [1, 2, 3], "float_key": 3.14, "string_key": "test"}}
1374+
{"id": 281474976710658, "label": "", "properties": {"lst": [1, null, 3.14, "string", {"key": "value"}, []]}}
1375+
{"id": 281474976710659, "label": "", "properties": {"name": "orphan"}}
1376+
{"id": 281474976710660, "label": "", "properties": {"name": "F"}}
1377+
{"id": 281474976710661, "label": "", "properties": {"name": "T"}}
1378+
{"id": 844424930131969, "label": "v", "properties": {}}
1379+
{"id": 844424930131970, "label": "v", "properties": {"i": 0}}
1380+
{"id": 1125899906842625, "label": "v1", "properties": {"id": "initial"}}
1381+
{"id": 1125899906842626, "label": "v1", "properties": {"id": "middle"}}
1382+
{"id": 1125899906842627, "label": "v1", "properties": {"id": "end"}}
1383+
{"id": 1688849860263937, "label": "v2", "properties": {"id": "initial"}}
1384+
{"id": 1688849860263938, "label": "v2", "properties": {"id": "middle"}}
1385+
{"id": 1688849860263939, "label": "v2", "properties": {"id": "end"}}
1386+
{"id": 2251799813685249, "label": "v3", "properties": {"id": "initial"}}
1387+
{"id": 2251799813685250, "label": "v3", "properties": {"id": "middle"}}
1388+
{"id": 2251799813685251, "label": "v3", "properties": {"id": "end"}}
1389+
{"id": 2814749767106561, "label": "loop", "properties": {"id": "initial"}}
1390+
{"id": 3377699720527873, "label": "duplicate", "properties": {}}
1391+
{"id": 3940649673949185, "label": "other_v", "properties": {}}
1392+
{"id": 3940649673949186, "label": "other_v", "properties": {}}
1393+
{"id": 4222124650659841, "label": "opt_match_v", "properties": {"name": "someone"}}
1394+
{"id": 4222124650659842, "label": "opt_match_v", "properties": {"name": "somebody"}}
1395+
{"id": 4222124650659843, "label": "opt_match_v", "properties": {"name": "anybody"}}
1396+
{"id": 4222124650659844, "label": "opt_match_v", "properties": {"name": "nobody"}}
1397+
(24 rows)
1398+
13661399
--
13671400
-- Clean up
13681401
--
13691402
DROP GRAPH cypher_match CASCADE;
1370-
ERROR: syntax error at or near "n"
1371-
LINE 1: n
1372-
^
1403+
NOTICE: drop cascades to 16 other objects
1404+
DETAIL: drop cascades to table cypher_match._ag_label_vertex
1405+
drop cascades to table cypher_match._ag_label_edge
1406+
drop cascades to table cypher_match.v
1407+
drop cascades to table cypher_match.v1
1408+
drop cascades to table cypher_match.e1
1409+
drop cascades to table cypher_match.v2
1410+
drop cascades to table cypher_match.e2
1411+
drop cascades to table cypher_match.v3
1412+
drop cascades to table cypher_match.e3
1413+
drop cascades to table cypher_match.loop
1414+
drop cascades to table cypher_match.self
1415+
drop cascades to table cypher_match.duplicate
1416+
drop cascades to table cypher_match.dup_edge
1417+
drop cascades to table cypher_match.other_v
1418+
drop cascades to table cypher_match.opt_match_v
1419+
drop cascades to table cypher_match.opt_match_e
1420+
NOTICE: graph "cypher_match" has been dropped
1421+
drop_graph
1422+
------------
1423+
1424+
(1 row)
1425+
13731426
--
13741427
-- End
13751428
--

regress/sql/cypher_match.sql

+24-1
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,31 @@ MATCH (n)
318318
WHERE EXISTS (
319319
SELECT * FROM tst as t where t.i = n.i
320320
)
321-
RETURN n; n
321+
RETURN n;
322322

323+
MATCH (n)
324+
WHERE NOT EXISTS (
325+
SELECT * FROM tst as t where t.i = n.i
326+
)
327+
RETURN n;
328+
329+
MATCH (n)
330+
WHERE n.i = ALL (
331+
SELECT i::postgraph.gtype FROM tst as t
332+
)
333+
RETURN n;
334+
335+
MATCH (n)
336+
WHERE n.i = ANY (
337+
SELECT i::postgraph.gtype FROM tst as t
338+
)
339+
RETURN n;
340+
341+
MATCH (n)
342+
WHERE n.i = SOME (
343+
SELECT i::postgraph.gtype FROM tst as t
344+
)
345+
RETURN n;
323346
--
324347
-- Clean up
325348
--

sql/postgraph-typecasting.sql.in

+12
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,18 @@ CREATE CAST (gtype AS bigint)
262262
WITH FUNCTION gtype_to_int8(gtype)
263263
AS ASSIGNMENT;
264264

265+
-- int4 -> gtype
266+
CREATE FUNCTION int4_to_gtype(int4)
267+
RETURNS gtype
268+
LANGUAGE c
269+
IMMUTABLE
270+
RETURNS NULL ON NULL INPUT
271+
PARALLEL SAFE
272+
AS 'MODULE_PATHNAME';
273+
274+
CREATE CAST (int4 AS gtype)
275+
WITH FUNCTION int4_to_gtype(int4);
276+
265277
-- int8[] -> gtype
266278
CREATE FUNCTION int8_array_to_gtype(int8[])
267279
RETURNS gtype

src/backend/parser/cypher_gram.y

+11
Original file line numberDiff line numberDiff line change
@@ -17145,6 +17145,17 @@ cypher_a_expr:
1714517145
n->location = @2;
1714617146
$$ = (Node *) n;
1714717147
}
17148+
| cypher_a_expr all_op sub_type select_with_parens %prec OPERATOR
17149+
{
17150+
SubLink *n = makeNode(SubLink);
17151+
n->subLinkType = $3;
17152+
n->subLinkId = 0;
17153+
n->testexpr = $1;
17154+
n->operName = list_make2(makeString("postgraph"), makeString($2));
17155+
n->subselect = $4;
17156+
n->location = @2;
17157+
$$ = (Node *) n;
17158+
}
1714817159
| cypher_expr_atom
1714917160
;
1715017161

src/backend/utils/adt/gtype_typecasting.c

+7
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,13 @@ int8_to_gtype(PG_FUNCTION_ARGS) {
14711471
AG_RETURN_GTYPE_P(integer_to_gtype(PG_GETARG_INT64(0)));
14721472
}
14731473

1474+
PG_FUNCTION_INFO_V1(int4_to_gtype);
1475+
// int4 -> gtype.
1476+
Datum
1477+
int4_to_gtype(PG_FUNCTION_ARGS) {
1478+
AG_RETURN_GTYPE_P(integer_to_gtype(PG_GETARG_INT32(0)));
1479+
}
1480+
14741481
PG_FUNCTION_INFO_V1(text_to_gtype);
14751482
//text -> gtype
14761483
Datum

0 commit comments

Comments
 (0)