public inbox for [email protected]  
help / color / mirror / Atom feed
[PATCH] Resolve unknown-type literals in GRAPH_TABLE COLUMNS
6+ messages / 4 participants
[nested] [flat]

* [PATCH] Resolve unknown-type literals in GRAPH_TABLE COLUMNS
@ 2026-04-25 19:52  SATYANARAYANA NARLAPURAM <[email protected]>
  0 siblings, 1 reply; 6+ messages in thread

From: SATYANARAYANA NARLAPURAM @ 2026-04-25 19:52 UTC (permalink / raw)
  To: PostgreSQL Hackers <[email protected]>; Ashutosh Bapat <[email protected]>

Hi hackers,

transformRangeGraphTable() calls transformExpr() and
assign_list_collations() for COLUMNS expressions but missed calling
resolveTargetListUnknowns(). As a result, literals such as 'val1'
in a COLUMNS clause retained type "unknown", causing failures with
ORDER BY, UNION, and output conversions.

Fix by calling resolveTargetListUnknowns() on the columns target
list right after assign_list_collations(), similar to SELECT target lists
in
transformSelectStmt().

Attached a patch to fix this, which also includes test cases to reproduce.


Thanks,
Satya


Attachments:

  [application/octet-stream] 0001-Resolve-unknown-type-literals-in-GRAPH_TABLE-COLUMNS.patch (4.3K, 3-0001-Resolve-unknown-type-literals-in-GRAPH_TABLE-COLUMNS.patch)
  download | inline diff:
From efa000905343f6c2512f363c0013c19fbad5b8a5 Mon Sep 17 00:00:00 2001
From: Satya Narlapuram <[email protected]>
Date: Sat, 25 Apr 2026 17:30:43 +0000
Subject: [PATCH] Resolve unknown-type literals in GRAPH_TABLE COLUMNS
 expressions

transformRangeGraphTable() calls transformExpr() and
assign_list_collations() for COLUMNS expressions but missed calling
resolveTargetListUnknowns(). As a result, literals such as 'col1' 
in a COLUMNS clause retained type "unknown", causing failures with 
ORDER BY, UNION, and output conversions.

Fix by calling resolveTargetListUnknowns() on the columns target
list right after assign_list_collations(), similar to SELECT target 
lists in transformSelectStmt().
---
 src/backend/parser/parse_clause.c         |  3 ++
 src/test/regress/expected/graph_table.out | 54 +++++++++++++++++++++++
 src/test/regress/sql/graph_table.sql      | 21 +++++++++
 3 files changed, 78 insertions(+)

diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c
index f3736979..77981850 100644
--- a/src/backend/parser/parse_clause.c
+++ b/src/backend/parser/parse_clause.c
@@ -1010,6 +1010,9 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt)
 	 */
 	assign_list_collations(pstate, columns);
 
+	/* Resolve unknown-type literals to text */
+	resolveTargetListUnknowns(pstate, columns);
+
 	table_close(rel, NoLock);
 
 	pstate->p_graph_table_pstate = NULL;
diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out
index d3c20610..212fa29f 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -1110,4 +1110,58 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1) COLUMNS (upper(a.vname) AS n)) OR
  V13
 (3 rows)
 
+-- String and NULL literals in COLUMNS must resolve to proper types
+-- (not remain as "unknown")
+SELECT pg_typeof(c1), c1
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1)) ORDER BY n;
+ pg_typeof |  c1  
+-----------+------
+ text      | val1
+ text      | val1
+ text      | val1
+(3 rows)
+
+-- ORDER BY on a literal column
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1)) ORDER BY c1, n;
+  n  |  c1  
+-----+------
+ v11 | val1
+ v12 | val1
+ v13 | val1
+(3 rows)
+
+-- UNION between GRAPH_TABLE literal column and plain text
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1))
+  UNION ALL SELECT 'val2', 'val3';
+  n   |  c1  
+------+------
+ v11  | val1
+ v12  | val1
+ v13  | val1
+ val2 | val3
+(4 rows)
+
+-- NULL literal must not cause failures
+SELECT n, pg_typeof(c1)
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, NULL AS c1)) ORDER BY n;
+  n  | pg_typeof 
+-----+-----------
+ v11 | text
+ v12 | text
+ v13 | text
+(3 rows)
+
+-- integer literal type is preserved
+SELECT pg_typeof(c1)
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (42 AS c1)) LIMIT 1;
+ pg_typeof 
+-----------
+ integer
+(1 row)
+
 -- 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 2521c4ca..3d74ab4c 100644
--- a/src/test/regress/sql/graph_table.sql
+++ b/src/test/regress/sql/graph_table.sql
@@ -634,4 +634,25 @@ SELECT src.vname, count(*)
 SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1) COLUMNS (a.vname || '!' AS n)) ORDER BY n;
 SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1) COLUMNS (upper(a.vname) AS n)) ORDER BY n;
 
+-- String and NULL literals in COLUMNS must resolve to proper types
+-- (not remain as "unknown")
+SELECT pg_typeof(c1), c1
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1)) ORDER BY n;
+-- ORDER BY on a literal column
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1)) ORDER BY c1, n;
+-- UNION between GRAPH_TABLE literal column and plain text
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1))
+  UNION ALL SELECT 'val2', 'val3';
+-- NULL literal must not cause failures
+SELECT n, pg_typeof(c1)
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, NULL AS c1)) ORDER BY n;
+-- integer literal type is preserved
+SELECT pg_typeof(c1)
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (42 AS c1)) LIMIT 1;
+
 -- leave the objects behind for pg_upgrade/pg_dump tests
-- 
2.43.0



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: [PATCH] Resolve unknown-type literals in GRAPH_TABLE COLUMNS
@ 2026-04-26 15:10  Junwang Zhao <[email protected]>
  parent: SATYANARAYANA NARLAPURAM <[email protected]>
  0 siblings, 1 reply; 6+ messages in thread

From: Junwang Zhao @ 2026-04-26 15:10 UTC (permalink / raw)
  To: SATYANARAYANA NARLAPURAM <[email protected]>; +Cc: PostgreSQL Hackers <[email protected]>; Ashutosh Bapat <[email protected]>

Hi SATYANARAYANA,

On Sun, Apr 26, 2026 at 3:53 AM SATYANARAYANA NARLAPURAM
<[email protected]> wrote:
>
> Hi hackers,
>
> transformRangeGraphTable() calls transformExpr() and
> assign_list_collations() for COLUMNS expressions but missed calling
> resolveTargetListUnknowns(). As a result, literals such as 'val1'
> in a COLUMNS clause retained type "unknown", causing failures with
> ORDER BY, UNION, and output conversions.
>
> Fix by calling resolveTargetListUnknowns() on the columns target
> list right after assign_list_collations(), similar to SELECT target lists in
> transformSelectStmt().
>
> Attached a patch to fix this, which also includes test cases to reproduce.

I can reproduce this and the patch fixes it.

One question: why is resolveTargetListUnknowns called after
assign_list_collations?

I'm asking because in transformSelectStmt, resolveTargetListUnknowns
is invoked before assign_query_collations. It might not matter, but keeping
the order consistent would be good for readers.

>
>
> Thanks,
> Satya
>
>


-- 
Regards
Junwang Zhao





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: [PATCH] Resolve unknown-type literals in GRAPH_TABLE COLUMNS
@ 2026-04-27 06:04  SATYANARAYANA NARLAPURAM <[email protected]>
  parent: Junwang Zhao <[email protected]>
  0 siblings, 1 reply; 6+ messages in thread

From: SATYANARAYANA NARLAPURAM @ 2026-04-27 06:04 UTC (permalink / raw)
  To: Junwang Zhao <[email protected]>; +Cc: PostgreSQL Hackers <[email protected]>; Ashutosh Bapat <[email protected]>

Hi,

On Sun, Apr 26, 2026 at 8:10 AM Junwang Zhao <[email protected]> wrote:

> Hi SATYANARAYANA,
>
> On Sun, Apr 26, 2026 at 3:53 AM SATYANARAYANA NARLAPURAM
> <[email protected]> wrote:
> >
> > Hi hackers,
> >
> > transformRangeGraphTable() calls transformExpr() and
> > assign_list_collations() for COLUMNS expressions but missed calling
> > resolveTargetListUnknowns(). As a result, literals such as 'val1'
> > in a COLUMNS clause retained type "unknown", causing failures with
> > ORDER BY, UNION, and output conversions.
> >
> > Fix by calling resolveTargetListUnknowns() on the columns target
> > list right after assign_list_collations(), similar to SELECT target
> lists in
> > transformSelectStmt().
> >
> > Attached a patch to fix this, which also includes test cases to
> reproduce.
>
> I can reproduce this and the patch fixes it.
>
> One question: why is resolveTargetListUnknowns called after
> assign_list_collations?
>
> I'm asking because in transformSelectStmt, resolveTargetListUnknowns
> is invoked before assign_query_collations. It might not matter, but keeping
> the order consistent would be good for readers.


Updated the patch. It should be before.


Thanks,
Satya


Attachments:

  [application/octet-stream] v2-0001-Resolve-unknown-type-literals-in-GRAPH_TABLE-COLUMNS.patch (4.1K, 3-v2-0001-Resolve-unknown-type-literals-in-GRAPH_TABLE-COLUMNS.patch)
  download | inline diff:
From: Satya Narlapuram <[email protected]>
Subject: [PATCH] Resolve unknown-type literals in GRAPH_TABLE COLUMNS expressions

transformRangeGraphTable() calls transformExpr() and
assign_list_collations() for COLUMNS expressions but missed calling
resolveTargetListUnknowns(). As a result, literals such as 'col1'
in a COLUMNS clause retained type "unknown", causing failures with
ORDER BY, UNION, and output conversions.

Fix by calling resolveTargetListUnknowns() on the columns target
list right before assign_list_collations(), similar to SELECT target
lists in transformSelectStmt().
---
diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c
index f3736979..f679f510 100644
--- a/src/backend/parser/parse_clause.c
+++ b/src/backend/parser/parse_clause.c
@@ -1004,6 +1004,9 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt)
 		columns = lappend(columns, te);
 	}
 
+	/* Resolve unknown-type literals to text */
+	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 d3c20610..212fa29f 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -1110,4 +1110,58 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1) COLUMNS (upper(a.vname) AS n)) OR
  V13
 (3 rows)
 
+-- String and NULL literals in COLUMNS must resolve to proper types
+-- (not remain as "unknown")
+SELECT pg_typeof(c1), c1
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1)) ORDER BY n;
+ pg_typeof |  c1  
+-----------+------
+ text      | val1
+ text      | val1
+ text      | val1
+(3 rows)
+
+-- ORDER BY on a literal column
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1)) ORDER BY c1, n;
+  n  |  c1  
+-----+------
+ v11 | val1
+ v12 | val1
+ v13 | val1
+(3 rows)
+
+-- UNION between GRAPH_TABLE literal column and plain text
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1))
+  UNION ALL SELECT 'val2', 'val3';
+  n   |  c1  
+------+------
+ v11  | val1
+ v12  | val1
+ v13  | val1
+ val2 | val3
+(4 rows)
+
+-- NULL literal must not cause failures
+SELECT n, pg_typeof(c1)
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, NULL AS c1)) ORDER BY n;
+  n  | pg_typeof 
+-----+-----------
+ v11 | text
+ v12 | text
+ v13 | text
+(3 rows)
+
+-- integer literal type is preserved
+SELECT pg_typeof(c1)
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (42 AS c1)) LIMIT 1;
+ pg_typeof 
+-----------
+ integer
+(1 row)
+
 -- 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 2521c4ca..3d74ab4c 100644
--- a/src/test/regress/sql/graph_table.sql
+++ b/src/test/regress/sql/graph_table.sql
@@ -634,4 +634,25 @@ SELECT src.vname, count(*)
 SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1) COLUMNS (a.vname || '!' AS n)) ORDER BY n;
 SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1) COLUMNS (upper(a.vname) AS n)) ORDER BY n;
 
+-- String and NULL literals in COLUMNS must resolve to proper types
+-- (not remain as "unknown")
+SELECT pg_typeof(c1), c1
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1)) ORDER BY n;
+-- ORDER BY on a literal column
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1)) ORDER BY c1, n;
+-- UNION between GRAPH_TABLE literal column and plain text
+SELECT * FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, 'val1' AS c1))
+  UNION ALL SELECT 'val2', 'val3';
+-- NULL literal must not cause failures
+SELECT n, pg_typeof(c1)
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (a.vname AS n, NULL AS c1)) ORDER BY n;
+-- integer literal type is preserved
+SELECT pg_typeof(c1)
+  FROM GRAPH_TABLE (g1 MATCH (a IS vl1)
+    COLUMNS (42 AS c1)) LIMIT 1;
+
 -- leave the objects behind for pg_upgrade/pg_dump tests


^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: [PATCH] Resolve unknown-type literals in GRAPH_TABLE COLUMNS
@ 2026-04-29 14:09  Ashutosh Bapat <[email protected]>
  parent: SATYANARAYANA NARLAPURAM <[email protected]>
  0 siblings, 1 reply; 6+ messages in thread

From: Ashutosh Bapat @ 2026-04-29 14:09 UTC (permalink / raw)
  To: SATYANARAYANA NARLAPURAM <[email protected]>; +Cc: Junwang Zhao <[email protected]>; PostgreSQL Hackers <[email protected]>

On Mon, Apr 27, 2026 at 11:34 AM SATYANARAYANA NARLAPURAM
<[email protected]> wrote:
>
> Hi,
>
> On Sun, Apr 26, 2026 at 8:10 AM Junwang Zhao <[email protected]> wrote:
>>
>> Hi SATYANARAYANA,
>>
>> On Sun, Apr 26, 2026 at 3:53 AM SATYANARAYANA NARLAPURAM
>> <[email protected]> wrote:
>> >
>> > Hi hackers,
>> >
>> > transformRangeGraphTable() calls transformExpr() and
>> > assign_list_collations() for COLUMNS expressions but missed calling
>> > resolveTargetListUnknowns(). As a result, literals such as 'val1'
>> > in a COLUMNS clause retained type "unknown", causing failures with
>> > ORDER BY, UNION, and output conversions.
>> >
>> > Fix by calling resolveTargetListUnknowns() on the columns target
>> > list right after assign_list_collations(), similar to SELECT target lists in
>> > transformSelectStmt().
>> >
>> > Attached a patch to fix this, which also includes test cases to reproduce.
>>
>> I can reproduce this and the patch fixes it.
>>
>> One question: why is resolveTargetListUnknowns called after
>> assign_list_collations?
>>
>> I'm asking because in transformSelectStmt, resolveTargetListUnknowns
>> is invoked before assign_query_collations. It might not matter, but keeping
>> the order consistent would be good for readers.
>
>
> Updated the patch. It should be before.

Do we really need a test for ORDER BY on a literal column? I replaced
all the test queries with a single one which covers all the scenarios
covered by those queries.

The patch needed to consider pstate->p_resolve_unknowns. Unknown
literals are not resolved as text always. See the test query.

--
Best Wishes,
Ashutosh Bapat


Attachments:

  [text/x-patch] v20260429-0001-Resolve-unknown-type-literals-in-GRAPH_TAB.patch (3.0K, 2-v20260429-0001-Resolve-unknown-type-literals-in-GRAPH_TAB.patch)
  download | inline diff:
From c9e6b0df945e90bb0be43c88d1c55e1ee0a9a982 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <[email protected]>
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 <[email protected]>
Author: Satya Narlapuram <[email protected]>
Author: Ashutosh Bapat <[email protected]>
Reviewed by: Junwang Zhao <[email protected]>
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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: [PATCH] Resolve unknown-type literals in GRAPH_TABLE COLUMNS
@ 2026-05-04 15:12  Peter Eisentraut <[email protected]>
  parent: Ashutosh Bapat <[email protected]>
  0 siblings, 1 reply; 6+ messages in thread

From: Peter Eisentraut @ 2026-05-04 15:12 UTC (permalink / raw)
  To: Ashutosh Bapat <[email protected]>; SATYANARAYANA NARLAPURAM <[email protected]>; +Cc: Junwang Zhao <[email protected]>; PostgreSQL Hackers <[email protected]>

On 29.04.26 16:09, Ashutosh Bapat wrote:
> On Mon, Apr 27, 2026 at 11:34 AM SATYANARAYANA NARLAPURAM
> <[email protected]> wrote:
>>
>> Hi,
>>
>> On Sun, Apr 26, 2026 at 8:10 AM Junwang Zhao <[email protected]> wrote:
>>>
>>> Hi SATYANARAYANA,
>>>
>>> On Sun, Apr 26, 2026 at 3:53 AM SATYANARAYANA NARLAPURAM
>>> <[email protected]> wrote:
>>>>
>>>> Hi hackers,
>>>>
>>>> transformRangeGraphTable() calls transformExpr() and
>>>> assign_list_collations() for COLUMNS expressions but missed calling
>>>> resolveTargetListUnknowns(). As a result, literals such as 'val1'
>>>> in a COLUMNS clause retained type "unknown", causing failures with
>>>> ORDER BY, UNION, and output conversions.
>>>>
>>>> Fix by calling resolveTargetListUnknowns() on the columns target
>>>> list right after assign_list_collations(), similar to SELECT target lists in
>>>> transformSelectStmt().
>>>>
>>>> Attached a patch to fix this, which also includes test cases to reproduce.
>>>
>>> I can reproduce this and the patch fixes it.
>>>
>>> One question: why is resolveTargetListUnknowns called after
>>> assign_list_collations?
>>>
>>> I'm asking because in transformSelectStmt, resolveTargetListUnknowns
>>> is invoked before assign_query_collations. It might not matter, but keeping
>>> the order consistent would be good for readers.
>>
>>
>> Updated the patch. It should be before.
> 
> Do we really need a test for ORDER BY on a literal column? I replaced
> all the test queries with a single one which covers all the scenarios
> covered by those queries.
> 
> The patch needed to consider pstate->p_resolve_unknowns. Unknown
> literals are not resolved as text always. See the test query.

I couldn't find a commit to apply this patch cleanly (the subject says 
patch 5/5, so maybe you had some unpublished local changes?).  After 
applying the test case manually, it looks like the test output is 
already correct without the code change.  So if this patch is still 
required, we need a better test case.






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: [PATCH] Resolve unknown-type literals in GRAPH_TABLE COLUMNS
@ 2026-05-12 06:20  Ashutosh Bapat <[email protected]>
  parent: Peter Eisentraut <[email protected]>
  0 siblings, 0 replies; 6+ messages in thread

From: Ashutosh Bapat @ 2026-05-12 06:20 UTC (permalink / raw)
  To: Peter Eisentraut <[email protected]>; +Cc: SATYANARAYANA NARLAPURAM <[email protected]>; Junwang Zhao <[email protected]>; PostgreSQL Hackers <[email protected]>

On Mon, May 4, 2026 at 8:42 PM Peter Eisentraut <[email protected]> wrote:
>
> On 29.04.26 16:09, Ashutosh Bapat wrote:
> > On Mon, Apr 27, 2026 at 11:34 AM SATYANARAYANA NARLAPURAM
> > <[email protected]> wrote:
> >>
> >> Hi,
> >>
> >> On Sun, Apr 26, 2026 at 8:10 AM Junwang Zhao <[email protected]> wrote:
> >>>
> >>> Hi SATYANARAYANA,
> >>>
> >>> On Sun, Apr 26, 2026 at 3:53 AM SATYANARAYANA NARLAPURAM
> >>> <[email protected]> wrote:
> >>>>
> >>>> Hi hackers,
> >>>>
> >>>> transformRangeGraphTable() calls transformExpr() and
> >>>> assign_list_collations() for COLUMNS expressions but missed calling
> >>>> resolveTargetListUnknowns(). As a result, literals such as 'val1'
> >>>> in a COLUMNS clause retained type "unknown", causing failures with
> >>>> ORDER BY, UNION, and output conversions.
> >>>>
> >>>> Fix by calling resolveTargetListUnknowns() on the columns target
> >>>> list right after assign_list_collations(), similar to SELECT target lists in
> >>>> transformSelectStmt().
> >>>>
> >>>> Attached a patch to fix this, which also includes test cases to reproduce.
> >>>
> >>> I can reproduce this and the patch fixes it.
> >>>
> >>> One question: why is resolveTargetListUnknowns called after
> >>> assign_list_collations?
> >>>
> >>> I'm asking because in transformSelectStmt, resolveTargetListUnknowns
> >>> is invoked before assign_query_collations. It might not matter, but keeping
> >>> the order consistent would be good for readers.
> >>
> >>
> >> Updated the patch. It should be before.
> >
> > Do we really need a test for ORDER BY on a literal column? I replaced
> > all the test queries with a single one which covers all the scenarios
> > covered by those queries.
> >
> > The patch needed to consider pstate->p_resolve_unknowns. Unknown
> > literals are not resolved as text always. See the test query.
>
> I couldn't find a commit to apply this patch cleanly (the subject says
> patch 5/5, so maybe you had some unpublished local changes?).

I am maintaining all the SQL/PGQ fixes in the same branch and creating
patches from that branch. Hence 5/5. But still it shouldn't have
applied cleanly. Both git cherry-pick and git am <attached patch
file>, on a fresh branch succeeded. Please let me know if you still
face the issue.

> After
> applying the test case manually, it looks like the test output is
> already correct without the code change.  So if this patch is still
> required, we need a better test case.
>

Yes, the test query doesn't reproduce the bug. Actually all but only
two queries from the Satya's patch show the bug. I have included a
test query which fails, with expected error message, without code
changes now. Also I don't think we need a separate section for this
test query. Included the query in one of the earliest sections in the
file.

-- 
Best Wishes,
Ashutosh Bapat


Attachments:

  [text/x-patch] v20260512-0001-Resolve-unknown-type-literals-in-GRAPH_TAB.patch (3.8K, 2-v20260512-0001-Resolve-unknown-type-literals-in-GRAPH_TAB.patch)
  download | inline diff:
From 69fcbf2b907ff83aaa17593c5596e495cc0720e7 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <[email protected]>
Date: Wed, 29 Apr 2026 19:07:13 +0530
Subject: [PATCH v20260512 4/4] 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 <[email protected]>
Author: Satya Narlapuram <[email protected]>
Author: Ashutosh Bapat <[email protected]>
Reviewed by: Junwang Zhao <[email protected]>
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 | 9 +++++++++
 src/test/regress/sql/graph_table.sql      | 2 ++
 3 files changed, 15 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 cc6d80afd82..e8d49fd5cd4 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -160,6 +160,15 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (c.name));
  customer3
 (3 rows)
 
+-- Unknown type resolution
+SELECT *, pg_typeof(unknown_col) AS unknown_col_type, pg_typeof(null_col) AS null_col_type FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (c.name, 'unknown-literal' AS unknown_col, NULL AS null_col));
+   name    |   unknown_col   | null_col | unknown_col_type | null_col_type 
+-----------+-----------------+----------+------------------+---------------
+ customer1 | unknown-literal |          | text             | text
+ customer2 | unknown-literal |          | text             | text
+ customer3 | unknown-literal |          | text             | text
+(3 rows)
+
 SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.name));
    name    
 -----------
diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql
index 0e381ec72bc..e761f09e057 100644
--- a/src/test/regress/sql/graph_table.sql
+++ b/src/test/regress/sql/graph_table.sql
@@ -134,6 +134,8 @@ INSERT INTO wishlist_items (wishlist_items_id, wishlist_id, product_no) VALUES
 
 -- single element path pattern
 SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (c.name));
+-- Unknown type resolution
+SELECT *, pg_typeof(unknown_col) AS unknown_col_type, pg_typeof(null_col) AS null_col_type FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (c.name, 'unknown-literal' AS unknown_col, NULL AS null_col));
 SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.name));
 -- graph element specification without label or variable
 SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[]->(o IS orders) COLUMNS (c.name AS customer_name));
-- 
2.34.1



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-05-12 06:20 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-04-25 19:52 [PATCH] Resolve unknown-type literals in GRAPH_TABLE COLUMNS SATYANARAYANA NARLAPURAM <[email protected]>
2026-04-26 15:10 ` Junwang Zhao <[email protected]>
2026-04-27 06:04   ` SATYANARAYANA NARLAPURAM <[email protected]>
2026-04-29 14:09     ` Ashutosh Bapat <[email protected]>
2026-05-04 15:12       ` Peter Eisentraut <[email protected]>
2026-05-12 06:20         ` Ashutosh Bapat <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox