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