From 7d757cb2f0a55e8134f5dcf83843f013edc8791b Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Thu, 22 Oct 2026 12:46:00 +0900 Subject: [PATCH v1 2/2] Fix JSON_TABLE and XMLTABLE alias lists in deparsed SQL XMLTABLE and JSON_TABLE never printed a column alias list, because the clause names its columns itself. But the columns can still be renamed, by a user-written alias list or by the deparser, and the text then referred to columns that do not exist ("column p does not exist"). The alias of the RTE was not printed either, even when it was renamed to avoid another one in an outer query level ("missing FROM-clause entry for table json_table_1"). Print both when needed, as for other RTEs. The XMLTABLE part goes back to 15; JSON_TABLE exists only from 17. Backpatch-through: 15 --- src/backend/utils/adt/ruleutils.c | 21 +++++++++++++------ .../regress/expected/sqljson_jsontable.out | 12 +++++------ src/test/regress/expected/xml.out | 7 +++---- src/test/regress/sql/sqljson_jsontable.sql | 4 +--- src/test/regress/sql/xml.sql | 3 +-- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index e9c1c9db1b1..b4e41605a0e 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -4556,17 +4556,15 @@ set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte, * are different from the underlying "real" names. For a function RTE, * always emit a complete column alias list; this is to protect against * possible instability of the default column names (eg, from altering - * parameter names). For tablefunc RTEs, we never print aliases, because - * the column names are part of the clause itself. For other RTE types, - * print if we changed anything OR if there were user-written column - * aliases (since the latter would be part of the underlying "reality"). + * parameter names). For other RTE types, print if we changed anything OR + * if there were user-written column aliases (since the latter would be + * part of the underlying "reality"). That includes tablefunc RTEs, since + * XMLTABLE and JSON_TABLE accept a column alias list too. */ if (rte->rtekind == RTE_RELATION) colinfo->printaliases = changed_any; else if (rte->rtekind == RTE_FUNCTION) colinfo->printaliases = true; - else if (rte->rtekind == RTE_TABLEFUNC) - colinfo->printaliases = false; else if (rte->alias && rte->alias->colnames != NIL) colinfo->printaliases = true; else @@ -13000,6 +12998,17 @@ get_rte_alias(RangeTblEntry *rte, int varno, bool use_as, if (strcmp(refname, rte->ctename) != 0) printalias = true; } + else if (rte->rtekind == RTE_TABLEFUNC) + { + /* + * No need to print alias if it's the default name the parser assigned + * (this would normally be the case, but not if set_rtable_names had + * to resolve a conflict). There is no user-written alias here, so + * eref->aliasname is that default name. + */ + if (strcmp(refname, rte->eref->aliasname) != 0) + printalias = true; + } if (printalias) appendStringInfo(context->buf, "%s%s", diff --git a/src/test/regress/expected/sqljson_jsontable.out b/src/test/regress/expected/sqljson_jsontable.out index 650150c6ac0..609551c796f 100644 --- a/src/test/regress/expected/sqljson_jsontable.out +++ b/src/test/regress/expected/sqljson_jsontable.out @@ -2051,7 +2051,7 @@ SELECT * FROM JSON_TABLE(jsonb '{}', '$' PASSING ROW(1, 2) < ROW(3, 4) AS x -- Test JSON_TABLE() column alias list deparsing, and the alias of a -- JSON_TABLE() that is renamed to avoid a conflict with another one in an --- outer query level +-- outer query level; leave the views around for the pg_upgrade test CREATE VIEW json_table_view_alias AS SELECT t.p, t.q FROM JSON_TABLE(jsonb '[{"a": 1, "b": 2}]', '$[*]' @@ -2066,7 +2066,7 @@ CREATE OR REPLACE VIEW public.json_table_view_alias AS a integer PATH '$."a"', b integer PATH '$."b"' ) - ) t + ) t(p, q) CREATE VIEW json_table_view_conflict AS SELECT a, (SELECT max(b) FROM JSON_TABLE(jsonb '[{"b": 2}]', '$[*]' COLUMNS (b int PATH '$.b'))) AS m @@ -2080,7 +2080,7 @@ CREATE OR REPLACE VIEW public.json_table_view_conflict AS COLUMNS ( b integer PATH '$."b"' ) - )) AS m + ) json_table_1) AS m FROM JSON_TABLE( '[{"a": 1}]'::jsonb, '$[*]' AS json_table_path_0 COLUMNS ( @@ -2104,13 +2104,11 @@ CREATE OR REPLACE VIEW public.json_table_view_natural AS a integer PATH '$."a"', b integer PATH '$."b"' ) - ) t1 + ) t1(x, y) JOIN JSON_TABLE( '[{"a": 2, "b": 1}]'::jsonb, '$[*]' AS json_table_path_0 COLUMNS ( a integer PATH '$."a"', b integer PATH '$."b"' ) - ) t2 USING (x, y) -DROP VIEW json_table_view_alias, json_table_view_conflict, - json_table_view_natural; + ) t2(y, x) USING (x, y) diff --git a/src/test/regress/expected/xml.out b/src/test/regress/expected/xml.out index 15a63493482..2e86e6854ff 100644 --- a/src/test/regress/expected/xml.out +++ b/src/test/regress/expected/xml.out @@ -1935,7 +1935,7 @@ SELECT xmltext('x'|| '

73

'::xml || .42 || true || 'j'::char); -- Test XMLTABLE() column alias list deparsing, and the alias of an -- XMLTABLE() that is renamed to avoid a conflict with another one in an --- outer query level +-- outer query level; leave the views around for the pg_upgrade test CREATE VIEW xmltableview_alias AS SELECT t.p FROM xmldata, XMLTABLE('/ROWS/ROW' PASSING data @@ -1944,7 +1944,7 @@ FROM xmldata, XMLTABLE('/ROWS/ROW' PASSING data CREATE OR REPLACE VIEW public.xmltableview_alias AS SELECT t.p FROM xmldata, - LATERAL XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS a integer PATH ('@id'::text)) t + LATERAL XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS a integer PATH ('@id'::text)) t(p) CREATE VIEW xmltableview_conflict AS SELECT a, (SELECT max(b) FROM XMLTABLE('/ROWS/ROW' PASSING data COLUMNS b int PATH '@id')) AS m @@ -1953,7 +1953,6 @@ FROM xmldata, XMLTABLE('/ROWS/ROW' PASSING data COLUMNS a int PATH '@id'); CREATE OR REPLACE VIEW public.xmltableview_conflict AS SELECT "xmltable".a, ( SELECT max(xmltable_1.b) AS max - FROM XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS b integer PATH ('@id'::text))) AS m + FROM XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS b integer PATH ('@id'::text)) xmltable_1) AS m FROM xmldata, LATERAL XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS a integer PATH ('@id'::text)) -DROP VIEW xmltableview_alias, xmltableview_conflict; diff --git a/src/test/regress/sql/sqljson_jsontable.sql b/src/test/regress/sql/sqljson_jsontable.sql index 4e5f33a6196..42b6ad67d39 100644 --- a/src/test/regress/sql/sqljson_jsontable.sql +++ b/src/test/regress/sql/sqljson_jsontable.sql @@ -1120,7 +1120,7 @@ SELECT * FROM JSON_TABLE(jsonb '{}', '$' PASSING ROW(1, 2) < ROW(3, 4) AS x -- Test JSON_TABLE() column alias list deparsing, and the alias of a -- JSON_TABLE() that is renamed to avoid a conflict with another one in an --- outer query level +-- outer query level; leave the views around for the pg_upgrade test CREATE VIEW json_table_view_alias AS SELECT t.p, t.q FROM JSON_TABLE(jsonb '[{"a": 1, "b": 2}]', '$[*]' @@ -1139,5 +1139,3 @@ FROM JSON_TABLE(jsonb '[{"a": 1, "b": 2}]', '$[*]' NATURAL JOIN JSON_TABLE(jsonb '[{"a": 2, "b": 1}]', '$[*]' COLUMNS (a int PATH '$.a', b int PATH '$.b')) t2(y, x); \sv json_table_view_natural; -DROP VIEW json_table_view_alias, json_table_view_conflict, - json_table_view_natural; diff --git a/src/test/regress/sql/xml.sql b/src/test/regress/sql/xml.sql index 438bb2f980e..755141a2fb6 100644 --- a/src/test/regress/sql/xml.sql +++ b/src/test/regress/sql/xml.sql @@ -727,7 +727,7 @@ SELECT xmltext('x'|| '

73

'::xml || .42 || true || 'j'::char); -- Test XMLTABLE() column alias list deparsing, and the alias of an -- XMLTABLE() that is renamed to avoid a conflict with another one in an --- outer query level +-- outer query level; leave the views around for the pg_upgrade test CREATE VIEW xmltableview_alias AS SELECT t.p FROM xmldata, XMLTABLE('/ROWS/ROW' PASSING data @@ -738,4 +738,3 @@ SELECT a, (SELECT max(b) FROM XMLTABLE('/ROWS/ROW' PASSING data COLUMNS b int PATH '@id')) AS m FROM xmldata, XMLTABLE('/ROWS/ROW' PASSING data COLUMNS a int PATH '@id'); \sv xmltableview_conflict -DROP VIEW xmltableview_alias, xmltableview_conflict; -- 2.54.0 (Apple Git-157)