postgres.git / summary / log / commit / refs
commit c5b3253b8abd7b89689d1095c7c1154b784ca9c9
Author: Peter Eisentraut <peter@eisentraut.org>
Date: Tue Mar 31 09:37:35 2026 +0000
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>
Reviewed-by: Henson Choi <assam258@gmail.com>
Discussion: https://www.postgresql.org/message-id/CAExHW5u6AoDfNg4%3DR5eVJn_bJn%3DC%3DwVPrto02P_06fxy39fniA%40mail.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 (
[parent: 68a8601ee9ec]