sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | %include { |
| 2 | /* |
| 3 | ** 2001-09-15 |
| 4 | ** |
| 5 | ** The author disclaims copyright to this source code. In place of |
| 6 | ** a legal notice, here is a blessing: |
| 7 | ** |
| 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. |
| 11 | ** |
| 12 | ************************************************************************* |
| 13 | ** 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 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. |
| 21 | */ |
| 22 | } |
| 23 | |
| 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 |
| 29 | |
| 30 | // All token codes are small integers with #defines that begin with "TK_" |
| 31 | %token_prefix TK_ |
| 32 | |
| 33 | // The type of the data attached to each token is Token. This is also the |
| 34 | // default type for non-terminals. |
| 35 | // |
| 36 | %token_type {Token} |
| 37 | %default_type {Token} |
| 38 | |
| 39 | // An extra argument to the constructor for the parser, which is available |
| 40 | // to all actions. |
| 41 | %extra_context {Parse *pParse} |
| 42 | |
| 43 | // This code runs whenever there is a syntax error |
| 44 | // |
| 45 | %syntax_error { |
| 46 | UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ |
| 47 | if( TOKEN.z[0] ){ |
| 48 | parserSyntaxError(pParse, &TOKEN); |
| 49 | }else{ |
| 50 | sqlite3ErrorMsg(pParse, "incomplete input"); |
| 51 | } |
| 52 | } |
| 53 | %stack_overflow { |
| 54 | if( pParse->nErr==0 ) sqlite3ErrorMsg(pParse, "Recursion limit"); |
| 55 | } |
| 56 | |
| 57 | // The name of the generated procedure that implements the parser |
| 58 | // is as follows: |
| 59 | %name sqlite3Parser |
| 60 | |
| 61 | // The following text is included near the beginning of the C source |
| 62 | // code file that implements the parser. |
| 63 | // |
| 64 | %include { |
| 65 | #include "sqliteInt.h" |
| 66 | |
| 67 | /* |
| 68 | ** Verify that the pParse->isCreate field is set |
| 69 | */ |
| 70 | #define ASSERT_IS_CREATE assert(pParse->isCreate) |
| 71 | |
| 72 | /* |
| 73 | ** Disable all error recovery processing in the parser push-down |
| 74 | ** automaton. |
| 75 | */ |
| 76 | #define YYNOERRORRECOVERY 1 |
| 77 | |
| 78 | /* |
| 79 | ** Make yytestcase() the same as testcase() |
| 80 | */ |
| 81 | #define yytestcase(X) testcase(X) |
| 82 | |
| 83 | /* |
| 84 | ** Indicate that sqlite3ParserFree() will never be called with a null |
| 85 | ** pointer. |
| 86 | */ |
| 87 | #define YYPARSEFREENEVERNULL 1 |
| 88 | |
| 89 | /* |
| 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 |
| 96 | ** omitted. |
| 97 | */ |
| 98 | #ifdef SQLITE_AMALGAMATION |
| 99 | # define sqlite3Parser_ENGINEALWAYSONSTACK 1 |
| 100 | #endif |
| 101 | |
| 102 | /* |
| 103 | ** Alternative datatype for the argument to the malloc() routine passed |
| 104 | ** into sqlite3ParserAlloc(). The default is size_t. |
| 105 | */ |
| 106 | #define YYMALLOCARGTYPE u64 |
| 107 | |
| 108 | /* |
| 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 |
| 112 | ** |
| 113 | ** UPDATE ON (a,b,c) |
| 114 | ** |
| 115 | ** Then the "b" IdList records the list "a,b,c". |
| 116 | */ |
| 117 | struct TrigEvent { int a; IdList * b; }; |
| 118 | |
| 119 | struct FrameBound { int eType; Expr *pExpr; }; |
| 120 | |
| 121 | /* |
| 122 | ** Generate a syntax error |
| 123 | */ |
| 124 | static void parserSyntaxError(Parse *pParse, Token *p){ |
| 125 | sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", p); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | ** Disable lookaside memory allocation for objects that might be |
| 130 | ** shared across database connections. |
| 131 | */ |
| 132 | static void disableLookaside(Parse *pParse){ |
| 133 | sqlite3 *db = pParse->db; |
| 134 | pParse->disableLookaside++; |
| 135 | #ifdef SQLITE_DEBUG |
| 136 | pParse->isCreate = 1; |
| 137 | #endif |
| 138 | memset(&pParse->u1.cr, 0, sizeof(pParse->u1.cr)); |
| 139 | DisableLookaside; |
| 140 | } |
| 141 | |
| 142 | #if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \ |
| 143 | && defined(SQLITE_UDL_CAPABLE_PARSER) |
| 144 | /* |
| 145 | ** Issue an error message if an ORDER BY or LIMIT clause occurs on an |
| 146 | ** UPDATE or DELETE statement. |
| 147 | */ |
| 148 | static void updateDeleteLimitError( |
| 149 | Parse *pParse, |
| 150 | ExprList *pOrderBy, |
| 151 | Expr *pLimit |
| 152 | ){ |
| 153 | if( pOrderBy ){ |
| 154 | sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\""); |
| 155 | }else{ |
| 156 | sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\""); |
| 157 | } |
| 158 | sqlite3ExprListDelete(pParse->db, pOrderBy); |
| 159 | sqlite3ExprDelete(pParse->db, pLimit); |
| 160 | } |
| 161 | #endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */ |
| 162 | |
| 163 | } // end %include |
| 164 | |
| 165 | // Input is a single SQL command |
| 166 | input ::= cmdlist. |
| 167 | cmdlist ::= cmdlist ecmd. |
| 168 | cmdlist ::= ecmd. |
| 169 | ecmd ::= SEMI. |
| 170 | ecmd ::= cmdx SEMI. |
| 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); } |
| 177 | |
| 178 | ///////////////////// Begin and end transactions. //////////////////////////// |
| 179 | // |
| 180 | |
| 181 | cmd ::= BEGIN transtype(Y) trans_opt. {sqlite3BeginTransaction(pParse, Y);} |
| 182 | trans_opt ::= . |
| 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);} |
| 192 | |
| 193 | savepoint_opt ::= SAVEPOINT. |
| 194 | savepoint_opt ::= . |
| 195 | cmd ::= SAVEPOINT nm(X). { |
| 196 | sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X); |
| 197 | } |
| 198 | cmd ::= RELEASE savepoint_opt nm(X). { |
| 199 | sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X); |
| 200 | } |
| 201 | cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). { |
| 202 | sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X); |
| 203 | } |
| 204 | |
| 205 | ///////////////////// The CREATE TABLE statement //////////////////////////// |
| 206 | // |
| 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); |
| 210 | } |
| 211 | createkw(A) ::= CREATE(A). { |
| 212 | disableLookaside(pParse); |
| 213 | } |
| 214 | |
| 215 | %type ifnotexists {int} |
| 216 | ifnotexists(A) ::= . {A = 0;} |
| 217 | ifnotexists(A) ::= IF NOT EXISTS. {A = 1;} |
| 218 | %type temp {int} |
| 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); |
| 225 | } |
| 226 | create_table_args ::= AS select(S). { |
| 227 | sqlite3EndTable(pParse,0,0,0,S); |
| 228 | sqlite3SelectDelete(pParse->db, S); |
| 229 | } |
| 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; |
| 238 | }else{ |
| 239 | A = 0; |
| 240 | sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z); |
| 241 | } |
| 242 | } |
| 243 | table_option(A) ::= nm(X). { |
| 244 | if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){ |
| 245 | A = TF_Strict; |
| 246 | }else{ |
| 247 | A = 0; |
| 248 | sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z); |
| 249 | } |
| 250 | } |
| 251 | columnlist ::= columnlist COMMA columnname carglist. |
| 252 | columnlist ::= columnname carglist. |
| 253 | columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);} |
| 254 | |
| 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. |
| 262 | // |
| 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. |
| 267 | |
| 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. |
| 271 | // |
| 272 | %fallback ID |
| 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 |
| 279 | NULLS FIRST LAST |
| 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 |
| 288 | WITHIN |
| 289 | %endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES |
| 290 | %ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 291 | GENERATED ALWAYS |
| 292 | %endif |
| 293 | MATERIALIZED |
| 294 | REINDEX RENAME CTIME_KW IF |
| 295 | . |
| 296 | %wildcard ANY. |
| 297 | |
| 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. |
| 302 | // |
| 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 |
| 307 | // constraint. |
| 308 | // |
| 309 | %left OR. |
| 310 | %left AND. |
| 311 | %right NOT. |
| 312 | %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. |
| 313 | %left GT LE LT GE. |
| 314 | %right ESCAPE. |
| 315 | %left BITAND BITOR LSHIFT RSHIFT. |
| 316 | %left PLUS MINUS. |
| 317 | %left STAR SLASH REM. |
| 318 | %left CONCAT PTR. |
| 319 | %left COLLATE. |
| 320 | %right BITNOT. |
| 321 | %nonassoc ON. |
| 322 | |
| 323 | // An IDENTIFIER can be a generic identifier, or one of several |
| 324 | // keywords. Any non-standard keyword can also be an identifier. |
| 325 | // |
| 326 | %token_class id ID|INDEXED. |
| 327 | |
| 328 | // And "ids" is an identifier-or-string. |
| 329 | // |
| 330 | %token_class ids ID|STRING. |
| 331 | |
| 332 | // An identifier or a join-keyword |
| 333 | // |
| 334 | %token_class idj ID|INDEXED|JOIN_KW. |
| 335 | |
| 336 | // The name of a column or table can be any of the following: |
| 337 | // |
| 338 | %type nm {Token} |
| 339 | nm(A) ::= idj(A). |
| 340 | nm(A) ::= STRING(A). |
| 341 | |
| 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. |
| 345 | // |
| 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); |
| 351 | } |
| 352 | typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). { |
| 353 | A.n = (int)(&Y.z[Y.n] - A.z); |
| 354 | } |
| 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);} |
| 358 | signed ::= plus_num. |
| 359 | signed ::= minus_num. |
| 360 | |
| 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. |
| 365 | // For example: |
| 366 | // |
| 367 | // something ::= .... scanpt(A) phrase scanpt(Z). |
| 368 | // |
| 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. |
| 373 | // |
| 374 | %type scanpt {const char*} |
| 375 | scanpt(A) ::= . { |
| 376 | assert( yyLookahead!=YYNOCODE ); |
| 377 | A = yyLookaheadToken.z; |
| 378 | } |
| 379 | scantok(A) ::= . { |
| 380 | assert( yyLookahead!=YYNOCODE ); |
| 381 | A = yyLookaheadToken; |
| 382 | } |
| 383 | |
| 384 | // "carglist" is a list of additional constraints that come after the |
| 385 | // column name and column type in a CREATE TABLE statement. |
| 386 | // |
| 387 | carglist ::= carglist ccons. |
| 388 | carglist ::= . |
| 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]); |
| 399 | } |
| 400 | ccons ::= DEFAULT scantok id(X). { |
| 401 | Expr *p = tokenExpr(pParse, TK_STRING, X); |
| 402 | if( p ){ |
| 403 | sqlite3ExprIdToTrueFalse(p); |
| 404 | testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) ); |
| 405 | } |
| 406 | sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n); |
| 407 | } |
| 408 | |
| 409 | // In addition to the type name, we also care about the primary key and |
| 410 | // UNIQUE constraints. |
| 411 | // |
| 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);} |
| 427 | |
| 428 | // The optional AUTOINCREMENT keyword |
| 429 | %type autoinc {int} |
| 430 | autoinc(X) ::= . {X = 0;} |
| 431 | autoinc(X) ::= AUTOINCR. {X = 1;} |
| 432 | |
| 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 |
| 436 | // check fails. |
| 437 | // |
| 438 | %type refargs {int} |
| 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; } |
| 446 | %type refact {int} |
| 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;} |
| 459 | |
| 460 | conslist_opt(A) ::= . {A.n = 0; A.z = 0;} |
| 461 | conslist_opt(A) ::= COMMA(A) conslist. |
| 462 | conslist ::= conslist tconscomma tcons. |
| 463 | conslist ::= tcons. |
| 464 | tconscomma ::= COMMA. {ASSERT_IS_CREATE; pParse->u1.cr.constraintName.n = 0;} |
| 465 | tconscomma ::= . |
| 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); |
| 478 | } |
| 479 | %type defer_subclause_opt {int} |
| 480 | defer_subclause_opt(A) ::= . {A = 0;} |
| 481 | defer_subclause_opt(A) ::= defer_subclause(A). |
| 482 | |
| 483 | // The following is a non-standard extension that allows us to declare the |
| 484 | // default behavior when there is a constraint conflict. |
| 485 | // |
| 486 | %type onconf {int} |
| 487 | %type orconf {int} |
| 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;} |
| 496 | |
| 497 | ////////////////////////// The DROP TABLE ///////////////////////////////////// |
| 498 | // |
| 499 | cmd ::= DROP TABLE ifexists(E) fullname(X). { |
| 500 | sqlite3DropTable(pParse, X, 0, E); |
| 501 | } |
| 502 | %type ifexists {int} |
| 503 | ifexists(A) ::= IF EXISTS. {A = 1;} |
| 504 | ifexists(A) ::= . {A = 0;} |
| 505 | |
| 506 | ///////////////////// The CREATE VIEW statement ///////////////////////////// |
| 507 | // |
| 508 | %ifndef SQLITE_OMIT_VIEW |
| 509 | cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C) |
| 510 | AS select(S). { |
| 511 | sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E); |
| 512 | } |
| 513 | cmd ::= DROP VIEW ifexists(E) fullname(X). { |
| 514 | sqlite3DropTable(pParse, X, 1, E); |
| 515 | } |
| 516 | %endif SQLITE_OMIT_VIEW |
| 517 | |
| 518 | //////////////////////// The SELECT statement ///////////////////////////////// |
| 519 | // |
| 520 | cmd ::= select(X). { |
| 521 | SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0}; |
| 522 | if( (pParse->db->mDbFlags & DBFLAG_EncodingFixed)!=0 |
| 523 | || sqlite3ReadSchema(pParse)==SQLITE_OK |
| 524 | ){ |
| 525 | sqlite3Select(pParse, X, &dest); |
| 526 | } |
| 527 | sqlite3SelectDelete(pParse->db, X); |
| 528 | } |
| 529 | |
| 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, $$);} |
| 536 | |
| 537 | %include { |
| 538 | /* |
| 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. |
| 542 | */ |
| 543 | static void parserDoubleLinkSelect(Parse *pParse, Select *p){ |
| 544 | assert( p!=0 ); |
| 545 | if( p->pPrior ){ |
| 546 | Select *pNext = 0, *pLoop = p; |
| 547 | int mxSelect, cnt = 1; |
| 548 | while(1){ |
| 549 | pLoop->pNext = pNext; |
| 550 | pLoop->selFlags |= SF_Compound; |
| 551 | pNext = pLoop; |
| 552 | pLoop = pLoop->pPrior; |
| 553 | if( pLoop==0 ) break; |
| 554 | cnt++; |
| 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)); |
| 559 | break; |
| 560 | } |
| 561 | } |
| 562 | if( (p->selFlags & (SF_MultiValue|SF_Values))==0 |
| 563 | && (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 |
| 564 | && cnt>mxSelect |
| 565 | ){ |
| 566 | sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 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. |
| 573 | */ |
| 574 | static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){ |
| 575 | if( pSelect ){ |
| 576 | pSelect->pWith = pWith; |
| 577 | parserDoubleLinkSelect(pParse, pSelect); |
| 578 | }else{ |
| 579 | sqlite3WithDelete(pParse->db, pWith); |
| 580 | } |
| 581 | return pSelect; |
| 582 | } |
| 583 | |
| 584 | /* Memory allocator for parser stack resizing. This is a thin wrapper around |
| 585 | ** sqlite3_realloc() that includes a call to sqlite3FaultSim() to facilitate |
| 586 | ** testing. |
| 587 | */ |
| 588 | static void *parserStackRealloc( |
| 589 | void *pOld, /* Prior allocation */ |
| 590 | sqlite3_uint64 newSize, /* Requested new alloation size */ |
| 591 | Parse *pParse /* Parsing context */ |
| 592 | ){ |
| 593 | void *p = sqlite3FaultSim(700) ? 0 : sqlite3_realloc(pOld, newSize); |
| 594 | if( p==0 ) sqlite3OomFault(pParse->db); |
| 595 | return p; |
| 596 | } |
| 597 | static void parserStackFree(void *pOld, Parse *pParse){ |
| 598 | (void)pParse; |
| 599 | sqlite3_free(pOld); |
| 600 | } |
| 601 | |
| 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]; |
| 605 | } |
| 606 | } |
| 607 | |
| 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);} |
| 612 | |
| 613 | %endif /* SQLITE_OMIT_CTE */ |
| 614 | select(A) ::= selectnowith(A). { |
| 615 | Select *p = A; |
| 616 | if( p ){ |
| 617 | parserDoubleLinkSelect(pParse, p); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | selectnowith(A) ::= oneselect(A). |
| 622 | %ifndef SQLITE_OMIT_COMPOUND_SELECT |
| 623 | selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). { |
| 624 | Select *pRhs = Z; |
| 625 | Select *pLhs = A; |
| 626 | if( pRhs && pRhs->pPrior ){ |
| 627 | SrcList *pFrom; |
| 628 | Token x; |
| 629 | x.n = 0; |
| 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); |
| 633 | } |
| 634 | if( pRhs ){ |
| 635 | pRhs->op = (u8)Y; |
| 636 | pRhs->pPrior = pLhs; |
| 637 | if( ALWAYS(pLhs) ) pLhs->selFlags &= ~(u32)SF_MultiValue; |
| 638 | pRhs->selFlags &= ~(u32)SF_MultiValue; |
| 639 | if( Y!=TK_ALL ) pParse->hasCompound = 1; |
| 640 | }else{ |
| 641 | sqlite3SelectDelete(pParse->db, pLhs); |
| 642 | } |
| 643 | A = pRhs; |
| 644 | } |
| 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 |
| 650 | |
| 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); |
| 655 | } |
| 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); |
| 661 | if( A ){ |
| 662 | A->pWinDefn = R; |
| 663 | }else{ |
| 664 | sqlite3WindowListDelete(pParse->db, R); |
| 665 | } |
| 666 | } |
| 667 | %endif |
| 668 | |
| 669 | |
| 670 | // Single row VALUES clause. |
| 671 | // |
| 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); |
| 677 | } |
| 678 | |
| 679 | // Multiple row VALUES clause. |
| 680 | // |
| 681 | %type mvalues {Select*} |
| 682 | oneselect(A) ::= mvalues(A). { |
| 683 | sqlite3MultiValuesEnd(pParse, A); |
| 684 | } |
| 685 | %destructor mvalues {sqlite3SelectDelete(pParse->db, $$);} |
| 686 | mvalues(A) ::= values(A) COMMA LP nexprlist(Y) RP. { |
| 687 | A = sqlite3MultiValues(pParse, A, Y); |
| 688 | } |
| 689 | mvalues(A) ::= mvalues(A) COMMA LP nexprlist(Y) RP. { |
| 690 | A = sqlite3MultiValues(pParse, A, Y); |
| 691 | } |
| 692 | |
| 693 | // The "distinct" nonterminal is true (1) if the DISTINCT keyword is |
| 694 | // present and false (0) if it is not. |
| 695 | // |
| 696 | %type distinct {int} |
| 697 | distinct(A) ::= DISTINCT. {A = SF_Distinct;} |
| 698 | distinct(A) ::= ALL. {A = SF_All;} |
| 699 | distinct(A) ::= . {A = 0;} |
| 700 | |
| 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. |
| 705 | // |
| 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); |
| 716 | } |
| 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); |
| 721 | } |
| 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); |
| 729 | } |
| 730 | |
| 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. |
| 733 | // |
| 734 | %type as {Token} |
| 735 | as(X) ::= AS nm(Y). {X = Y;} |
| 736 | as(X) ::= ids(X). |
| 737 | as(X) ::= . {X.n = 0; X.z = 0;} |
| 738 | |
| 739 | |
| 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, $$);} |
| 746 | |
| 747 | // A complete FROM clause. |
| 748 | // |
| 749 | from(A) ::= . {A = 0;} |
| 750 | from(A) ::= FROM seltablist(X). { |
| 751 | A = X; |
| 752 | sqlite3SrcListShiftJoinType(pParse,A); |
| 753 | } |
| 754 | |
| 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. |
| 757 | // |
| 758 | stl_prefix(A) ::= seltablist(A) joinop(Y). { |
| 759 | if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y; |
| 760 | } |
| 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); |
| 764 | } |
| 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); |
| 768 | } |
| 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); |
| 772 | } |
| 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); |
| 776 | } |
| 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 ){ |
| 779 | A = F; |
| 780 | }else if( ALWAYS(F!=0) && F->nSrc==1 ){ |
| 781 | A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N); |
| 782 | if( A ){ |
| 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 | pOld->zName = 0; |
| 788 | assert( pOld->fg.fixedSchema==0 ); |
| 789 | if( pOld->fg.isSubquery ){ |
| 790 | pNew->fg.isSubquery = 1; |
| 791 | pNew->u4.pSubq = pOld->u4.pSubq; |
| 792 | pOld->u4.pSubq = 0; |
| 793 | pOld->fg.isSubquery = 0; |
| 794 | assert( pNew->u4.pSubq!=0 && pNew->u4.pSubq->pSelect!=0 ); |
| 795 | if( (pNew->u4.pSubq->pSelect->selFlags & SF_NestedFrom)!=0 ){ |
| 796 | pNew->fg.isNestedFrom = 1; |
| 797 | } |
| 798 | }else{ |
| 799 | pNew->u4.zDatabase = pOld->u4.zDatabase; |
| 800 | pOld->u4.zDatabase = 0; |
| 801 | } |
| 802 | if( pOld->fg.isTabFunc ){ |
| 803 | pNew->u1.pFuncArg = pOld->u1.pFuncArg; |
| 804 | pOld->u1.pFuncArg = 0; |
| 805 | pOld->fg.isTabFunc = 0; |
| 806 | pNew->fg.isTabFunc = 1; |
| 807 | }else if( pOld->fg.isIndexedBy ){ |
| 808 | pNew->u1.zIndexedBy = pOld->u1.zIndexedBy; |
| 809 | pOld->u1.zIndexedBy = 0; |
| 810 | pOld->fg.isIndexedBy = 0; |
| 811 | pNew->fg.isIndexedBy = 1; |
| 812 | } |
| 813 | pNew->fg.notIndexed = pOld->fg.notIndexed; |
| 814 | } |
| 815 | sqlite3SrcListDelete(pParse->db, F); |
| 816 | }else{ |
| 817 | Select *pSubquery; |
| 818 | sqlite3SrcListShiftJoinType(pParse,F); |
| 819 | pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0); |
| 820 | A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N); |
| 821 | } |
| 822 | } |
| 823 | %endif SQLITE_OMIT_SUBQUERY |
| 824 | |
| 825 | %type dbnm {Token} |
| 826 | dbnm(A) ::= . {A.z=0; A.n=0;} |
| 827 | dbnm(A) ::= DOT nm(X). {A = X;} |
| 828 | |
| 829 | %type fullname {SrcList*} |
| 830 | %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);} |
| 831 | fullname(A) ::= nm(X). { |
| 832 | A = sqlite3SrcListAppend(pParse,0,&X,0); |
| 833 | if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X); |
| 834 | } |
| 835 | fullname(A) ::= nm(X) DOT nm(Y). { |
| 836 | A = sqlite3SrcListAppend(pParse,0,&X,&Y); |
| 837 | if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y); |
| 838 | } |
| 839 | |
| 840 | %type xfullname {SrcList*} |
| 841 | %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);} |
| 842 | xfullname(A) ::= nm(X). { |
| 843 | A = sqlite3SrcListAppend(pParse,0,&X,0); |
| 844 | if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X); |
| 845 | } |
| 846 | xfullname(A) ::= nm(X) DOT nm(Y). { |
| 847 | A = sqlite3SrcListAppend(pParse,0,&X,&Y); |
| 848 | if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y); |
| 849 | } |
| 850 | xfullname(A) ::= nm(X) AS nm(Z). { |
| 851 | A = sqlite3SrcListAppend(pParse,0,&X,0); |
| 852 | if( A ){ |
| 853 | if( IN_RENAME_OBJECT ){ |
| 854 | sqlite3RenameTokenMap(pParse, A->a[0].zName, &X); |
| 855 | } |
| 856 | A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z); |
| 857 | } |
| 858 | } |
| 859 | xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). { |
| 860 | A = sqlite3SrcListAppend(pParse,0,&X,&Y); |
| 861 | if( A ){ |
| 862 | if( IN_RENAME_OBJECT ){ |
| 863 | sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y); |
| 864 | } |
| 865 | A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | |
| 870 | %type joinop {int} |
| 871 | joinop(X) ::= COMMA|JOIN. { X = JT_INNER; } |
| 872 | joinop(X) ::= JOIN_KW(A) JOIN. |
| 873 | {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/} |
| 874 | joinop(X) ::= JOIN_KW(A) nm(B) JOIN. |
| 875 | {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/} |
| 876 | joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN. |
| 877 | {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/} |
| 878 | |
| 879 | // There is a parsing ambiguity in an upsert statement that uses a |
| 880 | // SELECT on the RHS of a the INSERT: |
| 881 | // |
| 882 | // INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ... |
| 883 | // here ----^^ |
| 884 | // |
| 885 | // When the ON token is encountered, the parser does not know if it is |
| 886 | // the beginning of an ON CONFLICT clause, or the beginning of an ON |
| 887 | // clause associated with the JOIN. The conflict is resolved in favor |
| 888 | // of the JOIN. If an ON CONFLICT clause is intended, insert a dummy |
| 889 | // WHERE clause in between, like this: |
| 890 | // |
| 891 | // INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ... |
| 892 | // |
| 893 | // The [AND] and [OR] precedence marks in the rules for on_using cause the |
| 894 | // ON in this context to always be interpreted as belonging to the JOIN. |
| 895 | // |
| 896 | %type on_using {OnOrUsing} |
| 897 | //%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);} |
| 898 | on_using(N) ::= ON expr(E). {N.pOn = E; N.pUsing = 0;} |
| 899 | on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;} |
| 900 | on_using(N) ::= . [OR] {N.pOn = 0; N.pUsing = 0;} |
| 901 | |
| 902 | // Note that this block abuses the Token type just a little. If there is |
| 903 | // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If |
| 904 | // there is an INDEXED BY clause, then the token is populated as per normal, |
| 905 | // with z pointing to the token data and n containing the number of bytes |
| 906 | // in the token. |
| 907 | // |
| 908 | // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is |
| 909 | // normally illegal. The sqlite3SrcListIndexedBy() function |
| 910 | // recognizes and interprets this as a special case. |
| 911 | // |
| 912 | %type indexed_opt {Token} |
| 913 | %type indexed_by {Token} |
| 914 | indexed_opt(A) ::= . {A.z=0; A.n=0;} |
| 915 | indexed_opt(A) ::= indexed_by(A). |
| 916 | indexed_by(A) ::= INDEXED BY nm(X). {A = X;} |
| 917 | indexed_by(A) ::= NOT INDEXED. {A.z=0; A.n=1;} |
| 918 | |
| 919 | %type orderby_opt {ExprList*} |
| 920 | %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);} |
| 921 | |
| 922 | // the sortlist non-terminal stores a list of expression where each |
| 923 | // expression is optionally followed by ASC or DESC to indicate the |
| 924 | // sort order. |
| 925 | // |
| 926 | %type sortlist {ExprList*} |
| 927 | %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);} |
| 928 | |
| 929 | orderby_opt(A) ::= . {A = 0;} |
| 930 | orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} |
| 931 | sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). { |
| 932 | A = sqlite3ExprListAppend(pParse,A,Y); |
| 933 | sqlite3ExprListSetSortOrder(A,Z,X); |
| 934 | } |
| 935 | sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). { |
| 936 | A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/ |
| 937 | sqlite3ExprListSetSortOrder(A,Z,X); |
| 938 | } |
| 939 | |
| 940 | %type sortorder {int} |
| 941 | |
| 942 | sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;} |
| 943 | sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;} |
| 944 | sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;} |
| 945 | |
| 946 | %type nulls {int} |
| 947 | nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;} |
| 948 | nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;} |
| 949 | nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;} |
| 950 | |
| 951 | %type groupby_opt {ExprList*} |
| 952 | %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);} |
| 953 | groupby_opt(A) ::= . {A = 0;} |
| 954 | groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;} |
| 955 | |
| 956 | %type having_opt {Expr*} |
| 957 | %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);} |
| 958 | having_opt(A) ::= . {A = 0;} |
| 959 | having_opt(A) ::= HAVING expr(X). {A = X;} |
| 960 | |
| 961 | %type limit_opt {Expr*} |
| 962 | |
| 963 | // The destructor for limit_opt will never fire in the current grammar. |
| 964 | // The limit_opt non-terminal only occurs at the end of a single production |
| 965 | // rule for SELECT statements. As soon as the rule that create the |
| 966 | // limit_opt non-terminal reduces, the SELECT statement rule will also |
| 967 | // reduce. So there is never a limit_opt non-terminal on the stack |
| 968 | // except as a transient. So there is never anything to destroy. |
| 969 | // |
| 970 | //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);} |
| 971 | limit_opt(A) ::= . {A = 0;} |
| 972 | limit_opt(A) ::= LIMIT expr(X). |
| 973 | {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);} |
| 974 | limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). |
| 975 | {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);} |
| 976 | limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). |
| 977 | {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);} |
| 978 | |
| 979 | /////////////////////////// The DELETE statement ///////////////////////////// |
| 980 | // |
| 981 | %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER |
| 982 | cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W) |
| 983 | orderby_opt(O) limit_opt(L). { |
| 984 | sqlite3SrcListIndexedBy(pParse, X, &I); |
| 985 | #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT |
| 986 | if( O || L ){ |
| 987 | updateDeleteLimitError(pParse,O,L); |
| 988 | O = 0; |
| 989 | L = 0; |
| 990 | } |
| 991 | #endif |
| 992 | sqlite3DeleteFrom(pParse,X,W,O,L); |
| 993 | } |
| 994 | %else |
| 995 | cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). { |
| 996 | sqlite3SrcListIndexedBy(pParse, X, &I); |
| 997 | sqlite3DeleteFrom(pParse,X,W,0,0); |
| 998 | } |
| 999 | %endif |
| 1000 | |
| 1001 | %type where_opt {Expr*} |
| 1002 | %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);} |
| 1003 | %type where_opt_ret {Expr*} |
| 1004 | %destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);} |
| 1005 | |
| 1006 | where_opt(A) ::= . {A = 0;} |
| 1007 | where_opt(A) ::= WHERE expr(X). {A = X;} |
| 1008 | where_opt_ret(A) ::= . {A = 0;} |
| 1009 | where_opt_ret(A) ::= WHERE expr(X). {A = X;} |
| 1010 | where_opt_ret(A) ::= RETURNING selcollist(X). |
| 1011 | {sqlite3AddReturning(pParse,X); A = 0;} |
| 1012 | where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y). |
| 1013 | {sqlite3AddReturning(pParse,Y); A = X;} |
| 1014 | |
| 1015 | ////////////////////////// The UPDATE command //////////////////////////////// |
| 1016 | // |
| 1017 | %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER |
| 1018 | cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F) |
| 1019 | where_opt_ret(W) orderby_opt(O) limit_opt(L). { |
| 1020 | sqlite3SrcListIndexedBy(pParse, X, &I); |
| 1021 | if( F ){ |
| 1022 | SrcList *pFromClause = F; |
| 1023 | if( pFromClause->nSrc>1 ){ |
| 1024 | Select *pSubquery; |
| 1025 | Token as; |
| 1026 | pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0); |
| 1027 | as.n = 0; |
| 1028 | as.z = 0; |
| 1029 | pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0); |
| 1030 | } |
| 1031 | X = sqlite3SrcListAppendList(pParse, X, pFromClause); |
| 1032 | } |
| 1033 | sqlite3ExprListCheckLength(pParse,Y,"set list"); |
| 1034 | #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT |
| 1035 | if( O || L ){ |
| 1036 | updateDeleteLimitError(pParse,O,L); |
| 1037 | O = 0; |
| 1038 | L = 0; |
| 1039 | } |
| 1040 | #endif |
| 1041 | sqlite3Update(pParse,X,Y,W,R,O,L,0); |
| 1042 | } |
| 1043 | %else |
| 1044 | cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F) |
| 1045 | where_opt_ret(W). { |
| 1046 | sqlite3SrcListIndexedBy(pParse, X, &I); |
| 1047 | sqlite3ExprListCheckLength(pParse,Y,"set list"); |
| 1048 | if( F ){ |
| 1049 | SrcList *pFromClause = F; |
| 1050 | if( pFromClause->nSrc>1 ){ |
| 1051 | Select *pSubquery; |
| 1052 | Token as; |
| 1053 | pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0); |
| 1054 | as.n = 0; |
| 1055 | as.z = 0; |
| 1056 | pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0); |
| 1057 | } |
| 1058 | X = sqlite3SrcListAppendList(pParse, X, pFromClause); |
| 1059 | } |
| 1060 | sqlite3Update(pParse,X,Y,W,R,0,0,0); |
| 1061 | } |
| 1062 | %endif |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | %type setlist {ExprList*} |
| 1067 | %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);} |
| 1068 | |
| 1069 | setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). { |
| 1070 | A = sqlite3ExprListAppend(pParse, A, Y); |
| 1071 | sqlite3ExprListSetName(pParse, A, &X, 1); |
| 1072 | } |
| 1073 | setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). { |
| 1074 | A = sqlite3ExprListAppendVector(pParse, A, X, Y); |
| 1075 | } |
| 1076 | setlist(A) ::= nm(X) EQ expr(Y). { |
| 1077 | A = sqlite3ExprListAppend(pParse, 0, Y); |
| 1078 | sqlite3ExprListSetName(pParse, A, &X, 1); |
| 1079 | } |
| 1080 | setlist(A) ::= LP idlist(X) RP EQ expr(Y). { |
| 1081 | A = sqlite3ExprListAppendVector(pParse, 0, X, Y); |
| 1082 | } |
| 1083 | |
| 1084 | ////////////////////////// The INSERT command ///////////////////////////////// |
| 1085 | // |
| 1086 | cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S) |
| 1087 | upsert(U). { |
| 1088 | sqlite3Insert(pParse, X, S, F, R, U); |
| 1089 | } |
| 1090 | cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning. |
| 1091 | { |
| 1092 | sqlite3Insert(pParse, X, 0, F, R, 0); |
| 1093 | } |
| 1094 | |
| 1095 | %type upsert {Upsert*} |
| 1096 | |
| 1097 | // Because upsert only occurs at the tip end of the INSERT rule for cmd, |
| 1098 | // there is never a case where the value of the upsert pointer will not |
| 1099 | // be destroyed by the cmd action. So comment-out the destructor to |
| 1100 | // avoid unreachable code. |
| 1101 | //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);} |
| 1102 | upsert(A) ::= . { A = 0; } |
| 1103 | upsert(A) ::= RETURNING selcollist(X). { A = 0; sqlite3AddReturning(pParse,X); } |
| 1104 | upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) |
| 1105 | DO UPDATE SET setlist(Z) where_opt(W) upsert(N). |
| 1106 | { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);} |
| 1107 | upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N). |
| 1108 | { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); } |
| 1109 | upsert(A) ::= ON CONFLICT DO NOTHING returning. |
| 1110 | { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); } |
| 1111 | upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning. |
| 1112 | { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);} |
| 1113 | |
| 1114 | returning ::= RETURNING selcollist(X). {sqlite3AddReturning(pParse,X);} |
| 1115 | returning ::= . |
| 1116 | |
| 1117 | %type insert_cmd {int} |
| 1118 | insert_cmd(A) ::= INSERT orconf(R). {A = R;} |
| 1119 | insert_cmd(A) ::= REPLACE. {A = OE_Replace;} |
| 1120 | |
| 1121 | %type idlist_opt {IdList*} |
| 1122 | %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);} |
| 1123 | %type idlist {IdList*} |
| 1124 | %destructor idlist {sqlite3IdListDelete(pParse->db, $$);} |
| 1125 | |
| 1126 | idlist_opt(A) ::= . {A = 0;} |
| 1127 | idlist_opt(A) ::= LP idlist(X) RP. {A = X;} |
| 1128 | idlist(A) ::= idlist(A) COMMA nm(Y). |
| 1129 | {A = sqlite3IdListAppend(pParse,A,&Y);} |
| 1130 | idlist(A) ::= nm(Y). |
| 1131 | {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/} |
| 1132 | |
| 1133 | /////////////////////////// Expression Processing ///////////////////////////// |
| 1134 | // |
| 1135 | |
| 1136 | %type expr {Expr*} |
| 1137 | %destructor expr {sqlite3ExprDelete(pParse->db, $$);} |
| 1138 | %type term {Expr*} |
| 1139 | %destructor term {sqlite3ExprDelete(pParse->db, $$);} |
| 1140 | |
| 1141 | %include { |
| 1142 | |
| 1143 | /* Construct a new Expr object from a single token */ |
| 1144 | static Expr *tokenExpr(Parse *pParse, int op, Token t){ |
| 1145 | Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1); |
| 1146 | if( p ){ |
| 1147 | /* memset(p, 0, sizeof(Expr)); */ |
| 1148 | p->op = (u8)op; |
| 1149 | p->affExpr = 0; |
| 1150 | p->flags = EP_Leaf; |
| 1151 | ExprClearVVAProperties(p); |
| 1152 | /* p->iAgg = -1; // Not required */ |
| 1153 | p->pLeft = p->pRight = 0; |
| 1154 | p->pAggInfo = 0; |
| 1155 | memset(&p->x, 0, sizeof(p->x)); |
| 1156 | memset(&p->y, 0, sizeof(p->y)); |
| 1157 | p->op2 = 0; |
| 1158 | p->iTable = 0; |
| 1159 | p->iColumn = 0; |
| 1160 | p->u.zToken = (char*)&p[1]; |
| 1161 | memcpy(p->u.zToken, t.z, t.n); |
| 1162 | p->u.zToken[t.n] = 0; |
| 1163 | p->w.iOfst = (int)(t.z - pParse->zTail); |
| 1164 | if( sqlite3Isquote(p->u.zToken[0]) ){ |
| 1165 | sqlite3DequoteExpr(p); |
| 1166 | } |
| 1167 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 1168 | p->nHeight = 1; |
| 1169 | #endif |
| 1170 | if( IN_RENAME_OBJECT ){ |
| 1171 | return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t); |
| 1172 | } |
| 1173 | } |
| 1174 | return p; |
| 1175 | } |
| 1176 | |
| 1177 | } |
| 1178 | |
| 1179 | expr(A) ::= term(A). |
| 1180 | expr(A) ::= LP expr(X) RP. {A = X;} |
| 1181 | expr(A) ::= idj(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/} |
| 1182 | expr(A) ::= nm(X) DOT nm(Y). { |
| 1183 | Expr *temp1 = tokenExpr(pParse,TK_ID,X); |
| 1184 | Expr *temp2 = tokenExpr(pParse,TK_ID,Y); |
| 1185 | A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2); |
| 1186 | } |
| 1187 | expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { |
| 1188 | Expr *temp1 = tokenExpr(pParse,TK_ID,X); |
| 1189 | Expr *temp2 = tokenExpr(pParse,TK_ID,Y); |
| 1190 | Expr *temp3 = tokenExpr(pParse,TK_ID,Z); |
| 1191 | Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3); |
| 1192 | if( IN_RENAME_OBJECT ){ |
| 1193 | sqlite3RenameTokenRemap(pParse, 0, temp1); |
| 1194 | } |
| 1195 | A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4); |
| 1196 | } |
| 1197 | term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/} |
| 1198 | term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/} |
| 1199 | term(A) ::= INTEGER(X). { |
| 1200 | int iValue; |
| 1201 | if( sqlite3GetInt32(X.z, &iValue)==0 ){ |
| 1202 | A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 0); |
| 1203 | }else{ |
| 1204 | A = sqlite3ExprInt32(pParse->db, iValue); |
| 1205 | } |
| 1206 | if( A ) A->w.iOfst = (int)(X.z - pParse->zTail); |
| 1207 | } |
| 1208 | expr(A) ::= VARIABLE(X). { |
| 1209 | if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){ |
| 1210 | u32 n = X.n; |
| 1211 | A = tokenExpr(pParse, TK_VARIABLE, X); |
| 1212 | sqlite3ExprAssignVarNumber(pParse, A, n); |
| 1213 | }else{ |
| 1214 | /* When doing a nested parse, one can include terms in an expression |
| 1215 | ** that look like this: #1 #2 ... These terms refer to registers |
| 1216 | ** in the virtual machine. #N is the N-th register. */ |
| 1217 | Token t = X; /*A-overwrites-X*/ |
| 1218 | assert( t.n>=2 ); |
| 1219 | if( pParse->nested==0 ){ |
| 1220 | parserSyntaxError(pParse, &t); |
| 1221 | A = 0; |
| 1222 | }else{ |
| 1223 | A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0); |
| 1224 | if( A ) sqlite3GetInt32(&t.z[1], &A->iTable); |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | expr(A) ::= expr(A) COLLATE ids(C). { |
| 1229 | A = sqlite3ExprAddCollateToken(pParse, A, &C, 1); |
| 1230 | } |
| 1231 | %ifndef SQLITE_OMIT_CAST |
| 1232 | expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. { |
| 1233 | A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1); |
| 1234 | sqlite3ExprAttachSubtrees(pParse->db, A, E, 0); |
| 1235 | } |
| 1236 | %endif SQLITE_OMIT_CAST |
| 1237 | |
| 1238 | |
| 1239 | expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP. { |
| 1240 | A = sqlite3ExprFunction(pParse, Y, &X, D); |
| 1241 | } |
| 1242 | expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP. { |
| 1243 | A = sqlite3ExprFunction(pParse, Y, &X, D); |
| 1244 | sqlite3ExprAddFunctionOrderBy(pParse, A, O); |
| 1245 | } |
| 1246 | expr(A) ::= idj(X) LP STAR RP. { |
| 1247 | A = sqlite3ExprFunction(pParse, 0, &X, 0); |
| 1248 | } |
| 1249 | |
| 1250 | %ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES |
| 1251 | %include { |
| 1252 | /* Generate an expression node that represents an ordered-set aggregate function. |
| 1253 | ** |
| 1254 | ** SQLite does not do anything special to evaluate ordered-set aggregates. The |
| 1255 | ** aggregate function itself is expected to do any required ordering on its own. |
| 1256 | ** This is just syntactic sugar. |
| 1257 | ** |
| 1258 | ** This syntax: percentile(f) WITHIN GROUP ( ORDER BY y ) |
| 1259 | ** |
| 1260 | ** Is equivalent to: percentile(y,f) |
| 1261 | ** |
| 1262 | ** The purpose of this function is to generate an Expr node from the first syntax |
| 1263 | ** into a TK_FUNCTION node that looks like it came from the second syntax. |
| 1264 | ** |
| 1265 | ** Only functions that have the SQLITE_SELFORDER1 property are allowed to do this |
| 1266 | ** transformation. Because DISTINCT is not allowed in the ordered-set aggregate |
| 1267 | ** syntax, an error is raised if DISTINCT is used. |
| 1268 | */ |
| 1269 | static Expr *sqlite3ExprAddOrderedsetFunction( |
| 1270 | Parse *pParse, /* Parsing context */ |
| 1271 | Token *pFuncname, /* Name of the function */ |
| 1272 | int isDistinct, /* DISTINCT or ALL qualifier */ |
| 1273 | ExprList *pOrig, /* Arguments to the function */ |
| 1274 | Expr *pOrderby /* Expression in the ORDER BY clause */ |
| 1275 | ){ |
| 1276 | ExprList *p; /* Modified argument list */ |
| 1277 | Expr *pExpr; /* Final result */ |
| 1278 | p = sqlite3ExprListAppend(pParse, 0, pOrderby); |
| 1279 | if( pOrig ){ |
| 1280 | int i; |
| 1281 | for(i=0; i<pOrig->nExpr; i++){ |
| 1282 | p = sqlite3ExprListAppend(pParse, p, pOrig->a[i].pExpr); |
| 1283 | pOrig->a[i].pExpr = 0; |
| 1284 | } |
| 1285 | sqlite3ExprListDelete(pParse->db, pOrig); |
| 1286 | } |
| 1287 | pExpr = sqlite3ExprFunction(pParse, p, pFuncname, 0); |
| 1288 | if( pParse->nErr==0 ){ |
| 1289 | FuncDef *pDef; |
| 1290 | u8 enc = ENC(pParse->db); |
| 1291 | assert( pExpr!=0 ); /* Because otherwise pParse->nErr would not be zero */ |
| 1292 | assert( p!=0 ); /* Because otherwise pParse->nErr would not be zero */ |
| 1293 | pDef = sqlite3FindFunction(pParse->db, pExpr->u.zToken, -2, enc, 0); |
| 1294 | if( pDef==0 || (pDef->funcFlags & SQLITE_SELFORDER1)==0 ){ |
| 1295 | sqlite3ErrorMsg(pParse, "%#T() is not an ordered-set aggregate", pExpr); |
| 1296 | }else if( isDistinct==SF_Distinct ){ |
| 1297 | sqlite3ErrorMsg(pParse, "DISTINCT not allowed on ordered-set aggregate %T()", |
| 1298 | pFuncname); |
| 1299 | } |
| 1300 | } |
| 1301 | return pExpr; |
| 1302 | } |
| 1303 | } |
| 1304 | expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP WITHIN GROUP LP ORDER BY expr(E) RP. { |
| 1305 | A = sqlite3ExprAddOrderedsetFunction(pParse, &X, D, Y, E); |
| 1306 | } |
| 1307 | %endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES |
| 1308 | |
| 1309 | %ifndef SQLITE_OMIT_WINDOWFUNC |
| 1310 | expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP filter_over(Z). { |
| 1311 | A = sqlite3ExprFunction(pParse, Y, &X, D); |
| 1312 | sqlite3WindowAttach(pParse, A, Z); |
| 1313 | } |
| 1314 | expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP filter_over(Z). { |
| 1315 | A = sqlite3ExprFunction(pParse, Y, &X, D); |
| 1316 | sqlite3WindowAttach(pParse, A, Z); |
| 1317 | sqlite3ExprAddFunctionOrderBy(pParse, A, O); |
| 1318 | } |
| 1319 | expr(A) ::= idj(X) LP STAR RP filter_over(Z). { |
| 1320 | A = sqlite3ExprFunction(pParse, 0, &X, 0); |
| 1321 | sqlite3WindowAttach(pParse, A, Z); |
| 1322 | } |
| 1323 | %ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES |
| 1324 | expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP WITHIN GROUP LP ORDER BY expr(E) RP |
| 1325 | filter_over(Z). { |
| 1326 | A = sqlite3ExprAddOrderedsetFunction(pParse, &X, D, Y, E); |
| 1327 | sqlite3WindowAttach(pParse, A, Z); |
| 1328 | } |
| 1329 | %endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES |
| 1330 | |
| 1331 | %endif SQLITE_OMIT_WINDOWFUNC |
| 1332 | |
| 1333 | term(A) ::= CTIME_KW(OP). { |
| 1334 | A = sqlite3ExprFunction(pParse, 0, &OP, 0); |
| 1335 | } |
| 1336 | |
| 1337 | expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. { |
| 1338 | ExprList *pList = sqlite3ExprListAppend(pParse, X, Y); |
| 1339 | A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0); |
| 1340 | if( A ){ |
| 1341 | int i; |
| 1342 | A->x.pList = pList; |
| 1343 | for(i=0; i<pList->nExpr; i++){ |
| 1344 | assert( pList->a[i].pExpr!=0 ); |
| 1345 | A->flags |= pList->a[i].pExpr->flags & EP_Propagate; |
| 1346 | } |
| 1347 | }else{ |
| 1348 | sqlite3ExprListDelete(pParse->db, pList); |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);} |
| 1353 | expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} |
| 1354 | expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y). |
| 1355 | {A=sqlite3PExpr(pParse,@OP,A,Y);} |
| 1356 | expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} |
| 1357 | expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y). |
| 1358 | {A=sqlite3PExpr(pParse,@OP,A,Y);} |
| 1359 | expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y). |
| 1360 | {A=sqlite3PExpr(pParse,@OP,A,Y);} |
| 1361 | expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y). |
| 1362 | {A=sqlite3PExpr(pParse,@OP,A,Y);} |
| 1363 | expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} |
| 1364 | %type likeop {Token} |
| 1365 | likeop(A) ::= LIKE_KW|MATCH(A). |
| 1366 | likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/} |
| 1367 | expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] { |
| 1368 | ExprList *pList; |
| 1369 | int bNot = OP.n & 0x80000000; |
| 1370 | OP.n &= 0x7fffffff; |
| 1371 | pList = sqlite3ExprListAppend(pParse,0, Y); |
| 1372 | pList = sqlite3ExprListAppend(pParse,pList, A); |
| 1373 | A = sqlite3ExprFunction(pParse, pList, &OP, 0); |
| 1374 | if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
| 1375 | if( A ) A->flags |= EP_InfixFunc; |
| 1376 | } |
| 1377 | expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] { |
| 1378 | ExprList *pList; |
| 1379 | int bNot = OP.n & 0x80000000; |
| 1380 | OP.n &= 0x7fffffff; |
| 1381 | pList = sqlite3ExprListAppend(pParse,0, Y); |
| 1382 | pList = sqlite3ExprListAppend(pParse,pList, A); |
| 1383 | pList = sqlite3ExprListAppend(pParse,pList, E); |
| 1384 | A = sqlite3ExprFunction(pParse, pList, &OP, 0); |
| 1385 | if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
| 1386 | if( A ) A->flags |= EP_InfixFunc; |
| 1387 | } |
| 1388 | |
| 1389 | %include { |
| 1390 | /* Create a TK_ISNULL or TK_NOTNULL expression, perhaps optimized to |
| 1391 | ** to TK_TRUEFALSE, if possible */ |
| 1392 | static Expr *sqlite3PExprIsNull( |
| 1393 | Parse *pParse, /* Parsing context */ |
| 1394 | int op, /* TK_ISNULL or TK_NOTNULL */ |
| 1395 | Expr *pLeft /* Operand */ |
| 1396 | ){ |
| 1397 | Expr *p = pLeft; |
| 1398 | assert( op==TK_ISNULL || op==TK_NOTNULL ); |
| 1399 | assert( pLeft!=0 ); |
| 1400 | while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ |
| 1401 | p = p->pLeft; |
| 1402 | assert( p!=0 ); |
| 1403 | } |
| 1404 | switch( p->op ){ |
| 1405 | case TK_INTEGER: |
| 1406 | case TK_STRING: |
| 1407 | case TK_FLOAT: |
| 1408 | case TK_BLOB: |
| 1409 | sqlite3ExprDeferredDelete(pParse, pLeft); |
| 1410 | p = sqlite3ExprInt32(pParse->db, op==TK_NOTNULL); |
| 1411 | if( p ) p->op2 = 1; |
| 1412 | return p; |
| 1413 | default: |
| 1414 | break; |
| 1415 | } |
| 1416 | return sqlite3PExpr(pParse, op, pLeft, 0); |
| 1417 | } |
| 1418 | |
| 1419 | /* Create a TK_IS or TK_ISNOT operator, perhaps optimized to |
| 1420 | ** TK_ISNULL or TK_NOTNULL or TK_TRUEFALSE. */ |
| 1421 | static Expr *sqlite3PExprIs( |
| 1422 | Parse *pParse, /* Parsing context */ |
| 1423 | int op, /* TK_IS or TK_ISNOT */ |
| 1424 | Expr *pLeft, /* Left operand */ |
| 1425 | Expr *pRight /* Right operand */ |
| 1426 | ){ |
| 1427 | if( pRight && pRight->op==TK_NULL ){ |
| 1428 | sqlite3ExprDeferredDelete(pParse, pRight); |
| 1429 | return sqlite3PExprIsNull(pParse, op==TK_IS ? TK_ISNULL : TK_NOTNULL, pLeft); |
| 1430 | } |
| 1431 | return sqlite3PExpr(pParse, op, pLeft, pRight); |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExprIsNull(pParse,@E,A);} |
| 1436 | expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExprIsNull(pParse,TK_NOTNULL,A);} |
| 1437 | |
| 1438 | // expr1 IS expr2 same as expr1 IS NOT DISTINCT FROM expr2 |
| 1439 | // expr1 IS NOT expr2 same as expr1 IS DISTINCT FROM expr2 |
| 1440 | // |
| 1441 | expr(A) ::= expr(A) IS expr(Y). { |
| 1442 | A = sqlite3PExprIs(pParse, TK_IS, A, Y); |
| 1443 | } |
| 1444 | expr(A) ::= expr(A) IS NOT expr(Y). { |
| 1445 | A = sqlite3PExprIs(pParse, TK_ISNOT, A, Y); |
| 1446 | } |
| 1447 | expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y). { |
| 1448 | A = sqlite3PExprIs(pParse, TK_IS, A, Y); |
| 1449 | } |
| 1450 | expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). { |
| 1451 | A = sqlite3PExprIs(pParse, TK_ISNOT, A, Y); |
| 1452 | } |
| 1453 | |
| 1454 | expr(A) ::= NOT(B) expr(X). |
| 1455 | {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/} |
| 1456 | expr(A) ::= BITNOT(B) expr(X). |
| 1457 | {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/} |
| 1458 | expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] { |
| 1459 | Expr *p = X; |
| 1460 | u8 op = @B + (TK_UPLUS-TK_PLUS); |
| 1461 | assert( TK_UPLUS>TK_PLUS ); |
| 1462 | assert( TK_UMINUS == TK_MINUS + (TK_UPLUS - TK_PLUS) ); |
| 1463 | if( p && p->op==TK_UPLUS ){ |
| 1464 | p->op = op; |
| 1465 | A = p; |
| 1466 | }else{ |
| 1467 | A = sqlite3PExpr(pParse, op, p, 0); |
| 1468 | /*A-overwrites-B*/ |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | expr(A) ::= expr(B) PTR(C) expr(D). { |
| 1473 | ExprList *pList = sqlite3ExprListAppend(pParse, 0, B); |
| 1474 | pList = sqlite3ExprListAppend(pParse, pList, D); |
| 1475 | A = sqlite3ExprFunction(pParse, pList, &C, 0); |
| 1476 | } |
| 1477 | |
| 1478 | %type between_op {int} |
| 1479 | between_op(A) ::= BETWEEN. {A = 0;} |
| 1480 | between_op(A) ::= NOT BETWEEN. {A = 1;} |
| 1481 | expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] { |
| 1482 | ExprList *pList = sqlite3ExprListAppend(pParse,0, X); |
| 1483 | pList = sqlite3ExprListAppend(pParse,pList, Y); |
| 1484 | A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0); |
| 1485 | if( A ){ |
| 1486 | A->x.pList = pList; |
| 1487 | sqlite3ExprSetHeightAndFlags(pParse, A); |
| 1488 | }else{ |
| 1489 | sqlite3ExprListDelete(pParse->db, pList); |
| 1490 | } |
| 1491 | if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
| 1492 | } |
| 1493 | %ifndef SQLITE_OMIT_SUBQUERY |
| 1494 | %type in_op {int} |
| 1495 | in_op(A) ::= IN. {A = 0;} |
| 1496 | in_op(A) ::= NOT IN. {A = 1;} |
| 1497 | expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] { |
| 1498 | if( Y==0 ){ |
| 1499 | /* Expressions of the form |
| 1500 | ** |
| 1501 | ** expr1 IN () |
| 1502 | ** expr1 NOT IN () |
| 1503 | ** |
| 1504 | ** simplify to constants 0 (false) and 1 (true), respectively. |
| 1505 | ** |
| 1506 | ** Except, do not apply this optimization if expr1 contains a function |
| 1507 | ** because that function might be an aggregate (we don't know yet whether |
| 1508 | ** it is or not) and if it is an aggregate, that could change the meaning |
| 1509 | ** of the whole query. |
| 1510 | */ |
| 1511 | Expr *pB = sqlite3ExprInt32(pParse->db, N!=0); |
| 1512 | if( !ExprHasProperty(A, EP_HasFunc) ){ |
| 1513 | if( pB ) pB->op2 = 1; |
| 1514 | sqlite3ExprUnmapAndDelete(pParse, A); |
| 1515 | A = pB; |
| 1516 | }else{ |
| 1517 | A = sqlite3PExpr(pParse, N ? TK_OR : TK_AND, pB, A); |
| 1518 | } |
| 1519 | }else{ |
| 1520 | Expr *pRHS = Y->a[0].pExpr; |
| 1521 | if( Y->nExpr==1 |
| 1522 | && sqlite3ExprIsConstant(pParse,pRHS) |
| 1523 | && ExprHasProperty(pRHS, EP_Collate)==0 |
| 1524 | && A->op!=TK_VECTOR |
| 1525 | ){ |
| 1526 | Y->a[0].pExpr = 0; |
| 1527 | sqlite3ExprListDelete(pParse->db, Y); |
| 1528 | pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0); |
| 1529 | A = sqlite3PExpr(pParse, TK_EQ, A, pRHS); |
| 1530 | }else if( Y->nExpr==1 && pRHS->op==TK_SELECT ){ |
| 1531 | A = sqlite3PExpr(pParse, TK_IN, A, 0); |
| 1532 | sqlite3PExprAddSelect(pParse, A, pRHS->x.pSelect); |
| 1533 | pRHS->x.pSelect = 0; |
| 1534 | sqlite3ExprListDelete(pParse->db, Y); |
| 1535 | }else{ |
| 1536 | A = sqlite3PExpr(pParse, TK_IN, A, 0); |
| 1537 | if( A==0 ){ |
| 1538 | sqlite3ExprListDelete(pParse->db, Y); |
| 1539 | }else if( A->pLeft->op==TK_VECTOR ){ |
| 1540 | int nExpr = A->pLeft->x.pList->nExpr; |
| 1541 | Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y); |
| 1542 | if( pSelectRHS ){ |
| 1543 | parserDoubleLinkSelect(pParse, pSelectRHS); |
| 1544 | sqlite3PExprAddSelect(pParse, A, pSelectRHS); |
| 1545 | } |
| 1546 | }else{ |
| 1547 | A->x.pList = Y; |
| 1548 | sqlite3ExprSetHeightAndFlags(pParse, A); |
| 1549 | } |
| 1550 | } |
| 1551 | if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
| 1552 | } |
| 1553 | } |
| 1554 | expr(A) ::= LP select(X) RP. { |
| 1555 | A = sqlite3PExpr(pParse, TK_SELECT, 0, 0); |
| 1556 | sqlite3PExprAddSelect(pParse, A, X); |
| 1557 | } |
| 1558 | expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] { |
| 1559 | A = sqlite3PExpr(pParse, TK_IN, A, 0); |
| 1560 | sqlite3PExprAddSelect(pParse, A, Y); |
| 1561 | if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
| 1562 | } |
| 1563 | expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] { |
| 1564 | SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z); |
| 1565 | Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0); |
| 1566 | if( pSelect && IN_RENAME_OBJECT ){ |
| 1567 | sqlite3RenameTokenMap(pParse, pSrc->a[0].zName, Z.z ? &Z : &Y); |
| 1568 | } |
| 1569 | if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E); |
| 1570 | A = sqlite3PExpr(pParse, TK_IN, A, 0); |
| 1571 | sqlite3PExprAddSelect(pParse, A, pSelect); |
| 1572 | if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); |
| 1573 | } |
| 1574 | expr(A) ::= EXISTS LP select(Y) RP. { |
| 1575 | Expr *p; |
| 1576 | p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0); |
| 1577 | sqlite3PExprAddSelect(pParse, p, Y); |
| 1578 | } |
| 1579 | %endif SQLITE_OMIT_SUBQUERY |
| 1580 | |
| 1581 | /* CASE expressions */ |
| 1582 | expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. { |
| 1583 | A = sqlite3PExpr(pParse, TK_CASE, X, 0); |
| 1584 | if( A ){ |
| 1585 | A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y; |
| 1586 | sqlite3ExprSetHeightAndFlags(pParse, A); |
| 1587 | }else{ |
| 1588 | sqlite3ExprListDelete(pParse->db, Y); |
| 1589 | sqlite3ExprDelete(pParse->db, Z); |
| 1590 | } |
| 1591 | } |
| 1592 | %type case_exprlist {ExprList*} |
| 1593 | %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);} |
| 1594 | case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). { |
| 1595 | A = sqlite3ExprListAppend(pParse,A, Y); |
| 1596 | A = sqlite3ExprListAppend(pParse,A, Z); |
| 1597 | } |
| 1598 | case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { |
| 1599 | A = sqlite3ExprListAppend(pParse,0, Y); |
| 1600 | A = sqlite3ExprListAppend(pParse,A, Z); |
| 1601 | } |
| 1602 | %type case_else {Expr*} |
| 1603 | %destructor case_else {sqlite3ExprDelete(pParse->db, $$);} |
| 1604 | case_else(A) ::= ELSE expr(X). {A = X;} |
| 1605 | case_else(A) ::= . {A = 0;} |
| 1606 | %type case_operand {Expr*} |
| 1607 | %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);} |
| 1608 | case_operand(A) ::= expr(A). |
| 1609 | case_operand(A) ::= . {A = 0;} |
| 1610 | |
| 1611 | %type exprlist {ExprList*} |
| 1612 | %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);} |
| 1613 | %type nexprlist {ExprList*} |
| 1614 | %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);} |
| 1615 | |
| 1616 | exprlist(A) ::= nexprlist(A). |
| 1617 | exprlist(A) ::= . {A = 0;} |
| 1618 | nexprlist(A) ::= nexprlist(A) COMMA expr(Y). |
| 1619 | {A = sqlite3ExprListAppend(pParse,A,Y);} |
| 1620 | nexprlist(A) ::= expr(Y). |
| 1621 | {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/} |
| 1622 | |
| 1623 | %ifndef SQLITE_OMIT_SUBQUERY |
| 1624 | /* A paren_exprlist is an optional expression list contained inside |
| 1625 | ** of parenthesis */ |
| 1626 | %type paren_exprlist {ExprList*} |
| 1627 | %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);} |
| 1628 | paren_exprlist(A) ::= . {A = 0;} |
| 1629 | paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;} |
| 1630 | %endif SQLITE_OMIT_SUBQUERY |
| 1631 | |
| 1632 | |
| 1633 | ///////////////////////////// The CREATE INDEX command /////////////////////// |
| 1634 | // |
| 1635 | cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D) |
| 1636 | ON nm(Y) LP sortlist(Z) RP where_opt(W). { |
| 1637 | sqlite3CreateIndex(pParse, &X, &D, |
| 1638 | sqlite3SrcListAppend(pParse,0,&Y,0), Z, U, |
| 1639 | &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF); |
| 1640 | if( IN_RENAME_OBJECT && pParse->pNewIndex ){ |
| 1641 | sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | %type uniqueflag {int} |
| 1646 | uniqueflag(A) ::= UNIQUE. {A = OE_Abort;} |
| 1647 | uniqueflag(A) ::= . {A = OE_None;} |
| 1648 | |
| 1649 | |
| 1650 | // The eidlist non-terminal (Expression Id List) generates an ExprList |
| 1651 | // from a list of identifiers. The identifier names are in ExprList.a[].zName. |
| 1652 | // This list is stored in an ExprList rather than an IdList so that it |
| 1653 | // can be easily sent to sqlite3ColumnsExprList(). |
| 1654 | // |
| 1655 | // eidlist is grouped with CREATE INDEX because it used to be the non-terminal |
| 1656 | // used for the arguments to an index. That is just an historical accident. |
| 1657 | // |
| 1658 | // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted |
| 1659 | // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate |
| 1660 | // places - places that might have been stored in the sqlite_schema table. |
| 1661 | // Those extra features were ignored. But because they might be in some |
| 1662 | // (busted) old databases, we need to continue parsing them when loading |
| 1663 | // historical schemas. |
| 1664 | // |
| 1665 | %type eidlist {ExprList*} |
| 1666 | %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);} |
| 1667 | %type eidlist_opt {ExprList*} |
| 1668 | %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);} |
| 1669 | |
| 1670 | %include { |
| 1671 | /* Add a single new term to an ExprList that is used to store a |
| 1672 | ** list of identifiers. Report an error if the ID list contains |
| 1673 | ** a COLLATE clause or an ASC or DESC keyword, except ignore the |
| 1674 | ** error while parsing a legacy schema. |
| 1675 | */ |
| 1676 | static ExprList *parserAddExprIdListTerm( |
| 1677 | Parse *pParse, |
| 1678 | ExprList *pPrior, |
| 1679 | Token *pIdToken, |
| 1680 | int hasCollate, |
| 1681 | int sortOrder |
| 1682 | ){ |
| 1683 | ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0); |
| 1684 | if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED) |
| 1685 | && pParse->db->init.busy==0 |
| 1686 | ){ |
| 1687 | sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"", |
| 1688 | pIdToken->n, pIdToken->z); |
| 1689 | } |
| 1690 | sqlite3ExprListSetName(pParse, p, pIdToken, 1); |
| 1691 | return p; |
| 1692 | } |
| 1693 | } // end %include |
| 1694 | |
| 1695 | eidlist_opt(A) ::= . {A = 0;} |
| 1696 | eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;} |
| 1697 | eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). { |
| 1698 | A = parserAddExprIdListTerm(pParse, A, &Y, C, Z); |
| 1699 | } |
| 1700 | eidlist(A) ::= nm(Y) collate(C) sortorder(Z). { |
| 1701 | A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/ |
| 1702 | } |
| 1703 | |
| 1704 | %type collate {int} |
| 1705 | collate(C) ::= . {C = 0;} |
| 1706 | collate(C) ::= COLLATE ids. {C = 1;} |
| 1707 | |
| 1708 | |
| 1709 | ///////////////////////////// The DROP INDEX command ///////////////////////// |
| 1710 | // |
| 1711 | cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);} |
| 1712 | |
| 1713 | ///////////////////////////// The VACUUM command ///////////////////////////// |
| 1714 | // |
| 1715 | %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH |
| 1716 | %type vinto {Expr*} |
| 1717 | %destructor vinto {sqlite3ExprDelete(pParse->db, $$);} |
| 1718 | cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);} |
| 1719 | cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);} |
| 1720 | vinto(A) ::= INTO expr(X). {A = X;} |
| 1721 | vinto(A) ::= . {A = 0;} |
| 1722 | %endif |
| 1723 | |
| 1724 | ///////////////////////////// The PRAGMA command ///////////////////////////// |
| 1725 | // |
| 1726 | %ifndef SQLITE_OMIT_PRAGMA |
| 1727 | cmd ::= PRAGMA nm(X) dbnm(Z). {sqlite3Pragma(pParse,&X,&Z,0,0);} |
| 1728 | cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y). {sqlite3Pragma(pParse,&X,&Z,&Y,0);} |
| 1729 | cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);} |
| 1730 | cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). |
| 1731 | {sqlite3Pragma(pParse,&X,&Z,&Y,1);} |
| 1732 | cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP. |
| 1733 | {sqlite3Pragma(pParse,&X,&Z,&Y,1);} |
| 1734 | |
| 1735 | nmnum(A) ::= plus_num(A). |
| 1736 | nmnum(A) ::= nm(A). |
| 1737 | nmnum(A) ::= ON(A). |
| 1738 | nmnum(A) ::= DELETE(A). |
| 1739 | nmnum(A) ::= DEFAULT(A). |
| 1740 | %endif SQLITE_OMIT_PRAGMA |
| 1741 | %token_class number INTEGER|FLOAT. |
| 1742 | plus_num(A) ::= PLUS number(X). {A = X;} |
| 1743 | plus_num(A) ::= number(A). |
| 1744 | minus_num(A) ::= MINUS number(X). {A = X;} |
| 1745 | //////////////////////////// The CREATE TRIGGER command ///////////////////// |
| 1746 | |
| 1747 | %ifndef SQLITE_OMIT_TRIGGER |
| 1748 | |
| 1749 | cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). { |
| 1750 | Token all; |
| 1751 | all.z = A.z; |
| 1752 | all.n = (int)(Z.z - A.z) + Z.n; |
| 1753 | sqlite3FinishTrigger(pParse, S, &all); |
| 1754 | } |
| 1755 | |
| 1756 | trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) |
| 1757 | trigger_time(C) trigger_event(D) |
| 1758 | ON fullname(E) foreach_clause when_clause(G). { |
| 1759 | sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR); |
| 1760 | A = (Z.n==0?B:Z); /*A-overwrites-T*/ |
| 1761 | #ifdef SQLITE_DEBUG |
| 1762 | assert( pParse->isCreate ); /* Set by createkw reduce action */ |
| 1763 | pParse->isCreate = 0; /* But, should not be set for CREATE TRIGGER */ |
| 1764 | #endif |
| 1765 | } |
| 1766 | |
| 1767 | %type trigger_time {int} |
| 1768 | trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ } |
| 1769 | trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} |
| 1770 | trigger_time(A) ::= . { A = TK_BEFORE; } |
| 1771 | |
| 1772 | %type trigger_event {struct TrigEvent} |
| 1773 | %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);} |
| 1774 | trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} |
| 1775 | trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} |
| 1776 | trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;} |
| 1777 | |
| 1778 | foreach_clause ::= . |
| 1779 | foreach_clause ::= FOR EACH ROW. |
| 1780 | |
| 1781 | %type when_clause {Expr*} |
| 1782 | %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);} |
| 1783 | when_clause(A) ::= . { A = 0; } |
| 1784 | when_clause(A) ::= WHEN expr(X). { A = X; } |
| 1785 | |
| 1786 | %type trigger_cmd_list {TriggerStep*} |
| 1787 | %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);} |
| 1788 | trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. { |
| 1789 | A->pLast->pNext = X; |
| 1790 | A->pLast = X; |
| 1791 | } |
| 1792 | trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. { |
| 1793 | A->pLast = A; |
| 1794 | } |
| 1795 | |
| 1796 | // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE |
| 1797 | // statements within triggers. We make a specific error message for this |
| 1798 | // since it is an exception to the default grammar rules. |
| 1799 | // |
| 1800 | tridxby ::= . |
| 1801 | tridxby ::= INDEXED BY nm. { |
| 1802 | sqlite3ErrorMsg(pParse, |
| 1803 | "the INDEXED BY clause is not allowed on UPDATE or DELETE statements " |
| 1804 | "within triggers"); |
| 1805 | } |
| 1806 | tridxby ::= NOT INDEXED. { |
| 1807 | sqlite3ErrorMsg(pParse, |
| 1808 | "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements " |
| 1809 | "within triggers"); |
| 1810 | } |
| 1811 | |
| 1812 | |
| 1813 | |
| 1814 | %type trigger_cmd {TriggerStep*} |
| 1815 | %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);} |
| 1816 | // UPDATE |
| 1817 | trigger_cmd(A) ::= |
| 1818 | UPDATE(B) orconf(R) xfullname(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E). |
| 1819 | {A = sqlite3TriggerUpdateStep(pParse, X, F, Y, Z, R, B.z, E);} |
| 1820 | |
| 1821 | // INSERT |
| 1822 | trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO |
| 1823 | xfullname(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). { |
| 1824 | A = sqlite3TriggerInsertStep(pParse,X,F,S,R,U,B,Z);/*A-overwrites-R*/ |
| 1825 | } |
| 1826 | // DELETE |
| 1827 | trigger_cmd(A) ::= DELETE(B) FROM xfullname(X) tridxby where_opt(Y) scanpt(E). |
| 1828 | {A = sqlite3TriggerDeleteStep(pParse, X, Y, B.z, E);} |
| 1829 | |
| 1830 | // SELECT |
| 1831 | trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E). |
| 1832 | {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/} |
| 1833 | |
| 1834 | // The special RAISE expression that may occur in trigger programs |
| 1835 | expr(A) ::= RAISE LP IGNORE RP. { |
| 1836 | A = sqlite3PExpr(pParse, TK_RAISE, 0, 0); |
| 1837 | if( A ){ |
| 1838 | A->affExpr = OE_Ignore; |
| 1839 | } |
| 1840 | } |
| 1841 | expr(A) ::= RAISE LP raisetype(T) COMMA expr(Z) RP. { |
| 1842 | A = sqlite3PExpr(pParse, TK_RAISE, Z, 0); |
| 1843 | if( A ) { |
| 1844 | A->affExpr = (char)T; |
| 1845 | } |
| 1846 | } |
| 1847 | %endif !SQLITE_OMIT_TRIGGER |
| 1848 | |
| 1849 | %type raisetype {int} |
| 1850 | raisetype(A) ::= ROLLBACK. {A = OE_Rollback;} |
| 1851 | raisetype(A) ::= ABORT. {A = OE_Abort;} |
| 1852 | raisetype(A) ::= FAIL. {A = OE_Fail;} |
| 1853 | |
| 1854 | |
| 1855 | //////////////////////// DROP TRIGGER statement ////////////////////////////// |
| 1856 | %ifndef SQLITE_OMIT_TRIGGER |
| 1857 | cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). { |
| 1858 | sqlite3DropTrigger(pParse,X,NOERR); |
| 1859 | } |
| 1860 | %endif !SQLITE_OMIT_TRIGGER |
| 1861 | |
| 1862 | //////////////////////// ATTACH DATABASE file AS name ///////////////////////// |
| 1863 | %ifndef SQLITE_OMIT_ATTACH |
| 1864 | cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). { |
| 1865 | sqlite3Attach(pParse, F, D, K); |
| 1866 | } |
| 1867 | cmd ::= DETACH database_kw_opt expr(D). { |
| 1868 | sqlite3Detach(pParse, D); |
| 1869 | } |
| 1870 | |
| 1871 | %type key_opt {Expr*} |
| 1872 | %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);} |
| 1873 | key_opt(A) ::= . { A = 0; } |
| 1874 | key_opt(A) ::= KEY expr(X). { A = X; } |
| 1875 | |
| 1876 | database_kw_opt ::= DATABASE. |
| 1877 | database_kw_opt ::= . |
| 1878 | %endif SQLITE_OMIT_ATTACH |
| 1879 | |
| 1880 | ////////////////////////// REINDEX collation ////////////////////////////////// |
| 1881 | %ifndef SQLITE_OMIT_REINDEX |
| 1882 | cmd ::= REINDEX. {sqlite3Reindex(pParse, 0, 0);} |
| 1883 | cmd ::= REINDEX nm(X) dbnm(Y). {sqlite3Reindex(pParse, &X, &Y);} |
| 1884 | %endif SQLITE_OMIT_REINDEX |
| 1885 | |
| 1886 | /////////////////////////////////// ANALYZE /////////////////////////////////// |
| 1887 | %ifndef SQLITE_OMIT_ANALYZE |
| 1888 | cmd ::= ANALYZE. {sqlite3Analyze(pParse, 0, 0);} |
| 1889 | cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);} |
| 1890 | %endif |
| 1891 | |
| 1892 | //////////////////////// ALTER TABLE table ... //////////////////////////////// |
| 1893 | %ifndef SQLITE_OMIT_ALTERTABLE |
| 1894 | %ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1895 | cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). { |
| 1896 | sqlite3AlterRenameTable(pParse,X,&Z); |
| 1897 | } |
| 1898 | |
| 1899 | // The ALTER TABLE ADD COLUMN command. This is broken into two sections so |
| 1900 | // that sqlite3AlterBeginAddColumn() is called before parsing the various |
| 1901 | // constraints and so on (carglist) attached to the new column definition. |
| 1902 | cmd ::= alter_add(Y) carglist. { |
| 1903 | Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n; |
| 1904 | sqlite3AlterFinishAddColumn(pParse, &Y); |
| 1905 | } |
| 1906 | alter_add(A) ::= ALTER TABLE fullname(X) ADD kwcolumn_opt nm(Y) typetoken(Z). { |
| 1907 | disableLookaside(pParse); |
| 1908 | sqlite3AlterBeginAddColumn(pParse, X); |
| 1909 | sqlite3AddColumn(pParse, Y, Z); |
| 1910 | A = Y; |
| 1911 | } |
| 1912 | |
| 1913 | cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). { |
| 1914 | sqlite3AlterDropColumn(pParse, X, &Y); |
| 1915 | } |
| 1916 | cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). { |
| 1917 | sqlite3AlterRenameColumn(pParse, X, &Y, &Z); |
| 1918 | } |
| 1919 | cmd ::= ALTER TABLE fullname(X) DROP CONSTRAINT nm(Y). { |
| 1920 | sqlite3AlterDropConstraint(pParse, X, &Y, 0); |
| 1921 | } |
| 1922 | cmd ::= ALTER TABLE fullname(X) ALTER kwcolumn_opt nm(Y) DROP NOT NULL. { |
| 1923 | sqlite3AlterDropConstraint(pParse, X, 0, &Y); |
| 1924 | } |
| 1925 | cmd ::= ALTER TABLE fullname(X) ALTER kwcolumn_opt nm(Y) SET NOT(Z) NULL onconf. { |
| 1926 | sqlite3AlterSetNotNull(pParse, X, &Y, &Z); |
| 1927 | } |
| 1928 | cmd ::= ALTER TABLE fullname(X) ADD CONSTRAINT(Y) nm(Z) CHECK LP(A) expr(E) RP(B) onconf. { |
| 1929 | sqlite3AlterAddConstraint(pParse, X, &Y, &Z, A.z+1, (B.z-A.z-1), E); |
| 1930 | } |
| 1931 | cmd ::= ALTER TABLE fullname(X) ADD CHECK(Y) LP(A) expr(E) RP(B) onconf. { |
| 1932 | sqlite3AlterAddConstraint(pParse, X, &Y, 0, A.z+1, (B.z-A.z-1), E); |
| 1933 | } |
| 1934 | |
| 1935 | kwcolumn_opt ::= . |
| 1936 | kwcolumn_opt ::= COLUMNKW. |
| 1937 | |
| 1938 | %endif SQLITE_OMIT_VIRTUALTABLE |
| 1939 | %endif SQLITE_OMIT_ALTERTABLE |
| 1940 | |
| 1941 | //////////////////////// CREATE VIRTUAL TABLE ... ///////////////////////////// |
| 1942 | %ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1943 | cmd ::= create_vtab. {sqlite3VtabFinishParse(pParse,0);} |
| 1944 | cmd ::= create_vtab LP vtabarglist RP(X). {sqlite3VtabFinishParse(pParse,&X);} |
| 1945 | create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E) |
| 1946 | nm(X) dbnm(Y) USING nm(Z). { |
| 1947 | sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E); |
| 1948 | } |
| 1949 | vtabarglist ::= vtabarg. |
| 1950 | vtabarglist ::= vtabarglist COMMA vtabarg. |
| 1951 | vtabarg ::= . {sqlite3VtabArgInit(pParse);} |
| 1952 | vtabarg ::= vtabarg vtabargtoken. |
| 1953 | vtabargtoken ::= ANY(X). {sqlite3VtabArgExtend(pParse,&X);} |
| 1954 | vtabargtoken ::= lp anylist RP(X). {sqlite3VtabArgExtend(pParse,&X);} |
| 1955 | lp ::= LP(X). {sqlite3VtabArgExtend(pParse,&X);} |
| 1956 | anylist ::= . |
| 1957 | anylist ::= anylist LP anylist RP. |
| 1958 | anylist ::= anylist ANY. |
| 1959 | %endif SQLITE_OMIT_VIRTUALTABLE |
| 1960 | |
| 1961 | |
| 1962 | //////////////////////// COMMON TABLE EXPRESSIONS //////////////////////////// |
| 1963 | %type wqlist {With*} |
| 1964 | %destructor wqlist {sqlite3WithDelete(pParse->db, $$);} |
| 1965 | %type wqitem {Cte*} |
| 1966 | // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable |
| 1967 | |
| 1968 | with ::= . |
| 1969 | %ifndef SQLITE_OMIT_CTE |
| 1970 | with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); } |
| 1971 | with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); } |
| 1972 | |
| 1973 | %type wqas {u8} |
| 1974 | wqas(A) ::= AS. {A = M10d_Any;} |
| 1975 | wqas(A) ::= AS MATERIALIZED. {A = M10d_Yes;} |
| 1976 | wqas(A) ::= AS NOT MATERIALIZED. {A = M10d_No;} |
| 1977 | wqitem(A) ::= withnm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. { |
| 1978 | A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/ |
| 1979 | } |
| 1980 | withnm(A) ::= nm(A). {pParse->bHasWith = 1;} |
| 1981 | wqlist(A) ::= wqitem(X). { |
| 1982 | A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/ |
| 1983 | } |
| 1984 | wqlist(A) ::= wqlist(A) COMMA wqitem(X). { |
| 1985 | A = sqlite3WithAdd(pParse, A, X); |
| 1986 | } |
| 1987 | %endif SQLITE_OMIT_CTE |
| 1988 | |
| 1989 | //////////////////////// WINDOW FUNCTION EXPRESSIONS ///////////////////////// |
| 1990 | // These must be at the end of this file. Specifically, the rules that |
| 1991 | // introduce tokens WINDOW, OVER and FILTER must appear last. This causes |
| 1992 | // the integer values assigned to these tokens to be larger than all other |
| 1993 | // tokens that may be output by the tokenizer except TK_SPACE, TK_COMMENT, |
| 1994 | // and TK_ILLEGAL. |
| 1995 | // |
| 1996 | %ifndef SQLITE_OMIT_WINDOWFUNC |
| 1997 | %type windowdefn_list {Window*} |
| 1998 | %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);} |
| 1999 | windowdefn_list(A) ::= windowdefn(A). |
| 2000 | windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). { |
| 2001 | assert( Z!=0 ); |
| 2002 | sqlite3WindowChain(pParse, Z, Y); |
| 2003 | Z->pNextWin = Y; |
| 2004 | A = Z; |
| 2005 | } |
| 2006 | |
| 2007 | %type windowdefn {Window*} |
| 2008 | %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);} |
| 2009 | windowdefn(A) ::= nm(X) AS LP window(Y) RP. { |
| 2010 | if( ALWAYS(Y) ){ |
| 2011 | Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n); |
| 2012 | } |
| 2013 | A = Y; |
| 2014 | } |
| 2015 | |
| 2016 | %type window {Window*} |
| 2017 | %destructor window {sqlite3WindowDelete(pParse->db, $$);} |
| 2018 | |
| 2019 | %type frame_opt {Window*} |
| 2020 | %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);} |
| 2021 | |
| 2022 | %type part_opt {ExprList*} |
| 2023 | %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);} |
| 2024 | |
| 2025 | %type filter_clause {Expr*} |
| 2026 | %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);} |
| 2027 | |
| 2028 | %type over_clause {Window*} |
| 2029 | %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);} |
| 2030 | |
| 2031 | %type filter_over {Window*} |
| 2032 | %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);} |
| 2033 | |
| 2034 | %type range_or_rows {int} |
| 2035 | |
| 2036 | %type frame_bound {struct FrameBound} |
| 2037 | %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);} |
| 2038 | %type frame_bound_s {struct FrameBound} |
| 2039 | %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);} |
| 2040 | %type frame_bound_e {struct FrameBound} |
| 2041 | %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);} |
| 2042 | |
| 2043 | window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). { |
| 2044 | A = sqlite3WindowAssemble(pParse, Z, X, Y, 0); |
| 2045 | } |
| 2046 | window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). { |
| 2047 | A = sqlite3WindowAssemble(pParse, Z, X, Y, &W); |
| 2048 | } |
| 2049 | window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). { |
| 2050 | A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0); |
| 2051 | } |
| 2052 | window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). { |
| 2053 | A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W); |
| 2054 | } |
| 2055 | window(A) ::= frame_opt(A). |
| 2056 | window(A) ::= nm(W) frame_opt(Z). { |
| 2057 | A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W); |
| 2058 | } |
| 2059 | |
| 2060 | frame_opt(A) ::= . { |
| 2061 | A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0); |
| 2062 | } |
| 2063 | frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). { |
| 2064 | A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z); |
| 2065 | } |
| 2066 | frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND |
| 2067 | frame_bound_e(Z) frame_exclude_opt(W). { |
| 2068 | A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W); |
| 2069 | } |
| 2070 | |
| 2071 | range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/} |
| 2072 | |
| 2073 | frame_bound_s(A) ::= frame_bound(X). {A = X;} |
| 2074 | frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;} |
| 2075 | frame_bound_e(A) ::= frame_bound(X). {A = X;} |
| 2076 | frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;} |
| 2077 | |
| 2078 | frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y). |
| 2079 | {A.eType = @Y; A.pExpr = X;} |
| 2080 | frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;} |
| 2081 | |
| 2082 | %type frame_exclude_opt {u8} |
| 2083 | frame_exclude_opt(A) ::= . {A = 0;} |
| 2084 | frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;} |
| 2085 | |
| 2086 | %type frame_exclude {u8} |
| 2087 | frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/} |
| 2088 | frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/} |
| 2089 | frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/} |
| 2090 | |
| 2091 | |
| 2092 | %type window_clause {Window*} |
| 2093 | %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);} |
| 2094 | window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; } |
| 2095 | |
| 2096 | filter_over(A) ::= filter_clause(F) over_clause(O). { |
| 2097 | if( O ){ |
| 2098 | O->pFilter = F; |
| 2099 | }else{ |
| 2100 | sqlite3ExprDelete(pParse->db, F); |
| 2101 | } |
| 2102 | A = O; |
| 2103 | } |
| 2104 | filter_over(A) ::= over_clause(O). { |
| 2105 | A = O; |
| 2106 | } |
| 2107 | filter_over(A) ::= filter_clause(F). { |
| 2108 | A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); |
| 2109 | if( A ){ |
| 2110 | A->eFrmType = TK_FILTER; |
| 2111 | A->pFilter = F; |
| 2112 | }else{ |
| 2113 | sqlite3ExprDelete(pParse->db, F); |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | over_clause(A) ::= OVER LP window(Z) RP. { |
| 2118 | A = Z; |
| 2119 | assert( A!=0 ); |
| 2120 | } |
| 2121 | over_clause(A) ::= OVER nm(Z). { |
| 2122 | A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); |
| 2123 | if( A ){ |
| 2124 | A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n); |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; } |
| 2129 | %endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 2130 | |
| 2131 | /* |
| 2132 | ** The code generator needs some extra TK_ token values for tokens that |
| 2133 | ** are synthesized and do not actually appear in the grammar: |
| 2134 | */ |
| 2135 | %token |
| 2136 | COLUMN /* Reference to a table column */ |
| 2137 | AGG_FUNCTION /* An aggregate function */ |
| 2138 | AGG_COLUMN /* An aggregated column */ |
| 2139 | TRUEFALSE /* True or false keyword */ |
| 2140 | ISNOT /* Combination of IS and NOT */ |
| 2141 | FUNCTION /* A function invocation */ |
| 2142 | UPLUS /* Unary plus */ |
| 2143 | UMINUS /* Unary minus */ |
| 2144 | TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */ |
| 2145 | REGISTER /* Reference to a VDBE register */ |
| 2146 | VECTOR /* Vector */ |
| 2147 | SELECT_COLUMN /* Choose a single column from a multi-column SELECT */ |
| 2148 | IF_NULL_ROW /* the if-null-row operator */ |
| 2149 | ASTERISK /* The "*" in count(*) and similar */ |
| 2150 | SPAN /* The span operator */ |
| 2151 | ERROR /* An expression containing an error */ |
| 2152 | . |
| 2153 | |
| 2154 | term(A) ::= QNUMBER(X). { |
| 2155 | A=tokenExpr(pParse,@X,X); |
| 2156 | sqlite3DequoteNumber(pParse, A); |
| 2157 | } |
| 2158 | |
| 2159 | /* There must be no more than 255 tokens defined above. If this grammar |
| 2160 | ** is extended with new rules and tokens, they must either be so few in |
| 2161 | ** number that TK_SPAN is no more than 255, or else the new tokens must |
| 2162 | ** appear after this line. |
| 2163 | */ |
| 2164 | %include { |
| 2165 | #if TK_SPAN>255 |
| 2166 | # error too many tokens in the grammar |
| 2167 | #endif |
| 2168 | } |
| 2169 | |
| 2170 | /* |
| 2171 | ** The TK_SPACE, TK_COMMENT, and TK_ILLEGAL tokens must be the last three |
| 2172 | ** tokens. The parser depends on this. Those tokens are not used in any |
| 2173 | ** grammar rule. They are only used by the tokenizer. Declare them last |
| 2174 | ** so that they are guaranteed to be the last three. |
| 2175 | */ |
| 2176 | %token SPACE COMMENT ILLEGAL. |