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