diff options
| author | Peter Eisentraut | 2021-03-31 15:09:24 +0000 |
|---|---|---|
| committer | Peter Eisentraut | 2021-03-31 15:10:50 +0000 |
| commit | 055fee7eb4dcc78e58672aef146334275e1cc40d (patch) | |
| tree | 2034e69c471453e9aea59712b09d3fed95bce330 /src/include | |
| parent | 27e1f14563cf982f1f4d71e21ef247866662a052 (diff) | |
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 <vik.fearing@2ndquadrant.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/454638cf-d563-ab76-a585-2564428062af@2ndquadrant.com
Diffstat (limited to 'src/include')
| -rw-r--r-- | src/include/nodes/parsenodes.h | 7 | ||||
| -rw-r--r-- | src/include/nodes/primnodes.h | 6 | ||||
| -rw-r--r-- | src/include/parser/parse_node.h | 5 | ||||
| -rw-r--r-- | src/include/parser/parse_relation.h | 1 |
4 files changed, 18 insertions, 1 deletions
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 334262b1dd8..7960cfe1a82 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -1070,6 +1070,13 @@ typedef struct RangeTblEntry List *joinrightcols; /* right-side input column numbers */ /* + * join_using_alias is an alias clause attached directly to JOIN/USING. It + * is different from the alias field (below) in that it does not hide the + * range variables of the tables being joined. + */ + Alias *join_using_alias; + + /* * Fields valid for a function RTE (else NIL/zero): * * When funcordinality is true, the eref->colnames list includes an alias diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index d4ce0370884..f66e1449d86 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -1499,6 +1499,11 @@ typedef struct RangeTblRef * alias has a critical impact on semantics, because a join with an alias * restricts visibility of the tables/columns inside it. * + * join_using_alias is an Alias node representing the join correlation + * name that SQL:2016 and later allow to be attached to JOIN/USING. + * Its column alias list includes only the common column names from USING, + * and it does not restrict visibility of the join's input tables. + * * During parse analysis, an RTE is created for the Join, and its index * is filled into rtindex. This RTE is present mainly so that Vars can * be created that refer to the outputs of the join. The planner sometimes @@ -1514,6 +1519,7 @@ typedef struct JoinExpr Node *larg; /* left subtree */ Node *rarg; /* right subtree */ List *usingClause; /* USING clause, if any (list of String) */ + Alias *join_using_alias; /* alias attached to USING clause, if any */ Node *quals; /* qualifiers on join, if any */ Alias *alias; /* user-written alias clause, if any */ int rtindex; /* RT index assigned for join, or 0 */ diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index 70739bcd5b6..1500de2dd08 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -228,7 +228,10 @@ struct ParseState * An element of a namespace list. * * p_names contains the table name and column names exposed by this nsitem. - * (Currently, it's always equal to p_rte->eref.) + * (Typically it's equal to p_rte->eref, but for a JOIN USING alias it's + * equal to p_rte->join_using_alias. Since the USING columns will be the + * join's first N columns, the net effect is just that we expose only those + * join columns via this nsitem.) * * p_rte and p_rtindex link to the underlying rangetable entry. * diff --git a/src/include/parser/parse_relation.h b/src/include/parser/parse_relation.h index 5dbe5ba2e27..8336c2c5a29 100644 --- a/src/include/parser/parse_relation.h +++ b/src/include/parser/parse_relation.h @@ -88,6 +88,7 @@ extern ParseNamespaceItem *addRangeTableEntryForJoin(ParseState *pstate, List *aliasvars, List *leftcols, List *rightcols, + Alias *joinalias, Alias *alias, bool inFromCl); extern ParseNamespaceItem *addRangeTableEntryForCTE(ParseState *pstate, |
