]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Property references are preferred over regular column references
authorPeter Eisentraut <peter@eisentraut.org>
Tue, 31 Mar 2026 09:37:35 +0000 (11:37 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Tue, 31 Mar 2026 09:47:19 +0000 (11:47 +0200)
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
src/test/regress/expected/graph_table.out
src/test/regress/sql/graph_table.sql

index 312dfdc182ad218681434a78030d91197249a7ed..d5fc9e7d6bcce4eff269c48fafaeef3ce7c8682f 100644 (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
index 2fc5a2e8c5acafc34f99bba4729948347bcc671a..01f480d1a57e62a7797786ff10a1648b67e41584 100644 (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,
index 7c2438e84a3e35daac7afacb5ea6e6cfc20c4111..30e450b384245d4baecc872fcf40370c4b3c036e 100644 (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 (