Experimental changes that prevent parser stack overflows by growing the
[sqlite.git] / src / parse.y
blob0e41f54cf179eee93386a256e5bc8b720415ef13
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 // Function used to enlarge the parser stack, if needed
25 %realloc sqlite3_realloc64
26 %free sqlite3_free
28 // All token codes are small integers with #defines that begin with "TK_"
29 %token_prefix TK_
31 // The type of the data attached to each token is Token. This is also the
32 // default type for non-terminals.
34 %token_type {Token}
35 %default_type {Token}
37 // An extra argument to the constructor for the parser, which is available
38 // to all actions.
39 %extra_context {Parse *pParse}
41 // This code runs whenever there is a syntax error
43 %syntax_error {
44 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
45 if( TOKEN.z[0] ){
46 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
47 }else{
48 sqlite3ErrorMsg(pParse, "incomplete input");
51 %stack_overflow {
52 sqlite3ErrorMsg(pParse, "parser stack overflow");
55 // The name of the generated procedure that implements the parser
56 // is as follows:
57 %name sqlite3Parser
59 // The following text is included near the beginning of the C source
60 // code file that implements the parser.
62 %include {
63 #include "sqliteInt.h"
66 ** Disable all error recovery processing in the parser push-down
67 ** automaton.
69 #define YYNOERRORRECOVERY 1
72 ** Make yytestcase() the same as testcase()
74 #define yytestcase(X) testcase(X)
77 ** Indicate that sqlite3ParserFree() will never be called with a null
78 ** pointer.
80 #define YYPARSEFREENEVERNULL 1
83 ** In the amalgamation, the parse.c file generated by lemon and the
84 ** tokenize.c file are concatenated. In that case, sqlite3RunParser()
85 ** has access to the the size of the yyParser object and so the parser
86 ** engine can be allocated from stack. In that case, only the
87 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
88 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
89 ** omitted.
91 #ifdef SQLITE_AMALGAMATION
92 # define sqlite3Parser_ENGINEALWAYSONSTACK 1
93 #endif
96 ** Alternative datatype for the argument to the malloc() routine passed
97 ** into sqlite3ParserAlloc(). The default is size_t.
99 #define YYMALLOCARGTYPE u64
102 ** An instance of the following structure describes the event of a
103 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
104 ** TK_DELETE, or TK_INSTEAD. If the event is of the form
106 ** UPDATE ON (a,b,c)
108 ** Then the "b" IdList records the list "a,b,c".
110 struct TrigEvent { int a; IdList * b; };
112 struct FrameBound { int eType; Expr *pExpr; };
115 ** Disable lookaside memory allocation for objects that might be
116 ** shared across database connections.
118 static void disableLookaside(Parse *pParse){
119 sqlite3 *db = pParse->db;
120 pParse->disableLookaside++;
121 DisableLookaside;
124 #if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \
125 && defined(SQLITE_UDL_CAPABLE_PARSER)
127 ** Issue an error message if an ORDER BY or LIMIT clause occurs on an
128 ** UPDATE or DELETE statement.
130 static void updateDeleteLimitError(
131 Parse *pParse,
132 ExprList *pOrderBy,
133 Expr *pLimit
135 if( pOrderBy ){
136 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
137 }else{
138 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
140 sqlite3ExprListDelete(pParse->db, pOrderBy);
141 sqlite3ExprDelete(pParse->db, pLimit);
143 #endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
145 } // end %include
147 // Input is a single SQL command
148 input ::= cmdlist.
149 cmdlist ::= cmdlist ecmd.
150 cmdlist ::= ecmd.
151 ecmd ::= SEMI.
152 ecmd ::= cmdx SEMI.
153 %ifndef SQLITE_OMIT_EXPLAIN
154 ecmd ::= explain cmdx SEMI. {NEVER-REDUCE}
155 explain ::= EXPLAIN. { if( pParse->pReprepare==0 ) pParse->explain = 1; }
156 explain ::= EXPLAIN QUERY PLAN. { if( pParse->pReprepare==0 ) pParse->explain = 2; }
157 %endif SQLITE_OMIT_EXPLAIN
158 cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
160 ///////////////////// Begin and end transactions. ////////////////////////////
163 cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
164 trans_opt ::= .
165 trans_opt ::= TRANSACTION.
166 trans_opt ::= TRANSACTION nm.
167 %type transtype {int}
168 transtype(A) ::= . {A = TK_DEFERRED;}
169 transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/}
170 transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
171 transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
172 cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
173 cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
175 savepoint_opt ::= SAVEPOINT.
176 savepoint_opt ::= .
177 cmd ::= SAVEPOINT nm(X). {
178 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
180 cmd ::= RELEASE savepoint_opt nm(X). {
181 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
183 cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
184 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
187 ///////////////////// The CREATE TABLE statement ////////////////////////////
189 cmd ::= create_table create_table_args.
190 create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
191 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
193 createkw(A) ::= CREATE(A). {disableLookaside(pParse);}
195 %type ifnotexists {int}
196 ifnotexists(A) ::= . {A = 0;}
197 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
198 %type temp {int}
199 %ifndef SQLITE_OMIT_TEMPDB
200 temp(A) ::= TEMP. {A = pParse->db->init.busy==0;}
201 %endif SQLITE_OMIT_TEMPDB
202 temp(A) ::= . {A = 0;}
203 create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_option_set(F). {
204 sqlite3EndTable(pParse,&X,&E,F,0);
206 create_table_args ::= AS select(S). {
207 sqlite3EndTable(pParse,0,0,0,S);
208 sqlite3SelectDelete(pParse->db, S);
210 %type table_option_set {u32}
211 %type table_option {u32}
212 table_option_set(A) ::= . {A = 0;}
213 table_option_set(A) ::= table_option(A).
214 table_option_set(A) ::= table_option_set(X) COMMA table_option(Y). {A = X|Y;}
215 table_option(A) ::= WITHOUT nm(X). {
216 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
217 A = TF_WithoutRowid | TF_NoVisibleRowid;
218 }else{
219 A = 0;
220 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
223 table_option(A) ::= nm(X). {
224 if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){
225 A = TF_Strict;
226 }else{
227 A = 0;
228 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
231 columnlist ::= columnlist COMMA columnname carglist.
232 columnlist ::= columnname carglist.
233 columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);}
235 // Declare some tokens early in order to influence their values, to
236 // improve performance and reduce the executable size. The goal here is
237 // to get the "jump" operations in ISNULL through ESCAPE to have numeric
238 // values that are early enough so that all jump operations are clustered
239 // at the beginning.
241 %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
242 %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
243 %token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
244 %token GT LE LT GE ESCAPE.
246 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
247 // fallback to ID if they will not parse as their original value.
248 // This obviates the need for the "id" nonterminal.
250 %fallback ID
251 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
252 CONFLICT DATABASE DEFERRED DESC DETACH DO
253 EACH END EXCLUSIVE EXPLAIN FAIL FOR
254 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
255 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
256 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
257 NULLS FIRST LAST
258 %ifdef SQLITE_OMIT_COMPOUND_SELECT
259 EXCEPT INTERSECT UNION
260 %endif SQLITE_OMIT_COMPOUND_SELECT
261 %ifndef SQLITE_OMIT_WINDOWFUNC
262 CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED
263 EXCLUDE GROUPS OTHERS TIES
264 %endif SQLITE_OMIT_WINDOWFUNC
265 %ifndef SQLITE_OMIT_GENERATED_COLUMNS
266 GENERATED ALWAYS
267 %endif
268 MATERIALIZED
269 REINDEX RENAME CTIME_KW IF
271 %wildcard ANY.
273 // Define operator precedence early so that this is the first occurrence
274 // of the operator tokens in the grammar. Keeping the operators together
275 // causes them to be assigned integer values that are close together,
276 // which keeps parser tables smaller.
278 // The token values assigned to these symbols is determined by the order
279 // in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
280 // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
281 // the sqlite3ExprIfFalse() routine for additional information on this
282 // constraint.
284 %left OR.
285 %left AND.
286 %right NOT.
287 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
288 %left GT LE LT GE.
289 %right ESCAPE.
290 %left BITAND BITOR LSHIFT RSHIFT.
291 %left PLUS MINUS.
292 %left STAR SLASH REM.
293 %left CONCAT PTR.
294 %left COLLATE.
295 %right BITNOT.
296 %nonassoc ON.
298 // An IDENTIFIER can be a generic identifier, or one of several
299 // keywords. Any non-standard keyword can also be an identifier.
301 %token_class id ID|INDEXED.
303 // And "ids" is an identifer-or-string.
305 %token_class ids ID|STRING.
307 // An identifier or a join-keyword
309 %token_class idj ID|INDEXED|JOIN_KW.
311 // The name of a column or table can be any of the following:
313 %type nm {Token}
314 nm(A) ::= idj(A).
315 nm(A) ::= STRING(A).
317 // A typetoken is really zero or more tokens that form a type name such
318 // as can be found after the column name in a CREATE TABLE statement.
319 // Multiple tokens are concatenated to form the value of the typetoken.
321 %type typetoken {Token}
322 typetoken(A) ::= . {A.n = 0; A.z = 0;}
323 typetoken(A) ::= typename(A).
324 typetoken(A) ::= typename(A) LP signed RP(Y). {
325 A.n = (int)(&Y.z[Y.n] - A.z);
327 typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
328 A.n = (int)(&Y.z[Y.n] - A.z);
330 %type typename {Token}
331 typename(A) ::= ids(A).
332 typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
333 signed ::= plus_num.
334 signed ::= minus_num.
336 // The scanpt non-terminal takes a value which is a pointer to the
337 // input text just past the last token that has been shifted into
338 // the parser. By surrounding some phrase in the grammar with two
339 // scanpt non-terminals, we can capture the input text for that phrase.
340 // For example:
342 // something ::= .... scanpt(A) phrase scanpt(Z).
344 // The text that is parsed as "phrase" is a string starting at A
345 // and containing (int)(Z-A) characters. There might be some extra
346 // whitespace on either end of the text, but that can be removed in
347 // post-processing, if needed.
349 %type scanpt {const char*}
350 scanpt(A) ::= . {
351 assert( yyLookahead!=YYNOCODE );
352 A = yyLookaheadToken.z;
354 scantok(A) ::= . {
355 assert( yyLookahead!=YYNOCODE );
356 A = yyLookaheadToken;
359 // "carglist" is a list of additional constraints that come after the
360 // column name and column type in a CREATE TABLE statement.
362 carglist ::= carglist ccons.
363 carglist ::= .
364 ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
365 ccons ::= DEFAULT scantok(A) term(X).
366 {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);}
367 ccons ::= DEFAULT LP(A) expr(X) RP(Z).
368 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
369 ccons ::= DEFAULT PLUS(A) scantok(Z) term(X).
370 {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);}
371 ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). {
372 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
373 sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]);
375 ccons ::= DEFAULT scantok id(X). {
376 Expr *p = tokenExpr(pParse, TK_STRING, X);
377 if( p ){
378 sqlite3ExprIdToTrueFalse(p);
379 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
381 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
384 // In addition to the type name, we also care about the primary key and
385 // UNIQUE constraints.
387 ccons ::= NULL onconf.
388 ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
389 ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
390 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
391 ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
392 SQLITE_IDXTYPE_UNIQUE);}
393 ccons ::= CHECK LP(A) expr(X) RP(B). {sqlite3AddCheckConstraint(pParse,X,A.z,B.z);}
394 ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
395 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
396 ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
397 ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);}
398 ccons ::= GENERATED ALWAYS AS generated.
399 ccons ::= AS generated.
400 generated ::= LP expr(E) RP. {sqlite3AddGenerated(pParse,E,0);}
401 generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);}
403 // The optional AUTOINCREMENT keyword
404 %type autoinc {int}
405 autoinc(X) ::= . {X = 0;}
406 autoinc(X) ::= AUTOINCR. {X = 1;}
408 // The next group of rules parses the arguments to a REFERENCES clause
409 // that determine if the referential integrity checking is deferred or
410 // or immediate and which determine what action to take if a ref-integ
411 // check fails.
413 %type refargs {int}
414 refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */}
415 refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
416 %type refarg {struct {int value; int mask;}}
417 refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
418 refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; }
419 refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
420 refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
421 %type refact {int}
422 refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */}
423 refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */}
424 refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */}
425 refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */}
426 refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */}
427 %type defer_subclause {int}
428 defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;}
429 defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
430 %type init_deferred_pred_opt {int}
431 init_deferred_pred_opt(A) ::= . {A = 0;}
432 init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
433 init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
435 conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
436 conslist_opt(A) ::= COMMA(A) conslist.
437 conslist ::= conslist tconscomma tcons.
438 conslist ::= tcons.
439 tconscomma ::= COMMA. {pParse->constraintName.n = 0;}
440 tconscomma ::= .
441 tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
442 tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
443 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
444 tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
445 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
446 SQLITE_IDXTYPE_UNIQUE);}
447 tcons ::= CHECK LP(A) expr(E) RP(B) onconf.
448 {sqlite3AddCheckConstraint(pParse,E,A.z,B.z);}
449 tcons ::= FOREIGN KEY LP eidlist(FA) RP
450 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
451 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
452 sqlite3DeferForeignKey(pParse, D);
454 %type defer_subclause_opt {int}
455 defer_subclause_opt(A) ::= . {A = 0;}
456 defer_subclause_opt(A) ::= defer_subclause(A).
458 // The following is a non-standard extension that allows us to declare the
459 // default behavior when there is a constraint conflict.
461 %type onconf {int}
462 %type orconf {int}
463 %type resolvetype {int}
464 onconf(A) ::= . {A = OE_Default;}
465 onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
466 orconf(A) ::= . {A = OE_Default;}
467 orconf(A) ::= OR resolvetype(X). {A = X;}
468 resolvetype(A) ::= raisetype(A).
469 resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
470 resolvetype(A) ::= REPLACE. {A = OE_Replace;}
472 ////////////////////////// The DROP TABLE /////////////////////////////////////
474 cmd ::= DROP TABLE ifexists(E) fullname(X). {
475 sqlite3DropTable(pParse, X, 0, E);
477 %type ifexists {int}
478 ifexists(A) ::= IF EXISTS. {A = 1;}
479 ifexists(A) ::= . {A = 0;}
481 ///////////////////// The CREATE VIEW statement /////////////////////////////
483 %ifndef SQLITE_OMIT_VIEW
484 cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
485 AS select(S). {
486 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
488 cmd ::= DROP VIEW ifexists(E) fullname(X). {
489 sqlite3DropTable(pParse, X, 1, E);
491 %endif SQLITE_OMIT_VIEW
493 //////////////////////// The SELECT statement /////////////////////////////////
495 cmd ::= select(X). {
496 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
497 sqlite3Select(pParse, X, &dest);
498 sqlite3SelectDelete(pParse->db, X);
501 %type select {Select*}
502 %destructor select {sqlite3SelectDelete(pParse->db, $$);}
503 %type selectnowith {Select*}
504 %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
505 %type oneselect {Select*}
506 %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
508 %include {
510 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
511 ** all elements in the list. And make sure list length does not exceed
512 ** SQLITE_LIMIT_COMPOUND_SELECT.
514 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
515 assert( p!=0 );
516 if( p->pPrior ){
517 Select *pNext = 0, *pLoop = p;
518 int mxSelect, cnt = 1;
519 while(1){
520 pLoop->pNext = pNext;
521 pLoop->selFlags |= SF_Compound;
522 pNext = pLoop;
523 pLoop = pLoop->pPrior;
524 if( pLoop==0 ) break;
525 cnt++;
526 if( pLoop->pOrderBy || pLoop->pLimit ){
527 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
528 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
529 sqlite3SelectOpName(pNext->op));
530 break;
533 if( (p->selFlags & SF_MultiValue)==0 &&
534 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
535 cnt>mxSelect
537 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
542 /* Attach a With object describing the WITH clause to a Select
543 ** object describing the query for which the WITH clause is a prefix.
545 static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){
546 if( pSelect ){
547 pSelect->pWith = pWith;
548 parserDoubleLinkSelect(pParse, pSelect);
549 }else{
550 sqlite3WithDelete(pParse->db, pWith);
552 return pSelect;
556 %ifndef SQLITE_OMIT_CTE
557 select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);}
558 select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X).
559 {A = attachWithToSelect(pParse,X,W);}
560 %endif /* SQLITE_OMIT_CTE */
561 select(A) ::= selectnowith(A). {
562 Select *p = A;
563 if( p ){
564 parserDoubleLinkSelect(pParse, p);
568 selectnowith(A) ::= oneselect(A).
569 %ifndef SQLITE_OMIT_COMPOUND_SELECT
570 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
571 Select *pRhs = Z;
572 Select *pLhs = A;
573 if( pRhs && pRhs->pPrior ){
574 SrcList *pFrom;
575 Token x;
576 x.n = 0;
577 parserDoubleLinkSelect(pParse, pRhs);
578 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0);
579 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
581 if( pRhs ){
582 pRhs->op = (u8)Y;
583 pRhs->pPrior = pLhs;
584 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
585 pRhs->selFlags &= ~SF_MultiValue;
586 if( Y!=TK_ALL ) pParse->hasCompound = 1;
587 }else{
588 sqlite3SelectDelete(pParse->db, pLhs);
590 A = pRhs;
592 %type multiselect_op {int}
593 multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
594 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
595 multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
596 %endif SQLITE_OMIT_COMPOUND_SELECT
598 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
599 groupby_opt(P) having_opt(Q)
600 orderby_opt(Z) limit_opt(L). {
601 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
603 %ifndef SQLITE_OMIT_WINDOWFUNC
604 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
605 groupby_opt(P) having_opt(Q) window_clause(R)
606 orderby_opt(Z) limit_opt(L). {
607 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
608 if( A ){
609 A->pWinDefn = R;
610 }else{
611 sqlite3WindowListDelete(pParse->db, R);
614 %endif
617 oneselect(A) ::= values(A).
619 %type values {Select*}
620 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
621 values(A) ::= VALUES LP nexprlist(X) RP. {
622 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
624 values(A) ::= values(A) COMMA LP nexprlist(Y) RP. {
625 Select *pRight, *pLeft = A;
626 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
627 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
628 if( pRight ){
629 pRight->op = TK_ALL;
630 pRight->pPrior = pLeft;
631 A = pRight;
632 }else{
633 A = pLeft;
637 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
638 // present and false (0) if it is not.
640 %type distinct {int}
641 distinct(A) ::= DISTINCT. {A = SF_Distinct;}
642 distinct(A) ::= ALL. {A = SF_All;}
643 distinct(A) ::= . {A = 0;}
645 // selcollist is a list of expressions that are to become the return
646 // values of the SELECT statement. The "*" in statements like
647 // "SELECT * FROM ..." is encoded as a special expression with an
648 // opcode of TK_ASTERISK.
650 %type selcollist {ExprList*}
651 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
652 %type sclp {ExprList*}
653 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
654 sclp(A) ::= selcollist(A) COMMA.
655 sclp(A) ::= . {A = 0;}
656 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
657 A = sqlite3ExprListAppend(pParse, A, X);
658 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
659 sqlite3ExprListSetSpan(pParse,A,B,Z);
661 selcollist(A) ::= sclp(A) scanpt STAR(X). {
662 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
663 sqlite3ExprSetErrorOffset(p, (int)(X.z - pParse->zTail));
664 A = sqlite3ExprListAppend(pParse, A, p);
666 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR(Y). {
667 Expr *pRight, *pLeft, *pDot;
668 pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
669 sqlite3ExprSetErrorOffset(pRight, (int)(Y.z - pParse->zTail));
670 pLeft = tokenExpr(pParse, TK_ID, X);
671 pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
672 A = sqlite3ExprListAppend(pParse,A, pDot);
675 // An option "AS <id>" phrase that can follow one of the expressions that
676 // define the result set, or one of the tables in the FROM clause.
678 %type as {Token}
679 as(X) ::= AS nm(Y). {X = Y;}
680 as(X) ::= ids(X).
681 as(X) ::= . {X.n = 0; X.z = 0;}
684 %type seltablist {SrcList*}
685 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
686 %type stl_prefix {SrcList*}
687 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
688 %type from {SrcList*}
689 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
691 // A complete FROM clause.
693 from(A) ::= . {A = 0;}
694 from(A) ::= FROM seltablist(X). {
695 A = X;
696 sqlite3SrcListShiftJoinType(pParse,A);
699 // "seltablist" is a "Select Table List" - the content of the FROM clause
700 // in a SELECT statement. "stl_prefix" is a prefix of this list.
702 stl_prefix(A) ::= seltablist(A) joinop(Y). {
703 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
705 stl_prefix(A) ::= . {A = 0;}
706 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) on_using(N). {
707 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
709 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_by(I) on_using(N). {
710 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
711 sqlite3SrcListIndexedBy(pParse, A, &I);
713 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) on_using(N). {
714 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
715 sqlite3SrcListFuncArgs(pParse, A, E);
717 %ifndef SQLITE_OMIT_SUBQUERY
718 seltablist(A) ::= stl_prefix(A) LP select(S) RP as(Z) on_using(N). {
719 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,&N);
721 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP as(Z) on_using(N). {
722 if( A==0 && Z.n==0 && N.pOn==0 && N.pUsing==0 ){
723 A = F;
724 }else if( ALWAYS(F!=0) && F->nSrc==1 ){
725 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N);
726 if( A ){
727 SrcItem *pNew = &A->a[A->nSrc-1];
728 SrcItem *pOld = F->a;
729 pNew->zName = pOld->zName;
730 pNew->zDatabase = pOld->zDatabase;
731 pNew->pSelect = pOld->pSelect;
732 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
733 pNew->fg.isNestedFrom = 1;
735 if( pOld->fg.isTabFunc ){
736 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
737 pOld->u1.pFuncArg = 0;
738 pOld->fg.isTabFunc = 0;
739 pNew->fg.isTabFunc = 1;
741 pOld->zName = pOld->zDatabase = 0;
742 pOld->pSelect = 0;
744 sqlite3SrcListDelete(pParse->db, F);
745 }else{
746 Select *pSubquery;
747 sqlite3SrcListShiftJoinType(pParse,F);
748 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
749 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N);
752 %endif SQLITE_OMIT_SUBQUERY
754 %type dbnm {Token}
755 dbnm(A) ::= . {A.z=0; A.n=0;}
756 dbnm(A) ::= DOT nm(X). {A = X;}
758 %type fullname {SrcList*}
759 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
760 fullname(A) ::= nm(X). {
761 A = sqlite3SrcListAppend(pParse,0,&X,0);
762 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);
764 fullname(A) ::= nm(X) DOT nm(Y). {
765 A = sqlite3SrcListAppend(pParse,0,&X,&Y);
766 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);
769 %type xfullname {SrcList*}
770 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
771 xfullname(A) ::= nm(X).
772 {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/}
773 xfullname(A) ::= nm(X) DOT nm(Y).
774 {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/}
775 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
776 A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/
777 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
779 xfullname(A) ::= nm(X) AS nm(Z). {
780 A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/
781 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
784 %type joinop {int}
785 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
786 joinop(X) ::= JOIN_KW(A) JOIN.
787 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
788 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
789 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
790 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
791 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
793 // There is a parsing abiguity in an upsert statement that uses a
794 // SELECT on the RHS of a the INSERT:
796 // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
797 // here ----^^
799 // When the ON token is encountered, the parser does not know if it is
800 // the beginning of an ON CONFLICT clause, or the beginning of an ON
801 // clause associated with the JOIN. The conflict is resolved in favor
802 // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
803 // WHERE clause in between, like this:
805 // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
807 // The [AND] and [OR] precedence marks in the rules for on_using cause the
808 // ON in this context to always be interpreted as belonging to the JOIN.
810 %type on_using {OnOrUsing}
811 //%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);}
812 on_using(N) ::= ON expr(E). {N.pOn = E; N.pUsing = 0;}
813 on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;}
814 on_using(N) ::= . [OR] {N.pOn = 0; N.pUsing = 0;}
816 // Note that this block abuses the Token type just a little. If there is
817 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
818 // there is an INDEXED BY clause, then the token is populated as per normal,
819 // with z pointing to the token data and n containing the number of bytes
820 // in the token.
822 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
823 // normally illegal. The sqlite3SrcListIndexedBy() function
824 // recognizes and interprets this as a special case.
826 %type indexed_opt {Token}
827 %type indexed_by {Token}
828 indexed_opt(A) ::= . {A.z=0; A.n=0;}
829 indexed_opt(A) ::= indexed_by(A).
830 indexed_by(A) ::= INDEXED BY nm(X). {A = X;}
831 indexed_by(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
833 %type orderby_opt {ExprList*}
834 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
836 // the sortlist non-terminal stores a list of expression where each
837 // expression is optionally followed by ASC or DESC to indicate the
838 // sort order.
840 %type sortlist {ExprList*}
841 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
843 orderby_opt(A) ::= . {A = 0;}
844 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
845 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
846 A = sqlite3ExprListAppend(pParse,A,Y);
847 sqlite3ExprListSetSortOrder(A,Z,X);
849 sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
850 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
851 sqlite3ExprListSetSortOrder(A,Z,X);
854 %type sortorder {int}
856 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
857 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
858 sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
860 %type nulls {int}
861 nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;}
862 nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;}
863 nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;}
865 %type groupby_opt {ExprList*}
866 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
867 groupby_opt(A) ::= . {A = 0;}
868 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
870 %type having_opt {Expr*}
871 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
872 having_opt(A) ::= . {A = 0;}
873 having_opt(A) ::= HAVING expr(X). {A = X;}
875 %type limit_opt {Expr*}
877 // The destructor for limit_opt will never fire in the current grammar.
878 // The limit_opt non-terminal only occurs at the end of a single production
879 // rule for SELECT statements. As soon as the rule that create the
880 // limit_opt non-terminal reduces, the SELECT statement rule will also
881 // reduce. So there is never a limit_opt non-terminal on the stack
882 // except as a transient. So there is never anything to destroy.
884 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
885 limit_opt(A) ::= . {A = 0;}
886 limit_opt(A) ::= LIMIT expr(X).
887 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
888 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
889 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
890 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
891 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
893 /////////////////////////// The DELETE statement /////////////////////////////
895 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
896 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W)
897 orderby_opt(O) limit_opt(L). {
898 sqlite3SrcListIndexedBy(pParse, X, &I);
899 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
900 if( O || L ){
901 updateDeleteLimitError(pParse,O,L);
902 O = 0;
903 L = 0;
905 #endif
906 sqlite3DeleteFrom(pParse,X,W,O,L);
908 %else
909 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). {
910 sqlite3SrcListIndexedBy(pParse, X, &I);
911 sqlite3DeleteFrom(pParse,X,W,0,0);
913 %endif
915 %type where_opt {Expr*}
916 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
917 %type where_opt_ret {Expr*}
918 %destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);}
920 where_opt(A) ::= . {A = 0;}
921 where_opt(A) ::= WHERE expr(X). {A = X;}
922 where_opt_ret(A) ::= . {A = 0;}
923 where_opt_ret(A) ::= WHERE expr(X). {A = X;}
924 where_opt_ret(A) ::= RETURNING selcollist(X).
925 {sqlite3AddReturning(pParse,X); A = 0;}
926 where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y).
927 {sqlite3AddReturning(pParse,Y); A = X;}
929 ////////////////////////// The UPDATE command ////////////////////////////////
931 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
932 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
933 where_opt_ret(W) orderby_opt(O) limit_opt(L). {
934 sqlite3SrcListIndexedBy(pParse, X, &I);
935 if( F ){
936 SrcList *pFromClause = F;
937 if( pFromClause->nSrc>1 ){
938 Select *pSubquery;
939 Token as;
940 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
941 as.n = 0;
942 as.z = 0;
943 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
945 X = sqlite3SrcListAppendList(pParse, X, pFromClause);
947 sqlite3ExprListCheckLength(pParse,Y,"set list");
948 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
949 if( O || L ){
950 updateDeleteLimitError(pParse,O,L);
951 O = 0;
952 L = 0;
954 #endif
955 sqlite3Update(pParse,X,Y,W,R,O,L,0);
957 %else
958 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
959 where_opt_ret(W). {
960 sqlite3SrcListIndexedBy(pParse, X, &I);
961 sqlite3ExprListCheckLength(pParse,Y,"set list");
962 if( F ){
963 SrcList *pFromClause = F;
964 if( pFromClause->nSrc>1 ){
965 Select *pSubquery;
966 Token as;
967 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
968 as.n = 0;
969 as.z = 0;
970 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
972 X = sqlite3SrcListAppendList(pParse, X, pFromClause);
974 sqlite3Update(pParse,X,Y,W,R,0,0,0);
976 %endif
980 %type setlist {ExprList*}
981 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
983 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
984 A = sqlite3ExprListAppend(pParse, A, Y);
985 sqlite3ExprListSetName(pParse, A, &X, 1);
987 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
988 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
990 setlist(A) ::= nm(X) EQ expr(Y). {
991 A = sqlite3ExprListAppend(pParse, 0, Y);
992 sqlite3ExprListSetName(pParse, A, &X, 1);
994 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
995 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
998 ////////////////////////// The INSERT command /////////////////////////////////
1000 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
1001 upsert(U). {
1002 sqlite3Insert(pParse, X, S, F, R, U);
1004 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning.
1006 sqlite3Insert(pParse, X, 0, F, R, 0);
1009 %type upsert {Upsert*}
1011 // Because upsert only occurs at the tip end of the INSERT rule for cmd,
1012 // there is never a case where the value of the upsert pointer will not
1013 // be destroyed by the cmd action. So comment-out the destructor to
1014 // avoid unreachable code.
1015 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
1016 upsert(A) ::= . { A = 0; }
1017 upsert(A) ::= RETURNING selcollist(X). { A = 0; sqlite3AddReturning(pParse,X); }
1018 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
1019 DO UPDATE SET setlist(Z) where_opt(W) upsert(N).
1020 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);}
1021 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N).
1022 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); }
1023 upsert(A) ::= ON CONFLICT DO NOTHING returning.
1024 { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
1025 upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning.
1026 { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);}
1028 returning ::= RETURNING selcollist(X). {sqlite3AddReturning(pParse,X);}
1029 returning ::= .
1031 %type insert_cmd {int}
1032 insert_cmd(A) ::= INSERT orconf(R). {A = R;}
1033 insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
1035 %type idlist_opt {IdList*}
1036 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
1037 %type idlist {IdList*}
1038 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
1040 idlist_opt(A) ::= . {A = 0;}
1041 idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
1042 idlist(A) ::= idlist(A) COMMA nm(Y).
1043 {A = sqlite3IdListAppend(pParse,A,&Y);}
1044 idlist(A) ::= nm(Y).
1045 {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}
1047 /////////////////////////// Expression Processing /////////////////////////////
1050 %type expr {Expr*}
1051 %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
1052 %type term {Expr*}
1053 %destructor term {sqlite3ExprDelete(pParse->db, $$);}
1055 %include {
1057 /* Construct a new Expr object from a single token */
1058 static Expr *tokenExpr(Parse *pParse, int op, Token t){
1059 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
1060 if( p ){
1061 /* memset(p, 0, sizeof(Expr)); */
1062 p->op = (u8)op;
1063 p->affExpr = 0;
1064 p->flags = EP_Leaf;
1065 ExprClearVVAProperties(p);
1066 /* p->iAgg = -1; // Not required */
1067 p->pLeft = p->pRight = 0;
1068 p->pAggInfo = 0;
1069 memset(&p->x, 0, sizeof(p->x));
1070 memset(&p->y, 0, sizeof(p->y));
1071 p->op2 = 0;
1072 p->iTable = 0;
1073 p->iColumn = 0;
1074 p->u.zToken = (char*)&p[1];
1075 memcpy(p->u.zToken, t.z, t.n);
1076 p->u.zToken[t.n] = 0;
1077 p->w.iOfst = (int)(t.z - pParse->zTail);
1078 if( sqlite3Isquote(p->u.zToken[0]) ){
1079 sqlite3DequoteExpr(p);
1081 #if SQLITE_MAX_EXPR_DEPTH>0
1082 p->nHeight = 1;
1083 #endif
1084 if( IN_RENAME_OBJECT ){
1085 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
1088 return p;
1093 expr(A) ::= term(A).
1094 expr(A) ::= LP expr(X) RP. {A = X;}
1095 expr(A) ::= idj(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
1096 expr(A) ::= nm(X) DOT nm(Y). {
1097 Expr *temp1 = tokenExpr(pParse,TK_ID,X);
1098 Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
1099 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
1101 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
1102 Expr *temp1 = tokenExpr(pParse,TK_ID,X);
1103 Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
1104 Expr *temp3 = tokenExpr(pParse,TK_ID,Z);
1105 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
1106 if( IN_RENAME_OBJECT ){
1107 sqlite3RenameTokenRemap(pParse, 0, temp1);
1109 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
1111 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1112 term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1113 term(A) ::= INTEGER(X). {
1114 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
1115 if( A ) A->w.iOfst = (int)(X.z - pParse->zTail);
1117 expr(A) ::= VARIABLE(X). {
1118 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
1119 u32 n = X.n;
1120 A = tokenExpr(pParse, TK_VARIABLE, X);
1121 sqlite3ExprAssignVarNumber(pParse, A, n);
1122 }else{
1123 /* When doing a nested parse, one can include terms in an expression
1124 ** that look like this: #1 #2 ... These terms refer to registers
1125 ** in the virtual machine. #N is the N-th register. */
1126 Token t = X; /*A-overwrites-X*/
1127 assert( t.n>=2 );
1128 if( pParse->nested==0 ){
1129 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
1130 A = 0;
1131 }else{
1132 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1133 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
1137 expr(A) ::= expr(A) COLLATE ids(C). {
1138 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
1140 %ifndef SQLITE_OMIT_CAST
1141 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1142 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1143 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
1145 %endif SQLITE_OMIT_CAST
1148 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP. {
1149 A = sqlite3ExprFunction(pParse, Y, &X, D);
1151 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP. {
1152 A = sqlite3ExprFunction(pParse, Y, &X, D);
1153 sqlite3ExprAddFunctionOrderBy(pParse, A, O);
1155 expr(A) ::= idj(X) LP STAR RP. {
1156 A = sqlite3ExprFunction(pParse, 0, &X, 0);
1159 %ifndef SQLITE_OMIT_WINDOWFUNC
1160 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
1161 A = sqlite3ExprFunction(pParse, Y, &X, D);
1162 sqlite3WindowAttach(pParse, A, Z);
1164 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP filter_over(Z). {
1165 A = sqlite3ExprFunction(pParse, Y, &X, D);
1166 sqlite3WindowAttach(pParse, A, Z);
1167 sqlite3ExprAddFunctionOrderBy(pParse, A, O);
1169 expr(A) ::= idj(X) LP STAR RP filter_over(Z). {
1170 A = sqlite3ExprFunction(pParse, 0, &X, 0);
1171 sqlite3WindowAttach(pParse, A, Z);
1173 %endif
1175 term(A) ::= CTIME_KW(OP). {
1176 A = sqlite3ExprFunction(pParse, 0, &OP, 0);
1179 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1180 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1181 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1182 if( A ){
1183 A->x.pList = pList;
1184 if( ALWAYS(pList->nExpr) ){
1185 A->flags |= pList->a[0].pExpr->flags & EP_Propagate;
1187 }else{
1188 sqlite3ExprListDelete(pParse->db, pList);
1192 expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);}
1193 expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1194 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
1195 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1196 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1197 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
1198 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1199 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
1200 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1201 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
1202 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1203 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1204 %type likeop {Token}
1205 likeop(A) ::= LIKE_KW|MATCH(A).
1206 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
1207 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
1208 ExprList *pList;
1209 int bNot = OP.n & 0x80000000;
1210 OP.n &= 0x7fffffff;
1211 pList = sqlite3ExprListAppend(pParse,0, Y);
1212 pList = sqlite3ExprListAppend(pParse,pList, A);
1213 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1214 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1215 if( A ) A->flags |= EP_InfixFunc;
1217 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
1218 ExprList *pList;
1219 int bNot = OP.n & 0x80000000;
1220 OP.n &= 0x7fffffff;
1221 pList = sqlite3ExprListAppend(pParse,0, Y);
1222 pList = sqlite3ExprListAppend(pParse,pList, A);
1223 pList = sqlite3ExprListAppend(pParse,pList, E);
1224 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1225 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1226 if( A ) A->flags |= EP_InfixFunc;
1229 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1230 expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
1232 %include {
1233 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1234 ** unary TK_ISNULL or TK_NOTNULL expression. */
1235 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1236 sqlite3 *db = pParse->db;
1237 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
1238 pA->op = (u8)op;
1239 sqlite3ExprDelete(db, pA->pRight);
1240 pA->pRight = 0;
1245 // expr1 IS expr2
1246 // expr1 IS NOT expr2
1248 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1249 // is any other expression, code as TK_IS or TK_ISNOT.
1251 expr(A) ::= expr(A) IS expr(Y). {
1252 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1253 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1255 expr(A) ::= expr(A) IS NOT expr(Y). {
1256 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1257 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1259 expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y). {
1260 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1261 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1263 expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). {
1264 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1265 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1268 expr(A) ::= NOT(B) expr(X).
1269 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1270 expr(A) ::= BITNOT(B) expr(X).
1271 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1272 expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1273 A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
1274 /*A-overwrites-B*/
1277 expr(A) ::= expr(B) PTR(C) expr(D). {
1278 ExprList *pList = sqlite3ExprListAppend(pParse, 0, B);
1279 pList = sqlite3ExprListAppend(pParse, pList, D);
1280 A = sqlite3ExprFunction(pParse, pList, &C, 0);
1283 %type between_op {int}
1284 between_op(A) ::= BETWEEN. {A = 0;}
1285 between_op(A) ::= NOT BETWEEN. {A = 1;}
1286 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1287 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1288 pList = sqlite3ExprListAppend(pParse,pList, Y);
1289 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1290 if( A ){
1291 A->x.pList = pList;
1292 }else{
1293 sqlite3ExprListDelete(pParse->db, pList);
1295 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1297 %ifndef SQLITE_OMIT_SUBQUERY
1298 %type in_op {int}
1299 in_op(A) ::= IN. {A = 0;}
1300 in_op(A) ::= NOT IN. {A = 1;}
1301 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
1302 if( Y==0 ){
1303 /* Expressions of the form
1305 ** expr1 IN ()
1306 ** expr1 NOT IN ()
1308 ** simplify to constants 0 (false) and 1 (true), respectively,
1309 ** regardless of the value of expr1.
1311 sqlite3ExprUnmapAndDelete(pParse, A);
1312 A = sqlite3Expr(pParse->db, TK_STRING, N ? "true" : "false");
1313 if( A ) sqlite3ExprIdToTrueFalse(A);
1314 }else{
1315 Expr *pRHS = Y->a[0].pExpr;
1316 if( Y->nExpr==1 && sqlite3ExprIsConstant(pRHS) && A->op!=TK_VECTOR ){
1317 Y->a[0].pExpr = 0;
1318 sqlite3ExprListDelete(pParse->db, Y);
1319 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
1320 A = sqlite3PExpr(pParse, TK_EQ, A, pRHS);
1321 }else if( Y->nExpr==1 && pRHS->op==TK_SELECT ){
1322 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1323 sqlite3PExprAddSelect(pParse, A, pRHS->x.pSelect);
1324 pRHS->x.pSelect = 0;
1325 sqlite3ExprListDelete(pParse->db, Y);
1326 }else{
1327 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1328 if( A==0 ){
1329 sqlite3ExprListDelete(pParse->db, Y);
1330 }else if( A->pLeft->op==TK_VECTOR ){
1331 int nExpr = A->pLeft->x.pList->nExpr;
1332 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y);
1333 if( pSelectRHS ){
1334 parserDoubleLinkSelect(pParse, pSelectRHS);
1335 sqlite3PExprAddSelect(pParse, A, pSelectRHS);
1337 }else{
1338 A->x.pList = Y;
1339 sqlite3ExprSetHeightAndFlags(pParse, A);
1342 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1345 expr(A) ::= LP select(X) RP. {
1346 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1347 sqlite3PExprAddSelect(pParse, A, X);
1349 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1350 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1351 sqlite3PExprAddSelect(pParse, A, Y);
1352 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1354 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1355 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
1356 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
1357 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1358 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1359 sqlite3PExprAddSelect(pParse, A, pSelect);
1360 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1362 expr(A) ::= EXISTS LP select(Y) RP. {
1363 Expr *p;
1364 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
1365 sqlite3PExprAddSelect(pParse, p, Y);
1367 %endif SQLITE_OMIT_SUBQUERY
1369 /* CASE expressions */
1370 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1371 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1372 if( A ){
1373 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1374 sqlite3ExprSetHeightAndFlags(pParse, A);
1375 }else{
1376 sqlite3ExprListDelete(pParse->db, Y);
1377 sqlite3ExprDelete(pParse->db, Z);
1380 %type case_exprlist {ExprList*}
1381 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1382 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1383 A = sqlite3ExprListAppend(pParse,A, Y);
1384 A = sqlite3ExprListAppend(pParse,A, Z);
1386 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1387 A = sqlite3ExprListAppend(pParse,0, Y);
1388 A = sqlite3ExprListAppend(pParse,A, Z);
1390 %type case_else {Expr*}
1391 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1392 case_else(A) ::= ELSE expr(X). {A = X;}
1393 case_else(A) ::= . {A = 0;}
1394 %type case_operand {Expr*}
1395 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1396 case_operand(A) ::= expr(A).
1397 case_operand(A) ::= . {A = 0;}
1399 %type exprlist {ExprList*}
1400 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1401 %type nexprlist {ExprList*}
1402 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1404 exprlist(A) ::= nexprlist(A).
1405 exprlist(A) ::= . {A = 0;}
1406 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1407 {A = sqlite3ExprListAppend(pParse,A,Y);}
1408 nexprlist(A) ::= expr(Y).
1409 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
1411 %ifndef SQLITE_OMIT_SUBQUERY
1412 /* A paren_exprlist is an optional expression list contained inside
1413 ** of parenthesis */
1414 %type paren_exprlist {ExprList*}
1415 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1416 paren_exprlist(A) ::= . {A = 0;}
1417 paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
1418 %endif SQLITE_OMIT_SUBQUERY
1421 ///////////////////////////// The CREATE INDEX command ///////////////////////
1423 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1424 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1425 sqlite3CreateIndex(pParse, &X, &D,
1426 sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
1427 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1428 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
1429 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
1433 %type uniqueflag {int}
1434 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1435 uniqueflag(A) ::= . {A = OE_None;}
1438 // The eidlist non-terminal (Expression Id List) generates an ExprList
1439 // from a list of identifiers. The identifier names are in ExprList.a[].zName.
1440 // This list is stored in an ExprList rather than an IdList so that it
1441 // can be easily sent to sqlite3ColumnsExprList().
1443 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1444 // used for the arguments to an index. That is just an historical accident.
1446 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1447 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1448 // places - places that might have been stored in the sqlite_schema table.
1449 // Those extra features were ignored. But because they might be in some
1450 // (busted) old databases, we need to continue parsing them when loading
1451 // historical schemas.
1453 %type eidlist {ExprList*}
1454 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1455 %type eidlist_opt {ExprList*}
1456 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1458 %include {
1459 /* Add a single new term to an ExprList that is used to store a
1460 ** list of identifiers. Report an error if the ID list contains
1461 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1462 ** error while parsing a legacy schema.
1464 static ExprList *parserAddExprIdListTerm(
1465 Parse *pParse,
1466 ExprList *pPrior,
1467 Token *pIdToken,
1468 int hasCollate,
1469 int sortOrder
1471 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1472 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1473 && pParse->db->init.busy==0
1475 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1476 pIdToken->n, pIdToken->z);
1478 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1479 return p;
1481 } // end %include
1483 eidlist_opt(A) ::= . {A = 0;}
1484 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
1485 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1486 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1488 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1489 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1492 %type collate {int}
1493 collate(C) ::= . {C = 0;}
1494 collate(C) ::= COLLATE ids. {C = 1;}
1497 ///////////////////////////// The DROP INDEX command /////////////////////////
1499 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
1501 ///////////////////////////// The VACUUM command /////////////////////////////
1503 %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH
1504 %type vinto {Expr*}
1505 %destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
1506 cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);}
1507 cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);}
1508 vinto(A) ::= INTO expr(X). {A = X;}
1509 vinto(A) ::= . {A = 0;}
1510 %endif
1512 ///////////////////////////// The PRAGMA command /////////////////////////////
1514 %ifndef SQLITE_OMIT_PRAGMA
1515 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1516 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1517 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1518 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1519 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1520 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1521 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1523 nmnum(A) ::= plus_num(A).
1524 nmnum(A) ::= nm(A).
1525 nmnum(A) ::= ON(A).
1526 nmnum(A) ::= DELETE(A).
1527 nmnum(A) ::= DEFAULT(A).
1528 %endif SQLITE_OMIT_PRAGMA
1529 %token_class number INTEGER|FLOAT.
1530 plus_num(A) ::= PLUS number(X). {A = X;}
1531 plus_num(A) ::= number(A).
1532 minus_num(A) ::= MINUS number(X). {A = X;}
1533 //////////////////////////// The CREATE TRIGGER command /////////////////////
1535 %ifndef SQLITE_OMIT_TRIGGER
1537 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1538 Token all;
1539 all.z = A.z;
1540 all.n = (int)(Z.z - A.z) + Z.n;
1541 sqlite3FinishTrigger(pParse, S, &all);
1544 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1545 trigger_time(C) trigger_event(D)
1546 ON fullname(E) foreach_clause when_clause(G). {
1547 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1548 A = (Z.n==0?B:Z); /*A-overwrites-T*/
1551 %type trigger_time {int}
1552 trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
1553 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1554 trigger_time(A) ::= . { A = TK_BEFORE; }
1556 %type trigger_event {struct TrigEvent}
1557 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1558 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1559 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1560 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1562 foreach_clause ::= .
1563 foreach_clause ::= FOR EACH ROW.
1565 %type when_clause {Expr*}
1566 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1567 when_clause(A) ::= . { A = 0; }
1568 when_clause(A) ::= WHEN expr(X). { A = X; }
1570 %type trigger_cmd_list {TriggerStep*}
1571 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1572 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1573 assert( A!=0 );
1574 A->pLast->pNext = X;
1575 A->pLast = X;
1577 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1578 assert( A!=0 );
1579 A->pLast = A;
1582 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1583 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1584 // the same database as the table that the trigger fires on.
1586 %type trnm {Token}
1587 trnm(A) ::= nm(A).
1588 trnm(A) ::= nm DOT nm(X). {
1589 A = X;
1590 sqlite3ErrorMsg(pParse,
1591 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1592 "statements within triggers");
1595 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1596 // statements within triggers. We make a specific error message for this
1597 // since it is an exception to the default grammar rules.
1599 tridxby ::= .
1600 tridxby ::= INDEXED BY nm. {
1601 sqlite3ErrorMsg(pParse,
1602 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1603 "within triggers");
1605 tridxby ::= NOT INDEXED. {
1606 sqlite3ErrorMsg(pParse,
1607 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1608 "within triggers");
1613 %type trigger_cmd {TriggerStep*}
1614 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1615 // UPDATE
1616 trigger_cmd(A) ::=
1617 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E).
1618 {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);}
1620 // INSERT
1621 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1622 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
1623 A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
1625 // DELETE
1626 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1627 {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
1629 // SELECT
1630 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1631 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
1633 // The special RAISE expression that may occur in trigger programs
1634 expr(A) ::= RAISE LP IGNORE RP. {
1635 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1636 if( A ){
1637 A->affExpr = OE_Ignore;
1640 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1641 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1642 if( A ) {
1643 A->affExpr = (char)T;
1646 %endif !SQLITE_OMIT_TRIGGER
1648 %type raisetype {int}
1649 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1650 raisetype(A) ::= ABORT. {A = OE_Abort;}
1651 raisetype(A) ::= FAIL. {A = OE_Fail;}
1654 //////////////////////// DROP TRIGGER statement //////////////////////////////
1655 %ifndef SQLITE_OMIT_TRIGGER
1656 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1657 sqlite3DropTrigger(pParse,X,NOERR);
1659 %endif !SQLITE_OMIT_TRIGGER
1661 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1662 %ifndef SQLITE_OMIT_ATTACH
1663 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1664 sqlite3Attach(pParse, F, D, K);
1666 cmd ::= DETACH database_kw_opt expr(D). {
1667 sqlite3Detach(pParse, D);
1670 %type key_opt {Expr*}
1671 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1672 key_opt(A) ::= . { A = 0; }
1673 key_opt(A) ::= KEY expr(X). { A = X; }
1675 database_kw_opt ::= DATABASE.
1676 database_kw_opt ::= .
1677 %endif SQLITE_OMIT_ATTACH
1679 ////////////////////////// REINDEX collation //////////////////////////////////
1680 %ifndef SQLITE_OMIT_REINDEX
1681 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1682 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
1683 %endif SQLITE_OMIT_REINDEX
1685 /////////////////////////////////// ANALYZE ///////////////////////////////////
1686 %ifndef SQLITE_OMIT_ANALYZE
1687 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1688 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1689 %endif
1691 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1692 %ifndef SQLITE_OMIT_ALTERTABLE
1693 %ifndef SQLITE_OMIT_VIRTUALTABLE
1694 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1695 sqlite3AlterRenameTable(pParse,X,&Z);
1697 cmd ::= ALTER TABLE add_column_fullname
1698 ADD kwcolumn_opt columnname(Y) carglist. {
1699 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1700 sqlite3AlterFinishAddColumn(pParse, &Y);
1702 cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). {
1703 sqlite3AlterDropColumn(pParse, X, &Y);
1706 add_column_fullname ::= fullname(X). {
1707 disableLookaside(pParse);
1708 sqlite3AlterBeginAddColumn(pParse, X);
1710 cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
1711 sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
1714 kwcolumn_opt ::= .
1715 kwcolumn_opt ::= COLUMNKW.
1717 %endif SQLITE_OMIT_VIRTUALTABLE
1718 %endif SQLITE_OMIT_ALTERTABLE
1720 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1721 %ifndef SQLITE_OMIT_VIRTUALTABLE
1722 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1723 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
1724 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1725 nm(X) dbnm(Y) USING nm(Z). {
1726 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1728 vtabarglist ::= vtabarg.
1729 vtabarglist ::= vtabarglist COMMA vtabarg.
1730 vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1731 vtabarg ::= vtabarg vtabargtoken.
1732 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1733 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1734 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1735 anylist ::= .
1736 anylist ::= anylist LP anylist RP.
1737 anylist ::= anylist ANY.
1738 %endif SQLITE_OMIT_VIRTUALTABLE
1741 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1742 %type wqlist {With*}
1743 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1744 %type wqitem {Cte*}
1745 // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable
1747 with ::= .
1748 %ifndef SQLITE_OMIT_CTE
1749 with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1750 with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1752 %type wqas {u8}
1753 wqas(A) ::= AS. {A = M10d_Any;}
1754 wqas(A) ::= AS MATERIALIZED. {A = M10d_Yes;}
1755 wqas(A) ::= AS NOT MATERIALIZED. {A = M10d_No;}
1756 wqitem(A) ::= nm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. {
1757 A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/
1759 wqlist(A) ::= wqitem(X). {
1760 A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/
1762 wqlist(A) ::= wqlist(A) COMMA wqitem(X). {
1763 A = sqlite3WithAdd(pParse, A, X);
1765 %endif SQLITE_OMIT_CTE
1767 //////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
1768 // These must be at the end of this file. Specifically, the rules that
1769 // introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1770 // the integer values assigned to these tokens to be larger than all other
1771 // tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
1773 %ifndef SQLITE_OMIT_WINDOWFUNC
1774 %type windowdefn_list {Window*}
1775 %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
1776 windowdefn_list(A) ::= windowdefn(A).
1777 windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
1778 assert( Z!=0 );
1779 sqlite3WindowChain(pParse, Z, Y);
1780 Z->pNextWin = Y;
1781 A = Z;
1784 %type windowdefn {Window*}
1785 %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
1786 windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
1787 if( ALWAYS(Y) ){
1788 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1790 A = Y;
1793 %type window {Window*}
1794 %destructor window {sqlite3WindowDelete(pParse->db, $$);}
1796 %type frame_opt {Window*}
1797 %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1799 %type part_opt {ExprList*}
1800 %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1802 %type filter_clause {Expr*}
1803 %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
1805 %type over_clause {Window*}
1806 %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
1808 %type filter_over {Window*}
1809 %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
1811 %type range_or_rows {int}
1813 %type frame_bound {struct FrameBound}
1814 %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1815 %type frame_bound_s {struct FrameBound}
1816 %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1817 %type frame_bound_e {struct FrameBound}
1818 %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1820 window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1821 A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
1823 window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1824 A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
1826 window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
1827 A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
1829 window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
1830 A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
1832 window(A) ::= frame_opt(A).
1833 window(A) ::= nm(W) frame_opt(Z). {
1834 A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
1837 frame_opt(A) ::= . {
1838 A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
1840 frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). {
1841 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
1843 frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
1844 frame_bound_e(Z) frame_exclude_opt(W). {
1845 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
1848 range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/}
1850 frame_bound_s(A) ::= frame_bound(X). {A = X;}
1851 frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
1852 frame_bound_e(A) ::= frame_bound(X). {A = X;}
1853 frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
1855 frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
1856 {A.eType = @Y; A.pExpr = X;}
1857 frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;}
1859 %type frame_exclude_opt {u8}
1860 frame_exclude_opt(A) ::= . {A = 0;}
1861 frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
1863 %type frame_exclude {u8}
1864 frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/}
1865 frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
1866 frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/}
1869 %type window_clause {Window*}
1870 %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
1871 window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
1873 filter_over(A) ::= filter_clause(F) over_clause(O). {
1874 if( O ){
1875 O->pFilter = F;
1876 }else{
1877 sqlite3ExprDelete(pParse->db, F);
1879 A = O;
1881 filter_over(A) ::= over_clause(O). {
1882 A = O;
1884 filter_over(A) ::= filter_clause(F). {
1885 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1886 if( A ){
1887 A->eFrmType = TK_FILTER;
1888 A->pFilter = F;
1889 }else{
1890 sqlite3ExprDelete(pParse->db, F);
1894 over_clause(A) ::= OVER LP window(Z) RP. {
1895 A = Z;
1896 assert( A!=0 );
1898 over_clause(A) ::= OVER nm(Z). {
1899 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1900 if( A ){
1901 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
1905 filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
1906 %endif /* SQLITE_OMIT_WINDOWFUNC */
1909 ** The code generator needs some extra TK_ token values for tokens that
1910 ** are synthesized and do not actually appear in the grammar:
1912 %token
1913 COLUMN /* Reference to a table column */
1914 AGG_FUNCTION /* An aggregate function */
1915 AGG_COLUMN /* An aggregated column */
1916 TRUEFALSE /* True or false keyword */
1917 ISNOT /* Combination of IS and NOT */
1918 FUNCTION /* A function invocation */
1919 UMINUS /* Unary minus */
1920 UPLUS /* Unary plus */
1921 TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
1922 REGISTER /* Reference to a VDBE register */
1923 VECTOR /* Vector */
1924 SELECT_COLUMN /* Choose a single column from a multi-column SELECT */
1925 IF_NULL_ROW /* the if-null-row operator */
1926 ASTERISK /* The "*" in count(*) and similar */
1927 SPAN /* The span operator */
1928 ERROR /* An expression containing an error */
1930 /* There must be no more than 255 tokens defined above. If this grammar
1931 ** is extended with new rules and tokens, they must either be so few in
1932 ** number that TK_SPAN is no more than 255, or else the new tokens must
1933 ** appear after this line.
1935 %include {
1936 #if TK_SPAN>255
1937 # error too many tokens in the grammar
1938 #endif
1942 ** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens. The
1943 ** parser depends on this. Those tokens are not used in any grammar rule.
1944 ** They are only used by the tokenizer. Declare them last so that they
1945 ** are guaranteed to be the last two tokens
1947 %token SPACE ILLEGAL.