Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8b728e5

Browse files
committed
Fix oversight in new code for printing rangetable aliases.
In commit 11e1318, 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.
1 parent 49ec613 commit 8b728e5

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

src/backend/utils/adt/ruleutils.c

+21-10
Original file line numberDiff line numberDiff line change
@@ -6739,11 +6739,12 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
67396739
int varno = ((RangeTblRef *) jtnode)->rtindex;
67406740
RangeTblEntry *rte = rt_fetch(varno, query->rtable);
67416741
char *refname = get_rtable_name(varno, context);
6742-
bool gavealias = false;
6742+
bool printalias;
67436743

67446744
if (rte->lateral)
67456745
appendStringInfoString(buf, "LATERAL ");
67466746

6747+
/* Print the FROM item proper */
67476748
switch (rte->rtekind)
67486749
{
67496750
case RTE_RELATION:
@@ -6776,11 +6777,12 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
67766777
break;
67776778
}
67786779

6780+
/* Print the relation alias, if needed */
6781+
printalias = false;
67796782
if (rte->alias != NULL)
67806783
{
67816784
/* Always print alias if user provided one */
6782-
appendStringInfo(buf, " %s", quote_identifier(refname));
6783-
gavealias = true;
6785+
printalias = true;
67846786
}
67856787
else if (rte->rtekind == RTE_RELATION)
67866788
{
@@ -6790,10 +6792,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
67906792
* resolve a conflict).
67916793
*/
67926794
if (strcmp(refname, get_relation_name(rte->relid)) != 0)
6793-
{
6794-
appendStringInfo(buf, " %s", quote_identifier(refname));
6795-
gavealias = true;
6796-
}
6795+
printalias = true;
67976796
}
67986797
else if (rte->rtekind == RTE_FUNCTION)
67996798
{
@@ -6802,16 +6801,28 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
68026801
* renaming of the function and/or instability of the
68036802
* FigureColname rules for things that aren't simple functions.
68046803
*/
6805-
appendStringInfo(buf, " %s", quote_identifier(refname));
6806-
gavealias = true;
6804+
printalias = true;
6805+
}
6806+
else if (rte->rtekind == RTE_CTE)
6807+
{
6808+
/*
6809+
* No need to print alias if it's same as CTE name (this would
6810+
* normally be the case, but not if set_rtable_names had to
6811+
* resolve a conflict).
6812+
*/
6813+
if (strcmp(refname, rte->ctename) != 0)
6814+
printalias = true;
68076815
}
6816+
if (printalias)
6817+
appendStringInfo(buf, " %s", quote_identifier(refname));
68086818

6819+
/* Print the column definitions or aliases, if needed */
68096820
if (rte->rtekind == RTE_FUNCTION)
68106821
{
68116822
if (rte->funccoltypes != NIL)
68126823
{
68136824
/* Function returning RECORD, reconstruct the columndefs */
6814-
if (!gavealias)
6825+
if (!printalias)
68156826
appendStringInfo(buf, " AS ");
68166827
get_from_clause_coldeflist(rte->eref->colnames,
68176828
rte->funccoltypes,

src/test/regress/expected/with.out

+24
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,30 @@ SELECT pg_get_viewdef('vsubdepartment'::regclass, true);
292292
FROM subdepartment;
293293
(1 row)
294294

295+
-- Another reverse-listing example
296+
CREATE VIEW sums_1_100 AS
297+
WITH RECURSIVE t(n) AS (
298+
VALUES (1)
299+
UNION ALL
300+
SELECT n+1 FROM t WHERE n < 100
301+
)
302+
SELECT sum(n) FROM t;
303+
\d+ sums_1_100
304+
View "public.sums_1_100"
305+
Column | Type | Modifiers | Storage | Description
306+
--------+--------+-----------+---------+-------------
307+
sum | bigint | | plain |
308+
View definition:
309+
WITH RECURSIVE t(n) AS (
310+
VALUES (1)
311+
UNION ALL
312+
SELECT t_1.n + 1
313+
FROM t t_1
314+
WHERE t_1.n < 100
315+
)
316+
SELECT sum(t.n) AS sum
317+
FROM t;
318+
295319
-- corner case in which sub-WITH gets initialized first
296320
with recursive q as (
297321
select * from department

src/test/regress/sql/with.sql

+11
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,17 @@ SELECT * FROM vsubdepartment ORDER BY name;
178178
SELECT pg_get_viewdef('vsubdepartment'::regclass);
179179
SELECT pg_get_viewdef('vsubdepartment'::regclass, true);
180180

181+
-- Another reverse-listing example
182+
CREATE VIEW sums_1_100 AS
183+
WITH RECURSIVE t(n) AS (
184+
VALUES (1)
185+
UNION ALL
186+
SELECT n+1 FROM t WHERE n < 100
187+
)
188+
SELECT sum(n) FROM t;
189+
190+
\d+ sums_1_100
191+
181192
-- corner case in which sub-WITH gets initialized first
182193
with recursive q as (
183194
select * from department

0 commit comments

Comments
 (0)