Skip to content

Commit 1176c44

Browse files
ashutosh-bapatCommitfest Bot
authored and
Commitfest Bot
committed
support WHERE clause in graph pattern
1 parent ba80017 commit 1176c44

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

src/backend/rewrite/rewriteGraphTable.c

+9
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ generate_query_for_graph_path(RangeTblEntry *rte, List *graph_path)
313313
qual_exprs = list_concat(qual_exprs, gpe->qual_exprs);
314314
}
315315

316+
if (rte->graph_pattern->whereClause)
317+
{
318+
Node *path_quals = replace_property_refs(rte->relid,
319+
(Node *) rte->graph_pattern->whereClause,
320+
graph_path);
321+
322+
qual_exprs = lappend(qual_exprs, path_quals);
323+
}
324+
316325
path_query->jointree = makeFromExpr(fromlist,
317326
(Node *) makeBoolExpr(AND_EXPR, qual_exprs, -1));
318327

src/test/regress/expected/graph_table.out

+28-5
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ create table e1_2 (id_1 int,
237237
id_2_2 int,
238238
ename varchar(10),
239239
eprop1 int);
240+
-- edge connecting v2 and v1
241+
create table e2_1 (id_2_1 int,
242+
id_2_2 int,
243+
id_1 int,
244+
ename varchar(10),
245+
eprop1 int);
240246
-- edge connecting v1 and v3
241247
create table e1_3 (id_1 int,
242248
id_3 int,
@@ -268,6 +274,11 @@ edge tables (
268274
destination key (id_2_1, id_2_2) references v2 (id1, id2)
269275
label el1 properties (eprop1, ename)
270276
label l1 properties (ename as elname),
277+
e2_1 key (id_2_1, id_2_2, id_1)
278+
source key (id_2_1, id_2_2) references v2 (id1, id2)
279+
destination key (id_1) references v1 (id)
280+
label el1 properties (eprop1, ename)
281+
label l1 properties (ename as elname),
271282
e1_3
272283
source key (id_1) references v1 (id)
273284
destination key (id_3) references v3 (id)
@@ -296,17 +307,18 @@ insert into e1_2 values (1, 1000, 2, 'e121', 10001),
296307
insert into e1_3 values (1, 2003, 'e131', 10003),
297308
(1, 2001, 'e132', 10004);
298309
insert into e2_3 values (1000, 2, 2002, 'e231', 10005);
310+
insert into e2_1 values (1000, 1, 2, 'e211', 10006);
299311
-- empty element path pattern, counts number of edges in the graph
300312
SELECT count(*) FROM GRAPH_TABLE (g1 MATCH ()-[]->() COLUMNS (1 as one));
301313
count
302314
-------
303-
5
315+
6
304316
(1 row)
305317

306318
SELECT count(*) FROM GRAPH_TABLE (g1 MATCH ()->() COLUMNS (1 as one));
307319
count
308320
-------
309-
5
321+
6
310322
(1 row)
311323

312324
-- Vertex element v2 has label vl3 which exposes property vprop1. But vl3 is
@@ -337,6 +349,14 @@ select src, conn, dest, lprop1, vprop2, vprop1 from graph_table (g1 match (a is
337349
v11 | e132 | v31 | vl3_prop | | 2010
338350
(4 rows)
339351

352+
-- WHERE clause in graph pattern
353+
SELECT self, through FROM GRAPH_TABLE (g1 MATCH (a)->(b)->(c) WHERE a.vname = c.vname and a.vname <> b.vname COLUMNS (a.vname as self, b.vname as through));
354+
self | through
355+
------+---------
356+
v12 | v21
357+
v21 | v12
358+
(2 rows)
359+
340360
-- Errors
341361
-- vl1 is not associated with property vprop2
342362
select src, src_vprop2, conn, dest from graph_table (g1 match (a is vl1)-[b is el1]->(c is vl2 | vl3) columns (a.vname as src, a.vprop2 as src_vprop2, b.ename as conn, c.vname as dest));
@@ -362,8 +382,9 @@ select * from graph_table (g1 match (src)-[conn]->(dest) columns (src.vname as s
362382
v11 | e121 | v22 | 10 | | | 1020 | 1200 | vl2_prop | 10001 |
363383
v11 | e131 | v33 | 10 | | | 2030 | | vl3_prop | 10003 |
364384
v11 | e132 | v31 | 10 | | | 2010 | | vl3_prop | 10004 |
385+
v21 | e211 | v12 | 1010 | 1100 | vl2_prop | 20 | | | 10006 |
365386
v22 | e231 | v32 | 1020 | 1200 | vl2_prop | 2020 | | vl3_prop | | 100050
366-
(5 rows)
387+
(6 rows)
367388

368389
-- three label disjunction
369390
select * from graph_table (g1 match (src IS vl1 | vl2 | vl3)-[conn]->(dest) columns (src.vname as svname, conn.ename as cename, dest.vname as dvname));
@@ -373,8 +394,9 @@ select * from graph_table (g1 match (src IS vl1 | vl2 | vl3)-[conn]->(dest) colu
373394
v11 | e121 | v22
374395
v11 | e131 | v33
375396
v11 | e132 | v31
397+
v21 | e211 | v12
376398
v22 | e231 | v32
377-
(5 rows)
399+
(6 rows)
378400

379401
-- graph'ical query: find a vertex which is not connected to any other vertex as a source or a destination.
380402
with all_connected_vertices as (select svn, dvn from graph_table (g1 match (src)-[conn]->(dest) columns (src.vname as svn, dest.vname as dvn))),
@@ -394,8 +416,9 @@ select sn, cn, dn from graph_table (g1 match (src : l1)-[conn : l1]->(dest : l1)
394416
v11 | e121 | v22
395417
v11 | e131 | v33
396418
v11 | e132 | v31
419+
v21 | e211 | v12
397420
v22 | e231 | v32
398-
(5 rows)
421+
(6 rows)
399422

400423
-- property graph with some of the elements, labels and properties same as the
401424
-- previous one. Test whether components from the specified property graph are

src/test/regress/sql/graph_table.sql

+14
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ create table e1_2 (id_1 int,
169169
id_2_2 int,
170170
ename varchar(10),
171171
eprop1 int);
172+
-- edge connecting v2 and v1
173+
create table e2_1 (id_2_1 int,
174+
id_2_2 int,
175+
id_1 int,
176+
ename varchar(10),
177+
eprop1 int);
172178

173179
-- edge connecting v1 and v3
174180
create table e1_3 (id_1 int,
@@ -203,6 +209,11 @@ edge tables (
203209
destination key (id_2_1, id_2_2) references v2 (id1, id2)
204210
label el1 properties (eprop1, ename)
205211
label l1 properties (ename as elname),
212+
e2_1 key (id_2_1, id_2_2, id_1)
213+
source key (id_2_1, id_2_2) references v2 (id1, id2)
214+
destination key (id_1) references v1 (id)
215+
label el1 properties (eprop1, ename)
216+
label l1 properties (ename as elname),
206217
e1_3
207218
source key (id_1) references v1 (id)
208219
destination key (id_3) references v3 (id)
@@ -236,6 +247,7 @@ insert into e1_2 values (1, 1000, 2, 'e121', 10001),
236247
insert into e1_3 values (1, 2003, 'e131', 10003),
237248
(1, 2001, 'e132', 10004);
238249
insert into e2_3 values (1000, 2, 2002, 'e231', 10005);
250+
insert into e2_1 values (1000, 1, 2, 'e211', 10006);
239251

240252
-- empty element path pattern, counts number of edges in the graph
241253
SELECT count(*) FROM GRAPH_TABLE (g1 MATCH ()-[]->() COLUMNS (1 as one));
@@ -250,6 +262,8 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1 | vl2) COLUMNS (a.vname,
250262
a.vprop1));
251263
-- vprop2 is associated with vl2 but not vl3
252264
select src, conn, dest, lprop1, vprop2, vprop1 from graph_table (g1 match (a is vl1)-[b is el1]->(c is vl2 | vl3) columns (a.vname as src, b.ename as conn, c.vname as dest, c.lprop1, c.vprop2, c.vprop1));
265+
-- WHERE clause in graph pattern
266+
SELECT self, through FROM GRAPH_TABLE (g1 MATCH (a)->(b)->(c) WHERE a.vname = c.vname and a.vname <> b.vname COLUMNS (a.vname as self, b.vname as through));
253267

254268
-- Errors
255269
-- vl1 is not associated with property vprop2

0 commit comments

Comments
 (0)