summaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/ruleutils.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2004-10-27 18:09:41 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2004-10-27 18:09:41 +0000
commitb2b0673e4be50915b92ec39de6ace4d2c5ba6a1d (patch)
treefa2b31cd42ff8b9d383b75c033832f42af0fee85 /src/backend/utils/adt/ruleutils.c
parent3fe704209adddf2835bb3e694267acddfc49bc9e (diff)
When displaying a Var that is a reference to a column of an unnamed join,
try to display it as a reference to the underlying column instead. This is a legitimate substitution (it wouldn't be for a named join) and it fixes some cases where the display would otherwise be ambiguous. Per example from Sim Zacks.
Diffstat (limited to 'src/backend/utils/adt/ruleutils.c')
-rw-r--r--src/backend/utils/adt/ruleutils.c25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 3833ee2ee2f..bb0fc2a1794 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -3,7 +3,7 @@
* back to source text
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.183 2004/10/17 21:17:27 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.184 2004/10/27 18:09:38 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
@@ -2558,6 +2558,29 @@ get_names_for_var(Var *var, deparse_context *context,
}
else if (rte->rtekind == RTE_JOIN)
{
+ /*
+ * If it's an unnamed join, look at the expansion of the alias
+ * variable. If it's a simple reference to one of the input
+ * vars then recursively find the name of that var, instead.
+ * (This allows correct decompiling of cases where there are
+ * identically named columns on both sides of the join.)
+ * When it's not a simple reference, we have to just return
+ * the unqualified variable name (this can only happen with
+ * columns that were merged by USING or NATURAL clauses).
+ */
+ if (var->varattno > 0)
+ {
+ Var *aliasvar;
+
+ aliasvar = (Var *) list_nth(rte->joinaliasvars,
+ var->varattno-1);
+ if (IsA(aliasvar, Var))
+ {
+ get_names_for_var(aliasvar, context,
+ schemaname, refname, attname);
+ return;
+ }
+ }
/* Unnamed join has neither schemaname nor refname */
*refname = NULL;
}