blob: 97a0ffd934c589f1930fdc0050fe68570af946eb [file] [log] [blame]
drh60c71b02020-09-01 11:20:03 +00001%include {
drh348784e2000-05-29 20:41:49 +00002/*
drh60c71b02020-09-01 11:20:03 +00003** 2001-09-15
drh348784e2000-05-29 20:41:49 +00004**
drhb19a2bc2001-09-16 00:13:26 +00005** The author disclaims copyright to this source code. In place of
6** a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00007**
drhb19a2bc2001-09-16 00:13:26 +00008** May you do good and not evil.
9** May you find forgiveness for yourself and forgive others.
10** May you share freely, never taking more than you give.
drh348784e2000-05-29 20:41:49 +000011**
12*************************************************************************
drh60c71b02020-09-01 11:20:03 +000013** This file contains SQLite's SQL parser.
14**
15** The canonical source code to this file ("parse.y") is a Lemon grammar
16** file that specifies the input grammar and actions to take while parsing.
17** That input file is processed by Lemon to generate a C-language
18** implementation of a parser for the given grammer. You might be reading
19** this comment as part of the translated C-code. Edits should be made
20** to the original parse.y sources.
drh348784e2000-05-29 20:41:49 +000021*/
drh60c71b02020-09-01 11:20:03 +000022}
drh487e2622005-06-25 18:42:14 +000023
24// All token codes are small integers with #defines that begin with "TK_"
drh348784e2000-05-29 20:41:49 +000025%token_prefix TK_
drh487e2622005-06-25 18:42:14 +000026
27// The type of the data attached to each token is Token. This is also the
28// default type for non-terminals.
29//
drh348784e2000-05-29 20:41:49 +000030%token_type {Token}
drhf57b14a2001-09-14 18:54:08 +000031%default_type {Token}
drh487e2622005-06-25 18:42:14 +000032
drhfb32c442018-04-21 13:51:42 +000033// An extra argument to the constructor for the parser, which is available
34// to all actions.
35%extra_context {Parse *pParse}
drh487e2622005-06-25 18:42:14 +000036
37// This code runs whenever there is a syntax error
38//
drh348784e2000-05-29 20:41:49 +000039%syntax_error {
drh128255f2008-12-08 16:01:12 +000040 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
drh6116ee42018-01-10 00:40:06 +000041 if( TOKEN.z[0] ){
42 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
43 }else{
44 sqlite3ErrorMsg(pParse, "incomplete input");
45 }
drh348784e2000-05-29 20:41:49 +000046}
drh8fc33452006-02-27 21:58:07 +000047%stack_overflow {
48 sqlite3ErrorMsg(pParse, "parser stack overflow");
49}
drh487e2622005-06-25 18:42:14 +000050
51// The name of the generated procedure that implements the parser
52// is as follows:
danielk19774adee202004-05-08 08:23:19 +000053%name sqlite3Parser
drh487e2622005-06-25 18:42:14 +000054
55// The following text is included near the beginning of the C source
56// code file that implements the parser.
57//
drh348784e2000-05-29 20:41:49 +000058%include {
59#include "sqliteInt.h"
drh9bbca4c2001-11-06 04:00:18 +000060
61/*
drhd3ec02d2009-06-12 02:27:14 +000062** Disable all error recovery processing in the parser push-down
63** automaton.
64*/
65#define YYNOERRORRECOVERY 1
66
67/*
drh8a415d32009-06-12 13:53:51 +000068** Make yytestcase() the same as testcase()
69*/
70#define yytestcase(X) testcase(X)
71
72/*
drh82415f22015-11-09 19:33:42 +000073** Indicate that sqlite3ParserFree() will never be called with a null
74** pointer.
75*/
drh644f4c12015-11-12 15:04:05 +000076#define YYPARSEFREENEVERNULL 1
drh82415f22015-11-09 19:33:42 +000077
78/*
drhd26cc542017-01-28 20:46:37 +000079** In the amalgamation, the parse.c file generated by lemon and the
80** tokenize.c file are concatenated. In that case, sqlite3RunParser()
81** has access to the the size of the yyParser object and so the parser
82** engine can be allocated from stack. In that case, only the
83** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
84** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
85** omitted.
86*/
87#ifdef SQLITE_AMALGAMATION
88# define sqlite3Parser_ENGINEALWAYSONSTACK 1
89#endif
90
91/*
drh82415f22015-11-09 19:33:42 +000092** Alternative datatype for the argument to the malloc() routine passed
93** into sqlite3ParserAlloc(). The default is size_t.
94*/
95#define YYMALLOCARGTYPE u64
96
97/*
drhad3cab52002-05-24 02:04:32 +000098** An instance of the following structure describes the event of a
99** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
100** TK_DELETE, or TK_INSTEAD. If the event is of the form
101**
102** UPDATE ON (a,b,c)
103**
104** Then the "b" IdList records the list "a,b,c".
danielk1977c3f9bad2002-05-15 08:30:12 +0000105*/
drhad3cab52002-05-24 02:04:32 +0000106struct TrigEvent { int a; IdList * b; };
drhcaec2f12003-01-07 02:47:47 +0000107
dan86fb6e12018-05-16 20:58:07 +0000108struct FrameBound { int eType; Expr *pExpr; };
109
drh25d65432004-07-22 15:02:25 +0000110/*
drh4a642b62016-02-05 01:55:27 +0000111** Disable lookaside memory allocation for objects that might be
112** shared across database connections.
113*/
114static void disableLookaside(Parse *pParse){
drh31f69622019-10-05 14:39:36 +0000115 sqlite3 *db = pParse->db;
drh4a642b62016-02-05 01:55:27 +0000116 pParse->disableLookaside++;
drh31f69622019-10-05 14:39:36 +0000117 DisableLookaside;
drh4a642b62016-02-05 01:55:27 +0000118}
119
drh27ee29f2020-07-03 21:18:07 +0000120#if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \
121 && defined(SQLITE_UDL_CAPABLE_PARSER)
drhc601f102020-07-03 17:24:35 +0000122/*
123** Issue an error message if an ORDER BY or LIMIT clause occurs on an
124** UPDATE or DELETE statement.
125*/
126static void updateDeleteLimitError(
127 Parse *pParse,
128 ExprList *pOrderBy,
129 Expr *pLimit
130){
131 if( pOrderBy ){
132 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
133 }else{
134 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
135 }
136 sqlite3ExprListDelete(pParse->db, pOrderBy);
137 sqlite3ExprDelete(pParse->db, pLimit);
138}
139#endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
140
drhcaec2f12003-01-07 02:47:47 +0000141} // end %include
drh348784e2000-05-29 20:41:49 +0000142
drh826fb5a2004-02-14 23:59:57 +0000143// Input is a single SQL command
drhc4a3c772001-04-04 11:48:57 +0000144input ::= cmdlist.
drh094b2bb2002-03-13 18:54:07 +0000145cmdlist ::= cmdlist ecmd.
drh826fb5a2004-02-14 23:59:57 +0000146cmdlist ::= ecmd.
drhb7f91642004-10-31 02:22:47 +0000147ecmd ::= SEMI.
drh2424aa72018-04-11 17:10:54 +0000148ecmd ::= cmdx SEMI.
drhb7f91642004-10-31 02:22:47 +0000149%ifndef SQLITE_OMIT_EXPLAIN
drhe94006e2019-12-10 20:41:48 +0000150ecmd ::= explain cmdx SEMI. {NEVER-REDUCE}
drh8549d552016-01-07 17:09:43 +0000151explain ::= EXPLAIN. { pParse->explain = 1; }
152explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; }
drh154d4b22006-09-21 11:02:16 +0000153%endif SQLITE_OMIT_EXPLAIN
drh200a81d2008-08-08 14:19:41 +0000154cmdx ::= cmd. { sqlite3FinishCoding(pParse); }
drh348784e2000-05-29 20:41:49 +0000155
drh382c0242001-10-06 16:33:02 +0000156///////////////////// Begin and end transactions. ////////////////////////////
drhc4a3c772001-04-04 11:48:57 +0000157//
drhfa86c412002-02-02 15:01:15 +0000158
drh684917c2004-10-05 02:41:42 +0000159cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);}
drhc4a3c772001-04-04 11:48:57 +0000160trans_opt ::= .
161trans_opt ::= TRANSACTION.
drh5ad1a6c2002-07-01 12:27:09 +0000162trans_opt ::= TRANSACTION nm.
drh684917c2004-10-05 02:41:42 +0000163%type transtype {int}
164transtype(A) ::= . {A = TK_DEFERRED;}
drhcf82f0d2016-02-17 04:33:10 +0000165transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/}
166transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
167transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
drh07a3b112017-07-06 01:28:02 +0000168cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
169cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);}
drhc4a3c772001-04-04 11:48:57 +0000170
danielk1977fd7f0452008-12-17 17:30:26 +0000171savepoint_opt ::= SAVEPOINT.
172savepoint_opt ::= .
173cmd ::= SAVEPOINT nm(X). {
174 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
175}
176cmd ::= RELEASE savepoint_opt nm(X). {
177 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
178}
179cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
180 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
181}
182
drh382c0242001-10-06 16:33:02 +0000183///////////////////// The CREATE TABLE statement ////////////////////////////
drh348784e2000-05-29 20:41:49 +0000184//
185cmd ::= create_table create_table_args.
drhd9da78a2009-03-24 15:08:09 +0000186create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
danielk1977f1a381e2006-06-16 08:01:02 +0000187 sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
drh969fa7c2002-02-18 18:30:32 +0000188}
drhdabd04c2016-02-17 01:46:19 +0000189createkw(A) ::= CREATE(A). {disableLookaside(pParse);}
190
drhfaa59552005-12-29 23:33:54 +0000191%type ifnotexists {int}
192ifnotexists(A) ::= . {A = 0;}
193ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
drhf57b3392001-10-08 13:22:32 +0000194%type temp {int}
danielk197753c0f742005-03-29 03:10:59 +0000195%ifndef SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000196temp(A) ::= TEMP. {A = 1;}
drh154d4b22006-09-21 11:02:16 +0000197%endif SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000198temp(A) ::= . {A = 0;}
drh5969da42013-10-21 02:14:45 +0000199create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
200 sqlite3EndTable(pParse,&X,&E,F,0);
drh969fa7c2002-02-18 18:30:32 +0000201}
202create_table_args ::= AS select(S). {
drh5969da42013-10-21 02:14:45 +0000203 sqlite3EndTable(pParse,0,0,0,S);
drh633e6d52008-07-28 19:34:53 +0000204 sqlite3SelectDelete(pParse->db, S);
drh969fa7c2002-02-18 18:30:32 +0000205}
drh3334d082015-11-10 13:45:21 +0000206%type table_options {int}
drh5969da42013-10-21 02:14:45 +0000207table_options(A) ::= . {A = 0;}
208table_options(A) ::= WITHOUT nm(X). {
209 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
drhfccda8a2015-05-27 13:06:55 +0000210 A = TF_WithoutRowid | TF_NoVisibleRowid;
drh5969da42013-10-21 02:14:45 +0000211 }else{
212 A = 0;
213 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
214 }
215}
drh986dde72016-02-29 13:37:21 +0000216columnlist ::= columnlist COMMA columnname carglist.
217columnlist ::= columnname carglist.
drh2881ab62016-02-27 23:25:36 +0000218columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,&A,&Y);}
drhc4a3c772001-04-04 11:48:57 +0000219
drh6a8700b2017-08-02 11:04:00 +0000220// Declare some tokens early in order to influence their values, to
221// improve performance and reduce the executable size. The goal here is
222// to get the "jump" operations in ISNULL through ESCAPE to have numeric
223// values that are early enough so that all jump operations are clustered
drh7bbdc3c2019-04-05 21:17:11 +0000224// at the beginning.
drh6a8700b2017-08-02 11:04:00 +0000225//
226%token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
227%token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
228%token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
229%token GT LE LT GE ESCAPE.
230
drh31d6fd52017-04-14 19:03:10 +0000231// The following directive causes tokens ABORT, AFTER, ASC, etc. to
232// fallback to ID if they will not parse as their original value.
233// This obviates the need for the "id" nonterminal.
234//
235%fallback ID
236 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
drh0a6259f2018-04-16 13:26:53 +0000237 CONFLICT DATABASE DEFERRED DESC DETACH DO
drh6cd7d482018-04-12 15:43:05 +0000238 EACH END EXCLUSIVE EXPLAIN FAIL FOR
drh31d6fd52017-04-14 19:03:10 +0000239 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
dan86fb6e12018-05-16 20:58:07 +0000240 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
drh31d6fd52017-04-14 19:03:10 +0000241 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
dan6e118922019-08-12 16:36:38 +0000242 NULLS FIRST LAST
drh31d6fd52017-04-14 19:03:10 +0000243%ifdef SQLITE_OMIT_COMPOUND_SELECT
244 EXCEPT INTERSECT UNION
245%endif SQLITE_OMIT_COMPOUND_SELECT
drh3773c252018-06-28 03:38:49 +0000246%ifndef SQLITE_OMIT_WINDOWFUNC
dan6e2210e2018-06-30 18:54:56 +0000247 CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED
danced89122019-03-19 06:40:29 +0000248 EXCLUDE GROUPS OTHERS TIES
drh3773c252018-06-28 03:38:49 +0000249%endif SQLITE_OMIT_WINDOWFUNC
drh9ee94142019-10-30 13:00:23 +0000250%ifndef SQLITE_OMIT_GENERATED_COLUMNS
drh089c4bc2019-11-02 13:45:03 +0000251 GENERATED ALWAYS
drh9ee94142019-10-30 13:00:23 +0000252%endif
drh31d6fd52017-04-14 19:03:10 +0000253 REINDEX RENAME CTIME_KW IF
254 .
255%wildcard ANY.
256
drhf7b54962013-05-28 12:11:54 +0000257// Define operator precedence early so that this is the first occurrence
drh2d3917d2004-02-22 16:27:00 +0000258// of the operator tokens in the grammer. Keeping the operators together
259// causes them to be assigned integer values that are close together,
260// which keeps parser tables smaller.
261//
drhf2bc0132004-10-04 13:19:23 +0000262// The token values assigned to these symbols is determined by the order
263// in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
264// NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
265// the sqlite3ExprIfFalse() routine for additional information on this
266// constraint.
267//
drh2d3917d2004-02-22 16:27:00 +0000268%left OR.
269%left AND.
270%right NOT.
drh03bea702006-06-13 15:37:26 +0000271%left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
drh9a432672004-10-04 13:38:09 +0000272%left GT LE LT GE.
danielk19777c6303c2004-11-17 16:41:29 +0000273%right ESCAPE.
drh2d3917d2004-02-22 16:27:00 +0000274%left BITAND BITOR LSHIFT RSHIFT.
275%left PLUS MINUS.
276%left STAR SLASH REM.
drha34001c2007-02-02 12:44:37 +0000277%left CONCAT.
278%left COLLATE.
drh7ba5bc52009-09-22 20:08:34 +0000279%right BITNOT.
drh26cf56f2018-04-06 19:36:49 +0000280%nonassoc ON.
drh2d3917d2004-02-22 16:27:00 +0000281
drh7cc84c22016-04-11 13:36:42 +0000282// An IDENTIFIER can be a generic identifier, or one of several
283// keywords. Any non-standard keyword can also be an identifier.
284//
285%token_class id ID|INDEXED.
286
drh7cc84c22016-04-11 13:36:42 +0000287
drhc4a3c772001-04-04 11:48:57 +0000288// And "ids" is an identifer-or-string.
289//
dan59ff4252018-06-29 17:44:52 +0000290%token_class ids ID|STRING.
drhc4a3c772001-04-04 11:48:57 +0000291
drh5ad1a6c2002-07-01 12:27:09 +0000292// The name of a column or table can be any of the following:
293//
294%type nm {Token}
drh4dd0d3f2016-02-17 01:18:33 +0000295nm(A) ::= id(A).
296nm(A) ::= STRING(A).
297nm(A) ::= JOIN_KW(A).
drh5ad1a6c2002-07-01 12:27:09 +0000298
drh986dde72016-02-29 13:37:21 +0000299// A typetoken is really zero or more tokens that form a type name such
drh487e2622005-06-25 18:42:14 +0000300// as can be found after the column name in a CREATE TABLE statement.
301// Multiple tokens are concatenated to form the value of the typetoken.
302//
303%type typetoken {Token}
drh986dde72016-02-29 13:37:21 +0000304typetoken(A) ::= . {A.n = 0; A.z = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000305typetoken(A) ::= typename(A).
306typetoken(A) ::= typename(A) LP signed RP(Y). {
307 A.n = (int)(&Y.z[Y.n] - A.z);
drh487e2622005-06-25 18:42:14 +0000308}
drh4dd0d3f2016-02-17 01:18:33 +0000309typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
310 A.n = (int)(&Y.z[Y.n] - A.z);
drh487e2622005-06-25 18:42:14 +0000311}
drh382c0242001-10-06 16:33:02 +0000312%type typename {Token}
dan59ff4252018-06-29 17:44:52 +0000313typename(A) ::= ids(A).
314typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
drh60218d22007-04-06 11:26:00 +0000315signed ::= plus_num.
316signed ::= minus_num.
drh487e2622005-06-25 18:42:14 +0000317
drh1be266b2017-12-24 00:18:47 +0000318// The scanpt non-terminal takes a value which is a pointer to the
319// input text just past the last token that has been shifted into
320// the parser. By surrounding some phrase in the grammar with two
321// scanpt non-terminals, we can capture the input text for that phrase.
322// For example:
323//
324// something ::= .... scanpt(A) phrase scanpt(Z).
325//
326// The text that is parsed as "phrase" is a string starting at A
327// and containing (int)(Z-A) characters. There might be some extra
328// whitespace on either end of the text, but that can be removed in
329// post-processing, if needed.
330//
331%type scanpt {const char*}
332scanpt(A) ::= . {
drhf259df52017-12-27 20:38:35 +0000333 assert( yyLookahead!=YYNOCODE );
334 A = yyLookaheadToken.z;
drh1be266b2017-12-24 00:18:47 +0000335}
dan4db4b5b2019-05-18 19:49:08 +0000336scantok(A) ::= . {
337 assert( yyLookahead!=YYNOCODE );
338 A = yyLookaheadToken;
339}
drh1be266b2017-12-24 00:18:47 +0000340
drh487e2622005-06-25 18:42:14 +0000341// "carglist" is a list of additional constraints that come after the
342// column name and column type in a CREATE TABLE statement.
343//
drh4dc330d2012-05-07 19:21:36 +0000344carglist ::= carglist ccons.
drh348784e2000-05-29 20:41:49 +0000345carglist ::= .
drh4dc330d2012-05-07 19:21:36 +0000346ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
dan4db4b5b2019-05-18 19:49:08 +0000347ccons ::= DEFAULT scantok(A) term(X).
348 {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);}
drh1be266b2017-12-24 00:18:47 +0000349ccons ::= DEFAULT LP(A) expr(X) RP(Z).
350 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
dan4db4b5b2019-05-18 19:49:08 +0000351ccons ::= DEFAULT PLUS(A) scantok(Z) term(X).
352 {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);}
353ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). {
drh1be266b2017-12-24 00:18:47 +0000354 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
dan4db4b5b2019-05-18 19:49:08 +0000355 sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]);
danielk19777977a172004-11-09 12:44:37 +0000356}
dan4db4b5b2019-05-18 19:49:08 +0000357ccons ::= DEFAULT scantok id(X). {
drh1be266b2017-12-24 00:18:47 +0000358 Expr *p = tokenExpr(pParse, TK_STRING, X);
drhd7fd8992018-02-28 04:30:55 +0000359 if( p ){
360 sqlite3ExprIdToTrueFalse(p);
361 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
362 }
danc9461ec2018-08-29 21:00:16 +0000363 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
danielk19777977a172004-11-09 12:44:37 +0000364}
drh348784e2000-05-29 20:41:49 +0000365
drh382c0242001-10-06 16:33:02 +0000366// In addition to the type name, we also care about the primary key and
367// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000368//
drh0d316a42002-08-11 20:10:47 +0000369ccons ::= NULL onconf.
drhb7916a72009-05-27 10:31:29 +0000370ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
drhfdd6e852005-12-16 01:06:16 +0000371ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
drhb7916a72009-05-27 10:31:29 +0000372 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
drh62340f82016-05-31 21:18:15 +0000373ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
374 SQLITE_IDXTYPE_UNIQUE);}
drh92e21ef2020-08-27 18:36:30 +0000375ccons ::= CHECK LP(A) expr(X) RP(B). {sqlite3AddCheckConstraint(pParse,X,A.z,B.z);}
drh108aa002015-08-24 20:21:20 +0000376ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
drhb7916a72009-05-27 10:31:29 +0000377 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
378ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
dan59ff4252018-06-29 17:44:52 +0000379ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);}
drh81f7b372019-10-16 12:18:59 +0000380ccons ::= GENERATED ALWAYS AS generated.
381ccons ::= AS generated.
drh7e508f12019-10-16 19:31:46 +0000382generated ::= LP expr(E) RP. {sqlite3AddGenerated(pParse,E,0);}
383generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);}
drh04738cb2002-06-02 18:19:00 +0000384
drh205f48e2004-11-05 00:43:11 +0000385// The optional AUTOINCREMENT keyword
386%type autoinc {int}
drh2958a4e2004-11-12 03:56:15 +0000387autoinc(X) ::= . {X = 0;}
388autoinc(X) ::= AUTOINCR. {X = 1;}
drh205f48e2004-11-05 00:43:11 +0000389
drhc2eef3b2002-08-31 18:53:06 +0000390// The next group of rules parses the arguments to a REFERENCES clause
391// that determine if the referential integrity checking is deferred or
392// or immediate and which determine what action to take if a ref-integ
393// check fails.
drh04738cb2002-06-02 18:19:00 +0000394//
drhc2eef3b2002-08-31 18:53:06 +0000395%type refargs {int}
drhfcf486c2009-10-21 13:48:24 +0000396refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */}
drh4dd0d3f2016-02-17 01:18:33 +0000397refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
drhc2eef3b2002-08-31 18:53:06 +0000398%type refarg {struct {int value; int mask;}}
399refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
drhc29c5aa12009-12-09 21:43:36 +0000400refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; }
drhc2eef3b2002-08-31 18:53:06 +0000401refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
402refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
drhc2eef3b2002-08-31 18:53:06 +0000403%type refact {int}
drhfcf486c2009-10-21 13:48:24 +0000404refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */}
405refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */}
406refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */}
407refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */}
408refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */}
drhc2eef3b2002-08-31 18:53:06 +0000409%type defer_subclause {int}
dan1da40a32009-09-19 17:00:31 +0000410defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;}
drhc2eef3b2002-08-31 18:53:06 +0000411defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
412%type init_deferred_pred_opt {int}
413init_deferred_pred_opt(A) ::= . {A = 0;}
414init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
415init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000416
drhaeb281c2012-05-08 11:17:33 +0000417conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000418conslist_opt(A) ::= COMMA(A) conslist.
drhab35eae2012-05-12 18:29:53 +0000419conslist ::= conslist tconscomma tcons.
420conslist ::= tcons.
421tconscomma ::= COMMA. {pParse->constraintName.n = 0;}
422tconscomma ::= .
423tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
drh108aa002015-08-24 20:21:20 +0000424tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
drhb7916a72009-05-27 10:31:29 +0000425 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
drh108aa002015-08-24 20:21:20 +0000426tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
drh62340f82016-05-31 21:18:15 +0000427 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
428 SQLITE_IDXTYPE_UNIQUE);}
drh92e21ef2020-08-27 18:36:30 +0000429tcons ::= CHECK LP(A) expr(E) RP(B) onconf.
430 {sqlite3AddCheckConstraint(pParse,E,A.z,B.z);}
drh108aa002015-08-24 20:21:20 +0000431tcons ::= FOREIGN KEY LP eidlist(FA) RP
432 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
danielk19774adee202004-05-08 08:23:19 +0000433 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
434 sqlite3DeferForeignKey(pParse, D);
drhc2eef3b2002-08-31 18:53:06 +0000435}
436%type defer_subclause_opt {int}
437defer_subclause_opt(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000438defer_subclause_opt(A) ::= defer_subclause(A).
drh9cfcf5d2002-01-29 18:41:24 +0000439
440// The following is a non-standard extension that allows us to declare the
441// default behavior when there is a constraint conflict.
442//
443%type onconf {int}
drh3334d082015-11-10 13:45:21 +0000444%type orconf {int}
drh1c928532002-01-31 15:54:21 +0000445%type resolvetype {int}
drh74ad7fe2004-10-07 03:06:28 +0000446onconf(A) ::= . {A = OE_Default;}
447onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
448orconf(A) ::= . {A = OE_Default;}
drh3334d082015-11-10 13:45:21 +0000449orconf(A) ::= OR resolvetype(X). {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000450resolvetype(A) ::= raisetype(A).
drh74ad7fe2004-10-07 03:06:28 +0000451resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
452resolvetype(A) ::= REPLACE. {A = OE_Replace;}
drh348784e2000-05-29 20:41:49 +0000453
drh382c0242001-10-06 16:33:02 +0000454////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000455//
drha0733842005-12-29 01:11:36 +0000456cmd ::= DROP TABLE ifexists(E) fullname(X). {
457 sqlite3DropTable(pParse, X, 0, E);
danielk1977a8858102004-05-28 12:11:21 +0000458}
drha0733842005-12-29 01:11:36 +0000459%type ifexists {int}
460ifexists(A) ::= IF EXISTS. {A = 1;}
461ifexists(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000462
drha76b5df2002-02-23 02:32:10 +0000463///////////////////// The CREATE VIEW statement /////////////////////////////
464//
drhb7f91642004-10-31 02:22:47 +0000465%ifndef SQLITE_OMIT_VIEW
drh108aa002015-08-24 20:21:20 +0000466cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
drh8981b902015-08-24 17:42:49 +0000467 AS select(S). {
468 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
drha76b5df2002-02-23 02:32:10 +0000469}
drha0733842005-12-29 01:11:36 +0000470cmd ::= DROP VIEW ifexists(E) fullname(X). {
471 sqlite3DropTable(pParse, X, 1, E);
drha76b5df2002-02-23 02:32:10 +0000472}
drh154d4b22006-09-21 11:02:16 +0000473%endif SQLITE_OMIT_VIEW
drha76b5df2002-02-23 02:32:10 +0000474
drh382c0242001-10-06 16:33:02 +0000475//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000476//
dan7d562db2014-01-11 19:19:36 +0000477cmd ::= select(X). {
drha7c74002020-07-18 18:44:59 +0000478 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
drh7d10d5a2008-08-20 16:35:10 +0000479 sqlite3Select(pParse, X, &dest);
drh633e6d52008-07-28 19:34:53 +0000480 sqlite3SelectDelete(pParse->db, X);
drh9bb61fe2000-06-05 16:01:39 +0000481}
drhefb72512000-05-31 20:00:52 +0000482
drh9bb61fe2000-06-05 16:01:39 +0000483%type select {Select*}
drh633e6d52008-07-28 19:34:53 +0000484%destructor select {sqlite3SelectDelete(pParse->db, $$);}
dan7d562db2014-01-11 19:19:36 +0000485%type selectnowith {Select*}
486%destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
drh82c3d632000-06-06 21:56:07 +0000487%type oneselect {Select*}
drh633e6d52008-07-28 19:34:53 +0000488%destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
drh9bb61fe2000-06-05 16:01:39 +0000489
drh772460f2015-04-16 14:13:12 +0000490%include {
491 /*
492 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
493 ** all elements in the list. And make sure list length does not exceed
494 ** SQLITE_LIMIT_COMPOUND_SELECT.
495 */
drhe318a7f2015-04-16 23:04:17 +0000496 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
drh55f66b32019-07-16 19:44:32 +0000497 assert( p!=0 );
drhd227a292014-02-09 18:02:09 +0000498 if( p->pPrior ){
drh772460f2015-04-16 14:13:12 +0000499 Select *pNext = 0, *pLoop;
500 int mxSelect, cnt = 0;
drhd227a292014-02-09 18:02:09 +0000501 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
502 pLoop->pNext = pNext;
503 pLoop->selFlags |= SF_Compound;
504 }
drh772460f2015-04-16 14:13:12 +0000505 if( (p->selFlags & SF_MultiValue)==0 &&
506 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
507 cnt>mxSelect
drha0c01762015-01-05 16:27:43 +0000508 ){
drhd227a292014-02-09 18:02:09 +0000509 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
510 }
511 }
drh772460f2015-04-16 14:13:12 +0000512 }
drhf824b412021-02-20 14:57:16 +0000513
514 /* Attach a With object describing the WITH clause to a Select
515 ** object describing the query for which the WITH clause is a prefix.
516 */
517 static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){
518 if( pSelect ){
519 pSelect->pWith = pWith;
520 parserDoubleLinkSelect(pParse, pSelect);
521 }else{
522 sqlite3WithDelete(pParse->db, pWith);
523 }
524 return pSelect;
525 }
drh772460f2015-04-16 14:13:12 +0000526}
527
drh6a8b94e2018-05-30 01:14:20 +0000528%ifndef SQLITE_OMIT_CTE
drhf824b412021-02-20 14:57:16 +0000529select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);}
530select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X).
531 {A = attachWithToSelect(pParse,X,W);}
drh6a8b94e2018-05-30 01:14:20 +0000532%endif /* SQLITE_OMIT_CTE */
drha5746e02018-04-09 20:36:09 +0000533select(A) ::= selectnowith(X). {
534 Select *p = X;
535 if( p ){
536 parserDoubleLinkSelect(pParse, p);
537 }
538 A = p; /*A-overwrites-X*/
dana9f5c132014-01-13 16:36:40 +0000539}
dan7d562db2014-01-11 19:19:36 +0000540
drh4dd0d3f2016-02-17 01:18:33 +0000541selectnowith(A) ::= oneselect(A).
drhb7f91642004-10-31 02:22:47 +0000542%ifndef SQLITE_OMIT_COMPOUND_SELECT
drh4dd0d3f2016-02-17 01:18:33 +0000543selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
drhc0bf4932014-02-19 01:31:02 +0000544 Select *pRhs = Z;
drh4dd0d3f2016-02-17 01:18:33 +0000545 Select *pLhs = A;
drhc0bf4932014-02-19 01:31:02 +0000546 if( pRhs && pRhs->pPrior ){
547 SrcList *pFrom;
548 Token x;
549 x.n = 0;
drh772460f2015-04-16 14:13:12 +0000550 parserDoubleLinkSelect(pParse, pRhs);
drhc0bf4932014-02-19 01:31:02 +0000551 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
drh8c0833f2017-11-14 23:48:23 +0000552 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
drhc0bf4932014-02-19 01:31:02 +0000553 }
554 if( pRhs ){
555 pRhs->op = (u8)Y;
drh00d5ab72015-05-20 00:15:27 +0000556 pRhs->pPrior = pLhs;
557 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
drh772460f2015-04-16 14:13:12 +0000558 pRhs->selFlags &= ~SF_MultiValue;
drhd58d3272013-08-05 22:05:02 +0000559 if( Y!=TK_ALL ) pParse->hasCompound = 1;
drh43b78822007-06-15 17:03:14 +0000560 }else{
drh00d5ab72015-05-20 00:15:27 +0000561 sqlite3SelectDelete(pParse->db, pLhs);
drhdaffd0e2001-04-11 14:28:42 +0000562 }
drhc0bf4932014-02-19 01:31:02 +0000563 A = pRhs;
drh82c3d632000-06-06 21:56:07 +0000564}
drh0a36c572002-02-18 22:49:59 +0000565%type multiselect_op {int}
drhcf82f0d2016-02-17 04:33:10 +0000566multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
drhfd405312005-11-06 04:06:59 +0000567multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
drhcf82f0d2016-02-17 04:33:10 +0000568multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
drh154d4b22006-09-21 11:02:16 +0000569%endif SQLITE_OMIT_COMPOUND_SELECT
drh550a3302018-07-27 22:14:50 +0000570
drhfef37762018-07-10 19:48:35 +0000571oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
dan67a9b8e2018-06-22 20:51:35 +0000572 groupby_opt(P) having_opt(Q)
dane3bf6322018-06-08 20:58:27 +0000573 orderby_opt(Z) limit_opt(L). {
drh8c0833f2017-11-14 23:48:23 +0000574 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
drh550a3302018-07-27 22:14:50 +0000575}
576%ifndef SQLITE_OMIT_WINDOWFUNC
577oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
578 groupby_opt(P) having_opt(Q) window_clause(R)
579 orderby_opt(Z) limit_opt(L). {
580 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
dan6fde1792018-06-15 19:01:35 +0000581 if( A ){
582 A->pWinDefn = R;
583 }else{
584 sqlite3WindowListDelete(pParse->db, R);
585 }
drh9bb61fe2000-06-05 16:01:39 +0000586}
drh550a3302018-07-27 22:14:50 +0000587%endif
588
589
drh4dd0d3f2016-02-17 01:18:33 +0000590oneselect(A) ::= values(A).
drh75593d92014-01-10 20:46:55 +0000591
592%type values {Select*}
593%destructor values {sqlite3SelectDelete(pParse->db, $$);}
594values(A) ::= VALUES LP nexprlist(X) RP. {
drh8c0833f2017-11-14 23:48:23 +0000595 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
drh75593d92014-01-10 20:46:55 +0000596}
drh954733b2018-07-27 23:33:16 +0000597values(A) ::= values(A) COMMA LP nexprlist(Y) RP. {
drh4dd0d3f2016-02-17 01:18:33 +0000598 Select *pRight, *pLeft = A;
drh8c0833f2017-11-14 23:48:23 +0000599 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
drhf3151f02015-04-16 20:27:09 +0000600 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
drh75593d92014-01-10 20:46:55 +0000601 if( pRight ){
602 pRight->op = TK_ALL;
drh772460f2015-04-16 14:13:12 +0000603 pRight->pPrior = pLeft;
drh75593d92014-01-10 20:46:55 +0000604 A = pRight;
605 }else{
drh772460f2015-04-16 14:13:12 +0000606 A = pLeft;
drh75593d92014-01-10 20:46:55 +0000607 }
608}
drh9bb61fe2000-06-05 16:01:39 +0000609
610// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
611// present and false (0) if it is not.
612//
drh3334d082015-11-10 13:45:21 +0000613%type distinct {int}
drh832ee3d2012-12-18 19:36:11 +0000614distinct(A) ::= DISTINCT. {A = SF_Distinct;}
drh7cea7f92015-05-29 01:35:19 +0000615distinct(A) ::= ALL. {A = SF_All;}
drhefb72512000-05-31 20:00:52 +0000616distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000617
drh9bb61fe2000-06-05 16:01:39 +0000618// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000619// values of the SELECT statement. The "*" in statements like
620// "SELECT * FROM ..." is encoded as a special expression with an
drh1a1d3cd2015-11-19 16:33:31 +0000621// opcode of TK_ASTERISK.
drh9bb61fe2000-06-05 16:01:39 +0000622//
drh348784e2000-05-29 20:41:49 +0000623%type selcollist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000624%destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000625%type sclp {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000626%destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +0000627sclp(A) ::= selcollist(A) COMMA.
drh348784e2000-05-29 20:41:49 +0000628sclp(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000629selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
630 A = sqlite3ExprListAppend(pParse, A, X);
drhb7916a72009-05-27 10:31:29 +0000631 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
drh1be266b2017-12-24 00:18:47 +0000632 sqlite3ExprListSetSpan(pParse,A,B,Z);
drh01f3f252002-05-24 16:14:15 +0000633}
drhd3f5d612017-12-24 17:01:54 +0000634selcollist(A) ::= sclp(A) scanpt STAR. {
drh1a1d3cd2015-11-19 16:33:31 +0000635 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
drh4dd0d3f2016-02-17 01:18:33 +0000636 A = sqlite3ExprListAppend(pParse, A, p);
drh7c917d12001-12-16 20:05:05 +0000637}
drh1be266b2017-12-24 00:18:47 +0000638selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
drhabfd35e2016-12-06 22:47:23 +0000639 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
640 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
641 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
drh4dd0d3f2016-02-17 01:18:33 +0000642 A = sqlite3ExprListAppend(pParse,A, pDot);
drh54473222002-04-04 02:10:55 +0000643}
drh01f3f252002-05-24 16:14:15 +0000644
645// An option "AS <id>" phrase that can follow one of the expressions that
646// define the result set, or one of the tables in the FROM clause.
647//
648%type as {Token}
drh74ad7fe2004-10-07 03:06:28 +0000649as(X) ::= AS nm(Y). {X = Y;}
drh4dd0d3f2016-02-17 01:18:33 +0000650as(X) ::= ids(X).
drh986dde72016-02-29 13:37:21 +0000651as(X) ::= . {X.n = 0; X.z = 0;}
drh9bb61fe2000-06-05 16:01:39 +0000652
drh348784e2000-05-29 20:41:49 +0000653
drhad3cab52002-05-24 02:04:32 +0000654%type seltablist {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000655%destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
drhad3cab52002-05-24 02:04:32 +0000656%type stl_prefix {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000657%destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
drhad3cab52002-05-24 02:04:32 +0000658%type from {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000659%destructor from {sqlite3SrcListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000660
drh01f3f252002-05-24 16:14:15 +0000661// A complete FROM clause.
662//
dan69887c92020-04-27 20:55:33 +0000663from(A) ::= . {A = 0;}
drhfbdc7f62008-12-03 23:23:40 +0000664from(A) ::= FROM seltablist(X). {
drh61dfc312006-12-16 16:25:15 +0000665 A = X;
666 sqlite3SrcListShiftJoinType(A);
667}
drh01f3f252002-05-24 16:14:15 +0000668
669// "seltablist" is a "Select Table List" - the content of the FROM clause
670// in a SELECT statement. "stl_prefix" is a prefix of this list.
671//
drh4dd0d3f2016-02-17 01:18:33 +0000672stl_prefix(A) ::= seltablist(A) joinop(Y). {
drh8a48b9c2015-08-19 15:20:00 +0000673 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
drh01f3f252002-05-24 16:14:15 +0000674}
drh348784e2000-05-29 20:41:49 +0000675stl_prefix(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000676seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_opt(I)
drhe9240412012-12-18 13:12:03 +0000677 on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000678 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
danielk1977b1c685b2008-10-06 16:18:39 +0000679 sqlite3SrcListIndexedBy(pParse, A, &I);
drhc4a3c772001-04-04 11:48:57 +0000680}
drh4dd0d3f2016-02-17 01:18:33 +0000681seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z)
drh01d230c2015-08-19 17:11:37 +0000682 on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000683 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,N,U);
drh01d230c2015-08-19 17:11:37 +0000684 sqlite3SrcListFuncArgs(pParse, A, E);
685}
drh51522cd2005-01-20 13:36:19 +0000686%ifndef SQLITE_OMIT_SUBQUERY
drh4dd0d3f2016-02-17 01:18:33 +0000687 seltablist(A) ::= stl_prefix(A) LP select(S) RP
drh51522cd2005-01-20 13:36:19 +0000688 as(Z) on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000689 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,N,U);
drhd5feede2002-05-08 21:46:14 +0000690 }
drh4dd0d3f2016-02-17 01:18:33 +0000691 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP
drhfbdc7f62008-12-03 23:23:40 +0000692 as(Z) on_opt(N) using_opt(U). {
drh4dd0d3f2016-02-17 01:18:33 +0000693 if( A==0 && Z.n==0 && N==0 && U==0 ){
drhfbdc7f62008-12-03 23:23:40 +0000694 A = F;
drh832ee3d2012-12-18 19:36:11 +0000695 }else if( F->nSrc==1 ){
drh4dd0d3f2016-02-17 01:18:33 +0000696 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,N,U);
drh832ee3d2012-12-18 19:36:11 +0000697 if( A ){
698 struct SrcList_item *pNew = &A->a[A->nSrc-1];
699 struct SrcList_item *pOld = F->a;
700 pNew->zName = pOld->zName;
701 pNew->zDatabase = pOld->zDatabase;
drh3c449c62013-04-30 14:06:57 +0000702 pNew->pSelect = pOld->pSelect;
drh4a5cff72018-12-03 01:47:41 +0000703 if( pOld->fg.isTabFunc ){
704 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
705 pOld->u1.pFuncArg = 0;
706 pOld->fg.isTabFunc = 0;
707 pNew->fg.isTabFunc = 1;
708 }
drh832ee3d2012-12-18 19:36:11 +0000709 pOld->zName = pOld->zDatabase = 0;
drh3c449c62013-04-30 14:06:57 +0000710 pOld->pSelect = 0;
drh832ee3d2012-12-18 19:36:11 +0000711 }
712 sqlite3SrcListDelete(pParse->db, F);
drhfbdc7f62008-12-03 23:23:40 +0000713 }else{
714 Select *pSubquery;
715 sqlite3SrcListShiftJoinType(F);
drh8c0833f2017-11-14 23:48:23 +0000716 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
drh4dd0d3f2016-02-17 01:18:33 +0000717 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,N,U);
drhfbdc7f62008-12-03 23:23:40 +0000718 }
719 }
drh154d4b22006-09-21 11:02:16 +0000720%endif SQLITE_OMIT_SUBQUERY
drhb733d032004-01-24 20:18:12 +0000721
drh113088e2003-03-20 01:16:58 +0000722%type dbnm {Token}
723dbnm(A) ::= . {A.z=0; A.n=0;}
724dbnm(A) ::= DOT nm(X). {A = X;}
725
drh74ad7fe2004-10-07 03:06:28 +0000726%type fullname {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000727%destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
danc9461ec2018-08-29 21:00:16 +0000728fullname(A) ::= nm(X). {
drh29c992c2019-01-17 15:40:41 +0000729 A = sqlite3SrcListAppend(pParse,0,&X,0);
danc9461ec2018-08-29 21:00:16 +0000730 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);
731}
732fullname(A) ::= nm(X) DOT nm(Y). {
drh29c992c2019-01-17 15:40:41 +0000733 A = sqlite3SrcListAppend(pParse,0,&X,&Y);
danc9461ec2018-08-29 21:00:16 +0000734 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);
735}
drh74ad7fe2004-10-07 03:06:28 +0000736
drh5e3a6eb2018-04-19 11:45:16 +0000737%type xfullname {SrcList*}
738%destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
739xfullname(A) ::= nm(X).
drh29c992c2019-01-17 15:40:41 +0000740 {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/}
drh5e3a6eb2018-04-19 11:45:16 +0000741xfullname(A) ::= nm(X) DOT nm(Y).
drh29c992c2019-01-17 15:40:41 +0000742 {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/}
drh5e3a6eb2018-04-19 11:45:16 +0000743xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
drh29c992c2019-01-17 15:40:41 +0000744 A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/
drh5e3a6eb2018-04-19 11:45:16 +0000745 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
746}
747xfullname(A) ::= nm(X) AS nm(Z). {
drh29c992c2019-01-17 15:40:41 +0000748 A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/
drh5e3a6eb2018-04-19 11:45:16 +0000749 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
750}
751
drh01f3f252002-05-24 16:14:15 +0000752%type joinop {int}
drhfd405312005-11-06 04:06:59 +0000753joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
drhcf82f0d2016-02-17 04:33:10 +0000754joinop(X) ::= JOIN_KW(A) JOIN.
755 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
756joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
757 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
drh5ad1a6c2002-07-01 12:27:09 +0000758joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
drhcf82f0d2016-02-17 04:33:10 +0000759 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
drh01f3f252002-05-24 16:14:15 +0000760
drh26cf56f2018-04-06 19:36:49 +0000761// There is a parsing abiguity in an upsert statement that uses a
762// SELECT on the RHS of a the INSERT:
763//
764// INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
765// here ----^^
766//
767// When the ON token is encountered, the parser does not know if it is
768// the beginning of an ON CONFLICT clause, or the beginning of an ON
769// clause associated with the JOIN. The conflict is resolved in favor
770// of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
771// WHERE clause in between, like this:
772//
773// INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
774//
775// The [AND] and [OR] precedence marks in the rules for on_opt cause the
776// ON in this context to always be interpreted as belonging to the JOIN.
777//
drh01f3f252002-05-24 16:14:15 +0000778%type on_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000779%destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
drh26cf56f2018-04-06 19:36:49 +0000780on_opt(N) ::= ON expr(E). {N = E;}
781on_opt(N) ::= . [OR] {N = 0;}
drh01f3f252002-05-24 16:14:15 +0000782
danielk197785574e32008-10-06 05:32:18 +0000783// Note that this block abuses the Token type just a little. If there is
784// no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
785// there is an INDEXED BY clause, then the token is populated as per normal,
786// with z pointing to the token data and n containing the number of bytes
787// in the token.
788//
789// If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
danielk1977b1c685b2008-10-06 16:18:39 +0000790// normally illegal. The sqlite3SrcListIndexedBy() function
danielk197785574e32008-10-06 05:32:18 +0000791// recognizes and interprets this as a special case.
792//
793%type indexed_opt {Token}
794indexed_opt(A) ::= . {A.z=0; A.n=0;}
795indexed_opt(A) ::= INDEXED BY nm(X). {A = X;}
796indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
797
drh01f3f252002-05-24 16:14:15 +0000798%type using_opt {IdList*}
drh633e6d52008-07-28 19:34:53 +0000799%destructor using_opt {sqlite3IdListDelete(pParse->db, $$);}
drh81eba732013-10-19 23:31:56 +0000800using_opt(U) ::= USING LP idlist(L) RP. {U = L;}
drh01f3f252002-05-24 16:14:15 +0000801using_opt(U) ::= . {U = 0;}
802
803
drh348784e2000-05-29 20:41:49 +0000804%type orderby_opt {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000805%destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
drh108aa002015-08-24 20:21:20 +0000806
807// the sortlist non-terminal stores a list of expression where each
808// expression is optionally followed by ASC or DESC to indicate the
809// sort order.
810//
drh348784e2000-05-29 20:41:49 +0000811%type sortlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000812%destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000813
814orderby_opt(A) ::= . {A = 0;}
815orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
dan6e118922019-08-12 16:36:38 +0000816sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
drh1be266b2017-12-24 00:18:47 +0000817 A = sqlite3ExprListAppend(pParse,A,Y);
dan6e118922019-08-12 16:36:38 +0000818 sqlite3ExprListSetSortOrder(A,Z,X);
drh9bb61fe2000-06-05 16:01:39 +0000819}
dan6e118922019-08-12 16:36:38 +0000820sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
drh1be266b2017-12-24 00:18:47 +0000821 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
dan6e118922019-08-12 16:36:38 +0000822 sqlite3ExprListSetSortOrder(A,Z,X);
drh9bb61fe2000-06-05 16:01:39 +0000823}
drh348784e2000-05-29 20:41:49 +0000824
825%type sortorder {int}
826
drh8e2ca022002-06-17 17:07:19 +0000827sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
828sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
drhbc622bc2015-08-24 15:39:42 +0000829sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
drh348784e2000-05-29 20:41:49 +0000830
dan6e118922019-08-12 16:36:38 +0000831%type nulls {int}
832nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;}
833nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;}
834nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;}
835
drh22827922000-06-06 17:27:05 +0000836%type groupby_opt {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000837%destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
drh6206d502000-06-19 19:09:08 +0000838groupby_opt(A) ::= . {A = 0;}
drh9245c242007-06-20 12:18:31 +0000839groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
drh22827922000-06-06 17:27:05 +0000840
841%type having_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000842%destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
drh6206d502000-06-19 19:09:08 +0000843having_opt(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000844having_opt(A) ::= HAVING expr(X). {A = X;}
drh22827922000-06-06 17:27:05 +0000845
drh8c0833f2017-11-14 23:48:23 +0000846%type limit_opt {Expr*}
drh15926592007-04-06 15:02:13 +0000847
848// The destructor for limit_opt will never fire in the current grammar.
849// The limit_opt non-terminal only occurs at the end of a single production
850// rule for SELECT statements. As soon as the rule that create the
851// limit_opt non-terminal reduces, the SELECT statement rule will also
852// reduce. So there is never a limit_opt non-terminal on the stack
853// except as a transient. So there is never anything to destroy.
854//
drh8c0833f2017-11-14 23:48:23 +0000855//%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
856limit_opt(A) ::= . {A = 0;}
857limit_opt(A) ::= LIMIT expr(X).
drh1be266b2017-12-24 00:18:47 +0000858 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
danielk1977a2dc3b12005-02-05 12:48:48 +0000859limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
drh1be266b2017-12-24 00:18:47 +0000860 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
danielk1977a2dc3b12005-02-05 12:48:48 +0000861limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
drh1be266b2017-12-24 00:18:47 +0000862 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
drh9bbca4c2001-11-06 04:00:18 +0000863
drh382c0242001-10-06 16:33:02 +0000864/////////////////////////// The DELETE statement /////////////////////////////
865//
drhc601f102020-07-03 17:24:35 +0000866%if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
drh2053f312021-01-12 20:16:31 +0000867cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W)
drh931577f2008-10-10 14:27:16 +0000868 orderby_opt(O) limit_opt(L). {
danielk1977b1c685b2008-10-06 16:18:39 +0000869 sqlite3SrcListIndexedBy(pParse, X, &I);
drh6a0db872019-01-31 02:42:47 +0000870#ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
drhc601f102020-07-03 17:24:35 +0000871 if( O || L ){
872 updateDeleteLimitError(pParse,O,L);
873 O = 0;
874 L = 0;
875 }
drh6a0db872019-01-31 02:42:47 +0000876#endif
drh8c0833f2017-11-14 23:48:23 +0000877 sqlite3DeleteFrom(pParse,X,W,O,L);
danielk1977b1c685b2008-10-06 16:18:39 +0000878}
drhc601f102020-07-03 17:24:35 +0000879%else
drh2053f312021-01-12 20:16:31 +0000880cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). {
shane4281bd42008-10-07 05:27:11 +0000881 sqlite3SrcListIndexedBy(pParse, X, &I);
drh8c0833f2017-11-14 23:48:23 +0000882 sqlite3DeleteFrom(pParse,X,W,0,0);
shane4281bd42008-10-07 05:27:11 +0000883}
884%endif
drh348784e2000-05-29 20:41:49 +0000885
886%type where_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000887%destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
drh2053f312021-01-12 20:16:31 +0000888%type where_opt_ret {Expr*}
889%destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000890
891where_opt(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000892where_opt(A) ::= WHERE expr(X). {A = X;}
drh2053f312021-01-12 20:16:31 +0000893where_opt_ret(A) ::= . {A = 0;}
894where_opt_ret(A) ::= WHERE expr(X). {A = X;}
895where_opt_ret(A) ::= RETURNING selcollist(X).
896 {sqlite3AddReturning(pParse,X); A = 0;}
897where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y).
898 {sqlite3AddReturning(pParse,Y); A = X;}
drh348784e2000-05-29 20:41:49 +0000899
drh382c0242001-10-06 16:33:02 +0000900////////////////////////// The UPDATE command ////////////////////////////////
901//
drhc601f102020-07-03 17:24:35 +0000902%if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
dan69887c92020-04-27 20:55:33 +0000903cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
drhb8352472021-01-29 19:32:17 +0000904 where_opt_ret(W) orderby_opt(O) limit_opt(L). {
danielk1977b1c685b2008-10-06 16:18:39 +0000905 sqlite3SrcListIndexedBy(pParse, X, &I);
dan69887c92020-04-27 20:55:33 +0000906 X = sqlite3SrcListAppendList(pParse, X, F);
drhb1a6c3c2008-03-20 16:30:17 +0000907 sqlite3ExprListCheckLength(pParse,Y,"set list");
drhc601f102020-07-03 17:24:35 +0000908#ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
909 if( O || L ){
910 updateDeleteLimitError(pParse,O,L);
911 O = 0;
912 L = 0;
913 }
914#endif
drheac9fab2018-04-16 13:00:50 +0000915 sqlite3Update(pParse,X,Y,W,R,O,L,0);
danielk19777a15a4b2007-05-08 17:54:43 +0000916}
drhc601f102020-07-03 17:24:35 +0000917%else
dan69887c92020-04-27 20:55:33 +0000918cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
drhb8352472021-01-29 19:32:17 +0000919 where_opt_ret(W). {
shane4281bd42008-10-07 05:27:11 +0000920 sqlite3SrcListIndexedBy(pParse, X, &I);
921 sqlite3ExprListCheckLength(pParse,Y,"set list");
dan69887c92020-04-27 20:55:33 +0000922 X = sqlite3SrcListAppendList(pParse, X, F);
drheac9fab2018-04-16 13:00:50 +0000923 sqlite3Update(pParse,X,Y,W,R,0,0,0);
shane4281bd42008-10-07 05:27:11 +0000924}
925%endif
drh348784e2000-05-29 20:41:49 +0000926
drhc601f102020-07-03 17:24:35 +0000927
928
drhf8db1bc2005-04-22 02:38:37 +0000929%type setlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000930%destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
drhf8db1bc2005-04-22 02:38:37 +0000931
drh4dd0d3f2016-02-17 01:18:33 +0000932setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000933 A = sqlite3ExprListAppend(pParse, A, Y);
drhb7916a72009-05-27 10:31:29 +0000934 sqlite3ExprListSetName(pParse, A, &X, 1);
935}
drha1251bc2016-08-20 00:51:37 +0000936setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000937 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
drha1251bc2016-08-20 00:51:37 +0000938}
drhb7916a72009-05-27 10:31:29 +0000939setlist(A) ::= nm(X) EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000940 A = sqlite3ExprListAppend(pParse, 0, Y);
drhb7916a72009-05-27 10:31:29 +0000941 sqlite3ExprListSetName(pParse, A, &X, 1);
942}
drha1251bc2016-08-20 00:51:37 +0000943setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000944 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
drha1251bc2016-08-20 00:51:37 +0000945}
drh348784e2000-05-29 20:41:49 +0000946
drh382c0242001-10-06 16:33:02 +0000947////////////////////////// The INSERT command /////////////////////////////////
948//
drh5e3a6eb2018-04-19 11:45:16 +0000949cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
drh2c2e8442018-04-07 15:04:05 +0000950 upsert(U). {
drh46d2e5c2018-04-12 13:15:43 +0000951 sqlite3Insert(pParse, X, S, F, R, U);
dan4e9119d2014-01-13 15:12:23 +0000952}
drh2053f312021-01-12 20:16:31 +0000953cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning.
dan4e9119d2014-01-13 15:12:23 +0000954{
drh2c2e8442018-04-07 15:04:05 +0000955 sqlite3Insert(pParse, X, 0, F, R, 0);
dan4e9119d2014-01-13 15:12:23 +0000956}
drh348784e2000-05-29 20:41:49 +0000957
drh46d2e5c2018-04-12 13:15:43 +0000958%type upsert {Upsert*}
drh1c198482020-12-14 13:52:03 +0000959
960// Because upsert only occurs at the tip end of the INSERT rule for cmd,
961// there is never a case where the value of the upsert pointer will not
962// be destroyed by the cmd action. So comment-out the destructor to
963// avoid unreachable code.
964//%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
drh46d2e5c2018-04-12 13:15:43 +0000965upsert(A) ::= . { A = 0; }
drh2053f312021-01-12 20:16:31 +0000966upsert(A) ::= RETURNING selcollist(X). { A = 0; sqlite3AddReturning(pParse,X); }
drhe9c2e772018-04-13 13:06:45 +0000967upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
drh2549e4c2020-12-08 14:29:03 +0000968 DO UPDATE SET setlist(Z) where_opt(W) upsert(N).
969 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);}
970upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N).
971 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); }
drh2053f312021-01-12 20:16:31 +0000972upsert(A) ::= ON CONFLICT DO NOTHING returning.
drh2549e4c2020-12-08 14:29:03 +0000973 { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
drh2053f312021-01-12 20:16:31 +0000974upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning.
drh2549e4c2020-12-08 14:29:03 +0000975 { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);}
drh26cf56f2018-04-06 19:36:49 +0000976
drh2053f312021-01-12 20:16:31 +0000977returning ::= RETURNING selcollist(X). {sqlite3AddReturning(pParse,X);}
978returning ::= .
979
drh3334d082015-11-10 13:45:21 +0000980%type insert_cmd {int}
drhfa86c412002-02-02 15:01:15 +0000981insert_cmd(A) ::= INSERT orconf(R). {A = R;}
982insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
983
drh8981b902015-08-24 17:42:49 +0000984%type idlist_opt {IdList*}
985%destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
drh81eba732013-10-19 23:31:56 +0000986%type idlist {IdList*}
987%destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000988
drh8981b902015-08-24 17:42:49 +0000989idlist_opt(A) ::= . {A = 0;}
990idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000991idlist(A) ::= idlist(A) COMMA nm(Y).
dan5496d6a2018-08-13 17:14:26 +0000992 {A = sqlite3IdListAppend(pParse,A,&Y);}
drh81eba732013-10-19 23:31:56 +0000993idlist(A) ::= nm(Y).
dan5496d6a2018-08-13 17:14:26 +0000994 {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}
drh348784e2000-05-29 20:41:49 +0000995
drh382c0242001-10-06 16:33:02 +0000996/////////////////////////// Expression Processing /////////////////////////////
997//
drh348784e2000-05-29 20:41:49 +0000998
drh1be266b2017-12-24 00:18:47 +0000999%type expr {Expr*}
1000%destructor expr {sqlite3ExprDelete(pParse->db, $$);}
1001%type term {Expr*}
1002%destructor term {sqlite3ExprDelete(pParse->db, $$);}
drhb7916a72009-05-27 10:31:29 +00001003
1004%include {
drhb7916a72009-05-27 10:31:29 +00001005
1006 /* Construct a new Expr object from a single identifier. Use the
1007 ** new Expr to populate pOut. Set the span of pOut to be the identifier
1008 ** that created the expression.
1009 */
drh1be266b2017-12-24 00:18:47 +00001010 static Expr *tokenExpr(Parse *pParse, int op, Token t){
drh0cd874b2016-09-26 12:38:22 +00001011 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
1012 if( p ){
dand145e5f2018-08-21 08:29:48 +00001013 /* memset(p, 0, sizeof(Expr)); */
drh0cd874b2016-09-26 12:38:22 +00001014 p->op = (u8)op;
drh11949042019-08-05 18:01:42 +00001015 p->affExpr = 0;
drh0cd874b2016-09-26 12:38:22 +00001016 p->flags = EP_Leaf;
drhe7375bf2020-03-10 19:24:38 +00001017 ExprClearVVAProperties(p);
drh0cd874b2016-09-26 12:38:22 +00001018 p->iAgg = -1;
dand145e5f2018-08-21 08:29:48 +00001019 p->pLeft = p->pRight = 0;
1020 p->x.pList = 0;
1021 p->pAggInfo = 0;
drheda079c2018-09-20 19:02:15 +00001022 p->y.pTab = 0;
dand145e5f2018-08-21 08:29:48 +00001023 p->op2 = 0;
drh4fe759b2018-08-23 18:22:00 +00001024 p->iTable = 0;
drhd3130da2018-08-23 18:50:19 +00001025 p->iColumn = 0;
drh0cd874b2016-09-26 12:38:22 +00001026 p->u.zToken = (char*)&p[1];
1027 memcpy(p->u.zToken, t.z, t.n);
1028 p->u.zToken[t.n] = 0;
1029 if( sqlite3Isquote(p->u.zToken[0]) ){
drh51d35b02019-01-11 13:32:23 +00001030 sqlite3DequoteExpr(p);
drh0cd874b2016-09-26 12:38:22 +00001031 }
1032#if SQLITE_MAX_EXPR_DEPTH>0
1033 p->nHeight = 1;
1034#endif
danc9461ec2018-08-29 21:00:16 +00001035 if( IN_RENAME_OBJECT ){
dan07e95232018-08-21 16:32:53 +00001036 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
dand145e5f2018-08-21 08:29:48 +00001037 }
drh0cd874b2016-09-26 12:38:22 +00001038 }
drh1be266b2017-12-24 00:18:47 +00001039 return p;
drhb7916a72009-05-27 10:31:29 +00001040 }
dand145e5f2018-08-21 08:29:48 +00001041
drhb7916a72009-05-27 10:31:29 +00001042}
drh348784e2000-05-29 20:41:49 +00001043
drh4dd0d3f2016-02-17 01:18:33 +00001044expr(A) ::= term(A).
drh1be266b2017-12-24 00:18:47 +00001045expr(A) ::= LP expr(X) RP. {A = X;}
1046expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
1047expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
drh5ad1a6c2002-07-01 12:27:09 +00001048expr(A) ::= nm(X) DOT nm(Y). {
drh410c3012016-09-24 17:42:43 +00001049 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
1050 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
danc9461ec2018-08-29 21:00:16 +00001051 if( IN_RENAME_OBJECT ){
1052 sqlite3RenameTokenMap(pParse, (void*)temp2, &Y);
1053 sqlite3RenameTokenMap(pParse, (void*)temp1, &X);
1054 }
drh1be266b2017-12-24 00:18:47 +00001055 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
drhe1b6a5b2000-07-29 13:06:59 +00001056}
drhd24cc422003-03-27 12:51:24 +00001057expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
drh410c3012016-09-24 17:42:43 +00001058 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
1059 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
1060 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
drhabfd35e2016-12-06 22:47:23 +00001061 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
danc9461ec2018-08-29 21:00:16 +00001062 if( IN_RENAME_OBJECT ){
1063 sqlite3RenameTokenMap(pParse, (void*)temp3, &Z);
1064 sqlite3RenameTokenMap(pParse, (void*)temp2, &Y);
1065 }
drh1be266b2017-12-24 00:18:47 +00001066 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
drhd24cc422003-03-27 12:51:24 +00001067}
drh1be266b2017-12-24 00:18:47 +00001068term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1069term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
drh0cd874b2016-09-26 12:38:22 +00001070term(A) ::= INTEGER(X). {
drh1be266b2017-12-24 00:18:47 +00001071 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
drh0cd874b2016-09-26 12:38:22 +00001072}
drh7c972de2003-09-06 22:18:07 +00001073expr(A) ::= VARIABLE(X). {
drh8679fba2016-04-11 01:43:33 +00001074 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
drhde25a882016-10-03 15:28:24 +00001075 u32 n = X.n;
drh1be266b2017-12-24 00:18:47 +00001076 A = tokenExpr(pParse, TK_VARIABLE, X);
1077 sqlite3ExprAssignVarNumber(pParse, A, n);
drh8f3b1372016-04-11 01:26:31 +00001078 }else{
drhf59b12f2014-01-11 03:54:05 +00001079 /* When doing a nested parse, one can include terms in an expression
1080 ** that look like this: #1 #2 ... These terms refer to registers
1081 ** in the virtual machine. #N is the N-th register. */
drh8f3b1372016-04-11 01:26:31 +00001082 Token t = X; /*A-overwrites-X*/
1083 assert( t.n>=2 );
drhf59b12f2014-01-11 03:54:05 +00001084 if( pParse->nested==0 ){
drh43303de2016-02-17 12:34:03 +00001085 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
drh1be266b2017-12-24 00:18:47 +00001086 A = 0;
drhf59b12f2014-01-11 03:54:05 +00001087 }else{
drh1be266b2017-12-24 00:18:47 +00001088 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1089 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
drhf59b12f2014-01-11 03:54:05 +00001090 }
drhf59b12f2014-01-11 03:54:05 +00001091 }
drh7c972de2003-09-06 22:18:07 +00001092}
dan59ff4252018-06-29 17:44:52 +00001093expr(A) ::= expr(A) COLLATE ids(C). {
drh1be266b2017-12-24 00:18:47 +00001094 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
drh8b4c40d2007-02-01 23:02:45 +00001095}
drh487e2622005-06-25 18:42:14 +00001096%ifndef SQLITE_OMIT_CAST
drh1be266b2017-12-24 00:18:47 +00001097expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1098 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1099 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
drh487e2622005-06-25 18:42:14 +00001100}
drh154d4b22006-09-21 11:02:16 +00001101%endif SQLITE_OMIT_CAST
drh550a3302018-07-27 22:14:50 +00001102
1103
1104expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. {
drh954733b2018-07-27 23:33:16 +00001105 A = sqlite3ExprFunction(pParse, Y, &X, D);
drh550a3302018-07-27 22:14:50 +00001106}
1107expr(A) ::= id(X) LP STAR RP. {
drh954733b2018-07-27 23:33:16 +00001108 A = sqlite3ExprFunction(pParse, 0, &X, 0);
drh550a3302018-07-27 22:14:50 +00001109}
1110
dan67a9b8e2018-06-22 20:51:35 +00001111%ifndef SQLITE_OMIT_WINDOWFUNC
dan4f9adee2019-07-13 16:22:50 +00001112expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
dan64882712019-07-11 18:43:33 +00001113 A = sqlite3ExprFunction(pParse, Y, &X, D);
dan4f9adee2019-07-13 16:22:50 +00001114 sqlite3WindowAttach(pParse, A, Z);
dan64882712019-07-11 18:43:33 +00001115}
dan4f9adee2019-07-13 16:22:50 +00001116expr(A) ::= id(X) LP STAR RP filter_over(Z). {
drh954733b2018-07-27 23:33:16 +00001117 A = sqlite3ExprFunction(pParse, 0, &X, 0);
dan4f9adee2019-07-13 16:22:50 +00001118 sqlite3WindowAttach(pParse, A, Z);
drhe1b6a5b2000-07-29 13:06:59 +00001119}
drh550a3302018-07-27 22:14:50 +00001120%endif
1121
drhb71090f2005-05-23 17:26:51 +00001122term(A) ::= CTIME_KW(OP). {
drh954733b2018-07-27 23:33:16 +00001123 A = sqlite3ExprFunction(pParse, 0, &OP, 0);
drhb7916a72009-05-27 10:31:29 +00001124}
1125
drh1be266b2017-12-24 00:18:47 +00001126expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1127 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1128 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1129 if( A ){
1130 A->x.pList = pList;
drh76baf792019-10-28 04:20:28 +00001131 if( ALWAYS(pList->nExpr) ){
drh269d3222019-10-23 18:09:39 +00001132 A->flags |= pList->a[0].pExpr->flags & EP_Propagate;
1133 }
drh8bd0d582016-08-20 18:06:14 +00001134 }else{
1135 sqlite3ExprListDelete(pParse->db, pList);
dan71c57db2016-07-09 20:23:55 +00001136 }
1137}
1138
drhd5c851c2019-04-19 13:38:34 +00001139expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);}
drh1be266b2017-12-24 00:18:47 +00001140expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001141expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001142 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1143expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001144expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001145 {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001146expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001147 {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001148expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001149 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1150expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh410c3012016-09-24 17:42:43 +00001151%type likeop {Token}
drh7e84b372017-02-20 14:30:17 +00001152likeop(A) ::= LIKE_KW|MATCH(A).
drh410c3012016-09-24 17:42:43 +00001153likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
drh4dd0d3f2016-02-17 01:18:33 +00001154expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
drh8aa34ae2006-03-13 12:54:09 +00001155 ExprList *pList;
drh410c3012016-09-24 17:42:43 +00001156 int bNot = OP.n & 0x80000000;
1157 OP.n &= 0x7fffffff;
drh1be266b2017-12-24 00:18:47 +00001158 pList = sqlite3ExprListAppend(pParse,0, Y);
1159 pList = sqlite3ExprListAppend(pParse,pList, A);
drh954733b2018-07-27 23:33:16 +00001160 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
drh1be266b2017-12-24 00:18:47 +00001161 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1162 if( A ) A->flags |= EP_InfixFunc;
drh0ac65892002-04-20 14:24:41 +00001163}
drh4dd0d3f2016-02-17 01:18:33 +00001164expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
drh1dca1452010-07-19 02:30:33 +00001165 ExprList *pList;
drh410c3012016-09-24 17:42:43 +00001166 int bNot = OP.n & 0x80000000;
1167 OP.n &= 0x7fffffff;
drh1be266b2017-12-24 00:18:47 +00001168 pList = sqlite3ExprListAppend(pParse,0, Y);
1169 pList = sqlite3ExprListAppend(pParse,pList, A);
1170 pList = sqlite3ExprListAppend(pParse,pList, E);
drh954733b2018-07-27 23:33:16 +00001171 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
drh1be266b2017-12-24 00:18:47 +00001172 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1173 if( A ) A->flags |= EP_InfixFunc;
drh1dca1452010-07-19 02:30:33 +00001174}
danielk19777c6303c2004-11-17 16:41:29 +00001175
drh1be266b2017-12-24 00:18:47 +00001176expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1177expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
drh6a2fe092009-09-23 02:29:36 +00001178
drh6a517412009-11-12 03:46:34 +00001179%include {
1180 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1181 ** unary TK_ISNULL or TK_NOTNULL expression. */
1182 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1183 sqlite3 *db = pParse->db;
danc50f75d2018-09-06 18:56:36 +00001184 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
shaneh5e17e8b2009-12-03 04:40:47 +00001185 pA->op = (u8)op;
drh6a517412009-11-12 03:46:34 +00001186 sqlite3ExprDelete(db, pA->pRight);
1187 pA->pRight = 0;
1188 }
1189 }
1190}
1191
drh6a2fe092009-09-23 02:29:36 +00001192// expr1 IS expr2
1193// expr1 IS NOT expr2
1194//
1195// If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1196// is any other expression, code as TK_IS or TK_ISNOT.
1197//
drh4dd0d3f2016-02-17 01:18:33 +00001198expr(A) ::= expr(A) IS expr(Y). {
drh1be266b2017-12-24 00:18:47 +00001199 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1200 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
drh6a2fe092009-09-23 02:29:36 +00001201}
drh4dd0d3f2016-02-17 01:18:33 +00001202expr(A) ::= expr(A) IS NOT expr(Y). {
drh1be266b2017-12-24 00:18:47 +00001203 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1204 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
drh6a2fe092009-09-23 02:29:36 +00001205}
drhb7916a72009-05-27 10:31:29 +00001206
drh43303de2016-02-17 12:34:03 +00001207expr(A) ::= NOT(B) expr(X).
drh1be266b2017-12-24 00:18:47 +00001208 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
drh43303de2016-02-17 12:34:03 +00001209expr(A) ::= BITNOT(B) expr(X).
drh1be266b2017-12-24 00:18:47 +00001210 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
drhca5aa592018-06-19 11:15:19 +00001211expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1212 A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
1213 /*A-overwrites-B*/
1214}
drhb7916a72009-05-27 10:31:29 +00001215
drh2e3a1f12004-10-06 14:39:28 +00001216%type between_op {int}
1217between_op(A) ::= BETWEEN. {A = 0;}
1218between_op(A) ::= NOT BETWEEN. {A = 1;}
drh4dd0d3f2016-02-17 01:18:33 +00001219expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
drh1be266b2017-12-24 00:18:47 +00001220 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1221 pList = sqlite3ExprListAppend(pParse,pList, Y);
1222 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1223 if( A ){
1224 A->x.pList = pList;
drh53f733c2005-09-16 02:38:09 +00001225 }else{
drh633e6d52008-07-28 19:34:53 +00001226 sqlite3ExprListDelete(pParse->db, pList);
drh53f733c2005-09-16 02:38:09 +00001227 }
drh1be266b2017-12-24 00:18:47 +00001228 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drhfef52082000-06-06 01:50:43 +00001229}
drh51522cd2005-01-20 13:36:19 +00001230%ifndef SQLITE_OMIT_SUBQUERY
danielk19773e8c37e2005-01-21 03:12:14 +00001231 %type in_op {int}
1232 in_op(A) ::= IN. {A = 0;}
1233 in_op(A) ::= NOT IN. {A = 1;}
drh1be266b2017-12-24 00:18:47 +00001234 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
drh094430e2010-07-14 18:24:06 +00001235 if( Y==0 ){
dan473c1bf2010-07-15 11:14:21 +00001236 /* Expressions of the form
1237 **
1238 ** expr1 IN ()
1239 ** expr1 NOT IN ()
1240 **
1241 ** simplify to constants 0 (false) and 1 (true), respectively,
1242 ** regardless of the value of expr1.
1243 */
drh8e34e402019-06-11 10:43:56 +00001244 sqlite3ExprUnmapAndDelete(pParse, A);
drh5776ee52019-09-23 12:38:10 +00001245 A = sqlite3Expr(pParse->db, TK_INTEGER, N ? "1" : "0");
drh95b39592020-03-26 00:29:50 +00001246 }else if( Y->nExpr==1 && sqlite3ExprIsConstant(Y->a[0].pExpr) ){
drhfbfd1132020-01-28 18:09:53 +00001247 Expr *pRHS = Y->a[0].pExpr;
1248 Y->a[0].pExpr = 0;
1249 sqlite3ExprListDelete(pParse->db, Y);
drh95b39592020-03-26 00:29:50 +00001250 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
drhfbfd1132020-01-28 18:09:53 +00001251 A = sqlite3PExpr(pParse, TK_EQ, A, pRHS);
1252 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
danielk1977d5d56522005-03-16 12:15:20 +00001253 }else{
drh1be266b2017-12-24 00:18:47 +00001254 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1255 if( A ){
1256 A->x.pList = Y;
1257 sqlite3ExprSetHeightAndFlags(pParse, A);
drh094430e2010-07-14 18:24:06 +00001258 }else{
1259 sqlite3ExprListDelete(pParse->db, Y);
1260 }
drh1be266b2017-12-24 00:18:47 +00001261 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
danielk1977d5d56522005-03-16 12:15:20 +00001262 }
danielk19773e8c37e2005-01-21 03:12:14 +00001263 }
drh1be266b2017-12-24 00:18:47 +00001264 expr(A) ::= LP select(X) RP. {
1265 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1266 sqlite3PExprAddSelect(pParse, A, X);
drh51522cd2005-01-20 13:36:19 +00001267 }
drh1be266b2017-12-24 00:18:47 +00001268 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1269 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1270 sqlite3PExprAddSelect(pParse, A, Y);
1271 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drh51522cd2005-01-20 13:36:19 +00001272 }
drh5fbab882016-07-02 12:08:14 +00001273 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
drh29c992c2019-01-17 15:40:41 +00001274 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
drh8c0833f2017-11-14 23:48:23 +00001275 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
drh9de47572016-07-02 12:33:21 +00001276 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
drh1be266b2017-12-24 00:18:47 +00001277 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1278 sqlite3PExprAddSelect(pParse, A, pSelect);
1279 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drh51522cd2005-01-20 13:36:19 +00001280 }
drh1be266b2017-12-24 00:18:47 +00001281 expr(A) ::= EXISTS LP select(Y) RP. {
drh43303de2016-02-17 12:34:03 +00001282 Expr *p;
drh1be266b2017-12-24 00:18:47 +00001283 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
drh08de4f72016-04-11 01:06:47 +00001284 sqlite3PExprAddSelect(pParse, p, Y);
drh51522cd2005-01-20 13:36:19 +00001285 }
drh154d4b22006-09-21 11:02:16 +00001286%endif SQLITE_OMIT_SUBQUERY
drhfef52082000-06-06 01:50:43 +00001287
drh17a7f8d2002-03-24 13:13:27 +00001288/* CASE expressions */
drh1be266b2017-12-24 00:18:47 +00001289expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1290 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1291 if( A ){
1292 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1293 sqlite3ExprSetHeightAndFlags(pParse, A);
drh53f733c2005-09-16 02:38:09 +00001294 }else{
drh633e6d52008-07-28 19:34:53 +00001295 sqlite3ExprListDelete(pParse->db, Y);
drhc5cd1242013-09-12 16:50:49 +00001296 sqlite3ExprDelete(pParse->db, Z);
drh53f733c2005-09-16 02:38:09 +00001297 }
drh17a7f8d2002-03-24 13:13:27 +00001298}
1299%type case_exprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001300%destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +00001301case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
drh1be266b2017-12-24 00:18:47 +00001302 A = sqlite3ExprListAppend(pParse,A, Y);
1303 A = sqlite3ExprListAppend(pParse,A, Z);
drh17a7f8d2002-03-24 13:13:27 +00001304}
1305case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
drh1be266b2017-12-24 00:18:47 +00001306 A = sqlite3ExprListAppend(pParse,0, Y);
1307 A = sqlite3ExprListAppend(pParse,A, Z);
drh17a7f8d2002-03-24 13:13:27 +00001308}
1309%type case_else {Expr*}
drh633e6d52008-07-28 19:34:53 +00001310%destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +00001311case_else(A) ::= ELSE expr(X). {A = X;}
drh17a7f8d2002-03-24 13:13:27 +00001312case_else(A) ::= . {A = 0;}
1313%type case_operand {Expr*}
drh633e6d52008-07-28 19:34:53 +00001314%destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +00001315case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/}
drh17a7f8d2002-03-24 13:13:27 +00001316case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +00001317
1318%type exprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001319%destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh9245c242007-06-20 12:18:31 +00001320%type nexprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001321%destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +00001322
drh4dd0d3f2016-02-17 01:18:33 +00001323exprlist(A) ::= nexprlist(A).
drh9245c242007-06-20 12:18:31 +00001324exprlist(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +00001325nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
drh1be266b2017-12-24 00:18:47 +00001326 {A = sqlite3ExprListAppend(pParse,A,Y);}
drh17435752007-08-16 04:30:38 +00001327nexprlist(A) ::= expr(Y).
drh1be266b2017-12-24 00:18:47 +00001328 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
drh9245c242007-06-20 12:18:31 +00001329
drha5224732016-07-25 14:40:43 +00001330%ifndef SQLITE_OMIT_SUBQUERY
drh5fbab882016-07-02 12:08:14 +00001331/* A paren_exprlist is an optional expression list contained inside
1332** of parenthesis */
1333%type paren_exprlist {ExprList*}
1334%destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1335paren_exprlist(A) ::= . {A = 0;}
1336paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
drha5224732016-07-25 14:40:43 +00001337%endif SQLITE_OMIT_SUBQUERY
drh5fbab882016-07-02 12:08:14 +00001338
drhcce7d172000-05-31 15:34:51 +00001339
drh382c0242001-10-06 16:33:02 +00001340///////////////////////////// The CREATE INDEX command ///////////////////////
1341//
drhd9da78a2009-03-24 15:08:09 +00001342cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
drh108aa002015-08-24 20:21:20 +00001343 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
drh17435752007-08-16 04:30:38 +00001344 sqlite3CreateIndex(pParse, &X, &D,
drh29c992c2019-01-17 15:40:41 +00001345 sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
drh62340f82016-05-31 21:18:15 +00001346 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
danc9461ec2018-08-29 21:00:16 +00001347 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
1348 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
1349 }
drh9cfcf5d2002-01-29 18:41:24 +00001350}
drh717e6402001-09-27 03:22:32 +00001351
1352%type uniqueflag {int}
drh74ad7fe2004-10-07 03:06:28 +00001353uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1354uniqueflag(A) ::= . {A = OE_None;}
drh348784e2000-05-29 20:41:49 +00001355
drh348784e2000-05-29 20:41:49 +00001356
drh108aa002015-08-24 20:21:20 +00001357// The eidlist non-terminal (Expression Id List) generates an ExprList
1358// from a list of identifiers. The identifier names are in ExprList.a[].zName.
1359// This list is stored in an ExprList rather than an IdList so that it
1360// can be easily sent to sqlite3ColumnsExprList().
1361//
1362// eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1363// used for the arguments to an index. That is just an historical accident.
1364//
1365// IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1366// COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
drh067b92b2020-06-19 15:24:12 +00001367// places - places that might have been stored in the sqlite_schema table.
drh108aa002015-08-24 20:21:20 +00001368// Those extra features were ignored. But because they might be in some
1369// (busted) old databases, we need to continue parsing them when loading
1370// historical schemas.
1371//
1372%type eidlist {ExprList*}
1373%destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1374%type eidlist_opt {ExprList*}
1375%destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1376
1377%include {
1378 /* Add a single new term to an ExprList that is used to store a
1379 ** list of identifiers. Report an error if the ID list contains
1380 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1381 ** error while parsing a legacy schema.
1382 */
1383 static ExprList *parserAddExprIdListTerm(
1384 Parse *pParse,
1385 ExprList *pPrior,
1386 Token *pIdToken,
1387 int hasCollate,
1388 int sortOrder
1389 ){
1390 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1391 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1392 && pParse->db->init.busy==0
1393 ){
1394 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1395 pIdToken->n, pIdToken->z);
1396 }
1397 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1398 return p;
1399 }
1400} // end %include
1401
1402eidlist_opt(A) ::= . {A = 0;}
1403eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +00001404eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1405 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
danielk19770202b292004-06-09 09:55:16 +00001406}
drh108aa002015-08-24 20:21:20 +00001407eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
drhcf82f0d2016-02-17 04:33:10 +00001408 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
danielk19770202b292004-06-09 09:55:16 +00001409}
danielk19770202b292004-06-09 09:55:16 +00001410
drh108aa002015-08-24 20:21:20 +00001411%type collate {int}
1412collate(C) ::= . {C = 0;}
dan59ff4252018-06-29 17:44:52 +00001413collate(C) ::= COLLATE ids. {C = 1;}
drha34001c2007-02-02 12:44:37 +00001414
drh348784e2000-05-29 20:41:49 +00001415
drh8aff1012001-12-22 14:49:24 +00001416///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +00001417//
drh4d91a702006-01-04 15:54:36 +00001418cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
drh982cef72000-05-30 16:27:03 +00001419
drh382c0242001-10-06 16:33:02 +00001420///////////////////////////// The VACUUM command /////////////////////////////
1421//
drhc601f102020-07-03 17:24:35 +00001422%if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH
drh2f6239e2018-12-08 00:43:08 +00001423%type vinto {Expr*}
1424%destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
1425cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);}
1426cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);}
1427vinto(A) ::= INTO expr(X). {A = X;}
1428vinto(A) ::= . {A = 0;}
drhc601f102020-07-03 17:24:35 +00001429%endif
drhf57b14a2001-09-14 18:54:08 +00001430
drh382c0242001-10-06 16:33:02 +00001431///////////////////////////// The PRAGMA command /////////////////////////////
1432//
drh13d70422004-11-13 15:59:14 +00001433%ifndef SQLITE_OMIT_PRAGMA
drhada2ee02009-04-03 01:43:57 +00001434cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1435cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drha3eb4b42007-01-27 02:38:29 +00001436cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drhada2ee02009-04-03 01:43:57 +00001437cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1438 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1439cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1440 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1441
drhcf82f0d2016-02-17 04:33:10 +00001442nmnum(A) ::= plus_num(A).
1443nmnum(A) ::= nm(A).
1444nmnum(A) ::= ON(A).
1445nmnum(A) ::= DELETE(A).
1446nmnum(A) ::= DEFAULT(A).
drh154d4b22006-09-21 11:02:16 +00001447%endif SQLITE_OMIT_PRAGMA
drhf59b12f2014-01-11 03:54:05 +00001448%token_class number INTEGER|FLOAT.
drh8395b7b2012-01-28 19:44:22 +00001449plus_num(A) ::= PLUS number(X). {A = X;}
drhcf82f0d2016-02-17 04:33:10 +00001450plus_num(A) ::= number(A).
drhf57b14a2001-09-14 18:54:08 +00001451minus_num(A) ::= MINUS number(X). {A = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +00001452//////////////////////////// The CREATE TRIGGER command /////////////////////
drhf0f258b2003-04-21 18:48:45 +00001453
drhb7f91642004-10-31 02:22:47 +00001454%ifndef SQLITE_OMIT_TRIGGER
1455
drhd9da78a2009-03-24 15:08:09 +00001456cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
drh4b59ab52002-08-24 18:24:51 +00001457 Token all;
1458 all.z = A.z;
drhb27b7f52008-12-10 18:03:45 +00001459 all.n = (int)(Z.z - A.z) + Z.n;
danielk19774adee202004-05-08 08:23:19 +00001460 sqlite3FinishTrigger(pParse, S, &all);
drhf0f258b2003-04-21 18:48:45 +00001461}
1462
drhfdd48a72006-09-11 23:45:48 +00001463trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1464 trigger_time(C) trigger_event(D)
drh60218d22007-04-06 11:26:00 +00001465 ON fullname(E) foreach_clause when_clause(G). {
1466 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
drhcf82f0d2016-02-17 04:33:10 +00001467 A = (Z.n==0?B:Z); /*A-overwrites-T*/
danielk1977c3f9bad2002-05-15 08:30:12 +00001468}
1469
drhc4dd3fd2008-01-22 01:48:05 +00001470%type trigger_time {int}
drh6559e2c2017-06-28 14:26:37 +00001471trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
danielk1977c3f9bad2002-05-15 08:30:12 +00001472trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1473trigger_time(A) ::= . { A = TK_BEFORE; }
1474
drhad3cab52002-05-24 02:04:32 +00001475%type trigger_event {struct TrigEvent}
drh633e6d52008-07-28 19:34:53 +00001476%destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
drhcf82f0d2016-02-17 04:33:10 +00001477trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1478trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1479trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +00001480
drh60218d22007-04-06 11:26:00 +00001481foreach_clause ::= .
1482foreach_clause ::= FOR EACH ROW.
danielk1977c3f9bad2002-05-15 08:30:12 +00001483
drh0bb132b2004-07-20 14:06:51 +00001484%type when_clause {Expr*}
drh633e6d52008-07-28 19:34:53 +00001485%destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001486when_clause(A) ::= . { A = 0; }
drh1be266b2017-12-24 00:18:47 +00001487when_clause(A) ::= WHEN expr(X). { A = X; }
danielk1977c3f9bad2002-05-15 08:30:12 +00001488
drh0bb132b2004-07-20 14:06:51 +00001489%type trigger_cmd_list {TriggerStep*}
drh633e6d52008-07-28 19:34:53 +00001490%destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +00001491trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1492 assert( A!=0 );
1493 A->pLast->pNext = X;
1494 A->pLast = X;
drha69d9162003-04-17 22:57:53 +00001495}
drh4dd0d3f2016-02-17 01:18:33 +00001496trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1497 assert( A!=0 );
1498 A->pLast = A;
drh81238962008-08-11 14:26:35 +00001499}
danielk1977c3f9bad2002-05-15 08:30:12 +00001500
drhb1819a02009-07-03 15:37:27 +00001501// Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1502// within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1503// the same database as the table that the trigger fires on.
1504//
1505%type trnm {Token}
drh4dd0d3f2016-02-17 01:18:33 +00001506trnm(A) ::= nm(A).
drhb1819a02009-07-03 15:37:27 +00001507trnm(A) ::= nm DOT nm(X). {
1508 A = X;
1509 sqlite3ErrorMsg(pParse,
1510 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1511 "statements within triggers");
1512}
1513
1514// Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1515// statements within triggers. We make a specific error message for this
1516// since it is an exception to the default grammar rules.
1517//
1518tridxby ::= .
1519tridxby ::= INDEXED BY nm. {
1520 sqlite3ErrorMsg(pParse,
1521 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1522 "within triggers");
1523}
1524tridxby ::= NOT INDEXED. {
1525 sqlite3ErrorMsg(pParse,
1526 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1527 "within triggers");
1528}
1529
1530
1531
drh0bb132b2004-07-20 14:06:51 +00001532%type trigger_cmd {TriggerStep*}
drh633e6d52008-07-28 19:34:53 +00001533%destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001534// UPDATE
drhb1819a02009-07-03 15:37:27 +00001535trigger_cmd(A) ::=
dane7877b22020-07-14 19:51:01 +00001536 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E).
1537 {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001538
1539// INSERT
drhf259df52017-12-27 20:38:35 +00001540trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
drh2c2e8442018-04-07 15:04:05 +00001541 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
dan5be60c52018-08-15 20:28:39 +00001542 A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
drh2c2e8442018-04-07 15:04:05 +00001543}
danielk1977c3f9bad2002-05-15 08:30:12 +00001544// DELETE
drhf259df52017-12-27 20:38:35 +00001545trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
dan5be60c52018-08-15 20:28:39 +00001546 {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001547
1548// SELECT
drhf259df52017-12-27 20:38:35 +00001549trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1550 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
danielk1977c3f9bad2002-05-15 08:30:12 +00001551
danielk19776f349032002-06-11 02:25:40 +00001552// The special RAISE expression that may occur in trigger programs
drh1be266b2017-12-24 00:18:47 +00001553expr(A) ::= RAISE LP IGNORE RP. {
1554 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1555 if( A ){
drh11949042019-08-05 18:01:42 +00001556 A->affExpr = OE_Ignore;
drh8aa34ae2006-03-13 12:54:09 +00001557 }
drh4b59ab52002-08-24 18:24:51 +00001558}
drh1be266b2017-12-24 00:18:47 +00001559expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
1560 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
1561 if( A ) {
drh11949042019-08-05 18:01:42 +00001562 A->affExpr = (char)T;
drh8aa34ae2006-03-13 12:54:09 +00001563 }
drh4b59ab52002-08-24 18:24:51 +00001564}
drh154d4b22006-09-21 11:02:16 +00001565%endif !SQLITE_OMIT_TRIGGER
drhb7f91642004-10-31 02:22:47 +00001566
drh74ad7fe2004-10-07 03:06:28 +00001567%type raisetype {int}
1568raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1569raisetype(A) ::= ABORT. {A = OE_Abort;}
1570raisetype(A) ::= FAIL. {A = OE_Fail;}
1571
danielk19776f349032002-06-11 02:25:40 +00001572
danielk1977c3f9bad2002-05-15 08:30:12 +00001573//////////////////////// DROP TRIGGER statement //////////////////////////////
drhb7f91642004-10-31 02:22:47 +00001574%ifndef SQLITE_OMIT_TRIGGER
drhfdd48a72006-09-11 23:45:48 +00001575cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1576 sqlite3DropTrigger(pParse,X,NOERR);
danielk1977c3f9bad2002-05-15 08:30:12 +00001577}
drh154d4b22006-09-21 11:02:16 +00001578%endif !SQLITE_OMIT_TRIGGER
drh113088e2003-03-20 01:16:58 +00001579
1580//////////////////////// ATTACH DATABASE file AS name /////////////////////////
drhfdbcdee2007-03-27 14:44:50 +00001581%ifndef SQLITE_OMIT_ATTACH
danielk1977f744bb52005-12-06 17:19:11 +00001582cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
drh1be266b2017-12-24 00:18:47 +00001583 sqlite3Attach(pParse, F, D, K);
drh1c2d8412003-03-31 00:30:47 +00001584}
drhfdbcdee2007-03-27 14:44:50 +00001585cmd ::= DETACH database_kw_opt expr(D). {
drh1be266b2017-12-24 00:18:47 +00001586 sqlite3Detach(pParse, D);
drhfdbcdee2007-03-27 14:44:50 +00001587}
1588
drhc4dd3fd2008-01-22 01:48:05 +00001589%type key_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +00001590%destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
danielk1977f744bb52005-12-06 17:19:11 +00001591key_opt(A) ::= . { A = 0; }
drh1be266b2017-12-24 00:18:47 +00001592key_opt(A) ::= KEY expr(X). { A = X; }
drh113088e2003-03-20 01:16:58 +00001593
1594database_kw_opt ::= DATABASE.
1595database_kw_opt ::= .
drhfdbcdee2007-03-27 14:44:50 +00001596%endif SQLITE_OMIT_ATTACH
drh4343fea2004-11-05 23:46:15 +00001597
1598////////////////////////// REINDEX collation //////////////////////////////////
1599%ifndef SQLITE_OMIT_REINDEX
1600cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1601cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
drh154d4b22006-09-21 11:02:16 +00001602%endif SQLITE_OMIT_REINDEX
danielk19779fd2a9a2004-11-12 13:42:30 +00001603
drh9f18e8a2005-07-08 12:13:04 +00001604/////////////////////////////////// ANALYZE ///////////////////////////////////
1605%ifndef SQLITE_OMIT_ANALYZE
1606cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1607cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1608%endif
1609
danielk19779fd2a9a2004-11-12 13:42:30 +00001610//////////////////////// ALTER TABLE table ... ////////////////////////////////
1611%ifndef SQLITE_OMIT_ALTERTABLE
1612cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1613 sqlite3AlterRenameTable(pParse,X,&Z);
1614}
drh986dde72016-02-29 13:37:21 +00001615cmd ::= ALTER TABLE add_column_fullname
1616 ADD kwcolumn_opt columnname(Y) carglist. {
1617 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
danielk197719a8e7e2005-03-17 05:03:38 +00001618 sqlite3AlterFinishAddColumn(pParse, &Y);
1619}
drh329cb9e2021-02-19 09:36:50 +00001620cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). {
dan6e6d9832021-02-16 20:43:36 +00001621 sqlite3AlterDropColumn(pParse, X, &Y);
1622}
1623
danielk197719a8e7e2005-03-17 05:03:38 +00001624add_column_fullname ::= fullname(X). {
drh4a642b62016-02-05 01:55:27 +00001625 disableLookaside(pParse);
danielk197719a8e7e2005-03-17 05:03:38 +00001626 sqlite3AlterBeginAddColumn(pParse, X);
1627}
dancf8f2892018-08-09 20:47:01 +00001628cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
1629 sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
1630}
1631
danielk197719a8e7e2005-03-17 05:03:38 +00001632kwcolumn_opt ::= .
1633kwcolumn_opt ::= COLUMNKW.
dancf8f2892018-08-09 20:47:01 +00001634
drh154d4b22006-09-21 11:02:16 +00001635%endif SQLITE_OMIT_ALTERTABLE
drhe09daa92006-06-10 13:29:31 +00001636
1637//////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1638%ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001639cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1640cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
drhb421b892012-01-28 19:41:53 +00001641create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1642 nm(X) dbnm(Y) USING nm(Z). {
1643 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
drhb9bb7c12006-06-11 23:41:55 +00001644}
drhe09daa92006-06-10 13:29:31 +00001645vtabarglist ::= vtabarg.
1646vtabarglist ::= vtabarglist COMMA vtabarg.
drhb9bb7c12006-06-11 23:41:55 +00001647vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1648vtabarg ::= vtabarg vtabargtoken.
1649vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1650vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1651lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1652anylist ::= .
drhaaac8b42009-05-11 18:22:30 +00001653anylist ::= anylist LP anylist RP.
1654anylist ::= anylist ANY.
drh154d4b22006-09-21 11:02:16 +00001655%endif SQLITE_OMIT_VIRTUALTABLE
drh8b471862014-01-11 13:22:17 +00001656
1657
1658//////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
dan7d562db2014-01-11 19:19:36 +00001659%type wqlist {With*}
dan4e9119d2014-01-13 15:12:23 +00001660%destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
drhf824b412021-02-20 14:57:16 +00001661%type wqitem {Cte*}
1662// %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable
dan7d562db2014-01-11 19:19:36 +00001663
drha5746e02018-04-09 20:36:09 +00001664with ::= .
drh8b471862014-01-11 13:22:17 +00001665%ifndef SQLITE_OMIT_CTE
drha5746e02018-04-09 20:36:09 +00001666with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1667with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
dan7d562db2014-01-11 19:19:36 +00001668
drhf824b412021-02-20 14:57:16 +00001669wqitem(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
1670 A = sqlite3CteNew(pParse, &X, Y, Z); /*A-overwrites-X*/
dan7d562db2014-01-11 19:19:36 +00001671}
drhf824b412021-02-20 14:57:16 +00001672wqlist(A) ::= wqitem(X). {
1673 A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/
1674}
1675wqlist(A) ::= wqlist(A) COMMA wqitem(X). {
1676 A = sqlite3WithAdd(pParse, A, X);
drh8b471862014-01-11 13:22:17 +00001677}
1678%endif SQLITE_OMIT_CTE
dan34a7d792018-06-29 20:43:33 +00001679
1680//////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
dan6e2210e2018-06-30 18:54:56 +00001681// These must be at the end of this file. Specifically, the rules that
1682// introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1683// the integer values assigned to these tokens to be larger than all other
1684// tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
dan34a7d792018-06-29 20:43:33 +00001685//
1686%ifndef SQLITE_OMIT_WINDOWFUNC
1687%type windowdefn_list {Window*}
drha57aac22018-07-09 16:24:00 +00001688%destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
dan34a7d792018-06-29 20:43:33 +00001689windowdefn_list(A) ::= windowdefn(Z). { A = Z; }
1690windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
drha57aac22018-07-09 16:24:00 +00001691 assert( Z!=0 );
dane7c9ca42019-02-16 17:27:51 +00001692 sqlite3WindowChain(pParse, Z, Y);
drha57aac22018-07-09 16:24:00 +00001693 Z->pNextWin = Y;
dan34a7d792018-06-29 20:43:33 +00001694 A = Z;
1695}
1696
1697%type windowdefn {Window*}
1698%destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
dane7c9ca42019-02-16 17:27:51 +00001699windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
drha57aac22018-07-09 16:24:00 +00001700 if( ALWAYS(Y) ){
dan34a7d792018-06-29 20:43:33 +00001701 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1702 }
1703 A = Y;
1704}
1705
dan34a7d792018-06-29 20:43:33 +00001706%type window {Window*}
1707%destructor window {sqlite3WindowDelete(pParse->db, $$);}
1708
1709%type frame_opt {Window*}
1710%destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1711
dan34a7d792018-06-29 20:43:33 +00001712%type part_opt {ExprList*}
1713%destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1714
dan64882712019-07-11 18:43:33 +00001715%type filter_clause {Expr*}
1716%destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
1717
1718%type over_clause {Window*}
1719%destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
dan34a7d792018-06-29 20:43:33 +00001720
dan4f9adee2019-07-13 16:22:50 +00001721%type filter_over {Window*}
1722%destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
1723
dan34a7d792018-06-29 20:43:33 +00001724%type range_or_rows {int}
1725
1726%type frame_bound {struct FrameBound}
1727%destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
dan287fa172018-07-06 13:48:09 +00001728%type frame_bound_s {struct FrameBound}
1729%destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1730%type frame_bound_e {struct FrameBound}
1731%destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
dan34a7d792018-06-29 20:43:33 +00001732
dane7c9ca42019-02-16 17:27:51 +00001733window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1734 A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
1735}
1736window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1737 A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
1738}
1739window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
1740 A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
1741}
1742window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
1743 A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
1744}
1745window(A) ::= frame_opt(Z). {
dan34a7d792018-06-29 20:43:33 +00001746 A = Z;
dane7c9ca42019-02-16 17:27:51 +00001747}
1748window(A) ::= nm(W) frame_opt(Z). {
1749 A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
dan34a7d792018-06-29 20:43:33 +00001750}
1751
dan34a7d792018-06-29 20:43:33 +00001752frame_opt(A) ::= . {
dand35300f2019-03-14 20:53:21 +00001753 A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
dan34a7d792018-06-29 20:43:33 +00001754}
dand35300f2019-03-14 20:53:21 +00001755frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). {
1756 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
dan34a7d792018-06-29 20:43:33 +00001757}
drh0f134f02019-04-02 18:12:20 +00001758frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
1759 frame_bound_e(Z) frame_exclude_opt(W). {
dand35300f2019-03-14 20:53:21 +00001760 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
dan34a7d792018-06-29 20:43:33 +00001761}
1762
drh0f134f02019-04-02 18:12:20 +00001763range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/}
dan34a7d792018-06-29 20:43:33 +00001764
drh0f134f02019-04-02 18:12:20 +00001765frame_bound_s(A) ::= frame_bound(X). {A = X;}
1766frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
1767frame_bound_e(A) ::= frame_bound(X). {A = X;}
1768frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
dan287fa172018-07-06 13:48:09 +00001769
drh0f134f02019-04-02 18:12:20 +00001770frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
1771 {A.eType = @Y; A.pExpr = X;}
1772frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;}
dan34a7d792018-06-29 20:43:33 +00001773
dand35300f2019-03-14 20:53:21 +00001774%type frame_exclude_opt {u8}
drh0f134f02019-04-02 18:12:20 +00001775frame_exclude_opt(A) ::= . {A = 0;}
1776frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
dand35300f2019-03-14 20:53:21 +00001777
1778%type frame_exclude {u8}
drh0f134f02019-04-02 18:12:20 +00001779frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/}
1780frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
1781frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/}
dand35300f2019-03-14 20:53:21 +00001782
1783
drh550a3302018-07-27 22:14:50 +00001784%type window_clause {Window*}
1785%destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
1786window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
dan6e2210e2018-06-30 18:54:56 +00001787
dan4f9adee2019-07-13 16:22:50 +00001788filter_over(A) ::= filter_clause(F) over_clause(O). {
1789 O->pFilter = F;
1790 A = O;
dan64882712019-07-11 18:43:33 +00001791}
dan4f9adee2019-07-13 16:22:50 +00001792filter_over(A) ::= over_clause(O). {
1793 A = O;
dan64882712019-07-11 18:43:33 +00001794}
dan4f9adee2019-07-13 16:22:50 +00001795filter_over(A) ::= filter_clause(F). {
1796 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1797 if( A ){
1798 A->eFrmType = TK_FILTER;
1799 A->pFilter = F;
dan00885742019-07-13 18:27:54 +00001800 }else{
1801 sqlite3ExprDelete(pParse->db, F);
dan4f9adee2019-07-13 16:22:50 +00001802 }
dan64882712019-07-11 18:43:33 +00001803}
1804
1805over_clause(A) ::= OVER LP window(Z) RP. {
dan6e2210e2018-06-30 18:54:56 +00001806 A = Z;
drha57aac22018-07-09 16:24:00 +00001807 assert( A!=0 );
drha57aac22018-07-09 16:24:00 +00001808}
dan64882712019-07-11 18:43:33 +00001809over_clause(A) ::= OVER nm(Z). {
drha57aac22018-07-09 16:24:00 +00001810 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1811 if( A ){
1812 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
drha57aac22018-07-09 16:24:00 +00001813 }
dan6e2210e2018-06-30 18:54:56 +00001814}
1815
dan64882712019-07-11 18:43:33 +00001816filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
drhc7bf5712018-07-09 22:49:01 +00001817%endif /* SQLITE_OMIT_WINDOWFUNC */
drhf1722ba2019-04-05 20:56:46 +00001818
1819/*
1820** The code generator needs some extra TK_ token values for tokens that
1821** are synthesized and do not actually appear in the grammar:
1822*/
1823%token
drhf1722ba2019-04-05 20:56:46 +00001824 COLUMN /* Reference to a table column */
1825 AGG_FUNCTION /* An aggregate function */
1826 AGG_COLUMN /* An aggregated column */
danee6c5e52019-08-23 20:33:01 +00001827 TRUEFALSE /* True or false keyword */
1828 ISNOT /* Combination of IS and NOT */
1829 FUNCTION /* A function invocation */
drhf1722ba2019-04-05 20:56:46 +00001830 UMINUS /* Unary minus */
1831 UPLUS /* Unary plus */
1832 TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
1833 REGISTER /* Reference to a VDBE register */
1834 VECTOR /* Vector */
1835 SELECT_COLUMN /* Choose a single column from a multi-column SELECT */
1836 IF_NULL_ROW /* the if-null-row operator */
1837 ASTERISK /* The "*" in count(*) and similar */
1838 SPAN /* The span operator */
1839.
1840/* There must be no more than 255 tokens defined above. If this grammar
1841** is extended with new rules and tokens, they must either be so few in
1842** number that TK_SPAN is no more than 255, or else the new tokens must
1843** appear after this line.
1844*/
1845%include {
1846#if TK_SPAN>255
1847# error too many tokens in the grammar
1848#endif
1849}
1850
1851/*
1852** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens. The
1853** parser depends on this. Those tokens are not used in any grammar rule.
1854** They are only used by the tokenizer. Declare them last so that they
1855** are guaranteed to be the last two tokens
1856*/
1857%token SPACE ILLEGAL.