blob: d627f22ba21fd416a2e59d62c12f87c7fad3f485 [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
drhc6458712021-04-28 17:37:26 +0000196temp(A) ::= TEMP. {A = pParse->db->init.busy==0;}
drh154d4b22006-09-21 11:02:16 +0000197%endif SQLITE_OMIT_TEMPDB
drhd24cc422003-03-27 12:51:24 +0000198temp(A) ::= . {A = 0;}
drh44183f82021-08-18 13:13:58 +0000199create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_option_set(F). {
drh5969da42013-10-21 02:14:45 +0000200 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}
drh44183f82021-08-18 13:13:58 +0000206%type table_option_set {u32}
207%type table_option {u32}
208table_option_set(A) ::= . {A = 0;}
209table_option_set(A) ::= table_option(A).
210table_option_set(A) ::= table_option_set(X) COMMA table_option(Y). {A = X|Y;}
211table_option(A) ::= WITHOUT nm(X). {
drh5969da42013-10-21 02:14:45 +0000212 if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
drhfccda8a2015-05-27 13:06:55 +0000213 A = TF_WithoutRowid | TF_NoVisibleRowid;
drh5969da42013-10-21 02:14:45 +0000214 }else{
215 A = 0;
216 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
217 }
218}
drh44183f82021-08-18 13:13:58 +0000219table_option(A) ::= nm(X). {
220 if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){
221 A = TF_Strict;
222 }else{
223 A = 0;
224 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
225 }
226}
drh986dde72016-02-29 13:37:21 +0000227columnlist ::= columnlist COMMA columnname carglist.
228columnlist ::= columnname carglist.
drh77441fa2021-07-30 18:39:59 +0000229columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);}
drhc4a3c772001-04-04 11:48:57 +0000230
drh6a8700b2017-08-02 11:04:00 +0000231// Declare some tokens early in order to influence their values, to
232// improve performance and reduce the executable size. The goal here is
233// to get the "jump" operations in ISNULL through ESCAPE to have numeric
234// values that are early enough so that all jump operations are clustered
drh7bbdc3c2019-04-05 21:17:11 +0000235// at the beginning.
drh6a8700b2017-08-02 11:04:00 +0000236//
237%token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
238%token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
drhd5326c32022-01-07 14:58:47 +0000239%token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
drh6a8700b2017-08-02 11:04:00 +0000240%token GT LE LT GE ESCAPE.
241
drh31d6fd52017-04-14 19:03:10 +0000242// The following directive causes tokens ABORT, AFTER, ASC, etc. to
243// fallback to ID if they will not parse as their original value.
244// This obviates the need for the "id" nonterminal.
245//
246%fallback ID
247 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
drh0a6259f2018-04-16 13:26:53 +0000248 CONFLICT DATABASE DEFERRED DESC DETACH DO
drh6cd7d482018-04-12 15:43:05 +0000249 EACH END EXCLUSIVE EXPLAIN FAIL FOR
drh31d6fd52017-04-14 19:03:10 +0000250 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
dan86fb6e12018-05-16 20:58:07 +0000251 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
drh31d6fd52017-04-14 19:03:10 +0000252 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
dan6e118922019-08-12 16:36:38 +0000253 NULLS FIRST LAST
drh31d6fd52017-04-14 19:03:10 +0000254%ifdef SQLITE_OMIT_COMPOUND_SELECT
255 EXCEPT INTERSECT UNION
256%endif SQLITE_OMIT_COMPOUND_SELECT
drh3773c252018-06-28 03:38:49 +0000257%ifndef SQLITE_OMIT_WINDOWFUNC
dan6e2210e2018-06-30 18:54:56 +0000258 CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED
danced89122019-03-19 06:40:29 +0000259 EXCLUDE GROUPS OTHERS TIES
drh3773c252018-06-28 03:38:49 +0000260%endif SQLITE_OMIT_WINDOWFUNC
drh9ee94142019-10-30 13:00:23 +0000261%ifndef SQLITE_OMIT_GENERATED_COLUMNS
drh089c4bc2019-11-02 13:45:03 +0000262 GENERATED ALWAYS
drh9ee94142019-10-30 13:00:23 +0000263%endif
drh745912e2021-02-22 03:04:25 +0000264 MATERIALIZED
drh31d6fd52017-04-14 19:03:10 +0000265 REINDEX RENAME CTIME_KW IF
266 .
267%wildcard ANY.
268
drhf7b54962013-05-28 12:11:54 +0000269// Define operator precedence early so that this is the first occurrence
drh2d3917d2004-02-22 16:27:00 +0000270// of the operator tokens in the grammer. Keeping the operators together
271// causes them to be assigned integer values that are close together,
272// which keeps parser tables smaller.
273//
drhf2bc0132004-10-04 13:19:23 +0000274// The token values assigned to these symbols is determined by the order
275// in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
276// NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
277// the sqlite3ExprIfFalse() routine for additional information on this
278// constraint.
279//
drh2d3917d2004-02-22 16:27:00 +0000280%left OR.
281%left AND.
282%right NOT.
drh03bea702006-06-13 15:37:26 +0000283%left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
drh9a432672004-10-04 13:38:09 +0000284%left GT LE LT GE.
danielk19777c6303c2004-11-17 16:41:29 +0000285%right ESCAPE.
drh2d3917d2004-02-22 16:27:00 +0000286%left BITAND BITOR LSHIFT RSHIFT.
287%left PLUS MINUS.
288%left STAR SLASH REM.
drhd5326c32022-01-07 14:58:47 +0000289%left CONCAT PTR.
drha34001c2007-02-02 12:44:37 +0000290%left COLLATE.
drh7ba5bc52009-09-22 20:08:34 +0000291%right BITNOT.
drh26cf56f2018-04-06 19:36:49 +0000292%nonassoc ON.
drh2d3917d2004-02-22 16:27:00 +0000293
drh7cc84c22016-04-11 13:36:42 +0000294// An IDENTIFIER can be a generic identifier, or one of several
295// keywords. Any non-standard keyword can also be an identifier.
296//
297%token_class id ID|INDEXED.
298
drh7cc84c22016-04-11 13:36:42 +0000299
drhc4a3c772001-04-04 11:48:57 +0000300// And "ids" is an identifer-or-string.
301//
dan59ff4252018-06-29 17:44:52 +0000302%token_class ids ID|STRING.
drhc4a3c772001-04-04 11:48:57 +0000303
drh5ad1a6c2002-07-01 12:27:09 +0000304// The name of a column or table can be any of the following:
305//
306%type nm {Token}
drh4dd0d3f2016-02-17 01:18:33 +0000307nm(A) ::= id(A).
308nm(A) ::= STRING(A).
309nm(A) ::= JOIN_KW(A).
drh5ad1a6c2002-07-01 12:27:09 +0000310
drh986dde72016-02-29 13:37:21 +0000311// A typetoken is really zero or more tokens that form a type name such
drh487e2622005-06-25 18:42:14 +0000312// as can be found after the column name in a CREATE TABLE statement.
313// Multiple tokens are concatenated to form the value of the typetoken.
314//
315%type typetoken {Token}
drh986dde72016-02-29 13:37:21 +0000316typetoken(A) ::= . {A.n = 0; A.z = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000317typetoken(A) ::= typename(A).
318typetoken(A) ::= typename(A) LP signed RP(Y). {
319 A.n = (int)(&Y.z[Y.n] - A.z);
drh487e2622005-06-25 18:42:14 +0000320}
drh4dd0d3f2016-02-17 01:18:33 +0000321typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
322 A.n = (int)(&Y.z[Y.n] - A.z);
drh487e2622005-06-25 18:42:14 +0000323}
drh382c0242001-10-06 16:33:02 +0000324%type typename {Token}
dan59ff4252018-06-29 17:44:52 +0000325typename(A) ::= ids(A).
326typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
drh60218d22007-04-06 11:26:00 +0000327signed ::= plus_num.
328signed ::= minus_num.
drh487e2622005-06-25 18:42:14 +0000329
drh1be266b2017-12-24 00:18:47 +0000330// The scanpt non-terminal takes a value which is a pointer to the
331// input text just past the last token that has been shifted into
332// the parser. By surrounding some phrase in the grammar with two
333// scanpt non-terminals, we can capture the input text for that phrase.
334// For example:
335//
336// something ::= .... scanpt(A) phrase scanpt(Z).
337//
338// The text that is parsed as "phrase" is a string starting at A
339// and containing (int)(Z-A) characters. There might be some extra
340// whitespace on either end of the text, but that can be removed in
341// post-processing, if needed.
342//
343%type scanpt {const char*}
344scanpt(A) ::= . {
drhf259df52017-12-27 20:38:35 +0000345 assert( yyLookahead!=YYNOCODE );
346 A = yyLookaheadToken.z;
drh1be266b2017-12-24 00:18:47 +0000347}
dan4db4b5b2019-05-18 19:49:08 +0000348scantok(A) ::= . {
349 assert( yyLookahead!=YYNOCODE );
350 A = yyLookaheadToken;
351}
drh1be266b2017-12-24 00:18:47 +0000352
drh487e2622005-06-25 18:42:14 +0000353// "carglist" is a list of additional constraints that come after the
354// column name and column type in a CREATE TABLE statement.
355//
drh4dc330d2012-05-07 19:21:36 +0000356carglist ::= carglist ccons.
drh348784e2000-05-29 20:41:49 +0000357carglist ::= .
drh4dc330d2012-05-07 19:21:36 +0000358ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
dan4db4b5b2019-05-18 19:49:08 +0000359ccons ::= DEFAULT scantok(A) term(X).
360 {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);}
drh1be266b2017-12-24 00:18:47 +0000361ccons ::= DEFAULT LP(A) expr(X) RP(Z).
362 {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
dan4db4b5b2019-05-18 19:49:08 +0000363ccons ::= DEFAULT PLUS(A) scantok(Z) term(X).
364 {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);}
365ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). {
drh1be266b2017-12-24 00:18:47 +0000366 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
dan4db4b5b2019-05-18 19:49:08 +0000367 sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]);
danielk19777977a172004-11-09 12:44:37 +0000368}
dan4db4b5b2019-05-18 19:49:08 +0000369ccons ::= DEFAULT scantok id(X). {
drh1be266b2017-12-24 00:18:47 +0000370 Expr *p = tokenExpr(pParse, TK_STRING, X);
drhd7fd8992018-02-28 04:30:55 +0000371 if( p ){
372 sqlite3ExprIdToTrueFalse(p);
373 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
374 }
danc9461ec2018-08-29 21:00:16 +0000375 sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
danielk19777977a172004-11-09 12:44:37 +0000376}
drh348784e2000-05-29 20:41:49 +0000377
drh382c0242001-10-06 16:33:02 +0000378// In addition to the type name, we also care about the primary key and
379// UNIQUE constraints.
drh348784e2000-05-29 20:41:49 +0000380//
drh0d316a42002-08-11 20:10:47 +0000381ccons ::= NULL onconf.
drhb7916a72009-05-27 10:31:29 +0000382ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
drhfdd6e852005-12-16 01:06:16 +0000383ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
drhb7916a72009-05-27 10:31:29 +0000384 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
drh62340f82016-05-31 21:18:15 +0000385ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
386 SQLITE_IDXTYPE_UNIQUE);}
drh92e21ef2020-08-27 18:36:30 +0000387ccons ::= CHECK LP(A) expr(X) RP(B). {sqlite3AddCheckConstraint(pParse,X,A.z,B.z);}
drh108aa002015-08-24 20:21:20 +0000388ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
drhb7916a72009-05-27 10:31:29 +0000389 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
390ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);}
dan59ff4252018-06-29 17:44:52 +0000391ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);}
drh81f7b372019-10-16 12:18:59 +0000392ccons ::= GENERATED ALWAYS AS generated.
393ccons ::= AS generated.
drh7e508f12019-10-16 19:31:46 +0000394generated ::= LP expr(E) RP. {sqlite3AddGenerated(pParse,E,0);}
395generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);}
drh04738cb2002-06-02 18:19:00 +0000396
drh205f48e2004-11-05 00:43:11 +0000397// The optional AUTOINCREMENT keyword
398%type autoinc {int}
drh2958a4e2004-11-12 03:56:15 +0000399autoinc(X) ::= . {X = 0;}
400autoinc(X) ::= AUTOINCR. {X = 1;}
drh205f48e2004-11-05 00:43:11 +0000401
drhc2eef3b2002-08-31 18:53:06 +0000402// The next group of rules parses the arguments to a REFERENCES clause
403// that determine if the referential integrity checking is deferred or
404// or immediate and which determine what action to take if a ref-integ
405// check fails.
drh04738cb2002-06-02 18:19:00 +0000406//
drhc2eef3b2002-08-31 18:53:06 +0000407%type refargs {int}
drhfcf486c2009-10-21 13:48:24 +0000408refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */}
drh4dd0d3f2016-02-17 01:18:33 +0000409refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
drhc2eef3b2002-08-31 18:53:06 +0000410%type refarg {struct {int value; int mask;}}
411refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; }
drhc29c5aa12009-12-09 21:43:36 +0000412refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; }
drhc2eef3b2002-08-31 18:53:06 +0000413refarg(A) ::= ON DELETE refact(X). { A.value = X; A.mask = 0x0000ff; }
414refarg(A) ::= ON UPDATE refact(X). { A.value = X<<8; A.mask = 0x00ff00; }
drhc2eef3b2002-08-31 18:53:06 +0000415%type refact {int}
drhfcf486c2009-10-21 13:48:24 +0000416refact(A) ::= SET NULL. { A = OE_SetNull; /* EV: R-33326-45252 */}
417refact(A) ::= SET DEFAULT. { A = OE_SetDflt; /* EV: R-33326-45252 */}
418refact(A) ::= CASCADE. { A = OE_Cascade; /* EV: R-33326-45252 */}
419refact(A) ::= RESTRICT. { A = OE_Restrict; /* EV: R-33326-45252 */}
420refact(A) ::= NO ACTION. { A = OE_None; /* EV: R-33326-45252 */}
drhc2eef3b2002-08-31 18:53:06 +0000421%type defer_subclause {int}
dan1da40a32009-09-19 17:00:31 +0000422defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt. {A = 0;}
drhc2eef3b2002-08-31 18:53:06 +0000423defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X). {A = X;}
424%type init_deferred_pred_opt {int}
425init_deferred_pred_opt(A) ::= . {A = 0;}
426init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;}
427init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;}
drh348784e2000-05-29 20:41:49 +0000428
drhaeb281c2012-05-08 11:17:33 +0000429conslist_opt(A) ::= . {A.n = 0; A.z = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000430conslist_opt(A) ::= COMMA(A) conslist.
drhab35eae2012-05-12 18:29:53 +0000431conslist ::= conslist tconscomma tcons.
432conslist ::= tcons.
433tconscomma ::= COMMA. {pParse->constraintName.n = 0;}
434tconscomma ::= .
435tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;}
drh108aa002015-08-24 20:21:20 +0000436tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
drhb7916a72009-05-27 10:31:29 +0000437 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
drh108aa002015-08-24 20:21:20 +0000438tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
drh62340f82016-05-31 21:18:15 +0000439 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
440 SQLITE_IDXTYPE_UNIQUE);}
drh92e21ef2020-08-27 18:36:30 +0000441tcons ::= CHECK LP(A) expr(E) RP(B) onconf.
442 {sqlite3AddCheckConstraint(pParse,E,A.z,B.z);}
drh108aa002015-08-24 20:21:20 +0000443tcons ::= FOREIGN KEY LP eidlist(FA) RP
444 REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
danielk19774adee202004-05-08 08:23:19 +0000445 sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
446 sqlite3DeferForeignKey(pParse, D);
drhc2eef3b2002-08-31 18:53:06 +0000447}
448%type defer_subclause_opt {int}
449defer_subclause_opt(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +0000450defer_subclause_opt(A) ::= defer_subclause(A).
drh9cfcf5d2002-01-29 18:41:24 +0000451
452// The following is a non-standard extension that allows us to declare the
453// default behavior when there is a constraint conflict.
454//
455%type onconf {int}
drh3334d082015-11-10 13:45:21 +0000456%type orconf {int}
drh1c928532002-01-31 15:54:21 +0000457%type resolvetype {int}
drh74ad7fe2004-10-07 03:06:28 +0000458onconf(A) ::= . {A = OE_Default;}
459onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;}
460orconf(A) ::= . {A = OE_Default;}
drh3334d082015-11-10 13:45:21 +0000461orconf(A) ::= OR resolvetype(X). {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +0000462resolvetype(A) ::= raisetype(A).
drh74ad7fe2004-10-07 03:06:28 +0000463resolvetype(A) ::= IGNORE. {A = OE_Ignore;}
464resolvetype(A) ::= REPLACE. {A = OE_Replace;}
drh348784e2000-05-29 20:41:49 +0000465
drh382c0242001-10-06 16:33:02 +0000466////////////////////////// The DROP TABLE /////////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000467//
drha0733842005-12-29 01:11:36 +0000468cmd ::= DROP TABLE ifexists(E) fullname(X). {
469 sqlite3DropTable(pParse, X, 0, E);
danielk1977a8858102004-05-28 12:11:21 +0000470}
drha0733842005-12-29 01:11:36 +0000471%type ifexists {int}
472ifexists(A) ::= IF EXISTS. {A = 1;}
473ifexists(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000474
drha76b5df2002-02-23 02:32:10 +0000475///////////////////// The CREATE VIEW statement /////////////////////////////
476//
drhb7f91642004-10-31 02:22:47 +0000477%ifndef SQLITE_OMIT_VIEW
drh108aa002015-08-24 20:21:20 +0000478cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
drh8981b902015-08-24 17:42:49 +0000479 AS select(S). {
480 sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
drha76b5df2002-02-23 02:32:10 +0000481}
drha0733842005-12-29 01:11:36 +0000482cmd ::= DROP VIEW ifexists(E) fullname(X). {
483 sqlite3DropTable(pParse, X, 1, E);
drha76b5df2002-02-23 02:32:10 +0000484}
drh154d4b22006-09-21 11:02:16 +0000485%endif SQLITE_OMIT_VIEW
drha76b5df2002-02-23 02:32:10 +0000486
drh382c0242001-10-06 16:33:02 +0000487//////////////////////// The SELECT statement /////////////////////////////////
drh348784e2000-05-29 20:41:49 +0000488//
dan7d562db2014-01-11 19:19:36 +0000489cmd ::= select(X). {
drha7c74002020-07-18 18:44:59 +0000490 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
drh7d10d5a2008-08-20 16:35:10 +0000491 sqlite3Select(pParse, X, &dest);
drh633e6d52008-07-28 19:34:53 +0000492 sqlite3SelectDelete(pParse->db, X);
drh9bb61fe2000-06-05 16:01:39 +0000493}
drhefb72512000-05-31 20:00:52 +0000494
drh9bb61fe2000-06-05 16:01:39 +0000495%type select {Select*}
drh633e6d52008-07-28 19:34:53 +0000496%destructor select {sqlite3SelectDelete(pParse->db, $$);}
dan7d562db2014-01-11 19:19:36 +0000497%type selectnowith {Select*}
498%destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
drh82c3d632000-06-06 21:56:07 +0000499%type oneselect {Select*}
drh633e6d52008-07-28 19:34:53 +0000500%destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
drh9bb61fe2000-06-05 16:01:39 +0000501
drh772460f2015-04-16 14:13:12 +0000502%include {
503 /*
504 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
505 ** all elements in the list. And make sure list length does not exceed
506 ** SQLITE_LIMIT_COMPOUND_SELECT.
507 */
drhe318a7f2015-04-16 23:04:17 +0000508 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
drh55f66b32019-07-16 19:44:32 +0000509 assert( p!=0 );
drhd227a292014-02-09 18:02:09 +0000510 if( p->pPrior ){
drhaae0f742021-03-04 16:03:32 +0000511 Select *pNext = 0, *pLoop = p;
512 int mxSelect, cnt = 1;
513 while(1){
drhd227a292014-02-09 18:02:09 +0000514 pLoop->pNext = pNext;
515 pLoop->selFlags |= SF_Compound;
drhaae0f742021-03-04 16:03:32 +0000516 pNext = pLoop;
517 pLoop = pLoop->pPrior;
518 if( pLoop==0 ) break;
519 cnt++;
520 if( pLoop->pOrderBy || pLoop->pLimit ){
521 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
522 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
523 sqlite3SelectOpName(pNext->op));
524 break;
525 }
drhd227a292014-02-09 18:02:09 +0000526 }
drh772460f2015-04-16 14:13:12 +0000527 if( (p->selFlags & SF_MultiValue)==0 &&
528 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
529 cnt>mxSelect
drha0c01762015-01-05 16:27:43 +0000530 ){
drhd227a292014-02-09 18:02:09 +0000531 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
532 }
533 }
drh772460f2015-04-16 14:13:12 +0000534 }
drhf824b412021-02-20 14:57:16 +0000535
536 /* Attach a With object describing the WITH clause to a Select
537 ** object describing the query for which the WITH clause is a prefix.
538 */
539 static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){
540 if( pSelect ){
541 pSelect->pWith = pWith;
542 parserDoubleLinkSelect(pParse, pSelect);
543 }else{
544 sqlite3WithDelete(pParse->db, pWith);
545 }
546 return pSelect;
547 }
drh772460f2015-04-16 14:13:12 +0000548}
549
drh6a8b94e2018-05-30 01:14:20 +0000550%ifndef SQLITE_OMIT_CTE
drhf824b412021-02-20 14:57:16 +0000551select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);}
552select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X).
553 {A = attachWithToSelect(pParse,X,W);}
drh6a8b94e2018-05-30 01:14:20 +0000554%endif /* SQLITE_OMIT_CTE */
drha5746e02018-04-09 20:36:09 +0000555select(A) ::= selectnowith(X). {
556 Select *p = X;
557 if( p ){
558 parserDoubleLinkSelect(pParse, p);
559 }
560 A = p; /*A-overwrites-X*/
dana9f5c132014-01-13 16:36:40 +0000561}
dan7d562db2014-01-11 19:19:36 +0000562
drh4dd0d3f2016-02-17 01:18:33 +0000563selectnowith(A) ::= oneselect(A).
drhb7f91642004-10-31 02:22:47 +0000564%ifndef SQLITE_OMIT_COMPOUND_SELECT
drh4dd0d3f2016-02-17 01:18:33 +0000565selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). {
drhc0bf4932014-02-19 01:31:02 +0000566 Select *pRhs = Z;
drh4dd0d3f2016-02-17 01:18:33 +0000567 Select *pLhs = A;
drhc0bf4932014-02-19 01:31:02 +0000568 if( pRhs && pRhs->pPrior ){
569 SrcList *pFrom;
570 Token x;
571 x.n = 0;
drh772460f2015-04-16 14:13:12 +0000572 parserDoubleLinkSelect(pParse, pRhs);
drhd44f8b22022-04-07 01:11:13 +0000573 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0);
drh8c0833f2017-11-14 23:48:23 +0000574 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
drhc0bf4932014-02-19 01:31:02 +0000575 }
576 if( pRhs ){
577 pRhs->op = (u8)Y;
drh00d5ab72015-05-20 00:15:27 +0000578 pRhs->pPrior = pLhs;
579 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
drh772460f2015-04-16 14:13:12 +0000580 pRhs->selFlags &= ~SF_MultiValue;
drhd58d3272013-08-05 22:05:02 +0000581 if( Y!=TK_ALL ) pParse->hasCompound = 1;
drh43b78822007-06-15 17:03:14 +0000582 }else{
drh00d5ab72015-05-20 00:15:27 +0000583 sqlite3SelectDelete(pParse->db, pLhs);
drhdaffd0e2001-04-11 14:28:42 +0000584 }
drhc0bf4932014-02-19 01:31:02 +0000585 A = pRhs;
drh82c3d632000-06-06 21:56:07 +0000586}
drh0a36c572002-02-18 22:49:59 +0000587%type multiselect_op {int}
drhcf82f0d2016-02-17 04:33:10 +0000588multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/}
drhfd405312005-11-06 04:06:59 +0000589multiselect_op(A) ::= UNION ALL. {A = TK_ALL;}
drhcf82f0d2016-02-17 04:33:10 +0000590multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/}
drh154d4b22006-09-21 11:02:16 +0000591%endif SQLITE_OMIT_COMPOUND_SELECT
drh550a3302018-07-27 22:14:50 +0000592
drhfef37762018-07-10 19:48:35 +0000593oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
dan67a9b8e2018-06-22 20:51:35 +0000594 groupby_opt(P) having_opt(Q)
dane3bf6322018-06-08 20:58:27 +0000595 orderby_opt(Z) limit_opt(L). {
drh8c0833f2017-11-14 23:48:23 +0000596 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
drh550a3302018-07-27 22:14:50 +0000597}
598%ifndef SQLITE_OMIT_WINDOWFUNC
599oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
600 groupby_opt(P) having_opt(Q) window_clause(R)
601 orderby_opt(Z) limit_opt(L). {
602 A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
dan6fde1792018-06-15 19:01:35 +0000603 if( A ){
604 A->pWinDefn = R;
605 }else{
606 sqlite3WindowListDelete(pParse->db, R);
607 }
drh9bb61fe2000-06-05 16:01:39 +0000608}
drh550a3302018-07-27 22:14:50 +0000609%endif
610
611
drh4dd0d3f2016-02-17 01:18:33 +0000612oneselect(A) ::= values(A).
drh75593d92014-01-10 20:46:55 +0000613
614%type values {Select*}
615%destructor values {sqlite3SelectDelete(pParse->db, $$);}
616values(A) ::= VALUES LP nexprlist(X) RP. {
drh8c0833f2017-11-14 23:48:23 +0000617 A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
drh75593d92014-01-10 20:46:55 +0000618}
drh954733b2018-07-27 23:33:16 +0000619values(A) ::= values(A) COMMA LP nexprlist(Y) RP. {
drh4dd0d3f2016-02-17 01:18:33 +0000620 Select *pRight, *pLeft = A;
drh8c0833f2017-11-14 23:48:23 +0000621 pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
drhf3151f02015-04-16 20:27:09 +0000622 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
drh75593d92014-01-10 20:46:55 +0000623 if( pRight ){
624 pRight->op = TK_ALL;
drh772460f2015-04-16 14:13:12 +0000625 pRight->pPrior = pLeft;
drh75593d92014-01-10 20:46:55 +0000626 A = pRight;
627 }else{
drh772460f2015-04-16 14:13:12 +0000628 A = pLeft;
drh75593d92014-01-10 20:46:55 +0000629 }
630}
drh9bb61fe2000-06-05 16:01:39 +0000631
632// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
633// present and false (0) if it is not.
634//
drh3334d082015-11-10 13:45:21 +0000635%type distinct {int}
drh832ee3d2012-12-18 19:36:11 +0000636distinct(A) ::= DISTINCT. {A = SF_Distinct;}
drh7cea7f92015-05-29 01:35:19 +0000637distinct(A) ::= ALL. {A = SF_All;}
drhefb72512000-05-31 20:00:52 +0000638distinct(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +0000639
drh9bb61fe2000-06-05 16:01:39 +0000640// selcollist is a list of expressions that are to become the return
drh7c917d12001-12-16 20:05:05 +0000641// values of the SELECT statement. The "*" in statements like
642// "SELECT * FROM ..." is encoded as a special expression with an
drh1a1d3cd2015-11-19 16:33:31 +0000643// opcode of TK_ASTERISK.
drh9bb61fe2000-06-05 16:01:39 +0000644//
drh348784e2000-05-29 20:41:49 +0000645%type selcollist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000646%destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000647%type sclp {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000648%destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +0000649sclp(A) ::= selcollist(A) COMMA.
drh348784e2000-05-29 20:41:49 +0000650sclp(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000651selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). {
652 A = sqlite3ExprListAppend(pParse, A, X);
drhb7916a72009-05-27 10:31:29 +0000653 if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
drh1be266b2017-12-24 00:18:47 +0000654 sqlite3ExprListSetSpan(pParse,A,B,Z);
drh01f3f252002-05-24 16:14:15 +0000655}
drhd3f5d612017-12-24 17:01:54 +0000656selcollist(A) ::= sclp(A) scanpt STAR. {
drh1a1d3cd2015-11-19 16:33:31 +0000657 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
drh4dd0d3f2016-02-17 01:18:33 +0000658 A = sqlite3ExprListAppend(pParse, A, p);
drh7c917d12001-12-16 20:05:05 +0000659}
drh1be266b2017-12-24 00:18:47 +0000660selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
drhabfd35e2016-12-06 22:47:23 +0000661 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
drh796588a2022-02-05 21:49:47 +0000662 Expr *pLeft = tokenExpr(pParse, TK_ID, X);
drhabfd35e2016-12-06 22:47:23 +0000663 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
drh4dd0d3f2016-02-17 01:18:33 +0000664 A = sqlite3ExprListAppend(pParse,A, pDot);
drh54473222002-04-04 02:10:55 +0000665}
drh01f3f252002-05-24 16:14:15 +0000666
667// An option "AS <id>" phrase that can follow one of the expressions that
668// define the result set, or one of the tables in the FROM clause.
669//
670%type as {Token}
drh74ad7fe2004-10-07 03:06:28 +0000671as(X) ::= AS nm(Y). {X = Y;}
drh4dd0d3f2016-02-17 01:18:33 +0000672as(X) ::= ids(X).
drh986dde72016-02-29 13:37:21 +0000673as(X) ::= . {X.n = 0; X.z = 0;}
drh9bb61fe2000-06-05 16:01:39 +0000674
drh348784e2000-05-29 20:41:49 +0000675
drhad3cab52002-05-24 02:04:32 +0000676%type seltablist {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000677%destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
drhad3cab52002-05-24 02:04:32 +0000678%type stl_prefix {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000679%destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
drhad3cab52002-05-24 02:04:32 +0000680%type from {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000681%destructor from {sqlite3SrcListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000682
drh01f3f252002-05-24 16:14:15 +0000683// A complete FROM clause.
684//
dan69887c92020-04-27 20:55:33 +0000685from(A) ::= . {A = 0;}
drhfbdc7f62008-12-03 23:23:40 +0000686from(A) ::= FROM seltablist(X). {
drh61dfc312006-12-16 16:25:15 +0000687 A = X;
drhfdc621a2022-04-16 19:13:16 +0000688 sqlite3SrcListShiftJoinType(pParse,A);
drh61dfc312006-12-16 16:25:15 +0000689}
drh01f3f252002-05-24 16:14:15 +0000690
691// "seltablist" is a "Select Table List" - the content of the FROM clause
692// in a SELECT statement. "stl_prefix" is a prefix of this list.
693//
drh4dd0d3f2016-02-17 01:18:33 +0000694stl_prefix(A) ::= seltablist(A) joinop(Y). {
drh8a48b9c2015-08-19 15:20:00 +0000695 if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
drh01f3f252002-05-24 16:14:15 +0000696}
drh348784e2000-05-29 20:41:49 +0000697stl_prefix(A) ::= . {A = 0;}
drhd44f8b22022-04-07 01:11:13 +0000698seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) on_using(N). {
699 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
drh200adc92022-04-06 19:46:20 +0000700}
drhd44f8b22022-04-07 01:11:13 +0000701seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_by(I) on_using(N). {
702 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
danielk1977b1c685b2008-10-06 16:18:39 +0000703 sqlite3SrcListIndexedBy(pParse, A, &I);
drhc4a3c772001-04-04 11:48:57 +0000704}
drhd44f8b22022-04-07 01:11:13 +0000705seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) on_using(N). {
706 A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
drh01d230c2015-08-19 17:11:37 +0000707 sqlite3SrcListFuncArgs(pParse, A, E);
708}
drh51522cd2005-01-20 13:36:19 +0000709%ifndef SQLITE_OMIT_SUBQUERY
drhd44f8b22022-04-07 01:11:13 +0000710 seltablist(A) ::= stl_prefix(A) LP select(S) RP as(Z) on_using(N). {
711 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,&N);
drhd5feede2002-05-08 21:46:14 +0000712 }
drhd44f8b22022-04-07 01:11:13 +0000713 seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP as(Z) on_using(N). {
714 if( A==0 && Z.n==0 && N.pOn==0 && N.pUsing==0 ){
drhfbdc7f62008-12-03 23:23:40 +0000715 A = F;
drh832ee3d2012-12-18 19:36:11 +0000716 }else if( F->nSrc==1 ){
drhd44f8b22022-04-07 01:11:13 +0000717 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N);
drh832ee3d2012-12-18 19:36:11 +0000718 if( A ){
drh76012942021-02-21 21:04:54 +0000719 SrcItem *pNew = &A->a[A->nSrc-1];
720 SrcItem *pOld = F->a;
drh832ee3d2012-12-18 19:36:11 +0000721 pNew->zName = pOld->zName;
722 pNew->zDatabase = pOld->zDatabase;
drh3c449c62013-04-30 14:06:57 +0000723 pNew->pSelect = pOld->pSelect;
drh825a6bf2022-04-21 14:08:29 +0000724 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
725 pNew->fg.isNestedFrom = 1;
726 }
drh4a5cff72018-12-03 01:47:41 +0000727 if( pOld->fg.isTabFunc ){
728 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
729 pOld->u1.pFuncArg = 0;
730 pOld->fg.isTabFunc = 0;
731 pNew->fg.isTabFunc = 1;
732 }
drh832ee3d2012-12-18 19:36:11 +0000733 pOld->zName = pOld->zDatabase = 0;
drh3c449c62013-04-30 14:06:57 +0000734 pOld->pSelect = 0;
drh832ee3d2012-12-18 19:36:11 +0000735 }
736 sqlite3SrcListDelete(pParse->db, F);
drhfbdc7f62008-12-03 23:23:40 +0000737 }else{
738 Select *pSubquery;
drhfdc621a2022-04-16 19:13:16 +0000739 sqlite3SrcListShiftJoinType(pParse,F);
drh8c0833f2017-11-14 23:48:23 +0000740 pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
drhd44f8b22022-04-07 01:11:13 +0000741 A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N);
drhfbdc7f62008-12-03 23:23:40 +0000742 }
743 }
drh154d4b22006-09-21 11:02:16 +0000744%endif SQLITE_OMIT_SUBQUERY
drhb733d032004-01-24 20:18:12 +0000745
drh113088e2003-03-20 01:16:58 +0000746%type dbnm {Token}
747dbnm(A) ::= . {A.z=0; A.n=0;}
748dbnm(A) ::= DOT nm(X). {A = X;}
749
drh74ad7fe2004-10-07 03:06:28 +0000750%type fullname {SrcList*}
drh633e6d52008-07-28 19:34:53 +0000751%destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
danc9461ec2018-08-29 21:00:16 +0000752fullname(A) ::= nm(X). {
drh29c992c2019-01-17 15:40:41 +0000753 A = sqlite3SrcListAppend(pParse,0,&X,0);
danc9461ec2018-08-29 21:00:16 +0000754 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);
755}
756fullname(A) ::= nm(X) DOT nm(Y). {
drh29c992c2019-01-17 15:40:41 +0000757 A = sqlite3SrcListAppend(pParse,0,&X,&Y);
danc9461ec2018-08-29 21:00:16 +0000758 if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);
759}
drh74ad7fe2004-10-07 03:06:28 +0000760
drh5e3a6eb2018-04-19 11:45:16 +0000761%type xfullname {SrcList*}
762%destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
763xfullname(A) ::= nm(X).
drh29c992c2019-01-17 15:40:41 +0000764 {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/}
drh5e3a6eb2018-04-19 11:45:16 +0000765xfullname(A) ::= nm(X) DOT nm(Y).
drh29c992c2019-01-17 15:40:41 +0000766 {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/}
drh5e3a6eb2018-04-19 11:45:16 +0000767xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
drh29c992c2019-01-17 15:40:41 +0000768 A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/
drh5e3a6eb2018-04-19 11:45:16 +0000769 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
770}
771xfullname(A) ::= nm(X) AS nm(Z). {
drh29c992c2019-01-17 15:40:41 +0000772 A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/
drh5e3a6eb2018-04-19 11:45:16 +0000773 if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
774}
775
drh01f3f252002-05-24 16:14:15 +0000776%type joinop {int}
drhfd405312005-11-06 04:06:59 +0000777joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
drhcf82f0d2016-02-17 04:33:10 +0000778joinop(X) ::= JOIN_KW(A) JOIN.
779 {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/}
780joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
781 {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
drh5ad1a6c2002-07-01 12:27:09 +0000782joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
drhcf82f0d2016-02-17 04:33:10 +0000783 {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
drh01f3f252002-05-24 16:14:15 +0000784
drh26cf56f2018-04-06 19:36:49 +0000785// There is a parsing abiguity in an upsert statement that uses a
786// SELECT on the RHS of a the INSERT:
787//
788// INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
789// here ----^^
790//
791// When the ON token is encountered, the parser does not know if it is
792// the beginning of an ON CONFLICT clause, or the beginning of an ON
793// clause associated with the JOIN. The conflict is resolved in favor
794// of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
795// WHERE clause in between, like this:
796//
797// INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
798//
drhd44f8b22022-04-07 01:11:13 +0000799// The [AND] and [OR] precedence marks in the rules for on_using cause the
drh26cf56f2018-04-06 19:36:49 +0000800// ON in this context to always be interpreted as belonging to the JOIN.
801//
drhd44f8b22022-04-07 01:11:13 +0000802%type on_using {OnOrUsing}
803//%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);}
804on_using(N) ::= ON expr(E). {N.pOn = E; N.pUsing = 0;}
805on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;}
806on_using(N) ::= . [OR] {N.pOn = 0; N.pUsing = 0;}
drh01f3f252002-05-24 16:14:15 +0000807
danielk197785574e32008-10-06 05:32:18 +0000808// Note that this block abuses the Token type just a little. If there is
809// no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
810// there is an INDEXED BY clause, then the token is populated as per normal,
811// with z pointing to the token data and n containing the number of bytes
812// in the token.
813//
814// If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
danielk1977b1c685b2008-10-06 16:18:39 +0000815// normally illegal. The sqlite3SrcListIndexedBy() function
danielk197785574e32008-10-06 05:32:18 +0000816// recognizes and interprets this as a special case.
817//
818%type indexed_opt {Token}
drh200adc92022-04-06 19:46:20 +0000819%type indexed_by {Token}
danielk197785574e32008-10-06 05:32:18 +0000820indexed_opt(A) ::= . {A.z=0; A.n=0;}
drh200adc92022-04-06 19:46:20 +0000821indexed_opt(A) ::= indexed_by(A).
822indexed_by(A) ::= INDEXED BY nm(X). {A = X;}
823indexed_by(A) ::= NOT INDEXED. {A.z=0; A.n=1;}
danielk197785574e32008-10-06 05:32:18 +0000824
drh348784e2000-05-29 20:41:49 +0000825%type orderby_opt {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000826%destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
drh108aa002015-08-24 20:21:20 +0000827
828// the sortlist non-terminal stores a list of expression where each
829// expression is optionally followed by ASC or DESC to indicate the
830// sort order.
831//
drh348784e2000-05-29 20:41:49 +0000832%type sortlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000833%destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000834
835orderby_opt(A) ::= . {A = 0;}
836orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;}
dan6e118922019-08-12 16:36:38 +0000837sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
drh1be266b2017-12-24 00:18:47 +0000838 A = sqlite3ExprListAppend(pParse,A,Y);
dan6e118922019-08-12 16:36:38 +0000839 sqlite3ExprListSetSortOrder(A,Z,X);
drh9bb61fe2000-06-05 16:01:39 +0000840}
dan6e118922019-08-12 16:36:38 +0000841sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
drh1be266b2017-12-24 00:18:47 +0000842 A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
dan6e118922019-08-12 16:36:38 +0000843 sqlite3ExprListSetSortOrder(A,Z,X);
drh9bb61fe2000-06-05 16:01:39 +0000844}
drh348784e2000-05-29 20:41:49 +0000845
846%type sortorder {int}
847
drh8e2ca022002-06-17 17:07:19 +0000848sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;}
849sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;}
drhbc622bc2015-08-24 15:39:42 +0000850sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;}
drh348784e2000-05-29 20:41:49 +0000851
dan6e118922019-08-12 16:36:38 +0000852%type nulls {int}
853nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;}
854nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;}
855nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;}
856
drh22827922000-06-06 17:27:05 +0000857%type groupby_opt {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000858%destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
drh6206d502000-06-19 19:09:08 +0000859groupby_opt(A) ::= . {A = 0;}
drh9245c242007-06-20 12:18:31 +0000860groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
drh22827922000-06-06 17:27:05 +0000861
862%type having_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000863%destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
drh6206d502000-06-19 19:09:08 +0000864having_opt(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000865having_opt(A) ::= HAVING expr(X). {A = X;}
drh22827922000-06-06 17:27:05 +0000866
drh8c0833f2017-11-14 23:48:23 +0000867%type limit_opt {Expr*}
drh15926592007-04-06 15:02:13 +0000868
869// The destructor for limit_opt will never fire in the current grammar.
870// The limit_opt non-terminal only occurs at the end of a single production
871// rule for SELECT statements. As soon as the rule that create the
872// limit_opt non-terminal reduces, the SELECT statement rule will also
873// reduce. So there is never a limit_opt non-terminal on the stack
874// except as a transient. So there is never anything to destroy.
875//
drh8c0833f2017-11-14 23:48:23 +0000876//%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
877limit_opt(A) ::= . {A = 0;}
878limit_opt(A) ::= LIMIT expr(X).
drh1be266b2017-12-24 00:18:47 +0000879 {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
danielk1977a2dc3b12005-02-05 12:48:48 +0000880limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y).
drh1be266b2017-12-24 00:18:47 +0000881 {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
danielk1977a2dc3b12005-02-05 12:48:48 +0000882limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
drh1be266b2017-12-24 00:18:47 +0000883 {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
drh9bbca4c2001-11-06 04:00:18 +0000884
drh382c0242001-10-06 16:33:02 +0000885/////////////////////////// The DELETE statement /////////////////////////////
886//
drhc601f102020-07-03 17:24:35 +0000887%if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
drh2053f312021-01-12 20:16:31 +0000888cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W)
drh931577f2008-10-10 14:27:16 +0000889 orderby_opt(O) limit_opt(L). {
danielk1977b1c685b2008-10-06 16:18:39 +0000890 sqlite3SrcListIndexedBy(pParse, X, &I);
drh6a0db872019-01-31 02:42:47 +0000891#ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
drhc601f102020-07-03 17:24:35 +0000892 if( O || L ){
893 updateDeleteLimitError(pParse,O,L);
894 O = 0;
895 L = 0;
896 }
drh6a0db872019-01-31 02:42:47 +0000897#endif
drh8c0833f2017-11-14 23:48:23 +0000898 sqlite3DeleteFrom(pParse,X,W,O,L);
danielk1977b1c685b2008-10-06 16:18:39 +0000899}
drhc601f102020-07-03 17:24:35 +0000900%else
drh2053f312021-01-12 20:16:31 +0000901cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). {
shane4281bd42008-10-07 05:27:11 +0000902 sqlite3SrcListIndexedBy(pParse, X, &I);
drh8c0833f2017-11-14 23:48:23 +0000903 sqlite3DeleteFrom(pParse,X,W,0,0);
shane4281bd42008-10-07 05:27:11 +0000904}
905%endif
drh348784e2000-05-29 20:41:49 +0000906
907%type where_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +0000908%destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
drh2053f312021-01-12 20:16:31 +0000909%type where_opt_ret {Expr*}
910%destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +0000911
912where_opt(A) ::= . {A = 0;}
drh1be266b2017-12-24 00:18:47 +0000913where_opt(A) ::= WHERE expr(X). {A = X;}
drh2053f312021-01-12 20:16:31 +0000914where_opt_ret(A) ::= . {A = 0;}
915where_opt_ret(A) ::= WHERE expr(X). {A = X;}
916where_opt_ret(A) ::= RETURNING selcollist(X).
917 {sqlite3AddReturning(pParse,X); A = 0;}
918where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y).
919 {sqlite3AddReturning(pParse,Y); A = X;}
drh348784e2000-05-29 20:41:49 +0000920
drh382c0242001-10-06 16:33:02 +0000921////////////////////////// The UPDATE command ////////////////////////////////
922//
drhc601f102020-07-03 17:24:35 +0000923%if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
dan69887c92020-04-27 20:55:33 +0000924cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
drhb8352472021-01-29 19:32:17 +0000925 where_opt_ret(W) orderby_opt(O) limit_opt(L). {
danielk1977b1c685b2008-10-06 16:18:39 +0000926 sqlite3SrcListIndexedBy(pParse, X, &I);
drh19430052022-05-28 14:03:23 +0000927 if( F ){
928 SrcList *pFromClause = F;
929 if( pFromClause->nSrc>1 ){
930 Select *pSubquery;
931 Token as;
932 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
933 as.n = 0;
934 as.z = 0;
935 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
936 }
937 X = sqlite3SrcListAppendList(pParse, X, pFromClause);
938 }
drhb1a6c3c2008-03-20 16:30:17 +0000939 sqlite3ExprListCheckLength(pParse,Y,"set list");
drhc601f102020-07-03 17:24:35 +0000940#ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
941 if( O || L ){
942 updateDeleteLimitError(pParse,O,L);
943 O = 0;
944 L = 0;
945 }
946#endif
drheac9fab2018-04-16 13:00:50 +0000947 sqlite3Update(pParse,X,Y,W,R,O,L,0);
danielk19777a15a4b2007-05-08 17:54:43 +0000948}
drhc601f102020-07-03 17:24:35 +0000949%else
dan69887c92020-04-27 20:55:33 +0000950cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
drhb8352472021-01-29 19:32:17 +0000951 where_opt_ret(W). {
shane4281bd42008-10-07 05:27:11 +0000952 sqlite3SrcListIndexedBy(pParse, X, &I);
953 sqlite3ExprListCheckLength(pParse,Y,"set list");
drhfb98dac2022-05-25 02:32:11 +0000954 if( F ){
955 SrcList *pFromClause = F;
956 if( pFromClause->nSrc>1 ){
957 Select *pSubquery;
958 Token as;
959 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
960 as.n = 0;
961 as.z = 0;
962 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
963 }
964 X = sqlite3SrcListAppendList(pParse, X, pFromClause);
965 }
drheac9fab2018-04-16 13:00:50 +0000966 sqlite3Update(pParse,X,Y,W,R,0,0,0);
shane4281bd42008-10-07 05:27:11 +0000967}
968%endif
drh348784e2000-05-29 20:41:49 +0000969
drhc601f102020-07-03 17:24:35 +0000970
971
drhf8db1bc2005-04-22 02:38:37 +0000972%type setlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +0000973%destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
drhf8db1bc2005-04-22 02:38:37 +0000974
drh4dd0d3f2016-02-17 01:18:33 +0000975setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000976 A = sqlite3ExprListAppend(pParse, A, Y);
drhb7916a72009-05-27 10:31:29 +0000977 sqlite3ExprListSetName(pParse, A, &X, 1);
978}
drha1251bc2016-08-20 00:51:37 +0000979setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000980 A = sqlite3ExprListAppendVector(pParse, A, X, Y);
drha1251bc2016-08-20 00:51:37 +0000981}
drhb7916a72009-05-27 10:31:29 +0000982setlist(A) ::= nm(X) EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000983 A = sqlite3ExprListAppend(pParse, 0, Y);
drhb7916a72009-05-27 10:31:29 +0000984 sqlite3ExprListSetName(pParse, A, &X, 1);
985}
drha1251bc2016-08-20 00:51:37 +0000986setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
drh1be266b2017-12-24 00:18:47 +0000987 A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
drha1251bc2016-08-20 00:51:37 +0000988}
drh348784e2000-05-29 20:41:49 +0000989
drh382c0242001-10-06 16:33:02 +0000990////////////////////////// The INSERT command /////////////////////////////////
991//
drh5e3a6eb2018-04-19 11:45:16 +0000992cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
drh2c2e8442018-04-07 15:04:05 +0000993 upsert(U). {
drh46d2e5c2018-04-12 13:15:43 +0000994 sqlite3Insert(pParse, X, S, F, R, U);
dan4e9119d2014-01-13 15:12:23 +0000995}
drh2053f312021-01-12 20:16:31 +0000996cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning.
dan4e9119d2014-01-13 15:12:23 +0000997{
drh2c2e8442018-04-07 15:04:05 +0000998 sqlite3Insert(pParse, X, 0, F, R, 0);
dan4e9119d2014-01-13 15:12:23 +0000999}
drh348784e2000-05-29 20:41:49 +00001000
drh46d2e5c2018-04-12 13:15:43 +00001001%type upsert {Upsert*}
drh1c198482020-12-14 13:52:03 +00001002
1003// Because upsert only occurs at the tip end of the INSERT rule for cmd,
1004// there is never a case where the value of the upsert pointer will not
1005// be destroyed by the cmd action. So comment-out the destructor to
1006// avoid unreachable code.
1007//%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
drh46d2e5c2018-04-12 13:15:43 +00001008upsert(A) ::= . { A = 0; }
drh2053f312021-01-12 20:16:31 +00001009upsert(A) ::= RETURNING selcollist(X). { A = 0; sqlite3AddReturning(pParse,X); }
drhe9c2e772018-04-13 13:06:45 +00001010upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
drh2549e4c2020-12-08 14:29:03 +00001011 DO UPDATE SET setlist(Z) where_opt(W) upsert(N).
1012 { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);}
1013upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N).
1014 { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); }
drh2053f312021-01-12 20:16:31 +00001015upsert(A) ::= ON CONFLICT DO NOTHING returning.
drh2549e4c2020-12-08 14:29:03 +00001016 { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
drh2053f312021-01-12 20:16:31 +00001017upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning.
drh2549e4c2020-12-08 14:29:03 +00001018 { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);}
drh26cf56f2018-04-06 19:36:49 +00001019
drh2053f312021-01-12 20:16:31 +00001020returning ::= RETURNING selcollist(X). {sqlite3AddReturning(pParse,X);}
1021returning ::= .
1022
drh3334d082015-11-10 13:45:21 +00001023%type insert_cmd {int}
drhfa86c412002-02-02 15:01:15 +00001024insert_cmd(A) ::= INSERT orconf(R). {A = R;}
1025insert_cmd(A) ::= REPLACE. {A = OE_Replace;}
1026
drh8981b902015-08-24 17:42:49 +00001027%type idlist_opt {IdList*}
1028%destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
drh81eba732013-10-19 23:31:56 +00001029%type idlist {IdList*}
1030%destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +00001031
drh8981b902015-08-24 17:42:49 +00001032idlist_opt(A) ::= . {A = 0;}
1033idlist_opt(A) ::= LP idlist(X) RP. {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +00001034idlist(A) ::= idlist(A) COMMA nm(Y).
dan5496d6a2018-08-13 17:14:26 +00001035 {A = sqlite3IdListAppend(pParse,A,&Y);}
drh81eba732013-10-19 23:31:56 +00001036idlist(A) ::= nm(Y).
dan5496d6a2018-08-13 17:14:26 +00001037 {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}
drh348784e2000-05-29 20:41:49 +00001038
drh382c0242001-10-06 16:33:02 +00001039/////////////////////////// Expression Processing /////////////////////////////
1040//
drh348784e2000-05-29 20:41:49 +00001041
drh1be266b2017-12-24 00:18:47 +00001042%type expr {Expr*}
1043%destructor expr {sqlite3ExprDelete(pParse->db, $$);}
1044%type term {Expr*}
1045%destructor term {sqlite3ExprDelete(pParse->db, $$);}
drhb7916a72009-05-27 10:31:29 +00001046
1047%include {
drhb7916a72009-05-27 10:31:29 +00001048
drh796588a2022-02-05 21:49:47 +00001049 /* Construct a new Expr object from a single token */
drh1be266b2017-12-24 00:18:47 +00001050 static Expr *tokenExpr(Parse *pParse, int op, Token t){
drh0cd874b2016-09-26 12:38:22 +00001051 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
1052 if( p ){
dand145e5f2018-08-21 08:29:48 +00001053 /* memset(p, 0, sizeof(Expr)); */
drh0cd874b2016-09-26 12:38:22 +00001054 p->op = (u8)op;
drh11949042019-08-05 18:01:42 +00001055 p->affExpr = 0;
drh0cd874b2016-09-26 12:38:22 +00001056 p->flags = EP_Leaf;
drhe7375bf2020-03-10 19:24:38 +00001057 ExprClearVVAProperties(p);
drh9c949b12022-04-07 12:10:00 +00001058 /* p->iAgg = -1; // Not required */
dand145e5f2018-08-21 08:29:48 +00001059 p->pLeft = p->pRight = 0;
dand145e5f2018-08-21 08:29:48 +00001060 p->pAggInfo = 0;
drh477572b2021-10-07 20:46:29 +00001061 memset(&p->x, 0, sizeof(p->x));
1062 memset(&p->y, 0, sizeof(p->y));
dand145e5f2018-08-21 08:29:48 +00001063 p->op2 = 0;
drh4fe759b2018-08-23 18:22:00 +00001064 p->iTable = 0;
drhd3130da2018-08-23 18:50:19 +00001065 p->iColumn = 0;
drh0cd874b2016-09-26 12:38:22 +00001066 p->u.zToken = (char*)&p[1];
1067 memcpy(p->u.zToken, t.z, t.n);
1068 p->u.zToken[t.n] = 0;
drh796588a2022-02-05 21:49:47 +00001069 p->w.iOfst = (int)(t.z - pParse->zTail);
drh0cd874b2016-09-26 12:38:22 +00001070 if( sqlite3Isquote(p->u.zToken[0]) ){
drh51d35b02019-01-11 13:32:23 +00001071 sqlite3DequoteExpr(p);
drh0cd874b2016-09-26 12:38:22 +00001072 }
1073#if SQLITE_MAX_EXPR_DEPTH>0
1074 p->nHeight = 1;
1075#endif
danc9461ec2018-08-29 21:00:16 +00001076 if( IN_RENAME_OBJECT ){
dan07e95232018-08-21 16:32:53 +00001077 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
dand145e5f2018-08-21 08:29:48 +00001078 }
drh0cd874b2016-09-26 12:38:22 +00001079 }
drh1be266b2017-12-24 00:18:47 +00001080 return p;
drhb7916a72009-05-27 10:31:29 +00001081 }
dand145e5f2018-08-21 08:29:48 +00001082
drhb7916a72009-05-27 10:31:29 +00001083}
drh348784e2000-05-29 20:41:49 +00001084
drh4dd0d3f2016-02-17 01:18:33 +00001085expr(A) ::= term(A).
drh1be266b2017-12-24 00:18:47 +00001086expr(A) ::= LP expr(X) RP. {A = X;}
1087expr(A) ::= id(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
1088expr(A) ::= JOIN_KW(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
drh5ad1a6c2002-07-01 12:27:09 +00001089expr(A) ::= nm(X) DOT nm(Y). {
drh796588a2022-02-05 21:49:47 +00001090 Expr *temp1 = tokenExpr(pParse,TK_ID,X);
1091 Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
drh1be266b2017-12-24 00:18:47 +00001092 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
drhe1b6a5b2000-07-29 13:06:59 +00001093}
drhd24cc422003-03-27 12:51:24 +00001094expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
drh796588a2022-02-05 21:49:47 +00001095 Expr *temp1 = tokenExpr(pParse,TK_ID,X);
1096 Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
1097 Expr *temp3 = tokenExpr(pParse,TK_ID,Z);
drhabfd35e2016-12-06 22:47:23 +00001098 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
dan02ede432022-02-12 16:02:37 +00001099 if( IN_RENAME_OBJECT ){
1100 sqlite3RenameTokenRemap(pParse, 0, temp1);
1101 }
drh1be266b2017-12-24 00:18:47 +00001102 A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
drhd24cc422003-03-27 12:51:24 +00001103}
drh1be266b2017-12-24 00:18:47 +00001104term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
1105term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
drh0cd874b2016-09-26 12:38:22 +00001106term(A) ::= INTEGER(X). {
drh1be266b2017-12-24 00:18:47 +00001107 A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
drh17a93ae2022-02-06 11:26:51 +00001108 if( A ) A->w.iOfst = (int)(X.z - pParse->zTail);
drh0cd874b2016-09-26 12:38:22 +00001109}
drh7c972de2003-09-06 22:18:07 +00001110expr(A) ::= VARIABLE(X). {
drh8679fba2016-04-11 01:43:33 +00001111 if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
drhde25a882016-10-03 15:28:24 +00001112 u32 n = X.n;
drh1be266b2017-12-24 00:18:47 +00001113 A = tokenExpr(pParse, TK_VARIABLE, X);
1114 sqlite3ExprAssignVarNumber(pParse, A, n);
drh8f3b1372016-04-11 01:26:31 +00001115 }else{
drhf59b12f2014-01-11 03:54:05 +00001116 /* When doing a nested parse, one can include terms in an expression
1117 ** that look like this: #1 #2 ... These terms refer to registers
1118 ** in the virtual machine. #N is the N-th register. */
drh8f3b1372016-04-11 01:26:31 +00001119 Token t = X; /*A-overwrites-X*/
1120 assert( t.n>=2 );
drhf59b12f2014-01-11 03:54:05 +00001121 if( pParse->nested==0 ){
drh43303de2016-02-17 12:34:03 +00001122 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
drh1be266b2017-12-24 00:18:47 +00001123 A = 0;
drhf59b12f2014-01-11 03:54:05 +00001124 }else{
drh1be266b2017-12-24 00:18:47 +00001125 A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
1126 if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
drhf59b12f2014-01-11 03:54:05 +00001127 }
drhf59b12f2014-01-11 03:54:05 +00001128 }
drh7c972de2003-09-06 22:18:07 +00001129}
dan59ff4252018-06-29 17:44:52 +00001130expr(A) ::= expr(A) COLLATE ids(C). {
drh1be266b2017-12-24 00:18:47 +00001131 A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
drh8b4c40d2007-02-01 23:02:45 +00001132}
drh487e2622005-06-25 18:42:14 +00001133%ifndef SQLITE_OMIT_CAST
drh1be266b2017-12-24 00:18:47 +00001134expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
1135 A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
1136 sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
drh487e2622005-06-25 18:42:14 +00001137}
drh154d4b22006-09-21 11:02:16 +00001138%endif SQLITE_OMIT_CAST
drh550a3302018-07-27 22:14:50 +00001139
1140
1141expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. {
drh954733b2018-07-27 23:33:16 +00001142 A = sqlite3ExprFunction(pParse, Y, &X, D);
drh550a3302018-07-27 22:14:50 +00001143}
1144expr(A) ::= id(X) LP STAR RP. {
drh954733b2018-07-27 23:33:16 +00001145 A = sqlite3ExprFunction(pParse, 0, &X, 0);
drh550a3302018-07-27 22:14:50 +00001146}
1147
dan67a9b8e2018-06-22 20:51:35 +00001148%ifndef SQLITE_OMIT_WINDOWFUNC
dan4f9adee2019-07-13 16:22:50 +00001149expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
dan64882712019-07-11 18:43:33 +00001150 A = sqlite3ExprFunction(pParse, Y, &X, D);
dan4f9adee2019-07-13 16:22:50 +00001151 sqlite3WindowAttach(pParse, A, Z);
dan64882712019-07-11 18:43:33 +00001152}
dan4f9adee2019-07-13 16:22:50 +00001153expr(A) ::= id(X) LP STAR RP filter_over(Z). {
drh954733b2018-07-27 23:33:16 +00001154 A = sqlite3ExprFunction(pParse, 0, &X, 0);
dan4f9adee2019-07-13 16:22:50 +00001155 sqlite3WindowAttach(pParse, A, Z);
drhe1b6a5b2000-07-29 13:06:59 +00001156}
drh550a3302018-07-27 22:14:50 +00001157%endif
1158
drhb71090f2005-05-23 17:26:51 +00001159term(A) ::= CTIME_KW(OP). {
drh954733b2018-07-27 23:33:16 +00001160 A = sqlite3ExprFunction(pParse, 0, &OP, 0);
drhb7916a72009-05-27 10:31:29 +00001161}
1162
drh1be266b2017-12-24 00:18:47 +00001163expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
1164 ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
1165 A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
1166 if( A ){
1167 A->x.pList = pList;
drh76baf792019-10-28 04:20:28 +00001168 if( ALWAYS(pList->nExpr) ){
drh269d3222019-10-23 18:09:39 +00001169 A->flags |= pList->a[0].pExpr->flags & EP_Propagate;
1170 }
drh8bd0d582016-08-20 18:06:14 +00001171 }else{
1172 sqlite3ExprListDelete(pParse->db, pList);
dan71c57db2016-07-09 20:23:55 +00001173 }
1174}
1175
drhd5c851c2019-04-19 13:38:34 +00001176expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);}
drh1be266b2017-12-24 00:18:47 +00001177expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001178expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001179 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1180expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001181expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001182 {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001183expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001184 {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh4dd0d3f2016-02-17 01:18:33 +00001185expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
drh1be266b2017-12-24 00:18:47 +00001186 {A=sqlite3PExpr(pParse,@OP,A,Y);}
1187expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
drh410c3012016-09-24 17:42:43 +00001188%type likeop {Token}
drh7e84b372017-02-20 14:30:17 +00001189likeop(A) ::= LIKE_KW|MATCH(A).
drh410c3012016-09-24 17:42:43 +00001190likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
drh4dd0d3f2016-02-17 01:18:33 +00001191expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] {
drh8aa34ae2006-03-13 12:54:09 +00001192 ExprList *pList;
drh410c3012016-09-24 17:42:43 +00001193 int bNot = OP.n & 0x80000000;
1194 OP.n &= 0x7fffffff;
drh1be266b2017-12-24 00:18:47 +00001195 pList = sqlite3ExprListAppend(pParse,0, Y);
1196 pList = sqlite3ExprListAppend(pParse,pList, A);
drh954733b2018-07-27 23:33:16 +00001197 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
drh1be266b2017-12-24 00:18:47 +00001198 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1199 if( A ) A->flags |= EP_InfixFunc;
drh0ac65892002-04-20 14:24:41 +00001200}
drh4dd0d3f2016-02-17 01:18:33 +00001201expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] {
drh1dca1452010-07-19 02:30:33 +00001202 ExprList *pList;
drh410c3012016-09-24 17:42:43 +00001203 int bNot = OP.n & 0x80000000;
1204 OP.n &= 0x7fffffff;
drh1be266b2017-12-24 00:18:47 +00001205 pList = sqlite3ExprListAppend(pParse,0, Y);
1206 pList = sqlite3ExprListAppend(pParse,pList, A);
1207 pList = sqlite3ExprListAppend(pParse,pList, E);
drh954733b2018-07-27 23:33:16 +00001208 A = sqlite3ExprFunction(pParse, pList, &OP, 0);
drh1be266b2017-12-24 00:18:47 +00001209 if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
1210 if( A ) A->flags |= EP_InfixFunc;
drh1dca1452010-07-19 02:30:33 +00001211}
danielk19777c6303c2004-11-17 16:41:29 +00001212
drh1be266b2017-12-24 00:18:47 +00001213expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);}
1214expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
drh6a2fe092009-09-23 02:29:36 +00001215
drh6a517412009-11-12 03:46:34 +00001216%include {
1217 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
1218 ** unary TK_ISNULL or TK_NOTNULL expression. */
1219 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
1220 sqlite3 *db = pParse->db;
danc50f75d2018-09-06 18:56:36 +00001221 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
shaneh5e17e8b2009-12-03 04:40:47 +00001222 pA->op = (u8)op;
drh6a517412009-11-12 03:46:34 +00001223 sqlite3ExprDelete(db, pA->pRight);
1224 pA->pRight = 0;
1225 }
1226 }
1227}
1228
drh6a2fe092009-09-23 02:29:36 +00001229// expr1 IS expr2
1230// expr1 IS NOT expr2
1231//
1232// If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2
1233// is any other expression, code as TK_IS or TK_ISNOT.
1234//
drh4dd0d3f2016-02-17 01:18:33 +00001235expr(A) ::= expr(A) IS expr(Y). {
drh1be266b2017-12-24 00:18:47 +00001236 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1237 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
drh6a2fe092009-09-23 02:29:36 +00001238}
drh4dd0d3f2016-02-17 01:18:33 +00001239expr(A) ::= expr(A) IS NOT expr(Y). {
drh1be266b2017-12-24 00:18:47 +00001240 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1241 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
drh6a2fe092009-09-23 02:29:36 +00001242}
drh902e2602022-05-12 11:45:20 +00001243expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y). {
1244 A = sqlite3PExpr(pParse,TK_IS,A,Y);
1245 binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
1246}
1247expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). {
1248 A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
1249 binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
1250}
drhb7916a72009-05-27 10:31:29 +00001251
drh43303de2016-02-17 12:34:03 +00001252expr(A) ::= NOT(B) expr(X).
drh1be266b2017-12-24 00:18:47 +00001253 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
drh43303de2016-02-17 12:34:03 +00001254expr(A) ::= BITNOT(B) expr(X).
drh1be266b2017-12-24 00:18:47 +00001255 {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
drhca5aa592018-06-19 11:15:19 +00001256expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
1257 A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
1258 /*A-overwrites-B*/
1259}
drhb7916a72009-05-27 10:31:29 +00001260
drhd5326c32022-01-07 14:58:47 +00001261expr(A) ::= expr(B) PTR(C) expr(D). {
1262 ExprList *pList = sqlite3ExprListAppend(pParse, 0, B);
1263 pList = sqlite3ExprListAppend(pParse, pList, D);
1264 A = sqlite3ExprFunction(pParse, pList, &C, 0);
1265}
1266
drh2e3a1f12004-10-06 14:39:28 +00001267%type between_op {int}
1268between_op(A) ::= BETWEEN. {A = 0;}
1269between_op(A) ::= NOT BETWEEN. {A = 1;}
drh4dd0d3f2016-02-17 01:18:33 +00001270expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
drh1be266b2017-12-24 00:18:47 +00001271 ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
1272 pList = sqlite3ExprListAppend(pParse,pList, Y);
1273 A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
1274 if( A ){
1275 A->x.pList = pList;
drh53f733c2005-09-16 02:38:09 +00001276 }else{
drh633e6d52008-07-28 19:34:53 +00001277 sqlite3ExprListDelete(pParse->db, pList);
drh53f733c2005-09-16 02:38:09 +00001278 }
drh1be266b2017-12-24 00:18:47 +00001279 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drhfef52082000-06-06 01:50:43 +00001280}
drh51522cd2005-01-20 13:36:19 +00001281%ifndef SQLITE_OMIT_SUBQUERY
danielk19773e8c37e2005-01-21 03:12:14 +00001282 %type in_op {int}
1283 in_op(A) ::= IN. {A = 0;}
1284 in_op(A) ::= NOT IN. {A = 1;}
drh1be266b2017-12-24 00:18:47 +00001285 expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
drh094430e2010-07-14 18:24:06 +00001286 if( Y==0 ){
dan473c1bf2010-07-15 11:14:21 +00001287 /* Expressions of the form
1288 **
1289 ** expr1 IN ()
1290 ** expr1 NOT IN ()
1291 **
1292 ** simplify to constants 0 (false) and 1 (true), respectively,
1293 ** regardless of the value of expr1.
1294 */
drh8e34e402019-06-11 10:43:56 +00001295 sqlite3ExprUnmapAndDelete(pParse, A);
dan5348fbe2022-05-17 15:01:01 +00001296 A = sqlite3Expr(pParse->db, TK_STRING, N ? "true" : "false");
1297 if( A ) sqlite3ExprIdToTrueFalse(A);
danielk1977d5d56522005-03-16 12:15:20 +00001298 }else{
dan9289f512021-07-06 20:44:32 +00001299 Expr *pRHS = Y->a[0].pExpr;
dan74777f92021-07-07 13:53:55 +00001300 if( Y->nExpr==1 && sqlite3ExprIsConstant(pRHS) && A->op!=TK_VECTOR ){
dan9289f512021-07-06 20:44:32 +00001301 Y->a[0].pExpr = 0;
drh094430e2010-07-14 18:24:06 +00001302 sqlite3ExprListDelete(pParse->db, Y);
dan9289f512021-07-06 20:44:32 +00001303 pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
1304 A = sqlite3PExpr(pParse, TK_EQ, A, pRHS);
1305 }else{
1306 A = sqlite3PExpr(pParse, TK_IN, A, 0);
dan74777f92021-07-07 13:53:55 +00001307 if( A==0 ){
1308 sqlite3ExprListDelete(pParse->db, Y);
1309 }else if( A->pLeft->op==TK_VECTOR ){
1310 int nExpr = A->pLeft->x.pList->nExpr;
drh70988592021-07-07 19:40:18 +00001311 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y);
1312 if( pSelectRHS ){
1313 parserDoubleLinkSelect(pParse, pSelectRHS);
1314 sqlite3PExprAddSelect(pParse, A, pSelectRHS);
dan9289f512021-07-06 20:44:32 +00001315 }
1316 }else{
1317 A->x.pList = Y;
1318 sqlite3ExprSetHeightAndFlags(pParse, A);
1319 }
drh094430e2010-07-14 18:24:06 +00001320 }
drh1be266b2017-12-24 00:18:47 +00001321 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
danielk1977d5d56522005-03-16 12:15:20 +00001322 }
danielk19773e8c37e2005-01-21 03:12:14 +00001323 }
drh1be266b2017-12-24 00:18:47 +00001324 expr(A) ::= LP select(X) RP. {
1325 A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
1326 sqlite3PExprAddSelect(pParse, A, X);
drh51522cd2005-01-20 13:36:19 +00001327 }
drh1be266b2017-12-24 00:18:47 +00001328 expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] {
1329 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1330 sqlite3PExprAddSelect(pParse, A, Y);
1331 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drh51522cd2005-01-20 13:36:19 +00001332 }
drh5fbab882016-07-02 12:08:14 +00001333 expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
drh29c992c2019-01-17 15:40:41 +00001334 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
drh8c0833f2017-11-14 23:48:23 +00001335 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
drh9de47572016-07-02 12:33:21 +00001336 if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
drh1be266b2017-12-24 00:18:47 +00001337 A = sqlite3PExpr(pParse, TK_IN, A, 0);
1338 sqlite3PExprAddSelect(pParse, A, pSelect);
1339 if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
drh51522cd2005-01-20 13:36:19 +00001340 }
drh1be266b2017-12-24 00:18:47 +00001341 expr(A) ::= EXISTS LP select(Y) RP. {
drh43303de2016-02-17 12:34:03 +00001342 Expr *p;
drh1be266b2017-12-24 00:18:47 +00001343 p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
drh08de4f72016-04-11 01:06:47 +00001344 sqlite3PExprAddSelect(pParse, p, Y);
drh51522cd2005-01-20 13:36:19 +00001345 }
drh154d4b22006-09-21 11:02:16 +00001346%endif SQLITE_OMIT_SUBQUERY
drhfef52082000-06-06 01:50:43 +00001347
drh17a7f8d2002-03-24 13:13:27 +00001348/* CASE expressions */
drh1be266b2017-12-24 00:18:47 +00001349expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
1350 A = sqlite3PExpr(pParse, TK_CASE, X, 0);
1351 if( A ){
1352 A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
1353 sqlite3ExprSetHeightAndFlags(pParse, A);
drh53f733c2005-09-16 02:38:09 +00001354 }else{
drh633e6d52008-07-28 19:34:53 +00001355 sqlite3ExprListDelete(pParse->db, Y);
drhc5cd1242013-09-12 16:50:49 +00001356 sqlite3ExprDelete(pParse->db, Z);
drh53f733c2005-09-16 02:38:09 +00001357 }
drh17a7f8d2002-03-24 13:13:27 +00001358}
1359%type case_exprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001360%destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +00001361case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
drh1be266b2017-12-24 00:18:47 +00001362 A = sqlite3ExprListAppend(pParse,A, Y);
1363 A = sqlite3ExprListAppend(pParse,A, Z);
drh17a7f8d2002-03-24 13:13:27 +00001364}
1365case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
drh1be266b2017-12-24 00:18:47 +00001366 A = sqlite3ExprListAppend(pParse,0, Y);
1367 A = sqlite3ExprListAppend(pParse,A, Z);
drh17a7f8d2002-03-24 13:13:27 +00001368}
1369%type case_else {Expr*}
drh633e6d52008-07-28 19:34:53 +00001370%destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +00001371case_else(A) ::= ELSE expr(X). {A = X;}
drh17a7f8d2002-03-24 13:13:27 +00001372case_else(A) ::= . {A = 0;}
1373%type case_operand {Expr*}
drh633e6d52008-07-28 19:34:53 +00001374%destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
drh1be266b2017-12-24 00:18:47 +00001375case_operand(A) ::= expr(X). {A = X; /*A-overwrites-X*/}
drh17a7f8d2002-03-24 13:13:27 +00001376case_operand(A) ::= . {A = 0;}
drh348784e2000-05-29 20:41:49 +00001377
1378%type exprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001379%destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh9245c242007-06-20 12:18:31 +00001380%type nexprlist {ExprList*}
drh633e6d52008-07-28 19:34:53 +00001381%destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
drh348784e2000-05-29 20:41:49 +00001382
drh4dd0d3f2016-02-17 01:18:33 +00001383exprlist(A) ::= nexprlist(A).
drh9245c242007-06-20 12:18:31 +00001384exprlist(A) ::= . {A = 0;}
drh4dd0d3f2016-02-17 01:18:33 +00001385nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
drh1be266b2017-12-24 00:18:47 +00001386 {A = sqlite3ExprListAppend(pParse,A,Y);}
drh17435752007-08-16 04:30:38 +00001387nexprlist(A) ::= expr(Y).
drh1be266b2017-12-24 00:18:47 +00001388 {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
drh9245c242007-06-20 12:18:31 +00001389
drha5224732016-07-25 14:40:43 +00001390%ifndef SQLITE_OMIT_SUBQUERY
drh5fbab882016-07-02 12:08:14 +00001391/* A paren_exprlist is an optional expression list contained inside
1392** of parenthesis */
1393%type paren_exprlist {ExprList*}
1394%destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
1395paren_exprlist(A) ::= . {A = 0;}
1396paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;}
drha5224732016-07-25 14:40:43 +00001397%endif SQLITE_OMIT_SUBQUERY
drh5fbab882016-07-02 12:08:14 +00001398
drhcce7d172000-05-31 15:34:51 +00001399
drh382c0242001-10-06 16:33:02 +00001400///////////////////////////// The CREATE INDEX command ///////////////////////
1401//
drhd9da78a2009-03-24 15:08:09 +00001402cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
drh108aa002015-08-24 20:21:20 +00001403 ON nm(Y) LP sortlist(Z) RP where_opt(W). {
drh17435752007-08-16 04:30:38 +00001404 sqlite3CreateIndex(pParse, &X, &D,
drh29c992c2019-01-17 15:40:41 +00001405 sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
drh62340f82016-05-31 21:18:15 +00001406 &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
danc9461ec2018-08-29 21:00:16 +00001407 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
1408 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
1409 }
drh9cfcf5d2002-01-29 18:41:24 +00001410}
drh717e6402001-09-27 03:22:32 +00001411
1412%type uniqueflag {int}
drh74ad7fe2004-10-07 03:06:28 +00001413uniqueflag(A) ::= UNIQUE. {A = OE_Abort;}
1414uniqueflag(A) ::= . {A = OE_None;}
drh348784e2000-05-29 20:41:49 +00001415
drh348784e2000-05-29 20:41:49 +00001416
drh108aa002015-08-24 20:21:20 +00001417// The eidlist non-terminal (Expression Id List) generates an ExprList
1418// from a list of identifiers. The identifier names are in ExprList.a[].zName.
1419// This list is stored in an ExprList rather than an IdList so that it
1420// can be easily sent to sqlite3ColumnsExprList().
1421//
1422// eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1423// used for the arguments to an index. That is just an historical accident.
1424//
1425// IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1426// COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
drh067b92b2020-06-19 15:24:12 +00001427// places - places that might have been stored in the sqlite_schema table.
drh108aa002015-08-24 20:21:20 +00001428// Those extra features were ignored. But because they might be in some
1429// (busted) old databases, we need to continue parsing them when loading
1430// historical schemas.
1431//
1432%type eidlist {ExprList*}
1433%destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
1434%type eidlist_opt {ExprList*}
1435%destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
1436
1437%include {
1438 /* Add a single new term to an ExprList that is used to store a
1439 ** list of identifiers. Report an error if the ID list contains
1440 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1441 ** error while parsing a legacy schema.
1442 */
1443 static ExprList *parserAddExprIdListTerm(
1444 Parse *pParse,
1445 ExprList *pPrior,
1446 Token *pIdToken,
1447 int hasCollate,
1448 int sortOrder
1449 ){
1450 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
1451 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
1452 && pParse->db->init.busy==0
1453 ){
1454 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
1455 pIdToken->n, pIdToken->z);
1456 }
1457 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
1458 return p;
1459 }
1460} // end %include
1461
1462eidlist_opt(A) ::= . {A = 0;}
1463eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;}
drh4dd0d3f2016-02-17 01:18:33 +00001464eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). {
1465 A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
danielk19770202b292004-06-09 09:55:16 +00001466}
drh108aa002015-08-24 20:21:20 +00001467eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
drhcf82f0d2016-02-17 04:33:10 +00001468 A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
danielk19770202b292004-06-09 09:55:16 +00001469}
danielk19770202b292004-06-09 09:55:16 +00001470
drh108aa002015-08-24 20:21:20 +00001471%type collate {int}
1472collate(C) ::= . {C = 0;}
dan59ff4252018-06-29 17:44:52 +00001473collate(C) ::= COLLATE ids. {C = 1;}
drha34001c2007-02-02 12:44:37 +00001474
drh348784e2000-05-29 20:41:49 +00001475
drh8aff1012001-12-22 14:49:24 +00001476///////////////////////////// The DROP INDEX command /////////////////////////
drh382c0242001-10-06 16:33:02 +00001477//
drh4d91a702006-01-04 15:54:36 +00001478cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
drh982cef72000-05-30 16:27:03 +00001479
drh382c0242001-10-06 16:33:02 +00001480///////////////////////////// The VACUUM command /////////////////////////////
1481//
drhc601f102020-07-03 17:24:35 +00001482%if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH
drh2f6239e2018-12-08 00:43:08 +00001483%type vinto {Expr*}
1484%destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
1485cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);}
1486cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);}
1487vinto(A) ::= INTO expr(X). {A = X;}
1488vinto(A) ::= . {A = 0;}
drhc601f102020-07-03 17:24:35 +00001489%endif
drhf57b14a2001-09-14 18:54:08 +00001490
drh382c0242001-10-06 16:33:02 +00001491///////////////////////////// The PRAGMA command /////////////////////////////
1492//
drh13d70422004-11-13 15:59:14 +00001493%ifndef SQLITE_OMIT_PRAGMA
drhada2ee02009-04-03 01:43:57 +00001494cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);}
1495cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drha3eb4b42007-01-27 02:38:29 +00001496cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
drhada2ee02009-04-03 01:43:57 +00001497cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y).
1498 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1499cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
1500 {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
1501
drhcf82f0d2016-02-17 04:33:10 +00001502nmnum(A) ::= plus_num(A).
1503nmnum(A) ::= nm(A).
1504nmnum(A) ::= ON(A).
1505nmnum(A) ::= DELETE(A).
1506nmnum(A) ::= DEFAULT(A).
drh154d4b22006-09-21 11:02:16 +00001507%endif SQLITE_OMIT_PRAGMA
drhf59b12f2014-01-11 03:54:05 +00001508%token_class number INTEGER|FLOAT.
drh8395b7b2012-01-28 19:44:22 +00001509plus_num(A) ::= PLUS number(X). {A = X;}
drhcf82f0d2016-02-17 04:33:10 +00001510plus_num(A) ::= number(A).
drhf57b14a2001-09-14 18:54:08 +00001511minus_num(A) ::= MINUS number(X). {A = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +00001512//////////////////////////// The CREATE TRIGGER command /////////////////////
drhf0f258b2003-04-21 18:48:45 +00001513
drhb7f91642004-10-31 02:22:47 +00001514%ifndef SQLITE_OMIT_TRIGGER
1515
drhd9da78a2009-03-24 15:08:09 +00001516cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
drh4b59ab52002-08-24 18:24:51 +00001517 Token all;
1518 all.z = A.z;
drhb27b7f52008-12-10 18:03:45 +00001519 all.n = (int)(Z.z - A.z) + Z.n;
danielk19774adee202004-05-08 08:23:19 +00001520 sqlite3FinishTrigger(pParse, S, &all);
drhf0f258b2003-04-21 18:48:45 +00001521}
1522
drhfdd48a72006-09-11 23:45:48 +00001523trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z)
1524 trigger_time(C) trigger_event(D)
drh60218d22007-04-06 11:26:00 +00001525 ON fullname(E) foreach_clause when_clause(G). {
1526 sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
drhcf82f0d2016-02-17 04:33:10 +00001527 A = (Z.n==0?B:Z); /*A-overwrites-T*/
danielk1977c3f9bad2002-05-15 08:30:12 +00001528}
1529
drhc4dd3fd2008-01-22 01:48:05 +00001530%type trigger_time {int}
drh6559e2c2017-06-28 14:26:37 +00001531trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ }
danielk1977c3f9bad2002-05-15 08:30:12 +00001532trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;}
1533trigger_time(A) ::= . { A = TK_BEFORE; }
1534
drhad3cab52002-05-24 02:04:32 +00001535%type trigger_event {struct TrigEvent}
drh633e6d52008-07-28 19:34:53 +00001536%destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
drhcf82f0d2016-02-17 04:33:10 +00001537trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1538trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
1539trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
danielk1977c3f9bad2002-05-15 08:30:12 +00001540
drh60218d22007-04-06 11:26:00 +00001541foreach_clause ::= .
1542foreach_clause ::= FOR EACH ROW.
danielk1977c3f9bad2002-05-15 08:30:12 +00001543
drh0bb132b2004-07-20 14:06:51 +00001544%type when_clause {Expr*}
drh633e6d52008-07-28 19:34:53 +00001545%destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001546when_clause(A) ::= . { A = 0; }
drh1be266b2017-12-24 00:18:47 +00001547when_clause(A) ::= WHEN expr(X). { A = X; }
danielk1977c3f9bad2002-05-15 08:30:12 +00001548
drh0bb132b2004-07-20 14:06:51 +00001549%type trigger_cmd_list {TriggerStep*}
drh633e6d52008-07-28 19:34:53 +00001550%destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
drh4dd0d3f2016-02-17 01:18:33 +00001551trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
1552 assert( A!=0 );
1553 A->pLast->pNext = X;
1554 A->pLast = X;
drha69d9162003-04-17 22:57:53 +00001555}
drh4dd0d3f2016-02-17 01:18:33 +00001556trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. {
1557 assert( A!=0 );
1558 A->pLast = A;
drh81238962008-08-11 14:26:35 +00001559}
danielk1977c3f9bad2002-05-15 08:30:12 +00001560
drhb1819a02009-07-03 15:37:27 +00001561// Disallow qualified table names on INSERT, UPDATE, and DELETE statements
1562// within a trigger. The table to INSERT, UPDATE, or DELETE is always in
1563// the same database as the table that the trigger fires on.
1564//
1565%type trnm {Token}
drh4dd0d3f2016-02-17 01:18:33 +00001566trnm(A) ::= nm(A).
drhb1819a02009-07-03 15:37:27 +00001567trnm(A) ::= nm DOT nm(X). {
1568 A = X;
1569 sqlite3ErrorMsg(pParse,
1570 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
1571 "statements within triggers");
1572}
1573
1574// Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1575// statements within triggers. We make a specific error message for this
1576// since it is an exception to the default grammar rules.
1577//
1578tridxby ::= .
1579tridxby ::= INDEXED BY nm. {
1580 sqlite3ErrorMsg(pParse,
1581 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1582 "within triggers");
1583}
1584tridxby ::= NOT INDEXED. {
1585 sqlite3ErrorMsg(pParse,
1586 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1587 "within triggers");
1588}
1589
1590
1591
drh0bb132b2004-07-20 14:06:51 +00001592%type trigger_cmd {TriggerStep*}
drh633e6d52008-07-28 19:34:53 +00001593%destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001594// UPDATE
drhb1819a02009-07-03 15:37:27 +00001595trigger_cmd(A) ::=
dane7877b22020-07-14 19:51:01 +00001596 UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E).
1597 {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001598
1599// INSERT
drhf259df52017-12-27 20:38:35 +00001600trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
drh2c2e8442018-04-07 15:04:05 +00001601 trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
dan5be60c52018-08-15 20:28:39 +00001602 A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
drh2c2e8442018-04-07 15:04:05 +00001603}
danielk1977c3f9bad2002-05-15 08:30:12 +00001604// DELETE
drhf259df52017-12-27 20:38:35 +00001605trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
dan5be60c52018-08-15 20:28:39 +00001606 {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
danielk1977c3f9bad2002-05-15 08:30:12 +00001607
1608// SELECT
drhf259df52017-12-27 20:38:35 +00001609trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
1610 {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
danielk1977c3f9bad2002-05-15 08:30:12 +00001611
danielk19776f349032002-06-11 02:25:40 +00001612// The special RAISE expression that may occur in trigger programs
drh1be266b2017-12-24 00:18:47 +00001613expr(A) ::= RAISE LP IGNORE RP. {
1614 A = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
1615 if( A ){
drh11949042019-08-05 18:01:42 +00001616 A->affExpr = OE_Ignore;
drh8aa34ae2006-03-13 12:54:09 +00001617 }
drh4b59ab52002-08-24 18:24:51 +00001618}
drh1be266b2017-12-24 00:18:47 +00001619expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP. {
drh796588a2022-02-05 21:49:47 +00001620 A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
drh1be266b2017-12-24 00:18:47 +00001621 if( A ) {
drh11949042019-08-05 18:01:42 +00001622 A->affExpr = (char)T;
drh8aa34ae2006-03-13 12:54:09 +00001623 }
drh4b59ab52002-08-24 18:24:51 +00001624}
drh154d4b22006-09-21 11:02:16 +00001625%endif !SQLITE_OMIT_TRIGGER
drhb7f91642004-10-31 02:22:47 +00001626
drh74ad7fe2004-10-07 03:06:28 +00001627%type raisetype {int}
1628raisetype(A) ::= ROLLBACK. {A = OE_Rollback;}
1629raisetype(A) ::= ABORT. {A = OE_Abort;}
1630raisetype(A) ::= FAIL. {A = OE_Fail;}
1631
danielk19776f349032002-06-11 02:25:40 +00001632
danielk1977c3f9bad2002-05-15 08:30:12 +00001633//////////////////////// DROP TRIGGER statement //////////////////////////////
drhb7f91642004-10-31 02:22:47 +00001634%ifndef SQLITE_OMIT_TRIGGER
drhfdd48a72006-09-11 23:45:48 +00001635cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
1636 sqlite3DropTrigger(pParse,X,NOERR);
danielk1977c3f9bad2002-05-15 08:30:12 +00001637}
drh154d4b22006-09-21 11:02:16 +00001638%endif !SQLITE_OMIT_TRIGGER
drh113088e2003-03-20 01:16:58 +00001639
1640//////////////////////// ATTACH DATABASE file AS name /////////////////////////
drhfdbcdee2007-03-27 14:44:50 +00001641%ifndef SQLITE_OMIT_ATTACH
danielk1977f744bb52005-12-06 17:19:11 +00001642cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
drh1be266b2017-12-24 00:18:47 +00001643 sqlite3Attach(pParse, F, D, K);
drh1c2d8412003-03-31 00:30:47 +00001644}
drhfdbcdee2007-03-27 14:44:50 +00001645cmd ::= DETACH database_kw_opt expr(D). {
drh1be266b2017-12-24 00:18:47 +00001646 sqlite3Detach(pParse, D);
drhfdbcdee2007-03-27 14:44:50 +00001647}
1648
drhc4dd3fd2008-01-22 01:48:05 +00001649%type key_opt {Expr*}
drh633e6d52008-07-28 19:34:53 +00001650%destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
danielk1977f744bb52005-12-06 17:19:11 +00001651key_opt(A) ::= . { A = 0; }
drh1be266b2017-12-24 00:18:47 +00001652key_opt(A) ::= KEY expr(X). { A = X; }
drh113088e2003-03-20 01:16:58 +00001653
1654database_kw_opt ::= DATABASE.
1655database_kw_opt ::= .
drhfdbcdee2007-03-27 14:44:50 +00001656%endif SQLITE_OMIT_ATTACH
drh4343fea2004-11-05 23:46:15 +00001657
1658////////////////////////// REINDEX collation //////////////////////////////////
1659%ifndef SQLITE_OMIT_REINDEX
1660cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);}
1661cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);}
drh154d4b22006-09-21 11:02:16 +00001662%endif SQLITE_OMIT_REINDEX
danielk19779fd2a9a2004-11-12 13:42:30 +00001663
drh9f18e8a2005-07-08 12:13:04 +00001664/////////////////////////////////// ANALYZE ///////////////////////////////////
1665%ifndef SQLITE_OMIT_ANALYZE
1666cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);}
1667cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);}
1668%endif
1669
danielk19779fd2a9a2004-11-12 13:42:30 +00001670//////////////////////// ALTER TABLE table ... ////////////////////////////////
dan37f3ac82021-10-01 20:39:50 +00001671%ifndef SQLITE_OMIT_ALTERTABLE
1672%ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19779fd2a9a2004-11-12 13:42:30 +00001673cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
1674 sqlite3AlterRenameTable(pParse,X,&Z);
1675}
drh986dde72016-02-29 13:37:21 +00001676cmd ::= ALTER TABLE add_column_fullname
1677 ADD kwcolumn_opt columnname(Y) carglist. {
1678 Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
danielk197719a8e7e2005-03-17 05:03:38 +00001679 sqlite3AlterFinishAddColumn(pParse, &Y);
1680}
drh329cb9e2021-02-19 09:36:50 +00001681cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). {
dan6e6d9832021-02-16 20:43:36 +00001682 sqlite3AlterDropColumn(pParse, X, &Y);
1683}
1684
danielk197719a8e7e2005-03-17 05:03:38 +00001685add_column_fullname ::= fullname(X). {
drh4a642b62016-02-05 01:55:27 +00001686 disableLookaside(pParse);
danielk197719a8e7e2005-03-17 05:03:38 +00001687 sqlite3AlterBeginAddColumn(pParse, X);
1688}
dancf8f2892018-08-09 20:47:01 +00001689cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
1690 sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
1691}
1692
danielk197719a8e7e2005-03-17 05:03:38 +00001693kwcolumn_opt ::= .
1694kwcolumn_opt ::= COLUMNKW.
dancf8f2892018-08-09 20:47:01 +00001695
dan37f3ac82021-10-01 20:39:50 +00001696%endif SQLITE_OMIT_VIRTUALTABLE
1697%endif SQLITE_OMIT_ALTERTABLE
drhe09daa92006-06-10 13:29:31 +00001698
1699//////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1700%ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001701cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);}
1702cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);}
drhb421b892012-01-28 19:41:53 +00001703create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
1704 nm(X) dbnm(Y) USING nm(Z). {
1705 sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
drhb9bb7c12006-06-11 23:41:55 +00001706}
drhe09daa92006-06-10 13:29:31 +00001707vtabarglist ::= vtabarg.
1708vtabarglist ::= vtabarglist COMMA vtabarg.
drhb9bb7c12006-06-11 23:41:55 +00001709vtabarg ::= . {sqlite3VtabArgInit(pParse);}
1710vtabarg ::= vtabarg vtabargtoken.
1711vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);}
1712vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);}
1713lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);}
1714anylist ::= .
drhaaac8b42009-05-11 18:22:30 +00001715anylist ::= anylist LP anylist RP.
1716anylist ::= anylist ANY.
drh154d4b22006-09-21 11:02:16 +00001717%endif SQLITE_OMIT_VIRTUALTABLE
drh8b471862014-01-11 13:22:17 +00001718
1719
1720//////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
dan7d562db2014-01-11 19:19:36 +00001721%type wqlist {With*}
dan4e9119d2014-01-13 15:12:23 +00001722%destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
drhf824b412021-02-20 14:57:16 +00001723%type wqitem {Cte*}
1724// %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable
dan7d562db2014-01-11 19:19:36 +00001725
drha5746e02018-04-09 20:36:09 +00001726with ::= .
drh8b471862014-01-11 13:22:17 +00001727%ifndef SQLITE_OMIT_CTE
drha5746e02018-04-09 20:36:09 +00001728with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); }
1729with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); }
dan7d562db2014-01-11 19:19:36 +00001730
drh745912e2021-02-22 03:04:25 +00001731%type wqas {u8}
1732wqas(A) ::= AS. {A = M10d_Any;}
1733wqas(A) ::= AS MATERIALIZED. {A = M10d_Yes;}
1734wqas(A) ::= AS NOT MATERIALIZED. {A = M10d_No;}
1735wqitem(A) ::= nm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. {
1736 A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/
dan7d562db2014-01-11 19:19:36 +00001737}
drhf824b412021-02-20 14:57:16 +00001738wqlist(A) ::= wqitem(X). {
1739 A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/
1740}
1741wqlist(A) ::= wqlist(A) COMMA wqitem(X). {
1742 A = sqlite3WithAdd(pParse, A, X);
drh8b471862014-01-11 13:22:17 +00001743}
1744%endif SQLITE_OMIT_CTE
dan34a7d792018-06-29 20:43:33 +00001745
1746//////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
dan6e2210e2018-06-30 18:54:56 +00001747// These must be at the end of this file. Specifically, the rules that
1748// introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1749// the integer values assigned to these tokens to be larger than all other
1750// tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
dan34a7d792018-06-29 20:43:33 +00001751//
1752%ifndef SQLITE_OMIT_WINDOWFUNC
1753%type windowdefn_list {Window*}
drha57aac22018-07-09 16:24:00 +00001754%destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
dan34a7d792018-06-29 20:43:33 +00001755windowdefn_list(A) ::= windowdefn(Z). { A = Z; }
1756windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
drha57aac22018-07-09 16:24:00 +00001757 assert( Z!=0 );
dane7c9ca42019-02-16 17:27:51 +00001758 sqlite3WindowChain(pParse, Z, Y);
drha57aac22018-07-09 16:24:00 +00001759 Z->pNextWin = Y;
dan34a7d792018-06-29 20:43:33 +00001760 A = Z;
1761}
1762
1763%type windowdefn {Window*}
1764%destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
dane7c9ca42019-02-16 17:27:51 +00001765windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
drha57aac22018-07-09 16:24:00 +00001766 if( ALWAYS(Y) ){
dan34a7d792018-06-29 20:43:33 +00001767 Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
1768 }
1769 A = Y;
1770}
1771
dan34a7d792018-06-29 20:43:33 +00001772%type window {Window*}
1773%destructor window {sqlite3WindowDelete(pParse->db, $$);}
1774
1775%type frame_opt {Window*}
1776%destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
1777
dan34a7d792018-06-29 20:43:33 +00001778%type part_opt {ExprList*}
1779%destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
1780
dan64882712019-07-11 18:43:33 +00001781%type filter_clause {Expr*}
1782%destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
1783
1784%type over_clause {Window*}
1785%destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
dan34a7d792018-06-29 20:43:33 +00001786
dan4f9adee2019-07-13 16:22:50 +00001787%type filter_over {Window*}
1788%destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
1789
dan34a7d792018-06-29 20:43:33 +00001790%type range_or_rows {int}
1791
1792%type frame_bound {struct FrameBound}
1793%destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
dan287fa172018-07-06 13:48:09 +00001794%type frame_bound_s {struct FrameBound}
1795%destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
1796%type frame_bound_e {struct FrameBound}
1797%destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
dan34a7d792018-06-29 20:43:33 +00001798
dane7c9ca42019-02-16 17:27:51 +00001799window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1800 A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
1801}
1802window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
1803 A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
1804}
1805window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
1806 A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
1807}
1808window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
1809 A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
1810}
1811window(A) ::= frame_opt(Z). {
dan34a7d792018-06-29 20:43:33 +00001812 A = Z;
dane7c9ca42019-02-16 17:27:51 +00001813}
1814window(A) ::= nm(W) frame_opt(Z). {
1815 A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
dan34a7d792018-06-29 20:43:33 +00001816}
1817
dan34a7d792018-06-29 20:43:33 +00001818frame_opt(A) ::= . {
dand35300f2019-03-14 20:53:21 +00001819 A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
dan34a7d792018-06-29 20:43:33 +00001820}
dand35300f2019-03-14 20:53:21 +00001821frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). {
1822 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
dan34a7d792018-06-29 20:43:33 +00001823}
drh0f134f02019-04-02 18:12:20 +00001824frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
1825 frame_bound_e(Z) frame_exclude_opt(W). {
dand35300f2019-03-14 20:53:21 +00001826 A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
dan34a7d792018-06-29 20:43:33 +00001827}
1828
drh0f134f02019-04-02 18:12:20 +00001829range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/}
dan34a7d792018-06-29 20:43:33 +00001830
drh0f134f02019-04-02 18:12:20 +00001831frame_bound_s(A) ::= frame_bound(X). {A = X;}
1832frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
1833frame_bound_e(A) ::= frame_bound(X). {A = X;}
1834frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
dan287fa172018-07-06 13:48:09 +00001835
drh0f134f02019-04-02 18:12:20 +00001836frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
1837 {A.eType = @Y; A.pExpr = X;}
1838frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;}
dan34a7d792018-06-29 20:43:33 +00001839
dand35300f2019-03-14 20:53:21 +00001840%type frame_exclude_opt {u8}
drh0f134f02019-04-02 18:12:20 +00001841frame_exclude_opt(A) ::= . {A = 0;}
1842frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
dand35300f2019-03-14 20:53:21 +00001843
1844%type frame_exclude {u8}
drh0f134f02019-04-02 18:12:20 +00001845frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/}
1846frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
1847frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/}
dand35300f2019-03-14 20:53:21 +00001848
1849
drh550a3302018-07-27 22:14:50 +00001850%type window_clause {Window*}
1851%destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
1852window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
dan6e2210e2018-06-30 18:54:56 +00001853
dan4f9adee2019-07-13 16:22:50 +00001854filter_over(A) ::= filter_clause(F) over_clause(O). {
drhd4f7ec72021-04-08 14:15:26 +00001855 if( O ){
1856 O->pFilter = F;
1857 }else{
1858 sqlite3ExprDelete(pParse->db, F);
1859 }
dan4f9adee2019-07-13 16:22:50 +00001860 A = O;
dan64882712019-07-11 18:43:33 +00001861}
dan4f9adee2019-07-13 16:22:50 +00001862filter_over(A) ::= over_clause(O). {
1863 A = O;
dan64882712019-07-11 18:43:33 +00001864}
dan4f9adee2019-07-13 16:22:50 +00001865filter_over(A) ::= filter_clause(F). {
1866 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1867 if( A ){
1868 A->eFrmType = TK_FILTER;
1869 A->pFilter = F;
dan00885742019-07-13 18:27:54 +00001870 }else{
1871 sqlite3ExprDelete(pParse->db, F);
dan4f9adee2019-07-13 16:22:50 +00001872 }
dan64882712019-07-11 18:43:33 +00001873}
1874
1875over_clause(A) ::= OVER LP window(Z) RP. {
dan6e2210e2018-06-30 18:54:56 +00001876 A = Z;
drha57aac22018-07-09 16:24:00 +00001877 assert( A!=0 );
drha57aac22018-07-09 16:24:00 +00001878}
dan64882712019-07-11 18:43:33 +00001879over_clause(A) ::= OVER nm(Z). {
drha57aac22018-07-09 16:24:00 +00001880 A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
1881 if( A ){
1882 A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
drha57aac22018-07-09 16:24:00 +00001883 }
dan6e2210e2018-06-30 18:54:56 +00001884}
1885
dan64882712019-07-11 18:43:33 +00001886filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; }
drhc7bf5712018-07-09 22:49:01 +00001887%endif /* SQLITE_OMIT_WINDOWFUNC */
drhf1722ba2019-04-05 20:56:46 +00001888
1889/*
1890** The code generator needs some extra TK_ token values for tokens that
1891** are synthesized and do not actually appear in the grammar:
1892*/
1893%token
drhf1722ba2019-04-05 20:56:46 +00001894 COLUMN /* Reference to a table column */
1895 AGG_FUNCTION /* An aggregate function */
1896 AGG_COLUMN /* An aggregated column */
danee6c5e52019-08-23 20:33:01 +00001897 TRUEFALSE /* True or false keyword */
1898 ISNOT /* Combination of IS and NOT */
1899 FUNCTION /* A function invocation */
drhf1722ba2019-04-05 20:56:46 +00001900 UMINUS /* Unary minus */
1901 UPLUS /* Unary plus */
1902 TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
1903 REGISTER /* Reference to a VDBE register */
1904 VECTOR /* Vector */
1905 SELECT_COLUMN /* Choose a single column from a multi-column SELECT */
1906 IF_NULL_ROW /* the if-null-row operator */
1907 ASTERISK /* The "*" in count(*) and similar */
1908 SPAN /* The span operator */
drh05428122021-05-24 00:17:04 +00001909 ERROR /* An expression containing an error */
drhf1722ba2019-04-05 20:56:46 +00001910.
1911/* There must be no more than 255 tokens defined above. If this grammar
1912** is extended with new rules and tokens, they must either be so few in
1913** number that TK_SPAN is no more than 255, or else the new tokens must
1914** appear after this line.
1915*/
1916%include {
1917#if TK_SPAN>255
1918# error too many tokens in the grammar
1919#endif
1920}
1921
1922/*
1923** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens. The
1924** parser depends on this. Those tokens are not used in any grammar rule.
1925** They are only used by the tokenizer. Declare them last so that they
1926** are guaranteed to be the last two tokens
1927*/
1928%token SPACE ILLEGAL.