Commit 8b728e5c authored by Tom Lane's avatar Tom Lane

Fix oversight in new code for printing rangetable aliases.

In commit 11e13185, I missed the case of
a CTE RTE that doesn't have a user-defined alias, but does have an
alias assigned by set_rtable_names().  Per report from Peter Eisentraut.

While at it, refactor slightly to reduce code duplication.
parent 49ec6132
...@@ -6739,11 +6739,12 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context) ...@@ -6739,11 +6739,12 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
int varno = ((RangeTblRef *) jtnode)->rtindex; int varno = ((RangeTblRef *) jtnode)->rtindex;
RangeTblEntry *rte = rt_fetch(varno, query->rtable); RangeTblEntry *rte = rt_fetch(varno, query->rtable);
char *refname = get_rtable_name(varno, context); char *refname = get_rtable_name(varno, context);
bool gavealias = false; bool printalias;
if (rte->lateral) if (rte->lateral)
appendStringInfoString(buf, "LATERAL "); appendStringInfoString(buf, "LATERAL ");
/* Print the FROM item proper */
switch (rte->rtekind) switch (rte->rtekind)
{ {
case RTE_RELATION: case RTE_RELATION:
...@@ -6776,11 +6777,12 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context) ...@@ -6776,11 +6777,12 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
break; break;
} }
/* Print the relation alias, if needed */
printalias = false;
if (rte->alias != NULL) if (rte->alias != NULL)
{ {
/* Always print alias if user provided one */ /* Always print alias if user provided one */
appendStringInfo(buf, " %s", quote_identifier(refname)); printalias = true;
gavealias = true;
} }
else if (rte->rtekind == RTE_RELATION) else if (rte->rtekind == RTE_RELATION)
{ {
...@@ -6790,10 +6792,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context) ...@@ -6790,10 +6792,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
* resolve a conflict). * resolve a conflict).
*/ */
if (strcmp(refname, get_relation_name(rte->relid)) != 0) if (strcmp(refname, get_relation_name(rte->relid)) != 0)
{ printalias = true;
appendStringInfo(buf, " %s", quote_identifier(refname));
gavealias = true;
}
} }
else if (rte->rtekind == RTE_FUNCTION) else if (rte->rtekind == RTE_FUNCTION)
{ {
...@@ -6802,16 +6801,28 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context) ...@@ -6802,16 +6801,28 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
* renaming of the function and/or instability of the * renaming of the function and/or instability of the
* FigureColname rules for things that aren't simple functions. * FigureColname rules for things that aren't simple functions.
*/ */
appendStringInfo(buf, " %s", quote_identifier(refname)); printalias = true;
gavealias = true; }
else if (rte->rtekind == RTE_CTE)
{
/*
* No need to print alias if it's same as CTE name (this would
* normally be the case, but not if set_rtable_names had to
* resolve a conflict).
*/
if (strcmp(refname, rte->ctename) != 0)
printalias = true;
} }
if (printalias)
appendStringInfo(buf, " %s", quote_identifier(refname));
/* Print the column definitions or aliases, if needed */
if (rte->rtekind == RTE_FUNCTION) if (rte->rtekind == RTE_FUNCTION)
{ {
if (rte->funccoltypes != NIL) if (rte->funccoltypes != NIL)
{ {
/* Function returning RECORD, reconstruct the columndefs */ /* Function returning RECORD, reconstruct the columndefs */
if (!gavealias) if (!printalias)
appendStringInfo(buf, " AS "); appendStringInfo(buf, " AS ");
get_from_clause_coldeflist(rte->eref->colnames, get_from_clause_coldeflist(rte->eref->colnames,
rte->funccoltypes, rte->funccoltypes,
......
...@@ -292,6 +292,30 @@ SELECT pg_get_viewdef('vsubdepartment'::regclass, true); ...@@ -292,6 +292,30 @@ SELECT pg_get_viewdef('vsubdepartment'::regclass, true);
FROM subdepartment; FROM subdepartment;
(1 row) (1 row)
-- Another reverse-listing example
CREATE VIEW sums_1_100 AS
WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n) FROM t;
\d+ sums_1_100
View "public.sums_1_100"
Column | Type | Modifiers | Storage | Description
--------+--------+-----------+---------+-------------
sum | bigint | | plain |
View definition:
WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
SELECT t_1.n + 1
FROM t t_1
WHERE t_1.n < 100
)
SELECT sum(t.n) AS sum
FROM t;
-- corner case in which sub-WITH gets initialized first -- corner case in which sub-WITH gets initialized first
with recursive q as ( with recursive q as (
select * from department select * from department
......
...@@ -178,6 +178,17 @@ SELECT * FROM vsubdepartment ORDER BY name; ...@@ -178,6 +178,17 @@ SELECT * FROM vsubdepartment ORDER BY name;
SELECT pg_get_viewdef('vsubdepartment'::regclass); SELECT pg_get_viewdef('vsubdepartment'::regclass);
SELECT pg_get_viewdef('vsubdepartment'::regclass, true); SELECT pg_get_viewdef('vsubdepartment'::regclass, true);
-- Another reverse-listing example
CREATE VIEW sums_1_100 AS
WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n) FROM t;
\d+ sums_1_100
-- corner case in which sub-WITH gets initialized first -- corner case in which sub-WITH gets initialized first
with recursive q as ( with recursive q as (
select * from department select * from department
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment