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