xref: /sqlite-3.40.0/src/parse.y (revision af94adf0)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains SQLite's grammar for SQL.  Process this file
13 ** using the lemon parser generator to generate C code that runs
14 ** the parser.  Lemon will also generate a header file containing
15 ** numeric codes for all of the tokens.
16 */
17 
18 // All token codes are small integers with #defines that begin with "TK_"
19 %token_prefix TK_
20 
21 // The type of the data attached to each token is Token.  This is also the
22 // default type for non-terminals.
23 //
24 %token_type {Token}
25 %default_type {Token}
26 
27 // An extra argument to the constructor for the parser, which is available
28 // to all actions.
29 %extra_context {Parse *pParse}
30 
31 // This code runs whenever there is a syntax error
32 //
33 %syntax_error {
34   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
35   if( TOKEN.z[0] ){
36     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
37   }else{
38     sqlite3ErrorMsg(pParse, "incomplete input");
39   }
40 }
41 %stack_overflow {
42   sqlite3ErrorMsg(pParse, "parser stack overflow");
43 }
44 
45 // The name of the generated procedure that implements the parser
46 // is as follows:
47 %name sqlite3Parser
48 
49 // The following text is included near the beginning of the C source
50 // code file that implements the parser.
51 //
52 %include {
53 #include "sqliteInt.h"
54 
55 /*
56 ** Disable all error recovery processing in the parser push-down
57 ** automaton.
58 */
59 #define YYNOERRORRECOVERY 1
60 
61 /*
62 ** Make yytestcase() the same as testcase()
63 */
64 #define yytestcase(X) testcase(X)
65 
66 /*
67 ** Indicate that sqlite3ParserFree() will never be called with a null
68 ** pointer.
69 */
70 #define YYPARSEFREENEVERNULL 1
71 
72 /*
73 ** In the amalgamation, the parse.c file generated by lemon and the
74 ** tokenize.c file are concatenated.  In that case, sqlite3RunParser()
75 ** has access to the the size of the yyParser object and so the parser
76 ** engine can be allocated from stack.  In that case, only the
77 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
78 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
79 ** omitted.
80 */
81 #ifdef SQLITE_AMALGAMATION
82 # define sqlite3Parser_ENGINEALWAYSONSTACK 1
83 #endif
84 
85 /*
86 ** Alternative datatype for the argument to the malloc() routine passed
87 ** into sqlite3ParserAlloc().  The default is size_t.
88 */
89 #define YYMALLOCARGTYPE  u64
90 
91 /*
92 ** An instance of the following structure describes the event of a
93 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
94 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
95 **
96 **      UPDATE ON (a,b,c)
97 **
98 ** Then the "b" IdList records the list "a,b,c".
99 */
100 struct TrigEvent { int a; IdList * b; };
101 
102 struct FrameBound     { int eType; Expr *pExpr; };
103 
104 /*
105 ** Disable lookaside memory allocation for objects that might be
106 ** shared across database connections.
107 */
108 static void disableLookaside(Parse *pParse){
109   sqlite3 *db = pParse->db;
110   pParse->disableLookaside++;
111   DisableLookaside;
112 }
113 
114 } // end %include
115 
116 // Input is a single SQL command
117 input ::= cmdlist.
118 cmdlist ::= cmdlist ecmd.
119 cmdlist ::= ecmd.
120 ecmd ::= SEMI.
121 ecmd ::= cmdx SEMI.
122 %ifndef SQLITE_OMIT_EXPLAIN
123 ecmd ::= explain cmdx.
124 explain ::= EXPLAIN.              { pParse->explain = 1; }
125 explain ::= EXPLAIN QUERY PLAN.   { pParse->explain = 2; }
126 %endif  SQLITE_OMIT_EXPLAIN
127 cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
128 
129 ///////////////////// Begin and end transactions. ////////////////////////////
130 //
131 
132 cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
133 trans_opt ::= .
134 trans_opt ::= TRANSACTION.
135 trans_opt ::= TRANSACTION nm.
136 %type transtype {int}
137 transtype(A) ::= .             {A = TK_DEFERRED;}
138 transtype(A) ::= DEFERRED(X).  {A = @X; /*A-overwrites-X*/}
139 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
140 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
141 cmd ::= COMMIT|END(X) trans_opt.   {sqlite3EndTransaction(pParse,@X);}
142 cmd ::= ROLLBACK(X) trans_opt.     {sqlite3EndTransaction(pParse,@X);}
143 
144 savepoint_opt ::= SAVEPOINT.
145 savepoint_opt ::= .
146 cmd ::= SAVEPOINT nm(X). {
147   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
148 }
149 cmd ::= RELEASE savepoint_opt nm(X). {
150   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
151 }
152 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
153   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
154 }
155 
156 ///////////////////// The CREATE TABLE statement ////////////////////////////
157 //
158 cmd ::= create_table create_table_args.
159 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
160    sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
161 }
162 createkw(A) ::= CREATE(A).  {disableLookaside(pParse);}
163 
164 %type ifnotexists {int}
165 ifnotexists(A) ::= .              {A = 0;}
166 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
167 %type temp {int}
168 %ifndef SQLITE_OMIT_TEMPDB
169 temp(A) ::= TEMP.  {A = 1;}
170 %endif  SQLITE_OMIT_TEMPDB
171 temp(A) ::= .      {A = 0;}
172 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
173   sqlite3EndTable(pParse,&X,&E,F,0);
174 }
175 create_table_args ::= AS select(S). {
176   sqlite3EndTable(pParse,0,0,0,S);
177   sqlite3SelectDelete(pParse->db, S);
178 }
179 %type table_options {int}
180 table_options(A) ::= .    {A = 0;}
181 table_options(A) ::= WITHOUT nm(X). {
182   if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
183     A = TF_WithoutRowid | TF_NoVisibleRowid;
184   }else{
185     A = 0;
186     sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
187   }
188 }
189 columnlist ::= columnlist COMMA columnname carglist.
190 columnlist ::= columnname carglist.
191 columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
192 
193 // Declare some tokens early in order to influence their values, to
194 // improve performance and reduce the executable size.  The goal here is
195 // to get the "jump" operations in ISNULL through ESCAPE to have numeric
196 // values that are early enough so that all jump operations are clustered
197 // at the beginning.
198 //
199 %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
200 %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
201 %token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
202 %token GT LE LT GE ESCAPE.
203 
204 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
205 // fallback to ID if they will not parse as their original value.
206 // This obviates the need for the "id" nonterminal.
207 //
208 %fallback ID
209   ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
210   CONFLICT DATABASE DEFERRED DESC DETACH DO
211   EACH END EXCLUSIVE EXPLAIN FAIL FOR
212   IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
213   QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
214   ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
215   NULLS FIRST LAST
216 %ifdef SQLITE_OMIT_COMPOUND_SELECT
217   EXCEPT INTERSECT UNION
218 %endif SQLITE_OMIT_COMPOUND_SELECT
219 %ifndef SQLITE_OMIT_WINDOWFUNC
220   CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED
221   EXCLUDE GROUPS OTHERS TIES
222 %endif SQLITE_OMIT_WINDOWFUNC
223   REINDEX RENAME CTIME_KW IF
224   .
225 %wildcard ANY.
226 
227 // Define operator precedence early so that this is the first occurrence
228 // of the operator tokens in the grammer.  Keeping the operators together
229 // causes them to be assigned integer values that are close together,
230 // which keeps parser tables smaller.
231 //
232 // The token values assigned to these symbols is determined by the order
233 // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
234 // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
235 // the sqlite3ExprIfFalse() routine for additional information on this
236 // constraint.
237 //
238 %left OR.
239 %left AND.
240 %right NOT.
241 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
242 %left GT LE LT GE.
243 %right ESCAPE.
244 %left BITAND BITOR LSHIFT RSHIFT.
245 %left PLUS MINUS.
246 %left STAR SLASH REM.
247 %left CONCAT.
248 %left COLLATE.
249 %right BITNOT.
250 %nonassoc ON.
251 
252 // An IDENTIFIER can be a generic identifier, or one of several
253 // keywords.  Any non-standard keyword can also be an identifier.
254 //
255 %token_class id  ID|INDEXED.
256 
257 
258 // And "ids" is an identifer-or-string.
259 //
260 %token_class ids  ID|STRING.
261 
262 // The name of a column or table can be any of the following:
263 //
264 %type nm {Token}
265 nm(A) ::= id(A).
266 nm(A) ::= STRING(A).
267 nm(A) ::= JOIN_KW(A).
268 
269 // A typetoken is really zero or more tokens that form a type name such
270 // as can be found after the column name in a CREATE TABLE statement.
271 // Multiple tokens are concatenated to form the value of the typetoken.
272 //
273 %type typetoken {Token}
274 typetoken(A) ::= .   {A.n = 0; A.z = 0;}
275 typetoken(A) ::= typename(A).
276 typetoken(A) ::= typename(A) LP signed RP(Y). {
277   A.n = (int)(&Y.z[Y.n] - A.z);
278 }
279 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
280   A.n = (int)(&Y.z[Y.n] - A.z);
281 }
282 %type typename {Token}
283 typename(A) ::= ids(A).
284 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
285 signed ::= plus_num.
286 signed ::= minus_num.
287 
288 // The scanpt non-terminal takes a value which is a pointer to the
289 // input text just past the last token that has been shifted into
290 // the parser.  By surrounding some phrase in the grammar with two
291 // scanpt non-terminals, we can capture the input text for that phrase.
292 // For example:
293 //
294 //      something ::= .... scanpt(A) phrase scanpt(Z).
295 //
296 // The text that is parsed as "phrase" is a string starting at A
297 // and containing (int)(Z-A) characters.  There might be some extra
298 // whitespace on either end of the text, but that can be removed in
299 // post-processing, if needed.
300 //
301 %type scanpt {const char*}
302 scanpt(A) ::= . {
303   assert( yyLookahead!=YYNOCODE );
304   A = yyLookaheadToken.z;
305 }
306 scantok(A) ::= . {
307   assert( yyLookahead!=YYNOCODE );
308   A = yyLookaheadToken;
309 }
310 
311 // "carglist" is a list of additional constraints that come after the
312 // column name and column type in a CREATE TABLE statement.
313 //
314 carglist ::= carglist ccons.
315 carglist ::= .
316 ccons ::= CONSTRAINT nm(X).           {pParse->constraintName = X;}
317 ccons ::= DEFAULT scantok(A) term(X).
318                             {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);}
319 ccons ::= DEFAULT LP(A) expr(X) RP(Z).
320                             {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
321 ccons ::= DEFAULT PLUS(A) scantok(Z) term(X).
322                             {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);}
323 ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). {
324   Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
325   sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]);
326 }
327 ccons ::= DEFAULT scantok id(X).       {
328   Expr *p = tokenExpr(pParse, TK_STRING, X);
329   if( p ){
330     sqlite3ExprIdToTrueFalse(p);
331     testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
332   }
333     sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
334 }
335 
336 // In addition to the type name, we also care about the primary key and
337 // UNIQUE constraints.
338 //
339 ccons ::= NULL onconf.
340 ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
341 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
342                                  {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
343 ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
344                                    SQLITE_IDXTYPE_UNIQUE);}
345 ccons ::= CHECK LP expr(X) RP.   {sqlite3AddCheckConstraint(pParse,X);}
346 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
347                                  {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
348 ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
349 ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
350 
351 // The optional AUTOINCREMENT keyword
352 %type autoinc {int}
353 autoinc(X) ::= .          {X = 0;}
354 autoinc(X) ::= AUTOINCR.  {X = 1;}
355 
356 // The next group of rules parses the arguments to a REFERENCES clause
357 // that determine if the referential integrity checking is deferred or
358 // or immediate and which determine what action to take if a ref-integ
359 // check fails.
360 //
361 %type refargs {int}
362 refargs(A) ::= .                  { A = OE_None*0x0101; /* EV: R-19803-45884 */}
363 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
364 %type refarg {struct {int value; int mask;}}
365 refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
366 refarg(A) ::= ON INSERT refact.      { A.value = 0;     A.mask = 0x000000; }
367 refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
368 refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
369 %type refact {int}
370 refact(A) ::= SET NULL.              { A = OE_SetNull;  /* EV: R-33326-45252 */}
371 refact(A) ::= SET DEFAULT.           { A = OE_SetDflt;  /* EV: R-33326-45252 */}
372 refact(A) ::= CASCADE.               { A = OE_Cascade;  /* EV: R-33326-45252 */}
373 refact(A) ::= RESTRICT.              { A = OE_Restrict; /* EV: R-33326-45252 */}
374 refact(A) ::= NO ACTION.             { A = OE_None;     /* EV: R-33326-45252 */}
375 %type defer_subclause {int}
376 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt.     {A = 0;}
377 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
378 %type init_deferred_pred_opt {int}
379 init_deferred_pred_opt(A) ::= .                       {A = 0;}
380 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
381 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
382 
383 conslist_opt(A) ::= .                         {A.n = 0; A.z = 0;}
384 conslist_opt(A) ::= COMMA(A) conslist.
385 conslist ::= conslist tconscomma tcons.
386 conslist ::= tcons.
387 tconscomma ::= COMMA.            {pParse->constraintName.n = 0;}
388 tconscomma ::= .
389 tcons ::= CONSTRAINT nm(X).      {pParse->constraintName = X;}
390 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
391                                  {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
392 tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
393                                  {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
394                                        SQLITE_IDXTYPE_UNIQUE);}
395 tcons ::= CHECK LP expr(E) RP onconf.
396                                  {sqlite3AddCheckConstraint(pParse,E);}
397 tcons ::= FOREIGN KEY LP eidlist(FA) RP
398           REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
399     sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
400     sqlite3DeferForeignKey(pParse, D);
401 }
402 %type defer_subclause_opt {int}
403 defer_subclause_opt(A) ::= .                    {A = 0;}
404 defer_subclause_opt(A) ::= defer_subclause(A).
405 
406 // The following is a non-standard extension that allows us to declare the
407 // default behavior when there is a constraint conflict.
408 //
409 %type onconf {int}
410 %type orconf {int}
411 %type resolvetype {int}
412 onconf(A) ::= .                              {A = OE_Default;}
413 onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
414 orconf(A) ::= .                              {A = OE_Default;}
415 orconf(A) ::= OR resolvetype(X).             {A = X;}
416 resolvetype(A) ::= raisetype(A).
417 resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
418 resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
419 
420 ////////////////////////// The DROP TABLE /////////////////////////////////////
421 //
422 cmd ::= DROP TABLE ifexists(E) fullname(X). {
423   sqlite3DropTable(pParse, X, 0, E);
424 }
425 %type ifexists {int}
426 ifexists(A) ::= IF EXISTS.   {A = 1;}
427 ifexists(A) ::= .            {A = 0;}
428 
429 ///////////////////// The CREATE VIEW statement /////////////////////////////
430 //
431 %ifndef SQLITE_OMIT_VIEW
432 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
433           AS select(S). {
434   sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
435 }
436 cmd ::= DROP VIEW ifexists(E) fullname(X). {
437   sqlite3DropTable(pParse, X, 1, E);
438 }
439 %endif  SQLITE_OMIT_VIEW
440 
441 //////////////////////// The SELECT statement /////////////////////////////////
442 //
443 cmd ::= select(X).  {
444   SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
445   sqlite3Select(pParse, X, &dest);
446   sqlite3SelectDelete(pParse->db, X);
447 }
448 
449 %type select {Select*}
450 %destructor select {sqlite3SelectDelete(pParse->db, $$);}
451 %type selectnowith {Select*}
452 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
453 %type oneselect {Select*}
454 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
455 
456 %include {
457   /*
458   ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
459   ** all elements in the list.  And make sure list length does not exceed
460   ** SQLITE_LIMIT_COMPOUND_SELECT.
461   */
462   static void parserDoubleLinkSelect(Parse *pParse, Select *p){
463     assert( p!=0 );
464     if( p->pPrior ){
465       Select *pNext = 0, *pLoop;
466       int mxSelect, cnt = 0;
467       for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
468         pLoop->pNext = pNext;
469         pLoop->selFlags |= SF_Compound;
470       }
471       if( (p->selFlags & SF_MultiValue)==0 &&
472         (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
473         cnt>mxSelect
474       ){
475         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
476       }
477     }
478   }
479 }
480 
481 %ifndef SQLITE_OMIT_CTE
482 select(A) ::= WITH wqlist(W) selectnowith(X). {
483   Select *p = X;
484   if( p ){
485     p->pWith = W;
486     parserDoubleLinkSelect(pParse, p);
487   }else{
488     sqlite3WithDelete(pParse->db, W);
489   }
490   A = p;
491 }
492 select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). {
493   Select *p = X;
494   if( p ){
495     p->pWith = W;
496     parserDoubleLinkSelect(pParse, p);
497   }else{
498     sqlite3WithDelete(pParse->db, W);
499   }
500   A = p;
501 }
502 %endif /* SQLITE_OMIT_CTE */
503 select(A) ::= selectnowith(X). {
504   Select *p = X;
505   if( p ){
506     parserDoubleLinkSelect(pParse, p);
507   }
508   A = p; /*A-overwrites-X*/
509 }
510 
511 selectnowith(A) ::= oneselect(A).
512 %ifndef SQLITE_OMIT_COMPOUND_SELECT
513 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z).  {
514   Select *pRhs = Z;
515   Select *pLhs = A;
516   if( pRhs && pRhs->pPrior ){
517     SrcList *pFrom;
518     Token x;
519     x.n = 0;
520     parserDoubleLinkSelect(pParse, pRhs);
521     pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
522     pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
523   }
524   if( pRhs ){
525     pRhs->op = (u8)Y;
526     pRhs->pPrior = pLhs;
527     if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
528     pRhs->selFlags &= ~SF_MultiValue;
529     if( Y!=TK_ALL ) pParse->hasCompound = 1;
530   }else{
531     sqlite3SelectDelete(pParse->db, pLhs);
532   }
533   A = pRhs;
534 }
535 %type multiselect_op {int}
536 multiselect_op(A) ::= UNION(OP).             {A = @OP; /*A-overwrites-OP*/}
537 multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
538 multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP; /*A-overwrites-OP*/}
539 %endif SQLITE_OMIT_COMPOUND_SELECT
540 
541 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
542                  groupby_opt(P) having_opt(Q)
543                  orderby_opt(Z) limit_opt(L). {
544   A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
545 }
546 %ifndef SQLITE_OMIT_WINDOWFUNC
547 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
548                  groupby_opt(P) having_opt(Q) window_clause(R)
549                  orderby_opt(Z) limit_opt(L). {
550   A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
551   if( A ){
552     A->pWinDefn = R;
553   }else{
554     sqlite3WindowListDelete(pParse->db, R);
555   }
556 }
557 %endif
558 
559 
560 oneselect(A) ::= values(A).
561 
562 %type values {Select*}
563 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
564 values(A) ::= VALUES LP nexprlist(X) RP. {
565   A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
566 }
567 values(A) ::= values(A) COMMA LP nexprlist(Y) RP. {
568   Select *pRight, *pLeft = A;
569   pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
570   if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
571   if( pRight ){
572     pRight->op = TK_ALL;
573     pRight->pPrior = pLeft;
574     A = pRight;
575   }else{
576     A = pLeft;
577   }
578 }
579 
580 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
581 // present and false (0) if it is not.
582 //
583 %type distinct {int}
584 distinct(A) ::= DISTINCT.   {A = SF_Distinct;}
585 distinct(A) ::= ALL.        {A = SF_All;}
586 distinct(A) ::= .           {A = 0;}
587 
588 // selcollist is a list of expressions that are to become the return
589 // values of the SELECT statement.  The "*" in statements like
590 // "SELECT * FROM ..." is encoded as a special expression with an
591 // opcode of TK_ASTERISK.
592 //
593 %type selcollist {ExprList*}
594 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
595 %type sclp {ExprList*}
596 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
597 sclp(A) ::= selcollist(A) COMMA.
598 sclp(A) ::= .                                {A = 0;}
599 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y).     {
600    A = sqlite3ExprListAppend(pParse, A, X);
601    if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
602    sqlite3ExprListSetSpan(pParse,A,B,Z);
603 }
604 selcollist(A) ::= sclp(A) scanpt STAR. {
605   Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
606   A = sqlite3ExprListAppend(pParse, A, p);
607 }
608 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
609   Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
610   Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
611   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
612   A = sqlite3ExprListAppend(pParse,A, pDot);
613 }
614 
615 // An option "AS <id>" phrase that can follow one of the expressions that
616 // define the result set, or one of the tables in the FROM clause.
617 //
618 %type as {Token}
619 as(X) ::= AS nm(Y).    {X = Y;}
620 as(X) ::= ids(X).
621 as(X) ::= .            {X.n = 0; X.z = 0;}
622 
623 
624 %type seltablist {SrcList*}
625 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
626 %type stl_prefix {SrcList*}
627 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
628 %type from {SrcList*}
629 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
630 
631 // A complete FROM clause.
632 //
633 from(A) ::= .                {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
634 from(A) ::= FROM seltablist(X). {
635   A = X;
636   sqlite3SrcListShiftJoinType(A);
637 }
638 
639 // "seltablist" is a "Select Table List" - the content of the FROM clause
640 // in a SELECT statement.  "stl_prefix" is a prefix of this list.
641 //
642 stl_prefix(A) ::= seltablist(A) joinop(Y).    {
643    if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
644 }
645 stl_prefix(A) ::= .                           {A = 0;}
646 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
647                   on_opt(N) using_opt(U). {
648   A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
649   sqlite3SrcListIndexedBy(pParse, A, &I);
650 }
651 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
652                   on_opt(N) using_opt(U). {
653   A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
654   sqlite3SrcListFuncArgs(pParse, A, E);
655 }
656 %ifndef SQLITE_OMIT_SUBQUERY
657   seltablist(A) ::= stl_prefix(A) LP select(S) RP
658                     as(Z) on_opt(N) using_opt(U). {
659     A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
660   }
661   seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
662                     as(Z) on_opt(N) using_opt(U). {
663     if( A==0 && Z.n==0 && N==0 && U==0 ){
664       A = F;
665     }else if( F->nSrc==1 ){
666       A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
667       if( A ){
668         struct SrcList_item *pNew = &A->a[A->nSrc-1];
669         struct SrcList_item *pOld = F->a;
670         pNew->zName = pOld->zName;
671         pNew->zDatabase = pOld->zDatabase;
672         pNew->pSelect = pOld->pSelect;
673         if( pOld->fg.isTabFunc ){
674           pNew->u1.pFuncArg = pOld->u1.pFuncArg;
675           pOld->u1.pFuncArg = 0;
676           pOld->fg.isTabFunc = 0;
677           pNew->fg.isTabFunc = 1;
678         }
679         pOld->zName = pOld->zDatabase = 0;
680         pOld->pSelect = 0;
681       }
682       sqlite3SrcListDelete(pParse->db, F);
683     }else{
684       Select *pSubquery;
685       sqlite3SrcListShiftJoinType(F);
686       pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
687       A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
688     }
689   }
690 %endif  SQLITE_OMIT_SUBQUERY
691 
692 %type dbnm {Token}
693 dbnm(A) ::= .          {A.z=0; A.n=0;}
694 dbnm(A) ::= DOT nm(X). {A = X;}
695 
696 %type fullname {SrcList*}
697 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
698 fullname(A) ::= nm(X).  {
699   A = sqlite3SrcListAppend(pParse,0,&X,0);
700   if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);
701 }
702 fullname(A) ::= nm(X) DOT nm(Y). {
703   A = sqlite3SrcListAppend(pParse,0,&X,&Y);
704   if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);
705 }
706 
707 %type xfullname {SrcList*}
708 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
709 xfullname(A) ::= nm(X).
710    {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/}
711 xfullname(A) ::= nm(X) DOT nm(Y).
712    {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/}
713 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z).  {
714    A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/
715    if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
716 }
717 xfullname(A) ::= nm(X) AS nm(Z). {
718    A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/
719    if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
720 }
721 
722 %type joinop {int}
723 joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
724 joinop(X) ::= JOIN_KW(A) JOIN.
725                   {X = sqlite3JoinType(pParse,&A,0,0);  /*X-overwrites-A*/}
726 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
727                   {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
728 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
729                   {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
730 
731 // There is a parsing abiguity in an upsert statement that uses a
732 // SELECT on the RHS of a the INSERT:
733 //
734 //      INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
735 //                                        here ----^^
736 //
737 // When the ON token is encountered, the parser does not know if it is
738 // the beginning of an ON CONFLICT clause, or the beginning of an ON
739 // clause associated with the JOIN.  The conflict is resolved in favor
740 // of the JOIN.  If an ON CONFLICT clause is intended, insert a dummy
741 // WHERE clause in between, like this:
742 //
743 //      INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
744 //
745 // The [AND] and [OR] precedence marks in the rules for on_opt cause the
746 // ON in this context to always be interpreted as belonging to the JOIN.
747 //
748 %type on_opt {Expr*}
749 %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
750 on_opt(N) ::= ON expr(E).  {N = E;}
751 on_opt(N) ::= .     [OR]   {N = 0;}
752 
753 // Note that this block abuses the Token type just a little. If there is
754 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
755 // there is an INDEXED BY clause, then the token is populated as per normal,
756 // with z pointing to the token data and n containing the number of bytes
757 // in the token.
758 //
759 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
760 // normally illegal. The sqlite3SrcListIndexedBy() function
761 // recognizes and interprets this as a special case.
762 //
763 %type indexed_opt {Token}
764 indexed_opt(A) ::= .                 {A.z=0; A.n=0;}
765 indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
766 indexed_opt(A) ::= NOT INDEXED.      {A.z=0; A.n=1;}
767 
768 %type using_opt {IdList*}
769 %destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
770 using_opt(U) ::= USING LP idlist(L) RP.  {U = L;}
771 using_opt(U) ::= .                        {U = 0;}
772 
773 
774 %type orderby_opt {ExprList*}
775 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
776 
777 // the sortlist non-terminal stores a list of expression where each
778 // expression is optionally followed by ASC or DESC to indicate the
779 // sort order.
780 //
781 %type sortlist {ExprList*}
782 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
783 
784 orderby_opt(A) ::= .                          {A = 0;}
785 orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
786 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
787   A = sqlite3ExprListAppend(pParse,A,Y);
788   sqlite3ExprListSetSortOrder(A,Z,X);
789 }
790 sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
791   A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
792   sqlite3ExprListSetSortOrder(A,Z,X);
793 }
794 
795 %type sortorder {int}
796 
797 sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
798 sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
799 sortorder(A) ::= .              {A = SQLITE_SO_UNDEFINED;}
800 
801 %type nulls {int}
802 nulls(A) ::= NULLS FIRST.       {A = SQLITE_SO_ASC;}
803 nulls(A) ::= NULLS LAST.        {A = SQLITE_SO_DESC;}
804 nulls(A) ::= .                  {A = SQLITE_SO_UNDEFINED;}
805 
806 %type groupby_opt {ExprList*}
807 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
808 groupby_opt(A) ::= .                      {A = 0;}
809 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
810 
811 %type having_opt {Expr*}
812 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
813 having_opt(A) ::= .                {A = 0;}
814 having_opt(A) ::= HAVING expr(X).  {A = X;}
815 
816 %type limit_opt {Expr*}
817 
818 // The destructor for limit_opt will never fire in the current grammar.
819 // The limit_opt non-terminal only occurs at the end of a single production
820 // rule for SELECT statements.  As soon as the rule that create the
821 // limit_opt non-terminal reduces, the SELECT statement rule will also
822 // reduce.  So there is never a limit_opt non-terminal on the stack
823 // except as a transient.  So there is never anything to destroy.
824 //
825 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
826 limit_opt(A) ::= .       {A = 0;}
827 limit_opt(A) ::= LIMIT expr(X).
828                          {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
829 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
830                          {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
831 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
832                          {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
833 
834 /////////////////////////// The DELETE statement /////////////////////////////
835 //
836 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
837 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W)
838         orderby_opt(O) limit_opt(L). {
839   sqlite3SrcListIndexedBy(pParse, X, &I);
840 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
841   sqlite3ExprListDelete(pParse->db, O); O = 0;
842   sqlite3ExprDelete(pParse->db, L); L = 0;
843 #endif
844   sqlite3DeleteFrom(pParse,X,W,O,L);
845 }
846 %endif
847 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
848 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W). {
849   sqlite3SrcListIndexedBy(pParse, X, &I);
850   sqlite3DeleteFrom(pParse,X,W,0,0);
851 }
852 %endif
853 
854 %type where_opt {Expr*}
855 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
856 
857 where_opt(A) ::= .                    {A = 0;}
858 where_opt(A) ::= WHERE expr(X).       {A = X;}
859 
860 ////////////////////////// The UPDATE command ////////////////////////////////
861 //
862 %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
863 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
864         where_opt(W) orderby_opt(O) limit_opt(L).  {
865   sqlite3SrcListIndexedBy(pParse, X, &I);
866   sqlite3ExprListCheckLength(pParse,Y,"set list");
867   sqlite3Update(pParse,X,Y,W,R,O,L,0);
868 }
869 %endif
870 %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
871 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
872         where_opt(W).  {
873   sqlite3SrcListIndexedBy(pParse, X, &I);
874   sqlite3ExprListCheckLength(pParse,Y,"set list");
875   sqlite3Update(pParse,X,Y,W,R,0,0,0);
876 }
877 %endif
878 
879 %type setlist {ExprList*}
880 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
881 
882 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
883   A = sqlite3ExprListAppend(pParse, A, Y);
884   sqlite3ExprListSetName(pParse, A, &X, 1);
885 }
886 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
887   A = sqlite3ExprListAppendVector(pParse, A, X, Y);
888 }
889 setlist(A) ::= nm(X) EQ expr(Y). {
890   A = sqlite3ExprListAppend(pParse, 0, Y);
891   sqlite3ExprListSetName(pParse, A, &X, 1);
892 }
893 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
894   A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
895 }
896 
897 ////////////////////////// The INSERT command /////////////////////////////////
898 //
899 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
900         upsert(U). {
901   sqlite3Insert(pParse, X, S, F, R, U);
902 }
903 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES.
904 {
905   sqlite3Insert(pParse, X, 0, F, R, 0);
906 }
907 
908 %type upsert {Upsert*}
909 
910 // Because upsert only occurs at the tip end of the INSERT rule for cmd,
911 // there is never a case where the value of the upsert pointer will not
912 // be destroyed by the cmd action.  So comment-out the destructor to
913 // avoid unreachable code.
914 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
915 upsert(A) ::= . { A = 0; }
916 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
917               DO UPDATE SET setlist(Z) where_opt(W).
918               { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W);}
919 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING.
920               { A = sqlite3UpsertNew(pParse->db,T,TW,0,0); }
921 upsert(A) ::= ON CONFLICT DO NOTHING.
922               { A = sqlite3UpsertNew(pParse->db,0,0,0,0); }
923 
924 %type insert_cmd {int}
925 insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
926 insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
927 
928 %type idlist_opt {IdList*}
929 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
930 %type idlist {IdList*}
931 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
932 
933 idlist_opt(A) ::= .                       {A = 0;}
934 idlist_opt(A) ::= LP idlist(X) RP.    {A = X;}
935 idlist(A) ::= idlist(A) COMMA nm(Y).
936     {A = sqlite3IdListAppend(pParse,A,&Y);}
937 idlist(A) ::= nm(Y).
938     {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}
939 
940 /////////////////////////// Expression Processing /////////////////////////////
941 //
942 
943 %type expr {Expr*}
944 %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
945 %type term {Expr*}
946 %destructor term {sqlite3ExprDelete(pParse->db, $$);}
947 
948 %include {
949 
950   /* Construct a new Expr object from a single identifier.  Use the
951   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
952   ** that created the expression.
953   */
954   static Expr *tokenExpr(Parse *pParse, int op, Token t){
955     Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
956     if( p ){
957       /* memset(p, 0, sizeof(Expr)); */
958       p->op = (u8)op;
959       p->affExpr = 0;
960       p->flags = EP_Leaf;
961       p->iAgg = -1;
962       p->pLeft = p->pRight = 0;
963       p->x.pList = 0;
964       p->pAggInfo = 0;
965       p->y.pTab = 0;
966       p->op2 = 0;
967       p->iTable = 0;
968       p->iColumn = 0;
969       p->u.zToken = (char*)&p[1];
970       memcpy(p->u.zToken, t.z, t.n);
971       p->u.zToken[t.n] = 0;
972       if( sqlite3Isquote(p->u.zToken[0]) ){
973         sqlite3DequoteExpr(p);
974       }
975 #if SQLITE_MAX_EXPR_DEPTH>0
976       p->nHeight = 1;
977 #endif
978       if( IN_RENAME_OBJECT ){
979         return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
980       }
981     }
982     return p;
983   }
984 
985 }
986 
987 expr(A) ::= term(A).
988 expr(A) ::= LP expr(X) RP. {A = X;}
989 expr(A) ::= id(X).          {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
990 expr(A) ::= JOIN_KW(X).     {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
991 expr(A) ::= nm(X) DOT nm(Y). {
992   Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
993   Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
994   if( IN_RENAME_OBJECT ){
995     sqlite3RenameTokenMap(pParse, (void*)temp2, &Y);
996     sqlite3RenameTokenMap(pParse, (void*)temp1, &X);
997   }
998   A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
999 }
1000 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
1001   Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
1002   Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
1003   Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
1004   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
1005   if( IN_RENAME_OBJECT ){
1006     sqlite3RenameTokenMap(pParse, (void*)temp3, &Z);
1007     sqlite3RenameTokenMap(pParse, (void*)temp2, &Y);
1008   }
1009   A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
1010 }
1011 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1012 term(A) ::= STRING(X).          {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1013 term(A) ::= INTEGER(X). {
1014   A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
1015 }
1016 expr(A) ::= VARIABLE(X).     {
1017   if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
1018     u32 n = X.n;
1019     A = tokenExpr(pParse, TK_VARIABLE, X);
1020     sqlite3ExprAssignVarNumber(pParse, A, n);
1021   }else{
1022     /* When doing a nested parse, one can include terms in an expression
1023     ** that look like this:   #1 #2 ...  These terms refer to registers
1024     ** in the virtual machine.  #N is the N-th register. */
1025     Token t = X; /*A-overwrites-X*/
1026     assert( t.n>=2 );
1027     if( pParse->nested==0 ){
1028       sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
1029       A = 0;
1030     }else{
1031       A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1032       if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
1033     }
1034   }
1035 }
1036 expr(A) ::= expr(A) COLLATE ids(C). {
1037   A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
1038 }
1039 %ifndef SQLITE_OMIT_CAST
1040 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1041   A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1042   sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
1043 }
1044 %endif  SQLITE_OMIT_CAST
1045 
1046 
1047 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. {
1048   A = sqlite3ExprFunction(pParse, Y, &X, D);
1049 }
1050 expr(A) ::= id(X) LP STAR RP. {
1051   A = sqlite3ExprFunction(pParse, 0, &X, 0);
1052 }
1053 
1054 %ifndef SQLITE_OMIT_WINDOWFUNC
1055 expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
1056   A = sqlite3ExprFunction(pParse, Y, &X, D);
1057   sqlite3WindowAttach(pParse, A, Z);
1058 }
1059 expr(A) ::= id(X) LP STAR RP filter_over(Z). {
1060   A = sqlite3ExprFunction(pParse, 0, &X, 0);
1061   sqlite3WindowAttach(pParse, A, Z);
1062 }
1063 %endif
1064 
1065 term(A) ::= CTIME_KW(OP). {
1066   A = sqlite3ExprFunction(pParse, 0, &OP, 0);
1067 }
1068 
1069 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1070   ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1071   A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1072   if( A ){
1073     A->x.pList = pList;
1074   }else{
1075     sqlite3ExprListDelete(pParse->db, pList);
1076   }
1077 }
1078 
1079 expr(A) ::= expr(A) AND expr(Y).        {A=sqlite3ExprAnd(pParse,A,Y);}
1080 expr(A) ::= expr(A) OR(OP) expr(Y).     {A=sqlite3PExpr(pParse,@OP,A,Y);}
1081 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
1082                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
1083 expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {A=sqlite3PExpr(pParse,@OP,A,Y);}
1084 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
1085                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
1086 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
1087                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
1088 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
1089                                         {A=sqlite3PExpr(pParse,@OP,A,Y);}
1090 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1091 %type likeop {Token}
1092 likeop(A) ::= LIKE_KW|MATCH(A).
1093 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
1094 expr(A) ::= expr(A) likeop(OP) expr(Y).  [LIKE_KW]  {
1095   ExprList *pList;
1096   int bNot = OP.n & 0x80000000;
1097   OP.n &= 0x7fffffff;
1098   pList = sqlite3ExprListAppend(pParse,0, Y);
1099   pList = sqlite3ExprListAppend(pParse,pList, A);
1100   A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1101   if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1102   if( A ) A->flags |= EP_InfixFunc;
1103 }
1104 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E).  [LIKE_KW]  {
1105   ExprList *pList;
1106   int bNot = OP.n & 0x80000000;
1107   OP.n &= 0x7fffffff;
1108   pList = sqlite3ExprListAppend(pParse,0, Y);
1109   pList = sqlite3ExprListAppend(pParse,pList, A);
1110   pList = sqlite3ExprListAppend(pParse,pList, E);
1111   A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1112   if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1113   if( A ) A->flags |= EP_InfixFunc;
1114 }
1115 
1116 expr(A) ::= expr(A) ISNULL|NOTNULL(E).   {A = sqlite3PExpr(pParse,@E,A,0);}
1117 expr(A) ::= expr(A) NOT NULL.    {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
1118 
1119 %include {
1120   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1121   ** unary TK_ISNULL or TK_NOTNULL expression. */
1122   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1123     sqlite3 *db = pParse->db;
1124     if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
1125       pA->op = (u8)op;
1126       sqlite3ExprDelete(db, pA->pRight);
1127       pA->pRight = 0;
1128     }
1129   }
1130 }
1131 
1132 //    expr1 IS expr2
1133 //    expr1 IS NOT expr2
1134 //
1135 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL.  If expr2
1136 // is any other expression, code as TK_IS or TK_ISNOT.
1137 //
1138 expr(A) ::= expr(A) IS expr(Y).     {
1139   A = sqlite3PExpr(pParse,TK_IS,A,Y);
1140   binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1141 }
1142 expr(A) ::= expr(A) IS NOT expr(Y). {
1143   A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1144   binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1145 }
1146 
1147 expr(A) ::= NOT(B) expr(X).
1148               {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1149 expr(A) ::= BITNOT(B) expr(X).
1150               {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1151 expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1152   A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
1153   /*A-overwrites-B*/
1154 }
1155 
1156 %type between_op {int}
1157 between_op(A) ::= BETWEEN.     {A = 0;}
1158 between_op(A) ::= NOT BETWEEN. {A = 1;}
1159 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1160   ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1161   pList = sqlite3ExprListAppend(pParse,pList, Y);
1162   A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1163   if( A ){
1164     A->x.pList = pList;
1165   }else{
1166     sqlite3ExprListDelete(pParse->db, pList);
1167   }
1168   if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1169 }
1170 %ifndef SQLITE_OMIT_SUBQUERY
1171   %type in_op {int}
1172   in_op(A) ::= IN.      {A = 0;}
1173   in_op(A) ::= NOT IN.  {A = 1;}
1174   expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
1175     if( Y==0 ){
1176       /* Expressions of the form
1177       **
1178       **      expr1 IN ()
1179       **      expr1 NOT IN ()
1180       **
1181       ** simplify to constants 0 (false) and 1 (true), respectively,
1182       ** regardless of the value of expr1.
1183       */
1184       sqlite3ExprUnmapAndDelete(pParse, A);
1185       A = sqlite3Expr(pParse->db, TK_INTEGER, N ? "1" : "0");
1186     }else{
1187       A = sqlite3PExpr(pParse, TK_IN, A, 0);
1188       if( A ){
1189         A->x.pList = Y;
1190         sqlite3ExprSetHeightAndFlags(pParse, A);
1191       }else{
1192         sqlite3ExprListDelete(pParse->db, Y);
1193       }
1194       if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1195     }
1196   }
1197   expr(A) ::= LP select(X) RP. {
1198     A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1199     sqlite3PExprAddSelect(pParse, A, X);
1200   }
1201   expr(A) ::= expr(A) in_op(N) LP select(Y) RP.  [IN] {
1202     A = sqlite3PExpr(pParse, TK_IN, A, 0);
1203     sqlite3PExprAddSelect(pParse, A, Y);
1204     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1205   }
1206   expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1207     SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
1208     Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
1209     if( E )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1210     A = sqlite3PExpr(pParse, TK_IN, A, 0);
1211     sqlite3PExprAddSelect(pParse, A, pSelect);
1212     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1213   }
1214   expr(A) ::= EXISTS LP select(Y) RP. {
1215     Expr *p;
1216     p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
1217     sqlite3PExprAddSelect(pParse, p, Y);
1218   }
1219 %endif SQLITE_OMIT_SUBQUERY
1220 
1221 /* CASE expressions */
1222 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1223   A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1224   if( A ){
1225     A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1226     sqlite3ExprSetHeightAndFlags(pParse, A);
1227   }else{
1228     sqlite3ExprListDelete(pParse->db, Y);
1229     sqlite3ExprDelete(pParse->db, Z);
1230   }
1231 }
1232 %type case_exprlist {ExprList*}
1233 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1234 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1235   A = sqlite3ExprListAppend(pParse,A, Y);
1236   A = sqlite3ExprListAppend(pParse,A, Z);
1237 }
1238 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1239   A = sqlite3ExprListAppend(pParse,0, Y);
1240   A = sqlite3ExprListAppend(pParse,A, Z);
1241 }
1242 %type case_else {Expr*}
1243 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1244 case_else(A) ::=  ELSE expr(X).         {A = X;}
1245 case_else(A) ::=  .                     {A = 0;}
1246 %type case_operand {Expr*}
1247 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1248 case_operand(A) ::= expr(X).            {A = X; /*A-overwrites-X*/}
1249 case_operand(A) ::= .                   {A = 0;}
1250 
1251 %type exprlist {ExprList*}
1252 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1253 %type nexprlist {ExprList*}
1254 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1255 
1256 exprlist(A) ::= nexprlist(A).
1257 exprlist(A) ::= .                            {A = 0;}
1258 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1259     {A = sqlite3ExprListAppend(pParse,A,Y);}
1260 nexprlist(A) ::= expr(Y).
1261     {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
1262 
1263 %ifndef SQLITE_OMIT_SUBQUERY
1264 /* A paren_exprlist is an optional expression list contained inside
1265 ** of parenthesis */
1266 %type paren_exprlist {ExprList*}
1267 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1268 paren_exprlist(A) ::= .   {A = 0;}
1269 paren_exprlist(A) ::= LP exprlist(X) RP.  {A = X;}
1270 %endif SQLITE_OMIT_SUBQUERY
1271 
1272 
1273 ///////////////////////////// The CREATE INDEX command ///////////////////////
1274 //
1275 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1276         ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1277   sqlite3CreateIndex(pParse, &X, &D,
1278                      sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
1279                       &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1280   if( IN_RENAME_OBJECT && pParse->pNewIndex ){
1281     sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
1282   }
1283 }
1284 
1285 %type uniqueflag {int}
1286 uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
1287 uniqueflag(A) ::= .        {A = OE_None;}
1288 
1289 
1290 // The eidlist non-terminal (Expression Id List) generates an ExprList
1291 // from a list of identifiers.  The identifier names are in ExprList.a[].zName.
1292 // This list is stored in an ExprList rather than an IdList so that it
1293 // can be easily sent to sqlite3ColumnsExprList().
1294 //
1295 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1296 // used for the arguments to an index.  That is just an historical accident.
1297 //
1298 // IMPORTANT COMPATIBILITY NOTE:  Some prior versions of SQLite accepted
1299 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1300 // places - places that might have been stored in the sqlite_master schema.
1301 // Those extra features were ignored.  But because they might be in some
1302 // (busted) old databases, we need to continue parsing them when loading
1303 // historical schemas.
1304 //
1305 %type eidlist {ExprList*}
1306 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1307 %type eidlist_opt {ExprList*}
1308 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1309 
1310 %include {
1311   /* Add a single new term to an ExprList that is used to store a
1312   ** list of identifiers.  Report an error if the ID list contains
1313   ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1314   ** error while parsing a legacy schema.
1315   */
1316   static ExprList *parserAddExprIdListTerm(
1317     Parse *pParse,
1318     ExprList *pPrior,
1319     Token *pIdToken,
1320     int hasCollate,
1321     int sortOrder
1322   ){
1323     ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1324     if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1325         && pParse->db->init.busy==0
1326     ){
1327       sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1328                          pIdToken->n, pIdToken->z);
1329     }
1330     sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1331     return p;
1332   }
1333 } // end %include
1334 
1335 eidlist_opt(A) ::= .                         {A = 0;}
1336 eidlist_opt(A) ::= LP eidlist(X) RP.         {A = X;}
1337 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z).  {
1338   A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1339 }
1340 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1341   A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1342 }
1343 
1344 %type collate {int}
1345 collate(C) ::= .              {C = 0;}
1346 collate(C) ::= COLLATE ids.   {C = 1;}
1347 
1348 
1349 ///////////////////////////// The DROP INDEX command /////////////////////////
1350 //
1351 cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
1352 
1353 ///////////////////////////// The VACUUM command /////////////////////////////
1354 //
1355 %ifndef SQLITE_OMIT_VACUUM
1356 %ifndef SQLITE_OMIT_ATTACH
1357 %type vinto {Expr*}
1358 %destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
1359 cmd ::= VACUUM vinto(Y).                {sqlite3Vacuum(pParse,0,Y);}
1360 cmd ::= VACUUM nm(X) vinto(Y).          {sqlite3Vacuum(pParse,&X,Y);}
1361 vinto(A) ::= INTO expr(X).              {A = X;}
1362 vinto(A) ::= .                          {A = 0;}
1363 %endif  SQLITE_OMIT_ATTACH
1364 %endif  SQLITE_OMIT_VACUUM
1365 
1366 ///////////////////////////// The PRAGMA command /////////////////////////////
1367 //
1368 %ifndef SQLITE_OMIT_PRAGMA
1369 cmd ::= PRAGMA nm(X) dbnm(Z).                {sqlite3Pragma(pParse,&X,&Z,0,0);}
1370 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).    {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1371 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1372 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1373                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1374 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1375                                              {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1376 
1377 nmnum(A) ::= plus_num(A).
1378 nmnum(A) ::= nm(A).
1379 nmnum(A) ::= ON(A).
1380 nmnum(A) ::= DELETE(A).
1381 nmnum(A) ::= DEFAULT(A).
1382 %endif SQLITE_OMIT_PRAGMA
1383 %token_class number INTEGER|FLOAT.
1384 plus_num(A) ::= PLUS number(X).       {A = X;}
1385 plus_num(A) ::= number(A).
1386 minus_num(A) ::= MINUS number(X).     {A = X;}
1387 //////////////////////////// The CREATE TRIGGER command /////////////////////
1388 
1389 %ifndef SQLITE_OMIT_TRIGGER
1390 
1391 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1392   Token all;
1393   all.z = A.z;
1394   all.n = (int)(Z.z - A.z) + Z.n;
1395   sqlite3FinishTrigger(pParse, S, &all);
1396 }
1397 
1398 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1399                     trigger_time(C) trigger_event(D)
1400                     ON fullname(E) foreach_clause when_clause(G). {
1401   sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1402   A = (Z.n==0?B:Z); /*A-overwrites-T*/
1403 }
1404 
1405 %type trigger_time {int}
1406 trigger_time(A) ::= BEFORE|AFTER(X).  { A = @X; /*A-overwrites-X*/ }
1407 trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
1408 trigger_time(A) ::= .            { A = TK_BEFORE; }
1409 
1410 %type trigger_event {struct TrigEvent}
1411 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1412 trigger_event(A) ::= DELETE|INSERT(X).   {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1413 trigger_event(A) ::= UPDATE(X).          {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1414 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1415 
1416 foreach_clause ::= .
1417 foreach_clause ::= FOR EACH ROW.
1418 
1419 %type when_clause {Expr*}
1420 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1421 when_clause(A) ::= .             { A = 0; }
1422 when_clause(A) ::= WHEN expr(X). { A = X; }
1423 
1424 %type trigger_cmd_list {TriggerStep*}
1425 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1426 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1427   assert( A!=0 );
1428   A->pLast->pNext = X;
1429   A->pLast = X;
1430 }
1431 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1432   assert( A!=0 );
1433   A->pLast = A;
1434 }
1435 
1436 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1437 // within a trigger.  The table to INSERT, UPDATE, or DELETE is always in
1438 // the same database as the table that the trigger fires on.
1439 //
1440 %type trnm {Token}
1441 trnm(A) ::= nm(A).
1442 trnm(A) ::= nm DOT nm(X). {
1443   A = X;
1444   sqlite3ErrorMsg(pParse,
1445         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1446         "statements within triggers");
1447 }
1448 
1449 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1450 // statements within triggers.  We make a specific error message for this
1451 // since it is an exception to the default grammar rules.
1452 //
1453 tridxby ::= .
1454 tridxby ::= INDEXED BY nm. {
1455   sqlite3ErrorMsg(pParse,
1456         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1457         "within triggers");
1458 }
1459 tridxby ::= NOT INDEXED. {
1460   sqlite3ErrorMsg(pParse,
1461         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1462         "within triggers");
1463 }
1464 
1465 
1466 
1467 %type trigger_cmd {TriggerStep*}
1468 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1469 // UPDATE
1470 trigger_cmd(A) ::=
1471    UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z) scanpt(E).
1472    {A = sqlite3TriggerUpdateStep(pParse, &X, Y, Z, R, B.z, E);}
1473 
1474 // INSERT
1475 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1476                       trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
1477    A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
1478 }
1479 // DELETE
1480 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1481    {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
1482 
1483 // SELECT
1484 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1485    {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
1486 
1487 // The special RAISE expression that may occur in trigger programs
1488 expr(A) ::= RAISE LP IGNORE RP.  {
1489   A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1490   if( A ){
1491     A->affExpr = OE_Ignore;
1492   }
1493 }
1494 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP.  {
1495   A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1496   if( A ) {
1497     A->affExpr = (char)T;
1498   }
1499 }
1500 %endif  !SQLITE_OMIT_TRIGGER
1501 
1502 %type raisetype {int}
1503 raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
1504 raisetype(A) ::= ABORT.     {A = OE_Abort;}
1505 raisetype(A) ::= FAIL.      {A = OE_Fail;}
1506 
1507 
1508 ////////////////////////  DROP TRIGGER statement //////////////////////////////
1509 %ifndef SQLITE_OMIT_TRIGGER
1510 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1511   sqlite3DropTrigger(pParse,X,NOERR);
1512 }
1513 %endif  !SQLITE_OMIT_TRIGGER
1514 
1515 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1516 %ifndef SQLITE_OMIT_ATTACH
1517 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1518   sqlite3Attach(pParse, F, D, K);
1519 }
1520 cmd ::= DETACH database_kw_opt expr(D). {
1521   sqlite3Detach(pParse, D);
1522 }
1523 
1524 %type key_opt {Expr*}
1525 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1526 key_opt(A) ::= .                     { A = 0; }
1527 key_opt(A) ::= KEY expr(X).          { A = X; }
1528 
1529 database_kw_opt ::= DATABASE.
1530 database_kw_opt ::= .
1531 %endif SQLITE_OMIT_ATTACH
1532 
1533 ////////////////////////// REINDEX collation //////////////////////////////////
1534 %ifndef SQLITE_OMIT_REINDEX
1535 cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
1536 cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
1537 %endif  SQLITE_OMIT_REINDEX
1538 
1539 /////////////////////////////////// ANALYZE ///////////////////////////////////
1540 %ifndef SQLITE_OMIT_ANALYZE
1541 cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
1542 cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
1543 %endif
1544 
1545 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1546 %ifndef SQLITE_OMIT_ALTERTABLE
1547 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1548   sqlite3AlterRenameTable(pParse,X,&Z);
1549 }
1550 cmd ::= ALTER TABLE add_column_fullname
1551         ADD kwcolumn_opt columnname(Y) carglist. {
1552   Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1553   sqlite3AlterFinishAddColumn(pParse, &Y);
1554 }
1555 add_column_fullname ::= fullname(X). {
1556   disableLookaside(pParse);
1557   sqlite3AlterBeginAddColumn(pParse, X);
1558 }
1559 cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
1560   sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
1561 }
1562 
1563 kwcolumn_opt ::= .
1564 kwcolumn_opt ::= COLUMNKW.
1565 
1566 %endif  SQLITE_OMIT_ALTERTABLE
1567 
1568 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1569 %ifndef SQLITE_OMIT_VIRTUALTABLE
1570 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
1571 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
1572 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1573                 nm(X) dbnm(Y) USING nm(Z). {
1574     sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1575 }
1576 vtabarglist ::= vtabarg.
1577 vtabarglist ::= vtabarglist COMMA vtabarg.
1578 vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
1579 vtabarg ::= vtabarg vtabargtoken.
1580 vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
1581 vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
1582 lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
1583 anylist ::= .
1584 anylist ::= anylist LP anylist RP.
1585 anylist ::= anylist ANY.
1586 %endif  SQLITE_OMIT_VIRTUALTABLE
1587 
1588 
1589 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1590 %type wqlist {With*}
1591 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1592 
1593 with ::= .
1594 %ifndef SQLITE_OMIT_CTE
1595 with ::= WITH wqlist(W).              { sqlite3WithPush(pParse, W, 1); }
1596 with ::= WITH RECURSIVE wqlist(W).    { sqlite3WithPush(pParse, W, 1); }
1597 
1598 wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1599   A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
1600 }
1601 wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1602   A = sqlite3WithAdd(pParse, A, &X, Y, Z);
1603 }
1604 %endif  SQLITE_OMIT_CTE
1605 
1606 //////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
1607 // These must be at the end of this file. Specifically, the rules that
1608 // introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1609 // the integer values assigned to these tokens to be larger than all other
1610 // tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
1611 //
1612 %ifndef SQLITE_OMIT_WINDOWFUNC
1613 %type windowdefn_list {Window*}
1614 %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
1615 windowdefn_list(A) ::= windowdefn(Z). { A = Z; }
1616 windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
1617   assert( Z!=0 );
1618   sqlite3WindowChain(pParse, Z, Y);
1619   Z->pNextWin = Y;
1620   A = Z;
1621 }
1622 
1623 %type windowdefn {Window*}
1624 %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
1625 windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
1626   if( ALWAYS(Y) ){
1627     Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1628   }
1629   A = Y;
1630 }
1631 
1632 %type window {Window*}
1633 %destructor window {sqlite3WindowDelete(pParse->db, $$);}
1634 
1635 %type frame_opt {Window*}
1636 %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1637 
1638 %type part_opt {ExprList*}
1639 %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1640 
1641 %type filter_clause {Expr*}
1642 %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
1643 
1644 %type over_clause {Window*}
1645 %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
1646 
1647 %type filter_over {Window*}
1648 %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
1649 
1650 %type range_or_rows {int}
1651 
1652 %type frame_bound {struct FrameBound}
1653 %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1654 %type frame_bound_s {struct FrameBound}
1655 %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1656 %type frame_bound_e {struct FrameBound}
1657 %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1658 
1659 window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1660   A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
1661 }
1662 window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1663   A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
1664 }
1665 window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
1666   A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
1667 }
1668 window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
1669   A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
1670 }
1671 window(A) ::= frame_opt(Z). {
1672   A = Z;
1673 }
1674 window(A) ::= nm(W) frame_opt(Z). {
1675   A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
1676 }
1677 
1678 frame_opt(A) ::= .                             {
1679   A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
1680 }
1681 frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). {
1682   A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
1683 }
1684 frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
1685                           frame_bound_e(Z) frame_exclude_opt(W). {
1686   A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
1687 }
1688 
1689 range_or_rows(A) ::= RANGE|ROWS|GROUPS(X).   {A = @X; /*A-overwrites-X*/}
1690 
1691 frame_bound_s(A) ::= frame_bound(X).         {A = X;}
1692 frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
1693 frame_bound_e(A) ::= frame_bound(X).         {A = X;}
1694 frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
1695 
1696 frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
1697                                              {A.eType = @Y; A.pExpr = X;}
1698 frame_bound(A) ::= CURRENT(X) ROW.           {A.eType = @X; A.pExpr = 0;}
1699 
1700 %type frame_exclude_opt {u8}
1701 frame_exclude_opt(A) ::= . {A = 0;}
1702 frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
1703 
1704 %type frame_exclude {u8}
1705 frame_exclude(A) ::= NO(X) OTHERS.   {A = @X; /*A-overwrites-X*/}
1706 frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
1707 frame_exclude(A) ::= GROUP|TIES(X).  {A = @X; /*A-overwrites-X*/}
1708 
1709 
1710 %type window_clause {Window*}
1711 %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
1712 window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
1713 
1714 filter_over(A) ::= filter_clause(F) over_clause(O). {
1715   O->pFilter = F;
1716   A = O;
1717 }
1718 filter_over(A) ::= over_clause(O). {
1719   A = O;
1720 }
1721 filter_over(A) ::= filter_clause(F). {
1722   A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1723   if( A ){
1724     A->eFrmType = TK_FILTER;
1725     A->pFilter = F;
1726   }else{
1727     sqlite3ExprDelete(pParse->db, F);
1728   }
1729 }
1730 
1731 over_clause(A) ::= OVER LP window(Z) RP. {
1732   A = Z;
1733   assert( A!=0 );
1734 }
1735 over_clause(A) ::= OVER nm(Z). {
1736   A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1737   if( A ){
1738     A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
1739   }
1740 }
1741 
1742 filter_clause(A) ::= FILTER LP WHERE expr(X) RP.  { A = X; }
1743 %endif /* SQLITE_OMIT_WINDOWFUNC */
1744 
1745 /*
1746 ** The code generator needs some extra TK_ token values for tokens that
1747 ** are synthesized and do not actually appear in the grammar:
1748 */
1749 %token
1750   COLUMN          /* Reference to a table column */
1751   AGG_FUNCTION    /* An aggregate function */
1752   AGG_COLUMN      /* An aggregated column */
1753   TRUEFALSE       /* True or false keyword */
1754   ISNOT           /* Combination of IS and NOT */
1755   FUNCTION        /* A function invocation */
1756   UMINUS          /* Unary minus */
1757   UPLUS           /* Unary plus */
1758   TRUTH           /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
1759   REGISTER        /* Reference to a VDBE register */
1760   VECTOR          /* Vector */
1761   SELECT_COLUMN   /* Choose a single column from a multi-column SELECT */
1762   IF_NULL_ROW     /* the if-null-row operator */
1763   ASTERISK        /* The "*" in count(*) and similar */
1764   SPAN            /* The span operator */
1765 .
1766 /* There must be no more than 255 tokens defined above.  If this grammar
1767 ** is extended with new rules and tokens, they must either be so few in
1768 ** number that TK_SPAN is no more than 255, or else the new tokens must
1769 ** appear after this line.
1770 */
1771 %include {
1772 #if TK_SPAN>255
1773 # error too many tokens in the grammar
1774 #endif
1775 }
1776 
1777 /*
1778 ** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens.  The
1779 ** parser depends on this.  Those tokens are not used in any grammar rule.
1780 ** They are only used by the tokenizer.  Declare them last so that they
1781 ** are guaranteed to be the last two tokens
1782 */
1783 %token SPACE ILLEGAL.
1784