summaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/ruleutils.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2012-10-12 16:14:11 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2012-10-12 16:14:43 -0400
commit8b728e5c6e0ce6b6d6f54b92b390f14aa1aca6db (patch)
treea07e99c61279eaabbd740744dc860bf3e298d971 /src/backend/utils/adt/ruleutils.c
parent49ec613201b2e9debdf9e9ad9a2ad7c6c8083729 (diff)
Fix oversight in new code for printing rangetable aliases.
In commit 11e131854f8231a21613f834c40fe9d046926387, 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.
Diffstat (limited to 'src/backend/utils/adt/ruleutils.c')
-rw-r--r--src/backend/utils/adt/ruleutils.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index c8d7d9c21b3..c3ede233bcd 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -6739,11 +6739,12 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
int varno = ((RangeTblRef *) jtnode)->rtindex;
RangeTblEntry *rte = rt_fetch(varno, query->rtable);
char *refname = get_rtable_name(varno, context);
- bool gavealias = false;
+ bool printalias;
if (rte->lateral)
appendStringInfoString(buf, "LATERAL ");
+ /* Print the FROM item proper */
switch (rte->rtekind)
{
case RTE_RELATION:
@@ -6776,11 +6777,12 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
break;
}
+ /* Print the relation alias, if needed */
+ printalias = false;
if (rte->alias != NULL)
{
/* Always print alias if user provided one */
- appendStringInfo(buf, " %s", quote_identifier(refname));
- gavealias = true;
+ printalias = true;
}
else if (rte->rtekind == RTE_RELATION)
{
@@ -6790,10 +6792,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
* resolve a conflict).
*/
if (strcmp(refname, get_relation_name(rte->relid)) != 0)
- {
- appendStringInfo(buf, " %s", quote_identifier(refname));
- gavealias = true;
- }
+ printalias = true;
}
else if (rte->rtekind == RTE_FUNCTION)
{
@@ -6802,16 +6801,28 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
* renaming of the function and/or instability of the
* FigureColname rules for things that aren't simple functions.
*/
- appendStringInfo(buf, " %s", quote_identifier(refname));
- gavealias = true;
+ printalias = 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->funccoltypes != NIL)
{
/* Function returning RECORD, reconstruct the columndefs */
- if (!gavealias)
+ if (!printalias)
appendStringInfo(buf, " AS ");
get_from_clause_coldeflist(rte->eref->colnames,
rte->funccoltypes,