blob: 7689288db630438f9117d9e37a6b3bb190d814bc [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){
drh31f69622019-10-05 14:39:36 +0000109 sqlite3 *db = pParse->db;
drh4a642b62016-02-05 01:55:27 +0000110 pParse->disableLookaside++;
drh31f69622019-10-05 14:39:36 +0000111 DisableLookaside;
drh4a642b62016-02-05 01:55:27 +0000112}
113
drhcaec2f12003-01-07 02:47:47 +0000114} // end %include
drh348784e2000-05-29 20:41:49 +0000115
drh826fb5a2004-02-14 23:59:57 +0000116// Input is a single SQL command
drhc4a3c772001-04-04 11:48:57 +0000117input ::= cmdlist.
drh094b2bb2002-03-13 18:54:07 +0000118cmdlist ::= cmdlist ecmd.
drh826fb5a2004-02-14 23:59:57 +0000119cmdlist ::= ecmd.
drhb7f91642004-10-31 02:22:47 +0000120ecmd ::= SEMI.
drh2424aa72018-04-11 17:10:54 +0000121ecmd ::= cmdx SEMI.
drhb7f91642004-10-31 02:22:47 +0000122%ifndef SQLITE_OMIT_EXPLAIN
drh2424aa72018-04-11 17:10:54 +0000123ecmd ::= explain cmdx.
drh8549d552016-01-07 17:09:43 +0000124explain ::= EXPLAIN. { pParse->explain = 1; }
125explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; }
drh154d4b22006-09-21 11:02:16 +0000126%endif SQLITE_OMIT_EXPLAIN
drh200a81d2008-08-08 14:19:41 +0000127cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
drh348784e2000-05-29 20:41:49 +0000128
drh382c0242001-10-06 16:33:02 +0000129///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +0000130//
drhfa86c412002-02-02 15:01:15 +0000131
drh684917c2004-10-05 02:41:42 +0000132cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
drhc4a3c772001-04-04 11:48:57 +0000133trans_opt ::= .
134trans_opt ::= TRANSACTION.
drh5ad1a6c2002-07-01 12:27:09 +0000135trans_opt ::= TRANSACTION nm.
drh684917c2004-10-05 02:41:42 +0000136%type transtype {int}
137transtype(A) ::= . {A = TK_DEFERRED;}
drhcf82f0d2016-02-17 04:33:10 +0000138transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/}
139transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
140transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
drh07a3b112017-07-06 01:28:02 +0000141cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
142cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
drhc4a3c772001-04-04 11:48:57 +0000143
danielk1977fd7f0452008-12-17 17:30:26 +0000144savepoint_opt ::= SAVEPOINT.
145savepoint_opt ::= .
146cmd ::= SAVEPOINT nm(X). {
147 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
148}
149cmd ::= RELEASE savepoint_opt nm(X). {
150 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
151}
152cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
153 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
154}
155
drh382c0242001-10-06 16:33:02 +0000156///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +0000157//
158cmd ::= create_table create_table_args.
drhd9da78a2009-03-24 15:08:09 +0000159create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
danielk1977f1a381e2006-06-16 08:01:02 +0000160 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
drh969fa7c2002-02-18 18:30:32 +0000161}
drhdabd04c2016-02-17 01:46:19 +0000162createkw(A) ::= CREATE(A). {disableLookaside(pParse);}
163
drhfaa59552005-12-29 23:33:54 +0000164%type ifnotexists {int}
165ifnotexists(A) ::= . {A = 0;}
166ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
drhf57b3392001-10-08 13:22:32 +0000167%type temp {int}
danielk197753c0f742005-03-29 03:10:59 +0000168%ifndef SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000169temp(A) ::= TEMP. {A = 1;}
drh154d4b22006-09-21 11:02:16 +0000170%endif SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000171temp(A) ::= . {A = 0;}
drh5969da42013-10-21 02:14:45 +0000172create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
173 sqlite3EndTable(pParse,&X,&E,F,0);
drh969fa7c2002-02-18 18:30:32 +0000174}
175create_table_args ::= AS select(S). {
drh5969da42013-10-21 02:14:45 +0000176 sqlite3EndTable(pParse,0,0,0,S);
drh633e6d52008-07-28 19:34:53 +0000177 sqlite3SelectDelete(pParse->db, S);
drh969fa7c2002-02-18 18:30:32 +0000178}
drh3334d082015-11-10 13:45:21 +0000179%type table_options {int}
drh5969da42013-10-21 02:14:45 +0000180table_options(A) ::= . {A = 0;}
181table_options(A) ::= WITHOUT nm(X). {
182 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
drhfccda8a2015-05-27 13:06:55 +0000183 A = TF_WithoutRowid | TF_NoVisibleRowid;
drh5969da42013-10-21 02:14:45 +0000184 }else{
185 A = 0;
186 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
187 }
188}
drh986dde72016-02-29 13:37:21 +0000189columnlist ::= columnlist COMMA columnname carglist.
190columnlist ::= columnname carglist.
drh2881ab62016-02-27 23:25:36 +0000191columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
drhc4a3c772001-04-04 11:48:57 +0000192
drh6a8700b2017-08-02 11:04:00 +0000193// Declare some tokens early in order to influence their values, to
194// improve performance and reduce the executable size. The goal here is
195// to get the "jump" operations in ISNULL through ESCAPE to have numeric
196// values that are early enough so that all jump operations are clustered
drh7bbdc3c2019-04-05 21:17:11 +0000197// at the beginning.
drh6a8700b2017-08-02 11:04:00 +0000198//
199%token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
200%token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
201%token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
202%token GT LE LT GE ESCAPE.
203
drh31d6fd52017-04-14 19:03:10 +0000204// The following directive causes tokens ABORT, AFTER, ASC, etc. to
205// fallback to ID if they will not parse as their original value.
206// This obviates the need for the "id" nonterminal.
207//
208%fallback ID
209 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
drh0a6259f2018-04-16 13:26:53 +0000210 CONFLICT DATABASE DEFERRED DESC DETACH DO
drh6cd7d482018-04-12 15:43:05 +0000211 EACH END EXCLUSIVE EXPLAIN FAIL FOR
drh31d6fd52017-04-14 19:03:10 +0000212 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
dan86fb6e12018-05-16 20:58:07 +0000213 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
drh31d6fd52017-04-14 19:03:10 +0000214 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
dan6e118922019-08-12 16:36:38 +0000215 NULLS FIRST LAST
drh31d6fd52017-04-14 19:03:10 +0000216%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
danced89122019-03-19 06:40:29 +0000221 EXCLUDE GROUPS OTHERS TIES
drh3773c252018-06-28 03:38:49 +0000222%endif SQLITE_OMIT_WINDOWFUNC
drh31d6fd52017-04-14 19:03:10 +0000223 REINDEX RENAME CTIME_KW IF
224 .
225%wildcard ANY.
226
drhf7b54962013-05-28 12:11:54 +0000227// Define operator precedence early so that this is the first occurrence
drh2d3917d2004-02-22 16:27:00 +0000228// of the operator tokens in the grammer. Keeping the operators together
229// causes them to be assigned integer values that are close together,
230// which keeps parser tables smaller.
231//
drhf2bc0132004-10-04 13:19:23 +0000232// The token values assigned to these symbols is determined by the order
233// in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
234// NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
235// the sqlite3ExprIfFalse() routine for additional information on this
236// constraint.
237//
drh2d3917d2004-02-22 16:27:00 +0000238%left OR.
239%left AND.
240%right NOT.
drh03bea702006-06-13 15:37:26 +0000241%left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
drh9a432672004-10-04 13:38:09 +0000242%left GT LE LT GE.
danielk19777c6303c2004-11-17 16:41:29 +0000243%right ESCAPE.
drh2d3917d2004-02-22 16:27:00 +0000244%left BITAND BITOR LSHIFT RSHIFT.
245%left PLUS MINUS.
246%left STAR SLASH REM.
drha34001c2007-02-02 12:44:37 +0000247%left CONCAT.
248%left COLLATE.
drh7ba5bc52009-09-22 20:08:34 +0000249%right BITNOT.
drh26cf56f2018-04-06 19:36:49 +0000250%nonassoc ON.
drh2d3917d2004-02-22 16:27:00 +0000251
drh7cc84c22016-04-11 13:36:42 +0000252// An IDENTIFIER can be a generic identifier, or one of several
253// keywords. Any non-standard keyword can also be an identifier.
254//
255%token_class id ID|INDEXED.
256
drh7cc84c22016-04-11 13:36:42 +0000257
drhc4a3c772001-04-04 11:48:57 +0000258// And "ids" is an identifer-or-string.
259//
dan59ff4252018-06-29 17:44:52 +0000260%token_class ids ID|STRING.
drhc4a3c772001-04-04 11:48:57 +0000261
drh5ad1a6c2002-07-01 12:27:09 +0000262// The name of a column or table can be any of the following:
263//
264%type nm {Token}
drh4dd0d3f2016-02-17 01:18:33 +0000265nm(A) ::= id(A).
266nm(A) ::= STRING(A).
267nm(A) ::= JOIN_KW(A).
drh5ad1a6c2002-07-01 12:27:09 +0000268
drh986dde72016-02-29 13:37:21 +0000269// A typetoken is really zero or more tokens that form a type name such
drh487e2622005-06-25 18:42:14 +0000270// as can be found after the column name in a CREATE TABLE statement.
271// Multiple tokens are concatenated to form the value of the typetoken.
272//
273%type typetoken {Token}
drh986dde72016-02-29 13:37:21 +0000274typetoken(A) ::= . {A.n = 0; A.z = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000275typetoken(A) ::= typename(A).
276typetoken(A) ::= typename(A) LP signed RP(Y). {
277 A.n = (int)(&Y.z[Y.n] - A.z);
drh487e2622005-06-25 18:42:14 +0000278}
drh4dd0d3f2016-02-17 01:18:33 +0000279typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
280 A.n = (int)(&Y.z[Y.n] - A.z);
drh487e2622005-06-25 18:42:14 +0000281}
drh382c0242001-10-06 16:33:02 +0000282%type typename {Token}
dan59ff4252018-06-29 17:44:52 +0000283typename(A) ::= ids(A).
284typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
drh60218d22007-04-06 11:26:00 +0000285signed ::= plus_num.
286signed ::= minus_num.
drh487e2622005-06-25 18:42:14 +0000287
drh1be266b2017-12-24 00:18:47 +0000288// The scanpt non-terminal takes a value which is a pointer to the
289// input text just past the last token that has been shifted into
290// the parser. By surrounding some phrase in the grammar with two
291// scanpt non-terminals, we can capture the input text for that phrase.
292// For example:
293//
294// something ::= .... scanpt(A) phrase scanpt(Z).
295//
296// The text that is parsed as "phrase" is a string starting at A
297// and containing (int)(Z-A) characters. There might be some extra
298// whitespace on either end of the text, but that can be removed in
299// post-processing, if needed.
300//
301%type scanpt {const char*}
302scanpt(A) ::= . {
drhf259df52017-12-27 20:38:35 +0000303 assert( yyLookahead!=YYNOCODE );
304 A = yyLookaheadToken.z;
drh1be266b2017-12-24 00:18:47 +0000305}
dan4db4b5b2019-05-18 19:49:08 +0000306scantok(A) ::= . {
307 assert( yyLookahead!=YYNOCODE );
308 A = yyLookaheadToken;
309}
drh1be266b2017-12-24 00:18:47 +0000310
drh487e2622005-06-25 18:42:14 +0000311// "carglist" is a list of additional constraints that come after the
312// column name and column type in a CREATE TABLE statement.
313//
drh4dc330d2012-05-07 19:21:36 +0000314carglist ::= carglist ccons.
drh348784e2000-05-29 20:41:49 +0000315carglist ::= .
drh4dc330d2012-05-07 19:21:36 +0000316ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
dan4db4b5b2019-05-18 19:49:08 +0000317ccons ::= DEFAULT scantok(A) term(X).
318 {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);}
drh1be266b2017-12-24 00:18:47 +0000319ccons ::= DEFAULT LP(A) expr(X) RP(Z).
320 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
dan4db4b5b2019-05-18 19:49:08 +0000321ccons ::= DEFAULT PLUS(A) scantok(Z) term(X).
322 {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);}
323ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). {
drh1be266b2017-12-24 00:18:47 +0000324 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
dan4db4b5b2019-05-18 19:49:08 +0000325 sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]);
danielk19777977a172004-11-09 12:44:37 +0000326}
dan4db4b5b2019-05-18 19:49:08 +0000327ccons ::= DEFAULT scantok id(X). {
drh1be266b2017-12-24 00:18:47 +0000328 Expr *p = tokenExpr(pParse, TK_STRING, X);
drhd7fd8992018-02-28 04:30:55 +0000329 if( p ){
330 sqlite3ExprIdToTrueFalse(p);
331 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
332 }
danc9461ec2018-08-29 21:00:16 +0000333 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
danielk19777977a172004-11-09 12:44:37 +0000334}
drh348784e2000-05-29 20:41:49 +0000335
drh382c0242001-10-06 16:33:02 +0000336// In addition to the type name, we also care about the primary key and
337// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000338//
drh0d316a42002-08-11 20:10:47 +0000339ccons ::= NULL onconf.
drhb7916a72009-05-27 10:31:29 +0000340ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
drhfdd6e852005-12-16 01:06:16 +0000341ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
drhb7916a72009-05-27 10:31:29 +0000342 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
drh62340f82016-05-31 21:18:15 +0000343ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
344 SQLITE_IDXTYPE_UNIQUE);}
drh1be266b2017-12-24 00:18:47 +0000345ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);}
drh108aa002015-08-24 20:21:20 +0000346ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
drhb7916a72009-05-27 10:31:29 +0000347 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
348ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
dan59ff4252018-06-29 17:44:52 +0000349ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);}
drh81f7b372019-10-16 12:18:59 +0000350ccons ::= GENERATED ALWAYS AS generated.
351ccons ::= AS generated.
drh7e508f12019-10-16 19:31:46 +0000352generated ::= LP expr(E) RP. {sqlite3AddGenerated(pParse,E,0);}
353generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);}
drh04738cb2002-06-02 18:19:00 +0000354
drh205f48e2004-11-05 00:43:11 +0000355// The optional AUTOINCREMENT keyword
356%type autoinc {int}
drh2958a4e2004-11-12 03:56:15 +0000357autoinc(X) ::= . {X = 0;}
358autoinc(X) ::= AUTOINCR. {X = 1;}
drh205f48e2004-11-05 00:43:11 +0000359
drhc2eef3b2002-08-31 18:53:06 +0000360// The next group of rules parses the arguments to a REFERENCES clause
361// that determine if the referential integrity checking is deferred or
362// or immediate and which determine what action to take if a ref-integ
363// check fails.
drh04738cb2002-06-02 18:19:00 +0000364//
drhc2eef3b2002-08-31 18:53:06 +0000365%type refargs {int}
drhfcf486c2009-10-21 13:48:24 +0000366refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */}
drh4dd0d3f2016-02-17 01:18:33 +0000367refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
drhc2eef3b2002-08-31 18:53:06 +0000368%type refarg {struct {int value; int mask;}}
369refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
drhc29c5aa12009-12-09 21:43:36 +0000370refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; }
drhc2eef3b2002-08-31 18:53:06 +0000371refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
372refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
drhc2eef3b2002-08-31 18:53:06 +0000373%type refact {int}
drhfcf486c2009-10-21 13:48:24 +0000374refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */}
375refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */}
376refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */}
377refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */}
378refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */}
drhc2eef3b2002-08-31 18:53:06 +0000379%type defer_subclause {int}
dan1da40a32009-09-19 17:00:31 +0000380defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;}
drhc2eef3b2002-08-31 18:53:06 +0000381defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
382%type init_deferred_pred_opt {int}
383init_deferred_pred_opt(A) ::= . {A = 0;}
384init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
385init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000386
drhaeb281c2012-05-08 11:17:33 +0000387conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000388conslist_opt(A) ::= COMMA(A) conslist.
drhab35eae2012-05-12 18:29:53 +0000389conslist ::= conslist tconscomma tcons.
390conslist ::= tcons.
391tconscomma ::= COMMA. {pParse->constraintName.n = 0;}
392tconscomma ::= .
393tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
drh108aa002015-08-24 20:21:20 +0000394tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
drhb7916a72009-05-27 10:31:29 +0000395 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
drh108aa002015-08-24 20:21:20 +0000396tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
drh62340f82016-05-31 21:18:15 +0000397 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
398 SQLITE_IDXTYPE_UNIQUE);}
drhb7916a72009-05-27 10:31:29 +0000399tcons ::= CHECK LP expr(E) RP onconf.
drh1be266b2017-12-24 00:18:47 +0000400 {sqlite3AddCheckConstraint(pParse,E);}
drh108aa002015-08-24 20:21:20 +0000401tcons ::= FOREIGN KEY LP eidlist(FA) RP
402 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
danielk19774adee202004-05-08 08:23:19 +0000403 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
404 sqlite3DeferForeignKey(pParse, D);
drhc2eef3b2002-08-31 18:53:06 +0000405}
406%type defer_subclause_opt {int}
407defer_subclause_opt(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000408defer_subclause_opt(A) ::= defer_subclause(A).
drh9cfcf5d2002-01-29 18:41:24 +0000409
410// The following is a non-standard extension that allows us to declare the
411// default behavior when there is a constraint conflict.
412//
413%type onconf {int}
drh3334d082015-11-10 13:45:21 +0000414%type orconf {int}
drh1c928532002-01-31 15:54:21 +0000415%type resolvetype {int}
drh74ad7fe2004-10-07 03:06:28 +0000416onconf(A) ::= . {A = OE_Default;}
417onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
418orconf(A) ::= . {A = OE_Default;}
drh3334d082015-11-10 13:45:21 +0000419orconf(A) ::= OR resolvetype(X). {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000420resolvetype(A) ::= raisetype(A).
drh74ad7fe2004-10-07 03:06:28 +0000421resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
422resolvetype(A) ::= REPLACE. {A = OE_Replace;}
drh348784e2000-05-29 20:41:49 +0000423
drh382c0242001-10-06 16:33:02 +0000424////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000425//
drha0733842005-12-29 01:11:36 +0000426cmd ::= DROP TABLE ifexists(E) fullname(X). {
427 sqlite3DropTable(pParse, X, 0, E);
danielk1977a8858102004-05-28 12:11:21 +0000428}
drha0733842005-12-29 01:11:36 +0000429%type ifexists {int}
430ifexists(A) ::= IF EXISTS. {A = 1;}
431ifexists(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000432
drha76b5df2002-02-23 02:32:10 +0000433///////////////////// The CREATE VIEW statement /////////////////////////////
434//
drhb7f91642004-10-31 02:22:47 +0000435%ifndef SQLITE_OMIT_VIEW
drh108aa002015-08-24 20:21:20 +0000436cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
drh8981b902015-08-24 17:42:49 +0000437 AS select(S). {
438 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
drha76b5df2002-02-23 02:32:10 +0000439}
drha0733842005-12-29 01:11:36 +0000440cmd ::= DROP VIEW ifexists(E) fullname(X). {
441 sqlite3DropTable(pParse, X, 1, E);
drha76b5df2002-02-23 02:32:10 +0000442}
drh154d4b22006-09-21 11:02:16 +0000443%endif SQLITE_OMIT_VIEW
drha76b5df2002-02-23 02:32:10 +0000444
drh382c0242001-10-06 16:33:02 +0000445//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000446//
dan7d562db2014-01-11 19:19:36 +0000447cmd ::= select(X). {
drhedf83d12014-01-22 18:31:27 +0000448 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
drh7d10d5a2008-08-20 16:35:10 +0000449 sqlite3Select(pParse, X, &dest);
drh633e6d52008-07-28 19:34:53 +0000450 sqlite3SelectDelete(pParse->db, X);
drh9bb61fe2000-06-05 16:01:39 +0000451}
drhefb72512000-05-31 20:00:52 +0000452
drh9bb61fe2000-06-05 16:01:39 +0000453%type select {Select*}
drh633e6d52008-07-28 19:34:53 +0000454%destructor select {sqlite3SelectDelete(pParse->db, $$);}
dan7d562db2014-01-11 19:19:36 +0000455%type selectnowith {Select*}
456%destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
drh82c3d632000-06-06 21:56:07 +0000457%type oneselect {Select*}
drh633e6d52008-07-28 19:34:53 +0000458%destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
drh9bb61fe2000-06-05 16:01:39 +0000459
drh772460f2015-04-16 14:13:12 +0000460%include {
461 /*
462 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
463 ** all elements in the list. And make sure list length does not exceed
464 ** SQLITE_LIMIT_COMPOUND_SELECT.
465 */
drhe318a7f2015-04-16 23:04:17 +0000466 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
drh55f66b32019-07-16 19:44:32 +0000467 assert( p!=0 );
drhd227a292014-02-09 18:02:09 +0000468 if( p->pPrior ){
drh772460f2015-04-16 14:13:12 +0000469 Select *pNext = 0, *pLoop;
470 int mxSelect, cnt = 0;
drhd227a292014-02-09 18:02:09 +0000471 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
472 pLoop->pNext = pNext;
473 pLoop->selFlags |= SF_Compound;
474 }
drh772460f2015-04-16 14:13:12 +0000475 if( (p->selFlags & SF_MultiValue)==0 &&
476 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
477 cnt>mxSelect
drha0c01762015-01-05 16:27:43 +0000478 ){
drhd227a292014-02-09 18:02:09 +0000479 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
480 }
481 }
drh772460f2015-04-16 14:13:12 +0000482 }
483}
484
drh6a8b94e2018-05-30 01:14:20 +0000485%ifndef SQLITE_OMIT_CTE
drha5746e02018-04-09 20:36:09 +0000486select(A) ::= WITH wqlist(W) selectnowith(X). {
drh772460f2015-04-16 14:13:12 +0000487 Select *p = X;
488 if( p ){
489 p->pWith = W;
490 parserDoubleLinkSelect(pParse, p);
dana9f5c132014-01-13 16:36:40 +0000491 }else{
492 sqlite3WithDelete(pParse->db, W);
493 }
drha5746e02018-04-09 20:36:09 +0000494 A = p;
495}
496select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). {
497 Select *p = X;
498 if( p ){
499 p->pWith = W;
500 parserDoubleLinkSelect(pParse, p);
501 }else{
502 sqlite3WithDelete(pParse->db, W);
503 }
504 A = p;
505}
drh6a8b94e2018-05-30 01:14:20 +0000506%endif /* SQLITE_OMIT_CTE */
drha5746e02018-04-09 20:36:09 +0000507select(A) ::= selectnowith(X). {
508 Select *p = X;
509 if( p ){
510 parserDoubleLinkSelect(pParse, p);
511 }
512 A = p; /*A-overwrites-X*/
dana9f5c132014-01-13 16:36:40 +0000513}
dan7d562db2014-01-11 19:19:36 +0000514
drh4dd0d3f2016-02-17 01:18:33 +0000515selectnowith(A) ::= oneselect(A).
drhb7f91642004-10-31 02:22:47 +0000516%ifndef SQLITE_OMIT_COMPOUND_SELECT
drh4dd0d3f2016-02-17 01:18:33 +0000517selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
drhc0bf4932014-02-19 01:31:02 +0000518 Select *pRhs = Z;
drh4dd0d3f2016-02-17 01:18:33 +0000519 Select *pLhs = A;
drhc0bf4932014-02-19 01:31:02 +0000520 if( pRhs && pRhs->pPrior ){
521 SrcList *pFrom;
522 Token x;
523 x.n = 0;
drh772460f2015-04-16 14:13:12 +0000524 parserDoubleLinkSelect(pParse, pRhs);
drhc0bf4932014-02-19 01:31:02 +0000525 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
drh8c0833f2017-11-14 23:48:23 +0000526 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
drhc0bf4932014-02-19 01:31:02 +0000527 }
528 if( pRhs ){
529 pRhs->op = (u8)Y;
drh00d5ab72015-05-20 00:15:27 +0000530 pRhs->pPrior = pLhs;
531 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
drh772460f2015-04-16 14:13:12 +0000532 pRhs->selFlags &= ~SF_MultiValue;
drhd58d3272013-08-05 22:05:02 +0000533 if( Y!=TK_ALL ) pParse->hasCompound = 1;
drh43b78822007-06-15 17:03:14 +0000534 }else{
drh00d5ab72015-05-20 00:15:27 +0000535 sqlite3SelectDelete(pParse->db, pLhs);
drhdaffd0e2001-04-11 14:28:42 +0000536 }
drhc0bf4932014-02-19 01:31:02 +0000537 A = pRhs;
drh82c3d632000-06-06 21:56:07 +0000538}
drh0a36c572002-02-18 22:49:59 +0000539%type multiselect_op {int}
drhcf82f0d2016-02-17 04:33:10 +0000540multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
drhfd405312005-11-06 04:06:59 +0000541multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
drhcf82f0d2016-02-17 04:33:10 +0000542multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
drh154d4b22006-09-21 11:02:16 +0000543%endif SQLITE_OMIT_COMPOUND_SELECT
drh550a3302018-07-27 22:14:50 +0000544
drhfef37762018-07-10 19:48:35 +0000545oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
dan67a9b8e2018-06-22 20:51:35 +0000546 groupby_opt(P) having_opt(Q)
dane3bf6322018-06-08 20:58:27 +0000547 orderby_opt(Z) limit_opt(L). {
drh8c0833f2017-11-14 23:48:23 +0000548 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
drh550a3302018-07-27 22:14:50 +0000549}
550%ifndef SQLITE_OMIT_WINDOWFUNC
551oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
552 groupby_opt(P) having_opt(Q) window_clause(R)
553 orderby_opt(Z) limit_opt(L). {
554 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
dan6fde1792018-06-15 19:01:35 +0000555 if( A ){
556 A->pWinDefn = R;
557 }else{
558 sqlite3WindowListDelete(pParse->db, R);
559 }
drh9bb61fe2000-06-05 16:01:39 +0000560}
drh550a3302018-07-27 22:14:50 +0000561%endif
562
563
drh4dd0d3f2016-02-17 01:18:33 +0000564oneselect(A) ::= values(A).
drh75593d92014-01-10 20:46:55 +0000565
566%type values {Select*}
567%destructor values {sqlite3SelectDelete(pParse->db, $$);}
568values(A) ::= VALUES LP nexprlist(X) RP. {
drh8c0833f2017-11-14 23:48:23 +0000569 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
drh75593d92014-01-10 20:46:55 +0000570}
drh954733b2018-07-27 23:33:16 +0000571values(A) ::= values(A) COMMA LP nexprlist(Y) RP. {
drh4dd0d3f2016-02-17 01:18:33 +0000572 Select *pRight, *pLeft = A;
drh8c0833f2017-11-14 23:48:23 +0000573 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
drhf3151f02015-04-16 20:27:09 +0000574 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
drh75593d92014-01-10 20:46:55 +0000575 if( pRight ){
576 pRight->op = TK_ALL;
drh772460f2015-04-16 14:13:12 +0000577 pRight->pPrior = pLeft;
drh75593d92014-01-10 20:46:55 +0000578 A = pRight;
579 }else{
drh772460f2015-04-16 14:13:12 +0000580 A = pLeft;
drh75593d92014-01-10 20:46:55 +0000581 }
582}
drh9bb61fe2000-06-05 16:01:39 +0000583
584// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
585// present and false (0) if it is not.
586//
drh3334d082015-11-10 13:45:21 +0000587%type distinct {int}
drh832ee3d2012-12-18 19:36:11 +0000588distinct(A) ::= DISTINCT. {A = SF_Distinct;}
drh7cea7f92015-05-29 01:35:19 +0000589distinct(A) ::= ALL. {A = SF_All;}
drhefb72512000-05-31 20:00:52 +0000590distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000591
drh9bb61fe2000-06-05 16:01:39 +0000592// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000593// values of the SELECT statement. The "*" in statements like
594// "SELECT * FROM ..." is encoded as a special expression with an
drh1a1d3cd2015-11-19 16:33:31 +0000595// opcode of TK_ASTERISK.
drh9bb61fe2000-06-05 16:01:39 +0000596//
drh348784e2000-05-29 20:41:49 +0000597%type selcollist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000598%destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000599%type sclp {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000600%destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +0000601sclp(A) ::= selcollist(A) COMMA.
drh348784e2000-05-29 20:41:49 +0000602sclp(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000603selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
604 A = sqlite3ExprListAppend(pParse, A, X);
drhb7916a72009-05-27 10:31:29 +0000605 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
drh1be266b2017-12-24 00:18:47 +0000606 sqlite3ExprListSetSpan(pParse,A,B,Z);
drh01f3f252002-05-24 16:14:15 +0000607}
drhd3f5d612017-12-24 17:01:54 +0000608selcollist(A) ::= sclp(A) scanpt STAR. {
drh1a1d3cd2015-11-19 16:33:31 +0000609 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
drh4dd0d3f2016-02-17 01:18:33 +0000610 A = sqlite3ExprListAppend(pParse, A, p);
drh7c917d12001-12-16 20:05:05 +0000611}
drh1be266b2017-12-24 00:18:47 +0000612selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
drhabfd35e2016-12-06 22:47:23 +0000613 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
614 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
615 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
drh4dd0d3f2016-02-17 01:18:33 +0000616 A = sqlite3ExprListAppend(pParse,A, pDot);
drh54473222002-04-04 02:10:55 +0000617}
drh01f3f252002-05-24 16:14:15 +0000618
619// An option "AS <id>" phrase that can follow one of the expressions that
620// define the result set, or one of the tables in the FROM clause.
621//
622%type as {Token}
drh74ad7fe2004-10-07 03:06:28 +0000623as(X) ::= AS nm(Y). {X = Y;}
drh4dd0d3f2016-02-17 01:18:33 +0000624as(X) ::= ids(X).
drh986dde72016-02-29 13:37:21 +0000625as(X) ::= . {X.n = 0; X.z = 0;}
drh9bb61fe2000-06-05 16:01:39 +0000626
drh348784e2000-05-29 20:41:49 +0000627
drhad3cab52002-05-24 02:04:32 +0000628%type seltablist {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000629%destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
drhad3cab52002-05-24 02:04:32 +0000630%type stl_prefix {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000631%destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
drhad3cab52002-05-24 02:04:32 +0000632%type from {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000633%destructor from {sqlite3SrcListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000634
drh01f3f252002-05-24 16:14:15 +0000635// A complete FROM clause.
636//
drh17435752007-08-16 04:30:38 +0000637from(A) ::= . {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));}
drhfbdc7f62008-12-03 23:23:40 +0000638from(A) ::= FROM seltablist(X). {
drh61dfc312006-12-16 16:25:15 +0000639 A = X;
640 sqlite3SrcListShiftJoinType(A);
641}
drh01f3f252002-05-24 16:14:15 +0000642
643// "seltablist" is a "Select Table List" - the content of the FROM clause
644// in a SELECT statement. "stl_prefix" is a prefix of this list.
645//
drh4dd0d3f2016-02-17 01:18:33 +0000646stl_prefix(A) ::= seltablist(A) joinop(Y). {
drh8a48b9c2015-08-19 15:20:00 +0000647 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
drh01f3f252002-05-24 16:14:15 +0000648}
drh348784e2000-05-29 20:41:49 +0000649stl_prefix(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000650seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
drhe9240412012-12-18 13:12:03 +0000651 on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000652 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
danielk1977b1c685b2008-10-06 16:18:39 +0000653 sqlite3SrcListIndexedBy(pParse, A, &I);
drhc4a3c772001-04-04 11:48:57 +0000654}
drh4dd0d3f2016-02-17 01:18:33 +0000655seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
drh01d230c2015-08-19 17:11:37 +0000656 on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000657 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
drh01d230c2015-08-19 17:11:37 +0000658 sqlite3SrcListFuncArgs(pParse, A, E);
659}
drh51522cd2005-01-20 13:36:19 +0000660%ifndef SQLITE_OMIT_SUBQUERY
drh4dd0d3f2016-02-17 01:18:33 +0000661 seltablist(A) ::= stl_prefix(A) LP select(S) RP
drh51522cd2005-01-20 13:36:19 +0000662 as(Z) on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000663 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
drhd5feede2002-05-08 21:46:14 +0000664 }
drh4dd0d3f2016-02-17 01:18:33 +0000665 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
drhfbdc7f62008-12-03 23:23:40 +0000666 as(Z) on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000667 if( A==0 && Z.n==0 && N==0 && U==0 ){
drhfbdc7f62008-12-03 23:23:40 +0000668 A = F;
drh832ee3d2012-12-18 19:36:11 +0000669 }else if( F->nSrc==1 ){
drh4dd0d3f2016-02-17 01:18:33 +0000670 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
drh832ee3d2012-12-18 19:36:11 +0000671 if( A ){
672 struct SrcList_item *pNew = &A->a[A->nSrc-1];
673 struct SrcList_item *pOld = F->a;
674 pNew->zName = pOld->zName;
675 pNew->zDatabase = pOld->zDatabase;
drh3c449c62013-04-30 14:06:57 +0000676 pNew->pSelect = pOld->pSelect;
drh4a5cff72018-12-03 01:47:41 +0000677 if( pOld->fg.isTabFunc ){
678 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
679 pOld->u1.pFuncArg = 0;
680 pOld->fg.isTabFunc = 0;
681 pNew->fg.isTabFunc = 1;
682 }
drh832ee3d2012-12-18 19:36:11 +0000683 pOld->zName = pOld->zDatabase = 0;
drh3c449c62013-04-30 14:06:57 +0000684 pOld->pSelect = 0;
drh832ee3d2012-12-18 19:36:11 +0000685 }
686 sqlite3SrcListDelete(pParse->db, F);
drhfbdc7f62008-12-03 23:23:40 +0000687 }else{
688 Select *pSubquery;
689 sqlite3SrcListShiftJoinType(F);
drh8c0833f2017-11-14 23:48:23 +0000690 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
drh4dd0d3f2016-02-17 01:18:33 +0000691 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
drhfbdc7f62008-12-03 23:23:40 +0000692 }
693 }
drh154d4b22006-09-21 11:02:16 +0000694%endif SQLITE_OMIT_SUBQUERY
drhb733d032004-01-24 20:18:12 +0000695
drh113088e2003-03-20 01:16:58 +0000696%type dbnm {Token}
697dbnm(A) ::= . {A.z=0; A.n=0;}
698dbnm(A) ::= DOT nm(X). {A = X;}
699
drh74ad7fe2004-10-07 03:06:28 +0000700%type fullname {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000701%destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
danc9461ec2018-08-29 21:00:16 +0000702fullname(A) ::= nm(X). {
drh29c992c2019-01-17 15:40:41 +0000703 A = sqlite3SrcListAppend(pParse,0,&X,0);
danc9461ec2018-08-29 21:00:16 +0000704 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);
705}
706fullname(A) ::= nm(X) DOT nm(Y). {
drh29c992c2019-01-17 15:40:41 +0000707 A = sqlite3SrcListAppend(pParse,0,&X,&Y);
danc9461ec2018-08-29 21:00:16 +0000708 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);
709}
drh74ad7fe2004-10-07 03:06:28 +0000710
drh5e3a6eb2018-04-19 11:45:16 +0000711%type xfullname {SrcList*}
712%destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
713xfullname(A) ::= nm(X).
drh29c992c2019-01-17 15:40:41 +0000714 {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/}
drh5e3a6eb2018-04-19 11:45:16 +0000715xfullname(A) ::= nm(X) DOT nm(Y).
drh29c992c2019-01-17 15:40:41 +0000716 {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/}
drh5e3a6eb2018-04-19 11:45:16 +0000717xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
drh29c992c2019-01-17 15:40:41 +0000718 A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/
drh5e3a6eb2018-04-19 11:45:16 +0000719 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
720}
721xfullname(A) ::= nm(X) AS nm(Z). {
drh29c992c2019-01-17 15:40:41 +0000722 A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/
drh5e3a6eb2018-04-19 11:45:16 +0000723 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
724}
725
drh01f3f252002-05-24 16:14:15 +0000726%type joinop {int}
drhfd405312005-11-06 04:06:59 +0000727joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
drhcf82f0d2016-02-17 04:33:10 +0000728joinop(X) ::= JOIN_KW(A) JOIN.
729 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
730joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
731 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
drh5ad1a6c2002-07-01 12:27:09 +0000732joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
drhcf82f0d2016-02-17 04:33:10 +0000733 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
drh01f3f252002-05-24 16:14:15 +0000734
drh26cf56f2018-04-06 19:36:49 +0000735// There is a parsing abiguity in an upsert statement that uses a
736// SELECT on the RHS of a the INSERT:
737//
738// INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
739// here ----^^
740//
741// When the ON token is encountered, the parser does not know if it is
742// the beginning of an ON CONFLICT clause, or the beginning of an ON
743// clause associated with the JOIN. The conflict is resolved in favor
744// of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
745// WHERE clause in between, like this:
746//
747// INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
748//
749// The [AND] and [OR] precedence marks in the rules for on_opt cause the
750// ON in this context to always be interpreted as belonging to the JOIN.
751//
drh01f3f252002-05-24 16:14:15 +0000752%type on_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000753%destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
drh26cf56f2018-04-06 19:36:49 +0000754on_opt(N) ::= ON expr(E). {N = E;}
755on_opt(N) ::= . [OR] {N = 0;}
drh01f3f252002-05-24 16:14:15 +0000756
danielk197785574e32008-10-06 05:32:18 +0000757// Note that this block abuses the Token type just a little. If there is
758// no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
759// there is an INDEXED BY clause, then the token is populated as per normal,
760// with z pointing to the token data and n containing the number of bytes
761// in the token.
762//
763// If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
danielk1977b1c685b2008-10-06 16:18:39 +0000764// normally illegal. The sqlite3SrcListIndexedBy() function
danielk197785574e32008-10-06 05:32:18 +0000765// recognizes and interprets this as a special case.
766//
767%type indexed_opt {Token}
768indexed_opt(A) ::= . {A.z=0; A.n=0;}
769indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
770indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
771
drh01f3f252002-05-24 16:14:15 +0000772%type using_opt {IdList*}
drh633e6d52008-07-28 19:34:53 +0000773%destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
drh81eba732013-10-19 23:31:56 +0000774using_opt(U) ::= USING LP idlist(L) RP. {U = L;}
drh01f3f252002-05-24 16:14:15 +0000775using_opt(U) ::= . {U = 0;}
776
777
drh348784e2000-05-29 20:41:49 +0000778%type orderby_opt {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000779%destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
drh108aa002015-08-24 20:21:20 +0000780
781// the sortlist non-terminal stores a list of expression where each
782// expression is optionally followed by ASC or DESC to indicate the
783// sort order.
784//
drh348784e2000-05-29 20:41:49 +0000785%type sortlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000786%destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000787
788orderby_opt(A) ::= . {A = 0;}
789orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
dan6e118922019-08-12 16:36:38 +0000790sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
drh1be266b2017-12-24 00:18:47 +0000791 A = sqlite3ExprListAppend(pParse,A,Y);
dan6e118922019-08-12 16:36:38 +0000792 sqlite3ExprListSetSortOrder(A,Z,X);
drh9bb61fe2000-06-05 16:01:39 +0000793}
dan6e118922019-08-12 16:36:38 +0000794sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
drh1be266b2017-12-24 00:18:47 +0000795 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
dan6e118922019-08-12 16:36:38 +0000796 sqlite3ExprListSetSortOrder(A,Z,X);
drh9bb61fe2000-06-05 16:01:39 +0000797}
drh348784e2000-05-29 20:41:49 +0000798
799%type sortorder {int}
800
drh8e2ca022002-06-17 17:07:19 +0000801sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
802sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
drhbc622bc2015-08-24 15:39:42 +0000803sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
drh348784e2000-05-29 20:41:49 +0000804
dan6e118922019-08-12 16:36:38 +0000805%type nulls {int}
806nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;}
807nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;}
808nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;}
809
drh22827922000-06-06 17:27:05 +0000810%type groupby_opt {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000811%destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
drh6206d502000-06-19 19:09:08 +0000812groupby_opt(A) ::= . {A = 0;}
drh9245c242007-06-20 12:18:31 +0000813groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
drh22827922000-06-06 17:27:05 +0000814
815%type having_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000816%destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
drh6206d502000-06-19 19:09:08 +0000817having_opt(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000818having_opt(A) ::= HAVING expr(X). {A = X;}
drh22827922000-06-06 17:27:05 +0000819
drh8c0833f2017-11-14 23:48:23 +0000820%type limit_opt {Expr*}
drh15926592007-04-06 15:02:13 +0000821
822// The destructor for limit_opt will never fire in the current grammar.
823// The limit_opt non-terminal only occurs at the end of a single production
824// rule for SELECT statements. As soon as the rule that create the
825// limit_opt non-terminal reduces, the SELECT statement rule will also
826// reduce. So there is never a limit_opt non-terminal on the stack
827// except as a transient. So there is never anything to destroy.
828//
drh8c0833f2017-11-14 23:48:23 +0000829//%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
830limit_opt(A) ::= . {A = 0;}
831limit_opt(A) ::= LIMIT expr(X).
drh1be266b2017-12-24 00:18:47 +0000832 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
danielk1977a2dc3b12005-02-05 12:48:48 +0000833limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
drh1be266b2017-12-24 00:18:47 +0000834 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
danielk1977a2dc3b12005-02-05 12:48:48 +0000835limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
drh1be266b2017-12-24 00:18:47 +0000836 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
drh9bbca4c2001-11-06 04:00:18 +0000837
drh382c0242001-10-06 16:33:02 +0000838/////////////////////////// The DELETE statement /////////////////////////////
839//
shane273f6192008-10-10 04:34:16 +0000840%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
drh5e3a6eb2018-04-19 11:45:16 +0000841cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W)
drh931577f2008-10-10 14:27:16 +0000842 orderby_opt(O) limit_opt(L). {
danielk1977b1c685b2008-10-06 16:18:39 +0000843 sqlite3SrcListIndexedBy(pParse, X, &I);
drh6a0db872019-01-31 02:42:47 +0000844#ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
845 sqlite3ExprListDelete(pParse->db, O); O = 0;
846 sqlite3ExprDelete(pParse->db, L); L = 0;
847#endif
drh8c0833f2017-11-14 23:48:23 +0000848 sqlite3DeleteFrom(pParse,X,W,O,L);
danielk1977b1c685b2008-10-06 16:18:39 +0000849}
shane4281bd42008-10-07 05:27:11 +0000850%endif
shane273f6192008-10-10 04:34:16 +0000851%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
drh5e3a6eb2018-04-19 11:45:16 +0000852cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W). {
shane4281bd42008-10-07 05:27:11 +0000853 sqlite3SrcListIndexedBy(pParse, X, &I);
drh8c0833f2017-11-14 23:48:23 +0000854 sqlite3DeleteFrom(pParse,X,W,0,0);
shane4281bd42008-10-07 05:27:11 +0000855}
856%endif
drh348784e2000-05-29 20:41:49 +0000857
858%type where_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000859%destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000860
861where_opt(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000862where_opt(A) ::= WHERE expr(X). {A = X;}
drh348784e2000-05-29 20:41:49 +0000863
drh382c0242001-10-06 16:33:02 +0000864////////////////////////// The UPDATE command ////////////////////////////////
865//
shane273f6192008-10-10 04:34:16 +0000866%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
drh5e3a6eb2018-04-19 11:45:16 +0000867cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
drh8b471862014-01-11 13:22:17 +0000868 where_opt(W) orderby_opt(O) limit_opt(L). {
danielk1977b1c685b2008-10-06 16:18:39 +0000869 sqlite3SrcListIndexedBy(pParse, X, &I);
drhb1a6c3c2008-03-20 16:30:17 +0000870 sqlite3ExprListCheckLength(pParse,Y,"set list");
drheac9fab2018-04-16 13:00:50 +0000871 sqlite3Update(pParse,X,Y,W,R,O,L,0);
danielk19777a15a4b2007-05-08 17:54:43 +0000872}
shane4281bd42008-10-07 05:27:11 +0000873%endif
shane273f6192008-10-10 04:34:16 +0000874%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
drh5e3a6eb2018-04-19 11:45:16 +0000875cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
drhe9240412012-12-18 13:12:03 +0000876 where_opt(W). {
shane4281bd42008-10-07 05:27:11 +0000877 sqlite3SrcListIndexedBy(pParse, X, &I);
878 sqlite3ExprListCheckLength(pParse,Y,"set list");
drheac9fab2018-04-16 13:00:50 +0000879 sqlite3Update(pParse,X,Y,W,R,0,0,0);
shane4281bd42008-10-07 05:27:11 +0000880}
881%endif
drh348784e2000-05-29 20:41:49 +0000882
drhf8db1bc2005-04-22 02:38:37 +0000883%type setlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000884%destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
drhf8db1bc2005-04-22 02:38:37 +0000885
drh4dd0d3f2016-02-17 01:18:33 +0000886setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000887 A = sqlite3ExprListAppend(pParse, A, Y);
drhb7916a72009-05-27 10:31:29 +0000888 sqlite3ExprListSetName(pParse, A, &X, 1);
889}
drha1251bc2016-08-20 00:51:37 +0000890setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000891 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
drha1251bc2016-08-20 00:51:37 +0000892}
drhb7916a72009-05-27 10:31:29 +0000893setlist(A) ::= nm(X) EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000894 A = sqlite3ExprListAppend(pParse, 0, Y);
drhb7916a72009-05-27 10:31:29 +0000895 sqlite3ExprListSetName(pParse, A, &X, 1);
896}
drha1251bc2016-08-20 00:51:37 +0000897setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000898 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
drha1251bc2016-08-20 00:51:37 +0000899}
drh348784e2000-05-29 20:41:49 +0000900
drh382c0242001-10-06 16:33:02 +0000901////////////////////////// The INSERT command /////////////////////////////////
902//
drh5e3a6eb2018-04-19 11:45:16 +0000903cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
drh2c2e8442018-04-07 15:04:05 +0000904 upsert(U). {
drh46d2e5c2018-04-12 13:15:43 +0000905 sqlite3Insert(pParse, X, S, F, R, U);
dan4e9119d2014-01-13 15:12:23 +0000906}
drh5e3a6eb2018-04-19 11:45:16 +0000907cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES.
dan4e9119d2014-01-13 15:12:23 +0000908{
drh2c2e8442018-04-07 15:04:05 +0000909 sqlite3Insert(pParse, X, 0, F, R, 0);
dan4e9119d2014-01-13 15:12:23 +0000910}
drh348784e2000-05-29 20:41:49 +0000911
drh46d2e5c2018-04-12 13:15:43 +0000912%type upsert {Upsert*}
drh54514c92018-04-17 21:59:34 +0000913
914// Because upsert only occurs at the tip end of the INSERT rule for cmd,
915// there is never a case where the value of the upsert pointer will not
916// be destroyed by the cmd action. So comment-out the destructor to
917// avoid unreachable code.
918//%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
drh46d2e5c2018-04-12 13:15:43 +0000919upsert(A) ::= . { A = 0; }
drhe9c2e772018-04-13 13:06:45 +0000920upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
drhdab0eb52018-04-12 17:28:06 +0000921 DO UPDATE SET setlist(Z) where_opt(W).
drhe9c2e772018-04-13 13:06:45 +0000922 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W);}
drhe9c2e772018-04-13 13:06:45 +0000923upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING.
924 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0); }
925upsert(A) ::= ON CONFLICT DO NOTHING.
926 { A = sqlite3UpsertNew(pParse->db,0,0,0,0); }
drh26cf56f2018-04-06 19:36:49 +0000927
drh3334d082015-11-10 13:45:21 +0000928%type insert_cmd {int}
drhfa86c412002-02-02 15:01:15 +0000929insert_cmd(A) ::= INSERT orconf(R). {A = R;}
930insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
931
drh8981b902015-08-24 17:42:49 +0000932%type idlist_opt {IdList*}
933%destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
drh81eba732013-10-19 23:31:56 +0000934%type idlist {IdList*}
935%destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000936
drh8981b902015-08-24 17:42:49 +0000937idlist_opt(A) ::= . {A = 0;}
938idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000939idlist(A) ::= idlist(A) COMMA nm(Y).
dan5496d6a2018-08-13 17:14:26 +0000940 {A = sqlite3IdListAppend(pParse,A,&Y);}
drh81eba732013-10-19 23:31:56 +0000941idlist(A) ::= nm(Y).
dan5496d6a2018-08-13 17:14:26 +0000942 {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}
drh348784e2000-05-29 20:41:49 +0000943
drh382c0242001-10-06 16:33:02 +0000944/////////////////////////// Expression Processing /////////////////////////////
945//
drh348784e2000-05-29 20:41:49 +0000946
drh1be266b2017-12-24 00:18:47 +0000947%type expr {Expr*}
948%destructor expr {sqlite3ExprDelete(pParse->db, $$);}
949%type term {Expr*}
950%destructor term {sqlite3ExprDelete(pParse->db, $$);}
drhb7916a72009-05-27 10:31:29 +0000951
952%include {
drhb7916a72009-05-27 10:31:29 +0000953
954 /* Construct a new Expr object from a single identifier. Use the
955 ** new Expr to populate pOut. Set the span of pOut to be the identifier
956 ** that created the expression.
957 */
drh1be266b2017-12-24 00:18:47 +0000958 static Expr *tokenExpr(Parse *pParse, int op, Token t){
drh0cd874b2016-09-26 12:38:22 +0000959 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
960 if( p ){
dand145e5f2018-08-21 08:29:48 +0000961 /* memset(p, 0, sizeof(Expr)); */
drh0cd874b2016-09-26 12:38:22 +0000962 p->op = (u8)op;
drh11949042019-08-05 18:01:42 +0000963 p->affExpr = 0;
drh0cd874b2016-09-26 12:38:22 +0000964 p->flags = EP_Leaf;
965 p->iAgg = -1;
dand145e5f2018-08-21 08:29:48 +0000966 p->pLeft = p->pRight = 0;
967 p->x.pList = 0;
968 p->pAggInfo = 0;
drheda079c2018-09-20 19:02:15 +0000969 p->y.pTab = 0;
dand145e5f2018-08-21 08:29:48 +0000970 p->op2 = 0;
drh4fe759b2018-08-23 18:22:00 +0000971 p->iTable = 0;
drhd3130da2018-08-23 18:50:19 +0000972 p->iColumn = 0;
drh0cd874b2016-09-26 12:38:22 +0000973 p->u.zToken = (char*)&p[1];
974 memcpy(p->u.zToken, t.z, t.n);
975 p->u.zToken[t.n] = 0;
976 if( sqlite3Isquote(p->u.zToken[0]) ){
drh51d35b02019-01-11 13:32:23 +0000977 sqlite3DequoteExpr(p);
drh0cd874b2016-09-26 12:38:22 +0000978 }
979#if SQLITE_MAX_EXPR_DEPTH>0
980 p->nHeight = 1;
981#endif
danc9461ec2018-08-29 21:00:16 +0000982 if( IN_RENAME_OBJECT ){
dan07e95232018-08-21 16:32:53 +0000983 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
dand145e5f2018-08-21 08:29:48 +0000984 }
drh0cd874b2016-09-26 12:38:22 +0000985 }
drh1be266b2017-12-24 00:18:47 +0000986 return p;
drhb7916a72009-05-27 10:31:29 +0000987 }
dand145e5f2018-08-21 08:29:48 +0000988
drhb7916a72009-05-27 10:31:29 +0000989}
drh348784e2000-05-29 20:41:49 +0000990
drh4dd0d3f2016-02-17 01:18:33 +0000991expr(A) ::= term(A).
drh1be266b2017-12-24 00:18:47 +0000992expr(A) ::= LP expr(X) RP. {A = X;}
993expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
994expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
drh5ad1a6c2002-07-01 12:27:09 +0000995expr(A) ::= nm(X) DOT nm(Y). {
drh410c3012016-09-24 17:42:43 +0000996 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
997 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
danc9461ec2018-08-29 21:00:16 +0000998 if( IN_RENAME_OBJECT ){
999 sqlite3RenameTokenMap(pParse, (void*)temp2, &Y);
1000 sqlite3RenameTokenMap(pParse, (void*)temp1, &X);
1001 }
drh1be266b2017-12-24 00:18:47 +00001002 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
drhe1b6a5b2000-07-29 13:06:59 +00001003}
drhd24cc422003-03-27 12:51:24 +00001004expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
drh410c3012016-09-24 17:42:43 +00001005 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
1006 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
1007 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
drhabfd35e2016-12-06 22:47:23 +00001008 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
danc9461ec2018-08-29 21:00:16 +00001009 if( IN_RENAME_OBJECT ){
1010 sqlite3RenameTokenMap(pParse, (void*)temp3, &Z);
1011 sqlite3RenameTokenMap(pParse, (void*)temp2, &Y);
1012 }
drh1be266b2017-12-24 00:18:47 +00001013 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
drhd24cc422003-03-27 12:51:24 +00001014}
drh1be266b2017-12-24 00:18:47 +00001015term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1016term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
drh0cd874b2016-09-26 12:38:22 +00001017term(A) ::= INTEGER(X). {
drh1be266b2017-12-24 00:18:47 +00001018 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
drh0cd874b2016-09-26 12:38:22 +00001019}
drh7c972de2003-09-06 22:18:07 +00001020expr(A) ::= VARIABLE(X). {
drh8679fba2016-04-11 01:43:33 +00001021 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
drhde25a882016-10-03 15:28:24 +00001022 u32 n = X.n;
drh1be266b2017-12-24 00:18:47 +00001023 A = tokenExpr(pParse, TK_VARIABLE, X);
1024 sqlite3ExprAssignVarNumber(pParse, A, n);
drh8f3b1372016-04-11 01:26:31 +00001025 }else{
drhf59b12f2014-01-11 03:54:05 +00001026 /* When doing a nested parse, one can include terms in an expression
1027 ** that look like this: #1 #2 ... These terms refer to registers
1028 ** in the virtual machine. #N is the N-th register. */
drh8f3b1372016-04-11 01:26:31 +00001029 Token t = X; /*A-overwrites-X*/
1030 assert( t.n>=2 );
drhf59b12f2014-01-11 03:54:05 +00001031 if( pParse->nested==0 ){
drh43303de2016-02-17 12:34:03 +00001032 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
drh1be266b2017-12-24 00:18:47 +00001033 A = 0;
drhf59b12f2014-01-11 03:54:05 +00001034 }else{
drh1be266b2017-12-24 00:18:47 +00001035 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1036 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
drhf59b12f2014-01-11 03:54:05 +00001037 }
drhf59b12f2014-01-11 03:54:05 +00001038 }
drh7c972de2003-09-06 22:18:07 +00001039}
dan59ff4252018-06-29 17:44:52 +00001040expr(A) ::= expr(A) COLLATE ids(C). {
drh1be266b2017-12-24 00:18:47 +00001041 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
drh8b4c40d2007-02-01 23:02:45 +00001042}
drh487e2622005-06-25 18:42:14 +00001043%ifndef SQLITE_OMIT_CAST
drh1be266b2017-12-24 00:18:47 +00001044expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1045 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1046 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
drh487e2622005-06-25 18:42:14 +00001047}
drh154d4b22006-09-21 11:02:16 +00001048%endif SQLITE_OMIT_CAST
drh550a3302018-07-27 22:14:50 +00001049
1050
1051expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. {
drh954733b2018-07-27 23:33:16 +00001052 A = sqlite3ExprFunction(pParse, Y, &X, D);
drh550a3302018-07-27 22:14:50 +00001053}
1054expr(A) ::= id(X) LP STAR RP. {
drh954733b2018-07-27 23:33:16 +00001055 A = sqlite3ExprFunction(pParse, 0, &X, 0);
drh550a3302018-07-27 22:14:50 +00001056}
1057
dan67a9b8e2018-06-22 20:51:35 +00001058%ifndef SQLITE_OMIT_WINDOWFUNC
dan4f9adee2019-07-13 16:22:50 +00001059expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
dan64882712019-07-11 18:43:33 +00001060 A = sqlite3ExprFunction(pParse, Y, &X, D);
dan4f9adee2019-07-13 16:22:50 +00001061 sqlite3WindowAttach(pParse, A, Z);
dan64882712019-07-11 18:43:33 +00001062}
dan4f9adee2019-07-13 16:22:50 +00001063expr(A) ::= id(X) LP STAR RP filter_over(Z). {
drh954733b2018-07-27 23:33:16 +00001064 A = sqlite3ExprFunction(pParse, 0, &X, 0);
dan4f9adee2019-07-13 16:22:50 +00001065 sqlite3WindowAttach(pParse, A, Z);
drhe1b6a5b2000-07-29 13:06:59 +00001066}
drh550a3302018-07-27 22:14:50 +00001067%endif
1068
drhb71090f2005-05-23 17:26:51 +00001069term(A) ::= CTIME_KW(OP). {
drh954733b2018-07-27 23:33:16 +00001070 A = sqlite3ExprFunction(pParse, 0, &OP, 0);
drhb7916a72009-05-27 10:31:29 +00001071}
1072
drh1be266b2017-12-24 00:18:47 +00001073expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1074 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1075 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1076 if( A ){
1077 A->x.pList = pList;
drh269d3222019-10-23 18:09:39 +00001078 if( pList->nExpr ){
1079 A->flags |= pList->a[0].pExpr->flags & EP_Propagate;
1080 }
drh8bd0d582016-08-20 18:06:14 +00001081 }else{
1082 sqlite3ExprListDelete(pParse->db, pList);
dan71c57db2016-07-09 20:23:55 +00001083 }
1084}
1085
drhd5c851c2019-04-19 13:38:34 +00001086expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);}
drh1be266b2017-12-24 00:18:47 +00001087expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001088expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001089 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1090expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001091expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001092 {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001093expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001094 {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001095expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001096 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1097expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh410c3012016-09-24 17:42:43 +00001098%type likeop {Token}
drh7e84b372017-02-20 14:30:17 +00001099likeop(A) ::= LIKE_KW|MATCH(A).
drh410c3012016-09-24 17:42:43 +00001100likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
drh4dd0d3f2016-02-17 01:18:33 +00001101expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
drh8aa34ae2006-03-13 12:54:09 +00001102 ExprList *pList;
drh410c3012016-09-24 17:42:43 +00001103 int bNot = OP.n & 0x80000000;
1104 OP.n &= 0x7fffffff;
drh1be266b2017-12-24 00:18:47 +00001105 pList = sqlite3ExprListAppend(pParse,0, Y);
1106 pList = sqlite3ExprListAppend(pParse,pList, A);
drh954733b2018-07-27 23:33:16 +00001107 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
drh1be266b2017-12-24 00:18:47 +00001108 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1109 if( A ) A->flags |= EP_InfixFunc;
drh0ac65892002-04-20 14:24:41 +00001110}
drh4dd0d3f2016-02-17 01:18:33 +00001111expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
drh1dca1452010-07-19 02:30:33 +00001112 ExprList *pList;
drh410c3012016-09-24 17:42:43 +00001113 int bNot = OP.n & 0x80000000;
1114 OP.n &= 0x7fffffff;
drh1be266b2017-12-24 00:18:47 +00001115 pList = sqlite3ExprListAppend(pParse,0, Y);
1116 pList = sqlite3ExprListAppend(pParse,pList, A);
1117 pList = sqlite3ExprListAppend(pParse,pList, E);
drh954733b2018-07-27 23:33:16 +00001118 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
drh1be266b2017-12-24 00:18:47 +00001119 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1120 if( A ) A->flags |= EP_InfixFunc;
drh1dca1452010-07-19 02:30:33 +00001121}
danielk19777c6303c2004-11-17 16:41:29 +00001122
drh1be266b2017-12-24 00:18:47 +00001123expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1124expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
drh6a2fe092009-09-23 02:29:36 +00001125
drh6a517412009-11-12 03:46:34 +00001126%include {
1127 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1128 ** unary TK_ISNULL or TK_NOTNULL expression. */
1129 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1130 sqlite3 *db = pParse->db;
danc50f75d2018-09-06 18:56:36 +00001131 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
shaneh5e17e8b2009-12-03 04:40:47 +00001132 pA->op = (u8)op;
drh6a517412009-11-12 03:46:34 +00001133 sqlite3ExprDelete(db, pA->pRight);
1134 pA->pRight = 0;
1135 }
1136 }
1137}
1138
drh6a2fe092009-09-23 02:29:36 +00001139// expr1 IS expr2
1140// expr1 IS NOT expr2
1141//
1142// If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1143// is any other expression, code as TK_IS or TK_ISNOT.
1144//
drh4dd0d3f2016-02-17 01:18:33 +00001145expr(A) ::= expr(A) IS expr(Y). {
drh1be266b2017-12-24 00:18:47 +00001146 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1147 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
drh6a2fe092009-09-23 02:29:36 +00001148}
drh4dd0d3f2016-02-17 01:18:33 +00001149expr(A) ::= expr(A) IS NOT expr(Y). {
drh1be266b2017-12-24 00:18:47 +00001150 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1151 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
drh6a2fe092009-09-23 02:29:36 +00001152}
drhb7916a72009-05-27 10:31:29 +00001153
drh43303de2016-02-17 12:34:03 +00001154expr(A) ::= NOT(B) expr(X).
drh1be266b2017-12-24 00:18:47 +00001155 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
drh43303de2016-02-17 12:34:03 +00001156expr(A) ::= BITNOT(B) expr(X).
drh1be266b2017-12-24 00:18:47 +00001157 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
drhca5aa592018-06-19 11:15:19 +00001158expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1159 A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
1160 /*A-overwrites-B*/
1161}
drhb7916a72009-05-27 10:31:29 +00001162
drh2e3a1f12004-10-06 14:39:28 +00001163%type between_op {int}
1164between_op(A) ::= BETWEEN. {A = 0;}
1165between_op(A) ::= NOT BETWEEN. {A = 1;}
drh4dd0d3f2016-02-17 01:18:33 +00001166expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
drh1be266b2017-12-24 00:18:47 +00001167 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1168 pList = sqlite3ExprListAppend(pParse,pList, Y);
1169 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1170 if( A ){
1171 A->x.pList = pList;
drh53f733c2005-09-16 02:38:09 +00001172 }else{
drh633e6d52008-07-28 19:34:53 +00001173 sqlite3ExprListDelete(pParse->db, pList);
drh53f733c2005-09-16 02:38:09 +00001174 }
drh1be266b2017-12-24 00:18:47 +00001175 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drhfef52082000-06-06 01:50:43 +00001176}
drh51522cd2005-01-20 13:36:19 +00001177%ifndef SQLITE_OMIT_SUBQUERY
danielk19773e8c37e2005-01-21 03:12:14 +00001178 %type in_op {int}
1179 in_op(A) ::= IN. {A = 0;}
1180 in_op(A) ::= NOT IN. {A = 1;}
drh1be266b2017-12-24 00:18:47 +00001181 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
drh094430e2010-07-14 18:24:06 +00001182 if( Y==0 ){
dan473c1bf2010-07-15 11:14:21 +00001183 /* Expressions of the form
1184 **
1185 ** expr1 IN ()
1186 ** expr1 NOT IN ()
1187 **
1188 ** simplify to constants 0 (false) and 1 (true), respectively,
1189 ** regardless of the value of expr1.
1190 */
drh8e34e402019-06-11 10:43:56 +00001191 sqlite3ExprUnmapAndDelete(pParse, A);
drh5776ee52019-09-23 12:38:10 +00001192 A = sqlite3Expr(pParse->db, TK_INTEGER, N ? "1" : "0");
danielk1977d5d56522005-03-16 12:15:20 +00001193 }else{
drh1be266b2017-12-24 00:18:47 +00001194 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1195 if( A ){
1196 A->x.pList = Y;
1197 sqlite3ExprSetHeightAndFlags(pParse, A);
drh094430e2010-07-14 18:24:06 +00001198 }else{
1199 sqlite3ExprListDelete(pParse->db, Y);
1200 }
drh1be266b2017-12-24 00:18:47 +00001201 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
danielk1977d5d56522005-03-16 12:15:20 +00001202 }
danielk19773e8c37e2005-01-21 03:12:14 +00001203 }
drh1be266b2017-12-24 00:18:47 +00001204 expr(A) ::= LP select(X) RP. {
1205 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1206 sqlite3PExprAddSelect(pParse, A, X);
drh51522cd2005-01-20 13:36:19 +00001207 }
drh1be266b2017-12-24 00:18:47 +00001208 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1209 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1210 sqlite3PExprAddSelect(pParse, A, Y);
1211 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drh51522cd2005-01-20 13:36:19 +00001212 }
drh5fbab882016-07-02 12:08:14 +00001213 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
drh29c992c2019-01-17 15:40:41 +00001214 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
drh8c0833f2017-11-14 23:48:23 +00001215 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
drh9de47572016-07-02 12:33:21 +00001216 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
drh1be266b2017-12-24 00:18:47 +00001217 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1218 sqlite3PExprAddSelect(pParse, A, pSelect);
1219 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drh51522cd2005-01-20 13:36:19 +00001220 }
drh1be266b2017-12-24 00:18:47 +00001221 expr(A) ::= EXISTS LP select(Y) RP. {
drh43303de2016-02-17 12:34:03 +00001222 Expr *p;
drh1be266b2017-12-24 00:18:47 +00001223 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
drh08de4f72016-04-11 01:06:47 +00001224 sqlite3PExprAddSelect(pParse, p, Y);
drh51522cd2005-01-20 13:36:19 +00001225 }
drh154d4b22006-09-21 11:02:16 +00001226%endif SQLITE_OMIT_SUBQUERY
drhfef52082000-06-06 01:50:43 +00001227
drh17a7f8d2002-03-24 13:13:27 +00001228/* CASE expressions */
drh1be266b2017-12-24 00:18:47 +00001229expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1230 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1231 if( A ){
1232 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1233 sqlite3ExprSetHeightAndFlags(pParse, A);
drh53f733c2005-09-16 02:38:09 +00001234 }else{
drh633e6d52008-07-28 19:34:53 +00001235 sqlite3ExprListDelete(pParse->db, Y);
drhc5cd1242013-09-12 16:50:49 +00001236 sqlite3ExprDelete(pParse->db, Z);
drh53f733c2005-09-16 02:38:09 +00001237 }
drh17a7f8d2002-03-24 13:13:27 +00001238}
1239%type case_exprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001240%destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +00001241case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
drh1be266b2017-12-24 00:18:47 +00001242 A = sqlite3ExprListAppend(pParse,A, Y);
1243 A = sqlite3ExprListAppend(pParse,A, Z);
drh17a7f8d2002-03-24 13:13:27 +00001244}
1245case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
drh1be266b2017-12-24 00:18:47 +00001246 A = sqlite3ExprListAppend(pParse,0, Y);
1247 A = sqlite3ExprListAppend(pParse,A, Z);
drh17a7f8d2002-03-24 13:13:27 +00001248}
1249%type case_else {Expr*}
drh633e6d52008-07-28 19:34:53 +00001250%destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +00001251case_else(A) ::= ELSE expr(X). {A = X;}
drh17a7f8d2002-03-24 13:13:27 +00001252case_else(A) ::= . {A = 0;}
1253%type case_operand {Expr*}
drh633e6d52008-07-28 19:34:53 +00001254%destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +00001255case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/}
drh17a7f8d2002-03-24 13:13:27 +00001256case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +00001257
1258%type exprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001259%destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh9245c242007-06-20 12:18:31 +00001260%type nexprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001261%destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +00001262
drh4dd0d3f2016-02-17 01:18:33 +00001263exprlist(A) ::= nexprlist(A).
drh9245c242007-06-20 12:18:31 +00001264exprlist(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +00001265nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
drh1be266b2017-12-24 00:18:47 +00001266 {A = sqlite3ExprListAppend(pParse,A,Y);}
drh17435752007-08-16 04:30:38 +00001267nexprlist(A) ::= expr(Y).
drh1be266b2017-12-24 00:18:47 +00001268 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
drh9245c242007-06-20 12:18:31 +00001269
drha5224732016-07-25 14:40:43 +00001270%ifndef SQLITE_OMIT_SUBQUERY
drh5fbab882016-07-02 12:08:14 +00001271/* A paren_exprlist is an optional expression list contained inside
1272** of parenthesis */
1273%type paren_exprlist {ExprList*}
1274%destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1275paren_exprlist(A) ::= . {A = 0;}
1276paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
drha5224732016-07-25 14:40:43 +00001277%endif SQLITE_OMIT_SUBQUERY
drh5fbab882016-07-02 12:08:14 +00001278
drhcce7d172000-05-31 15:34:51 +00001279
drh382c0242001-10-06 16:33:02 +00001280///////////////////////////// The CREATE INDEX command ///////////////////////
1281//
drhd9da78a2009-03-24 15:08:09 +00001282cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
drh108aa002015-08-24 20:21:20 +00001283 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
drh17435752007-08-16 04:30:38 +00001284 sqlite3CreateIndex(pParse, &X, &D,
drh29c992c2019-01-17 15:40:41 +00001285 sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
drh62340f82016-05-31 21:18:15 +00001286 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
danc9461ec2018-08-29 21:00:16 +00001287 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
1288 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
1289 }
drh9cfcf5d2002-01-29 18:41:24 +00001290}
drh717e6402001-09-27 03:22:32 +00001291
1292%type uniqueflag {int}
drh74ad7fe2004-10-07 03:06:28 +00001293uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1294uniqueflag(A) ::= . {A = OE_None;}
drh348784e2000-05-29 20:41:49 +00001295
drh348784e2000-05-29 20:41:49 +00001296
drh108aa002015-08-24 20:21:20 +00001297// The eidlist non-terminal (Expression Id List) generates an ExprList
1298// from a list of identifiers. The identifier names are in ExprList.a[].zName.
1299// This list is stored in an ExprList rather than an IdList so that it
1300// can be easily sent to sqlite3ColumnsExprList().
1301//
1302// eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1303// used for the arguments to an index. That is just an historical accident.
1304//
1305// IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1306// COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1307// places - places that might have been stored in the sqlite_master schema.
1308// Those extra features were ignored. But because they might be in some
1309// (busted) old databases, we need to continue parsing them when loading
1310// historical schemas.
1311//
1312%type eidlist {ExprList*}
1313%destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1314%type eidlist_opt {ExprList*}
1315%destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1316
1317%include {
1318 /* Add a single new term to an ExprList that is used to store a
1319 ** list of identifiers. Report an error if the ID list contains
1320 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1321 ** error while parsing a legacy schema.
1322 */
1323 static ExprList *parserAddExprIdListTerm(
1324 Parse *pParse,
1325 ExprList *pPrior,
1326 Token *pIdToken,
1327 int hasCollate,
1328 int sortOrder
1329 ){
1330 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1331 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1332 && pParse->db->init.busy==0
1333 ){
1334 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1335 pIdToken->n, pIdToken->z);
1336 }
1337 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1338 return p;
1339 }
1340} // end %include
1341
1342eidlist_opt(A) ::= . {A = 0;}
1343eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +00001344eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1345 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
danielk19770202b292004-06-09 09:55:16 +00001346}
drh108aa002015-08-24 20:21:20 +00001347eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
drhcf82f0d2016-02-17 04:33:10 +00001348 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
danielk19770202b292004-06-09 09:55:16 +00001349}
danielk19770202b292004-06-09 09:55:16 +00001350
drh108aa002015-08-24 20:21:20 +00001351%type collate {int}
1352collate(C) ::= . {C = 0;}
dan59ff4252018-06-29 17:44:52 +00001353collate(C) ::= COLLATE ids. {C = 1;}
drha34001c2007-02-02 12:44:37 +00001354
drh348784e2000-05-29 20:41:49 +00001355
drh8aff1012001-12-22 14:49:24 +00001356///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +00001357//
drh4d91a702006-01-04 15:54:36 +00001358cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
drh982cef72000-05-30 16:27:03 +00001359
drh382c0242001-10-06 16:33:02 +00001360///////////////////////////// The VACUUM command /////////////////////////////
1361//
drh154d4b22006-09-21 11:02:16 +00001362%ifndef SQLITE_OMIT_VACUUM
drhfdbcdee2007-03-27 14:44:50 +00001363%ifndef SQLITE_OMIT_ATTACH
drh2f6239e2018-12-08 00:43:08 +00001364%type vinto {Expr*}
1365%destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
1366cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);}
1367cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);}
1368vinto(A) ::= INTO expr(X). {A = X;}
1369vinto(A) ::= . {A = 0;}
drhfdbcdee2007-03-27 14:44:50 +00001370%endif SQLITE_OMIT_ATTACH
drh154d4b22006-09-21 11:02:16 +00001371%endif SQLITE_OMIT_VACUUM
drhf57b14a2001-09-14 18:54:08 +00001372
drh382c0242001-10-06 16:33:02 +00001373///////////////////////////// The PRAGMA command /////////////////////////////
1374//
drh13d70422004-11-13 15:59:14 +00001375%ifndef SQLITE_OMIT_PRAGMA
drhada2ee02009-04-03 01:43:57 +00001376cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1377cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drha3eb4b42007-01-27 02:38:29 +00001378cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drhada2ee02009-04-03 01:43:57 +00001379cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1380 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1381cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1382 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1383
drhcf82f0d2016-02-17 04:33:10 +00001384nmnum(A) ::= plus_num(A).
1385nmnum(A) ::= nm(A).
1386nmnum(A) ::= ON(A).
1387nmnum(A) ::= DELETE(A).
1388nmnum(A) ::= DEFAULT(A).
drh154d4b22006-09-21 11:02:16 +00001389%endif SQLITE_OMIT_PRAGMA
drhf59b12f2014-01-11 03:54:05 +00001390%token_class number INTEGER|FLOAT.
drh8395b7b2012-01-28 19:44:22 +00001391plus_num(A) ::= PLUS number(X). {A = X;}
drhcf82f0d2016-02-17 04:33:10 +00001392plus_num(A) ::= number(A).
drhf57b14a2001-09-14 18:54:08 +00001393minus_num(A) ::= MINUS number(X). {A = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +00001394//////////////////////////// The CREATE TRIGGER command /////////////////////
drhf0f258b2003-04-21 18:48:45 +00001395
drhb7f91642004-10-31 02:22:47 +00001396%ifndef SQLITE_OMIT_TRIGGER
1397
drhd9da78a2009-03-24 15:08:09 +00001398cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
drh4b59ab52002-08-24 18:24:51 +00001399 Token all;
1400 all.z = A.z;
drhb27b7f52008-12-10 18:03:45 +00001401 all.n = (int)(Z.z - A.z) + Z.n;
danielk19774adee202004-05-08 08:23:19 +00001402 sqlite3FinishTrigger(pParse, S, &all);
drhf0f258b2003-04-21 18:48:45 +00001403}
1404
drhfdd48a72006-09-11 23:45:48 +00001405trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1406 trigger_time(C) trigger_event(D)
drh60218d22007-04-06 11:26:00 +00001407 ON fullname(E) foreach_clause when_clause(G). {
1408 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
drhcf82f0d2016-02-17 04:33:10 +00001409 A = (Z.n==0?B:Z); /*A-overwrites-T*/
danielk1977c3f9bad2002-05-15 08:30:12 +00001410}
1411
drhc4dd3fd2008-01-22 01:48:05 +00001412%type trigger_time {int}
drh6559e2c2017-06-28 14:26:37 +00001413trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
danielk1977c3f9bad2002-05-15 08:30:12 +00001414trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1415trigger_time(A) ::= . { A = TK_BEFORE; }
1416
drhad3cab52002-05-24 02:04:32 +00001417%type trigger_event {struct TrigEvent}
drh633e6d52008-07-28 19:34:53 +00001418%destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
drhcf82f0d2016-02-17 04:33:10 +00001419trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1420trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1421trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +00001422
drh60218d22007-04-06 11:26:00 +00001423foreach_clause ::= .
1424foreach_clause ::= FOR EACH ROW.
danielk1977c3f9bad2002-05-15 08:30:12 +00001425
drh0bb132b2004-07-20 14:06:51 +00001426%type when_clause {Expr*}
drh633e6d52008-07-28 19:34:53 +00001427%destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001428when_clause(A) ::= . { A = 0; }
drh1be266b2017-12-24 00:18:47 +00001429when_clause(A) ::= WHEN expr(X). { A = X; }
danielk1977c3f9bad2002-05-15 08:30:12 +00001430
drh0bb132b2004-07-20 14:06:51 +00001431%type trigger_cmd_list {TriggerStep*}
drh633e6d52008-07-28 19:34:53 +00001432%destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +00001433trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1434 assert( A!=0 );
1435 A->pLast->pNext = X;
1436 A->pLast = X;
drha69d9162003-04-17 22:57:53 +00001437}
drh4dd0d3f2016-02-17 01:18:33 +00001438trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1439 assert( A!=0 );
1440 A->pLast = A;
drh81238962008-08-11 14:26:35 +00001441}
danielk1977c3f9bad2002-05-15 08:30:12 +00001442
drhb1819a02009-07-03 15:37:27 +00001443// Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1444// within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1445// the same database as the table that the trigger fires on.
1446//
1447%type trnm {Token}
drh4dd0d3f2016-02-17 01:18:33 +00001448trnm(A) ::= nm(A).
drhb1819a02009-07-03 15:37:27 +00001449trnm(A) ::= nm DOT nm(X). {
1450 A = X;
1451 sqlite3ErrorMsg(pParse,
1452 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1453 "statements within triggers");
1454}
1455
1456// Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1457// statements within triggers. We make a specific error message for this
1458// since it is an exception to the default grammar rules.
1459//
1460tridxby ::= .
1461tridxby ::= INDEXED BY nm. {
1462 sqlite3ErrorMsg(pParse,
1463 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1464 "within triggers");
1465}
1466tridxby ::= NOT INDEXED. {
1467 sqlite3ErrorMsg(pParse,
1468 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1469 "within triggers");
1470}
1471
1472
1473
drh0bb132b2004-07-20 14:06:51 +00001474%type trigger_cmd {TriggerStep*}
drh633e6d52008-07-28 19:34:53 +00001475%destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001476// UPDATE
drhb1819a02009-07-03 15:37:27 +00001477trigger_cmd(A) ::=
drhf259df52017-12-27 20:38:35 +00001478 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z) scanpt(E).
dan5be60c52018-08-15 20:28:39 +00001479 {A = sqlite3TriggerUpdateStep(pParse, &X, Y, Z, R, B.z, E);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001480
1481// INSERT
drhf259df52017-12-27 20:38:35 +00001482trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
drh2c2e8442018-04-07 15:04:05 +00001483 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
dan5be60c52018-08-15 20:28:39 +00001484 A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
drh2c2e8442018-04-07 15:04:05 +00001485}
danielk1977c3f9bad2002-05-15 08:30:12 +00001486// DELETE
drhf259df52017-12-27 20:38:35 +00001487trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
dan5be60c52018-08-15 20:28:39 +00001488 {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001489
1490// SELECT
drhf259df52017-12-27 20:38:35 +00001491trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1492 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
danielk1977c3f9bad2002-05-15 08:30:12 +00001493
danielk19776f349032002-06-11 02:25:40 +00001494// The special RAISE expression that may occur in trigger programs
drh1be266b2017-12-24 00:18:47 +00001495expr(A) ::= RAISE LP IGNORE RP. {
1496 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1497 if( A ){
drh11949042019-08-05 18:01:42 +00001498 A->affExpr = OE_Ignore;
drh8aa34ae2006-03-13 12:54:09 +00001499 }
drh4b59ab52002-08-24 18:24:51 +00001500}
drh1be266b2017-12-24 00:18:47 +00001501expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1502 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1503 if( A ) {
drh11949042019-08-05 18:01:42 +00001504 A->affExpr = (char)T;
drh8aa34ae2006-03-13 12:54:09 +00001505 }
drh4b59ab52002-08-24 18:24:51 +00001506}
drh154d4b22006-09-21 11:02:16 +00001507%endif !SQLITE_OMIT_TRIGGER
drhb7f91642004-10-31 02:22:47 +00001508
drh74ad7fe2004-10-07 03:06:28 +00001509%type raisetype {int}
1510raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1511raisetype(A) ::= ABORT. {A = OE_Abort;}
1512raisetype(A) ::= FAIL. {A = OE_Fail;}
1513
danielk19776f349032002-06-11 02:25:40 +00001514
danielk1977c3f9bad2002-05-15 08:30:12 +00001515//////////////////////// DROP TRIGGER statement //////////////////////////////
drhb7f91642004-10-31 02:22:47 +00001516%ifndef SQLITE_OMIT_TRIGGER
drhfdd48a72006-09-11 23:45:48 +00001517cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1518 sqlite3DropTrigger(pParse,X,NOERR);
danielk1977c3f9bad2002-05-15 08:30:12 +00001519}
drh154d4b22006-09-21 11:02:16 +00001520%endif !SQLITE_OMIT_TRIGGER
drh113088e2003-03-20 01:16:58 +00001521
1522//////////////////////// ATTACH DATABASE file AS name /////////////////////////
drhfdbcdee2007-03-27 14:44:50 +00001523%ifndef SQLITE_OMIT_ATTACH
danielk1977f744bb52005-12-06 17:19:11 +00001524cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
drh1be266b2017-12-24 00:18:47 +00001525 sqlite3Attach(pParse, F, D, K);
drh1c2d8412003-03-31 00:30:47 +00001526}
drhfdbcdee2007-03-27 14:44:50 +00001527cmd ::= DETACH database_kw_opt expr(D). {
drh1be266b2017-12-24 00:18:47 +00001528 sqlite3Detach(pParse, D);
drhfdbcdee2007-03-27 14:44:50 +00001529}
1530
drhc4dd3fd2008-01-22 01:48:05 +00001531%type key_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +00001532%destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
danielk1977f744bb52005-12-06 17:19:11 +00001533key_opt(A) ::= . { A = 0; }
drh1be266b2017-12-24 00:18:47 +00001534key_opt(A) ::= KEY expr(X). { A = X; }
drh113088e2003-03-20 01:16:58 +00001535
1536database_kw_opt ::= DATABASE.
1537database_kw_opt ::= .
drhfdbcdee2007-03-27 14:44:50 +00001538%endif SQLITE_OMIT_ATTACH
drh4343fea2004-11-05 23:46:15 +00001539
1540////////////////////////// REINDEX collation //////////////////////////////////
1541%ifndef SQLITE_OMIT_REINDEX
1542cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1543cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
drh154d4b22006-09-21 11:02:16 +00001544%endif SQLITE_OMIT_REINDEX
danielk19779fd2a9a2004-11-12 13:42:30 +00001545
drh9f18e8a2005-07-08 12:13:04 +00001546/////////////////////////////////// ANALYZE ///////////////////////////////////
1547%ifndef SQLITE_OMIT_ANALYZE
1548cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1549cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1550%endif
1551
danielk19779fd2a9a2004-11-12 13:42:30 +00001552//////////////////////// ALTER TABLE table ... ////////////////////////////////
1553%ifndef SQLITE_OMIT_ALTERTABLE
1554cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1555 sqlite3AlterRenameTable(pParse,X,&Z);
1556}
drh986dde72016-02-29 13:37:21 +00001557cmd ::= ALTER TABLE add_column_fullname
1558 ADD kwcolumn_opt columnname(Y) carglist. {
1559 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
danielk197719a8e7e2005-03-17 05:03:38 +00001560 sqlite3AlterFinishAddColumn(pParse, &Y);
1561}
1562add_column_fullname ::= fullname(X). {
drh4a642b62016-02-05 01:55:27 +00001563 disableLookaside(pParse);
danielk197719a8e7e2005-03-17 05:03:38 +00001564 sqlite3AlterBeginAddColumn(pParse, X);
1565}
dancf8f2892018-08-09 20:47:01 +00001566cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
1567 sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
1568}
1569
danielk197719a8e7e2005-03-17 05:03:38 +00001570kwcolumn_opt ::= .
1571kwcolumn_opt ::= COLUMNKW.
dancf8f2892018-08-09 20:47:01 +00001572
drh154d4b22006-09-21 11:02:16 +00001573%endif SQLITE_OMIT_ALTERTABLE
drhe09daa92006-06-10 13:29:31 +00001574
1575//////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1576%ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001577cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1578cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
drhb421b892012-01-28 19:41:53 +00001579create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1580 nm(X) dbnm(Y) USING nm(Z). {
1581 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
drhb9bb7c12006-06-11 23:41:55 +00001582}
drhe09daa92006-06-10 13:29:31 +00001583vtabarglist ::= vtabarg.
1584vtabarglist ::= vtabarglist COMMA vtabarg.
drhb9bb7c12006-06-11 23:41:55 +00001585vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1586vtabarg ::= vtabarg vtabargtoken.
1587vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1588vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1589lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1590anylist ::= .
drhaaac8b42009-05-11 18:22:30 +00001591anylist ::= anylist LP anylist RP.
1592anylist ::= anylist ANY.
drh154d4b22006-09-21 11:02:16 +00001593%endif SQLITE_OMIT_VIRTUALTABLE
drh8b471862014-01-11 13:22:17 +00001594
1595
1596//////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
dan7d562db2014-01-11 19:19:36 +00001597%type wqlist {With*}
dan4e9119d2014-01-13 15:12:23 +00001598%destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
dan7d562db2014-01-11 19:19:36 +00001599
drha5746e02018-04-09 20:36:09 +00001600with ::= .
drh8b471862014-01-11 13:22:17 +00001601%ifndef SQLITE_OMIT_CTE
drha5746e02018-04-09 20:36:09 +00001602with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1603with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
dan7d562db2014-01-11 19:19:36 +00001604
drh108aa002015-08-24 20:21:20 +00001605wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
drhcf82f0d2016-02-17 04:33:10 +00001606 A = sqlite3WithAdd(pParse, 0, &X, Y, Z); /*A-overwrites-X*/
dan7d562db2014-01-11 19:19:36 +00001607}
drh4dd0d3f2016-02-17 01:18:33 +00001608wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1609 A = sqlite3WithAdd(pParse, A, &X, Y, Z);
drh8b471862014-01-11 13:22:17 +00001610}
1611%endif SQLITE_OMIT_CTE
dan34a7d792018-06-29 20:43:33 +00001612
1613//////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
dan6e2210e2018-06-30 18:54:56 +00001614// These must be at the end of this file. Specifically, the rules that
1615// introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1616// the integer values assigned to these tokens to be larger than all other
1617// tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
dan34a7d792018-06-29 20:43:33 +00001618//
1619%ifndef SQLITE_OMIT_WINDOWFUNC
1620%type windowdefn_list {Window*}
drha57aac22018-07-09 16:24:00 +00001621%destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
dan34a7d792018-06-29 20:43:33 +00001622windowdefn_list(A) ::= windowdefn(Z). { A = Z; }
1623windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
drha57aac22018-07-09 16:24:00 +00001624 assert( Z!=0 );
dane7c9ca42019-02-16 17:27:51 +00001625 sqlite3WindowChain(pParse, Z, Y);
drha57aac22018-07-09 16:24:00 +00001626 Z->pNextWin = Y;
dan34a7d792018-06-29 20:43:33 +00001627 A = Z;
1628}
1629
1630%type windowdefn {Window*}
1631%destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
dane7c9ca42019-02-16 17:27:51 +00001632windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
drha57aac22018-07-09 16:24:00 +00001633 if( ALWAYS(Y) ){
dan34a7d792018-06-29 20:43:33 +00001634 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1635 }
1636 A = Y;
1637}
1638
dan34a7d792018-06-29 20:43:33 +00001639%type window {Window*}
1640%destructor window {sqlite3WindowDelete(pParse->db, $$);}
1641
1642%type frame_opt {Window*}
1643%destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1644
dan34a7d792018-06-29 20:43:33 +00001645%type part_opt {ExprList*}
1646%destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1647
dan64882712019-07-11 18:43:33 +00001648%type filter_clause {Expr*}
1649%destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
1650
1651%type over_clause {Window*}
1652%destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
dan34a7d792018-06-29 20:43:33 +00001653
dan4f9adee2019-07-13 16:22:50 +00001654%type filter_over {Window*}
1655%destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
1656
dan34a7d792018-06-29 20:43:33 +00001657%type range_or_rows {int}
1658
1659%type frame_bound {struct FrameBound}
1660%destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
dan287fa172018-07-06 13:48:09 +00001661%type frame_bound_s {struct FrameBound}
1662%destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1663%type frame_bound_e {struct FrameBound}
1664%destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
dan34a7d792018-06-29 20:43:33 +00001665
dane7c9ca42019-02-16 17:27:51 +00001666window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1667 A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
1668}
1669window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1670 A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
1671}
1672window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
1673 A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
1674}
1675window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
1676 A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
1677}
1678window(A) ::= frame_opt(Z). {
dan34a7d792018-06-29 20:43:33 +00001679 A = Z;
dane7c9ca42019-02-16 17:27:51 +00001680}
1681window(A) ::= nm(W) frame_opt(Z). {
1682 A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
dan34a7d792018-06-29 20:43:33 +00001683}
1684
dan34a7d792018-06-29 20:43:33 +00001685frame_opt(A) ::= . {
dand35300f2019-03-14 20:53:21 +00001686 A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
dan34a7d792018-06-29 20:43:33 +00001687}
dand35300f2019-03-14 20:53:21 +00001688frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). {
1689 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
dan34a7d792018-06-29 20:43:33 +00001690}
drh0f134f02019-04-02 18:12:20 +00001691frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
1692 frame_bound_e(Z) frame_exclude_opt(W). {
dand35300f2019-03-14 20:53:21 +00001693 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
dan34a7d792018-06-29 20:43:33 +00001694}
1695
drh0f134f02019-04-02 18:12:20 +00001696range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/}
dan34a7d792018-06-29 20:43:33 +00001697
drh0f134f02019-04-02 18:12:20 +00001698frame_bound_s(A) ::= frame_bound(X). {A = X;}
1699frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
1700frame_bound_e(A) ::= frame_bound(X). {A = X;}
1701frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
dan287fa172018-07-06 13:48:09 +00001702
drh0f134f02019-04-02 18:12:20 +00001703frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
1704 {A.eType = @Y; A.pExpr = X;}
1705frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;}
dan34a7d792018-06-29 20:43:33 +00001706
dand35300f2019-03-14 20:53:21 +00001707%type frame_exclude_opt {u8}
drh0f134f02019-04-02 18:12:20 +00001708frame_exclude_opt(A) ::= . {A = 0;}
1709frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
dand35300f2019-03-14 20:53:21 +00001710
1711%type frame_exclude {u8}
drh0f134f02019-04-02 18:12:20 +00001712frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/}
1713frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
1714frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/}
dand35300f2019-03-14 20:53:21 +00001715
1716
drh550a3302018-07-27 22:14:50 +00001717%type window_clause {Window*}
1718%destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
1719window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
dan6e2210e2018-06-30 18:54:56 +00001720
dan4f9adee2019-07-13 16:22:50 +00001721filter_over(A) ::= filter_clause(F) over_clause(O). {
1722 O->pFilter = F;
1723 A = O;
dan64882712019-07-11 18:43:33 +00001724}
dan4f9adee2019-07-13 16:22:50 +00001725filter_over(A) ::= over_clause(O). {
1726 A = O;
dan64882712019-07-11 18:43:33 +00001727}
dan4f9adee2019-07-13 16:22:50 +00001728filter_over(A) ::= filter_clause(F). {
1729 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1730 if( A ){
1731 A->eFrmType = TK_FILTER;
1732 A->pFilter = F;
dan00885742019-07-13 18:27:54 +00001733 }else{
1734 sqlite3ExprDelete(pParse->db, F);
dan4f9adee2019-07-13 16:22:50 +00001735 }
dan64882712019-07-11 18:43:33 +00001736}
1737
1738over_clause(A) ::= OVER LP window(Z) RP. {
dan6e2210e2018-06-30 18:54:56 +00001739 A = Z;
drha57aac22018-07-09 16:24:00 +00001740 assert( A!=0 );
drha57aac22018-07-09 16:24:00 +00001741}
dan64882712019-07-11 18:43:33 +00001742over_clause(A) ::= OVER nm(Z). {
drha57aac22018-07-09 16:24:00 +00001743 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1744 if( A ){
1745 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
drha57aac22018-07-09 16:24:00 +00001746 }
dan6e2210e2018-06-30 18:54:56 +00001747}
1748
dan64882712019-07-11 18:43:33 +00001749filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
drhc7bf5712018-07-09 22:49:01 +00001750%endif /* SQLITE_OMIT_WINDOWFUNC */
drhf1722ba2019-04-05 20:56:46 +00001751
1752/*
1753** The code generator needs some extra TK_ token values for tokens that
1754** are synthesized and do not actually appear in the grammar:
1755*/
1756%token
drhf1722ba2019-04-05 20:56:46 +00001757 COLUMN /* Reference to a table column */
1758 AGG_FUNCTION /* An aggregate function */
1759 AGG_COLUMN /* An aggregated column */
danee6c5e52019-08-23 20:33:01 +00001760 TRUEFALSE /* True or false keyword */
1761 ISNOT /* Combination of IS and NOT */
1762 FUNCTION /* A function invocation */
drhf1722ba2019-04-05 20:56:46 +00001763 UMINUS /* Unary minus */
1764 UPLUS /* Unary plus */
1765 TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
1766 REGISTER /* Reference to a VDBE register */
1767 VECTOR /* Vector */
1768 SELECT_COLUMN /* Choose a single column from a multi-column SELECT */
1769 IF_NULL_ROW /* the if-null-row operator */
1770 ASTERISK /* The "*" in count(*) and similar */
1771 SPAN /* The span operator */
1772.
1773/* There must be no more than 255 tokens defined above. If this grammar
1774** is extended with new rules and tokens, they must either be so few in
1775** number that TK_SPAN is no more than 255, or else the new tokens must
1776** appear after this line.
1777*/
1778%include {
1779#if TK_SPAN>255
1780# error too many tokens in the grammar
1781#endif
1782}
1783
1784/*
1785** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens. The
1786** parser depends on this. Those tokens are not used in any grammar rule.
1787** They are only used by the tokenizer. Declare them last so that they
1788** are guaranteed to be the last two tokens
1789*/
1790%token SPACE ILLEGAL.