000001  %include {
000002  /*
000003  ** 2001-09-15
000004  **
000005  ** The author disclaims copyright to this source code.  In place of
000006  ** a legal notice, here is a blessing:
000007  **
000008  **    May you do good and not evil.
000009  **    May you find forgiveness for yourself and forgive others.
000010  **    May you share freely, never taking more than you give.
000011  **
000012  *************************************************************************
000013  ** This file contains SQLite's SQL parser.
000014  **
000015  ** The canonical source code to this file ("parse.y") is a Lemon grammar 
000016  ** file that specifies the input grammar and actions to take while parsing.
000017  ** That input file is processed by Lemon to generate a C-language 
000018  ** implementation of a parser for the given grammar.  You might be reading
000019  ** this comment as part of the translated C-code.  Edits should be made
000020  ** to the original parse.y sources.
000021  */
000022  }
000023  
000024  // All token codes are small integers with #defines that begin with "TK_"
000025  %token_prefix TK_
000026  
000027  // The type of the data attached to each token is Token.  This is also the
000028  // default type for non-terminals.
000029  //
000030  %token_type {Token}
000031  %default_type {Token}
000032  
000033  // An extra argument to the constructor for the parser, which is available
000034  // to all actions.
000035  %extra_context {Parse *pParse}
000036  
000037  // This code runs whenever there is a syntax error
000038  //
000039  %syntax_error {
000040    UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
000041    if( TOKEN.z[0] ){
000042      sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
000043    }else{
000044      sqlite3ErrorMsg(pParse, "incomplete input");
000045    }
000046  }
000047  %stack_overflow {
000048    sqlite3ErrorMsg(pParse, "parser stack overflow");
000049  }
000050  
000051  // The name of the generated procedure that implements the parser
000052  // is as follows:
000053  %name sqlite3Parser
000054  
000055  // The following text is included near the beginning of the C source
000056  // code file that implements the parser.
000057  //
000058  %include {
000059  #include "sqliteInt.h"
000060  
000061  /*
000062  ** Disable all error recovery processing in the parser push-down
000063  ** automaton.
000064  */
000065  #define YYNOERRORRECOVERY 1
000066  
000067  /*
000068  ** Make yytestcase() the same as testcase()
000069  */
000070  #define yytestcase(X) testcase(X)
000071  
000072  /*
000073  ** Indicate that sqlite3ParserFree() will never be called with a null
000074  ** pointer.
000075  */
000076  #define YYPARSEFREENEVERNULL 1
000077  
000078  /*
000079  ** In the amalgamation, the parse.c file generated by lemon and the
000080  ** tokenize.c file are concatenated.  In that case, sqlite3RunParser()
000081  ** has access to the the size of the yyParser object and so the parser
000082  ** engine can be allocated from stack.  In that case, only the
000083  ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
000084  ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
000085  ** omitted.
000086  */
000087  #ifdef SQLITE_AMALGAMATION
000088  # define sqlite3Parser_ENGINEALWAYSONSTACK 1
000089  #endif
000090  
000091  /*
000092  ** Alternative datatype for the argument to the malloc() routine passed
000093  ** into sqlite3ParserAlloc().  The default is size_t.
000094  */
000095  #define YYMALLOCARGTYPE  u64
000096  
000097  /*
000098  ** An instance of the following structure describes the event of a
000099  ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
000100  ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
000101  **
000102  **      UPDATE ON (a,b,c)
000103  **
000104  ** Then the "b" IdList records the list "a,b,c".
000105  */
000106  struct TrigEvent { int a; IdList * b; };
000107  
000108  struct FrameBound     { int eType; Expr *pExpr; };
000109  
000110  /*
000111  ** Disable lookaside memory allocation for objects that might be
000112  ** shared across database connections.
000113  */
000114  static void disableLookaside(Parse *pParse){
000115    sqlite3 *db = pParse->db;
000116    pParse->disableLookaside++;
000117    DisableLookaside;
000118  }
000119  
000120  #if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \
000121   && defined(SQLITE_UDL_CAPABLE_PARSER)
000122  /*
000123  ** Issue an error message if an ORDER BY or LIMIT clause occurs on an
000124  ** UPDATE or DELETE statement.
000125  */
000126  static void updateDeleteLimitError(
000127    Parse *pParse,
000128    ExprList *pOrderBy,
000129    Expr *pLimit
000130  ){
000131    if( pOrderBy ){
000132      sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
000133    }else{
000134      sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
000135    }
000136    sqlite3ExprListDelete(pParse->db, pOrderBy);
000137    sqlite3ExprDelete(pParse->db, pLimit);
000138  }
000139  #endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
000140  
000141  } // end %include
000142  
000143  // Input is a single SQL command
000144  input ::= cmdlist.
000145  cmdlist ::= cmdlist ecmd.
000146  cmdlist ::= ecmd.
000147  ecmd ::= SEMI.
000148  ecmd ::= cmdx SEMI.
000149  %ifndef SQLITE_OMIT_EXPLAIN
000150  ecmd ::= explain cmdx SEMI.       {NEVER-REDUCE}
000151  explain ::= EXPLAIN.              { if( pParse->pReprepare==0 ) pParse->explain = 1; }
000152  explain ::= EXPLAIN QUERY PLAN.   { if( pParse->pReprepare==0 ) pParse->explain = 2; }
000153  %endif  SQLITE_OMIT_EXPLAIN
000154  cmdx ::= cmd.           { sqlite3FinishCoding(pParse); }
000155  
000156  ///////////////////// Begin and end transactions. ////////////////////////////
000157  //
000158  
000159  cmd ::= BEGIN transtype(Y) trans_opt.  {sqlite3BeginTransaction(pParse, Y);}
000160  trans_opt ::= .
000161  trans_opt ::= TRANSACTION.
000162  trans_opt ::= TRANSACTION nm.
000163  %type transtype {int}
000164  transtype(A) ::= .             {A = TK_DEFERRED;}
000165  transtype(A) ::= DEFERRED(X).  {A = @X; /*A-overwrites-X*/}
000166  transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/}
000167  transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/}
000168  cmd ::= COMMIT|END(X) trans_opt.   {sqlite3EndTransaction(pParse,@X);}
000169  cmd ::= ROLLBACK(X) trans_opt.     {sqlite3EndTransaction(pParse,@X);}
000170  
000171  savepoint_opt ::= SAVEPOINT.
000172  savepoint_opt ::= .
000173  cmd ::= SAVEPOINT nm(X). {
000174    sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &X);
000175  }
000176  cmd ::= RELEASE savepoint_opt nm(X). {
000177    sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &X);
000178  }
000179  cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
000180    sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &X);
000181  }
000182  
000183  ///////////////////// The CREATE TABLE statement ////////////////////////////
000184  //
000185  cmd ::= create_table create_table_args.
000186  create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
000187     sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
000188  }
000189  createkw(A) ::= CREATE(A).  {disableLookaside(pParse);}
000190  
000191  %type ifnotexists {int}
000192  ifnotexists(A) ::= .              {A = 0;}
000193  ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
000194  %type temp {int}
000195  %ifndef SQLITE_OMIT_TEMPDB
000196  temp(A) ::= TEMP.  {A = pParse->db->init.busy==0;}
000197  %endif  SQLITE_OMIT_TEMPDB
000198  temp(A) ::= .      {A = 0;}
000199  create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_option_set(F). {
000200    sqlite3EndTable(pParse,&X,&E,F,0);
000201  }
000202  create_table_args ::= AS select(S). {
000203    sqlite3EndTable(pParse,0,0,0,S);
000204    sqlite3SelectDelete(pParse->db, S);
000205  }
000206  %type table_option_set {u32}
000207  %type table_option {u32}
000208  table_option_set(A) ::= .    {A = 0;}
000209  table_option_set(A) ::= table_option(A).
000210  table_option_set(A) ::= table_option_set(X) COMMA table_option(Y). {A = X|Y;}
000211  table_option(A) ::= WITHOUT nm(X). {
000212    if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
000213      A = TF_WithoutRowid | TF_NoVisibleRowid;
000214    }else{
000215      A = 0;
000216      sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
000217    }
000218  }
000219  table_option(A) ::= nm(X). {
000220    if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){
000221      A = TF_Strict;
000222    }else{
000223      A = 0;
000224      sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
000225    }
000226  }
000227  columnlist ::= columnlist COMMA columnname carglist.
000228  columnlist ::= columnname carglist.
000229  columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);}
000230  
000231  // Declare some tokens early in order to influence their values, to 
000232  // improve performance and reduce the executable size.  The goal here is
000233  // to get the "jump" operations in ISNULL through ESCAPE to have numeric
000234  // values that are early enough so that all jump operations are clustered
000235  // at the beginning.
000236  //
000237  %token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST.
000238  %token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL.
000239  %token OR AND NOT IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
000240  %token GT LE LT GE ESCAPE.
000241  
000242  // The following directive causes tokens ABORT, AFTER, ASC, etc. to
000243  // fallback to ID if they will not parse as their original value.
000244  // This obviates the need for the "id" nonterminal.
000245  //
000246  %fallback ID
000247    ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
000248    CONFLICT DATABASE DEFERRED DESC DETACH DO
000249    EACH END EXCLUSIVE EXPLAIN FAIL FOR
000250    IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
000251    QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
000252    ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
000253    NULLS FIRST LAST
000254  %ifdef SQLITE_OMIT_COMPOUND_SELECT
000255    EXCEPT INTERSECT UNION
000256  %endif SQLITE_OMIT_COMPOUND_SELECT
000257  %ifndef SQLITE_OMIT_WINDOWFUNC
000258    CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED
000259    EXCLUDE GROUPS OTHERS TIES
000260  %endif SQLITE_OMIT_WINDOWFUNC
000261  %ifndef SQLITE_OMIT_GENERATED_COLUMNS
000262    GENERATED ALWAYS
000263  %endif
000264    MATERIALIZED
000265    REINDEX RENAME CTIME_KW IF
000266    .
000267  %wildcard ANY.
000268  
000269  // Define operator precedence early so that this is the first occurrence
000270  // of the operator tokens in the grammar.  Keeping the operators together
000271  // causes them to be assigned integer values that are close together,
000272  // which keeps parser tables smaller.
000273  //
000274  // The token values assigned to these symbols is determined by the order
000275  // in which lemon first sees them.  It must be the case that ISNULL/NOTNULL,
000276  // NE/EQ, GT/LE, and GE/LT are separated by only a single value.  See
000277  // the sqlite3ExprIfFalse() routine for additional information on this
000278  // constraint.
000279  //
000280  %left OR.
000281  %left AND.
000282  %right NOT.
000283  %left IS MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ.
000284  %left GT LE LT GE.
000285  %right ESCAPE.
000286  %left BITAND BITOR LSHIFT RSHIFT.
000287  %left PLUS MINUS.
000288  %left STAR SLASH REM.
000289  %left CONCAT PTR.
000290  %left COLLATE.
000291  %right BITNOT.
000292  %nonassoc ON.
000293  
000294  // An IDENTIFIER can be a generic identifier, or one of several
000295  // keywords.  Any non-standard keyword can also be an identifier.
000296  //
000297  %token_class id  ID|INDEXED.
000298  
000299  // And "ids" is an identifer-or-string.
000300  //
000301  %token_class ids  ID|STRING.
000302  
000303  // An identifier or a join-keyword
000304  //
000305  %token_class idj  ID|INDEXED|JOIN_KW.
000306  
000307  // The name of a column or table can be any of the following:
000308  //
000309  %type nm {Token}
000310  nm(A) ::= idj(A).
000311  nm(A) ::= STRING(A).
000312  
000313  // A typetoken is really zero or more tokens that form a type name such
000314  // as can be found after the column name in a CREATE TABLE statement.
000315  // Multiple tokens are concatenated to form the value of the typetoken.
000316  //
000317  %type typetoken {Token}
000318  typetoken(A) ::= .   {A.n = 0; A.z = 0;}
000319  typetoken(A) ::= typename(A).
000320  typetoken(A) ::= typename(A) LP signed RP(Y). {
000321    A.n = (int)(&Y.z[Y.n] - A.z);
000322  }
000323  typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). {
000324    A.n = (int)(&Y.z[Y.n] - A.z);
000325  }
000326  %type typename {Token}
000327  typename(A) ::= ids(A).
000328  typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
000329  signed ::= plus_num.
000330  signed ::= minus_num.
000331  
000332  // The scanpt non-terminal takes a value which is a pointer to the
000333  // input text just past the last token that has been shifted into
000334  // the parser.  By surrounding some phrase in the grammar with two
000335  // scanpt non-terminals, we can capture the input text for that phrase.
000336  // For example:
000337  //
000338  //      something ::= .... scanpt(A) phrase scanpt(Z).
000339  //
000340  // The text that is parsed as "phrase" is a string starting at A
000341  // and containing (int)(Z-A) characters.  There might be some extra
000342  // whitespace on either end of the text, but that can be removed in
000343  // post-processing, if needed.
000344  //
000345  %type scanpt {const char*}
000346  scanpt(A) ::= . {
000347    assert( yyLookahead!=YYNOCODE );
000348    A = yyLookaheadToken.z;
000349  }
000350  scantok(A) ::= . {
000351    assert( yyLookahead!=YYNOCODE );
000352    A = yyLookaheadToken;
000353  }
000354  
000355  // "carglist" is a list of additional constraints that come after the
000356  // column name and column type in a CREATE TABLE statement.
000357  //
000358  carglist ::= carglist ccons.
000359  carglist ::= .
000360  ccons ::= CONSTRAINT nm(X).           {pParse->constraintName = X;}
000361  ccons ::= DEFAULT scantok(A) term(X).
000362                              {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);}
000363  ccons ::= DEFAULT LP(A) expr(X) RP(Z).
000364                              {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
000365  ccons ::= DEFAULT PLUS(A) scantok(Z) term(X).
000366                              {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);}
000367  ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). {
000368    Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
000369    sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]);
000370  }
000371  ccons ::= DEFAULT scantok id(X).       {
000372    Expr *p = tokenExpr(pParse, TK_STRING, X);
000373    if( p ){
000374      sqlite3ExprIdToTrueFalse(p);
000375      testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
000376    }
000377      sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
000378  }
000379  
000380  // In addition to the type name, we also care about the primary key and
000381  // UNIQUE constraints.
000382  //
000383  ccons ::= NULL onconf.
000384  ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
000385  ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
000386                                   {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
000387  ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
000388                                     SQLITE_IDXTYPE_UNIQUE);}
000389  ccons ::= CHECK LP(A) expr(X) RP(B).  {sqlite3AddCheckConstraint(pParse,X,A.z,B.z);}
000390  ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
000391                                   {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
000392  ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
000393  ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
000394  ccons ::= GENERATED ALWAYS AS generated.
000395  ccons ::= AS generated.
000396  generated ::= LP expr(E) RP.          {sqlite3AddGenerated(pParse,E,0);}
000397  generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);}
000398  
000399  // The optional AUTOINCREMENT keyword
000400  %type autoinc {int}
000401  autoinc(X) ::= .          {X = 0;}
000402  autoinc(X) ::= AUTOINCR.  {X = 1;}
000403  
000404  // The next group of rules parses the arguments to a REFERENCES clause
000405  // that determine if the referential integrity checking is deferred or
000406  // or immediate and which determine what action to take if a ref-integ
000407  // check fails.
000408  //
000409  %type refargs {int}
000410  refargs(A) ::= .                  { A = OE_None*0x0101; /* EV: R-19803-45884 */}
000411  refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; }
000412  %type refarg {struct {int value; int mask;}}
000413  refarg(A) ::= MATCH nm.              { A.value = 0;     A.mask = 0x000000; }
000414  refarg(A) ::= ON INSERT refact.      { A.value = 0;     A.mask = 0x000000; }
000415  refarg(A) ::= ON DELETE refact(X).   { A.value = X;     A.mask = 0x0000ff; }
000416  refarg(A) ::= ON UPDATE refact(X).   { A.value = X<<8;  A.mask = 0x00ff00; }
000417  %type refact {int}
000418  refact(A) ::= SET NULL.              { A = OE_SetNull;  /* EV: R-33326-45252 */}
000419  refact(A) ::= SET DEFAULT.           { A = OE_SetDflt;  /* EV: R-33326-45252 */}
000420  refact(A) ::= CASCADE.               { A = OE_Cascade;  /* EV: R-33326-45252 */}
000421  refact(A) ::= RESTRICT.              { A = OE_Restrict; /* EV: R-33326-45252 */}
000422  refact(A) ::= NO ACTION.             { A = OE_None;     /* EV: R-33326-45252 */}
000423  %type defer_subclause {int}
000424  defer_subclause(A) ::= NOT DEFERRABLE init_deferred_pred_opt.     {A = 0;}
000425  defer_subclause(A) ::= DEFERRABLE init_deferred_pred_opt(X).      {A = X;}
000426  %type init_deferred_pred_opt {int}
000427  init_deferred_pred_opt(A) ::= .                       {A = 0;}
000428  init_deferred_pred_opt(A) ::= INITIALLY DEFERRED.     {A = 1;}
000429  init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE.    {A = 0;}
000430  
000431  conslist_opt(A) ::= .                         {A.n = 0; A.z = 0;}
000432  conslist_opt(A) ::= COMMA(A) conslist.
000433  conslist ::= conslist tconscomma tcons.
000434  conslist ::= tcons.
000435  tconscomma ::= COMMA.            {pParse->constraintName.n = 0;}
000436  tconscomma ::= .
000437  tcons ::= CONSTRAINT nm(X).      {pParse->constraintName = X;}
000438  tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
000439                                   {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
000440  tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
000441                                   {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
000442                                         SQLITE_IDXTYPE_UNIQUE);}
000443  tcons ::= CHECK LP(A) expr(E) RP(B) onconf.
000444                                   {sqlite3AddCheckConstraint(pParse,E,A.z,B.z);}
000445  tcons ::= FOREIGN KEY LP eidlist(FA) RP
000446            REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
000447      sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
000448      sqlite3DeferForeignKey(pParse, D);
000449  }
000450  %type defer_subclause_opt {int}
000451  defer_subclause_opt(A) ::= .                    {A = 0;}
000452  defer_subclause_opt(A) ::= defer_subclause(A).
000453  
000454  // The following is a non-standard extension that allows us to declare the
000455  // default behavior when there is a constraint conflict.
000456  //
000457  %type onconf {int}
000458  %type orconf {int}
000459  %type resolvetype {int}
000460  onconf(A) ::= .                              {A = OE_Default;}
000461  onconf(A) ::= ON CONFLICT resolvetype(X).    {A = X;}
000462  orconf(A) ::= .                              {A = OE_Default;}
000463  orconf(A) ::= OR resolvetype(X).             {A = X;}
000464  resolvetype(A) ::= raisetype(A).
000465  resolvetype(A) ::= IGNORE.                   {A = OE_Ignore;}
000466  resolvetype(A) ::= REPLACE.                  {A = OE_Replace;}
000467  
000468  ////////////////////////// The DROP TABLE /////////////////////////////////////
000469  //
000470  cmd ::= DROP TABLE ifexists(E) fullname(X). {
000471    sqlite3DropTable(pParse, X, 0, E);
000472  }
000473  %type ifexists {int}
000474  ifexists(A) ::= IF EXISTS.   {A = 1;}
000475  ifexists(A) ::= .            {A = 0;}
000476  
000477  ///////////////////// The CREATE VIEW statement /////////////////////////////
000478  //
000479  %ifndef SQLITE_OMIT_VIEW
000480  cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) eidlist_opt(C)
000481            AS select(S). {
000482    sqlite3CreateView(pParse, &X, &Y, &Z, C, S, T, E);
000483  }
000484  cmd ::= DROP VIEW ifexists(E) fullname(X). {
000485    sqlite3DropTable(pParse, X, 1, E);
000486  }
000487  %endif  SQLITE_OMIT_VIEW
000488  
000489  //////////////////////// The SELECT statement /////////////////////////////////
000490  //
000491  cmd ::= select(X).  {
000492    SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
000493    sqlite3Select(pParse, X, &dest);
000494    sqlite3SelectDelete(pParse->db, X);
000495  }
000496  
000497  %type select {Select*}
000498  %destructor select {sqlite3SelectDelete(pParse->db, $$);}
000499  %type selectnowith {Select*}
000500  %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
000501  %type oneselect {Select*}
000502  %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
000503  
000504  %include {
000505    /*
000506    ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
000507    ** all elements in the list.  And make sure list length does not exceed
000508    ** SQLITE_LIMIT_COMPOUND_SELECT.
000509    */
000510    static void parserDoubleLinkSelect(Parse *pParse, Select *p){
000511      assert( p!=0 );
000512      if( p->pPrior ){
000513        Select *pNext = 0, *pLoop = p;
000514        int mxSelect, cnt = 1;
000515        while(1){
000516          pLoop->pNext = pNext;
000517          pLoop->selFlags |= SF_Compound;
000518          pNext = pLoop;
000519          pLoop = pLoop->pPrior;
000520          if( pLoop==0 ) break;
000521          cnt++;        
000522          if( pLoop->pOrderBy || pLoop->pLimit ){
000523            sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
000524               pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
000525               sqlite3SelectOpName(pNext->op));
000526            break;
000527          }
000528        }
000529        if( (p->selFlags & SF_MultiValue)==0 && 
000530          (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
000531          cnt>mxSelect
000532        ){
000533          sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
000534        }
000535      }
000536    }
000537  
000538    /* Attach a With object describing the WITH clause to a Select
000539    ** object describing the query for which the WITH clause is a prefix.
000540    */
000541    static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){
000542      if( pSelect ){
000543        pSelect->pWith = pWith;
000544        parserDoubleLinkSelect(pParse, pSelect);
000545      }else{
000546        sqlite3WithDelete(pParse->db, pWith);
000547      }
000548      return pSelect;
000549    }
000550  }
000551  
000552  %ifndef SQLITE_OMIT_CTE
000553  select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);}
000554  select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X).
000555                                                {A = attachWithToSelect(pParse,X,W);}
000556  %endif /* SQLITE_OMIT_CTE */
000557  select(A) ::= selectnowith(A). {
000558    Select *p = A;
000559    if( p ){
000560      parserDoubleLinkSelect(pParse, p);
000561    }
000562  }
000563  
000564  selectnowith(A) ::= oneselect(A).
000565  %ifndef SQLITE_OMIT_COMPOUND_SELECT
000566  selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z).  {
000567    Select *pRhs = Z;
000568    Select *pLhs = A;
000569    if( pRhs && pRhs->pPrior ){
000570      SrcList *pFrom;
000571      Token x;
000572      x.n = 0;
000573      parserDoubleLinkSelect(pParse, pRhs);
000574      pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0);
000575      pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
000576    }
000577    if( pRhs ){
000578      pRhs->op = (u8)Y;
000579      pRhs->pPrior = pLhs;
000580      if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
000581      pRhs->selFlags &= ~SF_MultiValue;
000582      if( Y!=TK_ALL ) pParse->hasCompound = 1;
000583    }else{
000584      sqlite3SelectDelete(pParse->db, pLhs);
000585    }
000586    A = pRhs;
000587  }
000588  %type multiselect_op {int}
000589  multiselect_op(A) ::= UNION(OP).             {A = @OP; /*A-overwrites-OP*/}
000590  multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
000591  multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP; /*A-overwrites-OP*/}
000592  %endif SQLITE_OMIT_COMPOUND_SELECT
000593  
000594  oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
000595                   groupby_opt(P) having_opt(Q) 
000596                   orderby_opt(Z) limit_opt(L). {
000597    A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
000598  }
000599  %ifndef SQLITE_OMIT_WINDOWFUNC
000600  oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
000601                   groupby_opt(P) having_opt(Q) window_clause(R)
000602                   orderby_opt(Z) limit_opt(L). {
000603    A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L);
000604    if( A ){
000605      A->pWinDefn = R;
000606    }else{
000607      sqlite3WindowListDelete(pParse->db, R);
000608    }
000609  }
000610  %endif
000611  
000612  
000613  oneselect(A) ::= values(A).
000614  
000615  %type values {Select*}
000616  %destructor values {sqlite3SelectDelete(pParse->db, $$);}
000617  values(A) ::= VALUES LP nexprlist(X) RP. {
000618    A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0);
000619  }
000620  values(A) ::= values(A) COMMA LP nexprlist(Y) RP. {
000621    Select *pRight, *pLeft = A;
000622    pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0);
000623    if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
000624    if( pRight ){
000625      pRight->op = TK_ALL;
000626      pRight->pPrior = pLeft;
000627      A = pRight;
000628    }else{
000629      A = pLeft;
000630    }
000631  }
000632  
000633  // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
000634  // present and false (0) if it is not.
000635  //
000636  %type distinct {int}
000637  distinct(A) ::= DISTINCT.   {A = SF_Distinct;}
000638  distinct(A) ::= ALL.        {A = SF_All;}
000639  distinct(A) ::= .           {A = 0;}
000640  
000641  // selcollist is a list of expressions that are to become the return
000642  // values of the SELECT statement.  The "*" in statements like
000643  // "SELECT * FROM ..." is encoded as a special expression with an
000644  // opcode of TK_ASTERISK.
000645  //
000646  %type selcollist {ExprList*}
000647  %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
000648  %type sclp {ExprList*}
000649  %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
000650  sclp(A) ::= selcollist(A) COMMA.
000651  sclp(A) ::= .                                {A = 0;}
000652  selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y).     {
000653     A = sqlite3ExprListAppend(pParse, A, X);
000654     if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
000655     sqlite3ExprListSetSpan(pParse,A,B,Z);
000656  }
000657  selcollist(A) ::= sclp(A) scanpt STAR(X). {
000658    Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
000659    sqlite3ExprSetErrorOffset(p, (int)(X.z - pParse->zTail));
000660    A = sqlite3ExprListAppend(pParse, A, p);
000661  }
000662  selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR(Y). {
000663    Expr *pRight, *pLeft, *pDot;
000664    pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
000665    sqlite3ExprSetErrorOffset(pRight, (int)(Y.z - pParse->zTail));
000666    pLeft = tokenExpr(pParse, TK_ID, X);
000667    pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
000668    A = sqlite3ExprListAppend(pParse,A, pDot);
000669  }
000670  
000671  // An option "AS <id>" phrase that can follow one of the expressions that
000672  // define the result set, or one of the tables in the FROM clause.
000673  //
000674  %type as {Token}
000675  as(X) ::= AS nm(Y).    {X = Y;}
000676  as(X) ::= ids(X).
000677  as(X) ::= .            {X.n = 0; X.z = 0;}
000678  
000679  
000680  %type seltablist {SrcList*}
000681  %destructor seltablist {sqlite3SrcListDelete(pParse->db, $$);}
000682  %type stl_prefix {SrcList*}
000683  %destructor stl_prefix {sqlite3SrcListDelete(pParse->db, $$);}
000684  %type from {SrcList*}
000685  %destructor from {sqlite3SrcListDelete(pParse->db, $$);}
000686  
000687  // A complete FROM clause.
000688  //
000689  from(A) ::= .                {A = 0;}
000690  from(A) ::= FROM seltablist(X). {
000691    A = X;
000692    sqlite3SrcListShiftJoinType(pParse,A);
000693  }
000694  
000695  // "seltablist" is a "Select Table List" - the content of the FROM clause
000696  // in a SELECT statement.  "stl_prefix" is a prefix of this list.
000697  //
000698  stl_prefix(A) ::= seltablist(A) joinop(Y).    {
000699     if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y;
000700  }
000701  stl_prefix(A) ::= .                           {A = 0;}
000702  seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) on_using(N). {
000703    A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
000704  }
000705  seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_by(I) on_using(N). {
000706    A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
000707    sqlite3SrcListIndexedBy(pParse, A, &I);
000708  }
000709  seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) on_using(N). {
000710    A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N);
000711    sqlite3SrcListFuncArgs(pParse, A, E);
000712  }
000713  %ifndef SQLITE_OMIT_SUBQUERY
000714    seltablist(A) ::= stl_prefix(A) LP select(S) RP as(Z) on_using(N). {
000715      A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,&N);
000716    }
000717    seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP as(Z) on_using(N). {
000718      if( A==0 && Z.n==0 && N.pOn==0 && N.pUsing==0 ){
000719        A = F;
000720      }else if( ALWAYS(F!=0) && F->nSrc==1 ){
000721        A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N);
000722        if( A ){
000723          SrcItem *pNew = &A->a[A->nSrc-1];
000724          SrcItem *pOld = F->a;
000725          pNew->zName = pOld->zName;
000726          pNew->zDatabase = pOld->zDatabase;
000727          pNew->pSelect = pOld->pSelect;
000728          if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
000729            pNew->fg.isNestedFrom = 1;
000730          }
000731          if( pOld->fg.isTabFunc ){
000732            pNew->u1.pFuncArg = pOld->u1.pFuncArg;
000733            pOld->u1.pFuncArg = 0;
000734            pOld->fg.isTabFunc = 0;
000735            pNew->fg.isTabFunc = 1;
000736          }
000737          pOld->zName = pOld->zDatabase = 0;
000738          pOld->pSelect = 0;
000739        }
000740        sqlite3SrcListDelete(pParse->db, F);
000741      }else{
000742        Select *pSubquery;
000743        sqlite3SrcListShiftJoinType(pParse,F);
000744        pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0);
000745        A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N);
000746      }
000747    }
000748  %endif  SQLITE_OMIT_SUBQUERY
000749  
000750  %type dbnm {Token}
000751  dbnm(A) ::= .          {A.z=0; A.n=0;}
000752  dbnm(A) ::= DOT nm(X). {A = X;}
000753  
000754  %type fullname {SrcList*}
000755  %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);}
000756  fullname(A) ::= nm(X).  {
000757    A = sqlite3SrcListAppend(pParse,0,&X,0);
000758    if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X);
000759  }
000760  fullname(A) ::= nm(X) DOT nm(Y). {
000761    A = sqlite3SrcListAppend(pParse,0,&X,&Y);
000762    if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y);
000763  }
000764  
000765  %type xfullname {SrcList*}
000766  %destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
000767  xfullname(A) ::= nm(X).  
000768     {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/}
000769  xfullname(A) ::= nm(X) DOT nm(Y).  
000770     {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/}
000771  xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z).  {
000772     A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/
000773     if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
000774  }
000775  xfullname(A) ::= nm(X) AS nm(Z). {  
000776     A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/
000777     if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
000778  }
000779  
000780  %type joinop {int}
000781  joinop(X) ::= COMMA|JOIN.              { X = JT_INNER; }
000782  joinop(X) ::= JOIN_KW(A) JOIN.
000783                    {X = sqlite3JoinType(pParse,&A,0,0);  /*X-overwrites-A*/}
000784  joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
000785                    {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
000786  joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
000787                    {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
000788  
000789  // There is a parsing abiguity in an upsert statement that uses a
000790  // SELECT on the RHS of a the INSERT:
000791  //
000792  //      INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ...
000793  //                                        here ----^^
000794  //
000795  // When the ON token is encountered, the parser does not know if it is
000796  // the beginning of an ON CONFLICT clause, or the beginning of an ON
000797  // clause associated with the JOIN.  The conflict is resolved in favor
000798  // of the JOIN.  If an ON CONFLICT clause is intended, insert a dummy
000799  // WHERE clause in between, like this:
000800  //
000801  //      INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ...
000802  //
000803  // The [AND] and [OR] precedence marks in the rules for on_using cause the
000804  // ON in this context to always be interpreted as belonging to the JOIN.
000805  //
000806  %type on_using {OnOrUsing}
000807  //%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);}
000808  on_using(N) ::= ON expr(E).            {N.pOn = E; N.pUsing = 0;}
000809  on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;}
000810  on_using(N) ::= .                 [OR] {N.pOn = 0; N.pUsing = 0;}
000811  
000812  // Note that this block abuses the Token type just a little. If there is
000813  // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
000814  // there is an INDEXED BY clause, then the token is populated as per normal,
000815  // with z pointing to the token data and n containing the number of bytes
000816  // in the token.
000817  //
000818  // If there is a "NOT INDEXED" clause, then (z==0 && n==1), which is 
000819  // normally illegal. The sqlite3SrcListIndexedBy() function 
000820  // recognizes and interprets this as a special case.
000821  //
000822  %type indexed_opt {Token}
000823  %type indexed_by  {Token}
000824  indexed_opt(A) ::= .                 {A.z=0; A.n=0;}
000825  indexed_opt(A) ::= indexed_by(A).
000826  indexed_by(A)  ::= INDEXED BY nm(X). {A = X;}
000827  indexed_by(A)  ::= NOT INDEXED.      {A.z=0; A.n=1;}
000828  
000829  %type orderby_opt {ExprList*}
000830  %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);}
000831  
000832  // the sortlist non-terminal stores a list of expression where each
000833  // expression is optionally followed by ASC or DESC to indicate the
000834  // sort order.
000835  //
000836  %type sortlist {ExprList*}
000837  %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
000838  
000839  orderby_opt(A) ::= .                          {A = 0;}
000840  orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
000841  sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
000842    A = sqlite3ExprListAppend(pParse,A,Y);
000843    sqlite3ExprListSetSortOrder(A,Z,X);
000844  }
000845  sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
000846    A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
000847    sqlite3ExprListSetSortOrder(A,Z,X);
000848  }
000849  
000850  %type sortorder {int}
000851  
000852  sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
000853  sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
000854  sortorder(A) ::= .              {A = SQLITE_SO_UNDEFINED;}
000855  
000856  %type nulls {int}
000857  nulls(A) ::= NULLS FIRST.       {A = SQLITE_SO_ASC;}
000858  nulls(A) ::= NULLS LAST.        {A = SQLITE_SO_DESC;}
000859  nulls(A) ::= .                  {A = SQLITE_SO_UNDEFINED;}
000860  
000861  %type groupby_opt {ExprList*}
000862  %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
000863  groupby_opt(A) ::= .                      {A = 0;}
000864  groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
000865  
000866  %type having_opt {Expr*}
000867  %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
000868  having_opt(A) ::= .                {A = 0;}
000869  having_opt(A) ::= HAVING expr(X).  {A = X;}
000870  
000871  %type limit_opt {Expr*}
000872  
000873  // The destructor for limit_opt will never fire in the current grammar.
000874  // The limit_opt non-terminal only occurs at the end of a single production
000875  // rule for SELECT statements.  As soon as the rule that create the 
000876  // limit_opt non-terminal reduces, the SELECT statement rule will also
000877  // reduce.  So there is never a limit_opt non-terminal on the stack 
000878  // except as a transient.  So there is never anything to destroy.
000879  //
000880  //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
000881  limit_opt(A) ::= .       {A = 0;}
000882  limit_opt(A) ::= LIMIT expr(X).
000883                           {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
000884  limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). 
000885                           {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
000886  limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 
000887                           {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
000888  
000889  /////////////////////////// The DELETE statement /////////////////////////////
000890  //
000891  %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
000892  cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W)
000893          orderby_opt(O) limit_opt(L). {
000894    sqlite3SrcListIndexedBy(pParse, X, &I);
000895  #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
000896    if( O || L ){
000897      updateDeleteLimitError(pParse,O,L);
000898      O = 0;
000899      L = 0;
000900    }
000901  #endif
000902    sqlite3DeleteFrom(pParse,X,W,O,L);
000903  }
000904  %else
000905  cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). {
000906    sqlite3SrcListIndexedBy(pParse, X, &I);
000907    sqlite3DeleteFrom(pParse,X,W,0,0);
000908  }
000909  %endif
000910  
000911  %type where_opt {Expr*}
000912  %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
000913  %type where_opt_ret {Expr*}
000914  %destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);}
000915  
000916  where_opt(A) ::= .                    {A = 0;}
000917  where_opt(A) ::= WHERE expr(X).       {A = X;}
000918  where_opt_ret(A) ::= .                                      {A = 0;}
000919  where_opt_ret(A) ::= WHERE expr(X).                         {A = X;}
000920  where_opt_ret(A) ::= RETURNING selcollist(X).               
000921         {sqlite3AddReturning(pParse,X); A = 0;}
000922  where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y).
000923         {sqlite3AddReturning(pParse,Y); A = X;}
000924  
000925  ////////////////////////// The UPDATE command ////////////////////////////////
000926  //
000927  %if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER
000928  cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
000929          where_opt_ret(W) orderby_opt(O) limit_opt(L).  {
000930    sqlite3SrcListIndexedBy(pParse, X, &I);
000931    if( F ){
000932      SrcList *pFromClause = F;
000933      if( pFromClause->nSrc>1 ){
000934        Select *pSubquery;
000935        Token as;
000936        pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
000937        as.n = 0;
000938        as.z = 0;
000939        pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
000940      }
000941      X = sqlite3SrcListAppendList(pParse, X, pFromClause);
000942    }
000943    sqlite3ExprListCheckLength(pParse,Y,"set list"); 
000944  #ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
000945    if( O || L ){
000946      updateDeleteLimitError(pParse,O,L);
000947      O = 0;
000948      L = 0;
000949    }
000950  #endif
000951    sqlite3Update(pParse,X,Y,W,R,O,L,0);
000952  }
000953  %else
000954  cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F)
000955          where_opt_ret(W). {
000956    sqlite3SrcListIndexedBy(pParse, X, &I);
000957    sqlite3ExprListCheckLength(pParse,Y,"set list"); 
000958    if( F ){
000959      SrcList *pFromClause = F;
000960      if( pFromClause->nSrc>1 ){
000961        Select *pSubquery;
000962        Token as;
000963        pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
000964        as.n = 0;
000965        as.z = 0;
000966        pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
000967      }
000968      X = sqlite3SrcListAppendList(pParse, X, pFromClause);
000969    }
000970    sqlite3Update(pParse,X,Y,W,R,0,0,0);
000971  }
000972  %endif
000973  
000974  
000975  
000976  %type setlist {ExprList*}
000977  %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
000978  
000979  setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
000980    A = sqlite3ExprListAppend(pParse, A, Y);
000981    sqlite3ExprListSetName(pParse, A, &X, 1);
000982  }
000983  setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
000984    A = sqlite3ExprListAppendVector(pParse, A, X, Y);
000985  }
000986  setlist(A) ::= nm(X) EQ expr(Y). {
000987    A = sqlite3ExprListAppend(pParse, 0, Y);
000988    sqlite3ExprListSetName(pParse, A, &X, 1);
000989  }
000990  setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
000991    A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
000992  }
000993  
000994  ////////////////////////// The INSERT command /////////////////////////////////
000995  //
000996  cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
000997          upsert(U). {
000998    sqlite3Insert(pParse, X, S, F, R, U);
000999  }
001000  cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning.
001001  {
001002    sqlite3Insert(pParse, X, 0, F, R, 0);
001003  }
001004  
001005  %type upsert {Upsert*}
001006  
001007  // Because upsert only occurs at the tip end of the INSERT rule for cmd,
001008  // there is never a case where the value of the upsert pointer will not
001009  // be destroyed by the cmd action.  So comment-out the destructor to
001010  // avoid unreachable code.
001011  //%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);}
001012  upsert(A) ::= . { A = 0; }
001013  upsert(A) ::= RETURNING selcollist(X).  { A = 0; sqlite3AddReturning(pParse,X); }
001014  upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW)
001015                DO UPDATE SET setlist(Z) where_opt(W) upsert(N).
001016                { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);}
001017  upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N).
001018                { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); }
001019  upsert(A) ::= ON CONFLICT DO NOTHING returning.
001020                { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
001021  upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning.
001022                { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);}
001023  
001024  returning ::= RETURNING selcollist(X).  {sqlite3AddReturning(pParse,X);}
001025  returning ::= .
001026  
001027  %type insert_cmd {int}
001028  insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
001029  insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
001030  
001031  %type idlist_opt {IdList*}
001032  %destructor idlist_opt {sqlite3IdListDelete(pParse->db, $$);}
001033  %type idlist {IdList*}
001034  %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
001035  
001036  idlist_opt(A) ::= .                       {A = 0;}
001037  idlist_opt(A) ::= LP idlist(X) RP.    {A = X;}
001038  idlist(A) ::= idlist(A) COMMA nm(Y).
001039      {A = sqlite3IdListAppend(pParse,A,&Y);}
001040  idlist(A) ::= nm(Y).
001041      {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/}
001042  
001043  /////////////////////////// Expression Processing /////////////////////////////
001044  //
001045  
001046  %type expr {Expr*}
001047  %destructor expr {sqlite3ExprDelete(pParse->db, $$);}
001048  %type term {Expr*}
001049  %destructor term {sqlite3ExprDelete(pParse->db, $$);}
001050  
001051  %include {
001052  
001053    /* Construct a new Expr object from a single token */
001054    static Expr *tokenExpr(Parse *pParse, int op, Token t){
001055      Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
001056      if( p ){
001057        /* memset(p, 0, sizeof(Expr)); */
001058        p->op = (u8)op;
001059        p->affExpr = 0;
001060        p->flags = EP_Leaf;
001061        ExprClearVVAProperties(p);
001062        /* p->iAgg = -1; // Not required */
001063        p->pLeft = p->pRight = 0;
001064        p->pAggInfo = 0;
001065        memset(&p->x, 0, sizeof(p->x));
001066        memset(&p->y, 0, sizeof(p->y));
001067        p->op2 = 0;
001068        p->iTable = 0;
001069        p->iColumn = 0;
001070        p->u.zToken = (char*)&p[1];
001071        memcpy(p->u.zToken, t.z, t.n);
001072        p->u.zToken[t.n] = 0;
001073        p->w.iOfst = (int)(t.z - pParse->zTail);
001074        if( sqlite3Isquote(p->u.zToken[0]) ){
001075          sqlite3DequoteExpr(p);
001076        }
001077  #if SQLITE_MAX_EXPR_DEPTH>0
001078        p->nHeight = 1;
001079  #endif  
001080        if( IN_RENAME_OBJECT ){
001081          return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
001082        }
001083      }
001084      return p;
001085    }
001086  
001087  }
001088  
001089  expr(A) ::= term(A).
001090  expr(A) ::= LP expr(X) RP. {A = X;}
001091  expr(A) ::= idj(X).          {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
001092  expr(A) ::= nm(X) DOT nm(Y). {
001093    Expr *temp1 = tokenExpr(pParse,TK_ID,X);
001094    Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
001095    A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
001096  }
001097  expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
001098    Expr *temp1 = tokenExpr(pParse,TK_ID,X);
001099    Expr *temp2 = tokenExpr(pParse,TK_ID,Y);
001100    Expr *temp3 = tokenExpr(pParse,TK_ID,Z);
001101    Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
001102    if( IN_RENAME_OBJECT ){
001103      sqlite3RenameTokenRemap(pParse, 0, temp1);
001104    }
001105    A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
001106  }
001107  term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
001108  term(A) ::= STRING(X).          {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
001109  term(A) ::= INTEGER(X). {
001110    A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
001111    if( A ) A->w.iOfst = (int)(X.z - pParse->zTail);
001112  }
001113  expr(A) ::= VARIABLE(X).     {
001114    if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
001115      u32 n = X.n;
001116      A = tokenExpr(pParse, TK_VARIABLE, X);
001117      sqlite3ExprAssignVarNumber(pParse, A, n);
001118    }else{
001119      /* When doing a nested parse, one can include terms in an expression
001120      ** that look like this:   #1 #2 ...  These terms refer to registers
001121      ** in the virtual machine.  #N is the N-th register. */
001122      Token t = X; /*A-overwrites-X*/
001123      assert( t.n>=2 );
001124      if( pParse->nested==0 ){
001125        sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
001126        A = 0;
001127      }else{
001128        A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
001129        if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
001130      }
001131    }
001132  }
001133  expr(A) ::= expr(A) COLLATE ids(C). {
001134    A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
001135  }
001136  %ifndef SQLITE_OMIT_CAST
001137  expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
001138    A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
001139    sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
001140  }
001141  %endif  SQLITE_OMIT_CAST
001142  
001143  
001144  expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP. {
001145    A = sqlite3ExprFunction(pParse, Y, &X, D);
001146  }
001147  expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP. {
001148    A = sqlite3ExprFunction(pParse, Y, &X, D);
001149    sqlite3ExprAddFunctionOrderBy(pParse, A, O);
001150  }
001151  expr(A) ::= idj(X) LP STAR RP. {
001152    A = sqlite3ExprFunction(pParse, 0, &X, 0);
001153  }
001154  
001155  %ifndef SQLITE_OMIT_WINDOWFUNC
001156  expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
001157    A = sqlite3ExprFunction(pParse, Y, &X, D);
001158    sqlite3WindowAttach(pParse, A, Z);
001159  }
001160  expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP filter_over(Z). {
001161    A = sqlite3ExprFunction(pParse, Y, &X, D);
001162    sqlite3WindowAttach(pParse, A, Z);
001163    sqlite3ExprAddFunctionOrderBy(pParse, A, O);
001164  }
001165  expr(A) ::= idj(X) LP STAR RP filter_over(Z). {
001166    A = sqlite3ExprFunction(pParse, 0, &X, 0);
001167    sqlite3WindowAttach(pParse, A, Z);
001168  }
001169  %endif
001170  
001171  term(A) ::= CTIME_KW(OP). {
001172    A = sqlite3ExprFunction(pParse, 0, &OP, 0);
001173  }
001174  
001175  expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
001176    ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
001177    A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
001178    if( A ){
001179      A->x.pList = pList;
001180      if( ALWAYS(pList->nExpr) ){
001181        A->flags |= pList->a[0].pExpr->flags & EP_Propagate;
001182      }
001183    }else{
001184      sqlite3ExprListDelete(pParse->db, pList);
001185    }
001186  }
001187  
001188  expr(A) ::= expr(A) AND expr(Y).        {A=sqlite3ExprAnd(pParse,A,Y);}
001189  expr(A) ::= expr(A) OR(OP) expr(Y).     {A=sqlite3PExpr(pParse,@OP,A,Y);}
001190  expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
001191                                          {A=sqlite3PExpr(pParse,@OP,A,Y);}
001192  expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {A=sqlite3PExpr(pParse,@OP,A,Y);}
001193  expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
001194                                          {A=sqlite3PExpr(pParse,@OP,A,Y);}
001195  expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
001196                                          {A=sqlite3PExpr(pParse,@OP,A,Y);}
001197  expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
001198                                          {A=sqlite3PExpr(pParse,@OP,A,Y);}
001199  expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
001200  %type likeop {Token}
001201  likeop(A) ::= LIKE_KW|MATCH(A).
001202  likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
001203  expr(A) ::= expr(A) likeop(OP) expr(Y).  [LIKE_KW]  {
001204    ExprList *pList;
001205    int bNot = OP.n & 0x80000000;
001206    OP.n &= 0x7fffffff;
001207    pList = sqlite3ExprListAppend(pParse,0, Y);
001208    pList = sqlite3ExprListAppend(pParse,pList, A);
001209    A = sqlite3ExprFunction(pParse, pList, &OP, 0);
001210    if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001211    if( A ) A->flags |= EP_InfixFunc;
001212  }
001213  expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E).  [LIKE_KW]  {
001214    ExprList *pList;
001215    int bNot = OP.n & 0x80000000;
001216    OP.n &= 0x7fffffff;
001217    pList = sqlite3ExprListAppend(pParse,0, Y);
001218    pList = sqlite3ExprListAppend(pParse,pList, A);
001219    pList = sqlite3ExprListAppend(pParse,pList, E);
001220    A = sqlite3ExprFunction(pParse, pList, &OP, 0);
001221    if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001222    if( A ) A->flags |= EP_InfixFunc;
001223  }
001224  
001225  expr(A) ::= expr(A) ISNULL|NOTNULL(E).   {A = sqlite3PExpr(pParse,@E,A,0);}
001226  expr(A) ::= expr(A) NOT NULL.    {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
001227  
001228  %include {
001229    /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
001230    ** unary TK_ISNULL or TK_NOTNULL expression. */
001231    static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
001232      sqlite3 *db = pParse->db;
001233      if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
001234        pA->op = (u8)op;
001235        sqlite3ExprDelete(db, pA->pRight);
001236        pA->pRight = 0;
001237      }
001238    }
001239  }
001240  
001241  //    expr1 IS expr2
001242  //    expr1 IS NOT expr2
001243  //
001244  // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL.  If expr2
001245  // is any other expression, code as TK_IS or TK_ISNOT.
001246  // 
001247  expr(A) ::= expr(A) IS expr(Y).     {
001248    A = sqlite3PExpr(pParse,TK_IS,A,Y);
001249    binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
001250  }
001251  expr(A) ::= expr(A) IS NOT expr(Y). {
001252    A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
001253    binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
001254  }
001255  expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y).     {
001256    A = sqlite3PExpr(pParse,TK_IS,A,Y);
001257    binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
001258  }
001259  expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). {
001260    A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
001261    binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
001262  }
001263  
001264  expr(A) ::= NOT(B) expr(X).  
001265                {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
001266  expr(A) ::= BITNOT(B) expr(X).
001267                {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
001268  expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
001269    A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
001270    /*A-overwrites-B*/
001271  }
001272  
001273  expr(A) ::= expr(B) PTR(C) expr(D). {
001274    ExprList *pList = sqlite3ExprListAppend(pParse, 0, B);
001275    pList = sqlite3ExprListAppend(pParse, pList, D);
001276    A = sqlite3ExprFunction(pParse, pList, &C, 0);
001277  }
001278  
001279  %type between_op {int}
001280  between_op(A) ::= BETWEEN.     {A = 0;}
001281  between_op(A) ::= NOT BETWEEN. {A = 1;}
001282  expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
001283    ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
001284    pList = sqlite3ExprListAppend(pParse,pList, Y);
001285    A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
001286    if( A ){
001287      A->x.pList = pList;
001288    }else{
001289      sqlite3ExprListDelete(pParse->db, pList);
001290    } 
001291    if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001292  }
001293  %ifndef SQLITE_OMIT_SUBQUERY
001294    %type in_op {int}
001295    in_op(A) ::= IN.      {A = 0;}
001296    in_op(A) ::= NOT IN.  {A = 1;}
001297    expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
001298      if( Y==0 ){
001299        /* Expressions of the form
001300        **
001301        **      expr1 IN ()
001302        **      expr1 NOT IN ()
001303        **
001304        ** simplify to constants 0 (false) and 1 (true), respectively,
001305        ** regardless of the value of expr1.
001306        */
001307        sqlite3ExprUnmapAndDelete(pParse, A);
001308        A = sqlite3Expr(pParse->db, TK_STRING, N ? "true" : "false");
001309        if( A ) sqlite3ExprIdToTrueFalse(A);
001310      }else{
001311        Expr *pRHS = Y->a[0].pExpr;
001312        if( Y->nExpr==1 && sqlite3ExprIsConstant(pRHS) && A->op!=TK_VECTOR ){
001313          Y->a[0].pExpr = 0;
001314          sqlite3ExprListDelete(pParse->db, Y);
001315          pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
001316          A = sqlite3PExpr(pParse, TK_EQ, A, pRHS);
001317        }else if( Y->nExpr==1 && pRHS->op==TK_SELECT ){
001318          A = sqlite3PExpr(pParse, TK_IN, A, 0);
001319          sqlite3PExprAddSelect(pParse, A, pRHS->x.pSelect);
001320          pRHS->x.pSelect = 0;
001321          sqlite3ExprListDelete(pParse->db, Y);
001322        }else{
001323          A = sqlite3PExpr(pParse, TK_IN, A, 0);
001324          if( A==0 ){
001325            sqlite3ExprListDelete(pParse->db, Y);
001326          }else if( A->pLeft->op==TK_VECTOR ){
001327            int nExpr = A->pLeft->x.pList->nExpr;
001328            Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y);
001329            if( pSelectRHS ){
001330              parserDoubleLinkSelect(pParse, pSelectRHS);
001331              sqlite3PExprAddSelect(pParse, A, pSelectRHS);
001332            }
001333          }else{
001334            A->x.pList = Y;
001335            sqlite3ExprSetHeightAndFlags(pParse, A);
001336          }
001337        }
001338        if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001339      }
001340    }
001341    expr(A) ::= LP select(X) RP. {
001342      A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
001343      sqlite3PExprAddSelect(pParse, A, X);
001344    }
001345    expr(A) ::= expr(A) in_op(N) LP select(Y) RP.  [IN] {
001346      A = sqlite3PExpr(pParse, TK_IN, A, 0);
001347      sqlite3PExprAddSelect(pParse, A, Y);
001348      if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001349    }
001350    expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
001351      SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
001352      Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
001353      if( E )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
001354      A = sqlite3PExpr(pParse, TK_IN, A, 0);
001355      sqlite3PExprAddSelect(pParse, A, pSelect);
001356      if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001357    }
001358    expr(A) ::= EXISTS LP select(Y) RP. {
001359      Expr *p;
001360      p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
001361      sqlite3PExprAddSelect(pParse, p, Y);
001362    }
001363  %endif SQLITE_OMIT_SUBQUERY
001364  
001365  /* CASE expressions */
001366  expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
001367    A = sqlite3PExpr(pParse, TK_CASE, X, 0);
001368    if( A ){
001369      A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
001370      sqlite3ExprSetHeightAndFlags(pParse, A);
001371    }else{
001372      sqlite3ExprListDelete(pParse->db, Y);
001373      sqlite3ExprDelete(pParse->db, Z);
001374    }
001375  }
001376  %type case_exprlist {ExprList*}
001377  %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
001378  case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
001379    A = sqlite3ExprListAppend(pParse,A, Y);
001380    A = sqlite3ExprListAppend(pParse,A, Z);
001381  }
001382  case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
001383    A = sqlite3ExprListAppend(pParse,0, Y);
001384    A = sqlite3ExprListAppend(pParse,A, Z);
001385  }
001386  %type case_else {Expr*}
001387  %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
001388  case_else(A) ::=  ELSE expr(X).         {A = X;}
001389  case_else(A) ::=  .                     {A = 0;} 
001390  %type case_operand {Expr*}
001391  %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
001392  case_operand(A) ::= expr(A).
001393  case_operand(A) ::= .                   {A = 0;} 
001394  
001395  %type exprlist {ExprList*}
001396  %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
001397  %type nexprlist {ExprList*}
001398  %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
001399  
001400  exprlist(A) ::= nexprlist(A).
001401  exprlist(A) ::= .                            {A = 0;}
001402  nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
001403      {A = sqlite3ExprListAppend(pParse,A,Y);}
001404  nexprlist(A) ::= expr(Y).
001405      {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
001406  
001407  %ifndef SQLITE_OMIT_SUBQUERY
001408  /* A paren_exprlist is an optional expression list contained inside
001409  ** of parenthesis */
001410  %type paren_exprlist {ExprList*}
001411  %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
001412  paren_exprlist(A) ::= .   {A = 0;}
001413  paren_exprlist(A) ::= LP exprlist(X) RP.  {A = X;}
001414  %endif SQLITE_OMIT_SUBQUERY
001415  
001416  
001417  ///////////////////////////// The CREATE INDEX command ///////////////////////
001418  //
001419  cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
001420          ON nm(Y) LP sortlist(Z) RP where_opt(W). {
001421    sqlite3CreateIndex(pParse, &X, &D, 
001422                       sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
001423                        &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
001424    if( IN_RENAME_OBJECT && pParse->pNewIndex ){
001425      sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
001426    }
001427  }
001428  
001429  %type uniqueflag {int}
001430  uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
001431  uniqueflag(A) ::= .        {A = OE_None;}
001432  
001433  
001434  // The eidlist non-terminal (Expression Id List) generates an ExprList
001435  // from a list of identifiers.  The identifier names are in ExprList.a[].zName.
001436  // This list is stored in an ExprList rather than an IdList so that it
001437  // can be easily sent to sqlite3ColumnsExprList().
001438  //
001439  // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
001440  // used for the arguments to an index.  That is just an historical accident.
001441  //
001442  // IMPORTANT COMPATIBILITY NOTE:  Some prior versions of SQLite accepted
001443  // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
001444  // places - places that might have been stored in the sqlite_schema table.
001445  // Those extra features were ignored.  But because they might be in some
001446  // (busted) old databases, we need to continue parsing them when loading
001447  // historical schemas.
001448  //
001449  %type eidlist {ExprList*}
001450  %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
001451  %type eidlist_opt {ExprList*}
001452  %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
001453  
001454  %include {
001455    /* Add a single new term to an ExprList that is used to store a
001456    ** list of identifiers.  Report an error if the ID list contains
001457    ** a COLLATE clause or an ASC or DESC keyword, except ignore the
001458    ** error while parsing a legacy schema.
001459    */
001460    static ExprList *parserAddExprIdListTerm(
001461      Parse *pParse,
001462      ExprList *pPrior,
001463      Token *pIdToken,
001464      int hasCollate,
001465      int sortOrder
001466    ){
001467      ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
001468      if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
001469          && pParse->db->init.busy==0
001470      ){
001471        sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
001472                           pIdToken->n, pIdToken->z);
001473      }
001474      sqlite3ExprListSetName(pParse, p, pIdToken, 1);
001475      return p;
001476    }
001477  } // end %include
001478  
001479  eidlist_opt(A) ::= .                         {A = 0;}
001480  eidlist_opt(A) ::= LP eidlist(X) RP.         {A = X;}
001481  eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z).  {
001482    A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
001483  }
001484  eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
001485    A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
001486  }
001487  
001488  %type collate {int}
001489  collate(C) ::= .              {C = 0;}
001490  collate(C) ::= COLLATE ids.   {C = 1;}
001491  
001492  
001493  ///////////////////////////// The DROP INDEX command /////////////////////////
001494  //
001495  cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
001496  
001497  ///////////////////////////// The VACUUM command /////////////////////////////
001498  //
001499  %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH
001500  %type vinto {Expr*}
001501  %destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
001502  cmd ::= VACUUM vinto(Y).                {sqlite3Vacuum(pParse,0,Y);}
001503  cmd ::= VACUUM nm(X) vinto(Y).          {sqlite3Vacuum(pParse,&X,Y);}
001504  vinto(A) ::= INTO expr(X).              {A = X;}
001505  vinto(A) ::= .                          {A = 0;}
001506  %endif
001507  
001508  ///////////////////////////// The PRAGMA command /////////////////////////////
001509  //
001510  %ifndef SQLITE_OMIT_PRAGMA
001511  cmd ::= PRAGMA nm(X) dbnm(Z).                {sqlite3Pragma(pParse,&X,&Z,0,0);}
001512  cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).    {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
001513  cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
001514  cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). 
001515                                               {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
001516  cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
001517                                               {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
001518  
001519  nmnum(A) ::= plus_num(A).
001520  nmnum(A) ::= nm(A).
001521  nmnum(A) ::= ON(A).
001522  nmnum(A) ::= DELETE(A).
001523  nmnum(A) ::= DEFAULT(A).
001524  %endif SQLITE_OMIT_PRAGMA
001525  %token_class number INTEGER|FLOAT.
001526  plus_num(A) ::= PLUS number(X).       {A = X;}
001527  plus_num(A) ::= number(A).
001528  minus_num(A) ::= MINUS number(X).     {A = X;}
001529  //////////////////////////// The CREATE TRIGGER command /////////////////////
001530  
001531  %ifndef SQLITE_OMIT_TRIGGER
001532  
001533  cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
001534    Token all;
001535    all.z = A.z;
001536    all.n = (int)(Z.z - A.z) + Z.n;
001537    sqlite3FinishTrigger(pParse, S, &all);
001538  }
001539  
001540  trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) 
001541                      trigger_time(C) trigger_event(D)
001542                      ON fullname(E) foreach_clause when_clause(G). {
001543    sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
001544    A = (Z.n==0?B:Z); /*A-overwrites-T*/
001545  }
001546  
001547  %type trigger_time {int}
001548  trigger_time(A) ::= BEFORE|AFTER(X).  { A = @X; /*A-overwrites-X*/ }
001549  trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
001550  trigger_time(A) ::= .            { A = TK_BEFORE; }
001551  
001552  %type trigger_event {struct TrigEvent}
001553  %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
001554  trigger_event(A) ::= DELETE|INSERT(X).   {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
001555  trigger_event(A) ::= UPDATE(X).          {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
001556  trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
001557  
001558  foreach_clause ::= .
001559  foreach_clause ::= FOR EACH ROW.
001560  
001561  %type when_clause {Expr*}
001562  %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
001563  when_clause(A) ::= .             { A = 0; }
001564  when_clause(A) ::= WHEN expr(X). { A = X; }
001565  
001566  %type trigger_cmd_list {TriggerStep*}
001567  %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
001568  trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
001569    assert( A!=0 );
001570    A->pLast->pNext = X;
001571    A->pLast = X;
001572  }
001573  trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. { 
001574    assert( A!=0 );
001575    A->pLast = A;
001576  }
001577  
001578  // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
001579  // within a trigger.  The table to INSERT, UPDATE, or DELETE is always in 
001580  // the same database as the table that the trigger fires on.
001581  //
001582  %type trnm {Token}
001583  trnm(A) ::= nm(A).
001584  trnm(A) ::= nm DOT nm(X). {
001585    A = X;
001586    sqlite3ErrorMsg(pParse, 
001587          "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
001588          "statements within triggers");
001589  }
001590  
001591  // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
001592  // statements within triggers.  We make a specific error message for this
001593  // since it is an exception to the default grammar rules.
001594  //
001595  tridxby ::= .
001596  tridxby ::= INDEXED BY nm. {
001597    sqlite3ErrorMsg(pParse,
001598          "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
001599          "within triggers");
001600  }
001601  tridxby ::= NOT INDEXED. {
001602    sqlite3ErrorMsg(pParse,
001603          "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
001604          "within triggers");
001605  }
001606  
001607  
001608  
001609  %type trigger_cmd {TriggerStep*}
001610  %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
001611  // UPDATE 
001612  trigger_cmd(A) ::=
001613     UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E).  
001614     {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);}
001615  
001616  // INSERT
001617  trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
001618                        trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
001619     A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
001620  }
001621  // DELETE
001622  trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
001623     {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
001624  
001625  // SELECT
001626  trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
001627     {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
001628  
001629  // The special RAISE expression that may occur in trigger programs
001630  expr(A) ::= RAISE LP IGNORE RP.  {
001631    A = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
001632    if( A ){
001633      A->affExpr = OE_Ignore;
001634    }
001635  }
001636  expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP.  {
001637    A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
001638    if( A ) {
001639      A->affExpr = (char)T;
001640    }
001641  }
001642  %endif  !SQLITE_OMIT_TRIGGER
001643  
001644  %type raisetype {int}
001645  raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
001646  raisetype(A) ::= ABORT.     {A = OE_Abort;}
001647  raisetype(A) ::= FAIL.      {A = OE_Fail;}
001648  
001649  
001650  ////////////////////////  DROP TRIGGER statement //////////////////////////////
001651  %ifndef SQLITE_OMIT_TRIGGER
001652  cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
001653    sqlite3DropTrigger(pParse,X,NOERR);
001654  }
001655  %endif  !SQLITE_OMIT_TRIGGER
001656  
001657  //////////////////////// ATTACH DATABASE file AS name /////////////////////////
001658  %ifndef SQLITE_OMIT_ATTACH
001659  cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
001660    sqlite3Attach(pParse, F, D, K);
001661  }
001662  cmd ::= DETACH database_kw_opt expr(D). {
001663    sqlite3Detach(pParse, D);
001664  }
001665  
001666  %type key_opt {Expr*}
001667  %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
001668  key_opt(A) ::= .                     { A = 0; }
001669  key_opt(A) ::= KEY expr(X).          { A = X; }
001670  
001671  database_kw_opt ::= DATABASE.
001672  database_kw_opt ::= .
001673  %endif SQLITE_OMIT_ATTACH
001674  
001675  ////////////////////////// REINDEX collation //////////////////////////////////
001676  %ifndef SQLITE_OMIT_REINDEX
001677  cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
001678  cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
001679  %endif  SQLITE_OMIT_REINDEX
001680  
001681  /////////////////////////////////// ANALYZE ///////////////////////////////////
001682  %ifndef SQLITE_OMIT_ANALYZE
001683  cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
001684  cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
001685  %endif
001686  
001687  //////////////////////// ALTER TABLE table ... ////////////////////////////////
001688  %ifndef SQLITE_OMIT_ALTERTABLE 
001689  %ifndef SQLITE_OMIT_VIRTUALTABLE
001690  cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
001691    sqlite3AlterRenameTable(pParse,X,&Z);
001692  }
001693  cmd ::= ALTER TABLE add_column_fullname
001694          ADD kwcolumn_opt columnname(Y) carglist. {
001695    Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
001696    sqlite3AlterFinishAddColumn(pParse, &Y);
001697  }
001698  cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). {
001699    sqlite3AlterDropColumn(pParse, X, &Y);
001700  }
001701  
001702  add_column_fullname ::= fullname(X). {
001703    disableLookaside(pParse);
001704    sqlite3AlterBeginAddColumn(pParse, X);
001705  }
001706  cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
001707    sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
001708  }
001709  
001710  kwcolumn_opt ::= .
001711  kwcolumn_opt ::= COLUMNKW.
001712  
001713  %endif SQLITE_OMIT_VIRTUALTABLE
001714  %endif SQLITE_OMIT_ALTERTABLE
001715  
001716  //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
001717  %ifndef SQLITE_OMIT_VIRTUALTABLE
001718  cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
001719  cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
001720  create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
001721                  nm(X) dbnm(Y) USING nm(Z). {
001722      sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
001723  }
001724  vtabarglist ::= vtabarg.
001725  vtabarglist ::= vtabarglist COMMA vtabarg.
001726  vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
001727  vtabarg ::= vtabarg vtabargtoken.
001728  vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
001729  vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
001730  lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
001731  anylist ::= .
001732  anylist ::= anylist LP anylist RP.
001733  anylist ::= anylist ANY.
001734  %endif  SQLITE_OMIT_VIRTUALTABLE
001735  
001736  
001737  //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
001738  %type wqlist {With*}
001739  %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
001740  %type wqitem {Cte*}
001741  // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable
001742  
001743  with ::= .
001744  %ifndef SQLITE_OMIT_CTE
001745  with ::= WITH wqlist(W).              { sqlite3WithPush(pParse, W, 1); }
001746  with ::= WITH RECURSIVE wqlist(W).    { sqlite3WithPush(pParse, W, 1); }
001747  
001748  %type wqas {u8}
001749  wqas(A)   ::= AS.                  {A = M10d_Any;}
001750  wqas(A)   ::= AS MATERIALIZED.     {A = M10d_Yes;}
001751  wqas(A)   ::= AS NOT MATERIALIZED. {A = M10d_No;}
001752  wqitem(A) ::= nm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. {
001753    A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/
001754  }
001755  wqlist(A) ::= wqitem(X). {
001756    A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/
001757  }
001758  wqlist(A) ::= wqlist(A) COMMA wqitem(X). {
001759    A = sqlite3WithAdd(pParse, A, X);
001760  }
001761  %endif  SQLITE_OMIT_CTE
001762  
001763  //////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
001764  // These must be at the end of this file. Specifically, the rules that
001765  // introduce tokens WINDOW, OVER and FILTER must appear last. This causes 
001766  // the integer values assigned to these tokens to be larger than all other 
001767  // tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
001768  //
001769  %ifndef SQLITE_OMIT_WINDOWFUNC
001770  %type windowdefn_list {Window*}
001771  %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
001772  windowdefn_list(A) ::= windowdefn(A).
001773  windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
001774    assert( Z!=0 );
001775    sqlite3WindowChain(pParse, Z, Y);
001776    Z->pNextWin = Y;
001777    A = Z;
001778  }
001779  
001780  %type windowdefn {Window*}
001781  %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
001782  windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
001783    if( ALWAYS(Y) ){
001784      Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
001785    }
001786    A = Y;
001787  }
001788  
001789  %type window {Window*}
001790  %destructor window {sqlite3WindowDelete(pParse->db, $$);}
001791  
001792  %type frame_opt {Window*}
001793  %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
001794  
001795  %type part_opt {ExprList*}
001796  %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
001797  
001798  %type filter_clause {Expr*}
001799  %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
001800  
001801  %type over_clause {Window*}
001802  %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
001803  
001804  %type filter_over {Window*}
001805  %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
001806  
001807  %type range_or_rows {int}
001808  
001809  %type frame_bound {struct FrameBound}
001810  %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
001811  %type frame_bound_s {struct FrameBound}
001812  %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
001813  %type frame_bound_e {struct FrameBound}
001814  %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
001815  
001816  window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
001817    A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
001818  }
001819  window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
001820    A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
001821  }
001822  window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
001823    A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
001824  }
001825  window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
001826    A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
001827  }
001828  window(A) ::= frame_opt(A).
001829  window(A) ::= nm(W) frame_opt(Z). {
001830    A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
001831  }
001832  
001833  frame_opt(A) ::= .                             { 
001834    A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
001835  }
001836  frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). { 
001837    A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
001838  }
001839  frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
001840                            frame_bound_e(Z) frame_exclude_opt(W). { 
001841    A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
001842  }
001843  
001844  range_or_rows(A) ::= RANGE|ROWS|GROUPS(X).   {A = @X; /*A-overwrites-X*/}
001845  
001846  frame_bound_s(A) ::= frame_bound(X).         {A = X;}
001847  frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
001848  frame_bound_e(A) ::= frame_bound(X).         {A = X;}
001849  frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
001850  
001851  frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
001852                                               {A.eType = @Y; A.pExpr = X;}
001853  frame_bound(A) ::= CURRENT(X) ROW.           {A.eType = @X; A.pExpr = 0;}
001854  
001855  %type frame_exclude_opt {u8}
001856  frame_exclude_opt(A) ::= . {A = 0;}
001857  frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
001858  
001859  %type frame_exclude {u8}
001860  frame_exclude(A) ::= NO(X) OTHERS.   {A = @X; /*A-overwrites-X*/}
001861  frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
001862  frame_exclude(A) ::= GROUP|TIES(X).  {A = @X; /*A-overwrites-X*/}
001863  
001864  
001865  %type window_clause {Window*}
001866  %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
001867  window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
001868  
001869  filter_over(A) ::= filter_clause(F) over_clause(O). {
001870    if( O ){
001871      O->pFilter = F;
001872    }else{
001873      sqlite3ExprDelete(pParse->db, F);
001874    }
001875    A = O;
001876  }
001877  filter_over(A) ::= over_clause(O). {
001878    A = O;
001879  }
001880  filter_over(A) ::= filter_clause(F). {
001881    A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
001882    if( A ){
001883      A->eFrmType = TK_FILTER;
001884      A->pFilter = F;
001885    }else{
001886      sqlite3ExprDelete(pParse->db, F);
001887    }
001888  }
001889  
001890  over_clause(A) ::= OVER LP window(Z) RP. {
001891    A = Z;
001892    assert( A!=0 );
001893  }
001894  over_clause(A) ::= OVER nm(Z). {
001895    A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
001896    if( A ){
001897      A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
001898    }
001899  }
001900  
001901  filter_clause(A) ::= FILTER LP WHERE expr(X) RP.  { A = X; }
001902  %endif /* SQLITE_OMIT_WINDOWFUNC */
001903  
001904  /*
001905  ** The code generator needs some extra TK_ token values for tokens that
001906  ** are synthesized and do not actually appear in the grammar:
001907  */
001908  %token
001909    COLUMN          /* Reference to a table column */
001910    AGG_FUNCTION    /* An aggregate function */
001911    AGG_COLUMN      /* An aggregated column */
001912    TRUEFALSE       /* True or false keyword */
001913    ISNOT           /* Combination of IS and NOT */
001914    FUNCTION        /* A function invocation */
001915    UMINUS          /* Unary minus */
001916    UPLUS           /* Unary plus */
001917    TRUTH           /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
001918    REGISTER        /* Reference to a VDBE register */
001919    VECTOR          /* Vector */
001920    SELECT_COLUMN   /* Choose a single column from a multi-column SELECT */
001921    IF_NULL_ROW     /* the if-null-row operator */
001922    ASTERISK        /* The "*" in count(*) and similar */
001923    SPAN            /* The span operator */
001924    ERROR           /* An expression containing an error */
001925  .
001926  /* There must be no more than 255 tokens defined above.  If this grammar
001927  ** is extended with new rules and tokens, they must either be so few in
001928  ** number that TK_SPAN is no more than 255, or else the new tokens must
001929  ** appear after this line.
001930  */
001931  %include {
001932  #if TK_SPAN>255
001933  # error too many tokens in the grammar
001934  #endif
001935  }
001936  
001937  /*
001938  ** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens.  The
001939  ** parser depends on this.  Those tokens are not used in any grammar rule.
001940  ** They are only used by the tokenizer.  Declare them last so that they
001941  ** are guaranteed to be the last two tokens
001942  */
001943  %token SPACE ILLEGAL.