blob: 42efe6fb6fd6ba21e43fa4eda8c166feb10063f5 [file] [log] [blame]
drh348784e2000-05-29 20:41:49 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh348784e2000-05-29 20:41:49 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh348784e2000-05-29 20:41:49 +000010**
11*************************************************************************
12** This file contains SQLite's grammar for SQL. Process this file
13** using the lemon parser generator to generate C code that runs
14** the parser. Lemon will also generate a header file containing
15** numeric codes for all of the tokens.
drh348784e2000-05-29 20:41:49 +000016*/
drh487e2622005-06-25 18:42:14 +000017
18// All token codes are small integers with #defines that begin with "TK_"
drh348784e2000-05-29 20:41:49 +000019%token_prefix TK_
drh487e2622005-06-25 18:42:14 +000020
21// The type of the data attached to each token is Token. This is also the
22// default type for non-terminals.
23//
drh348784e2000-05-29 20:41:49 +000024%token_type {Token}
drhf57b14a2001-09-14 18:54:08 +000025%default_type {Token}
drh487e2622005-06-25 18:42:14 +000026
drhfb32c442018-04-21 13:51:42 +000027// An extra argument to the constructor for the parser, which is available
28// to all actions.
29%extra_context {Parse *pParse}
drh487e2622005-06-25 18:42:14 +000030
31// This code runs whenever there is a syntax error
32//
drh348784e2000-05-29 20:41:49 +000033%syntax_error {
drh128255f2008-12-08 16:01:12 +000034 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
drh6116ee42018-01-10 00:40:06 +000035 if( TOKEN.z[0] ){
36 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
37 }else{
38 sqlite3ErrorMsg(pParse, "incomplete input");
39 }
drh348784e2000-05-29 20:41:49 +000040}
drh8fc33452006-02-27 21:58:07 +000041%stack_overflow {
42 sqlite3ErrorMsg(pParse, "parser stack overflow");
43}
drh487e2622005-06-25 18:42:14 +000044
45// The name of the generated procedure that implements the parser
46// is as follows:
danielk19774adee202004-05-08 08:23:19 +000047%name sqlite3Parser
drh487e2622005-06-25 18:42:14 +000048
49// The following text is included near the beginning of the C source
50// code file that implements the parser.
51//
drh348784e2000-05-29 20:41:49 +000052%include {
53#include "sqliteInt.h"
drh9bbca4c2001-11-06 04:00:18 +000054
55/*
drhd3ec02d2009-06-12 02:27:14 +000056** Disable all error recovery processing in the parser push-down
57** automaton.
58*/
59#define YYNOERRORRECOVERY 1
60
61/*
drh8a415d32009-06-12 13:53:51 +000062** Make yytestcase() the same as testcase()
63*/
64#define yytestcase(X) testcase(X)
65
66/*
drh82415f22015-11-09 19:33:42 +000067** Indicate that sqlite3ParserFree() will never be called with a null
68** pointer.
69*/
drh644f4c12015-11-12 15:04:05 +000070#define YYPARSEFREENEVERNULL 1
drh82415f22015-11-09 19:33:42 +000071
72/*
drhd26cc542017-01-28 20:46:37 +000073** In the amalgamation, the parse.c file generated by lemon and the
74** tokenize.c file are concatenated. In that case, sqlite3RunParser()
75** has access to the the size of the yyParser object and so the parser
76** engine can be allocated from stack. In that case, only the
77** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
78** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
79** omitted.
80*/
81#ifdef SQLITE_AMALGAMATION
82# define sqlite3Parser_ENGINEALWAYSONSTACK 1
83#endif
84
85/*
drh82415f22015-11-09 19:33:42 +000086** Alternative datatype for the argument to the malloc() routine passed
87** into sqlite3ParserAlloc(). The default is size_t.
88*/
89#define YYMALLOCARGTYPE u64
90
91/*
drhad3cab52002-05-24 02:04:32 +000092** An instance of the following structure describes the event of a
93** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
94** TK_DELETE, or TK_INSTEAD. If the event is of the form
95**
96** UPDATE ON (a,b,c)
97**
98** Then the "b" IdList records the list "a,b,c".
danielk1977c3f9bad2002-05-15 08:30:12 +000099*/
drhad3cab52002-05-24 02:04:32 +0000100struct TrigEvent { int a; IdList * b; };
drhcaec2f12003-01-07 02:47:47 +0000101
dan86fb6e12018-05-16 20:58:07 +0000102struct FrameBound { int eType; Expr *pExpr; };
103
drh25d65432004-07-22 15:02:25 +0000104/*
drh4a642b62016-02-05 01:55:27 +0000105** Disable lookaside memory allocation for objects that might be
106** shared across database connections.
107*/
108static void disableLookaside(Parse *pParse){
109 pParse->disableLookaside++;
110 pParse->db->lookaside.bDisable++;
111}
112
drhcaec2f12003-01-07 02:47:47 +0000113} // end %include
drh348784e2000-05-29 20:41:49 +0000114
drh826fb5a2004-02-14 23:59:57 +0000115// Input is a single SQL command
drhc4a3c772001-04-04 11:48:57 +0000116input ::= cmdlist.
drh094b2bb2002-03-13 18:54:07 +0000117cmdlist ::= cmdlist ecmd.
drh826fb5a2004-02-14 23:59:57 +0000118cmdlist ::= ecmd.
drhb7f91642004-10-31 02:22:47 +0000119ecmd ::= SEMI.
drh2424aa72018-04-11 17:10:54 +0000120ecmd ::= cmdx SEMI.
drhb7f91642004-10-31 02:22:47 +0000121%ifndef SQLITE_OMIT_EXPLAIN
drh2424aa72018-04-11 17:10:54 +0000122ecmd ::= explain cmdx.
drh8549d552016-01-07 17:09:43 +0000123explain ::= EXPLAIN. { pParse->explain = 1; }
124explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; }
drh154d4b22006-09-21 11:02:16 +0000125%endif SQLITE_OMIT_EXPLAIN
drh200a81d2008-08-08 14:19:41 +0000126cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
drh348784e2000-05-29 20:41:49 +0000127
drh382c0242001-10-06 16:33:02 +0000128///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +0000129//
drhfa86c412002-02-02 15:01:15 +0000130
drh684917c2004-10-05 02:41:42 +0000131cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
drhc4a3c772001-04-04 11:48:57 +0000132trans_opt ::= .
133trans_opt ::= TRANSACTION.
drh5ad1a6c2002-07-01 12:27:09 +0000134trans_opt ::= TRANSACTION nm.
drh684917c2004-10-05 02:41:42 +0000135%type transtype {int}
136transtype(A) ::= . {A = TK_DEFERRED;}
drhcf82f0d2016-02-17 04:33:10 +0000137transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/}
138transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
139transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
drh07a3b112017-07-06 01:28:02 +0000140cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
141cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
drhc4a3c772001-04-04 11:48:57 +0000142
danielk1977fd7f0452008-12-17 17:30:26 +0000143savepoint_opt ::= SAVEPOINT.
144savepoint_opt ::= .
145cmd ::= SAVEPOINT nm(X). {
146 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
147}
148cmd ::= RELEASE savepoint_opt nm(X). {
149 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
150}
151cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
152 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
153}
154
drh382c0242001-10-06 16:33:02 +0000155///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +0000156//
157cmd ::= create_table create_table_args.
drhd9da78a2009-03-24 15:08:09 +0000158create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
danielk1977f1a381e2006-06-16 08:01:02 +0000159 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
drh969fa7c2002-02-18 18:30:32 +0000160}
drhdabd04c2016-02-17 01:46:19 +0000161createkw(A) ::= CREATE(A). {disableLookaside(pParse);}
162
drhfaa59552005-12-29 23:33:54 +0000163%type ifnotexists {int}
164ifnotexists(A) ::= . {A = 0;}
165ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
drhf57b3392001-10-08 13:22:32 +0000166%type temp {int}
danielk197753c0f742005-03-29 03:10:59 +0000167%ifndef SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000168temp(A) ::= TEMP. {A = 1;}
drh154d4b22006-09-21 11:02:16 +0000169%endif SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000170temp(A) ::= . {A = 0;}
drh5969da42013-10-21 02:14:45 +0000171create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
172 sqlite3EndTable(pParse,&X,&E,F,0);
drh969fa7c2002-02-18 18:30:32 +0000173}
174create_table_args ::= AS select(S). {
drh5969da42013-10-21 02:14:45 +0000175 sqlite3EndTable(pParse,0,0,0,S);
drh633e6d52008-07-28 19:34:53 +0000176 sqlite3SelectDelete(pParse->db, S);
drh969fa7c2002-02-18 18:30:32 +0000177}
drh3334d082015-11-10 13:45:21 +0000178%type table_options {int}
drh5969da42013-10-21 02:14:45 +0000179table_options(A) ::= . {A = 0;}
180table_options(A) ::= WITHOUT nm(X). {
181 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
drhfccda8a2015-05-27 13:06:55 +0000182 A = TF_WithoutRowid | TF_NoVisibleRowid;
drh5969da42013-10-21 02:14:45 +0000183 }else{
184 A = 0;
185 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
186 }
187}
drh986dde72016-02-29 13:37:21 +0000188columnlist ::= columnlist COMMA columnname carglist.
189columnlist ::= columnname carglist.
drh2881ab62016-02-27 23:25:36 +0000190columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
drhc4a3c772001-04-04 11:48:57 +0000191
drh6a8700b2017-08-02 11:04:00 +0000192// Declare some tokens early in order to influence their values, to
193// improve performance and reduce the executable size. The goal here is
194// to get the "jump" operations in ISNULL through ESCAPE to have numeric
195// values that are early enough so that all jump operations are clustered
196// at the beginning, but also so that the comparison tokens NE through GE
197// are as large as possible so that they are near to FUNCTION, which is a
198// token synthesized by addopcodes.tcl.
199//
200%token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
201%token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
202%token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
203%token GT LE LT GE ESCAPE.
204
drh31d6fd52017-04-14 19:03:10 +0000205// The following directive causes tokens ABORT, AFTER, ASC, etc. to
206// fallback to ID if they will not parse as their original value.
207// This obviates the need for the "id" nonterminal.
208//
209%fallback ID
210 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
drh0a6259f2018-04-16 13:26:53 +0000211 CONFLICT DATABASE DEFERRED DESC DETACH DO
drh6cd7d482018-04-12 15:43:05 +0000212 EACH END EXCLUSIVE EXPLAIN FAIL FOR
drh31d6fd52017-04-14 19:03:10 +0000213 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
dan86fb6e12018-05-16 20:58:07 +0000214 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
drh31d6fd52017-04-14 19:03:10 +0000215 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
216%ifdef SQLITE_OMIT_COMPOUND_SELECT
217 EXCEPT INTERSECT UNION
218%endif SQLITE_OMIT_COMPOUND_SELECT
drh3773c252018-06-28 03:38:49 +0000219%ifndef SQLITE_OMIT_WINDOWFUNC
dan6e2210e2018-06-30 18:54:56 +0000220 CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED
drh3773c252018-06-28 03:38:49 +0000221%endif SQLITE_OMIT_WINDOWFUNC
drh31d6fd52017-04-14 19:03:10 +0000222 REINDEX RENAME CTIME_KW IF
223 .
224%wildcard ANY.
225
drhf7b54962013-05-28 12:11:54 +0000226// Define operator precedence early so that this is the first occurrence
drh2d3917d2004-02-22 16:27:00 +0000227// of the operator tokens in the grammer. Keeping the operators together
228// causes them to be assigned integer values that are close together,
229// which keeps parser tables smaller.
230//
drhf2bc0132004-10-04 13:19:23 +0000231// The token values assigned to these symbols is determined by the order
232// in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
233// NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
234// the sqlite3ExprIfFalse() routine for additional information on this
235// constraint.
236//
drh2d3917d2004-02-22 16:27:00 +0000237%left OR.
238%left AND.
239%right NOT.
drh03bea702006-06-13 15:37:26 +0000240%left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
drh9a432672004-10-04 13:38:09 +0000241%left GT LE LT GE.
danielk19777c6303c2004-11-17 16:41:29 +0000242%right ESCAPE.
drh2d3917d2004-02-22 16:27:00 +0000243%left BITAND BITOR LSHIFT RSHIFT.
244%left PLUS MINUS.
245%left STAR SLASH REM.
drha34001c2007-02-02 12:44:37 +0000246%left CONCAT.
247%left COLLATE.
drh7ba5bc52009-09-22 20:08:34 +0000248%right BITNOT.
drh26cf56f2018-04-06 19:36:49 +0000249%nonassoc ON.
drh2d3917d2004-02-22 16:27:00 +0000250
drh7cc84c22016-04-11 13:36:42 +0000251// An IDENTIFIER can be a generic identifier, or one of several
252// keywords. Any non-standard keyword can also be an identifier.
253//
254%token_class id ID|INDEXED.
255
drh7cc84c22016-04-11 13:36:42 +0000256
drhc4a3c772001-04-04 11:48:57 +0000257// And "ids" is an identifer-or-string.
258//
dan59ff4252018-06-29 17:44:52 +0000259%token_class ids ID|STRING.
drhc4a3c772001-04-04 11:48:57 +0000260
drh5ad1a6c2002-07-01 12:27:09 +0000261// The name of a column or table can be any of the following:
262//
263%type nm {Token}
drh4dd0d3f2016-02-17 01:18:33 +0000264nm(A) ::= id(A).
265nm(A) ::= STRING(A).
266nm(A) ::= JOIN_KW(A).
drh5ad1a6c2002-07-01 12:27:09 +0000267
drh986dde72016-02-29 13:37:21 +0000268// A typetoken is really zero or more tokens that form a type name such
drh487e2622005-06-25 18:42:14 +0000269// as can be found after the column name in a CREATE TABLE statement.
270// Multiple tokens are concatenated to form the value of the typetoken.
271//
272%type typetoken {Token}
drh986dde72016-02-29 13:37:21 +0000273typetoken(A) ::= . {A.n = 0; A.z = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000274typetoken(A) ::= typename(A).
275typetoken(A) ::= typename(A) LP signed RP(Y). {
276 A.n = (int)(&Y.z[Y.n] - A.z);
drh487e2622005-06-25 18:42:14 +0000277}
drh4dd0d3f2016-02-17 01:18:33 +0000278typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
279 A.n = (int)(&Y.z[Y.n] - A.z);
drh487e2622005-06-25 18:42:14 +0000280}
drh382c0242001-10-06 16:33:02 +0000281%type typename {Token}
dan59ff4252018-06-29 17:44:52 +0000282typename(A) ::= ids(A).
283typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
drh60218d22007-04-06 11:26:00 +0000284signed ::= plus_num.
285signed ::= minus_num.
drh487e2622005-06-25 18:42:14 +0000286
drh1be266b2017-12-24 00:18:47 +0000287// The scanpt non-terminal takes a value which is a pointer to the
288// input text just past the last token that has been shifted into
289// the parser. By surrounding some phrase in the grammar with two
290// scanpt non-terminals, we can capture the input text for that phrase.
291// For example:
292//
293// something ::= .... scanpt(A) phrase scanpt(Z).
294//
295// The text that is parsed as "phrase" is a string starting at A
296// and containing (int)(Z-A) characters. There might be some extra
297// whitespace on either end of the text, but that can be removed in
298// post-processing, if needed.
299//
300%type scanpt {const char*}
301scanpt(A) ::= . {
drhf259df52017-12-27 20:38:35 +0000302 assert( yyLookahead!=YYNOCODE );
303 A = yyLookaheadToken.z;
drh1be266b2017-12-24 00:18:47 +0000304}
305
drh487e2622005-06-25 18:42:14 +0000306// "carglist" is a list of additional constraints that come after the
307// column name and column type in a CREATE TABLE statement.
308//
drh4dc330d2012-05-07 19:21:36 +0000309carglist ::= carglist ccons.
drh348784e2000-05-29 20:41:49 +0000310carglist ::= .
drh4dc330d2012-05-07 19:21:36 +0000311ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
drh1be266b2017-12-24 00:18:47 +0000312ccons ::= DEFAULT scanpt(A) term(X) scanpt(Z).
313 {sqlite3AddDefaultValue(pParse,X,A,Z);}
314ccons ::= DEFAULT LP(A) expr(X) RP(Z).
315 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
316ccons ::= DEFAULT PLUS(A) term(X) scanpt(Z).
317 {sqlite3AddDefaultValue(pParse,X,A.z,Z);}
318ccons ::= DEFAULT MINUS(A) term(X) scanpt(Z). {
319 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
320 sqlite3AddDefaultValue(pParse,p,A.z,Z);
danielk19777977a172004-11-09 12:44:37 +0000321}
drh1be266b2017-12-24 00:18:47 +0000322ccons ::= DEFAULT scanpt id(X). {
323 Expr *p = tokenExpr(pParse, TK_STRING, X);
drhd7fd8992018-02-28 04:30:55 +0000324 if( p ){
325 sqlite3ExprIdToTrueFalse(p);
326 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
327 }
drh1be266b2017-12-24 00:18:47 +0000328 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
danielk19777977a172004-11-09 12:44:37 +0000329}
drh348784e2000-05-29 20:41:49 +0000330
drh382c0242001-10-06 16:33:02 +0000331// In addition to the type name, we also care about the primary key and
332// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000333//
drh0d316a42002-08-11 20:10:47 +0000334ccons ::= NULL onconf.
drhb7916a72009-05-27 10:31:29 +0000335ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
drhfdd6e852005-12-16 01:06:16 +0000336ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
drhb7916a72009-05-27 10:31:29 +0000337 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
drh62340f82016-05-31 21:18:15 +0000338ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
339 SQLITE_IDXTYPE_UNIQUE);}
drh1be266b2017-12-24 00:18:47 +0000340ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);}
drh108aa002015-08-24 20:21:20 +0000341ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
drhb7916a72009-05-27 10:31:29 +0000342 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
343ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
dan59ff4252018-06-29 17:44:52 +0000344ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);}
drh04738cb2002-06-02 18:19:00 +0000345
drh205f48e2004-11-05 00:43:11 +0000346// The optional AUTOINCREMENT keyword
347%type autoinc {int}
drh2958a4e2004-11-12 03:56:15 +0000348autoinc(X) ::= . {X = 0;}
349autoinc(X) ::= AUTOINCR. {X = 1;}
drh205f48e2004-11-05 00:43:11 +0000350
drhc2eef3b2002-08-31 18:53:06 +0000351// The next group of rules parses the arguments to a REFERENCES clause
352// that determine if the referential integrity checking is deferred or
353// or immediate and which determine what action to take if a ref-integ
354// check fails.
drh04738cb2002-06-02 18:19:00 +0000355//
drhc2eef3b2002-08-31 18:53:06 +0000356%type refargs {int}
drhfcf486c2009-10-21 13:48:24 +0000357refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */}
drh4dd0d3f2016-02-17 01:18:33 +0000358refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
drhc2eef3b2002-08-31 18:53:06 +0000359%type refarg {struct {int value; int mask;}}
360refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
drhc29c5aa12009-12-09 21:43:36 +0000361refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; }
drhc2eef3b2002-08-31 18:53:06 +0000362refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
363refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
drhc2eef3b2002-08-31 18:53:06 +0000364%type refact {int}
drhfcf486c2009-10-21 13:48:24 +0000365refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */}
366refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */}
367refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */}
368refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */}
369refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */}
drhc2eef3b2002-08-31 18:53:06 +0000370%type defer_subclause {int}
dan1da40a32009-09-19 17:00:31 +0000371defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;}
drhc2eef3b2002-08-31 18:53:06 +0000372defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
373%type init_deferred_pred_opt {int}
374init_deferred_pred_opt(A) ::= . {A = 0;}
375init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
376init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000377
drhaeb281c2012-05-08 11:17:33 +0000378conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000379conslist_opt(A) ::= COMMA(A) conslist.
drhab35eae2012-05-12 18:29:53 +0000380conslist ::= conslist tconscomma tcons.
381conslist ::= tcons.
382tconscomma ::= COMMA. {pParse->constraintName.n = 0;}
383tconscomma ::= .
384tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
drh108aa002015-08-24 20:21:20 +0000385tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
drhb7916a72009-05-27 10:31:29 +0000386 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
drh108aa002015-08-24 20:21:20 +0000387tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
drh62340f82016-05-31 21:18:15 +0000388 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
389 SQLITE_IDXTYPE_UNIQUE);}
drhb7916a72009-05-27 10:31:29 +0000390tcons ::= CHECK LP expr(E) RP onconf.
drh1be266b2017-12-24 00:18:47 +0000391 {sqlite3AddCheckConstraint(pParse,E);}
drh108aa002015-08-24 20:21:20 +0000392tcons ::= FOREIGN KEY LP eidlist(FA) RP
393 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
danielk19774adee202004-05-08 08:23:19 +0000394 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
395 sqlite3DeferForeignKey(pParse, D);
drhc2eef3b2002-08-31 18:53:06 +0000396}
397%type defer_subclause_opt {int}
398defer_subclause_opt(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000399defer_subclause_opt(A) ::= defer_subclause(A).
drh9cfcf5d2002-01-29 18:41:24 +0000400
401// The following is a non-standard extension that allows us to declare the
402// default behavior when there is a constraint conflict.
403//
404%type onconf {int}
drh3334d082015-11-10 13:45:21 +0000405%type orconf {int}
drh1c928532002-01-31 15:54:21 +0000406%type resolvetype {int}
drh74ad7fe2004-10-07 03:06:28 +0000407onconf(A) ::= . {A = OE_Default;}
408onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
409orconf(A) ::= . {A = OE_Default;}
drh3334d082015-11-10 13:45:21 +0000410orconf(A) ::= OR resolvetype(X). {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000411resolvetype(A) ::= raisetype(A).
drh74ad7fe2004-10-07 03:06:28 +0000412resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
413resolvetype(A) ::= REPLACE. {A = OE_Replace;}
drh348784e2000-05-29 20:41:49 +0000414
drh382c0242001-10-06 16:33:02 +0000415////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000416//
drha0733842005-12-29 01:11:36 +0000417cmd ::= DROP TABLE ifexists(E) fullname(X). {
418 sqlite3DropTable(pParse, X, 0, E);
danielk1977a8858102004-05-28 12:11:21 +0000419}
drha0733842005-12-29 01:11:36 +0000420%type ifexists {int}
421ifexists(A) ::= IF EXISTS. {A = 1;}
422ifexists(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000423
drha76b5df2002-02-23 02:32:10 +0000424///////////////////// The CREATE VIEW statement /////////////////////////////
425//
drhb7f91642004-10-31 02:22:47 +0000426%ifndef SQLITE_OMIT_VIEW
drh108aa002015-08-24 20:21:20 +0000427cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
drh8981b902015-08-24 17:42:49 +0000428 AS select(S). {
429 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
drha76b5df2002-02-23 02:32:10 +0000430}
drha0733842005-12-29 01:11:36 +0000431cmd ::= DROP VIEW ifexists(E) fullname(X). {
432 sqlite3DropTable(pParse, X, 1, E);
drha76b5df2002-02-23 02:32:10 +0000433}
drh154d4b22006-09-21 11:02:16 +0000434%endif SQLITE_OMIT_VIEW
drha76b5df2002-02-23 02:32:10 +0000435
drh382c0242001-10-06 16:33:02 +0000436//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000437//
dan7d562db2014-01-11 19:19:36 +0000438cmd ::= select(X). {
drhedf83d12014-01-22 18:31:27 +0000439 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
drh7d10d5a2008-08-20 16:35:10 +0000440 sqlite3Select(pParse, X, &dest);
drh633e6d52008-07-28 19:34:53 +0000441 sqlite3SelectDelete(pParse->db, X);
drh9bb61fe2000-06-05 16:01:39 +0000442}
drhefb72512000-05-31 20:00:52 +0000443
drh9bb61fe2000-06-05 16:01:39 +0000444%type select {Select*}
drh633e6d52008-07-28 19:34:53 +0000445%destructor select {sqlite3SelectDelete(pParse->db, $$);}
dan7d562db2014-01-11 19:19:36 +0000446%type selectnowith {Select*}
447%destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
drh82c3d632000-06-06 21:56:07 +0000448%type oneselect {Select*}
drh633e6d52008-07-28 19:34:53 +0000449%destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
drh9bb61fe2000-06-05 16:01:39 +0000450
drh772460f2015-04-16 14:13:12 +0000451%include {
452 /*
453 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
454 ** all elements in the list. And make sure list length does not exceed
455 ** SQLITE_LIMIT_COMPOUND_SELECT.
456 */
drhe318a7f2015-04-16 23:04:17 +0000457 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
drhd227a292014-02-09 18:02:09 +0000458 if( p->pPrior ){
drh772460f2015-04-16 14:13:12 +0000459 Select *pNext = 0, *pLoop;
460 int mxSelect, cnt = 0;
drhd227a292014-02-09 18:02:09 +0000461 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
462 pLoop->pNext = pNext;
463 pLoop->selFlags |= SF_Compound;
464 }
drh772460f2015-04-16 14:13:12 +0000465 if( (p->selFlags & SF_MultiValue)==0 &&
466 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
467 cnt>mxSelect
drha0c01762015-01-05 16:27:43 +0000468 ){
drhd227a292014-02-09 18:02:09 +0000469 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
470 }
471 }
drh772460f2015-04-16 14:13:12 +0000472 }
473}
474
drh6a8b94e2018-05-30 01:14:20 +0000475%ifndef SQLITE_OMIT_CTE
drha5746e02018-04-09 20:36:09 +0000476select(A) ::= WITH wqlist(W) selectnowith(X). {
drh772460f2015-04-16 14:13:12 +0000477 Select *p = X;
478 if( p ){
479 p->pWith = W;
480 parserDoubleLinkSelect(pParse, p);
dana9f5c132014-01-13 16:36:40 +0000481 }else{
482 sqlite3WithDelete(pParse->db, W);
483 }
drha5746e02018-04-09 20:36:09 +0000484 A = p;
485}
486select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). {
487 Select *p = X;
488 if( p ){
489 p->pWith = W;
490 parserDoubleLinkSelect(pParse, p);
491 }else{
492 sqlite3WithDelete(pParse->db, W);
493 }
494 A = p;
495}
drh6a8b94e2018-05-30 01:14:20 +0000496%endif /* SQLITE_OMIT_CTE */
drha5746e02018-04-09 20:36:09 +0000497select(A) ::= selectnowith(X). {
498 Select *p = X;
499 if( p ){
500 parserDoubleLinkSelect(pParse, p);
501 }
502 A = p; /*A-overwrites-X*/
dana9f5c132014-01-13 16:36:40 +0000503}
dan7d562db2014-01-11 19:19:36 +0000504
drh4dd0d3f2016-02-17 01:18:33 +0000505selectnowith(A) ::= oneselect(A).
drhb7f91642004-10-31 02:22:47 +0000506%ifndef SQLITE_OMIT_COMPOUND_SELECT
drh4dd0d3f2016-02-17 01:18:33 +0000507selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
drhc0bf4932014-02-19 01:31:02 +0000508 Select *pRhs = Z;
drh4dd0d3f2016-02-17 01:18:33 +0000509 Select *pLhs = A;
drhc0bf4932014-02-19 01:31:02 +0000510 if( pRhs && pRhs->pPrior ){
511 SrcList *pFrom;
512 Token x;
513 x.n = 0;
drh772460f2015-04-16 14:13:12 +0000514 parserDoubleLinkSelect(pParse, pRhs);
drhc0bf4932014-02-19 01:31:02 +0000515 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
drh8c0833f2017-11-14 23:48:23 +0000516 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
drhc0bf4932014-02-19 01:31:02 +0000517 }
518 if( pRhs ){
519 pRhs->op = (u8)Y;
drh00d5ab72015-05-20 00:15:27 +0000520 pRhs->pPrior = pLhs;
521 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
drh772460f2015-04-16 14:13:12 +0000522 pRhs->selFlags &= ~SF_MultiValue;
drhd58d3272013-08-05 22:05:02 +0000523 if( Y!=TK_ALL ) pParse->hasCompound = 1;
drh43b78822007-06-15 17:03:14 +0000524 }else{
drh00d5ab72015-05-20 00:15:27 +0000525 sqlite3SelectDelete(pParse->db, pLhs);
drhdaffd0e2001-04-11 14:28:42 +0000526 }
drhc0bf4932014-02-19 01:31:02 +0000527 A = pRhs;
drh82c3d632000-06-06 21:56:07 +0000528}
drh0a36c572002-02-18 22:49:59 +0000529%type multiselect_op {int}
drhcf82f0d2016-02-17 04:33:10 +0000530multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
drhfd405312005-11-06 04:06:59 +0000531multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
drhcf82f0d2016-02-17 04:33:10 +0000532multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
drh154d4b22006-09-21 11:02:16 +0000533%endif SQLITE_OMIT_COMPOUND_SELECT
drhabd4c722014-09-20 18:18:33 +0000534oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
dan67a9b8e2018-06-22 20:51:35 +0000535 groupby_opt(P) having_opt(Q)
536%ifndef SQLITE_OMIT_WINDOWFUNC
537 windowdefn_opt(R)
538%endif
dane3bf6322018-06-08 20:58:27 +0000539 orderby_opt(Z) limit_opt(L). {
drh43303de2016-02-17 12:34:03 +0000540#if SELECTTRACE_ENABLED
541 Token s = S; /*A-overwrites-S*/
542#endif
drh8c0833f2017-11-14 23:48:23 +0000543 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
dan67a9b8e2018-06-22 20:51:35 +0000544#ifndef SQLITE_OMIT_WINDOWFUNC
dan6fde1792018-06-15 19:01:35 +0000545 if( A ){
546 A->pWinDefn = R;
547 }else{
548 sqlite3WindowListDelete(pParse->db, R);
549 }
dan67a9b8e2018-06-22 20:51:35 +0000550#endif // SQLITE_OMIT_WINDOWFUNC
drhabd4c722014-09-20 18:18:33 +0000551#if SELECTTRACE_ENABLED
drheb9b8842014-09-21 00:27:26 +0000552 /* Populate the Select.zSelName[] string that is used to help with
drhabd4c722014-09-20 18:18:33 +0000553 ** query planner debugging, to differentiate between multiple Select
554 ** objects in a complex query.
555 **
556 ** If the SELECT keyword is immediately followed by a C-style comment
557 ** then extract the first few alphanumeric characters from within that
drheb9b8842014-09-21 00:27:26 +0000558 ** comment to be the zSelName value. Otherwise, the label is #N where
drhabd4c722014-09-20 18:18:33 +0000559 ** is an integer that is incremented with each SELECT statement seen.
560 */
561 if( A!=0 ){
drh43303de2016-02-17 12:34:03 +0000562 const char *z = s.z+6;
drhabd4c722014-09-20 18:18:33 +0000563 int i;
drhcfd74702018-03-19 22:28:34 +0000564 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName,"#%d",++pParse->nSelect);
drhabd4c722014-09-20 18:18:33 +0000565 while( z[0]==' ' ) z++;
566 if( z[0]=='/' && z[1]=='*' ){
567 z += 2;
568 while( z[0]==' ' ) z++;
569 for(i=0; sqlite3Isalnum(z[i]); i++){}
drheb9b8842014-09-21 00:27:26 +0000570 sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
drhabd4c722014-09-20 18:18:33 +0000571 }
572 }
573#endif /* SELECTRACE_ENABLED */
drh9bb61fe2000-06-05 16:01:39 +0000574}
drh4dd0d3f2016-02-17 01:18:33 +0000575oneselect(A) ::= values(A).
drh75593d92014-01-10 20:46:55 +0000576
577%type values {Select*}
578%destructor values {sqlite3SelectDelete(pParse->db, $$);}
579values(A) ::= VALUES LP nexprlist(X) RP. {
drh8c0833f2017-11-14 23:48:23 +0000580 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
drh75593d92014-01-10 20:46:55 +0000581}
drh4dd0d3f2016-02-17 01:18:33 +0000582values(A) ::= values(A) COMMA LP exprlist(Y) RP. {
583 Select *pRight, *pLeft = A;
drh8c0833f2017-11-14 23:48:23 +0000584 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
drhf3151f02015-04-16 20:27:09 +0000585 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
drh75593d92014-01-10 20:46:55 +0000586 if( pRight ){
587 pRight->op = TK_ALL;
drh772460f2015-04-16 14:13:12 +0000588 pRight->pPrior = pLeft;
drh75593d92014-01-10 20:46:55 +0000589 A = pRight;
590 }else{
drh772460f2015-04-16 14:13:12 +0000591 A = pLeft;
drh75593d92014-01-10 20:46:55 +0000592 }
593}
drh9bb61fe2000-06-05 16:01:39 +0000594
595// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
596// present and false (0) if it is not.
597//
drh3334d082015-11-10 13:45:21 +0000598%type distinct {int}
drh832ee3d2012-12-18 19:36:11 +0000599distinct(A) ::= DISTINCT. {A = SF_Distinct;}
drh7cea7f92015-05-29 01:35:19 +0000600distinct(A) ::= ALL. {A = SF_All;}
drhefb72512000-05-31 20:00:52 +0000601distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000602
drh9bb61fe2000-06-05 16:01:39 +0000603// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000604// values of the SELECT statement. The "*" in statements like
605// "SELECT * FROM ..." is encoded as a special expression with an
drh1a1d3cd2015-11-19 16:33:31 +0000606// opcode of TK_ASTERISK.
drh9bb61fe2000-06-05 16:01:39 +0000607//
drh348784e2000-05-29 20:41:49 +0000608%type selcollist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000609%destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000610%type sclp {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000611%destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +0000612sclp(A) ::= selcollist(A) COMMA.
drh348784e2000-05-29 20:41:49 +0000613sclp(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000614selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
615 A = sqlite3ExprListAppend(pParse, A, X);
drhb7916a72009-05-27 10:31:29 +0000616 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
drh1be266b2017-12-24 00:18:47 +0000617 sqlite3ExprListSetSpan(pParse,A,B,Z);
drh01f3f252002-05-24 16:14:15 +0000618}
drhd3f5d612017-12-24 17:01:54 +0000619selcollist(A) ::= sclp(A) scanpt STAR. {
drh1a1d3cd2015-11-19 16:33:31 +0000620 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
drh4dd0d3f2016-02-17 01:18:33 +0000621 A = sqlite3ExprListAppend(pParse, A, p);
drh7c917d12001-12-16 20:05:05 +0000622}
drh1be266b2017-12-24 00:18:47 +0000623selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
drhabfd35e2016-12-06 22:47:23 +0000624 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
625 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
626 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
drh4dd0d3f2016-02-17 01:18:33 +0000627 A = sqlite3ExprListAppend(pParse,A, pDot);
drh54473222002-04-04 02:10:55 +0000628}
drh01f3f252002-05-24 16:14:15 +0000629
630// An option "AS <id>" phrase that can follow one of the expressions that
631// define the result set, or one of the tables in the FROM clause.
632//
633%type as {Token}
drh74ad7fe2004-10-07 03:06:28 +0000634as(X) ::= AS nm(Y). {X = Y;}
drh4dd0d3f2016-02-17 01:18:33 +0000635as(X) ::= ids(X).
drh986dde72016-02-29 13:37:21 +0000636as(X) ::= . {X.n = 0; X.z = 0;}
drh9bb61fe2000-06-05 16:01:39 +0000637
drh348784e2000-05-29 20:41:49 +0000638
drhad3cab52002-05-24 02:04:32 +0000639%type seltablist {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000640%destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
drhad3cab52002-05-24 02:04:32 +0000641%type stl_prefix {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000642%destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
drhad3cab52002-05-24 02:04:32 +0000643%type from {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000644%destructor from {sqlite3SrcListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000645
drh01f3f252002-05-24 16:14:15 +0000646// A complete FROM clause.
647//
drh17435752007-08-16 04:30:38 +0000648from(A) ::= . {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
drhfbdc7f62008-12-03 23:23:40 +0000649from(A) ::= FROM seltablist(X). {
drh61dfc312006-12-16 16:25:15 +0000650 A = X;
651 sqlite3SrcListShiftJoinType(A);
652}
drh01f3f252002-05-24 16:14:15 +0000653
654// "seltablist" is a "Select Table List" - the content of the FROM clause
655// in a SELECT statement. "stl_prefix" is a prefix of this list.
656//
drh4dd0d3f2016-02-17 01:18:33 +0000657stl_prefix(A) ::= seltablist(A) joinop(Y). {
drh8a48b9c2015-08-19 15:20:00 +0000658 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
drh01f3f252002-05-24 16:14:15 +0000659}
drh348784e2000-05-29 20:41:49 +0000660stl_prefix(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000661seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
drhe9240412012-12-18 13:12:03 +0000662 on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000663 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
danielk1977b1c685b2008-10-06 16:18:39 +0000664 sqlite3SrcListIndexedBy(pParse, A, &I);
drhc4a3c772001-04-04 11:48:57 +0000665}
drh4dd0d3f2016-02-17 01:18:33 +0000666seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
drh01d230c2015-08-19 17:11:37 +0000667 on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000668 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
drh01d230c2015-08-19 17:11:37 +0000669 sqlite3SrcListFuncArgs(pParse, A, E);
670}
drh51522cd2005-01-20 13:36:19 +0000671%ifndef SQLITE_OMIT_SUBQUERY
drh4dd0d3f2016-02-17 01:18:33 +0000672 seltablist(A) ::= stl_prefix(A) LP select(S) RP
drh51522cd2005-01-20 13:36:19 +0000673 as(Z) on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000674 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
drhd5feede2002-05-08 21:46:14 +0000675 }
drh4dd0d3f2016-02-17 01:18:33 +0000676 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
drhfbdc7f62008-12-03 23:23:40 +0000677 as(Z) on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000678 if( A==0 && Z.n==0 && N==0 && U==0 ){
drhfbdc7f62008-12-03 23:23:40 +0000679 A = F;
drh832ee3d2012-12-18 19:36:11 +0000680 }else if( F->nSrc==1 ){
drh4dd0d3f2016-02-17 01:18:33 +0000681 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
drh832ee3d2012-12-18 19:36:11 +0000682 if( A ){
683 struct SrcList_item *pNew = &A->a[A->nSrc-1];
684 struct SrcList_item *pOld = F->a;
685 pNew->zName = pOld->zName;
686 pNew->zDatabase = pOld->zDatabase;
drh3c449c62013-04-30 14:06:57 +0000687 pNew->pSelect = pOld->pSelect;
drh832ee3d2012-12-18 19:36:11 +0000688 pOld->zName = pOld->zDatabase = 0;
drh3c449c62013-04-30 14:06:57 +0000689 pOld->pSelect = 0;
drh832ee3d2012-12-18 19:36:11 +0000690 }
691 sqlite3SrcListDelete(pParse->db, F);
drhfbdc7f62008-12-03 23:23:40 +0000692 }else{
693 Select *pSubquery;
694 sqlite3SrcListShiftJoinType(F);
drh8c0833f2017-11-14 23:48:23 +0000695 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
drh4dd0d3f2016-02-17 01:18:33 +0000696 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
drhfbdc7f62008-12-03 23:23:40 +0000697 }
698 }
drh154d4b22006-09-21 11:02:16 +0000699%endif SQLITE_OMIT_SUBQUERY
drhb733d032004-01-24 20:18:12 +0000700
drh113088e2003-03-20 01:16:58 +0000701%type dbnm {Token}
702dbnm(A) ::= . {A.z=0; A.n=0;}
703dbnm(A) ::= DOT nm(X). {A = X;}
704
drh74ad7fe2004-10-07 03:06:28 +0000705%type fullname {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000706%destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
drha5746e02018-04-09 20:36:09 +0000707fullname(A) ::= nm(X).
708 {A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/}
709fullname(A) ::= nm(X) DOT nm(Y).
drhcf82f0d2016-02-17 04:33:10 +0000710 {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
drh74ad7fe2004-10-07 03:06:28 +0000711
drh5e3a6eb2018-04-19 11:45:16 +0000712%type xfullname {SrcList*}
713%destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
714xfullname(A) ::= nm(X).
715 {A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/}
716xfullname(A) ::= nm(X) DOT nm(Y).
717 {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
718xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
719 A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/
720 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
721}
722xfullname(A) ::= nm(X) AS nm(Z). {
723 A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/
724 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
725}
726
drh01f3f252002-05-24 16:14:15 +0000727%type joinop {int}
drhfd405312005-11-06 04:06:59 +0000728joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
drhcf82f0d2016-02-17 04:33:10 +0000729joinop(X) ::= JOIN_KW(A) JOIN.
730 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
731joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
732 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
drh5ad1a6c2002-07-01 12:27:09 +0000733joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
drhcf82f0d2016-02-17 04:33:10 +0000734 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
drh01f3f252002-05-24 16:14:15 +0000735
drh26cf56f2018-04-06 19:36:49 +0000736// There is a parsing abiguity in an upsert statement that uses a
737// SELECT on the RHS of a the INSERT:
738//
739// INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
740// here ----^^
741//
742// When the ON token is encountered, the parser does not know if it is
743// the beginning of an ON CONFLICT clause, or the beginning of an ON
744// clause associated with the JOIN. The conflict is resolved in favor
745// of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
746// WHERE clause in between, like this:
747//
748// INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
749//
750// The [AND] and [OR] precedence marks in the rules for on_opt cause the
751// ON in this context to always be interpreted as belonging to the JOIN.
752//
drh01f3f252002-05-24 16:14:15 +0000753%type on_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000754%destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
drh26cf56f2018-04-06 19:36:49 +0000755on_opt(N) ::= ON expr(E). {N = E;}
756on_opt(N) ::= . [OR] {N = 0;}
drh01f3f252002-05-24 16:14:15 +0000757
danielk197785574e32008-10-06 05:32:18 +0000758// Note that this block abuses the Token type just a little. If there is
759// no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
760// there is an INDEXED BY clause, then the token is populated as per normal,
761// with z pointing to the token data and n containing the number of bytes
762// in the token.
763//
764// If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
danielk1977b1c685b2008-10-06 16:18:39 +0000765// normally illegal. The sqlite3SrcListIndexedBy() function
danielk197785574e32008-10-06 05:32:18 +0000766// recognizes and interprets this as a special case.
767//
768%type indexed_opt {Token}
769indexed_opt(A) ::= . {A.z=0; A.n=0;}
770indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
771indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
772
drh01f3f252002-05-24 16:14:15 +0000773%type using_opt {IdList*}
drh633e6d52008-07-28 19:34:53 +0000774%destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
drh81eba732013-10-19 23:31:56 +0000775using_opt(U) ::= USING LP idlist(L) RP. {U = L;}
drh01f3f252002-05-24 16:14:15 +0000776using_opt(U) ::= . {U = 0;}
777
778
drh348784e2000-05-29 20:41:49 +0000779%type orderby_opt {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000780%destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
drh108aa002015-08-24 20:21:20 +0000781
782// the sortlist non-terminal stores a list of expression where each
783// expression is optionally followed by ASC or DESC to indicate the
784// sort order.
785//
drh348784e2000-05-29 20:41:49 +0000786%type sortlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000787%destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000788
789orderby_opt(A) ::= . {A = 0;}
790orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000791sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). {
drh1be266b2017-12-24 00:18:47 +0000792 A = sqlite3ExprListAppend(pParse,A,Y);
drhbc622bc2015-08-24 15:39:42 +0000793 sqlite3ExprListSetSortOrder(A,Z);
drh9bb61fe2000-06-05 16:01:39 +0000794}
drh8395b7b2012-01-28 19:44:22 +0000795sortlist(A) ::= expr(Y) sortorder(Z). {
drh1be266b2017-12-24 00:18:47 +0000796 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
drhbc622bc2015-08-24 15:39:42 +0000797 sqlite3ExprListSetSortOrder(A,Z);
drh9bb61fe2000-06-05 16:01:39 +0000798}
drh348784e2000-05-29 20:41:49 +0000799
800%type sortorder {int}
801
drh8e2ca022002-06-17 17:07:19 +0000802sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
803sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
drhbc622bc2015-08-24 15:39:42 +0000804sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
drh348784e2000-05-29 20:41:49 +0000805
drh22827922000-06-06 17:27:05 +0000806%type groupby_opt {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000807%destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
drh6206d502000-06-19 19:09:08 +0000808groupby_opt(A) ::= . {A = 0;}
drh9245c242007-06-20 12:18:31 +0000809groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
drh22827922000-06-06 17:27:05 +0000810
811%type having_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000812%destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
drh6206d502000-06-19 19:09:08 +0000813having_opt(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000814having_opt(A) ::= HAVING expr(X). {A = X;}
drh22827922000-06-06 17:27:05 +0000815
drh8c0833f2017-11-14 23:48:23 +0000816%type limit_opt {Expr*}
drh15926592007-04-06 15:02:13 +0000817
818// The destructor for limit_opt will never fire in the current grammar.
819// The limit_opt non-terminal only occurs at the end of a single production
820// rule for SELECT statements. As soon as the rule that create the
821// limit_opt non-terminal reduces, the SELECT statement rule will also
822// reduce. So there is never a limit_opt non-terminal on the stack
823// except as a transient. So there is never anything to destroy.
824//
drh8c0833f2017-11-14 23:48:23 +0000825//%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
826limit_opt(A) ::= . {A = 0;}
827limit_opt(A) ::= LIMIT expr(X).
drh1be266b2017-12-24 00:18:47 +0000828 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
danielk1977a2dc3b12005-02-05 12:48:48 +0000829limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
drh1be266b2017-12-24 00:18:47 +0000830 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
danielk1977a2dc3b12005-02-05 12:48:48 +0000831limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
drh1be266b2017-12-24 00:18:47 +0000832 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
drh9bbca4c2001-11-06 04:00:18 +0000833
drh382c0242001-10-06 16:33:02 +0000834/////////////////////////// The DELETE statement /////////////////////////////
835//
shane273f6192008-10-10 04:34:16 +0000836%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
drh5e3a6eb2018-04-19 11:45:16 +0000837cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W)
drh931577f2008-10-10 14:27:16 +0000838 orderby_opt(O) limit_opt(L). {
danielk1977b1c685b2008-10-06 16:18:39 +0000839 sqlite3SrcListIndexedBy(pParse, X, &I);
drh8c0833f2017-11-14 23:48:23 +0000840 sqlite3DeleteFrom(pParse,X,W,O,L);
danielk1977b1c685b2008-10-06 16:18:39 +0000841}
shane4281bd42008-10-07 05:27:11 +0000842%endif
shane273f6192008-10-10 04:34:16 +0000843%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
drh5e3a6eb2018-04-19 11:45:16 +0000844cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W). {
shane4281bd42008-10-07 05:27:11 +0000845 sqlite3SrcListIndexedBy(pParse, X, &I);
drh8c0833f2017-11-14 23:48:23 +0000846 sqlite3DeleteFrom(pParse,X,W,0,0);
shane4281bd42008-10-07 05:27:11 +0000847}
848%endif
drh348784e2000-05-29 20:41:49 +0000849
850%type where_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000851%destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000852
853where_opt(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000854where_opt(A) ::= WHERE expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000855
drh382c0242001-10-06 16:33:02 +0000856////////////////////////// The UPDATE command ////////////////////////////////
857//
shane273f6192008-10-10 04:34:16 +0000858%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
drh5e3a6eb2018-04-19 11:45:16 +0000859cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
drh8b471862014-01-11 13:22:17 +0000860 where_opt(W) orderby_opt(O) limit_opt(L). {
danielk1977b1c685b2008-10-06 16:18:39 +0000861 sqlite3SrcListIndexedBy(pParse, X, &I);
drhb1a6c3c2008-03-20 16:30:17 +0000862 sqlite3ExprListCheckLength(pParse,Y,"set list");
drheac9fab2018-04-16 13:00:50 +0000863 sqlite3Update(pParse,X,Y,W,R,O,L,0);
danielk19777a15a4b2007-05-08 17:54:43 +0000864}
shane4281bd42008-10-07 05:27:11 +0000865%endif
shane273f6192008-10-10 04:34:16 +0000866%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
drh5e3a6eb2018-04-19 11:45:16 +0000867cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
drhe9240412012-12-18 13:12:03 +0000868 where_opt(W). {
shane4281bd42008-10-07 05:27:11 +0000869 sqlite3SrcListIndexedBy(pParse, X, &I);
870 sqlite3ExprListCheckLength(pParse,Y,"set list");
drheac9fab2018-04-16 13:00:50 +0000871 sqlite3Update(pParse,X,Y,W,R,0,0,0);
shane4281bd42008-10-07 05:27:11 +0000872}
873%endif
drh348784e2000-05-29 20:41:49 +0000874
drhf8db1bc2005-04-22 02:38:37 +0000875%type setlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000876%destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
drhf8db1bc2005-04-22 02:38:37 +0000877
drh4dd0d3f2016-02-17 01:18:33 +0000878setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000879 A = sqlite3ExprListAppend(pParse, A, Y);
drhb7916a72009-05-27 10:31:29 +0000880 sqlite3ExprListSetName(pParse, A, &X, 1);
881}
drha1251bc2016-08-20 00:51:37 +0000882setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000883 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
drha1251bc2016-08-20 00:51:37 +0000884}
drhb7916a72009-05-27 10:31:29 +0000885setlist(A) ::= nm(X) EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000886 A = sqlite3ExprListAppend(pParse, 0, Y);
drhb7916a72009-05-27 10:31:29 +0000887 sqlite3ExprListSetName(pParse, A, &X, 1);
888}
drha1251bc2016-08-20 00:51:37 +0000889setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000890 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
drha1251bc2016-08-20 00:51:37 +0000891}
drh348784e2000-05-29 20:41:49 +0000892
drh382c0242001-10-06 16:33:02 +0000893////////////////////////// The INSERT command /////////////////////////////////
894//
drh5e3a6eb2018-04-19 11:45:16 +0000895cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
drh2c2e8442018-04-07 15:04:05 +0000896 upsert(U). {
drh46d2e5c2018-04-12 13:15:43 +0000897 sqlite3Insert(pParse, X, S, F, R, U);
dan4e9119d2014-01-13 15:12:23 +0000898}
drh5e3a6eb2018-04-19 11:45:16 +0000899cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES.
dan4e9119d2014-01-13 15:12:23 +0000900{
drh2c2e8442018-04-07 15:04:05 +0000901 sqlite3Insert(pParse, X, 0, F, R, 0);
dan4e9119d2014-01-13 15:12:23 +0000902}
drh348784e2000-05-29 20:41:49 +0000903
drh46d2e5c2018-04-12 13:15:43 +0000904%type upsert {Upsert*}
drh54514c92018-04-17 21:59:34 +0000905
906// Because upsert only occurs at the tip end of the INSERT rule for cmd,
907// there is never a case where the value of the upsert pointer will not
908// be destroyed by the cmd action. So comment-out the destructor to
909// avoid unreachable code.
910//%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
drh46d2e5c2018-04-12 13:15:43 +0000911upsert(A) ::= . { A = 0; }
drhe9c2e772018-04-13 13:06:45 +0000912upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
drhdab0eb52018-04-12 17:28:06 +0000913 DO UPDATE SET setlist(Z) where_opt(W).
drhe9c2e772018-04-13 13:06:45 +0000914 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W);}
drhe9c2e772018-04-13 13:06:45 +0000915upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING.
916 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0); }
917upsert(A) ::= ON CONFLICT DO NOTHING.
918 { A = sqlite3UpsertNew(pParse->db,0,0,0,0); }
drh26cf56f2018-04-06 19:36:49 +0000919
drh3334d082015-11-10 13:45:21 +0000920%type insert_cmd {int}
drhfa86c412002-02-02 15:01:15 +0000921insert_cmd(A) ::= INSERT orconf(R). {A = R;}
922insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
923
drh8981b902015-08-24 17:42:49 +0000924%type idlist_opt {IdList*}
925%destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
drh81eba732013-10-19 23:31:56 +0000926%type idlist {IdList*}
927%destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000928
drh8981b902015-08-24 17:42:49 +0000929idlist_opt(A) ::= . {A = 0;}
930idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000931idlist(A) ::= idlist(A) COMMA nm(Y).
932 {A = sqlite3IdListAppend(pParse->db,A,&Y);}
drh81eba732013-10-19 23:31:56 +0000933idlist(A) ::= nm(Y).
drhcf82f0d2016-02-17 04:33:10 +0000934 {A = sqlite3IdListAppend(pParse->db,0,&Y); /*A-overwrites-Y*/}
drh348784e2000-05-29 20:41:49 +0000935
drh382c0242001-10-06 16:33:02 +0000936/////////////////////////// Expression Processing /////////////////////////////
937//
drh348784e2000-05-29 20:41:49 +0000938
drh1be266b2017-12-24 00:18:47 +0000939%type expr {Expr*}
940%destructor expr {sqlite3ExprDelete(pParse->db, $$);}
941%type term {Expr*}
942%destructor term {sqlite3ExprDelete(pParse->db, $$);}
drhb7916a72009-05-27 10:31:29 +0000943
944%include {
drhb7916a72009-05-27 10:31:29 +0000945
946 /* Construct a new Expr object from a single identifier. Use the
947 ** new Expr to populate pOut. Set the span of pOut to be the identifier
948 ** that created the expression.
949 */
drh1be266b2017-12-24 00:18:47 +0000950 static Expr *tokenExpr(Parse *pParse, int op, Token t){
drh0cd874b2016-09-26 12:38:22 +0000951 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
952 if( p ){
953 memset(p, 0, sizeof(Expr));
954 p->op = (u8)op;
955 p->flags = EP_Leaf;
956 p->iAgg = -1;
957 p->u.zToken = (char*)&p[1];
958 memcpy(p->u.zToken, t.z, t.n);
959 p->u.zToken[t.n] = 0;
960 if( sqlite3Isquote(p->u.zToken[0]) ){
961 if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
962 sqlite3Dequote(p->u.zToken);
963 }
964#if SQLITE_MAX_EXPR_DEPTH>0
965 p->nHeight = 1;
966#endif
967 }
drh1be266b2017-12-24 00:18:47 +0000968 return p;
drhb7916a72009-05-27 10:31:29 +0000969 }
970}
drh348784e2000-05-29 20:41:49 +0000971
drh4dd0d3f2016-02-17 01:18:33 +0000972expr(A) ::= term(A).
drh1be266b2017-12-24 00:18:47 +0000973expr(A) ::= LP expr(X) RP. {A = X;}
974expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
975expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
drh5ad1a6c2002-07-01 12:27:09 +0000976expr(A) ::= nm(X) DOT nm(Y). {
drh410c3012016-09-24 17:42:43 +0000977 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
978 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
drh1be266b2017-12-24 00:18:47 +0000979 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
drhe1b6a5b2000-07-29 13:06:59 +0000980}
drhd24cc422003-03-27 12:51:24 +0000981expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
drh410c3012016-09-24 17:42:43 +0000982 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
983 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
984 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
drhabfd35e2016-12-06 22:47:23 +0000985 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
drh1be266b2017-12-24 00:18:47 +0000986 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
drhd24cc422003-03-27 12:51:24 +0000987}
drh1be266b2017-12-24 00:18:47 +0000988term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
989term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
drh0cd874b2016-09-26 12:38:22 +0000990term(A) ::= INTEGER(X). {
drh1be266b2017-12-24 00:18:47 +0000991 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
drh0cd874b2016-09-26 12:38:22 +0000992}
drh7c972de2003-09-06 22:18:07 +0000993expr(A) ::= VARIABLE(X). {
drh8679fba2016-04-11 01:43:33 +0000994 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
drhde25a882016-10-03 15:28:24 +0000995 u32 n = X.n;
drh1be266b2017-12-24 00:18:47 +0000996 A = tokenExpr(pParse, TK_VARIABLE, X);
997 sqlite3ExprAssignVarNumber(pParse, A, n);
drh8f3b1372016-04-11 01:26:31 +0000998 }else{
drhf59b12f2014-01-11 03:54:05 +0000999 /* When doing a nested parse, one can include terms in an expression
1000 ** that look like this: #1 #2 ... These terms refer to registers
1001 ** in the virtual machine. #N is the N-th register. */
drh8f3b1372016-04-11 01:26:31 +00001002 Token t = X; /*A-overwrites-X*/
1003 assert( t.n>=2 );
drhf59b12f2014-01-11 03:54:05 +00001004 if( pParse->nested==0 ){
drh43303de2016-02-17 12:34:03 +00001005 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
drh1be266b2017-12-24 00:18:47 +00001006 A = 0;
drhf59b12f2014-01-11 03:54:05 +00001007 }else{
drh1be266b2017-12-24 00:18:47 +00001008 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1009 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
drhf59b12f2014-01-11 03:54:05 +00001010 }
drhf59b12f2014-01-11 03:54:05 +00001011 }
drh7c972de2003-09-06 22:18:07 +00001012}
dan59ff4252018-06-29 17:44:52 +00001013expr(A) ::= expr(A) COLLATE ids(C). {
drh1be266b2017-12-24 00:18:47 +00001014 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
drh8b4c40d2007-02-01 23:02:45 +00001015}
drh487e2622005-06-25 18:42:14 +00001016%ifndef SQLITE_OMIT_CAST
drh1be266b2017-12-24 00:18:47 +00001017expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1018 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1019 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
drh487e2622005-06-25 18:42:14 +00001020}
drh154d4b22006-09-21 11:02:16 +00001021%endif SQLITE_OMIT_CAST
dan59ff4252018-06-29 17:44:52 +00001022expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP
dan67a9b8e2018-06-22 20:51:35 +00001023%ifndef SQLITE_OMIT_WINDOWFUNC
1024 over_opt(Z)
1025%endif
1026. {
drh994704d2009-06-12 12:04:16 +00001027 if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
drhe5c941b2007-05-08 13:58:26 +00001028 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
drh4e05c832007-05-11 01:44:50 +00001029 }
drh1be266b2017-12-24 00:18:47 +00001030 A = sqlite3ExprFunction(pParse, Y, &X);
dan86fb6e12018-05-16 20:58:07 +00001031 sqlite3WindowAttach(pParse, A, Z);
drh1be266b2017-12-24 00:18:47 +00001032 if( D==SF_Distinct && A ){
1033 A->flags |= EP_Distinct;
drhfd357972005-09-09 01:33:19 +00001034 }
drhe1b6a5b2000-07-29 13:06:59 +00001035}
dan59ff4252018-06-29 17:44:52 +00001036expr(A) ::= id(X) LP STAR RP
dan67a9b8e2018-06-22 20:51:35 +00001037%ifndef SQLITE_OMIT_WINDOWFUNC
1038 over_opt(Z)
1039%endif
1040. {
drh1be266b2017-12-24 00:18:47 +00001041 A = sqlite3ExprFunction(pParse, 0, &X);
danb6e9f7a2018-05-19 14:15:29 +00001042 sqlite3WindowAttach(pParse, A, Z);
drhe1b6a5b2000-07-29 13:06:59 +00001043}
drhb71090f2005-05-23 17:26:51 +00001044term(A) ::= CTIME_KW(OP). {
drh1be266b2017-12-24 00:18:47 +00001045 A = sqlite3ExprFunction(pParse, 0, &OP);
drhb7916a72009-05-27 10:31:29 +00001046}
1047
drh1be266b2017-12-24 00:18:47 +00001048expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1049 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1050 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1051 if( A ){
1052 A->x.pList = pList;
drh8bd0d582016-08-20 18:06:14 +00001053 }else{
1054 sqlite3ExprListDelete(pParse->db, pList);
dan71c57db2016-07-09 20:23:55 +00001055 }
1056}
1057
drh1be266b2017-12-24 00:18:47 +00001058expr(A) ::= expr(A) AND(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
1059expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001060expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001061 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1062expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001063expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001064 {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001065expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001066 {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001067expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001068 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1069expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh410c3012016-09-24 17:42:43 +00001070%type likeop {Token}
drh7e84b372017-02-20 14:30:17 +00001071likeop(A) ::= LIKE_KW|MATCH(A).
drh410c3012016-09-24 17:42:43 +00001072likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
drh4dd0d3f2016-02-17 01:18:33 +00001073expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
drh8aa34ae2006-03-13 12:54:09 +00001074 ExprList *pList;
drh410c3012016-09-24 17:42:43 +00001075 int bNot = OP.n & 0x80000000;
1076 OP.n &= 0x7fffffff;
drh1be266b2017-12-24 00:18:47 +00001077 pList = sqlite3ExprListAppend(pParse,0, Y);
1078 pList = sqlite3ExprListAppend(pParse,pList, A);
1079 A = sqlite3ExprFunction(pParse, pList, &OP);
1080 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1081 if( A ) A->flags |= EP_InfixFunc;
drh0ac65892002-04-20 14:24:41 +00001082}
drh4dd0d3f2016-02-17 01:18:33 +00001083expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
drh1dca1452010-07-19 02:30:33 +00001084 ExprList *pList;
drh410c3012016-09-24 17:42:43 +00001085 int bNot = OP.n & 0x80000000;
1086 OP.n &= 0x7fffffff;
drh1be266b2017-12-24 00:18:47 +00001087 pList = sqlite3ExprListAppend(pParse,0, Y);
1088 pList = sqlite3ExprListAppend(pParse,pList, A);
1089 pList = sqlite3ExprListAppend(pParse,pList, E);
1090 A = sqlite3ExprFunction(pParse, pList, &OP);
1091 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1092 if( A ) A->flags |= EP_InfixFunc;
drh1dca1452010-07-19 02:30:33 +00001093}
danielk19777c6303c2004-11-17 16:41:29 +00001094
drh1be266b2017-12-24 00:18:47 +00001095expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1096expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
drh6a2fe092009-09-23 02:29:36 +00001097
drh6a517412009-11-12 03:46:34 +00001098%include {
1099 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1100 ** unary TK_ISNULL or TK_NOTNULL expression. */
1101 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1102 sqlite3 *db = pParse->db;
dan895c00e2016-01-28 18:22:46 +00001103 if( pA && pY && pY->op==TK_NULL ){
shaneh5e17e8b2009-12-03 04:40:47 +00001104 pA->op = (u8)op;
drh6a517412009-11-12 03:46:34 +00001105 sqlite3ExprDelete(db, pA->pRight);
1106 pA->pRight = 0;
1107 }
1108 }
1109}
1110
drh6a2fe092009-09-23 02:29:36 +00001111// expr1 IS expr2
1112// expr1 IS NOT expr2
1113//
1114// If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1115// is any other expression, code as TK_IS or TK_ISNOT.
1116//
drh4dd0d3f2016-02-17 01:18:33 +00001117expr(A) ::= expr(A) IS expr(Y). {
drh1be266b2017-12-24 00:18:47 +00001118 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1119 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
drh6a2fe092009-09-23 02:29:36 +00001120}
drh4dd0d3f2016-02-17 01:18:33 +00001121expr(A) ::= expr(A) IS NOT expr(Y). {
drh1be266b2017-12-24 00:18:47 +00001122 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1123 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
drh6a2fe092009-09-23 02:29:36 +00001124}
drhb7916a72009-05-27 10:31:29 +00001125
drh43303de2016-02-17 12:34:03 +00001126expr(A) ::= NOT(B) expr(X).
drh1be266b2017-12-24 00:18:47 +00001127 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
drh43303de2016-02-17 12:34:03 +00001128expr(A) ::= BITNOT(B) expr(X).
drh1be266b2017-12-24 00:18:47 +00001129 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
drhca5aa592018-06-19 11:15:19 +00001130expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1131 A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
1132 /*A-overwrites-B*/
1133}
drhb7916a72009-05-27 10:31:29 +00001134
drh2e3a1f12004-10-06 14:39:28 +00001135%type between_op {int}
1136between_op(A) ::= BETWEEN. {A = 0;}
1137between_op(A) ::= NOT BETWEEN. {A = 1;}
drh4dd0d3f2016-02-17 01:18:33 +00001138expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
drh1be266b2017-12-24 00:18:47 +00001139 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1140 pList = sqlite3ExprListAppend(pParse,pList, Y);
1141 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1142 if( A ){
1143 A->x.pList = pList;
drh53f733c2005-09-16 02:38:09 +00001144 }else{
drh633e6d52008-07-28 19:34:53 +00001145 sqlite3ExprListDelete(pParse->db, pList);
drh53f733c2005-09-16 02:38:09 +00001146 }
drh1be266b2017-12-24 00:18:47 +00001147 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drhfef52082000-06-06 01:50:43 +00001148}
drh51522cd2005-01-20 13:36:19 +00001149%ifndef SQLITE_OMIT_SUBQUERY
danielk19773e8c37e2005-01-21 03:12:14 +00001150 %type in_op {int}
1151 in_op(A) ::= IN. {A = 0;}
1152 in_op(A) ::= NOT IN. {A = 1;}
drh1be266b2017-12-24 00:18:47 +00001153 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
drh094430e2010-07-14 18:24:06 +00001154 if( Y==0 ){
dan473c1bf2010-07-15 11:14:21 +00001155 /* Expressions of the form
1156 **
1157 ** expr1 IN ()
1158 ** expr1 NOT IN ()
1159 **
1160 ** simplify to constants 0 (false) and 1 (true), respectively,
1161 ** regardless of the value of expr1.
1162 */
drh1be266b2017-12-24 00:18:47 +00001163 sqlite3ExprDelete(pParse->db, A);
1164 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
drh2b59b3a2014-03-20 13:26:47 +00001165 }else if( Y->nExpr==1 ){
1166 /* Expressions of the form:
1167 **
1168 ** expr1 IN (?1)
1169 ** expr1 NOT IN (?2)
1170 **
drhfbb24d12014-03-20 17:03:30 +00001171 ** with exactly one value on the RHS can be simplified to something
1172 ** like this:
drh2b59b3a2014-03-20 13:26:47 +00001173 **
drhfbb24d12014-03-20 17:03:30 +00001174 ** expr1 == ?1
1175 ** expr1 <> ?2
1176 **
1177 ** But, the RHS of the == or <> is marked with the EP_Generic flag
1178 ** so that it may not contribute to the computation of comparison
1179 ** affinity or the collating sequence to use for comparison. Otherwise,
1180 ** the semantics would be subtly different from IN or NOT IN.
drh2b59b3a2014-03-20 13:26:47 +00001181 */
drhfbb24d12014-03-20 17:03:30 +00001182 Expr *pRHS = Y->a[0].pExpr;
drh2b59b3a2014-03-20 13:26:47 +00001183 Y->a[0].pExpr = 0;
1184 sqlite3ExprListDelete(pParse->db, Y);
drh5b1420e2014-03-20 19:04:56 +00001185 /* pRHS cannot be NULL because a malloc error would have been detected
1186 ** before now and control would have never reached this point */
1187 if( ALWAYS(pRHS) ){
drhfbb24d12014-03-20 17:03:30 +00001188 pRHS->flags &= ~EP_Collate;
1189 pRHS->flags |= EP_Generic;
1190 }
drh1be266b2017-12-24 00:18:47 +00001191 A = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A, pRHS);
danielk1977d5d56522005-03-16 12:15:20 +00001192 }else{
drh1be266b2017-12-24 00:18:47 +00001193 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1194 if( A ){
1195 A->x.pList = Y;
1196 sqlite3ExprSetHeightAndFlags(pParse, A);
drh094430e2010-07-14 18:24:06 +00001197 }else{
1198 sqlite3ExprListDelete(pParse->db, Y);
1199 }
drh1be266b2017-12-24 00:18:47 +00001200 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
danielk1977d5d56522005-03-16 12:15:20 +00001201 }
danielk19773e8c37e2005-01-21 03:12:14 +00001202 }
drh1be266b2017-12-24 00:18:47 +00001203 expr(A) ::= LP select(X) RP. {
1204 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1205 sqlite3PExprAddSelect(pParse, A, X);
drh51522cd2005-01-20 13:36:19 +00001206 }
drh1be266b2017-12-24 00:18:47 +00001207 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1208 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1209 sqlite3PExprAddSelect(pParse, A, Y);
1210 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drh51522cd2005-01-20 13:36:19 +00001211 }
drh5fbab882016-07-02 12:08:14 +00001212 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
drh17435752007-08-16 04:30:38 +00001213 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
drh8c0833f2017-11-14 23:48:23 +00001214 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
drh9de47572016-07-02 12:33:21 +00001215 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
drh1be266b2017-12-24 00:18:47 +00001216 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1217 sqlite3PExprAddSelect(pParse, A, pSelect);
1218 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drh51522cd2005-01-20 13:36:19 +00001219 }
drh1be266b2017-12-24 00:18:47 +00001220 expr(A) ::= EXISTS LP select(Y) RP. {
drh43303de2016-02-17 12:34:03 +00001221 Expr *p;
drh1be266b2017-12-24 00:18:47 +00001222 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
drh08de4f72016-04-11 01:06:47 +00001223 sqlite3PExprAddSelect(pParse, p, Y);
drh51522cd2005-01-20 13:36:19 +00001224 }
drh154d4b22006-09-21 11:02:16 +00001225%endif SQLITE_OMIT_SUBQUERY
drhfef52082000-06-06 01:50:43 +00001226
drh17a7f8d2002-03-24 13:13:27 +00001227/* CASE expressions */
drh1be266b2017-12-24 00:18:47 +00001228expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1229 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1230 if( A ){
1231 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1232 sqlite3ExprSetHeightAndFlags(pParse, A);
drh53f733c2005-09-16 02:38:09 +00001233 }else{
drh633e6d52008-07-28 19:34:53 +00001234 sqlite3ExprListDelete(pParse->db, Y);
drhc5cd1242013-09-12 16:50:49 +00001235 sqlite3ExprDelete(pParse->db, Z);
drh53f733c2005-09-16 02:38:09 +00001236 }
drh17a7f8d2002-03-24 13:13:27 +00001237}
1238%type case_exprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001239%destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +00001240case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
drh1be266b2017-12-24 00:18:47 +00001241 A = sqlite3ExprListAppend(pParse,A, Y);
1242 A = sqlite3ExprListAppend(pParse,A, Z);
drh17a7f8d2002-03-24 13:13:27 +00001243}
1244case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
drh1be266b2017-12-24 00:18:47 +00001245 A = sqlite3ExprListAppend(pParse,0, Y);
1246 A = sqlite3ExprListAppend(pParse,A, Z);
drh17a7f8d2002-03-24 13:13:27 +00001247}
1248%type case_else {Expr*}
drh633e6d52008-07-28 19:34:53 +00001249%destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +00001250case_else(A) ::= ELSE expr(X). {A = X;}
drh17a7f8d2002-03-24 13:13:27 +00001251case_else(A) ::= . {A = 0;}
1252%type case_operand {Expr*}
drh633e6d52008-07-28 19:34:53 +00001253%destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +00001254case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/}
drh17a7f8d2002-03-24 13:13:27 +00001255case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +00001256
1257%type exprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001258%destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh9245c242007-06-20 12:18:31 +00001259%type nexprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001260%destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +00001261
drh4dd0d3f2016-02-17 01:18:33 +00001262exprlist(A) ::= nexprlist(A).
drh9245c242007-06-20 12:18:31 +00001263exprlist(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +00001264nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
drh1be266b2017-12-24 00:18:47 +00001265 {A = sqlite3ExprListAppend(pParse,A,Y);}
drh17435752007-08-16 04:30:38 +00001266nexprlist(A) ::= expr(Y).
drh1be266b2017-12-24 00:18:47 +00001267 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
drh9245c242007-06-20 12:18:31 +00001268
drha5224732016-07-25 14:40:43 +00001269%ifndef SQLITE_OMIT_SUBQUERY
drh5fbab882016-07-02 12:08:14 +00001270/* A paren_exprlist is an optional expression list contained inside
1271** of parenthesis */
1272%type paren_exprlist {ExprList*}
1273%destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1274paren_exprlist(A) ::= . {A = 0;}
1275paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
drha5224732016-07-25 14:40:43 +00001276%endif SQLITE_OMIT_SUBQUERY
drh5fbab882016-07-02 12:08:14 +00001277
drhcce7d172000-05-31 15:34:51 +00001278
drh382c0242001-10-06 16:33:02 +00001279///////////////////////////// The CREATE INDEX command ///////////////////////
1280//
drhd9da78a2009-03-24 15:08:09 +00001281cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
drh108aa002015-08-24 20:21:20 +00001282 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
drh17435752007-08-16 04:30:38 +00001283 sqlite3CreateIndex(pParse, &X, &D,
1284 sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
drh62340f82016-05-31 21:18:15 +00001285 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
drh9cfcf5d2002-01-29 18:41:24 +00001286}
drh717e6402001-09-27 03:22:32 +00001287
1288%type uniqueflag {int}
drh74ad7fe2004-10-07 03:06:28 +00001289uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1290uniqueflag(A) ::= . {A = OE_None;}
drh348784e2000-05-29 20:41:49 +00001291
drh348784e2000-05-29 20:41:49 +00001292
drh108aa002015-08-24 20:21:20 +00001293// The eidlist non-terminal (Expression Id List) generates an ExprList
1294// from a list of identifiers. The identifier names are in ExprList.a[].zName.
1295// This list is stored in an ExprList rather than an IdList so that it
1296// can be easily sent to sqlite3ColumnsExprList().
1297//
1298// eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1299// used for the arguments to an index. That is just an historical accident.
1300//
1301// IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1302// COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1303// places - places that might have been stored in the sqlite_master schema.
1304// Those extra features were ignored. But because they might be in some
1305// (busted) old databases, we need to continue parsing them when loading
1306// historical schemas.
1307//
1308%type eidlist {ExprList*}
1309%destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1310%type eidlist_opt {ExprList*}
1311%destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1312
1313%include {
1314 /* Add a single new term to an ExprList that is used to store a
1315 ** list of identifiers. Report an error if the ID list contains
1316 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1317 ** error while parsing a legacy schema.
1318 */
1319 static ExprList *parserAddExprIdListTerm(
1320 Parse *pParse,
1321 ExprList *pPrior,
1322 Token *pIdToken,
1323 int hasCollate,
1324 int sortOrder
1325 ){
1326 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1327 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1328 && pParse->db->init.busy==0
1329 ){
1330 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1331 pIdToken->n, pIdToken->z);
1332 }
1333 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1334 return p;
1335 }
1336} // end %include
1337
1338eidlist_opt(A) ::= . {A = 0;}
1339eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +00001340eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1341 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
danielk19770202b292004-06-09 09:55:16 +00001342}
drh108aa002015-08-24 20:21:20 +00001343eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
drhcf82f0d2016-02-17 04:33:10 +00001344 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
danielk19770202b292004-06-09 09:55:16 +00001345}
danielk19770202b292004-06-09 09:55:16 +00001346
drh108aa002015-08-24 20:21:20 +00001347%type collate {int}
1348collate(C) ::= . {C = 0;}
dan59ff4252018-06-29 17:44:52 +00001349collate(C) ::= COLLATE ids. {C = 1;}
drha34001c2007-02-02 12:44:37 +00001350
drh348784e2000-05-29 20:41:49 +00001351
drh8aff1012001-12-22 14:49:24 +00001352///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +00001353//
drh4d91a702006-01-04 15:54:36 +00001354cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
drh982cef72000-05-30 16:27:03 +00001355
drh382c0242001-10-06 16:33:02 +00001356///////////////////////////// The VACUUM command /////////////////////////////
1357//
drh154d4b22006-09-21 11:02:16 +00001358%ifndef SQLITE_OMIT_VACUUM
drhfdbcdee2007-03-27 14:44:50 +00001359%ifndef SQLITE_OMIT_ATTACH
drh9ef5e772016-08-19 14:20:56 +00001360cmd ::= VACUUM. {sqlite3Vacuum(pParse,0);}
1361cmd ::= VACUUM nm(X). {sqlite3Vacuum(pParse,&X);}
drhfdbcdee2007-03-27 14:44:50 +00001362%endif SQLITE_OMIT_ATTACH
drh154d4b22006-09-21 11:02:16 +00001363%endif SQLITE_OMIT_VACUUM
drhf57b14a2001-09-14 18:54:08 +00001364
drh382c0242001-10-06 16:33:02 +00001365///////////////////////////// The PRAGMA command /////////////////////////////
1366//
drh13d70422004-11-13 15:59:14 +00001367%ifndef SQLITE_OMIT_PRAGMA
drhada2ee02009-04-03 01:43:57 +00001368cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1369cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drha3eb4b42007-01-27 02:38:29 +00001370cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drhada2ee02009-04-03 01:43:57 +00001371cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1372 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1373cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1374 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1375
drhcf82f0d2016-02-17 04:33:10 +00001376nmnum(A) ::= plus_num(A).
1377nmnum(A) ::= nm(A).
1378nmnum(A) ::= ON(A).
1379nmnum(A) ::= DELETE(A).
1380nmnum(A) ::= DEFAULT(A).
drh154d4b22006-09-21 11:02:16 +00001381%endif SQLITE_OMIT_PRAGMA
drhf59b12f2014-01-11 03:54:05 +00001382%token_class number INTEGER|FLOAT.
drh8395b7b2012-01-28 19:44:22 +00001383plus_num(A) ::= PLUS number(X). {A = X;}
drhcf82f0d2016-02-17 04:33:10 +00001384plus_num(A) ::= number(A).
drhf57b14a2001-09-14 18:54:08 +00001385minus_num(A) ::= MINUS number(X). {A = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +00001386//////////////////////////// The CREATE TRIGGER command /////////////////////
drhf0f258b2003-04-21 18:48:45 +00001387
drhb7f91642004-10-31 02:22:47 +00001388%ifndef SQLITE_OMIT_TRIGGER
1389
drhd9da78a2009-03-24 15:08:09 +00001390cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
drh4b59ab52002-08-24 18:24:51 +00001391 Token all;
1392 all.z = A.z;
drhb27b7f52008-12-10 18:03:45 +00001393 all.n = (int)(Z.z - A.z) + Z.n;
danielk19774adee202004-05-08 08:23:19 +00001394 sqlite3FinishTrigger(pParse, S, &all);
drhf0f258b2003-04-21 18:48:45 +00001395}
1396
drhfdd48a72006-09-11 23:45:48 +00001397trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1398 trigger_time(C) trigger_event(D)
drh60218d22007-04-06 11:26:00 +00001399 ON fullname(E) foreach_clause when_clause(G). {
1400 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
drhcf82f0d2016-02-17 04:33:10 +00001401 A = (Z.n==0?B:Z); /*A-overwrites-T*/
danielk1977c3f9bad2002-05-15 08:30:12 +00001402}
1403
drhc4dd3fd2008-01-22 01:48:05 +00001404%type trigger_time {int}
drh6559e2c2017-06-28 14:26:37 +00001405trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
danielk1977c3f9bad2002-05-15 08:30:12 +00001406trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1407trigger_time(A) ::= . { A = TK_BEFORE; }
1408
drhad3cab52002-05-24 02:04:32 +00001409%type trigger_event {struct TrigEvent}
drh633e6d52008-07-28 19:34:53 +00001410%destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
drhcf82f0d2016-02-17 04:33:10 +00001411trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1412trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1413trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +00001414
drh60218d22007-04-06 11:26:00 +00001415foreach_clause ::= .
1416foreach_clause ::= FOR EACH ROW.
danielk1977c3f9bad2002-05-15 08:30:12 +00001417
drh0bb132b2004-07-20 14:06:51 +00001418%type when_clause {Expr*}
drh633e6d52008-07-28 19:34:53 +00001419%destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001420when_clause(A) ::= . { A = 0; }
drh1be266b2017-12-24 00:18:47 +00001421when_clause(A) ::= WHEN expr(X). { A = X; }
danielk1977c3f9bad2002-05-15 08:30:12 +00001422
drh0bb132b2004-07-20 14:06:51 +00001423%type trigger_cmd_list {TriggerStep*}
drh633e6d52008-07-28 19:34:53 +00001424%destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +00001425trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1426 assert( A!=0 );
1427 A->pLast->pNext = X;
1428 A->pLast = X;
drha69d9162003-04-17 22:57:53 +00001429}
drh4dd0d3f2016-02-17 01:18:33 +00001430trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1431 assert( A!=0 );
1432 A->pLast = A;
drh81238962008-08-11 14:26:35 +00001433}
danielk1977c3f9bad2002-05-15 08:30:12 +00001434
drhb1819a02009-07-03 15:37:27 +00001435// Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1436// within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1437// the same database as the table that the trigger fires on.
1438//
1439%type trnm {Token}
drh4dd0d3f2016-02-17 01:18:33 +00001440trnm(A) ::= nm(A).
drhb1819a02009-07-03 15:37:27 +00001441trnm(A) ::= nm DOT nm(X). {
1442 A = X;
1443 sqlite3ErrorMsg(pParse,
1444 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1445 "statements within triggers");
1446}
1447
1448// Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1449// statements within triggers. We make a specific error message for this
1450// since it is an exception to the default grammar rules.
1451//
1452tridxby ::= .
1453tridxby ::= INDEXED BY nm. {
1454 sqlite3ErrorMsg(pParse,
1455 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1456 "within triggers");
1457}
1458tridxby ::= NOT INDEXED. {
1459 sqlite3ErrorMsg(pParse,
1460 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1461 "within triggers");
1462}
1463
1464
1465
drh0bb132b2004-07-20 14:06:51 +00001466%type trigger_cmd {TriggerStep*}
drh633e6d52008-07-28 19:34:53 +00001467%destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001468// UPDATE
drhb1819a02009-07-03 15:37:27 +00001469trigger_cmd(A) ::=
drhf259df52017-12-27 20:38:35 +00001470 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z) scanpt(E).
1471 {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R, B.z, E);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001472
1473// INSERT
drhf259df52017-12-27 20:38:35 +00001474trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
drh2c2e8442018-04-07 15:04:05 +00001475 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
drh46d2e5c2018-04-12 13:15:43 +00001476 A = sqlite3TriggerInsertStep(pParse->db,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
drh2c2e8442018-04-07 15:04:05 +00001477}
danielk1977c3f9bad2002-05-15 08:30:12 +00001478// DELETE
drhf259df52017-12-27 20:38:35 +00001479trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
1480 {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y, B.z, E);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001481
1482// SELECT
drhf259df52017-12-27 20:38:35 +00001483trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1484 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
danielk1977c3f9bad2002-05-15 08:30:12 +00001485
danielk19776f349032002-06-11 02:25:40 +00001486// The special RAISE expression that may occur in trigger programs
drh1be266b2017-12-24 00:18:47 +00001487expr(A) ::= RAISE LP IGNORE RP. {
1488 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1489 if( A ){
1490 A->affinity = OE_Ignore;
drh8aa34ae2006-03-13 12:54:09 +00001491 }
drh4b59ab52002-08-24 18:24:51 +00001492}
drh1be266b2017-12-24 00:18:47 +00001493expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1494 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1495 if( A ) {
1496 A->affinity = (char)T;
drh8aa34ae2006-03-13 12:54:09 +00001497 }
drh4b59ab52002-08-24 18:24:51 +00001498}
drh154d4b22006-09-21 11:02:16 +00001499%endif !SQLITE_OMIT_TRIGGER
drhb7f91642004-10-31 02:22:47 +00001500
drh74ad7fe2004-10-07 03:06:28 +00001501%type raisetype {int}
1502raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1503raisetype(A) ::= ABORT. {A = OE_Abort;}
1504raisetype(A) ::= FAIL. {A = OE_Fail;}
1505
danielk19776f349032002-06-11 02:25:40 +00001506
danielk1977c3f9bad2002-05-15 08:30:12 +00001507//////////////////////// DROP TRIGGER statement //////////////////////////////
drhb7f91642004-10-31 02:22:47 +00001508%ifndef SQLITE_OMIT_TRIGGER
drhfdd48a72006-09-11 23:45:48 +00001509cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1510 sqlite3DropTrigger(pParse,X,NOERR);
danielk1977c3f9bad2002-05-15 08:30:12 +00001511}
drh154d4b22006-09-21 11:02:16 +00001512%endif !SQLITE_OMIT_TRIGGER
drh113088e2003-03-20 01:16:58 +00001513
1514//////////////////////// ATTACH DATABASE file AS name /////////////////////////
drhfdbcdee2007-03-27 14:44:50 +00001515%ifndef SQLITE_OMIT_ATTACH
danielk1977f744bb52005-12-06 17:19:11 +00001516cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
drh1be266b2017-12-24 00:18:47 +00001517 sqlite3Attach(pParse, F, D, K);
drh1c2d8412003-03-31 00:30:47 +00001518}
drhfdbcdee2007-03-27 14:44:50 +00001519cmd ::= DETACH database_kw_opt expr(D). {
drh1be266b2017-12-24 00:18:47 +00001520 sqlite3Detach(pParse, D);
drhfdbcdee2007-03-27 14:44:50 +00001521}
1522
drhc4dd3fd2008-01-22 01:48:05 +00001523%type key_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +00001524%destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
danielk1977f744bb52005-12-06 17:19:11 +00001525key_opt(A) ::= . { A = 0; }
drh1be266b2017-12-24 00:18:47 +00001526key_opt(A) ::= KEY expr(X). { A = X; }
drh113088e2003-03-20 01:16:58 +00001527
1528database_kw_opt ::= DATABASE.
1529database_kw_opt ::= .
drhfdbcdee2007-03-27 14:44:50 +00001530%endif SQLITE_OMIT_ATTACH
drh4343fea2004-11-05 23:46:15 +00001531
1532////////////////////////// REINDEX collation //////////////////////////////////
1533%ifndef SQLITE_OMIT_REINDEX
1534cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1535cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
drh154d4b22006-09-21 11:02:16 +00001536%endif SQLITE_OMIT_REINDEX
danielk19779fd2a9a2004-11-12 13:42:30 +00001537
drh9f18e8a2005-07-08 12:13:04 +00001538/////////////////////////////////// ANALYZE ///////////////////////////////////
1539%ifndef SQLITE_OMIT_ANALYZE
1540cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1541cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1542%endif
1543
danielk19779fd2a9a2004-11-12 13:42:30 +00001544//////////////////////// ALTER TABLE table ... ////////////////////////////////
1545%ifndef SQLITE_OMIT_ALTERTABLE
1546cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1547 sqlite3AlterRenameTable(pParse,X,&Z);
1548}
drh986dde72016-02-29 13:37:21 +00001549cmd ::= ALTER TABLE add_column_fullname
1550 ADD kwcolumn_opt columnname(Y) carglist. {
1551 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
danielk197719a8e7e2005-03-17 05:03:38 +00001552 sqlite3AlterFinishAddColumn(pParse, &Y);
1553}
1554add_column_fullname ::= fullname(X). {
drh4a642b62016-02-05 01:55:27 +00001555 disableLookaside(pParse);
danielk197719a8e7e2005-03-17 05:03:38 +00001556 sqlite3AlterBeginAddColumn(pParse, X);
1557}
1558kwcolumn_opt ::= .
1559kwcolumn_opt ::= COLUMNKW.
drh154d4b22006-09-21 11:02:16 +00001560%endif SQLITE_OMIT_ALTERTABLE
drhe09daa92006-06-10 13:29:31 +00001561
1562//////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1563%ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001564cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1565cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
drhb421b892012-01-28 19:41:53 +00001566create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1567 nm(X) dbnm(Y) USING nm(Z). {
1568 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
drhb9bb7c12006-06-11 23:41:55 +00001569}
drhe09daa92006-06-10 13:29:31 +00001570vtabarglist ::= vtabarg.
1571vtabarglist ::= vtabarglist COMMA vtabarg.
drhb9bb7c12006-06-11 23:41:55 +00001572vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1573vtabarg ::= vtabarg vtabargtoken.
1574vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1575vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1576lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1577anylist ::= .
drhaaac8b42009-05-11 18:22:30 +00001578anylist ::= anylist LP anylist RP.
1579anylist ::= anylist ANY.
drh154d4b22006-09-21 11:02:16 +00001580%endif SQLITE_OMIT_VIRTUALTABLE
drh8b471862014-01-11 13:22:17 +00001581
1582
1583//////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
dan7d562db2014-01-11 19:19:36 +00001584%type wqlist {With*}
dan4e9119d2014-01-13 15:12:23 +00001585%destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
dan7d562db2014-01-11 19:19:36 +00001586
drha5746e02018-04-09 20:36:09 +00001587with ::= .
drh8b471862014-01-11 13:22:17 +00001588%ifndef SQLITE_OMIT_CTE
drha5746e02018-04-09 20:36:09 +00001589with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1590with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
dan7d562db2014-01-11 19:19:36 +00001591
drh108aa002015-08-24 20:21:20 +00001592wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
drhcf82f0d2016-02-17 04:33:10 +00001593 A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
dan7d562db2014-01-11 19:19:36 +00001594}
drh4dd0d3f2016-02-17 01:18:33 +00001595wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1596 A = sqlite3WithAdd(pParse, A, &X, Y, Z);
drh8b471862014-01-11 13:22:17 +00001597}
1598%endif SQLITE_OMIT_CTE
dan34a7d792018-06-29 20:43:33 +00001599
1600//////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
dan6e2210e2018-06-30 18:54:56 +00001601// These must be at the end of this file. Specifically, the rules that
1602// introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1603// the integer values assigned to these tokens to be larger than all other
1604// tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
dan34a7d792018-06-29 20:43:33 +00001605//
1606%ifndef SQLITE_OMIT_WINDOWFUNC
1607%type windowdefn_list {Window*}
1608%destructor windowdefn_list {sqlite3WindowDelete(pParse->db, $$);}
1609windowdefn_list(A) ::= windowdefn(Z). { A = Z; }
1610windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
1611 if( Z ) Z->pNextWin = Y;
1612 A = Z;
1613}
1614
1615%type windowdefn {Window*}
1616%destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
1617windowdefn(A) ::= nm(X) AS window(Y). {
1618 if( Y ){
1619 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1620 }
1621 A = Y;
1622}
1623
dan34a7d792018-06-29 20:43:33 +00001624%type window {Window*}
1625%destructor window {sqlite3WindowDelete(pParse->db, $$);}
1626
1627%type frame_opt {Window*}
1628%destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1629
1630%type window_or_nm {Window*}
1631%destructor window_or_nm {
1632sqlite3WindowDelete(pParse->db, $$);}
1633
1634%type part_opt {ExprList*}
1635%destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1636
1637%type filter_opt {Expr*}
1638%destructor filter_opt {sqlite3ExprDelete(pParse->db, $$);}
1639
1640%type range_or_rows {int}
1641
1642%type frame_bound {struct FrameBound}
1643%destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1644
dan34a7d792018-06-29 20:43:33 +00001645window_or_nm(A) ::= window(Z). {A = Z;}
1646window_or_nm(A) ::= nm(Z). {
1647 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1648 if( A ){
1649 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
1650 }
1651}
1652
1653window(A) ::= LP part_opt(X) orderby_opt(Y) frame_opt(Z) RP. {
1654 A = Z;
1655 if( A ){
1656 A->pPartition = X;
1657 A->pOrderBy = Y;
1658 }
1659}
1660
1661part_opt(A) ::= PARTITION BY exprlist(X). { A = X; }
1662part_opt(A) ::= . { A = 0; }
dan34a7d792018-06-29 20:43:33 +00001663
1664frame_opt(A) ::= . {
1665 A = sqlite3WindowAlloc(pParse, TK_RANGE, TK_UNBOUNDED, 0, TK_CURRENT, 0);
1666}
1667frame_opt(A) ::= range_or_rows(X) frame_bound(Y). {
1668 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0);
1669}
1670frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound(Y) AND frame_bound(Z). {
1671 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr);
1672}
1673
1674range_or_rows(A) ::= RANGE. { A = TK_RANGE; }
1675range_or_rows(A) ::= ROWS. { A = TK_ROWS; }
1676
1677frame_bound(A) ::= UNBOUNDED PRECEDING. { A.eType = TK_UNBOUNDED; A.pExpr = 0; }
1678frame_bound(A) ::= expr(X) PRECEDING. { A.eType = TK_PRECEDING; A.pExpr = X; }
1679frame_bound(A) ::= CURRENT ROW. { A.eType = TK_CURRENT ; A.pExpr = 0; }
1680frame_bound(A) ::= expr(X) FOLLOWING. { A.eType = TK_FOLLOWING; A.pExpr = X; }
1681frame_bound(A) ::= UNBOUNDED FOLLOWING. { A.eType = TK_UNBOUNDED; A.pExpr = 0; }
1682
1683%type windowdefn_opt {Window*}
1684%destructor windowdefn_opt {sqlite3WindowDelete(pParse->db, $$);}
1685windowdefn_opt(A) ::= . { A = 0; }
1686windowdefn_opt(A) ::= WINDOW windowdefn_list(B). { A = B; }
dan6e2210e2018-06-30 18:54:56 +00001687
1688%type over_opt {Window*}
1689%destructor over_opt {sqlite3WindowDelete(pParse->db, $$);}
1690over_opt(A) ::= . { A = 0; }
1691over_opt(A) ::= filter_opt(W) OVER window_or_nm(Z). {
1692 A = Z;
1693 if( A ) A->pFilter = W;
1694}
1695
1696filter_opt(A) ::= . { A = 0; }
1697filter_opt(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
dan34a7d792018-06-29 20:43:33 +00001698%endif // SQLITE_OMIT_WINDOWFUNC
1699