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