11
11
*
12
12
*
13
13
* IDENTIFICATION
14
- * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.199 2000/10/28 15:44:04 momjian Exp $
14
+ * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.200 2000/10/28 19:41:00 momjian Exp $
15
15
*
16
16
* HISTORY
17
17
* AUTHOR DATE MAJOR EVENT
@@ -127,15 +127,14 @@ static void doNegateFloat(Value *v);
127
127
DropGroupStmt , DropPLangStmt , DropSchemaStmt , DropStmt , DropTrigStmt ,
128
128
DropUserStmt , DropdbStmt , ExplainStmt , ExtendStmt , FetchStmt ,
129
129
GrantStmt , IndexStmt , InsertStmt , ListenStmt , LoadStmt , LockStmt ,
130
- NotifyStmt , OptimizableStmt , ProcedureStmt
131
- QualifiedSelectStmt , ReindexStmt ,
130
+ NotifyStmt , OptimizableStmt , ProcedureStmt , ReindexStmt ,
132
131
RemoveAggrStmt , RemoveFuncStmt , RemoveOperStmt ,
133
132
RenameStmt , RevokeStmt , RuleActionStmt , RuleActionStmtOrEmpty ,
134
133
RuleStmt , SelectStmt , SetSessionStmt , TransactionStmt , TruncateStmt ,
135
134
UnlistenStmt , UpdateStmt , VacuumStmt , VariableResetStmt ,
136
135
VariableSetStmt , VariableShowStmt , ViewStmt
137
136
138
- %type <node> subquery , simple_select , select_head , set_select
137
+ %type <node> select_clause , select_subclause
139
138
140
139
%type <list> SessionList
141
140
%type <node> SessionClause
@@ -178,20 +177,19 @@ static void doNegateFloat(Value *v);
178
177
result , OptTempTableName , relation_name_list , OptTableElementList ,
179
178
OptUnder , OptInherit , definition , opt_distinct ,
180
179
opt_with , func_args , func_args_list , func_as ,
181
- oper_argtypes , RuleActionList , RuleActionMulti ,
182
- RuleActionOrSelectMulti , RuleActions , RuleActionBracket ,
180
+ oper_argtypes , RuleActionList , RuleActionMulti ,
183
181
opt_column_list , columnList , opt_va_list , va_list ,
184
182
sort_clause , sortby_list , index_params , index_list , name_list ,
185
183
from_clause , from_list , opt_array_bounds ,
186
184
expr_list , attrs , target_list , update_target_list ,
187
185
def_list , opt_indirection , group_clause , TriggerFuncArgs ,
188
- opt_select_limit , select_limit
186
+ opt_select_limit
189
187
190
188
%type <typnam> func_arg , func_return , aggr_argtype
191
189
192
190
%type <boolean> opt_arg , TriggerForOpt , TriggerForType , OptTemp
193
191
194
- %type <list> opt_for_update_clause , for_update_clause , update_list
192
+ %type <list> for_update_clause , update_list
195
193
%type <boolean> opt_all
196
194
%type <boolean> opt_table
197
195
%type <boolean> opt_chain , opt_trans
@@ -2662,7 +2660,7 @@ opt_column: COLUMN { $$ = COLUMN; }
2662
2660
RuleStmt : CREATE RULE name AS
2663
2661
{ QueryIsRule=TRUE ; }
2664
2662
ON event TO event_object where_clause
2665
- DO opt_instead RuleActions
2663
+ DO opt_instead RuleActionList
2666
2664
{
2667
2665
RuleStmt *n = makeNode(RuleStmt);
2668
2666
n->rulename = $3 ;
@@ -2675,78 +2673,28 @@ RuleStmt: CREATE RULE name AS
2675
2673
}
2676
2674
;
2677
2675
2678
- RuleActions : NOTHING { $$ = NIL; }
2679
- | RuleActionStmt { $$ = makeList1($1 ); }
2680
- | SelectStmt { $$ = makeList1($1 ); }
2681
- | RuleActionList
2682
- | RuleActionBracket
2683
- ;
2684
-
2685
- /* LEGACY: Version 7.0 did not like SELECT statements in these lists,
2686
- * but because of an oddity in the syntax for select_clause, allowed
2687
- * certain forms like "DO INSTEAD (select 1)", and this is used in
2688
- * the regression tests.
2689
- * Here, we're allowing just one SELECT in parentheses, to preserve
2690
- * any such expectations, and make the regression tests work.
2691
- * ++ KO'G
2692
- */
2693
- RuleActionList : ' (' RuleActionMulti ' )' { $$ = $2 ; }
2694
- | ' (' SelectStmt ' )' { $$ = makeList1($2 ); }
2695
- ;
2696
-
2697
- /* An undocumented feature, bracketed lists are allowed to contain
2698
- * SELECT statements on the same basis as the others. Before this,
2699
- * they were the same as parenthesized lists, and did not allow
2700
- * SelectStmts. Anybody know why they were here originally? Or if
2701
- * they're in the regression tests at all?
2702
- * ++ KO'G
2703
- */
2704
- RuleActionBracket : ' [' RuleActionOrSelectMulti ' ]' { $$ = $2 ; }
2676
+ RuleActionList : NOTHING { $$ = NIL; }
2677
+ | SelectStmt { $$ = makeList1($1 ); }
2678
+ | RuleActionStmt { $$ = makeList1($1 ); }
2679
+ | ' [' RuleActionMulti ' ]' { $$ = $2 ; }
2680
+ | ' (' RuleActionMulti ' )' { $$ = $2 ; }
2705
2681
;
2706
2682
2707
2683
/* the thrashing around here is to discard "empty" statements... */
2708
2684
RuleActionMulti : RuleActionMulti ' ;' RuleActionStmtOrEmpty
2709
2685
{ if ($3 != (Node *) NULL )
2710
- if ($1 != NIL)
2711
- $$ = lappend($1 , $3 );
2712
- else
2713
- $$ = makeList1($3 );
2714
- else
2715
- $$ = $1 ;
2716
- }
2717
- | RuleActionStmtOrEmpty
2718
- { if ($1 != (Node *) NULL )
2719
- $$ = makeList1($1 );
2720
- else
2721
- $$ = NIL;
2722
- }
2723
- ;
2724
-
2725
- RuleActionOrSelectMulti : RuleActionOrSelectMulti ' ;' RuleActionStmtOrEmpty
2726
- { if ($3 != (Node *) NULL )
2727
- if ($1 != NIL)
2728
- $$ = lappend($1 , $3 );
2729
- else
2730
- $$ = makeList1($3 );
2686
+ $$ = lappend($1 , $3 );
2731
2687
else
2732
2688
$$ = $1 ;
2733
2689
}
2734
- | RuleActionOrSelectMulti ' ;' SelectStmt
2735
- { if ($1 != NIL)
2736
- $$ = lappend($1 , $3 );
2737
- else
2738
- $$ = makeList1($3 );
2739
- }
2740
2690
| RuleActionStmtOrEmpty
2741
2691
{ if ($1 != (Node *) NULL )
2742
2692
$$ = makeList1($1 );
2743
2693
else
2744
2694
$$ = NIL;
2745
2695
}
2746
- | SelectStmt { $$ = makeList1($1 ); }
2747
2696
;
2748
2697
2749
-
2750
2698
RuleActionStmt : InsertStmt
2751
2699
| UpdateStmt
2752
2700
| DeleteStmt
@@ -3300,12 +3248,7 @@ opt_cursor: BINARY { $$ = TRUE; }
3300
3248
* However, this is not checked by the grammar; parse analysis must check it.
3301
3249
*/
3302
3250
3303
- SelectStmt : QualifiedSelectStmt
3304
- | select_head
3305
- ;
3306
-
3307
- QualifiedSelectStmt :
3308
- select_head sort_clause opt_for_update_clause opt_select_limit
3251
+ SelectStmt : select_clause sort_clause for_update_clause opt_select_limit
3309
3252
{
3310
3253
SelectStmt *n = findLeftmostSelect($1 );
3311
3254
@@ -3315,35 +3258,34 @@ QualifiedSelectStmt:
3315
3258
n->limitCount = nth(1 , $4 );
3316
3259
$$ = $1 ;
3317
3260
}
3318
- | select_head for_update_clause opt_select_limit
3319
- {
3320
- SelectStmt *n = findLeftmostSelect($1 );
3261
+ ;
3321
3262
3322
- n->sortClause = NULL ;
3323
- n->forUpdate = $2 ;
3324
- n->limitOffset = nth(0 , $3 );
3325
- n->limitCount = nth(1 , $3 );
3326
- $$ = $1 ;
3263
+ /* This rule parses Select statements that can appear within set operations,
3264
+ * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
3265
+ * the ordering of the set operations. Without '(' and ')' we want the
3266
+ * operations to be ordered per the precedence specs at the head of this file.
3267
+ *
3268
+ * Since parentheses around SELECTs also appear in the expression grammar,
3269
+ * there is a parse ambiguity if parentheses are allowed at the top level of a
3270
+ * select_clause: are the parens part of the expression or part of the select?
3271
+ * We separate select_clause into two levels to resolve this: select_clause
3272
+ * can have top-level parentheses, select_subclause cannot.
3273
+ *
3274
+ * Note that sort clauses cannot be included at this level --- a sort clause
3275
+ * can only appear at the end of the complete Select, and it will be handled
3276
+ * by the topmost SelectStmt rule. Likewise FOR UPDATE and LIMIT.
3277
+ */
3278
+ select_clause : ' (' select_subclause ' )'
3279
+ {
3280
+ $$ = $2 ;
3327
3281
}
3328
- | select_head select_limit
3282
+ | select_subclause
3329
3283
{
3330
- SelectStmt *n = findLeftmostSelect($1 );
3331
-
3332
- n->sortClause = NULL ;
3333
- n->forUpdate = NULL ;
3334
- n->limitOffset = nth(0 , $2 );
3335
- n->limitCount = nth(1 , $2 );
3336
- $$ = $1 ;
3284
+ $$ = $1 ;
3337
3285
}
3338
3286
;
3339
3287
3340
- subquery : ' (' subquery ' )' { $$ = $2 ; }
3341
- | ' (' QualifiedSelectStmt ' )' { $$ = $2 ; }
3342
- | ' (' set_select ' )' { $$ = $2 ; }
3343
- | simple_select { $$ = $1 ; }
3344
- ;
3345
-
3346
- simple_select : SELECT opt_distinct target_list
3288
+ select_subclause : SELECT opt_distinct target_list
3347
3289
result from_clause where_clause
3348
3290
group_clause having_clause
3349
3291
{
@@ -3358,13 +3300,7 @@ simple_select: SELECT opt_distinct target_list
3358
3300
n->havingClause = $8 ;
3359
3301
$$ = (Node *)n;
3360
3302
}
3361
- ;
3362
-
3363
- select_head : simple_select { $$ = $1 ; }
3364
- | set_select { $$ = $1 ; }
3365
- ;
3366
-
3367
- set_select : select_head UNION opt_all subquery
3303
+ | select_clause UNION opt_all select_clause
3368
3304
{
3369
3305
SetOperationStmt *n = makeNode(SetOperationStmt);
3370
3306
n->op = SETOP_UNION;
@@ -3373,7 +3309,7 @@ set_select: select_head UNION opt_all subquery
3373
3309
n->rarg = $4 ;
3374
3310
$$ = (Node *) n;
3375
3311
}
3376
- | select_head INTERSECT opt_all subquery
3312
+ | select_clause INTERSECT opt_all select_clause
3377
3313
{
3378
3314
SetOperationStmt *n = makeNode(SetOperationStmt);
3379
3315
n->op = SETOP_INTERSECT;
@@ -3382,7 +3318,7 @@ set_select: select_head UNION opt_all subquery
3382
3318
n->rarg = $4 ;
3383
3319
$$ = (Node *) n;
3384
3320
}
3385
- | select_head EXCEPT opt_all subquery
3321
+ | select_clause EXCEPT opt_all select_clause
3386
3322
{
3387
3323
SetOperationStmt *n = makeNode(SetOperationStmt);
3388
3324
n->op = SETOP_EXCEPT;
@@ -3447,6 +3383,7 @@ opt_distinct: DISTINCT { $$ = makeList1(NIL); }
3447
3383
;
3448
3384
3449
3385
sort_clause : ORDER BY sortby_list { $$ = $3 ; }
3386
+ | /* EMPTY*/ { $$ = NIL; }
3450
3387
;
3451
3388
3452
3389
sortby_list : sortby { $$ = makeList1($1 ); }
@@ -3468,7 +3405,7 @@ OptUseOp: USING all_Op { $$ = $2; }
3468
3405
;
3469
3406
3470
3407
3471
- select_limit : LIMIT select_limit_value ' ,' select_offset_value
3408
+ opt_select_limit : LIMIT select_limit_value ' ,' select_offset_value
3472
3409
{ $$ = makeList2($4 , $2 ); }
3473
3410
| LIMIT select_limit_value OFFSET select_offset_value
3474
3411
{ $$ = makeList2($4 , $2 ); }
@@ -3478,9 +3415,6 @@ select_limit: LIMIT select_limit_value ',' select_offset_value
3478
3415
{ $$ = makeList2($2 , $4 ); }
3479
3416
| OFFSET select_offset_value
3480
3417
{ $$ = makeList2($2 , NULL ); }
3481
- ;
3482
-
3483
- opt_select_limit : select_limit { $$ = $1 ; }
3484
3418
| /* EMPTY */
3485
3419
{ $$ = makeList2(NULL , NULL ); }
3486
3420
;
@@ -3580,9 +3514,6 @@ having_clause: HAVING a_expr
3580
3514
3581
3515
for_update_clause : FOR UPDATE update_list { $$ = $3 ; }
3582
3516
| FOR READ ONLY { $$ = NULL ; }
3583
- ;
3584
-
3585
- opt_for_update_clause : for_update_clause { $$ = $1 ; }
3586
3517
| /* EMPTY */ { $$ = NULL ; }
3587
3518
;
3588
3519
@@ -3626,7 +3557,7 @@ table_ref: relation_expr
3626
3557
$1 ->name = $2 ;
3627
3558
$$ = (Node *) $1 ;
3628
3559
}
3629
- | ' (' SelectStmt ' )' alias_clause
3560
+ | ' (' select_subclause ' )' alias_clause
3630
3561
{
3631
3562
RangeSubselect *n = makeNode(RangeSubselect);
3632
3563
n->subquery = $2 ;
@@ -4162,7 +4093,7 @@ opt_interval: datetime { $$ = makeList1($1); }
4162
4093
* Define row_descriptor to allow yacc to break the reduce/reduce conflict
4163
4094
* with singleton expressions.
4164
4095
*/
4165
- row_expr : ' (' row_descriptor ' )' IN ' (' SelectStmt ' )'
4096
+ row_expr : ' (' row_descriptor ' )' IN ' (' select_subclause ' )'
4166
4097
{
4167
4098
SubLink *n = makeNode(SubLink);
4168
4099
n->lefthand = $2 ;
@@ -4172,7 +4103,7 @@ row_expr: '(' row_descriptor ')' IN '(' SelectStmt ')'
4172
4103
n->subselect = $6 ;
4173
4104
$$ = (Node *)n;
4174
4105
}
4175
- | ' (' row_descriptor ' )' NOT IN ' (' SelectStmt ' )'
4106
+ | ' (' row_descriptor ' )' NOT IN ' (' select_subclause ' )'
4176
4107
{
4177
4108
SubLink *n = makeNode(SubLink);
4178
4109
n->lefthand = $2 ;
@@ -4182,7 +4113,7 @@ row_expr: '(' row_descriptor ')' IN '(' SelectStmt ')'
4182
4113
n->subselect = $7 ;
4183
4114
$$ = (Node *)n;
4184
4115
}
4185
- | ' (' row_descriptor ' )' all_Op sub_type ' (' SelectStmt ' )'
4116
+ | ' (' row_descriptor ' )' all_Op sub_type ' (' select_subclause ' )'
4186
4117
{
4187
4118
SubLink *n = makeNode(SubLink);
4188
4119
n->lefthand = $2 ;
@@ -4195,7 +4126,7 @@ row_expr: '(' row_descriptor ')' IN '(' SelectStmt ')'
4195
4126
n->subselect = $7 ;
4196
4127
$$ = (Node *)n;
4197
4128
}
4198
- | ' (' row_descriptor ' )' all_Op ' (' SelectStmt ' )'
4129
+ | ' (' row_descriptor ' )' all_Op ' (' select_subclause ' )'
4199
4130
{
4200
4131
SubLink *n = makeNode(SubLink);
4201
4132
n->lefthand = $2 ;
@@ -4526,7 +4457,7 @@ a_expr: c_expr
4526
4457
$$ = n;
4527
4458
}
4528
4459
}
4529
- | a_expr all_Op sub_type ' (' SelectStmt ' )'
4460
+ | a_expr all_Op sub_type ' (' select_subclause ' )'
4530
4461
{
4531
4462
SubLink *n = makeNode(SubLink);
4532
4463
n->lefthand = makeList1($1 );
@@ -4922,7 +4853,7 @@ c_expr: attr
4922
4853
n->agg_distinct = FALSE ;
4923
4854
$$ = (Node *)n;
4924
4855
}
4925
- | ' (' SelectStmt ' )'
4856
+ | ' (' select_subclause ' )'
4926
4857
{
4927
4858
SubLink *n = makeNode(SubLink);
4928
4859
n->lefthand = NIL;
@@ -4932,7 +4863,7 @@ c_expr: attr
4932
4863
n->subselect = $2 ;
4933
4864
$$ = (Node *)n;
4934
4865
}
4935
- | EXISTS ' (' SelectStmt ' )'
4866
+ | EXISTS ' (' select_subclause ' )'
4936
4867
{
4937
4868
SubLink *n = makeNode(SubLink);
4938
4869
n->lefthand = NIL;
@@ -5031,7 +4962,7 @@ trim_list: a_expr FROM expr_list
5031
4962
{ $$ = $1 ; }
5032
4963
;
5033
4964
5034
- in_expr : SelectStmt
4965
+ in_expr : select_subclause
5035
4966
{
5036
4967
SubLink *n = makeNode(SubLink);
5037
4968
n->subselect = $1 ;
0 commit comments