@@ -509,7 +509,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
509509%type <ival> sub_type opt_materialized
510510%type <value> NumericOnly
511511%type <list> NumericOnly_list
512- %type <alias> alias_clause opt_alias_clause
512+ %type <alias> alias_clause opt_alias_clause opt_alias_clause_for_join_using
513513%type <list> func_alias_clause
514514%type <sortby> sortby
515515%type <ielem> index_elem index_elem_options
@@ -12144,6 +12144,7 @@ joined_table:
1214412144 n->larg = $1 ;
1214512145 n->rarg = $4 ;
1214612146 n->usingClause = NIL;
12147+ n->join_using_alias = NULL ;
1214712148 n->quals = NULL ;
1214812149 $$ = n;
1214912150 }
@@ -12155,9 +12156,16 @@ joined_table:
1215512156 n->larg = $1 ;
1215612157 n->rarg = $4 ;
1215712158 if ($5 != NULL && IsA($5 , List))
12158- n->usingClause = (List *) $5 ; /* USING clause */
12159+ {
12160+ /* USING clause */
12161+ n->usingClause = linitial_node(List, castNode(List, $5 ));
12162+ n->join_using_alias = lsecond_node(Alias, castNode(List, $5 ));
12163+ }
1215912164 else
12160- n->quals = $5 ; /* ON clause */
12165+ {
12166+ /* ON clause */
12167+ n->quals = $5 ;
12168+ }
1216112169 $$ = n;
1216212170 }
1216312171 | table_ref JOIN table_ref join_qual
@@ -12169,9 +12177,16 @@ joined_table:
1216912177 n->larg = $1 ;
1217012178 n->rarg = $3 ;
1217112179 if ($4 != NULL && IsA($4 , List))
12172- n->usingClause = (List *) $4 ; /* USING clause */
12180+ {
12181+ /* USING clause */
12182+ n->usingClause = linitial_node(List, castNode(List, $4 ));
12183+ n->join_using_alias = lsecond_node(Alias, castNode(List, $4 ));
12184+ }
1217312185 else
12174- n->quals = $4 ; /* ON clause */
12186+ {
12187+ /* ON clause */
12188+ n->quals = $4 ;
12189+ }
1217512190 $$ = n;
1217612191 }
1217712192 | table_ref NATURAL join_type JOIN table_ref
@@ -12182,6 +12197,7 @@ joined_table:
1218212197 n->larg = $1 ;
1218312198 n->rarg = $5 ;
1218412199 n->usingClause = NIL; /* figure out which columns later... */
12200+ n->join_using_alias = NULL ;
1218512201 n->quals = NULL ; /* fill later */
1218612202 $$ = n;
1218712203 }
@@ -12194,6 +12210,7 @@ joined_table:
1219412210 n->larg = $1 ;
1219512211 n->rarg = $4 ;
1219612212 n->usingClause = NIL; /* figure out which columns later... */
12213+ n->join_using_alias = NULL ;
1219712214 n->quals = NULL ; /* fill later */
1219812215 $$ = n;
1219912216 }
@@ -12228,6 +12245,22 @@ opt_alias_clause: alias_clause { $$ = $1; }
1222812245 | /* EMPTY*/ { $$ = NULL ; }
1222912246 ;
1223012247
12248+ /*
12249+ * The alias clause after JOIN ... USING only accepts the AS ColId spelling,
12250+ * per SQL standard. (The grammar could parse the other variants, but they
12251+ * don't seem to be useful, and it might lead to parser problems in the
12252+ * future.)
12253+ */
12254+ opt_alias_clause_for_join_using :
12255+ AS ColId
12256+ {
12257+ $$ = makeNode(Alias);
12258+ $$ ->aliasname = $2 ;
12259+ /* the column name list will be inserted later */
12260+ }
12261+ | /* EMPTY*/ { $$ = NULL ; }
12262+ ;
12263+
1223112264/*
1223212265 * func_alias_clause can include both an Alias and a coldeflist, so we make it
1223312266 * return a 2-element list that gets disassembled by calling production.
@@ -12272,15 +12305,24 @@ opt_outer: OUTER_P
1227212305
1227312306/* JOIN qualification clauses
1227412307 * Possibilities are:
12275- * USING ( column list ) allows only unqualified column names,
12308+ * USING ( column list ) [ AS alias ]
12309+ * allows only unqualified column names,
1227612310 * which must match between tables.
1227712311 * ON expr allows more general qualifications.
1227812312 *
12279- * We return USING as a List node, while an ON-expr will not be a List.
12313+ * We return USING as a two-element List (the first item being a sub-List
12314+ * of the common column names, and the second either an Alias item or NULL).
12315+ * An ON-expr will not be a List, so it can be told apart that way.
1228012316 */
1228112317
12282- join_qual : USING ' (' name_list ' )' { $$ = (Node *) $3 ; }
12283- | ON a_expr { $$ = $2 ; }
12318+ join_qual : USING ' (' name_list ' )' opt_alias_clause_for_join_using
12319+ {
12320+ $$ = (Node *) list_make2($3 , $5 );
12321+ }
12322+ | ON a_expr
12323+ {
12324+ $$ = $2 ;
12325+ }
1228412326 ;
1228512327
1228612328
0 commit comments