From 055fee7eb4dcc78e58672aef146334275e1cc40d Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 31 Mar 2021 17:09:24 +0200 Subject: Allow an alias to be attached to a JOIN ... USING This allows something like SELECT ... FROM t1 JOIN t2 USING (a, b, c) AS x where x has the columns a, b, c and unlike a regular alias it does not hide the range variables of the tables being joined t1 and t2. Per SQL:2016 feature F404 "Range variable for common column names". Reviewed-by: Vik Fearing Reviewed-by: Tom Lane Discussion: https://www.postgresql.org/message-id/flat/454638cf-d563-ab76-a585-2564428062af@2ndquadrant.com --- src/backend/parser/parse_relation.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/backend/parser/parse_relation.c') diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index b12416535ed..d451f055f72 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -753,6 +753,12 @@ scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, * else return InvalidAttrNumber. * If the name proves ambiguous within this RTE, raise error. * + * Actually, we only search the names listed in "eref". This can be either + * rte->eref, in which case we are indeed searching all the column names, + * or for a join it can be rte->join_using_alias, in which case we are only + * considering the common column names (which are the first N columns of the + * join, so everything works). + * * pstate and location are passed only for error-reporting purposes. * * Side effect: if fuzzystate is non-NULL, check non-system columns @@ -2134,6 +2140,7 @@ addRangeTableEntryForJoin(ParseState *pstate, List *aliasvars, List *leftcols, List *rightcols, + Alias *join_using_alias, Alias *alias, bool inFromCl) { @@ -2162,6 +2169,7 @@ addRangeTableEntryForJoin(ParseState *pstate, rte->joinaliasvars = aliasvars; rte->joinleftcols = leftcols; rte->joinrightcols = rightcols; + rte->join_using_alias = join_using_alias; rte->alias = alias; eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL); -- cgit v1.2.3