Add tests to bestindexC.test. No changes to code.
[sqlite.git] / src / parse.y
blob071e10abd424e8f4fb8babca69737ce85772085a
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);}
569 %endif /* SQLITE_OMIT_CTE */
570 select(A) ::= selectnowith(A). {
571 Select *p = A;
572 if( p ){
573 parserDoubleLinkSelect(pParse, p);
577 selectnowith(A) ::= oneselect(A).
578 %ifndef SQLITE_OMIT_COMPOUND_SELECT
579 selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
580 Select *pRhs = Z;
581 Select *pLhs = A;
582 if( pRhs && pRhs->pPrior ){
583 SrcList *pFrom;
584 Token x;
585 x.n = 0;
586 parserDoubleLinkSelect(pParse, pRhs);
587 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0);
588 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
590 if( pRhs ){
591 pRhs->op = (u8)Y;
592 pRhs->pPrior = pLhs;
593 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
594 pRhs->selFlags &= ~SF_MultiValue;
595 if( Y!=TK_ALL ) pParse->hasCompound = 1;
596 }else{
597 sqlite3SelectDelete(pParse->db, pLhs);
599 A = pRhs;
601 %type multiselect_op {int}
602 multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
603 multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
604 multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
605 %endif SQLITE_OMIT_COMPOUND_SELECT
607 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
608 groupby_opt(P) having_opt(Q)
609 orderby_opt(Z) limit_opt(L). {
610 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
612 %ifndef SQLITE_OMIT_WINDOWFUNC
613 oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
614 groupby_opt(P) having_opt(Q) window_clause(R)
615 orderby_opt(Z) limit_opt(L). {
616 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
617 if( A ){
618 A->pWinDefn = R;
619 }else{
620 sqlite3WindowListDelete(pParse->db, R);
623 %endif
626 // Single row VALUES clause.
628 %type values {Select*}
629 oneselect(A) ::= values(A).
630 %destructor values {sqlite3SelectDelete(pParse->db, $$);}
631 values(A) ::= VALUES LP nexprlist(X) RP. {
632 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
635 // Multiple row VALUES clause.
637 %type mvalues {Select*}
638 oneselect(A) ::= mvalues(A). {
639 sqlite3MultiValuesEnd(pParse, A);
641 %destructor mvalues {sqlite3SelectDelete(pParse->db, $$);}
642 mvalues(A) ::= values(A) COMMA LP nexprlist(Y) RP. {
643 A = sqlite3MultiValues(pParse, A, Y);
645 mvalues(A) ::= mvalues(A) COMMA LP nexprlist(Y) RP. {
646 A = sqlite3MultiValues(pParse, A, Y);
649 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
650 // present and false (0) if it is not.
652 %type distinct {int}
653 distinct(A) ::= DISTINCT. {A = SF_Distinct;}
654 distinct(A) ::= ALL. {A = SF_All;}
655 distinct(A) ::= . {A = 0;}
657 // selcollist is a list of expressions that are to become the return
658 // values of the SELECT statement. The "*" in statements like
659 // "SELECT * FROM ..." is encoded as a special expression with an
660 // opcode of TK_ASTERISK.
662 %type selcollist {ExprList*}
663 %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
664 %type sclp {ExprList*}
665 %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
666 sclp(A) ::= selcollist(A) COMMA.
667 sclp(A) ::= . {A = 0;}
668 selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
669 A = sqlite3ExprListAppend(pParse, A, X);
670 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
671 sqlite3ExprListSetSpan(pParse,A,B,Z);
673 selcollist(A) ::= sclp(A) scanpt STAR(X). {
674 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
675 sqlite3ExprSetErrorOffset(p, (int)(X.z - pParse->zTail));
676 A = sqlite3ExprListAppend(pParse, A, p);
678 selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR(Y). {
679 Expr *pRight, *pLeft, *pDot;
680 pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
681 sqlite3ExprSetErrorOffset(pRight, (int)(Y.z - pParse->zTail));
682 pLeft = tokenExpr(pParse, TK_ID, X);
683 pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
684 A = sqlite3ExprListAppend(pParse,A, pDot);
687 // An option "AS <id>" phrase that can follow one of the expressions that
688 // define the result set, or one of the tables in the FROM clause.
690 %type as {Token}
691 as(X) ::= AS nm(Y). {X = Y;}
692 as(X) ::= ids(X).
693 as(X) ::= . {X.n = 0; X.z = 0;}
696 %type seltablist {SrcList*}
697 %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
698 %type stl_prefix {SrcList*}
699 %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
700 %type from {SrcList*}
701 %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
703 // A complete FROM clause.
705 from(A) ::= . {A = 0;}
706 from(A) ::= FROM seltablist(X). {
707 A = X;
708 sqlite3SrcListShiftJoinType(pParse,A);
711 // "seltablist" is a "Select Table List" - the content of the FROM clause
712 // in a SELECT statement. "stl_prefix" is a prefix of this list.
714 stl_prefix(A) ::= seltablist(A) joinop(Y). {
715 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
717 stl_prefix(A) ::= . {A = 0;}
718 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) on_using(N). {
719 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
721 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_by(I) on_using(N). {
722 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
723 sqlite3SrcListIndexedBy(pParse, A, &I);
725 seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) on_using(N). {
726 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
727 sqlite3SrcListFuncArgs(pParse, A, E);
729 %ifndef SQLITE_OMIT_SUBQUERY
730 seltablist(A) ::= stl_prefix(A) LP select(S) RP as(Z) on_using(N). {
731 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,&N);
733 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP as(Z) on_using(N). {
734 if( A==0 && Z.n==0 && N.pOn==0 && N.pUsing==0 ){
735 A = F;
736 }else if( ALWAYS(F!=0) && F->nSrc==1 ){
737 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N);
738 if( A ){
739 SrcItem *pNew = &A->a[A->nSrc-1];
740 SrcItem *pOld = F->a;
741 pNew->zName = pOld->zName;
742 pNew->zDatabase = pOld->zDatabase;
743 pNew->pSelect = pOld->pSelect;
744 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
745 pNew->fg.isNestedFrom = 1;
747 if( pOld->fg.isTabFunc ){
748 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
749 pOld->u1.pFuncArg = 0;
750 pOld->fg.isTabFunc = 0;
751 pNew->fg.isTabFunc = 1;
753 pOld->zName = pOld->zDatabase = 0;
754 pOld->pSelect = 0;
756 sqlite3SrcListDelete(pParse->db, F);
757 }else{
758 Select *pSubquery;
759 sqlite3SrcListShiftJoinType(pParse,F);
760 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
761 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N);
764 %endif SQLITE_OMIT_SUBQUERY
766 %type dbnm {Token}
767 dbnm(A) ::= . {A.z=0; A.n=0;}
768 dbnm(A) ::= DOT nm(X). {A = X;}
770 %type fullname {SrcList*}
771 %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
772 fullname(A) ::= nm(X). {
773 A = sqlite3SrcListAppend(pParse,0,&X,0);
774 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);
776 fullname(A) ::= nm(X) DOT nm(Y). {
777 A = sqlite3SrcListAppend(pParse,0,&X,&Y);
778 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);
781 %type xfullname {SrcList*}
782 %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
783 xfullname(A) ::= nm(X).
784 {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/}
785 xfullname(A) ::= nm(X) DOT nm(Y).
786 {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/}
787 xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
788 A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/
789 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
791 xfullname(A) ::= nm(X) AS nm(Z). {
792 A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/
793 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
796 %type joinop {int}
797 joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
798 joinop(X) ::= JOIN_KW(A) JOIN.
799 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
800 joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
801 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
802 joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
803 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
805 // There is a parsing abiguity in an upsert statement that uses a
806 // SELECT on the RHS of a the INSERT:
808 // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
809 // here ----^^
811 // When the ON token is encountered, the parser does not know if it is
812 // the beginning of an ON CONFLICT clause, or the beginning of an ON
813 // clause associated with the JOIN. The conflict is resolved in favor
814 // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
815 // WHERE clause in between, like this:
817 // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
819 // The [AND] and [OR] precedence marks in the rules for on_using cause the
820 // ON in this context to always be interpreted as belonging to the JOIN.
822 %type on_using {OnOrUsing}
823 //%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);}
824 on_using(N) ::= ON expr(E). {N.pOn = E; N.pUsing = 0;}
825 on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;}
826 on_using(N) ::= . [OR] {N.pOn = 0; N.pUsing = 0;}
828 // Note that this block abuses the Token type just a little. If there is
829 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
830 // there is an INDEXED BY clause, then the token is populated as per normal,
831 // with z pointing to the token data and n containing the number of bytes
832 // in the token.
834 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
835 // normally illegal. The sqlite3SrcListIndexedBy() function
836 // recognizes and interprets this as a special case.
838 %type indexed_opt {Token}
839 %type indexed_by {Token}
840 indexed_opt(A) ::= . {A.z=0; A.n=0;}
841 indexed_opt(A) ::= indexed_by(A).
842 indexed_by(A) ::= INDEXED BY nm(X). {A = X;}
843 indexed_by(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
845 %type orderby_opt {ExprList*}
846 %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
848 // the sortlist non-terminal stores a list of expression where each
849 // expression is optionally followed by ASC or DESC to indicate the
850 // sort order.
852 %type sortlist {ExprList*}
853 %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
855 orderby_opt(A) ::= . {A = 0;}
856 orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
857 sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
858 A = sqlite3ExprListAppend(pParse,A,Y);
859 sqlite3ExprListSetSortOrder(A,Z,X);
861 sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
862 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
863 sqlite3ExprListSetSortOrder(A,Z,X);
866 %type sortorder {int}
868 sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
869 sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
870 sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
872 %type nulls {int}
873 nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;}
874 nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;}
875 nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;}
877 %type groupby_opt {ExprList*}
878 %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
879 groupby_opt(A) ::= . {A = 0;}
880 groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
882 %type having_opt {Expr*}
883 %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
884 having_opt(A) ::= . {A = 0;}
885 having_opt(A) ::= HAVING expr(X). {A = X;}
887 %type limit_opt {Expr*}
889 // The destructor for limit_opt will never fire in the current grammar.
890 // The limit_opt non-terminal only occurs at the end of a single production
891 // rule for SELECT statements. As soon as the rule that create the
892 // limit_opt non-terminal reduces, the SELECT statement rule will also
893 // reduce. So there is never a limit_opt non-terminal on the stack
894 // except as a transient. So there is never anything to destroy.
896 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
897 limit_opt(A) ::= . {A = 0;}
898 limit_opt(A) ::= LIMIT expr(X).
899 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
900 limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
901 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
902 limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
903 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
905 /////////////////////////// The DELETE statement /////////////////////////////
907 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
908 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W)
909 orderby_opt(O) limit_opt(L). {
910 sqlite3SrcListIndexedBy(pParse, X, &I);
911 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
912 if( O || L ){
913 updateDeleteLimitError(pParse,O,L);
914 O = 0;
915 L = 0;
917 #endif
918 sqlite3DeleteFrom(pParse,X,W,O,L);
920 %else
921 cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). {
922 sqlite3SrcListIndexedBy(pParse, X, &I);
923 sqlite3DeleteFrom(pParse,X,W,0,0);
925 %endif
927 %type where_opt {Expr*}
928 %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
929 %type where_opt_ret {Expr*}
930 %destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);}
932 where_opt(A) ::= . {A = 0;}
933 where_opt(A) ::= WHERE expr(X). {A = X;}
934 where_opt_ret(A) ::= . {A = 0;}
935 where_opt_ret(A) ::= WHERE expr(X). {A = X;}
936 where_opt_ret(A) ::= RETURNING selcollist(X).
937 {sqlite3AddReturning(pParse,X); A = 0;}
938 where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y).
939 {sqlite3AddReturning(pParse,Y); A = X;}
941 ////////////////////////// The UPDATE command ////////////////////////////////
943 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
944 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
945 where_opt_ret(W) orderby_opt(O) limit_opt(L). {
946 sqlite3SrcListIndexedBy(pParse, X, &I);
947 if( F ){
948 SrcList *pFromClause = F;
949 if( pFromClause->nSrc>1 ){
950 Select *pSubquery;
951 Token as;
952 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
953 as.n = 0;
954 as.z = 0;
955 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
957 X = sqlite3SrcListAppendList(pParse, X, pFromClause);
959 sqlite3ExprListCheckLength(pParse,Y,"set list");
960 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
961 if( O || L ){
962 updateDeleteLimitError(pParse,O,L);
963 O = 0;
964 L = 0;
966 #endif
967 sqlite3Update(pParse,X,Y,W,R,O,L,0);
969 %else
970 cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
971 where_opt_ret(W). {
972 sqlite3SrcListIndexedBy(pParse, X, &I);
973 sqlite3ExprListCheckLength(pParse,Y,"set list");
974 if( F ){
975 SrcList *pFromClause = F;
976 if( pFromClause->nSrc>1 ){
977 Select *pSubquery;
978 Token as;
979 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
980 as.n = 0;
981 as.z = 0;
982 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
984 X = sqlite3SrcListAppendList(pParse, X, pFromClause);
986 sqlite3Update(pParse,X,Y,W,R,0,0,0);
988 %endif
992 %type setlist {ExprList*}
993 %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
995 setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
996 A = sqlite3ExprListAppend(pParse, A, Y);
997 sqlite3ExprListSetName(pParse, A, &X, 1);
999 setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
1000 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
1002 setlist(A) ::= nm(X) EQ expr(Y). {
1003 A = sqlite3ExprListAppend(pParse, 0, Y);
1004 sqlite3ExprListSetName(pParse, A, &X, 1);
1006 setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
1007 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
1010 ////////////////////////// The INSERT command /////////////////////////////////
1012 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
1013 upsert(U). {
1014 sqlite3Insert(pParse, X, S, F, R, U);
1016 cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning.
1018 sqlite3Insert(pParse, X, 0, F, R, 0);
1021 %type upsert {Upsert*}
1023 // Because upsert only occurs at the tip end of the INSERT rule for cmd,
1024 // there is never a case where the value of the upsert pointer will not
1025 // be destroyed by the cmd action. So comment-out the destructor to
1026 // avoid unreachable code.
1027 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
1028 upsert(A) ::= . { A = 0; }
1029 upsert(A) ::= RETURNING selcollist(X). { A = 0; sqlite3AddReturning(pParse,X); }
1030 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
1031 DO UPDATE SET setlist(Z) where_opt(W) upsert(N).
1032 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);}
1033 upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N).
1034 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); }
1035 upsert(A) ::= ON CONFLICT DO NOTHING returning.
1036 { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
1037 upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning.
1038 { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);}
1040 returning ::= RETURNING selcollist(X). {sqlite3AddReturning(pParse,X);}
1041 returning ::= .
1043 %type insert_cmd {int}
1044 insert_cmd(A) ::= INSERT orconf(R). {A = R;}
1045 insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
1047 %type idlist_opt {IdList*}
1048 %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
1049 %type idlist {IdList*}
1050 %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
1052 idlist_opt(A) ::= . {A = 0;}
1053 idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
1054 idlist(A) ::= idlist(A) COMMA nm(Y).
1055 {A = sqlite3IdListAppend(pParse,A,&Y);}
1056 idlist(A) ::= nm(Y).
1057 {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}
1059 /////////////////////////// Expression Processing /////////////////////////////
1062 %type expr {Expr*}
1063 %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
1064 %type term {Expr*}
1065 %destructor term {sqlite3ExprDelete(pParse->db, $$);}
1067 %include {
1069 /* Construct a new Expr object from a single token */
1070 static Expr *tokenExpr(Parse *pParse, int op, Token t){
1071 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
1072 if( p ){
1073 /* memset(p, 0, sizeof(Expr)); */
1074 p->op = (u8)op;
1075 p->affExpr = 0;
1076 p->flags = EP_Leaf;
1077 ExprClearVVAProperties(p);
1078 /* p->iAgg = -1; // Not required */
1079 p->pLeft = p->pRight = 0;
1080 p->pAggInfo = 0;
1081 memset(&p->x, 0, sizeof(p->x));
1082 memset(&p->y, 0, sizeof(p->y));
1083 p->op2 = 0;
1084 p->iTable = 0;
1085 p->iColumn = 0;
1086 p->u.zToken = (char*)&p[1];
1087 memcpy(p->u.zToken, t.z, t.n);
1088 p->u.zToken[t.n] = 0;
1089 p->w.iOfst = (int)(t.z - pParse->zTail);
1090 if( sqlite3Isquote(p->u.zToken[0]) ){
1091 sqlite3DequoteExpr(p);
1093 #if SQLITE_MAX_EXPR_DEPTH>0
1094 p->nHeight = 1;
1095 #endif
1096 if( IN_RENAME_OBJECT ){
1097 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
1100 return p;
1105 expr(A) ::= term(A).
1106 expr(A) ::= LP expr(X) RP. {A = X;}
1107 expr(A) ::= idj(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
1108 expr(A) ::= nm(X) DOT nm(Y). {
1109 Expr *temp1 = tokenExpr(pParse,TK_ID,X);
1110 Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
1111 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
1113 expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
1114 Expr *temp1 = tokenExpr(pParse,TK_ID,X);
1115 Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
1116 Expr *temp3 = tokenExpr(pParse,TK_ID,Z);
1117 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
1118 if( IN_RENAME_OBJECT ){
1119 sqlite3RenameTokenRemap(pParse, 0, temp1);
1121 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
1123 term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1124 term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1125 term(A) ::= INTEGER(X). {
1126 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
1127 if( A ) A->w.iOfst = (int)(X.z - pParse->zTail);
1129 expr(A) ::= VARIABLE(X). {
1130 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
1131 u32 n = X.n;
1132 A = tokenExpr(pParse, TK_VARIABLE, X);
1133 sqlite3ExprAssignVarNumber(pParse, A, n);
1134 }else{
1135 /* When doing a nested parse, one can include terms in an expression
1136 ** that look like this: #1 #2 ... These terms refer to registers
1137 ** in the virtual machine. #N is the N-th register. */
1138 Token t = X; /*A-overwrites-X*/
1139 assert( t.n>=2 );
1140 if( pParse->nested==0 ){
1141 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
1142 A = 0;
1143 }else{
1144 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1145 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
1149 expr(A) ::= expr(A) COLLATE ids(C). {
1150 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
1152 %ifndef SQLITE_OMIT_CAST
1153 expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1154 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1155 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
1157 %endif SQLITE_OMIT_CAST
1160 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP. {
1161 A = sqlite3ExprFunction(pParse, Y, &X, D);
1163 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP. {
1164 A = sqlite3ExprFunction(pParse, Y, &X, D);
1165 sqlite3ExprAddFunctionOrderBy(pParse, A, O);
1167 expr(A) ::= idj(X) LP STAR RP. {
1168 A = sqlite3ExprFunction(pParse, 0, &X, 0);
1171 %ifndef SQLITE_OMIT_WINDOWFUNC
1172 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
1173 A = sqlite3ExprFunction(pParse, Y, &X, D);
1174 sqlite3WindowAttach(pParse, A, Z);
1176 expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP filter_over(Z). {
1177 A = sqlite3ExprFunction(pParse, Y, &X, D);
1178 sqlite3WindowAttach(pParse, A, Z);
1179 sqlite3ExprAddFunctionOrderBy(pParse, A, O);
1181 expr(A) ::= idj(X) LP STAR RP filter_over(Z). {
1182 A = sqlite3ExprFunction(pParse, 0, &X, 0);
1183 sqlite3WindowAttach(pParse, A, Z);
1185 %endif
1187 term(A) ::= CTIME_KW(OP). {
1188 A = sqlite3ExprFunction(pParse, 0, &OP, 0);
1191 expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1192 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1193 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1194 if( A ){
1195 A->x.pList = pList;
1196 if( ALWAYS(pList->nExpr) ){
1197 A->flags |= pList->a[0].pExpr->flags & EP_Propagate;
1199 }else{
1200 sqlite3ExprListDelete(pParse->db, pList);
1204 expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);}
1205 expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1206 expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
1207 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1208 expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1209 expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
1210 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1211 expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
1212 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1213 expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
1214 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1215 expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1216 %type likeop {Token}
1217 likeop(A) ::= LIKE_KW|MATCH(A).
1218 likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
1219 expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
1220 ExprList *pList;
1221 int bNot = OP.n & 0x80000000;
1222 OP.n &= 0x7fffffff;
1223 pList = sqlite3ExprListAppend(pParse,0, Y);
1224 pList = sqlite3ExprListAppend(pParse,pList, A);
1225 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1226 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1227 if( A ) A->flags |= EP_InfixFunc;
1229 expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
1230 ExprList *pList;
1231 int bNot = OP.n & 0x80000000;
1232 OP.n &= 0x7fffffff;
1233 pList = sqlite3ExprListAppend(pParse,0, Y);
1234 pList = sqlite3ExprListAppend(pParse,pList, A);
1235 pList = sqlite3ExprListAppend(pParse,pList, E);
1236 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
1237 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1238 if( A ) A->flags |= EP_InfixFunc;
1241 expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1242 expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
1244 %include {
1245 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1246 ** unary TK_ISNULL or TK_NOTNULL expression. */
1247 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1248 sqlite3 *db = pParse->db;
1249 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
1250 pA->op = (u8)op;
1251 sqlite3ExprDelete(db, pA->pRight);
1252 pA->pRight = 0;
1257 // expr1 IS expr2
1258 // expr1 IS NOT expr2
1260 // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1261 // is any other expression, code as TK_IS or TK_ISNOT.
1263 expr(A) ::= expr(A) IS expr(Y). {
1264 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1265 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1267 expr(A) ::= expr(A) IS NOT expr(Y). {
1268 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1269 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1271 expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y). {
1272 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1273 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1275 expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). {
1276 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1277 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1280 expr(A) ::= NOT(B) expr(X).
1281 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1282 expr(A) ::= BITNOT(B) expr(X).
1283 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
1284 expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1285 Expr *p = X;
1286 u8 op = @B + (TK_UPLUS-TK_PLUS);
1287 assert( TK_UPLUS>TK_PLUS );
1288 assert( TK_UMINUS == TK_MINUS + (TK_UPLUS - TK_PLUS) );
1289 if( p && p->op==TK_UPLUS ){
1290 p->op = op;
1291 A = p;
1292 }else{
1293 A = sqlite3PExpr(pParse, op, p, 0);
1294 /*A-overwrites-B*/
1298 expr(A) ::= expr(B) PTR(C) expr(D). {
1299 ExprList *pList = sqlite3ExprListAppend(pParse, 0, B);
1300 pList = sqlite3ExprListAppend(pParse, pList, D);
1301 A = sqlite3ExprFunction(pParse, pList, &C, 0);
1304 %type between_op {int}
1305 between_op(A) ::= BETWEEN. {A = 0;}
1306 between_op(A) ::= NOT BETWEEN. {A = 1;}
1307 expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
1308 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1309 pList = sqlite3ExprListAppend(pParse,pList, Y);
1310 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1311 if( A ){
1312 A->x.pList = pList;
1313 }else{
1314 sqlite3ExprListDelete(pParse->db, pList);
1316 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1318 %ifndef SQLITE_OMIT_SUBQUERY
1319 %type in_op {int}
1320 in_op(A) ::= IN. {A = 0;}
1321 in_op(A) ::= NOT IN. {A = 1;}
1322 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
1323 if( Y==0 ){
1324 /* Expressions of the form
1326 ** expr1 IN ()
1327 ** expr1 NOT IN ()
1329 ** simplify to constants 0 (false) and 1 (true), respectively,
1330 ** regardless of the value of expr1.
1332 sqlite3ExprUnmapAndDelete(pParse, A);
1333 A = sqlite3Expr(pParse->db, TK_STRING, N ? "true" : "false");
1334 if( A ) sqlite3ExprIdToTrueFalse(A);
1335 }else{
1336 Expr *pRHS = Y->a[0].pExpr;
1337 if( Y->nExpr==1 && sqlite3ExprIsConstant(pParse,pRHS) && A->op!=TK_VECTOR ){
1338 Y->a[0].pExpr = 0;
1339 sqlite3ExprListDelete(pParse->db, Y);
1340 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
1341 A = sqlite3PExpr(pParse, TK_EQ, A, pRHS);
1342 }else if( Y->nExpr==1 && pRHS->op==TK_SELECT ){
1343 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1344 sqlite3PExprAddSelect(pParse, A, pRHS->x.pSelect);
1345 pRHS->x.pSelect = 0;
1346 sqlite3ExprListDelete(pParse->db, Y);
1347 }else{
1348 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1349 if( A==0 ){
1350 sqlite3ExprListDelete(pParse->db, Y);
1351 }else if( A->pLeft->op==TK_VECTOR ){
1352 int nExpr = A->pLeft->x.pList->nExpr;
1353 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y);
1354 if( pSelectRHS ){
1355 parserDoubleLinkSelect(pParse, pSelectRHS);
1356 sqlite3PExprAddSelect(pParse, A, pSelectRHS);
1358 }else{
1359 A->x.pList = Y;
1360 sqlite3ExprSetHeightAndFlags(pParse, A);
1363 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1366 expr(A) ::= LP select(X) RP. {
1367 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1368 sqlite3PExprAddSelect(pParse, A, X);
1370 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1371 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1372 sqlite3PExprAddSelect(pParse, A, Y);
1373 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1375 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
1376 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
1377 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
1378 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
1379 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1380 sqlite3PExprAddSelect(pParse, A, pSelect);
1381 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1383 expr(A) ::= EXISTS LP select(Y) RP. {
1384 Expr *p;
1385 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
1386 sqlite3PExprAddSelect(pParse, p, Y);
1388 %endif SQLITE_OMIT_SUBQUERY
1390 /* CASE expressions */
1391 expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1392 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1393 if( A ){
1394 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1395 sqlite3ExprSetHeightAndFlags(pParse, A);
1396 }else{
1397 sqlite3ExprListDelete(pParse->db, Y);
1398 sqlite3ExprDelete(pParse->db, Z);
1401 %type case_exprlist {ExprList*}
1402 %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1403 case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
1404 A = sqlite3ExprListAppend(pParse,A, Y);
1405 A = sqlite3ExprListAppend(pParse,A, Z);
1407 case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
1408 A = sqlite3ExprListAppend(pParse,0, Y);
1409 A = sqlite3ExprListAppend(pParse,A, Z);
1411 %type case_else {Expr*}
1412 %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
1413 case_else(A) ::= ELSE expr(X). {A = X;}
1414 case_else(A) ::= . {A = 0;}
1415 %type case_operand {Expr*}
1416 %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
1417 case_operand(A) ::= expr(A).
1418 case_operand(A) ::= . {A = 0;}
1420 %type exprlist {ExprList*}
1421 %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1422 %type nexprlist {ExprList*}
1423 %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
1425 exprlist(A) ::= nexprlist(A).
1426 exprlist(A) ::= . {A = 0;}
1427 nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
1428 {A = sqlite3ExprListAppend(pParse,A,Y);}
1429 nexprlist(A) ::= expr(Y).
1430 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
1432 %ifndef SQLITE_OMIT_SUBQUERY
1433 /* A paren_exprlist is an optional expression list contained inside
1434 ** of parenthesis */
1435 %type paren_exprlist {ExprList*}
1436 %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1437 paren_exprlist(A) ::= . {A = 0;}
1438 paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
1439 %endif SQLITE_OMIT_SUBQUERY
1442 ///////////////////////////// The CREATE INDEX command ///////////////////////
1444 cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
1445 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
1446 sqlite3CreateIndex(pParse, &X, &D,
1447 sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
1448 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
1449 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
1450 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
1454 %type uniqueflag {int}
1455 uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1456 uniqueflag(A) ::= . {A = OE_None;}
1459 // The eidlist non-terminal (Expression Id List) generates an ExprList
1460 // from a list of identifiers. The identifier names are in ExprList.a[].zName.
1461 // This list is stored in an ExprList rather than an IdList so that it
1462 // can be easily sent to sqlite3ColumnsExprList().
1464 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1465 // used for the arguments to an index. That is just an historical accident.
1467 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1468 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1469 // places - places that might have been stored in the sqlite_schema table.
1470 // Those extra features were ignored. But because they might be in some
1471 // (busted) old databases, we need to continue parsing them when loading
1472 // historical schemas.
1474 %type eidlist {ExprList*}
1475 %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1476 %type eidlist_opt {ExprList*}
1477 %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1479 %include {
1480 /* Add a single new term to an ExprList that is used to store a
1481 ** list of identifiers. Report an error if the ID list contains
1482 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1483 ** error while parsing a legacy schema.
1485 static ExprList *parserAddExprIdListTerm(
1486 Parse *pParse,
1487 ExprList *pPrior,
1488 Token *pIdToken,
1489 int hasCollate,
1490 int sortOrder
1492 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1493 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1494 && pParse->db->init.busy==0
1496 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1497 pIdToken->n, pIdToken->z);
1499 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1500 return p;
1502 } // end %include
1504 eidlist_opt(A) ::= . {A = 0;}
1505 eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
1506 eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1507 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
1509 eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
1510 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
1513 %type collate {int}
1514 collate(C) ::= . {C = 0;}
1515 collate(C) ::= COLLATE ids. {C = 1;}
1518 ///////////////////////////// The DROP INDEX command /////////////////////////
1520 cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
1522 ///////////////////////////// The VACUUM command /////////////////////////////
1524 %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH
1525 %type vinto {Expr*}
1526 %destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
1527 cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);}
1528 cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);}
1529 vinto(A) ::= INTO expr(X). {A = X;}
1530 vinto(A) ::= . {A = 0;}
1531 %endif
1533 ///////////////////////////// The PRAGMA command /////////////////////////////
1535 %ifndef SQLITE_OMIT_PRAGMA
1536 cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1537 cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1538 cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
1539 cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1540 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1541 cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1542 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1544 nmnum(A) ::= plus_num(A).
1545 nmnum(A) ::= nm(A).
1546 nmnum(A) ::= ON(A).
1547 nmnum(A) ::= DELETE(A).
1548 nmnum(A) ::= DEFAULT(A).
1549 %endif SQLITE_OMIT_PRAGMA
1550 %token_class number INTEGER|FLOAT.
1551 plus_num(A) ::= PLUS number(X). {A = X;}
1552 plus_num(A) ::= number(A).
1553 minus_num(A) ::= MINUS number(X). {A = X;}
1554 //////////////////////////// The CREATE TRIGGER command /////////////////////
1556 %ifndef SQLITE_OMIT_TRIGGER
1558 cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
1559 Token all;
1560 all.z = A.z;
1561 all.n = (int)(Z.z - A.z) + Z.n;
1562 sqlite3FinishTrigger(pParse, S, &all);
1565 trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1566 trigger_time(C) trigger_event(D)
1567 ON fullname(E) foreach_clause when_clause(G). {
1568 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
1569 A = (Z.n==0?B:Z); /*A-overwrites-T*/
1572 %type trigger_time {int}
1573 trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
1574 trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1575 trigger_time(A) ::= . { A = TK_BEFORE; }
1577 %type trigger_event {struct TrigEvent}
1578 %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
1579 trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1580 trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1581 trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
1583 foreach_clause ::= .
1584 foreach_clause ::= FOR EACH ROW.
1586 %type when_clause {Expr*}
1587 %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
1588 when_clause(A) ::= . { A = 0; }
1589 when_clause(A) ::= WHEN expr(X). { A = X; }
1591 %type trigger_cmd_list {TriggerStep*}
1592 %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
1593 trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1594 assert( A!=0 );
1595 A->pLast->pNext = X;
1596 A->pLast = X;
1598 trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1599 assert( A!=0 );
1600 A->pLast = A;
1603 // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1604 // within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1605 // the same database as the table that the trigger fires on.
1607 %type trnm {Token}
1608 trnm(A) ::= nm(A).
1609 trnm(A) ::= nm DOT nm(X). {
1610 A = X;
1611 sqlite3ErrorMsg(pParse,
1612 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1613 "statements within triggers");
1616 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1617 // statements within triggers. We make a specific error message for this
1618 // since it is an exception to the default grammar rules.
1620 tridxby ::= .
1621 tridxby ::= INDEXED BY nm. {
1622 sqlite3ErrorMsg(pParse,
1623 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1624 "within triggers");
1626 tridxby ::= NOT INDEXED. {
1627 sqlite3ErrorMsg(pParse,
1628 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1629 "within triggers");
1634 %type trigger_cmd {TriggerStep*}
1635 %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
1636 // UPDATE
1637 trigger_cmd(A) ::=
1638 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E).
1639 {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);}
1641 // INSERT
1642 trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
1643 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
1644 A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
1646 // DELETE
1647 trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1648 {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
1650 // SELECT
1651 trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1652 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
1654 // The special RAISE expression that may occur in trigger programs
1655 expr(A) ::= RAISE LP IGNORE RP. {
1656 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1657 if( A ){
1658 A->affExpr = OE_Ignore;
1661 expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1662 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1663 if( A ) {
1664 A->affExpr = (char)T;
1667 %endif !SQLITE_OMIT_TRIGGER
1669 %type raisetype {int}
1670 raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1671 raisetype(A) ::= ABORT. {A = OE_Abort;}
1672 raisetype(A) ::= FAIL. {A = OE_Fail;}
1675 //////////////////////// DROP TRIGGER statement //////////////////////////////
1676 %ifndef SQLITE_OMIT_TRIGGER
1677 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1678 sqlite3DropTrigger(pParse,X,NOERR);
1680 %endif !SQLITE_OMIT_TRIGGER
1682 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1683 %ifndef SQLITE_OMIT_ATTACH
1684 cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
1685 sqlite3Attach(pParse, F, D, K);
1687 cmd ::= DETACH database_kw_opt expr(D). {
1688 sqlite3Detach(pParse, D);
1691 %type key_opt {Expr*}
1692 %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
1693 key_opt(A) ::= . { A = 0; }
1694 key_opt(A) ::= KEY expr(X). { A = X; }
1696 database_kw_opt ::= DATABASE.
1697 database_kw_opt ::= .
1698 %endif SQLITE_OMIT_ATTACH
1700 ////////////////////////// REINDEX collation //////////////////////////////////
1701 %ifndef SQLITE_OMIT_REINDEX
1702 cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1703 cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
1704 %endif SQLITE_OMIT_REINDEX
1706 /////////////////////////////////// ANALYZE ///////////////////////////////////
1707 %ifndef SQLITE_OMIT_ANALYZE
1708 cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1709 cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1710 %endif
1712 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1713 %ifndef SQLITE_OMIT_ALTERTABLE
1714 %ifndef SQLITE_OMIT_VIRTUALTABLE
1715 cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1716 sqlite3AlterRenameTable(pParse,X,&Z);
1718 cmd ::= ALTER TABLE add_column_fullname
1719 ADD kwcolumn_opt columnname(Y) carglist. {
1720 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
1721 sqlite3AlterFinishAddColumn(pParse, &Y);
1723 cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). {
1724 sqlite3AlterDropColumn(pParse, X, &Y);
1727 add_column_fullname ::= fullname(X). {
1728 disableLookaside(pParse);
1729 sqlite3AlterBeginAddColumn(pParse, X);
1731 cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
1732 sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
1735 kwcolumn_opt ::= .
1736 kwcolumn_opt ::= COLUMNKW.
1738 %endif SQLITE_OMIT_VIRTUALTABLE
1739 %endif SQLITE_OMIT_ALTERTABLE
1741 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1742 %ifndef SQLITE_OMIT_VIRTUALTABLE
1743 cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1744 cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
1745 create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1746 nm(X) dbnm(Y) USING nm(Z). {
1747 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
1749 vtabarglist ::= vtabarg.
1750 vtabarglist ::= vtabarglist COMMA vtabarg.
1751 vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1752 vtabarg ::= vtabarg vtabargtoken.
1753 vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1754 vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1755 lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1756 anylist ::= .
1757 anylist ::= anylist LP anylist RP.
1758 anylist ::= anylist ANY.
1759 %endif SQLITE_OMIT_VIRTUALTABLE
1762 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1763 %type wqlist {With*}
1764 %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
1765 %type wqitem {Cte*}
1766 // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable
1768 with ::= .
1769 %ifndef SQLITE_OMIT_CTE
1770 with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1771 with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1773 %type wqas {u8}
1774 wqas(A) ::= AS. {A = M10d_Any;}
1775 wqas(A) ::= AS MATERIALIZED. {A = M10d_Yes;}
1776 wqas(A) ::= AS NOT MATERIALIZED. {A = M10d_No;}
1777 wqitem(A) ::= withnm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. {
1778 A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/
1780 withnm(A) ::= nm(A). {pParse->bHasWith = 1;}
1781 wqlist(A) ::= wqitem(X). {
1782 A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/
1784 wqlist(A) ::= wqlist(A) COMMA wqitem(X). {
1785 A = sqlite3WithAdd(pParse, A, X);
1787 %endif SQLITE_OMIT_CTE
1789 //////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
1790 // These must be at the end of this file. Specifically, the rules that
1791 // introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1792 // the integer values assigned to these tokens to be larger than all other
1793 // tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
1795 %ifndef SQLITE_OMIT_WINDOWFUNC
1796 %type windowdefn_list {Window*}
1797 %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
1798 windowdefn_list(A) ::= windowdefn(A).
1799 windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
1800 assert( Z!=0 );
1801 sqlite3WindowChain(pParse, Z, Y);
1802 Z->pNextWin = Y;
1803 A = Z;
1806 %type windowdefn {Window*}
1807 %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
1808 windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
1809 if( ALWAYS(Y) ){
1810 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1812 A = Y;
1815 %type window {Window*}
1816 %destructor window {sqlite3WindowDelete(pParse->db, $$);}
1818 %type frame_opt {Window*}
1819 %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1821 %type part_opt {ExprList*}
1822 %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1824 %type filter_clause {Expr*}
1825 %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
1827 %type over_clause {Window*}
1828 %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
1830 %type filter_over {Window*}
1831 %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
1833 %type range_or_rows {int}
1835 %type frame_bound {struct FrameBound}
1836 %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1837 %type frame_bound_s {struct FrameBound}
1838 %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1839 %type frame_bound_e {struct FrameBound}
1840 %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1842 window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1843 A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
1845 window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1846 A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
1848 window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
1849 A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
1851 window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
1852 A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
1854 window(A) ::= frame_opt(A).
1855 window(A) ::= nm(W) frame_opt(Z). {
1856 A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
1859 frame_opt(A) ::= . {
1860 A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
1862 frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). {
1863 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
1865 frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
1866 frame_bound_e(Z) frame_exclude_opt(W). {
1867 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
1870 range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/}
1872 frame_bound_s(A) ::= frame_bound(X). {A = X;}
1873 frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
1874 frame_bound_e(A) ::= frame_bound(X). {A = X;}
1875 frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
1877 frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
1878 {A.eType = @Y; A.pExpr = X;}
1879 frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;}
1881 %type frame_exclude_opt {u8}
1882 frame_exclude_opt(A) ::= . {A = 0;}
1883 frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
1885 %type frame_exclude {u8}
1886 frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/}
1887 frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
1888 frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/}
1891 %type window_clause {Window*}
1892 %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
1893 window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
1895 filter_over(A) ::= filter_clause(F) over_clause(O). {
1896 if( O ){
1897 O->pFilter = F;
1898 }else{
1899 sqlite3ExprDelete(pParse->db, F);
1901 A = O;
1903 filter_over(A) ::= over_clause(O). {
1904 A = O;
1906 filter_over(A) ::= filter_clause(F). {
1907 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1908 if( A ){
1909 A->eFrmType = TK_FILTER;
1910 A->pFilter = F;
1911 }else{
1912 sqlite3ExprDelete(pParse->db, F);
1916 over_clause(A) ::= OVER LP window(Z) RP. {
1917 A = Z;
1918 assert( A!=0 );
1920 over_clause(A) ::= OVER nm(Z). {
1921 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1922 if( A ){
1923 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
1927 filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
1928 %endif /* SQLITE_OMIT_WINDOWFUNC */
1931 ** The code generator needs some extra TK_ token values for tokens that
1932 ** are synthesized and do not actually appear in the grammar:
1934 %token
1935 COLUMN /* Reference to a table column */
1936 AGG_FUNCTION /* An aggregate function */
1937 AGG_COLUMN /* An aggregated column */
1938 TRUEFALSE /* True or false keyword */
1939 ISNOT /* Combination of IS and NOT */
1940 FUNCTION /* A function invocation */
1941 UPLUS /* Unary plus */
1942 UMINUS /* Unary minus */
1943 TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
1944 REGISTER /* Reference to a VDBE register */
1945 VECTOR /* Vector */
1946 SELECT_COLUMN /* Choose a single column from a multi-column SELECT */
1947 IF_NULL_ROW /* the if-null-row operator */
1948 ASTERISK /* The "*" in count(*) and similar */
1949 SPAN /* The span operator */
1950 ERROR /* An expression containing an error */
1953 term(A) ::= QNUMBER(X). {
1954 A=tokenExpr(pParse,@X,X);
1955 sqlite3DequoteNumber(pParse, A);
1958 /* There must be no more than 255 tokens defined above. If this grammar
1959 ** is extended with new rules and tokens, they must either be so few in
1960 ** number that TK_SPAN is no more than 255, or else the new tokens must
1961 ** appear after this line.
1963 %include {
1964 #if TK_SPAN>255
1965 # error too many tokens in the grammar
1966 #endif
1970 ** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens. The
1971 ** parser depends on this. Those tokens are not used in any grammar rule.
1972 ** They are only used by the tokenizer. Declare them last so that they
1973 ** are guaranteed to be the last two tokens
1975 %token SPACE ILLEGAL.