5 ** The author disclaims copyright to this source code. In place of
6 ** a legal notice, here is a blessing:
8 ** May you do good and not evil.
9 ** May you find forgiveness for yourself and forgive others.
10 ** May you share freely, never taking more than you give.
12 *************************************************************************
13 ** This file contains SQLite's SQL parser.
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 grammar. 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.
24 // Setup for the parser stack
25 %stack_size
50 // Initial stack size
26 %stack_size_limit parserStackSizeLimit
// Function returning max stack size
27 %realloc parserStackRealloc
// realloc() for the stack
28 %free parserStackFree
// free() for the stack
30 // All token codes are small integers with #defines that begin with "TK_"
33 // The type of the data attached to each token is Token. This is also the
34 // default type for non-terminals.
39 // An extra argument to the constructor for the parser, which is available
41 %extra_context
{Parse
*pParse
}
43 // This code runs whenever there is a syntax error
46 UNUSED_PARAMETER
(yymajor
); /* Silence some compiler warnings */
48 parserSyntaxError
(pParse
, &TOKEN
);
50 sqlite3ErrorMsg
(pParse
, "incomplete input");
54 if
( pParse
->nErr
==0 ) sqlite3ErrorMsg
(pParse
, "Recursion limit");
57 // The name of the generated procedure that implements the parser
61 // The following text is included near the beginning of the C source
62 // code file that implements the parser.
65 #include "sqliteInt.h"
68 ** Verify that the pParse->isCreate field is set
70 #define ASSERT_IS_CREATE assert(pParse->isCreate)
73 ** Disable all error recovery processing in the parser push-down
76 #define YYNOERRORRECOVERY 1
79 ** Make yytestcase() the same as testcase()
81 #define yytestcase(X) testcase(X)
84 ** Indicate that sqlite3ParserFree() will never be called with a null
87 #define YYPARSEFREENEVERNULL 1
90 ** In the amalgamation, the parse.c file generated by lemon and the
91 ** tokenize.c file are concatenated. In that case, sqlite3RunParser()
92 ** has access to the the size of the yyParser object and so the parser
93 ** engine can be allocated from stack. In that case, only the
94 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
95 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
98 #ifdef SQLITE_AMALGAMATION
99 # define sqlite3Parser_ENGINEALWAYSONSTACK 1
103 ** Alternative datatype for the argument to the malloc() routine passed
104 ** into sqlite3ParserAlloc(). The default is size_t.
106 #define YYMALLOCARGTYPE u64
109 ** An instance of the following structure describes the event of a
110 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
111 ** TK_DELETE, or TK_INSTEAD. If the event is of the form
115 ** Then the "b" IdList records the list "a,b,c".
117 struct TrigEvent
{ int a
; IdList
* b
; };
119 struct FrameBound
{ int eType
; Expr
*pExpr
; };
122 ** Generate a syntax error
124 static void parserSyntaxError
(Parse
*pParse
, Token
*p
){
125 sqlite3ErrorMsg
(pParse
, "near \"%T\": syntax error", p
);
129 ** Disable lookaside memory allocation for objects that might be
130 ** shared across database connections.
132 static void disableLookaside
(Parse
*pParse
){
133 sqlite3
*db
= pParse
->db
;
134 pParse
->disableLookaside
++;
136 pParse
->isCreate
= 1;
138 memset
(&pParse
->u1.cr
, 0, sizeof
(pParse
->u1.cr
));
142 #if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \
143 && defined
(SQLITE_UDL_CAPABLE_PARSER
)
145 ** Issue an error message if an ORDER BY or LIMIT clause occurs on an
146 ** UPDATE or DELETE statement.
148 static void updateDeleteLimitError
(
154 sqlite3ErrorMsg
(pParse
, "syntax error near \"ORDER BY\"");
156 sqlite3ErrorMsg
(pParse
, "syntax error near \"LIMIT\"");
158 sqlite3ExprListDelete
(pParse
->db
, pOrderBy
);
159 sqlite3ExprDelete
(pParse
->db
, pLimit
);
161 #endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
165 // Input is a single SQL command
167 cmdlist
::= cmdlist ecmd.
171 %ifndef SQLITE_OMIT_EXPLAIN
172 ecmd
::= explain cmdx SEMI.
{NEVER
-REDUCE
}
173 explain
::= EXPLAIN.
{ if
( pParse
->pReprepare
==0 ) pParse
->explain
= 1; }
174 explain
::= EXPLAIN QUERY PLAN.
{ if
( pParse
->pReprepare
==0 ) pParse
->explain
= 2; }
175 %endif SQLITE_OMIT_EXPLAIN
176 cmdx
::= cmd.
{ sqlite3FinishCoding
(pParse
); }
178 ///////////////////// Begin and end transactions. ////////////////////////////
181 cmd
::= BEGIN transtype
(Y
) trans_opt.
{sqlite3BeginTransaction
(pParse
, Y
);}
183 trans_opt
::= TRANSACTION.
184 trans_opt
::= TRANSACTION nm.
185 %type transtype
{int}
186 transtype
(A
) ::= .
{A
= TK_DEFERRED
;}
187 transtype
(A
) ::= DEFERRED
(X
).
{A
= @X
; /*A-overwrites-X*/}
188 transtype
(A
) ::= IMMEDIATE
(X
).
{A
= @X
; /*A-overwrites-X*/}
189 transtype
(A
) ::= EXCLUSIVE
(X
).
{A
= @X
; /*A-overwrites-X*/}
190 cmd
::= COMMIT|END
(X
) trans_opt.
{sqlite3EndTransaction
(pParse
,@X
);}
191 cmd
::= ROLLBACK
(X
) trans_opt.
{sqlite3EndTransaction
(pParse
,@X
);}
193 savepoint_opt
::= SAVEPOINT.
195 cmd
::= SAVEPOINT nm
(X
).
{
196 sqlite3Savepoint
(pParse
, SAVEPOINT_BEGIN
, &X
);
198 cmd
::= RELEASE savepoint_opt nm
(X
).
{
199 sqlite3Savepoint
(pParse
, SAVEPOINT_RELEASE
, &X
);
201 cmd
::= ROLLBACK trans_opt TO savepoint_opt nm
(X
).
{
202 sqlite3Savepoint
(pParse
, SAVEPOINT_ROLLBACK
, &X
);
205 ///////////////////// The CREATE TABLE statement ////////////////////////////
207 cmd
::= create_table create_table_args.
208 create_table
::= createkw temp
(T
) TABLE ifnotexists
(E
) nm
(Y
) dbnm
(Z
).
{
209 sqlite3StartTable
(pParse
,&Y
,&Z
,T
,0,0,E
);
211 createkw
(A
) ::= CREATE
(A
).
{
212 disableLookaside
(pParse
);
215 %type ifnotexists
{int}
216 ifnotexists
(A
) ::= .
{A
= 0;}
217 ifnotexists
(A
) ::= IF NOT EXISTS.
{A
= 1;}
219 %ifndef SQLITE_OMIT_TEMPDB
220 temp
(A
) ::= TEMP.
{A
= pParse
->db
->init.busy
==0;}
221 %endif SQLITE_OMIT_TEMPDB
222 temp
(A
) ::= .
{A
= 0;}
223 create_table_args
::= LP columnlist conslist_opt
(X
) RP
(E
) table_option_set
(F
).
{
224 sqlite3EndTable
(pParse
,&X
,&E
,F
,0);
226 create_table_args
::= AS select
(S
).
{
227 sqlite3EndTable
(pParse
,0,0,0,S
);
228 sqlite3SelectDelete
(pParse
->db
, S
);
230 %type table_option_set
{u32
}
231 %type table_option
{u32
}
232 table_option_set
(A
) ::= .
{A
= 0;}
233 table_option_set
(A
) ::= table_option
(A
).
234 table_option_set
(A
) ::= table_option_set
(X
) COMMA table_option
(Y
).
{A
= X|Y
;}
235 table_option
(A
) ::= WITHOUT nm
(X
).
{
236 if
( X.n
==5 && sqlite3_strnicmp
(X.z
,"rowid",5)==0 ){
237 A
= TF_WithoutRowid | TF_NoVisibleRowid
;
240 sqlite3ErrorMsg
(pParse
, "unknown table option: %.*s", X.n
, X.z
);
243 table_option
(A
) ::= nm
(X
).
{
244 if
( X.n
==6 && sqlite3_strnicmp
(X.z
,"strict",6)==0 ){
248 sqlite3ErrorMsg
(pParse
, "unknown table option: %.*s", X.n
, X.z
);
251 columnlist
::= columnlist COMMA columnname carglist.
252 columnlist
::= columnname carglist.
253 columnname
(A
) ::= nm
(A
) typetoken
(Y
).
{sqlite3AddColumn
(pParse
,A
,Y
);}
255 // Declare some tokens early in order to influence their values, to
256 // improve performance and reduce the executable size. The goal here is
257 // to get the "jump" operations in ISNULL through ESCAPE to have numeric
258 // values that are early enough so that all jump operations are clustered
259 // at the beginning. Also, operators like NE and EQ need to be adjacent,
260 // and all of the comparison operators need to be clustered together.
261 // Various assert() statements throughout the code enforce these restrictions.
263 %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
264 %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
265 %token OR AND NOT IS ISNOT MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
266 %token GT LE LT GE ESCAPE.
268 // The following directive causes tokens ABORT, AFTER, ASC, etc. to
269 // fallback to ID if they will not parse as their original value.
270 // This obviates the need for the "id" nonterminal.
273 ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
274 CONFLICT DATABASE DEFERRED DESC DETACH DO
275 EACH END EXCLUSIVE EXPLAIN FAIL FOR
276 IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
277 QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
278 ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
280 %ifdef SQLITE_OMIT_COMPOUND_SELECT
281 EXCEPT INTERSECT UNION
282 %endif SQLITE_OMIT_COMPOUND_SELECT
283 %ifndef SQLITE_OMIT_WINDOWFUNC
284 CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED
285 EXCLUDE GROUPS OTHERS TIES
286 %endif SQLITE_OMIT_WINDOWFUNC
287 %ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES
289 %endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES
290 %ifndef SQLITE_OMIT_GENERATED_COLUMNS
294 REINDEX RENAME CTIME_KW IF
298 // Define operator precedence early so that this is the first occurrence
299 // of the operator tokens in the grammar. Keeping the operators together
300 // causes them to be assigned integer values that are close together,
301 // which keeps parser tables smaller.
303 // The token values assigned to these symbols is determined by the order
304 // in which lemon first sees them. It must be the case that ISNULL/NOTNULL,
305 // NE/EQ, GT/LE, and GE/LT are separated by only a single value. See
306 // the sqlite3ExprIfFalse() routine for additional information on this
312 %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
315 %left BITAND BITOR LSHIFT RSHIFT.
317 %left STAR SLASH REM.
323 // An IDENTIFIER can be a generic identifier, or one of several
324 // keywords. Any non-standard keyword can also be an identifier.
326 %token_class id ID|INDEXED.
328 // And "ids" is an identifier-or-string.
330 %token_class ids ID|STRING.
332 // An identifier or a join-keyword
334 %token_class idj ID|INDEXED|JOIN_KW.
336 // The name of a column or table can be any of the following:
342 // A typetoken is really zero or more tokens that form a type name such
343 // as can be found after the column name in a CREATE TABLE statement.
344 // Multiple tokens are concatenated to form the value of the typetoken.
346 %type typetoken
{Token
}
347 typetoken
(A
) ::= .
{A.n
= 0; A.z
= 0;}
348 typetoken
(A
) ::= typename
(A
).
349 typetoken
(A
) ::= typename
(A
) LP
signed RP
(Y
).
{
350 A.n
= (int)(&Y.z
[Y.n
] - A.z
);
352 typetoken
(A
) ::= typename
(A
) LP
signed COMMA
signed RP
(Y
).
{
353 A.n
= (int)(&Y.z
[Y.n
] - A.z
);
355 %type typename
{Token
}
356 typename
(A
) ::= ids
(A
).
357 typename
(A
) ::= typename
(A
) ids
(Y
).
{A.n
=Y.n
+(int)(Y.z
-A.z
);}
359 signed ::= minus_num.
361 // The scanpt non-terminal takes a value which is a pointer to the
362 // input text just past the last token that has been shifted into
363 // the parser. By surrounding some phrase in the grammar with two
364 // scanpt non-terminals, we can capture the input text for that phrase.
367 // something ::= .... scanpt(A) phrase scanpt(Z).
369 // The text that is parsed as "phrase" is a string starting at A
370 // and containing (int)(Z-A) characters. There might be some extra
371 // whitespace on either end of the text, but that can be removed in
372 // post-processing, if needed.
374 %type scanpt
{const char*}
376 assert
( yyLookahead
!=YYNOCODE
);
377 A
= yyLookaheadToken.z
;
380 assert
( yyLookahead
!=YYNOCODE
);
381 A
= yyLookaheadToken
;
384 // "carglist" is a list of additional constraints that come after the
385 // column name and column type in a CREATE TABLE statement.
387 carglist
::= carglist ccons.
389 ccons
::= CONSTRAINT nm
(X
).
{ASSERT_IS_CREATE
; pParse
->u1.cr.constraintName
= X
;}
390 ccons
::= DEFAULT scantok
(A
) term
(X
).
391 {sqlite3AddDefaultValue
(pParse
,X
,A.z
,&A.z
[A.n
]);}
392 ccons
::= DEFAULT LP
(A
) expr
(X
) RP
(Z
).
393 {sqlite3AddDefaultValue
(pParse
,X
,A.z
+1,Z.z
);}
394 ccons
::= DEFAULT PLUS
(A
) scantok
(Z
) term
(X
).
395 {sqlite3AddDefaultValue
(pParse
,X
,A.z
,&Z.z
[Z.n
]);}
396 ccons
::= DEFAULT MINUS
(A
) scantok
(Z
) term
(X
).
{
397 Expr
*p
= sqlite3PExpr
(pParse
, TK_UMINUS
, X
, 0);
398 sqlite3AddDefaultValue
(pParse
,p
,A.z
,&Z.z
[Z.n
]);
400 ccons
::= DEFAULT scantok id
(X
).
{
401 Expr
*p
= tokenExpr
(pParse
, TK_STRING
, X
);
403 sqlite3ExprIdToTrueFalse
(p
);
404 testcase
( p
->op
==TK_TRUEFALSE
&& sqlite3ExprTruthValue
(p
) );
406 sqlite3AddDefaultValue
(pParse
,p
,X.z
,X.z
+X.n
);
409 // In addition to the type name, we also care about the primary key and
410 // UNIQUE constraints.
412 ccons
::= NULL onconf.
413 ccons
::= NOT NULL onconf
(R
).
{sqlite3AddNotNull
(pParse
, R
);}
414 ccons
::= PRIMARY KEY sortorder
(Z
) onconf
(R
) autoinc
(I
).
415 {sqlite3AddPrimaryKey
(pParse
,0,R
,I
,Z
);}
416 ccons
::= UNIQUE onconf
(R
).
{sqlite3CreateIndex
(pParse
,0,0,0,0,R
,0,0,0,0,
417 SQLITE_IDXTYPE_UNIQUE
);}
418 ccons
::= CHECK LP
(A
) expr
(X
) RP
(B
).
{sqlite3AddCheckConstraint
(pParse
,X
,A.z
,B.z
);}
419 ccons
::= REFERENCES nm
(T
) eidlist_opt
(TA
) refargs
(R
).
420 {sqlite3CreateForeignKey
(pParse
,0,&T
,TA
,R
);}
421 ccons
::= defer_subclause
(D
).
{sqlite3DeferForeignKey
(pParse
,D
);}
422 ccons
::= COLLATE ids
(C
).
{sqlite3AddCollateType
(pParse
, &C
);}
423 ccons
::= GENERATED ALWAYS AS generated.
424 ccons
::= AS generated.
425 generated
::= LP expr
(E
) RP.
{sqlite3AddGenerated
(pParse
,E
,0);}
426 generated
::= LP expr
(E
) RP ID
(TYPE
).
{sqlite3AddGenerated
(pParse
,E
,&TYPE
);}
428 // The optional AUTOINCREMENT keyword
430 autoinc
(X
) ::= .
{X
= 0;}
431 autoinc
(X
) ::= AUTOINCR.
{X
= 1;}
433 // The next group of rules parses the arguments to a REFERENCES clause
434 // that determine if the referential integrity checking is deferred or
435 // or immediate and which determine what action to take if a ref-integ
439 refargs
(A
) ::= .
{ A
= OE_None
*0x0101; /* EV: R-19803-45884 */}
440 refargs
(A
) ::= refargs
(A
) refarg
(Y
).
{ A
= (A
& ~Y.mask
) | Y.value
; }
441 %type refarg
{struct {int value
; int mask
;}}
442 refarg
(A
) ::= MATCH nm.
{ A.value
= 0; A.mask
= 0x000000; }
443 refarg
(A
) ::= ON INSERT refact.
{ A.value
= 0; A.mask
= 0x000000; }
444 refarg
(A
) ::= ON DELETE refact
(X
).
{ A.value
= X
; A.mask
= 0x0000ff; }
445 refarg
(A
) ::= ON UPDATE refact
(X
).
{ A.value
= X
<<8; A.mask
= 0x00ff00; }
447 refact
(A
) ::= SET NULL.
{ A
= OE_SetNull
; /* EV: R-33326-45252 */}
448 refact
(A
) ::= SET DEFAULT.
{ A
= OE_SetDflt
; /* EV: R-33326-45252 */}
449 refact
(A
) ::= CASCADE.
{ A
= OE_Cascade
; /* EV: R-33326-45252 */}
450 refact
(A
) ::= RESTRICT.
{ A
= OE_Restrict
; /* EV: R-33326-45252 */}
451 refact
(A
) ::= NO ACTION.
{ A
= OE_None
; /* EV: R-33326-45252 */}
452 %type defer_subclause
{int}
453 defer_subclause
(A
) ::= NOT DEFERRABLE init_deferred_pred_opt.
{A
= 0;}
454 defer_subclause
(A
) ::= DEFERRABLE init_deferred_pred_opt
(X
).
{A
= X
;}
455 %type init_deferred_pred_opt
{int}
456 init_deferred_pred_opt
(A
) ::= .
{A
= 0;}
457 init_deferred_pred_opt
(A
) ::= INITIALLY DEFERRED.
{A
= 1;}
458 init_deferred_pred_opt
(A
) ::= INITIALLY IMMEDIATE.
{A
= 0;}
460 conslist_opt
(A
) ::= .
{A.n
= 0; A.z
= 0;}
461 conslist_opt
(A
) ::= COMMA
(A
) conslist.
462 conslist
::= conslist tconscomma tcons.
464 tconscomma
::= COMMA.
{ASSERT_IS_CREATE
; pParse
->u1.cr.constraintName.n
= 0;}
466 tcons
::= CONSTRAINT nm
(X
).
{ASSERT_IS_CREATE
; pParse
->u1.cr.constraintName
= X
;}
467 tcons
::= PRIMARY KEY LP sortlist
(X
) autoinc
(I
) RP onconf
(R
).
468 {sqlite3AddPrimaryKey
(pParse
,X
,R
,I
,0);}
469 tcons
::= UNIQUE LP sortlist
(X
) RP onconf
(R
).
470 {sqlite3CreateIndex
(pParse
,0,0,0,X
,R
,0,0,0,0,
471 SQLITE_IDXTYPE_UNIQUE
);}
472 tcons
::= CHECK LP
(A
) expr
(E
) RP
(B
) onconf.
473 {sqlite3AddCheckConstraint
(pParse
,E
,A.z
,B.z
);}
474 tcons
::= FOREIGN KEY LP eidlist
(FA
) RP
475 REFERENCES nm
(T
) eidlist_opt
(TA
) refargs
(R
) defer_subclause_opt
(D
).
{
476 sqlite3CreateForeignKey
(pParse
, FA
, &T
, TA
, R
);
477 sqlite3DeferForeignKey
(pParse
, D
);
479 %type defer_subclause_opt
{int}
480 defer_subclause_opt
(A
) ::= .
{A
= 0;}
481 defer_subclause_opt
(A
) ::= defer_subclause
(A
).
483 // The following is a non-standard extension that allows us to declare the
484 // default behavior when there is a constraint conflict.
488 %type resolvetype
{int}
489 onconf
(A
) ::= .
{A
= OE_Default
;}
490 onconf
(A
) ::= ON CONFLICT resolvetype
(X
).
{A
= X
;}
491 orconf
(A
) ::= .
{A
= OE_Default
;}
492 orconf
(A
) ::= OR resolvetype
(X
).
{A
= X
;}
493 resolvetype
(A
) ::= raisetype
(A
).
494 resolvetype
(A
) ::= IGNORE.
{A
= OE_Ignore
;}
495 resolvetype
(A
) ::= REPLACE.
{A
= OE_Replace
;}
497 ////////////////////////// The DROP TABLE /////////////////////////////////////
499 cmd
::= DROP TABLE ifexists
(E
) fullname
(X
).
{
500 sqlite3DropTable
(pParse
, X
, 0, E
);
503 ifexists
(A
) ::= IF EXISTS.
{A
= 1;}
504 ifexists
(A
) ::= .
{A
= 0;}
506 ///////////////////// The CREATE VIEW statement /////////////////////////////
508 %ifndef SQLITE_OMIT_VIEW
509 cmd
::= createkw
(X
) temp
(T
) VIEW ifnotexists
(E
) nm
(Y
) dbnm
(Z
) eidlist_opt
(C
)
511 sqlite3CreateView
(pParse
, &X
, &Y
, &Z
, C
, S
, T
, E
);
513 cmd
::= DROP VIEW ifexists
(E
) fullname
(X
).
{
514 sqlite3DropTable
(pParse
, X
, 1, E
);
516 %endif SQLITE_OMIT_VIEW
518 //////////////////////// The SELECT statement /////////////////////////////////
521 SelectDest dest
= {SRT_Output
, 0, 0, 0, 0, 0, 0};
522 if
( (pParse
->db
->mDbFlags
& DBFLAG_EncodingFixed
)!=0
523 || sqlite3ReadSchema
(pParse
)==SQLITE_OK
525 sqlite3Select
(pParse
, X
, &dest
);
527 sqlite3SelectDelete
(pParse
->db
, X
);
530 %type select
{Select
*}
531 %destructor select
{sqlite3SelectDelete
(pParse
->db
, $$
);}
532 %type selectnowith
{Select
*}
533 %destructor selectnowith
{sqlite3SelectDelete
(pParse
->db
, $$
);}
534 %type oneselect
{Select
*}
535 %destructor oneselect
{sqlite3SelectDelete
(pParse
->db
, $$
);}
539 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
540 ** all elements in the list. And make sure list length does not exceed
541 ** SQLITE_LIMIT_COMPOUND_SELECT.
543 static void parserDoubleLinkSelect
(Parse
*pParse
, Select
*p
){
546 Select
*pNext
= 0, *pLoop
= p
;
547 int mxSelect
, cnt
= 1;
549 pLoop
->pNext
= pNext
;
550 pLoop
->selFlags |
= SF_Compound
;
552 pLoop
= pLoop
->pPrior
;
553 if
( pLoop
==0 ) break
;
555 if
( pLoop
->pOrderBy || pLoop
->pLimit
){
556 sqlite3ErrorMsg
(pParse
,"%s clause should come after %s not before",
557 pLoop
->pOrderBy
!=0 ?
"ORDER BY" : "LIMIT",
558 sqlite3SelectOpName
(pNext
->op
));
562 if
( (p
->selFlags
& (SF_MultiValue|SF_Values
))==0
563 && (mxSelect
= pParse
->db
->aLimit
[SQLITE_LIMIT_COMPOUND_SELECT
])>0
566 sqlite3ErrorMsg
(pParse
, "too many terms in compound SELECT");
571 /* Attach a With object describing the WITH clause to a Select
572 ** object describing the query for which the WITH clause is a prefix.
574 static Select
*attachWithToSelect
(Parse
*pParse
, Select
*pSelect
, With
*pWith
){
576 pSelect
->pWith
= pWith
;
577 parserDoubleLinkSelect
(pParse
, pSelect
);
579 sqlite3WithDelete
(pParse
->db
, pWith
);
584 /* Memory allocator for parser stack resizing. This is a thin wrapper around
585 ** sqlite3_realloc() that includes a call to sqlite3FaultSim() to facilitate
588 static void *parserStackRealloc
(
589 void *pOld
, /* Prior allocation */
590 sqlite3_uint64 newSize
, /* Requested new alloation size */
591 Parse
*pParse
/* Parsing context */
593 void *p
= sqlite3FaultSim
(700) ?
0 : sqlite3_realloc
(pOld
, newSize
);
594 if
( p
==0 ) sqlite3OomFault
(pParse
->db
);
597 static void parserStackFree
(void *pOld
, Parse
*pParse
){
602 /* Return an integer that is the maximum allowed stack size */
603 static int parserStackSizeLimit
(Parse
*pParse
){
604 return pParse
->db
->aLimit
[SQLITE_LIMIT_PARSER_DEPTH
];
608 %ifndef SQLITE_OMIT_CTE
609 select
(A
) ::= WITH wqlist
(W
) selectnowith
(X
).
{A
= attachWithToSelect
(pParse
,X
,W
);}
610 select
(A
) ::= WITH RECURSIVE wqlist
(W
) selectnowith
(X
).
611 {A
= attachWithToSelect
(pParse
,X
,W
);}
613 %endif
/* SQLITE_OMIT_CTE */
614 select
(A
) ::= selectnowith
(A
).
{
617 parserDoubleLinkSelect
(pParse
, p
);
621 selectnowith
(A
) ::= oneselect
(A
).
622 %ifndef SQLITE_OMIT_COMPOUND_SELECT
623 selectnowith
(A
) ::= selectnowith
(A
) multiselect_op
(Y
) oneselect
(Z
).
{
626 if
( pRhs
&& pRhs
->pPrior
){
630 parserDoubleLinkSelect
(pParse
, pRhs
);
631 pFrom
= sqlite3SrcListAppendFromTerm
(pParse
,0,0,0,&x
,pRhs
,0);
632 pRhs
= sqlite3SelectNew
(pParse
,0,pFrom
,0,0,0,0,0,0);
637 if
( ALWAYS
(pLhs
) ) pLhs
->selFlags
&= ~
(u32
)SF_MultiValue
;
638 pRhs
->selFlags
&= ~
(u32
)SF_MultiValue
;
639 if
( Y
!=TK_ALL
) pParse
->hasCompound
= 1;
641 sqlite3SelectDelete
(pParse
->db
, pLhs
);
645 %type multiselect_op
{int}
646 multiselect_op
(A
) ::= UNION
(OP
).
{A
= @OP
; /*A-overwrites-OP*/}
647 multiselect_op
(A
) ::= UNION ALL.
{A
= TK_ALL
;}
648 multiselect_op
(A
) ::= EXCEPT|INTERSECT
(OP
).
{A
= @OP
; /*A-overwrites-OP*/}
649 %endif SQLITE_OMIT_COMPOUND_SELECT
651 oneselect
(A
) ::= SELECT distinct
(D
) selcollist
(W
) from
(X
) where_opt
(Y
)
652 groupby_opt
(P
) having_opt
(Q
)
653 orderby_opt
(Z
) limit_opt
(L
).
{
654 A
= sqlite3SelectNew
(pParse
,W
,X
,Y
,P
,Q
,Z
,D
,L
);
656 %ifndef SQLITE_OMIT_WINDOWFUNC
657 oneselect
(A
) ::= SELECT distinct
(D
) selcollist
(W
) from
(X
) where_opt
(Y
)
658 groupby_opt
(P
) having_opt
(Q
) window_clause
(R
)
659 orderby_opt
(Z
) limit_opt
(L
).
{
660 A
= sqlite3SelectNew
(pParse
,W
,X
,Y
,P
,Q
,Z
,D
,L
);
664 sqlite3WindowListDelete
(pParse
->db
, R
);
670 // Single row VALUES clause.
672 %type values
{Select
*}
673 oneselect
(A
) ::= values
(A
).
674 %destructor values
{sqlite3SelectDelete
(pParse
->db
, $$
);}
675 values
(A
) ::= VALUES LP nexprlist
(X
) RP.
{
676 A
= sqlite3SelectNew
(pParse
,X
,0,0,0,0,0,SF_Values
,0);
679 // Multiple row VALUES clause.
681 %type mvalues
{Select
*}
682 oneselect
(A
) ::= mvalues
(A
).
{
683 sqlite3MultiValuesEnd
(pParse
, A
);
685 %destructor mvalues
{sqlite3SelectDelete
(pParse
->db
, $$
);}
686 mvalues
(A
) ::= values
(A
) COMMA LP nexprlist
(Y
) RP.
{
687 A
= sqlite3MultiValues
(pParse
, A
, Y
);
689 mvalues
(A
) ::= mvalues
(A
) COMMA LP nexprlist
(Y
) RP.
{
690 A
= sqlite3MultiValues
(pParse
, A
, Y
);
693 // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
694 // present and false (0) if it is not.
697 distinct
(A
) ::= DISTINCT.
{A
= SF_Distinct
;}
698 distinct
(A
) ::= ALL.
{A
= SF_All
;}
699 distinct
(A
) ::= .
{A
= 0;}
701 // selcollist is a list of expressions that are to become the return
702 // values of the SELECT statement. The "*" in statements like
703 // "SELECT * FROM ..." is encoded as a special expression with an
704 // opcode of TK_ASTERISK.
706 %type selcollist
{ExprList
*}
707 %destructor selcollist
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
708 %type sclp
{ExprList
*}
709 %destructor sclp
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
710 sclp
(A
) ::= selcollist
(A
) COMMA.
711 sclp
(A
) ::= .
{A
= 0;}
712 selcollist
(A
) ::= sclp
(A
) scanpt
(B
) expr
(X
) scanpt
(Z
) as
(Y
).
{
713 A
= sqlite3ExprListAppend
(pParse
, A
, X
);
714 if
( Y.n
>0 ) sqlite3ExprListSetName
(pParse
, A
, &Y
, 1);
715 sqlite3ExprListSetSpan
(pParse
,A
,B
,Z
);
717 selcollist
(A
) ::= sclp
(A
) scanpt STAR
(X
).
{
718 Expr
*p
= sqlite3Expr
(pParse
->db
, TK_ASTERISK
, 0);
719 sqlite3ExprSetErrorOffset
(p
, (int)(X.z
- pParse
->zTail
));
720 A
= sqlite3ExprListAppend
(pParse
, A
, p
);
722 selcollist
(A
) ::= sclp
(A
) scanpt nm
(X
) DOT STAR
(Y
).
{
723 Expr
*pRight
, *pLeft
, *pDot
;
724 pRight
= sqlite3PExpr
(pParse
, TK_ASTERISK
, 0, 0);
725 sqlite3ExprSetErrorOffset
(pRight
, (int)(Y.z
- pParse
->zTail
));
726 pLeft
= tokenExpr
(pParse
, TK_ID
, X
);
727 pDot
= sqlite3PExpr
(pParse
, TK_DOT
, pLeft
, pRight
);
728 A
= sqlite3ExprListAppend
(pParse
,A
, pDot
);
731 // An option "AS <id>" phrase that can follow one of the expressions that
732 // define the result set, or one of the tables in the FROM clause.
735 as
(X
) ::= AS nm
(Y
).
{X
= Y
;}
737 as
(X
) ::= .
{X.n
= 0; X.z
= 0;}
740 %type seltablist
{SrcList
*}
741 %destructor seltablist
{sqlite3SrcListDelete
(pParse
->db
, $$
);}
742 %type stl_prefix
{SrcList
*}
743 %destructor stl_prefix
{sqlite3SrcListDelete
(pParse
->db
, $$
);}
744 %type from
{SrcList
*}
745 %destructor from
{sqlite3SrcListDelete
(pParse
->db
, $$
);}
747 // A complete FROM clause.
749 from
(A
) ::= .
{A
= 0;}
750 from
(A
) ::= FROM seltablist
(X
).
{
752 sqlite3SrcListShiftJoinType
(pParse
,A
);
755 // "seltablist" is a "Select Table List" - the content of the FROM clause
756 // in a SELECT statement. "stl_prefix" is a prefix of this list.
758 stl_prefix
(A
) ::= seltablist
(A
) joinop
(Y
).
{
759 if
( ALWAYS
(A
&& A
->nSrc
>0) ) A
->a
[A
->nSrc
-1].fg.jointype
= (u8
)Y
;
761 stl_prefix
(A
) ::= .
{A
= 0;}
762 seltablist
(A
) ::= stl_prefix
(A
) nm
(Y
) dbnm
(D
) as
(Z
) on_using
(N
).
{
763 A
= sqlite3SrcListAppendFromTerm
(pParse
,A
,&Y
,&D
,&Z
,0,&N
);
765 seltablist
(A
) ::= stl_prefix
(A
) nm
(Y
) dbnm
(D
) as
(Z
) indexed_by
(I
) on_using
(N
).
{
766 A
= sqlite3SrcListAppendFromTerm
(pParse
,A
,&Y
,&D
,&Z
,0,&N
);
767 sqlite3SrcListIndexedBy
(pParse
, A
, &I
);
769 seltablist
(A
) ::= stl_prefix
(A
) nm
(Y
) dbnm
(D
) LP exprlist
(E
) RP as
(Z
) on_using
(N
).
{
770 A
= sqlite3SrcListAppendFromTerm
(pParse
,A
,&Y
,&D
,&Z
,0,&N
);
771 sqlite3SrcListFuncArgs
(pParse
, A
, E
);
773 %ifndef SQLITE_OMIT_SUBQUERY
774 seltablist
(A
) ::= stl_prefix
(A
) LP select
(S
) RP as
(Z
) on_using
(N
).
{
775 A
= sqlite3SrcListAppendFromTerm
(pParse
,A
,0,0,&Z
,S
,&N
);
777 seltablist
(A
) ::= stl_prefix
(A
) LP seltablist
(F
) RP as
(Z
) on_using
(N
).
{
778 if
( A
==0 && Z.n
==0 && N.pOn
==0 && N.pUsing
==0 ){
780 }else if
( ALWAYS
(F
!=0) && F
->nSrc
==1 ){
781 A
= sqlite3SrcListAppendFromTerm
(pParse
,A
,0,0,&Z
,0,&N
);
783 SrcItem
*pNew
= &A
->a
[A
->nSrc
-1];
784 SrcItem
*pOld
= F
->a
;
785 assert
( pOld
->fg.fixedSchema
==0 );
786 pNew
->zName
= pOld
->zName
;
787 assert
( pOld
->fg.fixedSchema
==0 );
788 if
( pOld
->fg.isSubquery
){
789 pNew
->fg.isSubquery
= 1;
790 pNew
->u4.pSubq
= pOld
->u4.pSubq
;
792 pOld
->fg.isSubquery
= 0;
793 assert
( pNew
->u4.pSubq
!=0 && pNew
->u4.pSubq
->pSelect
!=0 );
794 if
( (pNew
->u4.pSubq
->pSelect
->selFlags
& SF_NestedFrom
)!=0 ){
795 pNew
->fg.isNestedFrom
= 1;
798 pNew
->u4.zDatabase
= pOld
->u4.zDatabase
;
799 pOld
->u4.zDatabase
= 0;
801 if
( pOld
->fg.isTabFunc
){
802 pNew
->u1.pFuncArg
= pOld
->u1.pFuncArg
;
803 pOld
->u1.pFuncArg
= 0;
804 pOld
->fg.isTabFunc
= 0;
805 pNew
->fg.isTabFunc
= 1;
809 sqlite3SrcListDelete
(pParse
->db
, F
);
812 sqlite3SrcListShiftJoinType
(pParse
,F
);
813 pSubquery
= sqlite3SelectNew
(pParse
,0,F
,0,0,0,0,SF_NestedFrom
,0);
814 A
= sqlite3SrcListAppendFromTerm
(pParse
,A
,0,0,&Z
,pSubquery
,&N
);
817 %endif SQLITE_OMIT_SUBQUERY
820 dbnm
(A
) ::= .
{A.z
=0; A.n
=0;}
821 dbnm
(A
) ::= DOT nm
(X
).
{A
= X
;}
823 %type fullname
{SrcList
*}
824 %destructor fullname
{sqlite3SrcListDelete
(pParse
->db
, $$
);}
825 fullname
(A
) ::= nm
(X
).
{
826 A
= sqlite3SrcListAppend
(pParse
,0,&X
,0);
827 if
( IN_RENAME_OBJECT
&& A
) sqlite3RenameTokenMap
(pParse
, A
->a
[0].zName
, &X
);
829 fullname
(A
) ::= nm
(X
) DOT nm
(Y
).
{
830 A
= sqlite3SrcListAppend
(pParse
,0,&X
,&Y
);
831 if
( IN_RENAME_OBJECT
&& A
) sqlite3RenameTokenMap
(pParse
, A
->a
[0].zName
, &Y
);
834 %type xfullname
{SrcList
*}
835 %destructor xfullname
{sqlite3SrcListDelete
(pParse
->db
, $$
);}
836 xfullname
(A
) ::= nm
(X
).
{
837 A
= sqlite3SrcListAppend
(pParse
,0,&X
,0);
838 if
( IN_RENAME_OBJECT
&& A
) sqlite3RenameTokenMap
(pParse
, A
->a
[0].zName
, &X
);
840 xfullname
(A
) ::= nm
(X
) DOT nm
(Y
).
{
841 A
= sqlite3SrcListAppend
(pParse
,0,&X
,&Y
);
842 if
( IN_RENAME_OBJECT
&& A
) sqlite3RenameTokenMap
(pParse
, A
->a
[0].zName
, &Y
);
844 xfullname
(A
) ::= nm
(X
) AS nm
(Z
).
{
845 A
= sqlite3SrcListAppend
(pParse
,0,&X
,0);
847 if
( IN_RENAME_OBJECT
){
848 sqlite3RenameTokenMap
(pParse
, A
->a
[0].zName
, &X
);
850 A
->a
[0].zAlias
= sqlite3NameFromToken
(pParse
->db
, &Z
);
854 xfullname
(A
) ::= nm
(X
) DOT nm
(Y
) AS nm
(Z
).
{
855 A
= sqlite3SrcListAppend
(pParse
,0,&X
,&Y
);
857 if
( IN_RENAME_OBJECT
){
858 sqlite3RenameTokenMap
(pParse
, A
->a
[0].zName
, &Y
);
860 A
->a
[0].zAlias
= sqlite3NameFromToken
(pParse
->db
, &Z
);
867 joinop
(X
) ::= COMMA|JOIN.
{ X
= JT_INNER
; }
868 joinop
(X
) ::= JOIN_KW
(A
) JOIN.
869 {X
= sqlite3JoinType
(pParse
,&A
,0,0); /*X-overwrites-A*/}
870 joinop
(X
) ::= JOIN_KW
(A
) nm
(B
) JOIN.
871 {X
= sqlite3JoinType
(pParse
,&A
,&B
,0); /*X-overwrites-A*/}
872 joinop
(X
) ::= JOIN_KW
(A
) nm
(B
) nm
(C
) JOIN.
873 {X
= sqlite3JoinType
(pParse
,&A
,&B
,&C
);/*X-overwrites-A*/}
875 // There is a parsing ambiguity in an upsert statement that uses a
876 // SELECT on the RHS of a the INSERT:
878 // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
881 // When the ON token is encountered, the parser does not know if it is
882 // the beginning of an ON CONFLICT clause, or the beginning of an ON
883 // clause associated with the JOIN. The conflict is resolved in favor
884 // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy
885 // WHERE clause in between, like this:
887 // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
889 // The [AND] and [OR] precedence marks in the rules for on_using cause the
890 // ON in this context to always be interpreted as belonging to the JOIN.
892 %type on_using
{OnOrUsing
}
893 //%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);}
894 on_using
(N
) ::= ON expr
(E
).
{N.pOn
= E
; N.pUsing
= 0;}
895 on_using
(N
) ::= USING LP idlist
(L
) RP.
{N.pOn
= 0; N.pUsing
= L
;}
896 on_using
(N
) ::= .
[OR
] {N.pOn
= 0; N.pUsing
= 0;}
898 // Note that this block abuses the Token type just a little. If there is
899 // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
900 // there is an INDEXED BY clause, then the token is populated as per normal,
901 // with z pointing to the token data and n containing the number of bytes
904 // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is
905 // normally illegal. The sqlite3SrcListIndexedBy() function
906 // recognizes and interprets this as a special case.
908 %type indexed_opt
{Token
}
909 %type indexed_by
{Token
}
910 indexed_opt
(A
) ::= .
{A.z
=0; A.n
=0;}
911 indexed_opt
(A
) ::= indexed_by
(A
).
912 indexed_by
(A
) ::= INDEXED BY nm
(X
).
{A
= X
;}
913 indexed_by
(A
) ::= NOT INDEXED.
{A.z
=0; A.n
=1;}
915 %type orderby_opt
{ExprList
*}
916 %destructor orderby_opt
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
918 // the sortlist non-terminal stores a list of expression where each
919 // expression is optionally followed by ASC or DESC to indicate the
922 %type sortlist
{ExprList
*}
923 %destructor sortlist
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
925 orderby_opt
(A
) ::= .
{A
= 0;}
926 orderby_opt
(A
) ::= ORDER BY sortlist
(X
).
{A
= X
;}
927 sortlist
(A
) ::= sortlist
(A
) COMMA expr
(Y
) sortorder
(Z
) nulls
(X
).
{
928 A
= sqlite3ExprListAppend
(pParse
,A
,Y
);
929 sqlite3ExprListSetSortOrder
(A
,Z
,X
);
931 sortlist
(A
) ::= expr
(Y
) sortorder
(Z
) nulls
(X
).
{
932 A
= sqlite3ExprListAppend
(pParse
,0,Y
); /*A-overwrites-Y*/
933 sqlite3ExprListSetSortOrder
(A
,Z
,X
);
936 %type sortorder
{int}
938 sortorder
(A
) ::= ASC.
{A
= SQLITE_SO_ASC
;}
939 sortorder
(A
) ::= DESC.
{A
= SQLITE_SO_DESC
;}
940 sortorder
(A
) ::= .
{A
= SQLITE_SO_UNDEFINED
;}
943 nulls
(A
) ::= NULLS FIRST.
{A
= SQLITE_SO_ASC
;}
944 nulls
(A
) ::= NULLS LAST.
{A
= SQLITE_SO_DESC
;}
945 nulls
(A
) ::= .
{A
= SQLITE_SO_UNDEFINED
;}
947 %type groupby_opt
{ExprList
*}
948 %destructor groupby_opt
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
949 groupby_opt
(A
) ::= .
{A
= 0;}
950 groupby_opt
(A
) ::= GROUP BY nexprlist
(X
).
{A
= X
;}
952 %type having_opt
{Expr
*}
953 %destructor having_opt
{sqlite3ExprDelete
(pParse
->db
, $$
);}
954 having_opt
(A
) ::= .
{A
= 0;}
955 having_opt
(A
) ::= HAVING expr
(X
).
{A
= X
;}
957 %type limit_opt
{Expr
*}
959 // The destructor for limit_opt will never fire in the current grammar.
960 // The limit_opt non-terminal only occurs at the end of a single production
961 // rule for SELECT statements. As soon as the rule that create the
962 // limit_opt non-terminal reduces, the SELECT statement rule will also
963 // reduce. So there is never a limit_opt non-terminal on the stack
964 // except as a transient. So there is never anything to destroy.
966 //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
967 limit_opt
(A
) ::= .
{A
= 0;}
968 limit_opt
(A
) ::= LIMIT expr
(X
).
969 {A
= sqlite3PExpr
(pParse
,TK_LIMIT
,X
,0);}
970 limit_opt
(A
) ::= LIMIT expr
(X
) OFFSET expr
(Y
).
971 {A
= sqlite3PExpr
(pParse
,TK_LIMIT
,X
,Y
);}
972 limit_opt
(A
) ::= LIMIT expr
(X
) COMMA expr
(Y
).
973 {A
= sqlite3PExpr
(pParse
,TK_LIMIT
,Y
,X
);}
975 /////////////////////////// The DELETE statement /////////////////////////////
977 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
978 cmd
::= with DELETE FROM xfullname
(X
) indexed_opt
(I
) where_opt_ret
(W
)
979 orderby_opt
(O
) limit_opt
(L
).
{
980 sqlite3SrcListIndexedBy
(pParse
, X
, &I
);
981 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
983 updateDeleteLimitError
(pParse
,O
,L
);
988 sqlite3DeleteFrom
(pParse
,X
,W
,O
,L
);
991 cmd
::= with DELETE FROM xfullname
(X
) indexed_opt
(I
) where_opt_ret
(W
).
{
992 sqlite3SrcListIndexedBy
(pParse
, X
, &I
);
993 sqlite3DeleteFrom
(pParse
,X
,W
,0,0);
997 %type where_opt
{Expr
*}
998 %destructor where_opt
{sqlite3ExprDelete
(pParse
->db
, $$
);}
999 %type where_opt_ret
{Expr
*}
1000 %destructor where_opt_ret
{sqlite3ExprDelete
(pParse
->db
, $$
);}
1002 where_opt
(A
) ::= .
{A
= 0;}
1003 where_opt
(A
) ::= WHERE expr
(X
).
{A
= X
;}
1004 where_opt_ret
(A
) ::= .
{A
= 0;}
1005 where_opt_ret
(A
) ::= WHERE expr
(X
).
{A
= X
;}
1006 where_opt_ret
(A
) ::= RETURNING selcollist
(X
).
1007 {sqlite3AddReturning
(pParse
,X
); A
= 0;}
1008 where_opt_ret
(A
) ::= WHERE expr
(X
) RETURNING selcollist
(Y
).
1009 {sqlite3AddReturning
(pParse
,Y
); A
= X
;}
1011 ////////////////////////// The UPDATE command ////////////////////////////////
1013 %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
1014 cmd
::= with UPDATE orconf
(R
) xfullname
(X
) indexed_opt
(I
) SET setlist
(Y
) from
(F
)
1015 where_opt_ret
(W
) orderby_opt
(O
) limit_opt
(L
).
{
1016 sqlite3SrcListIndexedBy
(pParse
, X
, &I
);
1018 SrcList
*pFromClause
= F
;
1019 if
( pFromClause
->nSrc
>1 ){
1022 pSubquery
= sqlite3SelectNew
(pParse
,0,pFromClause
,0,0,0,0,SF_NestedFrom
,0);
1025 pFromClause
= sqlite3SrcListAppendFromTerm
(pParse
,0,0,0,&as
,pSubquery
,0);
1027 X
= sqlite3SrcListAppendList
(pParse
, X
, pFromClause
);
1029 sqlite3ExprListCheckLength
(pParse
,Y
,"set list");
1030 #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
1032 updateDeleteLimitError
(pParse
,O
,L
);
1037 sqlite3Update
(pParse
,X
,Y
,W
,R
,O
,L
,0);
1040 cmd
::= with UPDATE orconf
(R
) xfullname
(X
) indexed_opt
(I
) SET setlist
(Y
) from
(F
)
1042 sqlite3SrcListIndexedBy
(pParse
, X
, &I
);
1043 sqlite3ExprListCheckLength
(pParse
,Y
,"set list");
1045 SrcList
*pFromClause
= F
;
1046 if
( pFromClause
->nSrc
>1 ){
1049 pSubquery
= sqlite3SelectNew
(pParse
,0,pFromClause
,0,0,0,0,SF_NestedFrom
,0);
1052 pFromClause
= sqlite3SrcListAppendFromTerm
(pParse
,0,0,0,&as
,pSubquery
,0);
1054 X
= sqlite3SrcListAppendList
(pParse
, X
, pFromClause
);
1056 sqlite3Update
(pParse
,X
,Y
,W
,R
,0,0,0);
1062 %type setlist
{ExprList
*}
1063 %destructor setlist
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
1065 setlist
(A
) ::= setlist
(A
) COMMA nm
(X
) EQ expr
(Y
).
{
1066 A
= sqlite3ExprListAppend
(pParse
, A
, Y
);
1067 sqlite3ExprListSetName
(pParse
, A
, &X
, 1);
1069 setlist
(A
) ::= setlist
(A
) COMMA LP idlist
(X
) RP EQ expr
(Y
).
{
1070 A
= sqlite3ExprListAppendVector
(pParse
, A
, X
, Y
);
1072 setlist
(A
) ::= nm
(X
) EQ expr
(Y
).
{
1073 A
= sqlite3ExprListAppend
(pParse
, 0, Y
);
1074 sqlite3ExprListSetName
(pParse
, A
, &X
, 1);
1076 setlist
(A
) ::= LP idlist
(X
) RP EQ expr
(Y
).
{
1077 A
= sqlite3ExprListAppendVector
(pParse
, 0, X
, Y
);
1080 ////////////////////////// The INSERT command /////////////////////////////////
1082 cmd
::= with insert_cmd
(R
) INTO xfullname
(X
) idlist_opt
(F
) select
(S
)
1084 sqlite3Insert
(pParse
, X
, S
, F
, R
, U
);
1086 cmd
::= with insert_cmd
(R
) INTO xfullname
(X
) idlist_opt
(F
) DEFAULT VALUES returning.
1088 sqlite3Insert
(pParse
, X
, 0, F
, R
, 0);
1091 %type upsert
{Upsert
*}
1093 // Because upsert only occurs at the tip end of the INSERT rule for cmd,
1094 // there is never a case where the value of the upsert pointer will not
1095 // be destroyed by the cmd action. So comment-out the destructor to
1096 // avoid unreachable code.
1097 //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
1098 upsert
(A
) ::= .
{ A
= 0; }
1099 upsert
(A
) ::= RETURNING selcollist
(X
).
{ A
= 0; sqlite3AddReturning
(pParse
,X
); }
1100 upsert
(A
) ::= ON CONFLICT LP sortlist
(T
) RP where_opt
(TW
)
1101 DO UPDATE SET setlist
(Z
) where_opt
(W
) upsert
(N
).
1102 { A
= sqlite3UpsertNew
(pParse
->db
,T
,TW
,Z
,W
,N
);}
1103 upsert
(A
) ::= ON CONFLICT LP sortlist
(T
) RP where_opt
(TW
) DO NOTHING upsert
(N
).
1104 { A
= sqlite3UpsertNew
(pParse
->db
,T
,TW
,0,0,N
); }
1105 upsert
(A
) ::= ON CONFLICT DO NOTHING returning.
1106 { A
= sqlite3UpsertNew
(pParse
->db
,0,0,0,0,0); }
1107 upsert
(A
) ::= ON CONFLICT DO UPDATE SET setlist
(Z
) where_opt
(W
) returning.
1108 { A
= sqlite3UpsertNew
(pParse
->db
,0,0,Z
,W
,0);}
1110 returning
::= RETURNING selcollist
(X
).
{sqlite3AddReturning
(pParse
,X
);}
1113 %type insert_cmd
{int}
1114 insert_cmd
(A
) ::= INSERT orconf
(R
).
{A
= R
;}
1115 insert_cmd
(A
) ::= REPLACE.
{A
= OE_Replace
;}
1117 %type idlist_opt
{IdList
*}
1118 %destructor idlist_opt
{sqlite3IdListDelete
(pParse
->db
, $$
);}
1119 %type idlist
{IdList
*}
1120 %destructor idlist
{sqlite3IdListDelete
(pParse
->db
, $$
);}
1122 idlist_opt
(A
) ::= .
{A
= 0;}
1123 idlist_opt
(A
) ::= LP idlist
(X
) RP.
{A
= X
;}
1124 idlist
(A
) ::= idlist
(A
) COMMA nm
(Y
).
1125 {A
= sqlite3IdListAppend
(pParse
,A
,&Y
);}
1126 idlist
(A
) ::= nm
(Y
).
1127 {A
= sqlite3IdListAppend
(pParse
,0,&Y
); /*A-overwrites-Y*/}
1129 /////////////////////////// Expression Processing /////////////////////////////
1133 %destructor expr
{sqlite3ExprDelete
(pParse
->db
, $$
);}
1135 %destructor term
{sqlite3ExprDelete
(pParse
->db
, $$
);}
1139 /* Construct a new Expr object from a single token */
1140 static Expr
*tokenExpr
(Parse
*pParse
, int op
, Token t
){
1141 Expr
*p
= sqlite3DbMallocRawNN
(pParse
->db
, sizeof
(Expr
)+t.n
+1);
1143 /* memset(p, 0, sizeof(Expr)); */
1147 ExprClearVVAProperties
(p
);
1148 /* p->iAgg = -1; // Not required */
1149 p
->pLeft
= p
->pRight
= 0;
1151 memset
(&p
->x
, 0, sizeof
(p
->x
));
1152 memset
(&p
->y
, 0, sizeof
(p
->y
));
1156 p
->u.zToken
= (char*)&p
[1];
1157 memcpy
(p
->u.zToken
, t.z
, t.n
);
1158 p
->u.zToken
[t.n
] = 0;
1159 p
->w.iOfst
= (int)(t.z
- pParse
->zTail
);
1160 if
( sqlite3Isquote
(p
->u.zToken
[0]) ){
1161 sqlite3DequoteExpr
(p
);
1163 #if SQLITE_MAX_EXPR_DEPTH>0
1166 if
( IN_RENAME_OBJECT
){
1167 return
(Expr
*)sqlite3RenameTokenMap
(pParse
, (void*)p
, &t
);
1175 expr
(A
) ::= term
(A
).
1176 expr
(A
) ::= LP expr
(X
) RP.
{A
= X
;}
1177 expr
(A
) ::= idj
(X
).
{A
=tokenExpr
(pParse
,TK_ID
,X
); /*A-overwrites-X*/}
1178 expr
(A
) ::= nm
(X
) DOT nm
(Y
).
{
1179 Expr
*temp1
= tokenExpr
(pParse
,TK_ID
,X
);
1180 Expr
*temp2
= tokenExpr
(pParse
,TK_ID
,Y
);
1181 A
= sqlite3PExpr
(pParse
, TK_DOT
, temp1
, temp2
);
1183 expr
(A
) ::= nm
(X
) DOT nm
(Y
) DOT nm
(Z
).
{
1184 Expr
*temp1
= tokenExpr
(pParse
,TK_ID
,X
);
1185 Expr
*temp2
= tokenExpr
(pParse
,TK_ID
,Y
);
1186 Expr
*temp3
= tokenExpr
(pParse
,TK_ID
,Z
);
1187 Expr
*temp4
= sqlite3PExpr
(pParse
, TK_DOT
, temp2
, temp3
);
1188 if
( IN_RENAME_OBJECT
){
1189 sqlite3RenameTokenRemap
(pParse
, 0, temp1
);
1191 A
= sqlite3PExpr
(pParse
, TK_DOT
, temp1
, temp4
);
1193 term
(A
) ::= NULL|FLOAT|BLOB
(X
).
{A
=tokenExpr
(pParse
,@X
,X
); /*A-overwrites-X*/}
1194 term
(A
) ::= STRING
(X
).
{A
=tokenExpr
(pParse
,@X
,X
); /*A-overwrites-X*/}
1195 term
(A
) ::= INTEGER
(X
).
{
1197 if
( sqlite3GetInt32
(X.z
, &iValue
)==0 ){
1198 A
= sqlite3ExprAlloc
(pParse
->db
, TK_INTEGER
, &X
, 0);
1200 A
= sqlite3ExprInt32
(pParse
->db
, iValue
);
1202 if
( A
) A
->w.iOfst
= (int)(X.z
- pParse
->zTail
);
1204 expr
(A
) ::= VARIABLE
(X
).
{
1205 if
( !(X.z
[0]=='#' && sqlite3Isdigit
(X.z
[1])) ){
1207 A
= tokenExpr
(pParse
, TK_VARIABLE
, X
);
1208 sqlite3ExprAssignVarNumber
(pParse
, A
, n
);
1210 /* When doing a nested parse, one can include terms in an expression
1211 ** that look like this: #1 #2 ... These terms refer to registers
1212 ** in the virtual machine. #N is the N-th register. */
1213 Token t
= X
; /*A-overwrites-X*/
1215 if
( pParse
->nested
==0 ){
1216 parserSyntaxError
(pParse
, &t
);
1219 A
= sqlite3PExpr
(pParse
, TK_REGISTER
, 0, 0);
1220 if
( A
) sqlite3GetInt32
(&t.z
[1], &A
->iTable
);
1224 expr
(A
) ::= expr
(A
) COLLATE ids
(C
).
{
1225 A
= sqlite3ExprAddCollateToken
(pParse
, A
, &C
, 1);
1227 %ifndef SQLITE_OMIT_CAST
1228 expr
(A
) ::= CAST LP expr
(E
) AS typetoken
(T
) RP.
{
1229 A
= sqlite3ExprAlloc
(pParse
->db
, TK_CAST
, &T
, 1);
1230 sqlite3ExprAttachSubtrees
(pParse
->db
, A
, E
, 0);
1232 %endif SQLITE_OMIT_CAST
1235 expr
(A
) ::= idj
(X
) LP distinct
(D
) exprlist
(Y
) RP.
{
1236 A
= sqlite3ExprFunction
(pParse
, Y
, &X
, D
);
1238 expr
(A
) ::= idj
(X
) LP distinct
(D
) exprlist
(Y
) ORDER BY sortlist
(O
) RP.
{
1239 A
= sqlite3ExprFunction
(pParse
, Y
, &X
, D
);
1240 sqlite3ExprAddFunctionOrderBy
(pParse
, A
, O
);
1242 expr
(A
) ::= idj
(X
) LP STAR RP.
{
1243 A
= sqlite3ExprFunction
(pParse
, 0, &X
, 0);
1246 %ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES
1248 /* Generate an expression node that represents an ordered-set aggregate function.
1250 ** SQLite does not do anything special to evaluate ordered-set aggregates. The
1251 ** aggregate function itself is expected to do any required ordering on its own.
1252 ** This is just syntactic sugar.
1254 ** This syntax: percentile(f) WITHIN GROUP ( ORDER BY y )
1256 ** Is equivalent to: percentile(y,f)
1258 ** The purpose of this function is to generate an Expr node from the first syntax
1259 ** into a TK_FUNCTION node that looks like it came from the second syntax.
1261 ** Only functions that have the SQLITE_SELFORDER1 property are allowed to do this
1262 ** transformation. Because DISTINCT is not allowed in the ordered-set aggregate
1263 ** syntax, an error is raised if DISTINCT is used.
1265 static Expr
*sqlite3ExprAddOrderedsetFunction
(
1266 Parse
*pParse
, /* Parsing context */
1267 Token
*pFuncname
, /* Name of the function */
1268 int isDistinct
, /* DISTINCT or ALL qualifier */
1269 ExprList
*pOrig
, /* Arguments to the function */
1270 Expr
*pOrderby
/* Expression in the ORDER BY clause */
1272 ExprList
*p
; /* Modified argument list */
1273 Expr
*pExpr
; /* Final result */
1274 p
= sqlite3ExprListAppend
(pParse
, 0, pOrderby
);
1277 for
(i
=0; i
<pOrig
->nExpr
; i
++){
1278 p
= sqlite3ExprListAppend
(pParse
, p
, pOrig
->a
[i
].pExpr
);
1279 pOrig
->a
[i
].pExpr
= 0;
1281 sqlite3ExprListDelete
(pParse
->db
, pOrig
);
1283 pExpr
= sqlite3ExprFunction
(pParse
, p
, pFuncname
, 0);
1284 if
( pParse
->nErr
==0 ){
1286 u8 enc
= ENC
(pParse
->db
);
1287 assert
( pExpr
!=0 ); /* Because otherwise pParse->nErr would not be zero */
1288 assert
( p
!=0 ); /* Because otherwise pParse->nErr would not be zero */
1289 pDef
= sqlite3FindFunction
(pParse
->db
, pExpr
->u.zToken
, -2, enc
, 0);
1290 if
( pDef
==0 ||
(pDef
->funcFlags
& SQLITE_SELFORDER1
)==0 ){
1291 sqlite3ErrorMsg
(pParse
, "%#T() is not an ordered-set aggregate", pExpr
);
1292 }else if
( isDistinct
==SF_Distinct
){
1293 sqlite3ErrorMsg
(pParse
, "DISTINCT not allowed on ordered-set aggregate %T()",
1300 expr
(A
) ::= idj
(X
) LP distinct
(D
) exprlist
(Y
) RP WITHIN GROUP LP ORDER BY expr
(E
) RP.
{
1301 A
= sqlite3ExprAddOrderedsetFunction
(pParse
, &X
, D
, Y
, E
);
1303 %endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES
1305 %ifndef SQLITE_OMIT_WINDOWFUNC
1306 expr
(A
) ::= idj
(X
) LP distinct
(D
) exprlist
(Y
) RP filter_over
(Z
).
{
1307 A
= sqlite3ExprFunction
(pParse
, Y
, &X
, D
);
1308 sqlite3WindowAttach
(pParse
, A
, Z
);
1310 expr
(A
) ::= idj
(X
) LP distinct
(D
) exprlist
(Y
) ORDER BY sortlist
(O
) RP filter_over
(Z
).
{
1311 A
= sqlite3ExprFunction
(pParse
, Y
, &X
, D
);
1312 sqlite3WindowAttach
(pParse
, A
, Z
);
1313 sqlite3ExprAddFunctionOrderBy
(pParse
, A
, O
);
1315 expr
(A
) ::= idj
(X
) LP STAR RP filter_over
(Z
).
{
1316 A
= sqlite3ExprFunction
(pParse
, 0, &X
, 0);
1317 sqlite3WindowAttach
(pParse
, A
, Z
);
1319 %ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES
1320 expr
(A
) ::= idj
(X
) LP distinct
(D
) exprlist
(Y
) RP WITHIN GROUP LP ORDER BY expr
(E
) RP
1322 A
= sqlite3ExprAddOrderedsetFunction
(pParse
, &X
, D
, Y
, E
);
1323 sqlite3WindowAttach
(pParse
, A
, Z
);
1325 %endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES
1327 %endif SQLITE_OMIT_WINDOWFUNC
1329 term
(A
) ::= CTIME_KW
(OP
).
{
1330 A
= sqlite3ExprFunction
(pParse
, 0, &OP
, 0);
1333 expr
(A
) ::= LP nexprlist
(X
) COMMA expr
(Y
) RP.
{
1334 ExprList
*pList
= sqlite3ExprListAppend
(pParse
, X
, Y
);
1335 A
= sqlite3PExpr
(pParse
, TK_VECTOR
, 0, 0);
1338 if
( ALWAYS
(pList
->nExpr
) ){
1339 A
->flags |
= pList
->a
[0].pExpr
->flags
& EP_Propagate
;
1342 sqlite3ExprListDelete
(pParse
->db
, pList
);
1346 expr
(A
) ::= expr
(A
) AND expr
(Y
).
{A
=sqlite3ExprAnd
(pParse
,A
,Y
);}
1347 expr
(A
) ::= expr
(A
) OR
(OP
) expr
(Y
).
{A
=sqlite3PExpr
(pParse
,@OP
,A
,Y
);}
1348 expr
(A
) ::= expr
(A
) LT|GT|GE|LE
(OP
) expr
(Y
).
1349 {A
=sqlite3PExpr
(pParse
,@OP
,A
,Y
);}
1350 expr
(A
) ::= expr
(A
) EQ|NE
(OP
) expr
(Y
).
{A
=sqlite3PExpr
(pParse
,@OP
,A
,Y
);}
1351 expr
(A
) ::= expr
(A
) BITAND|BITOR|LSHIFT|RSHIFT
(OP
) expr
(Y
).
1352 {A
=sqlite3PExpr
(pParse
,@OP
,A
,Y
);}
1353 expr
(A
) ::= expr
(A
) PLUS|MINUS
(OP
) expr
(Y
).
1354 {A
=sqlite3PExpr
(pParse
,@OP
,A
,Y
);}
1355 expr
(A
) ::= expr
(A
) STAR|SLASH|REM
(OP
) expr
(Y
).
1356 {A
=sqlite3PExpr
(pParse
,@OP
,A
,Y
);}
1357 expr
(A
) ::= expr
(A
) CONCAT
(OP
) expr
(Y
).
{A
=sqlite3PExpr
(pParse
,@OP
,A
,Y
);}
1358 %type likeop
{Token
}
1359 likeop
(A
) ::= LIKE_KW|MATCH
(A
).
1360 likeop
(A
) ::= NOT LIKE_KW|MATCH
(X
).
{A
=X
; A.n|
=0x80000000; /*A-overwrite-X*/}
1361 expr
(A
) ::= expr
(A
) likeop
(OP
) expr
(Y
).
[LIKE_KW
] {
1363 int bNot
= OP.n
& 0x80000000;
1365 pList
= sqlite3ExprListAppend
(pParse
,0, Y
);
1366 pList
= sqlite3ExprListAppend
(pParse
,pList
, A
);
1367 A
= sqlite3ExprFunction
(pParse
, pList
, &OP
, 0);
1368 if
( bNot
) A
= sqlite3PExpr
(pParse
, TK_NOT
, A
, 0);
1369 if
( A
) A
->flags |
= EP_InfixFunc
;
1371 expr
(A
) ::= expr
(A
) likeop
(OP
) expr
(Y
) ESCAPE expr
(E
).
[LIKE_KW
] {
1373 int bNot
= OP.n
& 0x80000000;
1375 pList
= sqlite3ExprListAppend
(pParse
,0, Y
);
1376 pList
= sqlite3ExprListAppend
(pParse
,pList
, A
);
1377 pList
= sqlite3ExprListAppend
(pParse
,pList
, E
);
1378 A
= sqlite3ExprFunction
(pParse
, pList
, &OP
, 0);
1379 if
( bNot
) A
= sqlite3PExpr
(pParse
, TK_NOT
, A
, 0);
1380 if
( A
) A
->flags |
= EP_InfixFunc
;
1384 /* Create a TK_ISNULL or TK_NOTNULL expression, perhaps optimized to
1385 ** to TK_TRUEFALSE, if possible */
1386 static Expr
*sqlite3PExprIsNull
(
1387 Parse
*pParse
, /* Parsing context */
1388 int op
, /* TK_ISNULL or TK_NOTNULL */
1389 Expr
*pLeft
/* Operand */
1392 assert
( op
==TK_ISNULL || op
==TK_NOTNULL
);
1394 while
( p
->op
==TK_UPLUS || p
->op
==TK_UMINUS
){
1403 sqlite3ExprDeferredDelete
(pParse
, pLeft
);
1404 return sqlite3ExprInt32
(pParse
->db
, op
==TK_NOTNULL
);
1408 return sqlite3PExpr
(pParse
, op
, pLeft
, 0);
1411 /* Create a TK_IS or TK_ISNOT operator, perhaps optimized to
1412 ** TK_ISNULL or TK_NOTNULL or TK_TRUEFALSE. */
1413 static Expr
*sqlite3PExprIs
(
1414 Parse
*pParse
, /* Parsing context */
1415 int op
, /* TK_IS or TK_ISNOT */
1416 Expr
*pLeft
, /* Left operand */
1417 Expr
*pRight
/* Right operand */
1419 if
( pRight
&& pRight
->op
==TK_NULL
){
1420 sqlite3ExprDeferredDelete
(pParse
, pRight
);
1421 return sqlite3PExprIsNull
(pParse
, op
==TK_IS ? TK_ISNULL
: TK_NOTNULL
, pLeft
);
1423 return sqlite3PExpr
(pParse
, op
, pLeft
, pRight
);
1427 expr
(A
) ::= expr
(A
) ISNULL|NOTNULL
(E
).
{A
= sqlite3PExprIsNull
(pParse
,@E
,A
);}
1428 expr
(A
) ::= expr
(A
) NOT NULL.
{A
= sqlite3PExprIsNull
(pParse
,TK_NOTNULL
,A
);}
1430 // expr1 IS expr2 same as expr1 IS NOT DISTINCT FROM expr2
1431 // expr1 IS NOT expr2 same as expr1 IS DISTINCT FROM expr2
1433 expr
(A
) ::= expr
(A
) IS expr
(Y
).
{
1434 A
= sqlite3PExprIs
(pParse
, TK_IS
, A
, Y
);
1436 expr
(A
) ::= expr
(A
) IS NOT expr
(Y
).
{
1437 A
= sqlite3PExprIs
(pParse
, TK_ISNOT
, A
, Y
);
1439 expr
(A
) ::= expr
(A
) IS NOT DISTINCT FROM expr
(Y
).
{
1440 A
= sqlite3PExprIs
(pParse
, TK_IS
, A
, Y
);
1442 expr
(A
) ::= expr
(A
) IS DISTINCT FROM expr
(Y
).
{
1443 A
= sqlite3PExprIs
(pParse
, TK_ISNOT
, A
, Y
);
1446 expr
(A
) ::= NOT
(B
) expr
(X
).
1447 {A
= sqlite3PExpr
(pParse
, @B
, X
, 0);/*A-overwrites-B*/}
1448 expr
(A
) ::= BITNOT
(B
) expr
(X
).
1449 {A
= sqlite3PExpr
(pParse
, @B
, X
, 0);/*A-overwrites-B*/}
1450 expr
(A
) ::= PLUS|MINUS
(B
) expr
(X
).
[BITNOT
] {
1452 u8 op
= @B
+ (TK_UPLUS
-TK_PLUS
);
1453 assert
( TK_UPLUS
>TK_PLUS
);
1454 assert
( TK_UMINUS
== TK_MINUS
+ (TK_UPLUS
- TK_PLUS
) );
1455 if
( p
&& p
->op
==TK_UPLUS
){
1459 A
= sqlite3PExpr
(pParse
, op
, p
, 0);
1464 expr
(A
) ::= expr
(B
) PTR
(C
) expr
(D
).
{
1465 ExprList
*pList
= sqlite3ExprListAppend
(pParse
, 0, B
);
1466 pList
= sqlite3ExprListAppend
(pParse
, pList
, D
);
1467 A
= sqlite3ExprFunction
(pParse
, pList
, &C
, 0);
1470 %type between_op
{int}
1471 between_op
(A
) ::= BETWEEN.
{A
= 0;}
1472 between_op
(A
) ::= NOT BETWEEN.
{A
= 1;}
1473 expr
(A
) ::= expr
(A
) between_op
(N
) expr
(X
) AND expr
(Y
).
[BETWEEN
] {
1474 ExprList
*pList
= sqlite3ExprListAppend
(pParse
,0, X
);
1475 pList
= sqlite3ExprListAppend
(pParse
,pList
, Y
);
1476 A
= sqlite3PExpr
(pParse
, TK_BETWEEN
, A
, 0);
1480 sqlite3ExprListDelete
(pParse
->db
, pList
);
1482 if
( N
) A
= sqlite3PExpr
(pParse
, TK_NOT
, A
, 0);
1484 %ifndef SQLITE_OMIT_SUBQUERY
1486 in_op
(A
) ::= IN.
{A
= 0;}
1487 in_op
(A
) ::= NOT IN.
{A
= 1;}
1488 expr
(A
) ::= expr
(A
) in_op
(N
) LP exprlist
(Y
) RP.
[IN
] {
1490 /* Expressions of the form
1495 ** simplify to constants 0 (false) and 1 (true), respectively.
1497 ** Except, do not apply this optimization if expr1 contains a function
1498 ** because that function might be an aggregate (we don't know yet whether
1499 ** it is or not) and if it is an aggregate, that could change the meaning
1500 ** of the whole query.
1502 Expr
*pB
= sqlite3Expr
(pParse
->db
, TK_STRING
, N ?
"true" : "false");
1503 if
( pB
) sqlite3ExprIdToTrueFalse
(pB
);
1504 if
( !ExprHasProperty
(A
, EP_HasFunc
) ){
1505 sqlite3ExprUnmapAndDelete
(pParse
, A
);
1508 A
= sqlite3PExpr
(pParse
, N ? TK_OR
: TK_AND
, pB
, A
);
1511 Expr
*pRHS
= Y
->a
[0].pExpr
;
1512 if
( Y
->nExpr
==1 && sqlite3ExprIsConstant
(pParse
,pRHS
) && A
->op
!=TK_VECTOR
){
1514 sqlite3ExprListDelete
(pParse
->db
, Y
);
1515 pRHS
= sqlite3PExpr
(pParse
, TK_UPLUS
, pRHS
, 0);
1516 A
= sqlite3PExpr
(pParse
, TK_EQ
, A
, pRHS
);
1517 }else if
( Y
->nExpr
==1 && pRHS
->op
==TK_SELECT
){
1518 A
= sqlite3PExpr
(pParse
, TK_IN
, A
, 0);
1519 sqlite3PExprAddSelect
(pParse
, A
, pRHS
->x.pSelect
);
1520 pRHS
->x.pSelect
= 0;
1521 sqlite3ExprListDelete
(pParse
->db
, Y
);
1523 A
= sqlite3PExpr
(pParse
, TK_IN
, A
, 0);
1525 sqlite3ExprListDelete
(pParse
->db
, Y
);
1526 }else if
( A
->pLeft
->op
==TK_VECTOR
){
1527 int nExpr
= A
->pLeft
->x.pList
->nExpr
;
1528 Select
*pSelectRHS
= sqlite3ExprListToValues
(pParse
, nExpr
, Y
);
1530 parserDoubleLinkSelect
(pParse
, pSelectRHS
);
1531 sqlite3PExprAddSelect
(pParse
, A
, pSelectRHS
);
1535 sqlite3ExprSetHeightAndFlags
(pParse
, A
);
1538 if
( N
) A
= sqlite3PExpr
(pParse
, TK_NOT
, A
, 0);
1541 expr
(A
) ::= LP select
(X
) RP.
{
1542 A
= sqlite3PExpr
(pParse
, TK_SELECT
, 0, 0);
1543 sqlite3PExprAddSelect
(pParse
, A
, X
);
1545 expr
(A
) ::= expr
(A
) in_op
(N
) LP select
(Y
) RP.
[IN
] {
1546 A
= sqlite3PExpr
(pParse
, TK_IN
, A
, 0);
1547 sqlite3PExprAddSelect
(pParse
, A
, Y
);
1548 if
( N
) A
= sqlite3PExpr
(pParse
, TK_NOT
, A
, 0);
1550 expr
(A
) ::= expr
(A
) in_op
(N
) nm
(Y
) dbnm
(Z
) paren_exprlist
(E
).
[IN
] {
1551 SrcList
*pSrc
= sqlite3SrcListAppend
(pParse
, 0,&Y
,&Z
);
1552 Select
*pSelect
= sqlite3SelectNew
(pParse
, 0,pSrc
,0,0,0,0,0,0);
1553 if
( E
) sqlite3SrcListFuncArgs
(pParse
, pSelect ? pSrc
: 0, E
);
1554 A
= sqlite3PExpr
(pParse
, TK_IN
, A
, 0);
1555 sqlite3PExprAddSelect
(pParse
, A
, pSelect
);
1556 if
( N
) A
= sqlite3PExpr
(pParse
, TK_NOT
, A
, 0);
1558 expr
(A
) ::= EXISTS LP select
(Y
) RP.
{
1560 p
= A
= sqlite3PExpr
(pParse
, TK_EXISTS
, 0, 0);
1561 sqlite3PExprAddSelect
(pParse
, p
, Y
);
1563 %endif SQLITE_OMIT_SUBQUERY
1565 /* CASE expressions */
1566 expr
(A
) ::= CASE case_operand
(X
) case_exprlist
(Y
) case_else
(Z
) END.
{
1567 A
= sqlite3PExpr
(pParse
, TK_CASE
, X
, 0);
1569 A
->x.pList
= Z ? sqlite3ExprListAppend
(pParse
,Y
,Z
) : Y
;
1570 sqlite3ExprSetHeightAndFlags
(pParse
, A
);
1572 sqlite3ExprListDelete
(pParse
->db
, Y
);
1573 sqlite3ExprDelete
(pParse
->db
, Z
);
1576 %type case_exprlist
{ExprList
*}
1577 %destructor case_exprlist
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
1578 case_exprlist
(A
) ::= case_exprlist
(A
) WHEN expr
(Y
) THEN expr
(Z
).
{
1579 A
= sqlite3ExprListAppend
(pParse
,A
, Y
);
1580 A
= sqlite3ExprListAppend
(pParse
,A
, Z
);
1582 case_exprlist
(A
) ::= WHEN expr
(Y
) THEN expr
(Z
).
{
1583 A
= sqlite3ExprListAppend
(pParse
,0, Y
);
1584 A
= sqlite3ExprListAppend
(pParse
,A
, Z
);
1586 %type case_else
{Expr
*}
1587 %destructor case_else
{sqlite3ExprDelete
(pParse
->db
, $$
);}
1588 case_else
(A
) ::= ELSE expr
(X
).
{A
= X
;}
1589 case_else
(A
) ::= .
{A
= 0;}
1590 %type case_operand
{Expr
*}
1591 %destructor case_operand
{sqlite3ExprDelete
(pParse
->db
, $$
);}
1592 case_operand
(A
) ::= expr
(A
).
1593 case_operand
(A
) ::= .
{A
= 0;}
1595 %type exprlist
{ExprList
*}
1596 %destructor exprlist
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
1597 %type nexprlist
{ExprList
*}
1598 %destructor nexprlist
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
1600 exprlist
(A
) ::= nexprlist
(A
).
1601 exprlist
(A
) ::= .
{A
= 0;}
1602 nexprlist
(A
) ::= nexprlist
(A
) COMMA expr
(Y
).
1603 {A
= sqlite3ExprListAppend
(pParse
,A
,Y
);}
1604 nexprlist
(A
) ::= expr
(Y
).
1605 {A
= sqlite3ExprListAppend
(pParse
,0,Y
); /*A-overwrites-Y*/}
1607 %ifndef SQLITE_OMIT_SUBQUERY
1608 /* A paren_exprlist is an optional expression list contained inside
1609 ** of parenthesis */
1610 %type paren_exprlist
{ExprList
*}
1611 %destructor paren_exprlist
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
1612 paren_exprlist
(A
) ::= .
{A
= 0;}
1613 paren_exprlist
(A
) ::= LP exprlist
(X
) RP.
{A
= X
;}
1614 %endif SQLITE_OMIT_SUBQUERY
1617 ///////////////////////////// The CREATE INDEX command ///////////////////////
1619 cmd
::= createkw
(S
) uniqueflag
(U
) INDEX ifnotexists
(NE
) nm
(X
) dbnm
(D
)
1620 ON nm
(Y
) LP sortlist
(Z
) RP where_opt
(W
).
{
1621 sqlite3CreateIndex
(pParse
, &X
, &D
,
1622 sqlite3SrcListAppend
(pParse
,0,&Y
,0), Z
, U
,
1623 &S
, W
, SQLITE_SO_ASC
, NE
, SQLITE_IDXTYPE_APPDEF
);
1624 if
( IN_RENAME_OBJECT
&& pParse
->pNewIndex
){
1625 sqlite3RenameTokenMap
(pParse
, pParse
->pNewIndex
->zName
, &Y
);
1629 %type uniqueflag
{int}
1630 uniqueflag
(A
) ::= UNIQUE.
{A
= OE_Abort
;}
1631 uniqueflag
(A
) ::= .
{A
= OE_None
;}
1634 // The eidlist non-terminal (Expression Id List) generates an ExprList
1635 // from a list of identifiers. The identifier names are in ExprList.a[].zName.
1636 // This list is stored in an ExprList rather than an IdList so that it
1637 // can be easily sent to sqlite3ColumnsExprList().
1639 // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
1640 // used for the arguments to an index. That is just an historical accident.
1642 // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted
1643 // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
1644 // places - places that might have been stored in the sqlite_schema table.
1645 // Those extra features were ignored. But because they might be in some
1646 // (busted) old databases, we need to continue parsing them when loading
1647 // historical schemas.
1649 %type eidlist
{ExprList
*}
1650 %destructor eidlist
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
1651 %type eidlist_opt
{ExprList
*}
1652 %destructor eidlist_opt
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
1655 /* Add a single new term to an ExprList that is used to store a
1656 ** list of identifiers. Report an error if the ID list contains
1657 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
1658 ** error while parsing a legacy schema.
1660 static ExprList
*parserAddExprIdListTerm
(
1667 ExprList
*p
= sqlite3ExprListAppend
(pParse
, pPrior
, 0);
1668 if
( (hasCollate || sortOrder
!=SQLITE_SO_UNDEFINED
)
1669 && pParse
->db
->init.busy
==0
1671 sqlite3ErrorMsg
(pParse
, "syntax error after column name \"%.*s\"",
1672 pIdToken
->n
, pIdToken
->z
);
1674 sqlite3ExprListSetName
(pParse
, p
, pIdToken
, 1);
1679 eidlist_opt
(A
) ::= .
{A
= 0;}
1680 eidlist_opt
(A
) ::= LP eidlist
(X
) RP.
{A
= X
;}
1681 eidlist
(A
) ::= eidlist
(A
) COMMA nm
(Y
) collate
(C
) sortorder
(Z
).
{
1682 A
= parserAddExprIdListTerm
(pParse
, A
, &Y
, C
, Z
);
1684 eidlist
(A
) ::= nm
(Y
) collate
(C
) sortorder
(Z
).
{
1685 A
= parserAddExprIdListTerm
(pParse
, 0, &Y
, C
, Z
); /*A-overwrites-Y*/
1689 collate
(C
) ::= .
{C
= 0;}
1690 collate
(C
) ::= COLLATE ids.
{C
= 1;}
1693 ///////////////////////////// The DROP INDEX command /////////////////////////
1695 cmd
::= DROP INDEX ifexists
(E
) fullname
(X
).
{sqlite3DropIndex
(pParse
, X
, E
);}
1697 ///////////////////////////// The VACUUM command /////////////////////////////
1699 %if
!SQLITE_OMIT_VACUUM
&& !SQLITE_OMIT_ATTACH
1701 %destructor vinto
{sqlite3ExprDelete
(pParse
->db
, $$
);}
1702 cmd
::= VACUUM vinto
(Y
).
{sqlite3Vacuum
(pParse
,0,Y
);}
1703 cmd
::= VACUUM nm
(X
) vinto
(Y
).
{sqlite3Vacuum
(pParse
,&X
,Y
);}
1704 vinto
(A
) ::= INTO expr
(X
).
{A
= X
;}
1705 vinto
(A
) ::= .
{A
= 0;}
1708 ///////////////////////////// The PRAGMA command /////////////////////////////
1710 %ifndef SQLITE_OMIT_PRAGMA
1711 cmd
::= PRAGMA nm
(X
) dbnm
(Z
).
{sqlite3Pragma
(pParse
,&X
,&Z
,0,0);}
1712 cmd
::= PRAGMA nm
(X
) dbnm
(Z
) EQ nmnum
(Y
).
{sqlite3Pragma
(pParse
,&X
,&Z
,&Y
,0);}
1713 cmd
::= PRAGMA nm
(X
) dbnm
(Z
) LP nmnum
(Y
) RP.
{sqlite3Pragma
(pParse
,&X
,&Z
,&Y
,0);}
1714 cmd
::= PRAGMA nm
(X
) dbnm
(Z
) EQ minus_num
(Y
).
1715 {sqlite3Pragma
(pParse
,&X
,&Z
,&Y
,1);}
1716 cmd
::= PRAGMA nm
(X
) dbnm
(Z
) LP minus_num
(Y
) RP.
1717 {sqlite3Pragma
(pParse
,&X
,&Z
,&Y
,1);}
1719 nmnum
(A
) ::= plus_num
(A
).
1722 nmnum
(A
) ::= DELETE
(A
).
1723 nmnum
(A
) ::= DEFAULT
(A
).
1724 %endif SQLITE_OMIT_PRAGMA
1725 %token_class number INTEGER|FLOAT.
1726 plus_num
(A
) ::= PLUS number
(X
).
{A
= X
;}
1727 plus_num
(A
) ::= number
(A
).
1728 minus_num
(A
) ::= MINUS number
(X
).
{A
= X
;}
1729 //////////////////////////// The CREATE TRIGGER command /////////////////////
1731 %ifndef SQLITE_OMIT_TRIGGER
1733 cmd
::= createkw trigger_decl
(A
) BEGIN trigger_cmd_list
(S
) END
(Z
).
{
1736 all.n
= (int)(Z.z
- A.z
) + Z.n
;
1737 sqlite3FinishTrigger
(pParse
, S
, &all
);
1740 trigger_decl
(A
) ::= temp
(T
) TRIGGER ifnotexists
(NOERR
) nm
(B
) dbnm
(Z
)
1741 trigger_time
(C
) trigger_event
(D
)
1742 ON fullname
(E
) foreach_clause when_clause
(G
).
{
1743 sqlite3BeginTrigger
(pParse
, &B
, &Z
, C
, D.a
, D.b
, E
, G
, T
, NOERR
);
1744 A
= (Z.n
==0?B
:Z
); /*A-overwrites-T*/
1746 assert
( pParse
->isCreate
); /* Set by createkw reduce action */
1747 pParse
->isCreate
= 0; /* But, should not be set for CREATE TRIGGER */
1751 %type trigger_time
{int}
1752 trigger_time
(A
) ::= BEFORE|AFTER
(X
).
{ A
= @X
; /*A-overwrites-X*/ }
1753 trigger_time
(A
) ::= INSTEAD OF.
{ A
= TK_INSTEAD
;}
1754 trigger_time
(A
) ::= .
{ A
= TK_BEFORE
; }
1756 %type trigger_event
{struct TrigEvent
}
1757 %destructor trigger_event
{sqlite3IdListDelete
(pParse
->db
, $$.b
);}
1758 trigger_event
(A
) ::= DELETE|INSERT
(X
).
{A.a
= @X
; /*A-overwrites-X*/ A.b
= 0;}
1759 trigger_event
(A
) ::= UPDATE
(X
).
{A.a
= @X
; /*A-overwrites-X*/ A.b
= 0;}
1760 trigger_event
(A
) ::= UPDATE OF idlist
(X
).
{A.a
= TK_UPDATE
; A.b
= X
;}
1762 foreach_clause
::= .
1763 foreach_clause
::= FOR EACH ROW.
1765 %type when_clause
{Expr
*}
1766 %destructor when_clause
{sqlite3ExprDelete
(pParse
->db
, $$
);}
1767 when_clause
(A
) ::= .
{ A
= 0; }
1768 when_clause
(A
) ::= WHEN expr
(X
).
{ A
= X
; }
1770 %type trigger_cmd_list
{TriggerStep
*}
1771 %destructor trigger_cmd_list
{sqlite3DeleteTriggerStep
(pParse
->db
, $$
);}
1772 trigger_cmd_list
(A
) ::= trigger_cmd_list
(A
) trigger_cmd
(X
) SEMI.
{
1773 A
->pLast
->pNext
= X
;
1776 trigger_cmd_list
(A
) ::= trigger_cmd
(A
) SEMI.
{
1780 // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
1781 // statements within triggers. We make a specific error message for this
1782 // since it is an exception to the default grammar rules.
1785 tridxby
::= INDEXED BY nm.
{
1786 sqlite3ErrorMsg
(pParse
,
1787 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
1790 tridxby
::= NOT INDEXED.
{
1791 sqlite3ErrorMsg
(pParse
,
1792 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
1798 %type trigger_cmd
{TriggerStep
*}
1799 %destructor trigger_cmd
{sqlite3DeleteTriggerStep
(pParse
->db
, $$
);}
1802 UPDATE
(B
) orconf
(R
) xfullname
(X
) tridxby SET setlist
(Y
) from
(F
) where_opt
(Z
) scanpt
(E
).
1803 {A
= sqlite3TriggerUpdateStep
(pParse
, X
, F
, Y
, Z
, R
, B.z
, E
);}
1806 trigger_cmd
(A
) ::= scanpt
(B
) insert_cmd
(R
) INTO
1807 xfullname
(X
) idlist_opt
(F
) select
(S
) upsert
(U
) scanpt
(Z
).
{
1808 A
= sqlite3TriggerInsertStep
(pParse
,X
,F
,S
,R
,U
,B
,Z
);/*A-overwrites-R*/
1811 trigger_cmd
(A
) ::= DELETE
(B
) FROM xfullname
(X
) tridxby where_opt
(Y
) scanpt
(E
).
1812 {A
= sqlite3TriggerDeleteStep
(pParse
, X
, Y
, B.z
, E
);}
1815 trigger_cmd
(A
) ::= scanpt
(B
) select
(X
) scanpt
(E
).
1816 {A
= sqlite3TriggerSelectStep
(pParse
->db
, X
, B
, E
); /*A-overwrites-X*/}
1818 // The special RAISE expression that may occur in trigger programs
1819 expr
(A
) ::= RAISE LP IGNORE RP.
{
1820 A
= sqlite3PExpr
(pParse
, TK_RAISE
, 0, 0);
1822 A
->affExpr
= OE_Ignore
;
1825 expr
(A
) ::= RAISE LP raisetype
(T
) COMMA expr
(Z
) RP.
{
1826 A
= sqlite3PExpr
(pParse
, TK_RAISE
, Z
, 0);
1828 A
->affExpr
= (char)T
;
1831 %endif
!SQLITE_OMIT_TRIGGER
1833 %type raisetype
{int}
1834 raisetype
(A
) ::= ROLLBACK.
{A
= OE_Rollback
;}
1835 raisetype
(A
) ::= ABORT.
{A
= OE_Abort
;}
1836 raisetype
(A
) ::= FAIL.
{A
= OE_Fail
;}
1839 //////////////////////// DROP TRIGGER statement //////////////////////////////
1840 %ifndef SQLITE_OMIT_TRIGGER
1841 cmd
::= DROP TRIGGER ifexists
(NOERR
) fullname
(X
).
{
1842 sqlite3DropTrigger
(pParse
,X
,NOERR
);
1844 %endif
!SQLITE_OMIT_TRIGGER
1846 //////////////////////// ATTACH DATABASE file AS name /////////////////////////
1847 %ifndef SQLITE_OMIT_ATTACH
1848 cmd
::= ATTACH database_kw_opt expr
(F
) AS expr
(D
) key_opt
(K
).
{
1849 sqlite3Attach
(pParse
, F
, D
, K
);
1851 cmd
::= DETACH database_kw_opt expr
(D
).
{
1852 sqlite3Detach
(pParse
, D
);
1855 %type key_opt
{Expr
*}
1856 %destructor key_opt
{sqlite3ExprDelete
(pParse
->db
, $$
);}
1857 key_opt
(A
) ::= .
{ A
= 0; }
1858 key_opt
(A
) ::= KEY expr
(X
).
{ A
= X
; }
1860 database_kw_opt
::= DATABASE.
1861 database_kw_opt
::= .
1862 %endif SQLITE_OMIT_ATTACH
1864 ////////////////////////// REINDEX collation //////////////////////////////////
1865 %ifndef SQLITE_OMIT_REINDEX
1866 cmd
::= REINDEX.
{sqlite3Reindex
(pParse
, 0, 0);}
1867 cmd
::= REINDEX nm
(X
) dbnm
(Y
).
{sqlite3Reindex
(pParse
, &X
, &Y
);}
1868 %endif SQLITE_OMIT_REINDEX
1870 /////////////////////////////////// ANALYZE ///////////////////////////////////
1871 %ifndef SQLITE_OMIT_ANALYZE
1872 cmd
::= ANALYZE.
{sqlite3Analyze
(pParse
, 0, 0);}
1873 cmd
::= ANALYZE nm
(X
) dbnm
(Y
).
{sqlite3Analyze
(pParse
, &X
, &Y
);}
1876 //////////////////////// ALTER TABLE table ... ////////////////////////////////
1877 %ifndef SQLITE_OMIT_ALTERTABLE
1878 %ifndef SQLITE_OMIT_VIRTUALTABLE
1879 cmd
::= ALTER TABLE fullname
(X
) RENAME TO nm
(Z
).
{
1880 sqlite3AlterRenameTable
(pParse
,X
,&Z
);
1883 // The ALTER TABLE ADD COLUMN command. This is broken into two sections so
1884 // that sqlite3AlterBeginAddColumn() is called before parsing the various
1885 // constraints and so on (carglist) attached to the new column definition.
1886 cmd
::= alter_add
(Y
) carglist.
{
1887 Y.n
= (int)(pParse
->sLastToken.z
-Y.z
) + pParse
->sLastToken.n
;
1888 sqlite3AlterFinishAddColumn
(pParse
, &Y
);
1890 alter_add
(A
) ::= ALTER TABLE fullname
(X
) ADD kwcolumn_opt nm
(Y
) typetoken
(Z
).
{
1891 disableLookaside
(pParse
);
1892 sqlite3AlterBeginAddColumn
(pParse
, X
);
1893 sqlite3AddColumn
(pParse
, Y
, Z
);
1897 cmd
::= ALTER TABLE fullname
(X
) DROP kwcolumn_opt nm
(Y
).
{
1898 sqlite3AlterDropColumn
(pParse
, X
, &Y
);
1900 cmd
::= ALTER TABLE fullname
(X
) RENAME kwcolumn_opt nm
(Y
) TO nm
(Z
).
{
1901 sqlite3AlterRenameColumn
(pParse
, X
, &Y
, &Z
);
1903 cmd
::= ALTER TABLE fullname
(X
) DROP CONSTRAINT nm
(Y
).
{
1904 sqlite3AlterDropConstraint
(pParse
, X
, &Y
, 0);
1906 cmd
::= ALTER TABLE fullname
(X
) ALTER kwcolumn_opt nm
(Y
) DROP NOT NULL.
{
1907 sqlite3AlterDropConstraint
(pParse
, X
, 0, &Y
);
1909 cmd
::= ALTER TABLE fullname
(X
) ALTER kwcolumn_opt nm
(Y
) SET NOT
(Z
) NULL onconf.
{
1910 sqlite3AlterSetNotNull
(pParse
, X
, &Y
, &Z
);
1912 cmd
::= ALTER TABLE fullname
(X
) ADD CONSTRAINT
(Y
) nm
(Z
) CHECK LP
(A
) expr RP
(B
) onconf.
{
1913 sqlite3AlterAddConstraint
(pParse
, X
, &Y
, &Z
, A.z
+1, (B.z
-A.z
-1));
1915 cmd
::= ALTER TABLE fullname
(X
) ADD CHECK
(Y
) LP
(A
) expr RP
(B
) onconf.
{
1916 sqlite3AlterAddConstraint
(pParse
, X
, &Y
, 0, A.z
+1, (B.z
-A.z
-1));
1920 kwcolumn_opt
::= COLUMNKW.
1922 %endif SQLITE_OMIT_VIRTUALTABLE
1923 %endif SQLITE_OMIT_ALTERTABLE
1925 //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
1926 %ifndef SQLITE_OMIT_VIRTUALTABLE
1927 cmd
::= create_vtab.
{sqlite3VtabFinishParse
(pParse
,0);}
1928 cmd
::= create_vtab LP vtabarglist RP
(X
).
{sqlite3VtabFinishParse
(pParse
,&X
);}
1929 create_vtab
::= createkw VIRTUAL TABLE ifnotexists
(E
)
1930 nm
(X
) dbnm
(Y
) USING nm
(Z
).
{
1931 sqlite3VtabBeginParse
(pParse
, &X
, &Y
, &Z
, E
);
1933 vtabarglist
::= vtabarg.
1934 vtabarglist
::= vtabarglist COMMA vtabarg.
1935 vtabarg
::= .
{sqlite3VtabArgInit
(pParse
);}
1936 vtabarg
::= vtabarg vtabargtoken.
1937 vtabargtoken
::= ANY
(X
).
{sqlite3VtabArgExtend
(pParse
,&X
);}
1938 vtabargtoken
::= lp anylist RP
(X
).
{sqlite3VtabArgExtend
(pParse
,&X
);}
1939 lp
::= LP
(X
).
{sqlite3VtabArgExtend
(pParse
,&X
);}
1941 anylist
::= anylist LP anylist RP.
1942 anylist
::= anylist ANY.
1943 %endif SQLITE_OMIT_VIRTUALTABLE
1946 //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
1947 %type wqlist
{With
*}
1948 %destructor wqlist
{sqlite3WithDelete
(pParse
->db
, $$
);}
1950 // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable
1953 %ifndef SQLITE_OMIT_CTE
1954 with
::= WITH wqlist
(W
).
{ sqlite3WithPush
(pParse
, W
, 1); }
1955 with
::= WITH RECURSIVE wqlist
(W
).
{ sqlite3WithPush
(pParse
, W
, 1); }
1958 wqas
(A
) ::= AS.
{A
= M10d_Any
;}
1959 wqas
(A
) ::= AS MATERIALIZED.
{A
= M10d_Yes
;}
1960 wqas
(A
) ::= AS NOT MATERIALIZED.
{A
= M10d_No
;}
1961 wqitem
(A
) ::= withnm
(X
) eidlist_opt
(Y
) wqas
(M
) LP select
(Z
) RP.
{
1962 A
= sqlite3CteNew
(pParse
, &X
, Y
, Z
, M
); /*A-overwrites-X*/
1964 withnm
(A
) ::= nm
(A
).
{pParse
->bHasWith
= 1;}
1965 wqlist
(A
) ::= wqitem
(X
).
{
1966 A
= sqlite3WithAdd
(pParse
, 0, X
); /*A-overwrites-X*/
1968 wqlist
(A
) ::= wqlist
(A
) COMMA wqitem
(X
).
{
1969 A
= sqlite3WithAdd
(pParse
, A
, X
);
1971 %endif SQLITE_OMIT_CTE
1973 //////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
1974 // These must be at the end of this file. Specifically, the rules that
1975 // introduce tokens WINDOW, OVER and FILTER must appear last. This causes
1976 // the integer values assigned to these tokens to be larger than all other
1977 // tokens that may be output by the tokenizer except TK_SPACE, TK_COMMENT,
1980 %ifndef SQLITE_OMIT_WINDOWFUNC
1981 %type windowdefn_list
{Window
*}
1982 %destructor windowdefn_list
{sqlite3WindowListDelete
(pParse
->db
, $$
);}
1983 windowdefn_list
(A
) ::= windowdefn
(A
).
1984 windowdefn_list
(A
) ::= windowdefn_list
(Y
) COMMA windowdefn
(Z
).
{
1986 sqlite3WindowChain
(pParse
, Z
, Y
);
1991 %type windowdefn
{Window
*}
1992 %destructor windowdefn
{sqlite3WindowDelete
(pParse
->db
, $$
);}
1993 windowdefn
(A
) ::= nm
(X
) AS LP window
(Y
) RP.
{
1995 Y
->zName
= sqlite3DbStrNDup
(pParse
->db
, X.z
, X.n
);
2000 %type window
{Window
*}
2001 %destructor window
{sqlite3WindowDelete
(pParse
->db
, $$
);}
2003 %type frame_opt
{Window
*}
2004 %destructor frame_opt
{sqlite3WindowDelete
(pParse
->db
, $$
);}
2006 %type part_opt
{ExprList
*}
2007 %destructor part_opt
{sqlite3ExprListDelete
(pParse
->db
, $$
);}
2009 %type filter_clause
{Expr
*}
2010 %destructor filter_clause
{sqlite3ExprDelete
(pParse
->db
, $$
);}
2012 %type over_clause
{Window
*}
2013 %destructor over_clause
{sqlite3WindowDelete
(pParse
->db
, $$
);}
2015 %type filter_over
{Window
*}
2016 %destructor filter_over
{sqlite3WindowDelete
(pParse
->db
, $$
);}
2018 %type range_or_rows
{int}
2020 %type frame_bound
{struct FrameBound
}
2021 %destructor frame_bound
{sqlite3ExprDelete
(pParse
->db
, $$.pExpr
);}
2022 %type frame_bound_s
{struct FrameBound
}
2023 %destructor frame_bound_s
{sqlite3ExprDelete
(pParse
->db
, $$.pExpr
);}
2024 %type frame_bound_e
{struct FrameBound
}
2025 %destructor frame_bound_e
{sqlite3ExprDelete
(pParse
->db
, $$.pExpr
);}
2027 window
(A
) ::= PARTITION BY nexprlist
(X
) orderby_opt
(Y
) frame_opt
(Z
).
{
2028 A
= sqlite3WindowAssemble
(pParse
, Z
, X
, Y
, 0);
2030 window
(A
) ::= nm
(W
) PARTITION BY nexprlist
(X
) orderby_opt
(Y
) frame_opt
(Z
).
{
2031 A
= sqlite3WindowAssemble
(pParse
, Z
, X
, Y
, &W
);
2033 window
(A
) ::= ORDER BY sortlist
(Y
) frame_opt
(Z
).
{
2034 A
= sqlite3WindowAssemble
(pParse
, Z
, 0, Y
, 0);
2036 window
(A
) ::= nm
(W
) ORDER BY sortlist
(Y
) frame_opt
(Z
).
{
2037 A
= sqlite3WindowAssemble
(pParse
, Z
, 0, Y
, &W
);
2039 window
(A
) ::= frame_opt
(A
).
2040 window
(A
) ::= nm
(W
) frame_opt
(Z
).
{
2041 A
= sqlite3WindowAssemble
(pParse
, Z
, 0, 0, &W
);
2044 frame_opt
(A
) ::= .
{
2045 A
= sqlite3WindowAlloc
(pParse
, 0, TK_UNBOUNDED
, 0, TK_CURRENT
, 0, 0);
2047 frame_opt
(A
) ::= range_or_rows
(X
) frame_bound_s
(Y
) frame_exclude_opt
(Z
).
{
2048 A
= sqlite3WindowAlloc
(pParse
, X
, Y.eType
, Y.pExpr
, TK_CURRENT
, 0, Z
);
2050 frame_opt
(A
) ::= range_or_rows
(X
) BETWEEN frame_bound_s
(Y
) AND
2051 frame_bound_e
(Z
) frame_exclude_opt
(W
).
{
2052 A
= sqlite3WindowAlloc
(pParse
, X
, Y.eType
, Y.pExpr
, Z.eType
, Z.pExpr
, W
);
2055 range_or_rows
(A
) ::= RANGE|ROWS|GROUPS
(X
).
{A
= @X
; /*A-overwrites-X*/}
2057 frame_bound_s
(A
) ::= frame_bound
(X
).
{A
= X
;}
2058 frame_bound_s
(A
) ::= UNBOUNDED
(X
) PRECEDING.
{A.eType
= @X
; A.pExpr
= 0;}
2059 frame_bound_e
(A
) ::= frame_bound
(X
).
{A
= X
;}
2060 frame_bound_e
(A
) ::= UNBOUNDED
(X
) FOLLOWING.
{A.eType
= @X
; A.pExpr
= 0;}
2062 frame_bound
(A
) ::= expr
(X
) PRECEDING|FOLLOWING
(Y
).
2063 {A.eType
= @Y
; A.pExpr
= X
;}
2064 frame_bound
(A
) ::= CURRENT
(X
) ROW.
{A.eType
= @X
; A.pExpr
= 0;}
2066 %type frame_exclude_opt
{u8
}
2067 frame_exclude_opt
(A
) ::= .
{A
= 0;}
2068 frame_exclude_opt
(A
) ::= EXCLUDE frame_exclude
(X
).
{A
= X
;}
2070 %type frame_exclude
{u8
}
2071 frame_exclude
(A
) ::= NO
(X
) OTHERS.
{A
= @X
; /*A-overwrites-X*/}
2072 frame_exclude
(A
) ::= CURRENT
(X
) ROW.
{A
= @X
; /*A-overwrites-X*/}
2073 frame_exclude
(A
) ::= GROUP|TIES
(X
).
{A
= @X
; /*A-overwrites-X*/}
2076 %type window_clause
{Window
*}
2077 %destructor window_clause
{sqlite3WindowListDelete
(pParse
->db
, $$
);}
2078 window_clause
(A
) ::= WINDOW windowdefn_list
(B
).
{ A
= B
; }
2080 filter_over
(A
) ::= filter_clause
(F
) over_clause
(O
).
{
2084 sqlite3ExprDelete
(pParse
->db
, F
);
2088 filter_over
(A
) ::= over_clause
(O
).
{
2091 filter_over
(A
) ::= filter_clause
(F
).
{
2092 A
= (Window
*)sqlite3DbMallocZero
(pParse
->db
, sizeof
(Window
));
2094 A
->eFrmType
= TK_FILTER
;
2097 sqlite3ExprDelete
(pParse
->db
, F
);
2101 over_clause
(A
) ::= OVER LP window
(Z
) RP.
{
2105 over_clause
(A
) ::= OVER nm
(Z
).
{
2106 A
= (Window
*)sqlite3DbMallocZero
(pParse
->db
, sizeof
(Window
));
2108 A
->zName
= sqlite3DbStrNDup
(pParse
->db
, Z.z
, Z.n
);
2112 filter_clause
(A
) ::= FILTER LP WHERE expr
(X
) RP.
{ A
= X
; }
2113 %endif
/* SQLITE_OMIT_WINDOWFUNC */
2116 ** The code generator needs some extra TK_ token values for tokens that
2117 ** are synthesized and do not actually appear in the grammar:
2120 COLUMN
/* Reference to a table column */
2121 AGG_FUNCTION
/* An aggregate function */
2122 AGG_COLUMN
/* An aggregated column */
2123 TRUEFALSE
/* True or false keyword */
2124 ISNOT
/* Combination of IS and NOT */
2125 FUNCTION
/* A function invocation */
2126 UPLUS
/* Unary plus */
2127 UMINUS
/* Unary minus */
2128 TRUTH
/* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
2129 REGISTER
/* Reference to a VDBE register */
2131 SELECT_COLUMN
/* Choose a single column from a multi-column SELECT */
2132 IF_NULL_ROW
/* the if-null-row operator */
2133 ASTERISK
/* The "*" in count(*) and similar */
2134 SPAN
/* The span operator */
2135 ERROR
/* An expression containing an error */
2138 term
(A
) ::= QNUMBER
(X
).
{
2139 A
=tokenExpr
(pParse
,@X
,X
);
2140 sqlite3DequoteNumber
(pParse
, A
);
2143 /* There must be no more than 255 tokens defined above. If this grammar
2144 ** is extended with new rules and tokens, they must either be so few in
2145 ** number that TK_SPAN is no more than 255, or else the new tokens must
2146 ** appear after this line.
2150 # error too many tokens in the grammar
2155 ** The TK_SPACE, TK_COMMENT, and TK_ILLEGAL tokens must be the last three
2156 ** tokens. The parser depends on this. Those tokens are not used in any
2157 ** grammar rule. They are only used by the tokenizer. Declare them last
2158 ** so that they are guaranteed to be the last three.
2160 %token SPACE COMMENT ILLEGAL.