From 83b76cacf96364137431510c6dd581550dd8bc36 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Fri, 27 Mar 2026 17:57:33 +0530
Subject: [PATCH v20260327 1/2] Property references are preferred over regular
 column references

When a ColumnRef can be resolved as a graph table property reference and a
lateral table column reference prefer the graph table property reference since
element pattern variables in the GRAPH_TABLE clause form the innermost
namespace.

Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
---
 src/backend/parser/parse_expr.c           | 14 ++++++++++----
 src/test/regress/expected/graph_table.out | 18 ++++++++++++++----
 src/test/regress/sql/graph_table.sql      |  6 +++++-
 3 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index 312dfdc182a..d5fc9e7d6bc 100644
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -614,6 +614,16 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref)
 			return node;
 	}
 
+	/*
+	 * Element pattern variables in a GRAPH_TABLE clause form the innermost
+	 * namespace since we do not allow subqueries in GRAPH_TABLE patterns. Try
+	 * to resolve the column reference as a graph table property reference
+	 * before trying to resolve it as a regular column reference.
+	 */
+	node = transformGraphTablePropertyRef(pstate, cref);
+	if (node != NULL)
+		return node;
+
 	/*----------
 	 * The allowed syntaxes are:
 	 *
@@ -826,10 +836,6 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref)
 			break;
 	}
 
-	/* Try it as a graph table property reference. */
-	if (node == NULL)
-		node = transformGraphTablePropertyRef(pstate, cref);
-
 	/*
 	 * Now give the PostParseColumnRefHook, if any, a chance.  We pass the
 	 * translation-so-far so that it can throw an error if it wishes in the
diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out
index 2fc5a2e8c5a..01f480d1a57 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -236,14 +236,24 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers)->(o IS orders) COLUMNS
 (2 rows)
 
 -- lateral test
-CREATE TABLE x1 (a int, b text);
+-- Use table with a column name same as a property in the property graph so as
+-- to test resolution preferences. Property references are preferred over
+-- lateral table references.
+CREATE TABLE x1 (a int, address text);
 INSERT INTO x1 VALUES (1, 'one'), (2, 'two');
 SELECT * FROM x1, GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US' AND c.customer_id = x1.a)-[IS customer_orders]->(o IS orders) COLUMNS (c.name AS customer_name, c.customer_id AS cid));
- a |  b  | customer_name | cid 
----+-----+---------------+-----
- 1 | one | customer1     |   1
+ a | address | customer_name | cid 
+---+---------+---------------+-----
+ 1 | one     | customer1     |   1
 (1 row)
 
+SELECT x1.a, g.* FROM x1, GRAPH_TABLE (myshop MATCH (x1 IS customers WHERE x1.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (x1.name AS customer_name, x1.customer_id AS cid, o.order_id)) g;
+ a | customer_name | cid | order_id 
+---+---------------+-----+----------
+ 1 | customer1     |   1 |        1
+ 2 | customer1     |   1 |        1
+(2 rows)
+
 DROP TABLE x1;
 CREATE TABLE v1 (
     id int PRIMARY KEY,
diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql
index 7c2438e84a3..30e450b3842 100644
--- a/src/test/regress/sql/graph_table.sql
+++ b/src/test/regress/sql/graph_table.sql
@@ -151,9 +151,13 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers)-[IS customer_orders | c
 SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers)->(o IS orders) COLUMNS (c.name, o.ordered_when)) ORDER BY 1;
 
 -- lateral test
-CREATE TABLE x1 (a int, b text);
+-- Use table with a column name same as a property in the property graph so as
+-- to test resolution preferences. Property references are preferred over
+-- lateral table references.
+CREATE TABLE x1 (a int, address text);
 INSERT INTO x1 VALUES (1, 'one'), (2, 'two');
 SELECT * FROM x1, GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US' AND c.customer_id = x1.a)-[IS customer_orders]->(o IS orders) COLUMNS (c.name AS customer_name, c.customer_id AS cid));
+SELECT x1.a, g.* FROM x1, GRAPH_TABLE (myshop MATCH (x1 IS customers WHERE x1.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (x1.name AS customer_name, x1.customer_id AS cid, o.order_id)) g;
 DROP TABLE x1;
 
 CREATE TABLE v1 (

base-commit: 6857947db5bbec823226cd5234f088524f933caa
-- 
2.34.1

