On Wed, Mar 18, 2026 at 6:45 PM Junwang Zhao <[email protected]> wrote:
>
> On Wed, Mar 18, 2026 at 7:49 PM Henson Choi <[email protected]> wrote:
> >
> > Hi Junwang,
> >
> >>
> >> Yeah, I noticed that too, I will compose a patch for further review.
> >
> >
> > Great, looking forward to reviewing it.
>
> Here.

Thanks for the patch. I developed a similar patch parallely; attached
here. But I used the existing objects in graph_table.sql to add more
tests instead of creating a whole new set of objects. The new queries
test both the vertex as well as edge patterns as suggested by Henson.

In my patch I have added a test for forward reference, but didn't
implement the support for the same. I am not sure whether we want to
support forward references right now, for the reasons mentioned below

a. Other products allow cross referencing, but it seems it's optional
in SQL standard. Conformance rule 11 of subclause 10.6 seems to make
the feature optional. If that's the case, we need to throw an error in
case of cross reference. Peter, what do you think?

b. The graph pattern handling code will go through more revisions as
we support path list, nested path patterns, path pattern quantifiers
and other ways to specify path patterns. Implementing forward
referencing right now, in a way, that fits the current code might
create more work in future revisions. Doing it after we have a few
more of the above list supported, will require comparatively less
work. We may require subqueries to represent some path patterns like
embedded path patterns or path patterns with quantifiers. Forward
referencing in FROM clause is now allowed, so we will need to do magic
with condition pull to support forward referencing. Delaying the
forward referencing support till then is better.

c. It's not a blocker: users can write the forward referencing
condition in the graph pattern where clause instead of element pattern
where clause.

-- 
Best Wishes,
Ashutosh Bapat
From 36d3b42ffde4d89a25df502886ee47685f39003c Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <[email protected]>
Date: Wed, 18 Mar 2026 19:27:38 +0530
Subject: [PATCH v20260318 2/2] Cross variable references in graph pattern
 causes segfault

When converting the WHERE clause in an element pattern,
generate_query_for_graph_path() calls replace_property_refs() to replace the
property references in it.  It passes only the current element as the context
for replacement. If there are references to variables from other element
patterns, it causes a segmentation fault since it does not find path_element
object corresponding to those variable. Fix the issue by passing all the path
elements in the graph pattern as context for replacement.

Author: Ashutosh Bapat <[email protected]>
Reported by: Man Zeng <[email protected]>
Reviewed by: Junwang Zhao <[email protected]>
Reviewed by: Henson Choi <[email protected]>
---
 src/backend/rewrite/rewriteGraphTable.c   |  2 +-
 src/test/regress/expected/graph_table.out | 14 ++++++++++++++
 src/test/regress/sql/graph_table.sql      |  4 ++++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/backend/rewrite/rewriteGraphTable.c b/src/backend/rewrite/rewriteGraphTable.c
index 06f2f3442d8..d3f382573fd 100644
--- a/src/backend/rewrite/rewriteGraphTable.c
+++ b/src/backend/rewrite/rewriteGraphTable.c
@@ -519,7 +519,7 @@ generate_query_for_graph_path(RangeTblEntry *rte, List *graph_path)
 		{
 			Node	   *tr;
 
-			tr = replace_property_refs(rte->relid, pf->whereClause, list_make1(pe));
+			tr = replace_property_refs(rte->relid, pf->whereClause, graph_path);
 
 			qual_exprs = lappend(qual_exprs, tr);
 		}
diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out
index 27c81ec6e42..eeaf1b85121 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -395,6 +395,20 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (v1 IS vl2)-(v2) COLUMNS (v1.vname AS v1name
  v22    | v32
 (3 rows)
 
+-- cross variable references
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b WHERE a.vprop1 = 10]->(c) COLUMNS (a.vname AS aname, a.vprop1 AS ap1, b.ename AS bname, b.eprop1 AS bp1, c.vname AS cname, c.vprop1 AS cp1)) ORDER BY aname, bname, cname;
+ aname | ap1 | bname |  bp1  | cname | cp1  
+-------+-----+-------+-------+-------+------
+ v11   |  10 | e121  | 10001 | v22   | 1020
+ v11   |  10 | e131  | 10003 | v33   | 2030
+ v11   |  10 | e132  | 10004 | v31   | 2010
+(3 rows)
+
+-- forward variable reference, not supported
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a WHERE b.eprop1 = 10001)-[b]->(c) COLUMNS (a.vname AS aname, a.vprop1 AS ap1, b.ename AS bname, b.eprop1 AS bp1, c.vname AS cname, c.vprop1 AS cp1)) ORDER BY aname, bname, cname;
+ERROR:  missing FROM-clause entry for table "b"
+LINE 1: SELECT * FROM GRAPH_TABLE (g1 MATCH (a WHERE b.eprop1 = 1000...
+                                                     ^
 -- Errors
 -- vl1 is not associated with property vprop2
 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));
diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql
index 6d2b08b997b..c3a35b69e92 100644
--- a/src/test/regress/sql/graph_table.sql
+++ b/src/test/regress/sql/graph_table.sql
@@ -274,6 +274,10 @@ SELECT src, conn, dest, lprop1, vprop2, vprop1 FROM GRAPH_TABLE (g1 MATCH (a IS
 -- edges directed in both ways - to and from v2
 SELECT * FROM GRAPH_TABLE (g1 MATCH (v1 IS vl2)-[conn]-(v2) COLUMNS (v1.vname AS v1name, conn.ename AS cname, v2.vname AS v2name));
 SELECT * FROM GRAPH_TABLE (g1 MATCH (v1 IS vl2)-(v2) COLUMNS (v1.vname AS v1name, v2.vname AS v2name));
+-- cross variable references
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b WHERE a.vprop1 = 10]->(c) COLUMNS (a.vname AS aname, a.vprop1 AS ap1, b.ename AS bname, b.eprop1 AS bp1, c.vname AS cname, c.vprop1 AS cp1)) ORDER BY aname, bname, cname;
+-- forward variable reference, not supported
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a WHERE b.eprop1 = 10001)-[b]->(c) COLUMNS (a.vname AS aname, a.vprop1 AS ap1, b.ename AS bname, b.eprop1 AS bp1, c.vname AS cname, c.vprop1 AS cp1)) ORDER BY aname, bname, cname;
 
 -- Errors
 -- vl1 is not associated with property vprop2
-- 
2.34.1

Reply via email to