@@ -509,7 +509,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
509
509
%type <ival> sub_type opt_materialized
510
510
%type <value> NumericOnly
511
511
%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
513
513
%type <list> func_alias_clause
514
514
%type <sortby> sortby
515
515
%type <ielem> index_elem index_elem_options
@@ -12144,6 +12144,7 @@ joined_table:
12144
12144
n->larg = $1 ;
12145
12145
n->rarg = $4 ;
12146
12146
n->usingClause = NIL;
12147
+ n->join_using_alias = NULL ;
12147
12148
n->quals = NULL ;
12148
12149
$$ = n;
12149
12150
}
@@ -12155,9 +12156,16 @@ joined_table:
12155
12156
n->larg = $1 ;
12156
12157
n->rarg = $4 ;
12157
12158
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
+ }
12159
12164
else
12160
- n->quals = $5 ; /* ON clause */
12165
+ {
12166
+ /* ON clause */
12167
+ n->quals = $5 ;
12168
+ }
12161
12169
$$ = n;
12162
12170
}
12163
12171
| table_ref JOIN table_ref join_qual
@@ -12169,9 +12177,16 @@ joined_table:
12169
12177
n->larg = $1 ;
12170
12178
n->rarg = $3 ;
12171
12179
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
+ }
12173
12185
else
12174
- n->quals = $4 ; /* ON clause */
12186
+ {
12187
+ /* ON clause */
12188
+ n->quals = $4 ;
12189
+ }
12175
12190
$$ = n;
12176
12191
}
12177
12192
| table_ref NATURAL join_type JOIN table_ref
@@ -12182,6 +12197,7 @@ joined_table:
12182
12197
n->larg = $1 ;
12183
12198
n->rarg = $5 ;
12184
12199
n->usingClause = NIL; /* figure out which columns later... */
12200
+ n->join_using_alias = NULL ;
12185
12201
n->quals = NULL ; /* fill later */
12186
12202
$$ = n;
12187
12203
}
@@ -12194,6 +12210,7 @@ joined_table:
12194
12210
n->larg = $1 ;
12195
12211
n->rarg = $4 ;
12196
12212
n->usingClause = NIL; /* figure out which columns later... */
12213
+ n->join_using_alias = NULL ;
12197
12214
n->quals = NULL ; /* fill later */
12198
12215
$$ = n;
12199
12216
}
@@ -12228,6 +12245,22 @@ opt_alias_clause: alias_clause { $$ = $1; }
12228
12245
| /* EMPTY*/ { $$ = NULL ; }
12229
12246
;
12230
12247
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
+
12231
12264
/*
12232
12265
* func_alias_clause can include both an Alias and a coldeflist, so we make it
12233
12266
* return a 2-element list that gets disassembled by calling production.
@@ -12272,15 +12305,24 @@ opt_outer: OUTER_P
12272
12305
12273
12306
/* JOIN qualification clauses
12274
12307
* Possibilities are:
12275
- * USING ( column list ) allows only unqualified column names,
12308
+ * USING ( column list ) [ AS alias ]
12309
+ * allows only unqualified column names,
12276
12310
* which must match between tables.
12277
12311
* ON expr allows more general qualifications.
12278
12312
*
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.
12280
12316
*/
12281
12317
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
+ }
12284
12326
;
12285
12327
12286
12328
0 commit comments