Fix collation of expressions in GRAPH_TABLE COLUMNS clause

GRAPH_TABLE clause is converted into a rangetable entry, which is
ignored by assign_query_collations().  Hence we assign collations
while transforming its parts.  But expressions in COLUMNS clause
missed that treatment, so fix that.

While at it, also add comments about collation assignment to the parts
of GRAPH_TABLE clause, and also fix a small grammar issue.

Reported-by: Satyanarayana Narlapuram <satyanarlapuram@gmail.com>
Author: Satyanarayana Narlapuram <satyanarlapuram@gmail.com>
Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Discussion: https://www.postgresql.org/message-id/CAHg+QDc4aaiufYSgrwMMPMMRTPtQ66SghcrPFbWJFZMqNaG+BA@mail.gmail.com
This commit is contained in:
Peter Eisentraut 2026-04-24 08:43:26 +02:00
parent 9082680c34
commit ac3bcc041c
4 changed files with 23 additions and 7 deletions

View file

@ -1003,6 +1003,12 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt)
columns = lappend(columns, te);
}
/*
* Assign collations to column expressions now since
* assign_query_collations() does not process rangetable entries.
*/
assign_list_collations(pstate, columns);
table_close(rel, NoLock);
pstate->p_graph_table_pstate = NULL;

View file

@ -252,6 +252,11 @@ transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep)
gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr);
gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE);
/*
* Assign collations here for the reason mentioned in the prologue of
* transformGraphPattern().
*/
assign_expr_collations(pstate, gep->whereClause);
gpstate->cur_gep = NULL;
@ -366,9 +371,14 @@ transformPathPatternList(ParseState *pstate, List *path_pattern)
* Transform a GraphPattern.
*
* A GraphPattern consists of a list of one or more path patterns and an
* optional where clause. Transform them. We use the previously constructure
* optional where clause. Transform them. We use the previously constructed
* list of variables in the GraphTableParseState to resolve property references
* in the WHERE clause.
*
* Since most parts of the GraphPattern do not require collation assignment, we
* assign collations to the required expressions as they are transformed. This
* avoids the need to traverse the whole GraphPattern again and avoids exposing
* it to assign_expr_collations().
*/
Node *
transformGraphPattern(ParseState *pstate, GraphPattern *graph_pattern)

View file

@ -652,13 +652,13 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-(a) COLUMNS (a.vname AS self));
v33
(1 row)
-- test collation specified in the expression
-- test explicit and implicit collation assignment
INSERT INTO e3_3 VALUES (2003, 2003, 'E331', 10011);
SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b]->(a)-[b]->(a) COLUMNS (a.vname AS self, b.ename AS loop_name)) ORDER BY loop_name COLLATE "C" ASC;
SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b]->(a)-[b]->(a) COLUMNS (upper(a.vname) AS self, b.ename AS loop_name)) ORDER BY loop_name COLLATE "C" ASC;
self | loop_name
------+-----------
v33 | E331
v33 | e331
V33 | E331
V33 | e331
(2 rows)
SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b IS el2 WHERE b.ename > 'E331' COLLATE "C"]->(a)-[b]->(a) COLUMNS (a.vname AS self, b.ename AS loop_name));

View file

@ -394,9 +394,9 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b]->(c)-[b]->(d) COLUMNS (a.vname AS an
SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[c]-(a) COLUMNS (a.vname AS self, c.ename AS loop_name));
SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-(a) COLUMNS (a.vname AS self));
-- test collation specified in the expression
-- test explicit and implicit collation assignment
INSERT INTO e3_3 VALUES (2003, 2003, 'E331', 10011);
SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b]->(a)-[b]->(a) COLUMNS (a.vname AS self, b.ename AS loop_name)) ORDER BY loop_name COLLATE "C" ASC;
SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b]->(a)-[b]->(a) COLUMNS (upper(a.vname) AS self, b.ename AS loop_name)) ORDER BY loop_name COLLATE "C" ASC;
SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b IS el2 WHERE b.ename > 'E331' COLLATE "C"]->(a)-[b]->(a) COLUMNS (a.vname AS self, b.ename AS loop_name));
SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b]->(a)-[b]->(a) WHERE b.ename > 'E331' COLLATE "C" COLUMNS (a.vname AS self, b.ename AS loop_name));
SELECT * FROM GRAPH_TABLE (g1 MATCH (a)-[b]->(a)-[b]->(a) COLUMNS (a.vname AS self, b.ename AS loop_name)) WHERE loop_name > 'E331' COLLATE "C";