11
11
*
12
12
*
13
13
* IDENTIFICATION
14
- * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.214 2001/01/06 10:50:02 petere Exp $
14
+ * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.215 2001/01/15 20:36:36 tgl Exp $
15
15
*
16
16
* HISTORY
17
17
* AUTHOR DATE MAJOR EVENT
@@ -146,7 +146,8 @@ static void doNegateFloat(Value *v);
146
146
UnlistenStmt , UpdateStmt , VacuumStmt , VariableResetStmt ,
147
147
VariableSetStmt , VariableShowStmt , ViewStmt , CheckPointStmt
148
148
149
- %type <node> select_no_parens , select_clause , simple_select
149
+ %type <node> select_no_parens , select_with_parens , select_clause ,
150
+ simple_select
150
151
151
152
%type <node> alter_column_action
152
153
%type <ival> drop_behavior
@@ -2666,16 +2667,7 @@ RuleActionMulti: RuleActionMulti ';' RuleActionStmtOrEmpty
2666
2667
}
2667
2668
;
2668
2669
2669
- /*
2670
- * Allowing RuleActionStmt to be a SelectStmt creates an ambiguity:
2671
- * is the RuleActionList "((SELECT foo))" a standalone RuleActionStmt,
2672
- * or a one-entry RuleActionMulti list? We don't really care, but yacc
2673
- * wants to know. We use operator precedence to resolve the ambiguity:
2674
- * giving this rule a higher precedence than ')' will force a reduce
2675
- * rather than shift decision, causing the one-entry-list interpretation
2676
- * to be chosen.
2677
- */
2678
- RuleActionStmt : SelectStmt %prec TYPECAST
2670
+ RuleActionStmt : SelectStmt
2679
2671
| InsertStmt
2680
2672
| UpdateStmt
2681
2673
| DeleteStmt
@@ -3262,32 +3254,48 @@ opt_cursor: BINARY { $$ = TRUE; }
3262
3254
* The rule returns either a single SelectStmt node or a tree of them,
3263
3255
* representing a set-operation tree.
3264
3256
*
3265
- * To avoid ambiguity problems with nested parentheses, we have to define
3266
- * a "select_no_parens" nonterminal in which there are no parentheses
3267
- * at the outermost level. This is used in the production
3268
- * c_expr: '(' select_no_parens ')'
3269
- * This gives a unique parsing of constructs where a subselect is nested
3270
- * in an expression with extra parentheses: the parentheses are not part
3271
- * of the subselect but of the outer expression. yacc is not quite bright
3272
- * enough to handle the situation completely, however. To prevent a shift/
3273
- * reduce conflict, we also have to attach a precedence to the
3274
- * SelectStmt: select_no_parens
3275
- * rule that is higher than the precedence of ')'. This means that when
3276
- * "((SELECT foo" has been parsed in an expression context, and the
3277
- * next token is ')', the parser will follow the '(' SelectStmt ')' reduction
3278
- * path rather than '(' select_no_parens ')'. The upshot is that excess
3279
- * parens don't work in this context: SELECT ((SELECT foo)) will give a
3280
- * parse error, whereas SELECT ((SELECT foo) UNION (SELECT bar)) is OK.
3281
- * This is ugly, but it beats not allowing excess parens anywhere...
3282
- *
3283
- * In all other contexts, we can use SelectStmt which allows outer parens.
3257
+ * There is an ambiguity when a sub-SELECT is within an a_expr and there
3258
+ * are excess parentheses: do the parentheses belong to the sub-SELECT or
3259
+ * to the surrounding a_expr? We don't really care, but yacc wants to know.
3260
+ * To resolve the ambiguity, we are careful to define the grammar so that
3261
+ * the decision is staved off as long as possible: as long as we can keep
3262
+ * absorbing parentheses into the sub-SELECT, we will do so, and only when
3263
+ * it's no longer possible to do that will we decide that parens belong to
3264
+ * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
3265
+ * parentheses are treated as part of the sub-select. The necessity of doing
3266
+ * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
3267
+ * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
3268
+ * SELECT viewpoint when we see the UNION.
3269
+ *
3270
+ * This approach is implemented by defining a nonterminal select_with_parens,
3271
+ * which represents a SELECT with at least one outer layer of parentheses,
3272
+ * and being careful to use select_with_parens, never '(' SelectStmt ')',
3273
+ * in the expression grammar. We will then have shift-reduce conflicts
3274
+ * which we can resolve in favor of always treating '(' <select> ')' as
3275
+ * a select_with_parens. To resolve the conflicts, the productions that
3276
+ * conflict with the select_with_parens productions are manually given
3277
+ * precedences lower than the precedence of ')', thereby ensuring that we
3278
+ * shift ')' (and then reduce to select_with_parens) rather than trying to
3279
+ * reduce the inner <select> nonterminal to something else. We use UMINUS
3280
+ * precedence for this, which is a fairly arbitrary choice.
3281
+ *
3282
+ * To be able to define select_with_parens itself without ambiguity, we need
3283
+ * a nonterminal select_no_parens that represents a SELECT structure with no
3284
+ * outermost parentheses. This is a little bit tedious, but it works.
3285
+ *
3286
+ * In non-expression contexts, we use SelectStmt which can represent a SELECT
3287
+ * with or without outer parentheses.
3284
3288
*/
3285
3289
3286
- SelectStmt : select_no_parens %prec TYPECAST
3290
+ SelectStmt : select_no_parens %prec UMINUS
3291
+ | select_with_parens %prec UMINUS
3292
+ ;
3293
+
3294
+ select_with_parens : ' (' select_no_parens ' )'
3287
3295
{
3288
- $$ = $1 ;
3296
+ $$ = $2 ;
3289
3297
}
3290
- | ' (' SelectStmt ' )'
3298
+ | ' (' select_with_parens ' )'
3291
3299
{
3292
3300
$$ = $2 ;
3293
3301
}
@@ -3318,13 +3326,7 @@ select_no_parens: simple_select
3318
3326
;
3319
3327
3320
3328
select_clause : simple_select
3321
- {
3322
- $$ = $1 ;
3323
- }
3324
- | ' (' SelectStmt ' )'
3325
- {
3326
- $$ = $2 ;
3327
- }
3329
+ | select_with_parens
3328
3330
;
3329
3331
3330
3332
/*
@@ -3342,8 +3344,10 @@ select_clause: simple_select
3342
3344
* (SELECT foo UNION SELECT bar) ORDER BY baz
3343
3345
* not
3344
3346
* SELECT foo UNION (SELECT bar ORDER BY baz)
3345
- * Likewise FOR UPDATE and LIMIT. This does not limit functionality,
3346
- * because you can reintroduce sort and limit clauses inside parentheses.
3347
+ * Likewise FOR UPDATE and LIMIT. Therefore, those clauses are described
3348
+ * as part of the select_no_parens production, not simple_select.
3349
+ * This does not limit functionality, because you can reintroduce sort and
3350
+ * limit clauses inside parentheses.
3347
3351
*
3348
3352
* NOTE: only the leftmost component SelectStmt should have INTO.
3349
3353
* However, this is not checked by the grammar; parse analysis must check it.
@@ -3614,11 +3618,11 @@ table_ref: relation_expr
3614
3618
$1 ->name = $2 ;
3615
3619
$$ = (Node *) $1 ;
3616
3620
}
3617
- | ' ( ' SelectStmt ' ) ' alias_clause
3621
+ | select_with_parens alias_clause
3618
3622
{
3619
3623
RangeSubselect *n = makeNode(RangeSubselect);
3620
- n->subquery = $2 ;
3621
- n->name = $4 ;
3624
+ n->subquery = $1 ;
3625
+ n->name = $2 ;
3622
3626
$$ = (Node *) n;
3623
3627
}
3624
3628
| joined_table
@@ -3788,15 +3792,15 @@ relation_expr: relation_name
3788
3792
$$ ->inhOpt = INH_DEFAULT;
3789
3793
$$ ->name = NULL ;
3790
3794
}
3791
- | relation_name ' *' % prec ' = '
3795
+ | relation_name ' *'
3792
3796
{
3793
3797
/* inheritance query */
3794
3798
$$ = makeNode(RangeVar);
3795
3799
$$ ->relname = $1 ;
3796
3800
$$ ->inhOpt = INH_YES;
3797
3801
$$ ->name = NULL ;
3798
3802
}
3799
- | ONLY relation_name % prec ' = '
3803
+ | ONLY relation_name
3800
3804
{
3801
3805
/* no inheritance */
3802
3806
$$ = makeNode(RangeVar);
@@ -4146,27 +4150,27 @@ opt_interval: datetime { $$ = makeList1($1); }
4146
4150
* Define row_descriptor to allow yacc to break the reduce/reduce conflict
4147
4151
* with singleton expressions.
4148
4152
*/
4149
- row_expr : ' (' row_descriptor ' )' IN ' ( ' SelectStmt ' ) '
4153
+ row_expr : ' (' row_descriptor ' )' IN select_with_parens
4150
4154
{
4151
4155
SubLink *n = makeNode(SubLink);
4152
4156
n->lefthand = $2 ;
4153
4157
n->oper = (List *) makeA_Expr(OP, " =" , NULL , NULL );
4154
4158
n->useor = FALSE ;
4155
4159
n->subLinkType = ANY_SUBLINK;
4156
- n->subselect = $6 ;
4160
+ n->subselect = $5 ;
4157
4161
$$ = (Node *)n;
4158
4162
}
4159
- | ' (' row_descriptor ' )' NOT IN ' ( ' SelectStmt ' ) '
4163
+ | ' (' row_descriptor ' )' NOT IN select_with_parens
4160
4164
{
4161
4165
SubLink *n = makeNode(SubLink);
4162
4166
n->lefthand = $2 ;
4163
4167
n->oper = (List *) makeA_Expr(OP, " <>" , NULL , NULL );
4164
4168
n->useor = TRUE ;
4165
4169
n->subLinkType = ALL_SUBLINK;
4166
- n->subselect = $7 ;
4170
+ n->subselect = $6 ;
4167
4171
$$ = (Node *)n;
4168
4172
}
4169
- | ' (' row_descriptor ' )' all_Op sub_type ' ( ' SelectStmt ' ) '
4173
+ | ' (' row_descriptor ' )' all_Op sub_type select_with_parens
4170
4174
{
4171
4175
SubLink *n = makeNode(SubLink);
4172
4176
n->lefthand = $2 ;
@@ -4176,10 +4180,10 @@ row_expr: '(' row_descriptor ')' IN '(' SelectStmt ')'
4176
4180
else
4177
4181
n->useor = FALSE ;
4178
4182
n->subLinkType = $5 ;
4179
- n->subselect = $7 ;
4183
+ n->subselect = $6 ;
4180
4184
$$ = (Node *)n;
4181
4185
}
4182
- | ' (' row_descriptor ' )' all_Op ' ( ' SelectStmt ' ) '
4186
+ | ' (' row_descriptor ' )' all_Op select_with_parens
4183
4187
{
4184
4188
SubLink *n = makeNode(SubLink);
4185
4189
n->lefthand = $2 ;
@@ -4189,7 +4193,7 @@ row_expr: '(' row_descriptor ')' IN '(' SelectStmt ')'
4189
4193
else
4190
4194
n->useor = FALSE ;
4191
4195
n->subLinkType = MULTIEXPR_SUBLINK;
4192
- n->subselect = $6 ;
4196
+ n->subselect = $5 ;
4193
4197
$$ = (Node *)n;
4194
4198
}
4195
4199
| ' (' row_descriptor ' )' all_Op ' (' row_descriptor ' )'
@@ -4291,9 +4295,9 @@ a_expr: c_expr
4291
4295
* If you add more explicitly-known operators, be sure to add them
4292
4296
* also to b_expr and to the MathOp list above.
4293
4297
*/
4294
- | ' +' a_expr %prec UMINUS
4298
+ | ' +' a_expr %prec UMINUS
4295
4299
{ $$ = makeA_Expr(OP, " +" , NULL , $2 ); }
4296
- | ' -' a_expr %prec UMINUS
4300
+ | ' -' a_expr %prec UMINUS
4297
4301
{ $$ = doNegate($2 ); }
4298
4302
| ' %' a_expr
4299
4303
{ $$ = makeA_Expr(OP, " %" , NULL , $2 ); }
@@ -4458,12 +4462,12 @@ a_expr: c_expr
4458
4462
makeA_Expr (OP, " <" , $1 , $4 ),
4459
4463
makeA_Expr(OP, " >" , $1 , $6 ));
4460
4464
}
4461
- | a_expr IN ' ( ' in_expr ' ) '
4465
+ | a_expr IN in_expr
4462
4466
{
4463
4467
/* in_expr returns a SubLink or a list of a_exprs */
4464
- if (IsA($4 , SubLink))
4468
+ if (IsA($3 , SubLink))
4465
4469
{
4466
- SubLink *n = (SubLink *)$4 ;
4470
+ SubLink *n = (SubLink *)$3 ;
4467
4471
n->lefthand = makeList1($1 );
4468
4472
n->oper = (List *) makeA_Expr(OP, " =" , NULL , NULL );
4469
4473
n->useor = FALSE ;
@@ -4474,7 +4478,7 @@ a_expr: c_expr
4474
4478
{
4475
4479
Node *n = NULL ;
4476
4480
List *l;
4477
- foreach (l, (List *) $4 )
4481
+ foreach (l, (List *) $3 )
4478
4482
{
4479
4483
Node *cmp = makeA_Expr (OP, " =" , $1 , lfirst (l));
4480
4484
if (n == NULL )
@@ -4485,12 +4489,12 @@ a_expr: c_expr
4485
4489
$$ = n;
4486
4490
}
4487
4491
}
4488
- | a_expr NOT IN ' ( ' in_expr ' ) '
4492
+ | a_expr NOT IN in_expr
4489
4493
{
4490
4494
/* in_expr returns a SubLink or a list of a_exprs */
4491
- if (IsA($5 , SubLink))
4495
+ if (IsA($4 , SubLink))
4492
4496
{
4493
- SubLink *n = (SubLink *)$5 ;
4497
+ SubLink *n = (SubLink *)$4 ;
4494
4498
n->lefthand = makeList1($1 );
4495
4499
n->oper = (List *) makeA_Expr(OP, " <>" , NULL , NULL );
4496
4500
n->useor = FALSE ;
@@ -4501,7 +4505,7 @@ a_expr: c_expr
4501
4505
{
4502
4506
Node *n = NULL ;
4503
4507
List *l;
4504
- foreach (l, (List *) $5 )
4508
+ foreach (l, (List *) $4 )
4505
4509
{
4506
4510
Node *cmp = makeA_Expr (OP, " <>" , $1 , lfirst (l));
4507
4511
if (n == NULL )
@@ -4512,14 +4516,14 @@ a_expr: c_expr
4512
4516
$$ = n;
4513
4517
}
4514
4518
}
4515
- | a_expr all_Op sub_type ' ( ' SelectStmt ' ) '
4519
+ | a_expr all_Op sub_type select_with_parens
4516
4520
{
4517
4521
SubLink *n = makeNode(SubLink);
4518
4522
n->lefthand = makeList1($1 );
4519
4523
n->oper = (List *) makeA_Expr(OP, $2 , NULL , NULL );
4520
4524
n->useor = FALSE ; /* doesn't matter since only one col */
4521
4525
n->subLinkType = $3 ;
4522
- n->subselect = $5 ;
4526
+ n->subselect = $4 ;
4523
4527
$$ = (Node *)n;
4524
4528
}
4525
4529
| row_expr
@@ -4539,9 +4543,9 @@ b_expr: c_expr
4539
4543
{ $$ = $1 ; }
4540
4544
| b_expr TYPECAST Typename
4541
4545
{ $$ = makeTypeCast($1 , $3 ); }
4542
- | ' +' b_expr %prec UMINUS
4546
+ | ' +' b_expr %prec UMINUS
4543
4547
{ $$ = makeA_Expr(OP, " +" , NULL , $2 ); }
4544
- | ' -' b_expr %prec UMINUS
4548
+ | ' -' b_expr %prec UMINUS
4545
4549
{ $$ = doNegate($2 ); }
4546
4550
| ' %' b_expr
4547
4551
{ $$ = makeA_Expr(OP, " %" , NULL , $2 ); }
@@ -4908,24 +4912,24 @@ c_expr: attr
4908
4912
n->agg_distinct = FALSE ;
4909
4913
$$ = (Node *)n;
4910
4914
}
4911
- | ' ( ' select_no_parens ' ) '
4915
+ | select_with_parens % prec UMINUS
4912
4916
{
4913
4917
SubLink *n = makeNode(SubLink);
4914
4918
n->lefthand = NIL;
4915
4919
n->oper = NIL;
4916
4920
n->useor = FALSE ;
4917
4921
n->subLinkType = EXPR_SUBLINK;
4918
- n->subselect = $2 ;
4922
+ n->subselect = $1 ;
4919
4923
$$ = (Node *)n;
4920
4924
}
4921
- | EXISTS ' ( ' SelectStmt ' ) '
4925
+ | EXISTS select_with_parens
4922
4926
{
4923
4927
SubLink *n = makeNode(SubLink);
4924
4928
n->lefthand = NIL;
4925
4929
n->oper = NIL;
4926
4930
n->useor = FALSE ;
4927
4931
n->subLinkType = EXISTS_SUBLINK;
4928
- n->subselect = $3 ;
4932
+ n->subselect = $2 ;
4929
4933
$$ = (Node *)n;
4930
4934
}
4931
4935
;
@@ -5037,14 +5041,14 @@ trim_list: a_expr FROM expr_list
5037
5041
{ $$ = $1 ; }
5038
5042
;
5039
5043
5040
- in_expr : SelectStmt
5044
+ in_expr : select_with_parens
5041
5045
{
5042
5046
SubLink *n = makeNode(SubLink);
5043
5047
n->subselect = $1 ;
5044
5048
$$ = (Node *)n;
5045
5049
}
5046
- | in_expr_nodes
5047
- { $$ = (Node *)$1 ; }
5050
+ | ' ( ' in_expr_nodes ' ) '
5051
+ { $$ = (Node *)$2 ; }
5048
5052
;
5049
5053
5050
5054
in_expr_nodes : a_expr
0 commit comments