Subject: Bug: Missing collation assignment for GRAPH_TABLE COLUMNS expressions

From: SATYANARAYANA NARLAPURAM <[email-masked]>
Date: Fri, 10 Apr 2026 08:41:58 -0700

Hi hackers,

GRAPH_TABLE COLUMNS expressions that involve collation-dependent functions
or operators fail with:

  ERROR: could not determine which collation to use for upper() function
  HINT: Use the COLLATE clause to set the collation explicitly.

Setup:

  CREATE TABLE vtx (id int PRIMARY KEY, name text);
  CREATE TABLE edg (id int PRIMARY KEY,
                    src int REFERENCES vtx(id),
                    dst int REFERENCES vtx(id));
  INSERT INTO vtx VALUES (1,'Alice'),(2,'Bob'),(3,'Carol');
  INSERT INTO edg VALUES (1,1,2),(2,2,3);

  CREATE PROPERTY GRAPH g
    VERTEX TABLES (vtx KEY (id))
    EDGE TABLES (edg KEY (id)
      SOURCE KEY (src) REFERENCES vtx (id)
      DESTINATION KEY (dst) REFERENCES vtx (id));

postgres=# SELECT * FROM GRAPH_TABLE (g
  MATCH (a IS vtx)-[e IS edg]->(b IS vtx) COLUMNS (upper(a.name) AS
src_upper));
ERROR:  could not determine which collation to use for upper() function
HINT:  Use the COLLATE clause to set the collation explicitly.

In transformRangeGraphTable(), the COLUMNS transformation loop calls
transformExpr()
on each column expression but omits the subsequent assign_expr_collations()
call.  Both
WHERE clause transformation sites in parse_graphtable.c correctly include
it.

Attached a patch to fix this.

Thanks,
Satya

---

From: Ashutosh Bapat <[email-masked]>
Date: Fri, 10 Apr 2026 21:55:00 +0530

Hi Satya,
Thanks for the report and patch.

On Fri, Apr 10, 2026 at 9:12 PM SATYANARAYANA NARLAPURAM
<[email-masked]> wrote:
>
> Hi hackers,
>
> GRAPH_TABLE COLUMNS expressions that involve collation-dependent functions or operators fail with:
>
>   MATCH (a IS vtx)-[e IS edg]->(b IS vtx) COLUMNS (upper(a.name) AS src_upper));
> In transformRangeGraphTable(), the COLUMNS transformation loop calls transformExpr()
> on each column expression but omits the subsequent assign_expr_collations() call.  Both
> WHERE clause transformation sites in parse_graphtable.c correctly include it.
>

I think the fix is in the right direction. It's better to call
assign_expr_collation only once on all the columns at the end of loop
of rgt->columns, just like assign_expr_collation is called on all the
conditions in WHERE clause once.

Good to see tests also included in the patch. Do we need all three
queries? Also those queries should be placed near the section "-- test
collation specified in the expression" and add a query for explicit
collation in COLUMNs expression.

-- 
Best Wishes,
Ashutosh Bapat

---

From: SATYANARAYANA NARLAPURAM <[email-masked]>
Date: Fri, 10 Apr 2026 11:06:05 -0700

Hi Ashutosh,

On Fri, Apr 10, 2026 at 9:25 AM Ashutosh Bapat <[email-masked]>
wrote:

> conditions in WHERE clause once

Addressed this in v2 patch.

> collation in COLUMNs expression.

Removed two tests and moved the test. Explicit collate test already exists.

Thanks,
Satya

---

From: Ashutosh Bapat <[email-masked]>
Date: Wed, 15 Apr 2026 20:37:18 +0530

On Fri, Apr 10, 2026 at 11:36 PM SATYANARAYANA NARLAPURAM
<[email-masked]> wrote:
>
> Hi Ashutosh,
>
> On Fri, Apr 10, 2026 at 9:25 AM Ashutosh Bapat <[email-masked]> wrote:
>>
>> Hi Satya,

If we call assign_expr_collations() on a list, the List expression
also gets a collation, which isn't what we want here. We want to
assign collations to the individual COLUMNs expression independently.
assign_list_collations() is better suited for that. I must say that
your earlier patch had got it right in this regard since it was
calling assign_expr_collations independently on each COLUMNs
expression. However, considering that an all properties reference is
replaced by a list of GraphPropertyRefs in place, I think calling
assign_list_collations() once on all COLUMNs expressions is a
future-proof fix. This is also inline with how collations are assigned
to targetlist expressions in a Query.

> Removed two tests and moved the test. Explicit collate test already exists.

I merged this test into an existing test to avoid adding yet another
query in the file that has many many queries already. Yes, an explicit
collation test is not needed separately.

While at it I added comments to explain why we aren't performing
en-masse collation assignment on a GraphTable or GraphPathPattern.

Please let me know what you think of the attached patch.

--
Best Wishes,
Ashutosh Bapat
From 7481f0d29508fc656efdd5ff2a692eeea406ac7a Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <[email-masked]>
Date: Wed, 15 Apr 2026 16:18:37 +0530
Subject: [PATCH v20260415 1/3] 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. Expressions in COLUMNS clause missed that
treatment.

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

Reported-by: Satyanarayana Narlapuram <[email-masked]>
Author: Satyanarayana Narlapuram <[email-masked]>
Author: Ashutosh Bapat <[email-masked]>
Discussion: https://www.postgresql.org/message-id/[email-masked]
---
 src/backend/parser/parse_clause.c         |  6 ++++++
 src/backend/parser/parse_graphtable.c     | 12 +++++++++++-
 src/test/regress/expected/graph_table.out |  8 ++++----
 src/test/regress/sql/graph_table.sql      |  4 ++--
 4 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c
index 967eea44f1c..4270c2382c4 100644
--- a/src/backend/parser/parse_clause.c
+++ b/src/backend/parser/parse_clause.c
@@ -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;
diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c
index 30ddce5aa9f..87386859a5c 100644
--- a/src/backend/parser/parse_graphtable.c
+++ b/src/backend/parser/parse_graphtable.c
@@ -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)
diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out
index b579e3df635..057f283c43d 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -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));
diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql
index 4ff98817420..278064818ff 100644
--- a/src/test/regress/sql/graph_table.sql
+++ b/src/test/regress/sql/graph_table.sql
@@ -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";

base-commit: f30d0c720f2ec979ab1b5b44b1f9f201d6efdf8c
-- 
2.34.1

---

From: SATYANARAYANA NARLAPURAM <[email-masked]>
Date: Wed, 15 Apr 2026 10:00:02 -0700

Hi Ashutosh,

> to targetlist expressions in a Query.

Nice catch!

> collation test is not needed separately.

Thanks!

> Please let me know what you think of the attached patch.

This LGTM. I reran the tests and all of them are passing.

Thanks,
Satya

---

From: Peter Eisentraut <[email-masked]>
Date: Fri, 24 Apr 2026 08:52:38 +0200

> Please let me know what you think of the attached patch.

committed