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
This commit is contained in:
Peter Eisentraut 2026-03-31 11:37:35 +02:00
parent 68a8601ee9
commit c5b3253b8a
3 changed files with 29 additions and 9 deletions

View file

@ -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

View file

@ -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,

View file

@ -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 (