From b2b0673e4be50915b92ec39de6ace4d2c5ba6a1d Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 27 Oct 2004 18:09:41 +0000 Subject: 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. --- src/backend/utils/adt/ruleutils.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/backend/utils/adt/ruleutils.c') 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; } -- cgit v1.2.3