Add tests for the new code on this branch.
[sqlite.git] / src / parse.y
blob12621b434871f2b6e50661aebf0b1a06172a353c
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 parserStackRealloc
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 sqlite3OomFault(pParse->db);
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;
555 /* Memory allocator for parser stack resizing. This is a thin wrapper around
556 ** sqlite3_realloc() that includes a call to sqlite3FaultSim() to facilitate
557 ** testing.
559 static void *parserStackRealloc(void *pOld, sqlite3_uint64 newSize){
560 return sqlite3FaultSim(700) ? 0 : sqlite3_realloc(pOld, newSize);
564 %ifndef SQLITE_OMIT_CTE
565 select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);}
566 select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X).
567 {A = attachWithToSelect(pParse,X,W);}
568 %endif /* SQLITE_OMIT_CTE */
569 select(A) ::= selectnowith(A). {
570 Select *p = A;
571 if( p ){
572 parserDoubleLinkSelect(pParse, p);
576 selectnowith(A) ::= oneselect(A).
577 %ifndef SQLITE_OMIT_COMPOUND_SELECT
578 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
579 Select *pRhs = Z;
580 Select *pLhs = A;
581 if( pRhs && pRhs->pPrior ){
582 SrcList *pFrom;
583 Token x;
584 x.n = 0;
585 parserDoubleLinkSelect(pParse, pRhs);
586 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0);
587 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
589 if( pRhs ){
590 pRhs->op = (u8)Y;
591 pRhs->pPrior = pLhs;
592 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
593 pRhs->selFlags &= ~SF_MultiValue;
594 if( Y!=TK_ALL ) pParse->hasCompound = 1;
595 }else{
596 sqlite3SelectDelete(pParse->db, pLhs);
598 A = pRhs;
600 %type multiselect_op {int}
601 multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
602 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
603 multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
604 %endif SQLITE_OMIT_COMPOUND_SELECT
606 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
607 groupby_opt(P) having_opt(Q)
608 orderby_opt(Z) limit_opt(L). {
609 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
611 %ifndef SQLITE_OMIT_WINDOWFUNC
612 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
613 groupby_opt(P) having_opt(Q) window_clause(R)
614 orderby_opt(Z) limit_opt(L). {
615 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
616 if( A ){
617 A->pWinDefn = R;
618 }else{
619 sqlite3WindowListDelete(pParse->db, R);
622 %endif
625 oneselect(A) ::= values(A).
627 %type values {Select*}
628 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
629 values(A) ::= VALUES LP nexprlist(X) RP. {
630 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
632 values(A) ::= values(A) COMMA LP nexprlist(Y) RP. {
633 Select *pRight, *pLeft = A;
634 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
635 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
636 if( pRight ){
637 pRight->op = TK_ALL;
638 pRight->pPrior = pLeft;
639 A = pRight;
640 }else{
641 A = pLeft;
645 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
646 // present and false (0) if it is not.
648 %type distinct {int}
649 distinct(A) ::= DISTINCT. {A = SF_Distinct;}
650 distinct(A) ::= ALL. {A = SF_All;}
651 distinct(A) ::= . {A = 0;}
653 // selcollist is a list of expressions that are to become the return
654 // values of the SELECT statement. The "*" in statements like
655 // "SELECT * FROM ..." is encoded as a special expression with an
656 // opcode of TK_ASTERISK.
658 %type selcollist {ExprList*}
659 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
660 %type sclp {ExprList*}
661 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
662 sclp(A) ::= selcollist(A) COMMA.
663 sclp(A) ::= . {A = 0;}
664 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
665 A = sqlite3ExprListAppend(pParse, A, X);
666 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
667 sqlite3ExprListSetSpan(pParse,A,B,Z);
669 selcollist(A) ::= sclp(A) scanpt STAR(X). {
670 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
671 sqlite3ExprSetErrorOffset(p, (int)(X.z - pParse->zTail));
672 A = sqlite3ExprListAppend(pParse, A, p);
674 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR(Y). {
675 Expr *pRight, *pLeft, *pDot;
676 pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
677 sqlite3ExprSetErrorOffset(pRight, (int)(Y.z - pParse->zTail));
678 pLeft = tokenExpr(pParse, TK_ID, X);
679 pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
680 A = sqlite3ExprListAppend(pParse,A, pDot);
683 // An option "AS <id>" phrase that can follow one of the expressions that
684 // define the result set, or one of the tables in the FROM clause.
686 %type as {Token}
687 as(X) ::= AS nm(Y). {X = Y;}
688 as(X) ::= ids(X).
689 as(X) ::= . {X.n = 0; X.z = 0;}
692 %type seltablist {SrcList*}
693 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
694 %type stl_prefix {SrcList*}
695 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
696 %type from {SrcList*}
697 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
699 // A complete FROM clause.
701 from(A) ::= . {A = 0;}
702 from(A) ::= FROM seltablist(X). {
703 A = X;
704 sqlite3SrcListShiftJoinType(pParse,A);
707 // "seltablist" is a "Select Table List" - the content of the FROM clause
708 // in a SELECT statement. "stl_prefix" is a prefix of this list.
710 stl_prefix(A) ::= seltablist(A) joinop(Y). {
711 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
713 stl_prefix(A) ::= . {A = 0;}
714 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) on_using(N). {
715 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
717 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_by(I) on_using(N). {
718 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
719 sqlite3SrcListIndexedBy(pParse, A, &I);
721 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) on_using(N). {
722 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
723 sqlite3SrcListFuncArgs(pParse, A, E);
725 %ifndef SQLITE_OMIT_SUBQUERY
726 seltablist(A) ::= stl_prefix(A) LP select(S) RP as(Z) on_using(N). {
727 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,&N);
729 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP as(Z) on_using(N). {
730 if( A==0 && Z.n==0 && N.pOn==0 && N.pUsing==0 ){
731 A = F;
732 }else if( ALWAYS(F!=0) && F->nSrc==1 ){
733 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N);
734 if( A ){
735 SrcItem *pNew = &A->a[A->nSrc-1];
736 SrcItem *pOld = F->a;
737 pNew->zName = pOld->zName;
738 pNew->zDatabase = pOld->zDatabase;
739 pNew->pSelect = pOld->pSelect;
740 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
741 pNew->fg.isNestedFrom = 1;
743 if( pOld->fg.isTabFunc ){
744 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
745 pOld->u1.pFuncArg = 0;
746 pOld->fg.isTabFunc = 0;
747 pNew->fg.isTabFunc = 1;
749 pOld->zName = pOld->zDatabase = 0;
750 pOld->pSelect = 0;
752 sqlite3SrcListDelete(pParse->db, F);
753 }else{
754 Select *pSubquery;
755 sqlite3SrcListShiftJoinType(pParse,F);
756 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
757 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N);
760 %endif SQLITE_OMIT_SUBQUERY
762 %type dbnm {Token}
763 dbnm(A) ::= . {A.z=0; A.n=0;}
764 dbnm(A) ::= DOT nm(X). {A = X;}
766 %type fullname {SrcList*}
767 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
768 fullname(A) ::= nm(X). {
769 A = sqlite3SrcListAppend(pParse,0,&X,0);
770 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);
772 fullname(A) ::= nm(X) DOT nm(Y). {
773 A = sqlite3SrcListAppend(pParse,0,&X,&Y);
774 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);
777 %type xfullname {SrcList*}
778 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
779 xfullname(A) ::= nm(X).
780 {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/}
781 xfullname(A) ::= nm(X) DOT nm(Y).
782 {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/}
783 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
784 A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/
785 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
787 xfullname(A) ::= nm(X) AS nm(Z). {
788 A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/
789 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
792 %type joinop {int}
793 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
794 joinop(X) ::= JOIN_KW(A) JOIN.
795 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
796 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
797 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
798 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
799 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
801 // There is a parsing abiguity in an upsert statement that uses a
802 // SELECT on the RHS of a the INSERT:
804 // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
805 // here ----^^
807 // When the ON token is encountered, the parser does not know if it is
808 // the beginning of an ON CONFLICT clause, or the beginning of an ON
809 // clause associated with the JOIN. The conflict is resolved in favor
810 // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
811 // WHERE clause in between, like this:
813 // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
815 // The [AND] and [OR] precedence marks in the rules for on_using cause the
816 // ON in this context to always be interpreted as belonging to the JOIN.
818 %type on_using {OnOrUsing}
819 //%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);}
820 on_using(N) ::= ON expr(E). {N.pOn = E; N.pUsing = 0;}
821 on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;}
822 on_using(N) ::= . [OR] {N.pOn = 0; N.pUsing = 0;}
824 // Note that this block abuses the Token type just a little. If there is
825 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
826 // there is an INDEXED BY clause, then the token is populated as per normal,
827 // with z pointing to the token data and n containing the number of bytes
828 // in the token.
830 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
831 // normally illegal. The sqlite3SrcListIndexedBy() function
832 // recognizes and interprets this as a special case.
834 %type indexed_opt {Token}
835 %type indexed_by {Token}
836 indexed_opt(A) ::= . {A.z=0; A.n=0;}
837 indexed_opt(A) ::= indexed_by(A).
838 indexed_by(A) ::= INDEXED BY nm(X). {A = X;}
839 indexed_by(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
841 %type orderby_opt {ExprList*}
842 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
844 // the sortlist non-terminal stores a list of expression where each
845 // expression is optionally followed by ASC or DESC to indicate the
846 // sort order.
848 %type sortlist {ExprList*}
849 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
851 orderby_opt(A) ::= . {A = 0;}
852 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
853 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
854 A = sqlite3ExprListAppend(pParse,A,Y);
855 sqlite3ExprListSetSortOrder(A,Z,X);
857 sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
858 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
859 sqlite3ExprListSetSortOrder(A,Z,X);
862 %type sortorder {int}
864 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
865 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
866 sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
868 %type nulls {int}
869 nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;}
870 nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;}
871 nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;}
873 %type groupby_opt {ExprList*}
874 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
875 groupby_opt(A) ::= . {A = 0;}
876 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
878 %type having_opt {Expr*}
879 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
880 having_opt(A) ::= . {A = 0;}
881 having_opt(A) ::= HAVING expr(X). {A = X;}
883 %type limit_opt {Expr*}
885 // The destructor for limit_opt will never fire in the current grammar.
886 // The limit_opt non-terminal only occurs at the end of a single production
887 // rule for SELECT statements. As soon as the rule that create the
888 // limit_opt non-terminal reduces, the SELECT statement rule will also
889 // reduce. So there is never a limit_opt non-terminal on the stack
890 // except as a transient. So there is never anything to destroy.
892 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
893 limit_opt(A) ::= . {A = 0;}
894 limit_opt(A) ::= LIMIT expr(X).
895 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
896 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
897 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
898 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
899 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
901 /////////////////////////// The DELETE statement /////////////////////////////
903 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
904 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W)
905 orderby_opt(O) limit_opt(L). {
906 sqlite3SrcListIndexedBy(pParse, X, &I);
907 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
908 if( O || L ){
909 updateDeleteLimitError(pParse,O,L);
910 O = 0;
911 L = 0;
913 #endif
914 sqlite3DeleteFrom(pParse,X,W,O,L);
916 %else
917 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). {
918 sqlite3SrcListIndexedBy(pParse, X, &I);
919 sqlite3DeleteFrom(pParse,X,W,0,0);
921 %endif
923 %type where_opt {Expr*}
924 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
925 %type where_opt_ret {Expr*}
926 %destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);}
928 where_opt(A) ::= . {A = 0;}
929 where_opt(A) ::= WHERE expr(X). {A = X;}
930 where_opt_ret(A) ::= . {A = 0;}
931 where_opt_ret(A) ::= WHERE expr(X). {A = X;}
932 where_opt_ret(A) ::= RETURNING selcollist(X).
933 {sqlite3AddReturning(pParse,X); A = 0;}
934 where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y).
935 {sqlite3AddReturning(pParse,Y); A = X;}
937 ////////////////////////// The UPDATE command ////////////////////////////////
939 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
940 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
941 where_opt_ret(W) orderby_opt(O) limit_opt(L). {
942 sqlite3SrcListIndexedBy(pParse, X, &I);
943 if( F ){
944 SrcList *pFromClause = F;
945 if( pFromClause->nSrc>1 ){
946 Select *pSubquery;
947 Token as;
948 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
949 as.n = 0;
950 as.z = 0;
951 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
953 X = sqlite3SrcListAppendList(pParse, X, pFromClause);
955 sqlite3ExprListCheckLength(pParse,Y,"set list");
956 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
957 if( O || L ){
958 updateDeleteLimitError(pParse,O,L);
959 O = 0;
960 L = 0;
962 #endif
963 sqlite3Update(pParse,X,Y,W,R,O,L,0);
965 %else
966 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
967 where_opt_ret(W). {
968 sqlite3SrcListIndexedBy(pParse, X, &I);
969 sqlite3ExprListCheckLength(pParse,Y,"set list");
970 if( F ){
971 SrcList *pFromClause = F;
972 if( pFromClause->nSrc>1 ){
973 Select *pSubquery;
974 Token as;
975 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
976 as.n = 0;
977 as.z = 0;
978 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
980 X = sqlite3SrcListAppendList(pParse, X, pFromClause);
982 sqlite3Update(pParse,X,Y,W,R,0,0,0);
984 %endif
988 %type setlist {ExprList*}
989 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
991 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
992 A = sqlite3ExprListAppend(pParse, A, Y);
993 sqlite3ExprListSetName(pParse, A, &X, 1);
995 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
996 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
998 setlist(A) ::= nm(X) EQ expr(Y). {
999 A = sqlite3ExprListAppend(pParse, 0, Y);
1000 sqlite3ExprListSetName(pParse, A, &X, 1);
1002 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
1003 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
1006 ////////////////////////// The INSERT command /////////////////////////////////
1008 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
1009 upsert(U). {
1010 sqlite3Insert(pParse, X, S, F, R, U);
1012 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning.
1014 sqlite3Insert(pParse, X, 0, F, R, 0);
1017 %type upsert {Upsert*}
1019 // Because upsert only occurs at the tip end of the INSERT rule for cmd,
1020 // there is never a case where the value of the upsert pointer will not
1021 // be destroyed by the cmd action. So comment-out the destructor to
1022 // avoid unreachable code.
1023 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
1024 upsert(A) ::= . { A = 0; }
1025 upsert(A) ::= RETURNING selcollist(X). { A = 0; sqlite3AddReturning(pParse,X); }
1026 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
1027 DO UPDATE SET setlist(Z) where_opt(W) upsert(N).
1028 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);}
1029 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N).
1030 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); }
1031 upsert(A) ::= ON CONFLICT DO NOTHING returning.
1032 { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
1033 upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning.
1034 { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);}
1036 returning ::= RETURNING selcollist(X). {sqlite3AddReturning(pParse,X);}
1037 returning ::= .
1039 %type insert_cmd {int}
1040 insert_cmd(A) ::= INSERT orconf(R). {A = R;}
1041 insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
1043 %type idlist_opt {IdList*}
1044 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
1045 %type idlist {IdList*}
1046 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
1048 idlist_opt(A) ::= . {A = 0;}
1049 idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
1050 idlist(A) ::= idlist(A) COMMA nm(Y).
1051 {A = sqlite3IdListAppend(pParse,A,&Y);}
1052 idlist(A) ::= nm(Y).
1053 {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}
1055 /////////////////////////// Expression Processing /////////////////////////////
1058 %type expr {Expr*}
1059 %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
1060 %type term {Expr*}
1061 %destructor term {sqlite3ExprDelete(pParse->db, $$);}
1063 %include {
1065 /* Construct a new Expr object from a single token */
1066 static Expr *tokenExpr(Parse *pParse, int op, Token t){
1067 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
1068 if( p ){
1069 /* memset(p, 0, sizeof(Expr)); */
1070 p->op = (u8)op;
1071 p->affExpr = 0;
1072 p->flags = EP_Leaf;
1073 ExprClearVVAProperties(p);
1074 /* p->iAgg = -1; // Not required */
1075 p->pLeft = p->pRight = 0;
1076 p->pAggInfo = 0;
1077 memset(&p->x, 0, sizeof(p->x));
1078 memset(&p->y, 0, sizeof(p->y));
1079 p->op2 = 0;
1080 p->iTable = 0;
1081 p->iColumn = 0;
1082 p->u.zToken = (char*)&p[1];
1083 memcpy(p->u.zToken, t.z, t.n);
1084 p->u.zToken[t.n] = 0;
1085 p->w.iOfst = (int)(t.z - pParse->zTail);
1086 if( sqlite3Isquote(p->u.zToken[0]) ){
1087 sqlite3DequoteExpr(p);
1089 #if SQLITE_MAX_EXPR_DEPTH>0
1090 p->nHeight = 1;
1091 #endif
1092 if( IN_RENAME_OBJECT ){
1093 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
1096 return p;
1101 expr(A) ::= term(A).
1102 expr(A) ::= LP expr(X) RP. {A = X;}
1103 expr(A) ::= idj(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
1104 expr(A) ::= nm(X) DOT nm(Y). {
1105 Expr *temp1 = tokenExpr(pParse,TK_ID,X);
1106 Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
1107 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
1109 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
1110 Expr *temp1 = tokenExpr(pParse,TK_ID,X);
1111 Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
1112 Expr *temp3 = tokenExpr(pParse,TK_ID,Z);
1113 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
1114 if( IN_RENAME_OBJECT ){
1115 sqlite3RenameTokenRemap(pParse, 0, temp1);
1117 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
1119 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1120 term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1121 term(A) ::= INTEGER(X). {
1122 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
1123 if( A ) A->w.iOfst = (int)(X.z - pParse->zTail);
1125 expr(A) ::= VARIABLE(X). {
1126 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
1127 u32 n = X.n;
1128 A = tokenExpr(pParse, TK_VARIABLE, X);
1129 sqlite3ExprAssignVarNumber(pParse, A, n);
1130 }else{
1131 /* When doing a nested parse, one can include terms in an expression
1132 ** that look like this: #1 #2 ... These terms refer to registers
1133 ** in the virtual machine. #N is the N-th register. */
1134 Token t = X; /*A-overwrites-X*/
1135 assert( t.n>=2 );
1136 if( pParse->nested==0 ){
1137 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
1138 A = 0;
1139 }else{
1140 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1141 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
1145 expr(A) ::= expr(A) COLLATE ids(C). {
1146 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
1148 %ifndef SQLITE_OMIT_CAST
1149 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1150 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1151 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
1153 %endif SQLITE_OMIT_CAST
1156 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP. {
1157 A = sqlite3ExprFunction(pParse, Y, &X, D);
1159 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP. {
1160 A = sqlite3ExprFunction(pParse, Y, &X, D);
1161 sqlite3ExprAddFunctionOrderBy(pParse, A, O);
1163 expr(A) ::= idj(X) LP STAR RP. {
1164 A = sqlite3ExprFunction(pParse, 0, &X, 0);
1167 %ifndef SQLITE_OMIT_WINDOWFUNC
1168 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
1169 A = sqlite3ExprFunction(pParse, Y, &X, D);
1170 sqlite3WindowAttach(pParse, A, Z);
1172 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP filter_over(Z). {
1173 A = sqlite3ExprFunction(pParse, Y, &X, D);
1174 sqlite3WindowAttach(pParse, A, Z);
1175 sqlite3ExprAddFunctionOrderBy(pParse, A, O);
1177 expr(A) ::= idj(X) LP STAR RP filter_over(Z). {
1178 A = sqlite3ExprFunction(pParse, 0, &X, 0);
1179 sqlite3WindowAttach(pParse, A, Z);
1181 %endif
1183 term(A) ::= CTIME_KW(OP). {
1184 A = sqlite3ExprFunction(pParse, 0, &OP, 0);
1187 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1188 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1189 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1190 if( A ){
1191 A->x.pList = pList;
1192 if( ALWAYS(pList->nExpr) ){
1193 A->flags |= pList->a[0].pExpr->flags & EP_Propagate;
1195 }else{
1196 sqlite3ExprListDelete(pParse->db, pList);
1200 expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);}
1201 expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1202 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
1203 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1204 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1205 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
1206 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1207 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
1208 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1209 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
1210 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1211 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1212 %type likeop {Token}
1213 likeop(A) ::= LIKE_KW|MATCH(A).
1214 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
1215 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
1216 ExprList *pList;
1217 int bNot = OP.n & 0x80000000;
1218 OP.n &= 0x7fffffff;
1219 pList = sqlite3ExprListAppend(pParse,0, Y);
1220 pList = sqlite3ExprListAppend(pParse,pList, A);
1221 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1222 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1223 if( A ) A->flags |= EP_InfixFunc;
1225 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
1226 ExprList *pList;
1227 int bNot = OP.n & 0x80000000;
1228 OP.n &= 0x7fffffff;
1229 pList = sqlite3ExprListAppend(pParse,0, Y);
1230 pList = sqlite3ExprListAppend(pParse,pList, A);
1231 pList = sqlite3ExprListAppend(pParse,pList, E);
1232 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1233 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1234 if( A ) A->flags |= EP_InfixFunc;
1237 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1238 expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
1240 %include {
1241 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1242 ** unary TK_ISNULL or TK_NOTNULL expression. */
1243 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1244 sqlite3 *db = pParse->db;
1245 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
1246 pA->op = (u8)op;
1247 sqlite3ExprDelete(db, pA->pRight);
1248 pA->pRight = 0;
1253 // expr1 IS expr2
1254 // expr1 IS NOT expr2
1256 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1257 // is any other expression, code as TK_IS or TK_ISNOT.
1259 expr(A) ::= expr(A) IS expr(Y). {
1260 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1261 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1263 expr(A) ::= expr(A) IS NOT expr(Y). {
1264 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1265 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1267 expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y). {
1268 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1269 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1271 expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). {
1272 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1273 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1276 expr(A) ::= NOT(B) expr(X).
1277 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1278 expr(A) ::= BITNOT(B) expr(X).
1279 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1280 expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1281 A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
1282 /*A-overwrites-B*/
1285 expr(A) ::= expr(B) PTR(C) expr(D). {
1286 ExprList *pList = sqlite3ExprListAppend(pParse, 0, B);
1287 pList = sqlite3ExprListAppend(pParse, pList, D);
1288 A = sqlite3ExprFunction(pParse, pList, &C, 0);
1291 %type between_op {int}
1292 between_op(A) ::= BETWEEN. {A = 0;}
1293 between_op(A) ::= NOT BETWEEN. {A = 1;}
1294 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1295 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1296 pList = sqlite3ExprListAppend(pParse,pList, Y);
1297 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1298 if( A ){
1299 A->x.pList = pList;
1300 }else{
1301 sqlite3ExprListDelete(pParse->db, pList);
1303 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1305 %ifndef SQLITE_OMIT_SUBQUERY
1306 %type in_op {int}
1307 in_op(A) ::= IN. {A = 0;}
1308 in_op(A) ::= NOT IN. {A = 1;}
1309 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
1310 if( Y==0 ){
1311 /* Expressions of the form
1313 ** expr1 IN ()
1314 ** expr1 NOT IN ()
1316 ** simplify to constants 0 (false) and 1 (true), respectively,
1317 ** regardless of the value of expr1.
1319 sqlite3ExprUnmapAndDelete(pParse, A);
1320 A = sqlite3Expr(pParse->db, TK_STRING, N ? "true" : "false");
1321 if( A ) sqlite3ExprIdToTrueFalse(A);
1322 }else{
1323 Expr *pRHS = Y->a[0].pExpr;
1324 if( Y->nExpr==1 && sqlite3ExprIsConstant(pRHS) && A->op!=TK_VECTOR ){
1325 Y->a[0].pExpr = 0;
1326 sqlite3ExprListDelete(pParse->db, Y);
1327 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
1328 A = sqlite3PExpr(pParse, TK_EQ, A, pRHS);
1329 }else if( Y->nExpr==1 && pRHS->op==TK_SELECT ){
1330 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1331 sqlite3PExprAddSelect(pParse, A, pRHS->x.pSelect);
1332 pRHS->x.pSelect = 0;
1333 sqlite3ExprListDelete(pParse->db, Y);
1334 }else{
1335 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1336 if( A==0 ){
1337 sqlite3ExprListDelete(pParse->db, Y);
1338 }else if( A->pLeft->op==TK_VECTOR ){
1339 int nExpr = A->pLeft->x.pList->nExpr;
1340 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y);
1341 if( pSelectRHS ){
1342 parserDoubleLinkSelect(pParse, pSelectRHS);
1343 sqlite3PExprAddSelect(pParse, A, pSelectRHS);
1345 }else{
1346 A->x.pList = Y;
1347 sqlite3ExprSetHeightAndFlags(pParse, A);
1350 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1353 expr(A) ::= LP select(X) RP. {
1354 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1355 sqlite3PExprAddSelect(pParse, A, X);
1357 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1358 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1359 sqlite3PExprAddSelect(pParse, A, Y);
1360 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1362 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1363 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
1364 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
1365 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1366 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1367 sqlite3PExprAddSelect(pParse, A, pSelect);
1368 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1370 expr(A) ::= EXISTS LP select(Y) RP. {
1371 Expr *p;
1372 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
1373 sqlite3PExprAddSelect(pParse, p, Y);
1375 %endif SQLITE_OMIT_SUBQUERY
1377 /* CASE expressions */
1378 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1379 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1380 if( A ){
1381 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1382 sqlite3ExprSetHeightAndFlags(pParse, A);
1383 }else{
1384 sqlite3ExprListDelete(pParse->db, Y);
1385 sqlite3ExprDelete(pParse->db, Z);
1388 %type case_exprlist {ExprList*}
1389 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1390 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1391 A = sqlite3ExprListAppend(pParse,A, Y);
1392 A = sqlite3ExprListAppend(pParse,A, Z);
1394 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1395 A = sqlite3ExprListAppend(pParse,0, Y);
1396 A = sqlite3ExprListAppend(pParse,A, Z);
1398 %type case_else {Expr*}
1399 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1400 case_else(A) ::= ELSE expr(X). {A = X;}
1401 case_else(A) ::= . {A = 0;}
1402 %type case_operand {Expr*}
1403 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1404 case_operand(A) ::= expr(A).
1405 case_operand(A) ::= . {A = 0;}
1407 %type exprlist {ExprList*}
1408 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1409 %type nexprlist {ExprList*}
1410 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1412 exprlist(A) ::= nexprlist(A).
1413 exprlist(A) ::= . {A = 0;}
1414 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1415 {A = sqlite3ExprListAppend(pParse,A,Y);}
1416 nexprlist(A) ::= expr(Y).
1417 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
1419 %ifndef SQLITE_OMIT_SUBQUERY
1420 /* A paren_exprlist is an optional expression list contained inside
1421 ** of parenthesis */
1422 %type paren_exprlist {ExprList*}
1423 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1424 paren_exprlist(A) ::= . {A = 0;}
1425 paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
1426 %endif SQLITE_OMIT_SUBQUERY
1429 ///////////////////////////// The CREATE INDEX command ///////////////////////
1431 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1432 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1433 sqlite3CreateIndex(pParse, &X, &D,
1434 sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
1435 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1436 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
1437 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
1441 %type uniqueflag {int}
1442 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1443 uniqueflag(A) ::= . {A = OE_None;}
1446 // The eidlist non-terminal (Expression Id List) generates an ExprList
1447 // from a list of identifiers. The identifier names are in ExprList.a[].zName.
1448 // This list is stored in an ExprList rather than an IdList so that it
1449 // can be easily sent to sqlite3ColumnsExprList().
1451 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1452 // used for the arguments to an index. That is just an historical accident.
1454 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1455 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1456 // places - places that might have been stored in the sqlite_schema table.
1457 // Those extra features were ignored. But because they might be in some
1458 // (busted) old databases, we need to continue parsing them when loading
1459 // historical schemas.
1461 %type eidlist {ExprList*}
1462 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1463 %type eidlist_opt {ExprList*}
1464 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1466 %include {
1467 /* Add a single new term to an ExprList that is used to store a
1468 ** list of identifiers. Report an error if the ID list contains
1469 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1470 ** error while parsing a legacy schema.
1472 static ExprList *parserAddExprIdListTerm(
1473 Parse *pParse,
1474 ExprList *pPrior,
1475 Token *pIdToken,
1476 int hasCollate,
1477 int sortOrder
1479 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1480 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1481 && pParse->db->init.busy==0
1483 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1484 pIdToken->n, pIdToken->z);
1486 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1487 return p;
1489 } // end %include
1491 eidlist_opt(A) ::= . {A = 0;}
1492 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
1493 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1494 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1496 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1497 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1500 %type collate {int}
1501 collate(C) ::= . {C = 0;}
1502 collate(C) ::= COLLATE ids. {C = 1;}
1505 ///////////////////////////// The DROP INDEX command /////////////////////////
1507 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
1509 ///////////////////////////// The VACUUM command /////////////////////////////
1511 %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH
1512 %type vinto {Expr*}
1513 %destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
1514 cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);}
1515 cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);}
1516 vinto(A) ::= INTO expr(X). {A = X;}
1517 vinto(A) ::= . {A = 0;}
1518 %endif
1520 ///////////////////////////// The PRAGMA command /////////////////////////////
1522 %ifndef SQLITE_OMIT_PRAGMA
1523 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1524 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1525 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1526 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1527 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1528 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1529 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1531 nmnum(A) ::= plus_num(A).
1532 nmnum(A) ::= nm(A).
1533 nmnum(A) ::= ON(A).
1534 nmnum(A) ::= DELETE(A).
1535 nmnum(A) ::= DEFAULT(A).
1536 %endif SQLITE_OMIT_PRAGMA
1537 %token_class number INTEGER|FLOAT.
1538 plus_num(A) ::= PLUS number(X). {A = X;}
1539 plus_num(A) ::= number(A).
1540 minus_num(A) ::= MINUS number(X). {A = X;}
1541 //////////////////////////// The CREATE TRIGGER command /////////////////////
1543 %ifndef SQLITE_OMIT_TRIGGER
1545 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1546 Token all;
1547 all.z = A.z;
1548 all.n = (int)(Z.z - A.z) + Z.n;
1549 sqlite3FinishTrigger(pParse, S, &all);
1552 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1553 trigger_time(C) trigger_event(D)
1554 ON fullname(E) foreach_clause when_clause(G). {
1555 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1556 A = (Z.n==0?B:Z); /*A-overwrites-T*/
1559 %type trigger_time {int}
1560 trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
1561 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1562 trigger_time(A) ::= . { A = TK_BEFORE; }
1564 %type trigger_event {struct TrigEvent}
1565 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1566 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1567 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1568 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1570 foreach_clause ::= .
1571 foreach_clause ::= FOR EACH ROW.
1573 %type when_clause {Expr*}
1574 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1575 when_clause(A) ::= . { A = 0; }
1576 when_clause(A) ::= WHEN expr(X). { A = X; }
1578 %type trigger_cmd_list {TriggerStep*}
1579 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1580 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1581 assert( A!=0 );
1582 A->pLast->pNext = X;
1583 A->pLast = X;
1585 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1586 assert( A!=0 );
1587 A->pLast = A;
1590 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1591 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1592 // the same database as the table that the trigger fires on.
1594 %type trnm {Token}
1595 trnm(A) ::= nm(A).
1596 trnm(A) ::= nm DOT nm(X). {
1597 A = X;
1598 sqlite3ErrorMsg(pParse,
1599 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1600 "statements within triggers");
1603 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1604 // statements within triggers. We make a specific error message for this
1605 // since it is an exception to the default grammar rules.
1607 tridxby ::= .
1608 tridxby ::= INDEXED BY nm. {
1609 sqlite3ErrorMsg(pParse,
1610 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1611 "within triggers");
1613 tridxby ::= NOT INDEXED. {
1614 sqlite3ErrorMsg(pParse,
1615 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1616 "within triggers");
1621 %type trigger_cmd {TriggerStep*}
1622 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1623 // UPDATE
1624 trigger_cmd(A) ::=
1625 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E).
1626 {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);}
1628 // INSERT
1629 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1630 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
1631 A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
1633 // DELETE
1634 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1635 {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
1637 // SELECT
1638 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1639 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
1641 // The special RAISE expression that may occur in trigger programs
1642 expr(A) ::= RAISE LP IGNORE RP. {
1643 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1644 if( A ){
1645 A->affExpr = OE_Ignore;
1648 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1649 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1650 if( A ) {
1651 A->affExpr = (char)T;
1654 %endif !SQLITE_OMIT_TRIGGER
1656 %type raisetype {int}
1657 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1658 raisetype(A) ::= ABORT. {A = OE_Abort;}
1659 raisetype(A) ::= FAIL. {A = OE_Fail;}
1662 //////////////////////// DROP TRIGGER statement //////////////////////////////
1663 %ifndef SQLITE_OMIT_TRIGGER
1664 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1665 sqlite3DropTrigger(pParse,X,NOERR);
1667 %endif !SQLITE_OMIT_TRIGGER
1669 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1670 %ifndef SQLITE_OMIT_ATTACH
1671 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1672 sqlite3Attach(pParse, F, D, K);
1674 cmd ::= DETACH database_kw_opt expr(D). {
1675 sqlite3Detach(pParse, D);
1678 %type key_opt {Expr*}
1679 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1680 key_opt(A) ::= . { A = 0; }
1681 key_opt(A) ::= KEY expr(X). { A = X; }
1683 database_kw_opt ::= DATABASE.
1684 database_kw_opt ::= .
1685 %endif SQLITE_OMIT_ATTACH
1687 ////////////////////////// REINDEX collation //////////////////////////////////
1688 %ifndef SQLITE_OMIT_REINDEX
1689 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1690 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
1691 %endif SQLITE_OMIT_REINDEX
1693 /////////////////////////////////// ANALYZE ///////////////////////////////////
1694 %ifndef SQLITE_OMIT_ANALYZE
1695 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1696 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1697 %endif
1699 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1700 %ifndef SQLITE_OMIT_ALTERTABLE
1701 %ifndef SQLITE_OMIT_VIRTUALTABLE
1702 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1703 sqlite3AlterRenameTable(pParse,X,&Z);
1705 cmd ::= ALTER TABLE add_column_fullname
1706 ADD kwcolumn_opt columnname(Y) carglist. {
1707 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1708 sqlite3AlterFinishAddColumn(pParse, &Y);
1710 cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). {
1711 sqlite3AlterDropColumn(pParse, X, &Y);
1714 add_column_fullname ::= fullname(X). {
1715 disableLookaside(pParse);
1716 sqlite3AlterBeginAddColumn(pParse, X);
1718 cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
1719 sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
1722 kwcolumn_opt ::= .
1723 kwcolumn_opt ::= COLUMNKW.
1725 %endif SQLITE_OMIT_VIRTUALTABLE
1726 %endif SQLITE_OMIT_ALTERTABLE
1728 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1729 %ifndef SQLITE_OMIT_VIRTUALTABLE
1730 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1731 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
1732 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1733 nm(X) dbnm(Y) USING nm(Z). {
1734 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1736 vtabarglist ::= vtabarg.
1737 vtabarglist ::= vtabarglist COMMA vtabarg.
1738 vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1739 vtabarg ::= vtabarg vtabargtoken.
1740 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1741 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1742 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1743 anylist ::= .
1744 anylist ::= anylist LP anylist RP.
1745 anylist ::= anylist ANY.
1746 %endif SQLITE_OMIT_VIRTUALTABLE
1749 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1750 %type wqlist {With*}
1751 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1752 %type wqitem {Cte*}
1753 // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable
1755 with ::= .
1756 %ifndef SQLITE_OMIT_CTE
1757 with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1758 with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1760 %type wqas {u8}
1761 wqas(A) ::= AS. {A = M10d_Any;}
1762 wqas(A) ::= AS MATERIALIZED. {A = M10d_Yes;}
1763 wqas(A) ::= AS NOT MATERIALIZED. {A = M10d_No;}
1764 wqitem(A) ::= nm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. {
1765 A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/
1767 wqlist(A) ::= wqitem(X). {
1768 A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/
1770 wqlist(A) ::= wqlist(A) COMMA wqitem(X). {
1771 A = sqlite3WithAdd(pParse, A, X);
1773 %endif SQLITE_OMIT_CTE
1775 //////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
1776 // These must be at the end of this file. Specifically, the rules that
1777 // introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1778 // the integer values assigned to these tokens to be larger than all other
1779 // tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
1781 %ifndef SQLITE_OMIT_WINDOWFUNC
1782 %type windowdefn_list {Window*}
1783 %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
1784 windowdefn_list(A) ::= windowdefn(A).
1785 windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
1786 assert( Z!=0 );
1787 sqlite3WindowChain(pParse, Z, Y);
1788 Z->pNextWin = Y;
1789 A = Z;
1792 %type windowdefn {Window*}
1793 %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
1794 windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
1795 if( ALWAYS(Y) ){
1796 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1798 A = Y;
1801 %type window {Window*}
1802 %destructor window {sqlite3WindowDelete(pParse->db, $$);}
1804 %type frame_opt {Window*}
1805 %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1807 %type part_opt {ExprList*}
1808 %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1810 %type filter_clause {Expr*}
1811 %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
1813 %type over_clause {Window*}
1814 %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
1816 %type filter_over {Window*}
1817 %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
1819 %type range_or_rows {int}
1821 %type frame_bound {struct FrameBound}
1822 %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1823 %type frame_bound_s {struct FrameBound}
1824 %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1825 %type frame_bound_e {struct FrameBound}
1826 %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1828 window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1829 A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
1831 window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1832 A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
1834 window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
1835 A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
1837 window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
1838 A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
1840 window(A) ::= frame_opt(A).
1841 window(A) ::= nm(W) frame_opt(Z). {
1842 A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
1845 frame_opt(A) ::= . {
1846 A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
1848 frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). {
1849 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
1851 frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
1852 frame_bound_e(Z) frame_exclude_opt(W). {
1853 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
1856 range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/}
1858 frame_bound_s(A) ::= frame_bound(X). {A = X;}
1859 frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
1860 frame_bound_e(A) ::= frame_bound(X). {A = X;}
1861 frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
1863 frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
1864 {A.eType = @Y; A.pExpr = X;}
1865 frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;}
1867 %type frame_exclude_opt {u8}
1868 frame_exclude_opt(A) ::= . {A = 0;}
1869 frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
1871 %type frame_exclude {u8}
1872 frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/}
1873 frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
1874 frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/}
1877 %type window_clause {Window*}
1878 %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
1879 window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
1881 filter_over(A) ::= filter_clause(F) over_clause(O). {
1882 if( O ){
1883 O->pFilter = F;
1884 }else{
1885 sqlite3ExprDelete(pParse->db, F);
1887 A = O;
1889 filter_over(A) ::= over_clause(O). {
1890 A = O;
1892 filter_over(A) ::= filter_clause(F). {
1893 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1894 if( A ){
1895 A->eFrmType = TK_FILTER;
1896 A->pFilter = F;
1897 }else{
1898 sqlite3ExprDelete(pParse->db, F);
1902 over_clause(A) ::= OVER LP window(Z) RP. {
1903 A = Z;
1904 assert( A!=0 );
1906 over_clause(A) ::= OVER nm(Z). {
1907 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1908 if( A ){
1909 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
1913 filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
1914 %endif /* SQLITE_OMIT_WINDOWFUNC */
1917 ** The code generator needs some extra TK_ token values for tokens that
1918 ** are synthesized and do not actually appear in the grammar:
1920 %token
1921 COLUMN /* Reference to a table column */
1922 AGG_FUNCTION /* An aggregate function */
1923 AGG_COLUMN /* An aggregated column */
1924 TRUEFALSE /* True or false keyword */
1925 ISNOT /* Combination of IS and NOT */
1926 FUNCTION /* A function invocation */
1927 UMINUS /* Unary minus */
1928 UPLUS /* Unary plus */
1929 TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
1930 REGISTER /* Reference to a VDBE register */
1931 VECTOR /* Vector */
1932 SELECT_COLUMN /* Choose a single column from a multi-column SELECT */
1933 IF_NULL_ROW /* the if-null-row operator */
1934 ASTERISK /* The "*" in count(*) and similar */
1935 SPAN /* The span operator */
1936 ERROR /* An expression containing an error */
1938 /* There must be no more than 255 tokens defined above. If this grammar
1939 ** is extended with new rules and tokens, they must either be so few in
1940 ** number that TK_SPAN is no more than 255, or else the new tokens must
1941 ** appear after this line.
1943 %include {
1944 #if TK_SPAN>255
1945 # error too many tokens in the grammar
1946 #endif
1950 ** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens. The
1951 ** parser depends on this. Those tokens are not used in any grammar rule.
1952 ** They are only used by the tokenizer. Declare them last so that they
1953 ** are guaranteed to be the last two tokens
1955 %token SPACE ILLEGAL.