1111 *
1212 *
1313 * IDENTIFICATION
14- * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.199 2000/10/2815:44:04 momjian Exp $
14+ * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.200 2000/10/2819:41:00 momjian Exp $
1515 *
1616 * HISTORY
1717 * AUTHORDATEMAJOR EVENT
@@ -127,15 +127,14 @@ static void doNegateFloat(Value *v);
127127DropGroupStmt ,DropPLangStmt ,DropSchemaStmt ,DropStmt ,DropTrigStmt ,
128128DropUserStmt ,DropdbStmt ,ExplainStmt ,ExtendStmt ,FetchStmt ,
129129GrantStmt ,IndexStmt ,InsertStmt ,ListenStmt ,LoadStmt ,LockStmt ,
130- NotifyStmt ,OptimizableStmt ,ProcedureStmt
131- QualifiedSelectStmt ,ReindexStmt ,
130+ NotifyStmt ,OptimizableStmt ,ProcedureStmt ,ReindexStmt ,
132131RemoveAggrStmt ,RemoveFuncStmt ,RemoveOperStmt ,
133132RenameStmt ,RevokeStmt ,RuleActionStmt ,RuleActionStmtOrEmpty ,
134133RuleStmt ,SelectStmt ,SetSessionStmt ,TransactionStmt ,TruncateStmt ,
135134UnlistenStmt ,UpdateStmt ,VacuumStmt ,VariableResetStmt ,
136135VariableSetStmt ,VariableShowStmt ,ViewStmt
137136
138- %type <node> subquery , simple_select , select_head , set_select
137+ %type <node> select_clause , select_subclause
139138
140139%type <list> SessionList
141140%type <node> SessionClause
@@ -178,20 +177,19 @@ static void doNegateFloat(Value *v);
178177result ,OptTempTableName ,relation_name_list ,OptTableElementList ,
179178OptUnder ,OptInherit ,definition ,opt_distinct ,
180179opt_with ,func_args ,func_args_list ,func_as ,
181- oper_argtypes ,RuleActionList ,RuleActionMulti ,
182- RuleActionOrSelectMulti ,RuleActions ,RuleActionBracket ,
180+ oper_argtypes ,RuleActionList ,RuleActionMulti ,
183181opt_column_list ,columnList ,opt_va_list ,va_list ,
184182sort_clause ,sortby_list ,index_params ,index_list ,name_list ,
185183from_clause ,from_list ,opt_array_bounds ,
186184expr_list ,attrs ,target_list ,update_target_list ,
187185def_list ,opt_indirection ,group_clause ,TriggerFuncArgs ,
188- opt_select_limit , select_limit
186+ opt_select_limit
189187
190188%type <typnam> func_arg ,func_return ,aggr_argtype
191189
192190%type <boolean> opt_arg ,TriggerForOpt ,TriggerForType ,OptTemp
193191
194- %type <list> opt_for_update_clause , for_update_clause ,update_list
192+ %type <list> for_update_clause ,update_list
195193%type <boolean> opt_all
196194%type <boolean> opt_table
197195%type <boolean> opt_chain ,opt_trans
@@ -2662,7 +2660,7 @@ opt_column: COLUMN{ $$ = COLUMN; }
26622660RuleStmt :CREATE RULE name AS
26632661 { QueryIsRule=TRUE ; }
26642662ON event TO event_object where_clause
2665- DO opt_instead RuleActions
2663+ DO opt_instead RuleActionList
26662664{
26672665RuleStmt *n = makeNode(RuleStmt);
26682666n->rulename =$3 ;
@@ -2675,78 +2673,28 @@ RuleStmt: CREATE RULE name AS
26752673}
26762674;
26772675
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 ; }
27052681;
27062682
27072683/* the thrashing around here is to discard "empty" statements...*/
27082684RuleActionMulti :RuleActionMulti ' ;' RuleActionStmtOrEmpty
27092685{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 );
27312687else
27322688$$ =$1 ;
27332689}
2734- | RuleActionOrSelectMulti ' ;' SelectStmt
2735- {if ($1 != NIL)
2736- $$ = lappend($1 ,$3 );
2737- else
2738- $$ = makeList1($3 );
2739- }
27402690| RuleActionStmtOrEmpty
27412691{if ($1 != (Node *)NULL )
27422692$$ = makeList1($1 );
27432693else
27442694$$ = NIL;
27452695}
2746- | SelectStmt {$$ = makeList1($1 ); }
27472696;
27482697
2749-
27502698RuleActionStmt :InsertStmt
27512699| UpdateStmt
27522700| DeleteStmt
@@ -3300,12 +3248,7 @@ opt_cursor: BINARY{ $$ = TRUE; }
33003248 * However, this is not checked by the grammar; parse analysis must check it.
33013249*/
33023250
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
33093252{
33103253SelectStmt *n = findLeftmostSelect($1 );
33113254
@@ -3315,35 +3258,34 @@ QualifiedSelectStmt:
33153258n->limitCount = nth(1 ,$4 );
33163259$$ =$1 ;
33173260}
3318- | select_head for_update_clause opt_select_limit
3319- {
3320- SelectStmt *n = findLeftmostSelect($1 );
3261+ ;
33213262
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 ;
33273281}
3328- | select_head select_limit
3282+ | select_subclause
33293283{
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 ;
33373285}
33383286;
33393287
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
33473289result from_clause where_clause
33483290group_clause having_clause
33493291{
@@ -3358,13 +3300,7 @@ simple_select: SELECT opt_distinct target_list
33583300n->havingClause =$8 ;
33593301$$ = (Node *)n;
33603302}
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
33683304{
33693305SetOperationStmt *n = makeNode(SetOperationStmt);
33703306n->op = SETOP_UNION;
@@ -3373,7 +3309,7 @@ set_select: select_head UNION opt_all subquery
33733309n->rarg =$4 ;
33743310$$ = (Node *) n;
33753311}
3376- | select_head INTERSECT opt_all subquery
3312+ | select_clause INTERSECT opt_all select_clause
33773313{
33783314SetOperationStmt *n = makeNode(SetOperationStmt);
33793315n->op = SETOP_INTERSECT;
@@ -3382,7 +3318,7 @@ set_select: select_head UNION opt_all subquery
33823318n->rarg =$4 ;
33833319$$ = (Node *) n;
33843320}
3385- | select_head EXCEPT opt_all subquery
3321+ | select_clause EXCEPT opt_all select_clause
33863322{
33873323SetOperationStmt *n = makeNode(SetOperationStmt);
33883324n->op = SETOP_EXCEPT;
@@ -3447,6 +3383,7 @@ opt_distinct: DISTINCT{ $$ = makeList1(NIL); }
34473383;
34483384
34493385sort_clause :ORDER BY sortby_list {$$ =$3 ; }
3386+ | /* EMPTY*/ {$$ = NIL; }
34503387;
34513388
34523389sortby_list :sortby {$$ = makeList1($1 ); }
@@ -3468,7 +3405,7 @@ OptUseOp: USING all_Op{ $$ = $2; }
34683405;
34693406
34703407
3471- select_limit :LIMIT select_limit_value ' ,' select_offset_value
3408+ opt_select_limit :LIMIT select_limit_value ' ,' select_offset_value
34723409{$$ = makeList2($4 ,$2 ); }
34733410| LIMIT select_limit_value OFFSET select_offset_value
34743411{$$ = makeList2($4 ,$2 ); }
@@ -3478,9 +3415,6 @@ select_limit:LIMIT select_limit_value ',' select_offset_value
34783415{$$ = makeList2($2 ,$4 ); }
34793416| OFFSET select_offset_value
34803417{$$ = makeList2($2 ,NULL ); }
3481- ;
3482-
3483- opt_select_limit :select_limit {$$ =$1 ; }
34843418| /* EMPTY*/
34853419{$$ = makeList2(NULL ,NULL ); }
34863420;
@@ -3580,9 +3514,6 @@ having_clause: HAVING a_expr
35803514
35813515for_update_clause :FOR UPDATE update_list {$$ =$3 ; }
35823516| FOR READ ONLY {$$ =NULL ; }
3583- ;
3584-
3585- opt_for_update_clause :for_update_clause {$$ =$1 ; }
35863517| /* EMPTY*/ {$$ =NULL ; }
35873518;
35883519
@@ -3626,7 +3557,7 @@ table_ref: relation_expr
36263557$1 ->name =$2 ;
36273558$$ = (Node *)$1 ;
36283559}
3629- | ' (' SelectStmt ' )' alias_clause
3560+ | ' (' select_subclause ' )' alias_clause
36303561{
36313562RangeSubselect *n = makeNode(RangeSubselect);
36323563n->subquery =$2 ;
@@ -4162,7 +4093,7 @@ opt_interval: datetime{ $$ = makeList1($1); }
41624093 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
41634094 * with singleton expressions.
41644095*/
4165- row_expr :' (' row_descriptor ' )' IN ' (' SelectStmt ' )'
4096+ row_expr :' (' row_descriptor ' )' IN ' (' select_subclause ' )'
41664097{
41674098SubLink *n = makeNode(SubLink);
41684099n->lefthand =$2 ;
@@ -4172,7 +4103,7 @@ row_expr: '(' row_descriptor ')' IN '(' SelectStmt ')'
41724103n->subselect =$6 ;
41734104$$ = (Node *)n;
41744105}
4175- | ' (' row_descriptor ' )' NOT IN ' (' SelectStmt ' )'
4106+ | ' (' row_descriptor ' )' NOT IN ' (' select_subclause ' )'
41764107{
41774108SubLink *n = makeNode(SubLink);
41784109n->lefthand =$2 ;
@@ -4182,7 +4113,7 @@ row_expr: '(' row_descriptor ')' IN '(' SelectStmt ')'
41824113n->subselect =$7 ;
41834114$$ = (Node *)n;
41844115}
4185- | ' (' row_descriptor ' )' all_Op sub_type ' (' SelectStmt ' )'
4116+ | ' (' row_descriptor ' )' all_Op sub_type ' (' select_subclause ' )'
41864117{
41874118SubLink *n = makeNode(SubLink);
41884119n->lefthand =$2 ;
@@ -4195,7 +4126,7 @@ row_expr: '(' row_descriptor ')' IN '(' SelectStmt ')'
41954126n->subselect =$7 ;
41964127$$ = (Node *)n;
41974128}
4198- | ' (' row_descriptor ' )' all_Op ' (' SelectStmt ' )'
4129+ | ' (' row_descriptor ' )' all_Op ' (' select_subclause ' )'
41994130{
42004131SubLink *n = makeNode(SubLink);
42014132n->lefthand =$2 ;
@@ -4526,7 +4457,7 @@ a_expr: c_expr
45264457$$ = n;
45274458}
45284459}
4529- | a_expr all_Op sub_type ' (' SelectStmt ' )'
4460+ | a_expr all_Op sub_type ' (' select_subclause ' )'
45304461{
45314462SubLink *n = makeNode(SubLink);
45324463n->lefthand = makeList1($1 );
@@ -4922,7 +4853,7 @@ c_expr: attr
49224853n->agg_distinct =FALSE ;
49234854$$ = (Node *)n;
49244855}
4925- | ' (' SelectStmt ' )'
4856+ | ' (' select_subclause ' )'
49264857{
49274858SubLink *n = makeNode(SubLink);
49284859n->lefthand = NIL;
@@ -4932,7 +4863,7 @@ c_expr: attr
49324863n->subselect =$2 ;
49334864$$ = (Node *)n;
49344865}
4935- | EXISTS ' (' SelectStmt ' )'
4866+ | EXISTS ' (' select_subclause ' )'
49364867{
49374868SubLink *n = makeNode(SubLink);
49384869n->lefthand = NIL;
@@ -5031,7 +4962,7 @@ trim_list: a_expr FROM expr_list
50314962{$$ =$1 ; }
50324963;
50334964
5034- in_expr :SelectStmt
4965+ in_expr :select_subclause
50354966{
50364967SubLink *n = makeNode(SubLink);
50374968n->subselect =$1 ;