From c9e6b0df945e90bb0be43c88d1c55e1ee0a9a982 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Wed, 29 Apr 2026 19:07:13 +0530
Subject: [PATCH v20260429 5/5] Resolve unknown-type literals in GRAPH_TABLE
 COLUMNS

The unknown-type literals in the COLUMNS clause of a GRAPH_TABLE are now
resolved to the appropriate types, causing various failures. Call
resolveTargetListUnknowns() on the columns targetlist to resolve unknonw type
literals. Do this before assigning collations so that the resolved types are
used for collations assignment.

Reported by: Satya Narlapuram <satyanarlapuram@gmail.com>
Author: Satya Narlapuram <satyanarlapuram@gmail.com>
Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Reviewed by: Junwang Zhao <zhjwpku@gmail.com>
Discussion: https://www.postgresql.org/message-id/CAHg+QDcyKNWyzDoKMxiZNjv7C-wAxs8y0ZoNkOV137Y+nk3UXg@mail.gmail.com
---
 src/backend/parser/parse_clause.c         |  4 ++++
 src/test/regress/expected/graph_table.out | 11 +++++++++++
 src/test/regress/sql/graph_table.sql      |  6 ++++++
 3 files changed, 21 insertions(+)

diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c
index 4270c2382c4..3d0585b8efb 100644
--- a/src/backend/parser/parse_clause.c
+++ b/src/backend/parser/parse_clause.c
@@ -1003,6 +1003,10 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt)
 		columns = lappend(columns, te);
 	}
 
+	/* resolve any still-unresolved output columns as being type text */
+	if (pstate->p_resolve_unknowns)
+		resolveTargetListUnknowns(pstate, columns);
+
 	/*
 	 * Assign collations to column expressions now since
 	 * assign_query_collations() does not process rangetable entries.
diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out
index 8038fcd39b7..5ef98e281e6 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -1047,4 +1047,15 @@ SELECT src.vname, count(*)
  v11   |     1
 (3 rows)
 
+-- Unknown type resolution
+SELECT c1, pg_typeof(c1), "null", pg_typeof("null") FROM
+    (SELECT *FROM GRAPH_TABLE (g1 MATCH (a IS vl1) COLUMNS ('1' AS c1, NULL as "null"))
+     UNION
+     SELECT 2, NULL);
+ c1 | pg_typeof | null | pg_typeof 
+----+-----------+------+-----------
+  1 | integer   |      | text
+  2 | integer   |      | text
+(2 rows)
+
 -- leave the objects behind for pg_upgrade/pg_dump tests
diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql
index a3681c6c0ef..d3ddcdc8e20 100644
--- a/src/test/regress/sql/graph_table.sql
+++ b/src/test/regress/sql/graph_table.sql
@@ -599,4 +599,10 @@ SELECT src.vname, count(*)
                                           COLUMNS (a.vname AS n))
                        WHERE n = src.vname);
 
+-- Unknown type resolution
+SELECT c1, pg_typeof(c1), "null", pg_typeof("null") FROM
+    (SELECT *FROM GRAPH_TABLE (g1 MATCH (a IS vl1) COLUMNS ('1' AS c1, NULL as "null"))
+     UNION
+     SELECT 2, NULL);
+
 -- leave the objects behind for pg_upgrade/pg_dump tests
-- 
2.34.1

