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 STAR RP. {
001148    A = sqlite3ExprFunction(pParse, 0, &X, 0);
001149  }
001150  
001151  %ifndef SQLITE_OMIT_WINDOWFUNC
001152  expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
001153    A = sqlite3ExprFunction(pParse, Y, &X, D);
001154    sqlite3WindowAttach(pParse, A, Z);
001155  }
001156  expr(A) ::= idj(X) LP STAR RP filter_over(Z). {
001157    A = sqlite3ExprFunction(pParse, 0, &X, 0);
001158    sqlite3WindowAttach(pParse, A, Z);
001159  }
001160  %endif
001161  
001162  term(A) ::= CTIME_KW(OP). {
001163    A = sqlite3ExprFunction(pParse, 0, &OP, 0);
001164  }
001165  
001166  expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
001167    ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
001168    A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
001169    if( A ){
001170      A->x.pList = pList;
001171      if( ALWAYS(pList->nExpr) ){
001172        A->flags |= pList->a[0].pExpr->flags & EP_Propagate;
001173      }
001174    }else{
001175      sqlite3ExprListDelete(pParse->db, pList);
001176    }
001177  }
001178  
001179  expr(A) ::= expr(A) AND expr(Y).        {A=sqlite3ExprAnd(pParse,A,Y);}
001180  expr(A) ::= expr(A) OR(OP) expr(Y).     {A=sqlite3PExpr(pParse,@OP,A,Y);}
001181  expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
001182                                          {A=sqlite3PExpr(pParse,@OP,A,Y);}
001183  expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {A=sqlite3PExpr(pParse,@OP,A,Y);}
001184  expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
001185                                          {A=sqlite3PExpr(pParse,@OP,A,Y);}
001186  expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
001187                                          {A=sqlite3PExpr(pParse,@OP,A,Y);}
001188  expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
001189                                          {A=sqlite3PExpr(pParse,@OP,A,Y);}
001190  expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
001191  %type likeop {Token}
001192  likeop(A) ::= LIKE_KW|MATCH(A).
001193  likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
001194  expr(A) ::= expr(A) likeop(OP) expr(Y).  [LIKE_KW]  {
001195    ExprList *pList;
001196    int bNot = OP.n & 0x80000000;
001197    OP.n &= 0x7fffffff;
001198    pList = sqlite3ExprListAppend(pParse,0, Y);
001199    pList = sqlite3ExprListAppend(pParse,pList, A);
001200    A = sqlite3ExprFunction(pParse, pList, &OP, 0);
001201    if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001202    if( A ) A->flags |= EP_InfixFunc;
001203  }
001204  expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E).  [LIKE_KW]  {
001205    ExprList *pList;
001206    int bNot = OP.n & 0x80000000;
001207    OP.n &= 0x7fffffff;
001208    pList = sqlite3ExprListAppend(pParse,0, Y);
001209    pList = sqlite3ExprListAppend(pParse,pList, A);
001210    pList = sqlite3ExprListAppend(pParse,pList, E);
001211    A = sqlite3ExprFunction(pParse, pList, &OP, 0);
001212    if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001213    if( A ) A->flags |= EP_InfixFunc;
001214  }
001215  
001216  expr(A) ::= expr(A) ISNULL|NOTNULL(E).   {A = sqlite3PExpr(pParse,@E,A,0);}
001217  expr(A) ::= expr(A) NOT NULL.    {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
001218  
001219  %include {
001220    /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
001221    ** unary TK_ISNULL or TK_NOTNULL expression. */
001222    static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
001223      sqlite3 *db = pParse->db;
001224      if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
001225        pA->op = (u8)op;
001226        sqlite3ExprDelete(db, pA->pRight);
001227        pA->pRight = 0;
001228      }
001229    }
001230  }
001231  
001232  //    expr1 IS expr2
001233  //    expr1 IS NOT expr2
001234  //
001235  // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL.  If expr2
001236  // is any other expression, code as TK_IS or TK_ISNOT.
001237  // 
001238  expr(A) ::= expr(A) IS expr(Y).     {
001239    A = sqlite3PExpr(pParse,TK_IS,A,Y);
001240    binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
001241  }
001242  expr(A) ::= expr(A) IS NOT expr(Y). {
001243    A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
001244    binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
001245  }
001246  expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y).     {
001247    A = sqlite3PExpr(pParse,TK_IS,A,Y);
001248    binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
001249  }
001250  expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). {
001251    A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
001252    binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
001253  }
001254  
001255  expr(A) ::= NOT(B) expr(X).  
001256                {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
001257  expr(A) ::= BITNOT(B) expr(X).
001258                {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
001259  expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] {
001260    A = sqlite3PExpr(pParse, @B==TK_PLUS ? TK_UPLUS : TK_UMINUS, X, 0);
001261    /*A-overwrites-B*/
001262  }
001263  
001264  expr(A) ::= expr(B) PTR(C) expr(D). {
001265    ExprList *pList = sqlite3ExprListAppend(pParse, 0, B);
001266    pList = sqlite3ExprListAppend(pParse, pList, D);
001267    A = sqlite3ExprFunction(pParse, pList, &C, 0);
001268  }
001269  
001270  %type between_op {int}
001271  between_op(A) ::= BETWEEN.     {A = 0;}
001272  between_op(A) ::= NOT BETWEEN. {A = 1;}
001273  expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
001274    ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
001275    pList = sqlite3ExprListAppend(pParse,pList, Y);
001276    A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
001277    if( A ){
001278      A->x.pList = pList;
001279    }else{
001280      sqlite3ExprListDelete(pParse->db, pList);
001281    } 
001282    if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001283  }
001284  %ifndef SQLITE_OMIT_SUBQUERY
001285    %type in_op {int}
001286    in_op(A) ::= IN.      {A = 0;}
001287    in_op(A) ::= NOT IN.  {A = 1;}
001288    expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
001289      if( Y==0 ){
001290        /* Expressions of the form
001291        **
001292        **      expr1 IN ()
001293        **      expr1 NOT IN ()
001294        **
001295        ** simplify to constants 0 (false) and 1 (true), respectively,
001296        ** regardless of the value of expr1.
001297        */
001298        sqlite3ExprUnmapAndDelete(pParse, A);
001299        A = sqlite3Expr(pParse->db, TK_STRING, N ? "true" : "false");
001300        if( A ) sqlite3ExprIdToTrueFalse(A);
001301      }else{
001302        Expr *pRHS = Y->a[0].pExpr;
001303        if( Y->nExpr==1 && sqlite3ExprIsConstant(pRHS) && A->op!=TK_VECTOR ){
001304          Y->a[0].pExpr = 0;
001305          sqlite3ExprListDelete(pParse->db, Y);
001306          pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0);
001307          A = sqlite3PExpr(pParse, TK_EQ, A, pRHS);
001308        }else if( Y->nExpr==1 && pRHS->op==TK_SELECT ){
001309          A = sqlite3PExpr(pParse, TK_IN, A, 0);
001310          sqlite3PExprAddSelect(pParse, A, pRHS->x.pSelect);
001311          pRHS->x.pSelect = 0;
001312          sqlite3ExprListDelete(pParse->db, Y);
001313        }else{
001314          A = sqlite3PExpr(pParse, TK_IN, A, 0);
001315          if( A==0 ){
001316            sqlite3ExprListDelete(pParse->db, Y);
001317          }else if( A->pLeft->op==TK_VECTOR ){
001318            int nExpr = A->pLeft->x.pList->nExpr;
001319            Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y);
001320            if( pSelectRHS ){
001321              parserDoubleLinkSelect(pParse, pSelectRHS);
001322              sqlite3PExprAddSelect(pParse, A, pSelectRHS);
001323            }
001324          }else{
001325            A->x.pList = Y;
001326            sqlite3ExprSetHeightAndFlags(pParse, A);
001327          }
001328        }
001329        if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001330      }
001331    }
001332    expr(A) ::= LP select(X) RP. {
001333      A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
001334      sqlite3PExprAddSelect(pParse, A, X);
001335    }
001336    expr(A) ::= expr(A) in_op(N) LP select(Y) RP.  [IN] {
001337      A = sqlite3PExpr(pParse, TK_IN, A, 0);
001338      sqlite3PExprAddSelect(pParse, A, Y);
001339      if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001340    }
001341    expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
001342      SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z);
001343      Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
001344      if( E )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
001345      A = sqlite3PExpr(pParse, TK_IN, A, 0);
001346      sqlite3PExprAddSelect(pParse, A, pSelect);
001347      if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
001348    }
001349    expr(A) ::= EXISTS LP select(Y) RP. {
001350      Expr *p;
001351      p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
001352      sqlite3PExprAddSelect(pParse, p, Y);
001353    }
001354  %endif SQLITE_OMIT_SUBQUERY
001355  
001356  /* CASE expressions */
001357  expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
001358    A = sqlite3PExpr(pParse, TK_CASE, X, 0);
001359    if( A ){
001360      A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
001361      sqlite3ExprSetHeightAndFlags(pParse, A);
001362    }else{
001363      sqlite3ExprListDelete(pParse->db, Y);
001364      sqlite3ExprDelete(pParse->db, Z);
001365    }
001366  }
001367  %type case_exprlist {ExprList*}
001368  %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
001369  case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
001370    A = sqlite3ExprListAppend(pParse,A, Y);
001371    A = sqlite3ExprListAppend(pParse,A, Z);
001372  }
001373  case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
001374    A = sqlite3ExprListAppend(pParse,0, Y);
001375    A = sqlite3ExprListAppend(pParse,A, Z);
001376  }
001377  %type case_else {Expr*}
001378  %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
001379  case_else(A) ::=  ELSE expr(X).         {A = X;}
001380  case_else(A) ::=  .                     {A = 0;} 
001381  %type case_operand {Expr*}
001382  %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
001383  case_operand(A) ::= expr(A).
001384  case_operand(A) ::= .                   {A = 0;} 
001385  
001386  %type exprlist {ExprList*}
001387  %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
001388  %type nexprlist {ExprList*}
001389  %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
001390  
001391  exprlist(A) ::= nexprlist(A).
001392  exprlist(A) ::= .                            {A = 0;}
001393  nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
001394      {A = sqlite3ExprListAppend(pParse,A,Y);}
001395  nexprlist(A) ::= expr(Y).
001396      {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
001397  
001398  %ifndef SQLITE_OMIT_SUBQUERY
001399  /* A paren_exprlist is an optional expression list contained inside
001400  ** of parenthesis */
001401  %type paren_exprlist {ExprList*}
001402  %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
001403  paren_exprlist(A) ::= .   {A = 0;}
001404  paren_exprlist(A) ::= LP exprlist(X) RP.  {A = X;}
001405  %endif SQLITE_OMIT_SUBQUERY
001406  
001407  
001408  ///////////////////////////// The CREATE INDEX command ///////////////////////
001409  //
001410  cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
001411          ON nm(Y) LP sortlist(Z) RP where_opt(W). {
001412    sqlite3CreateIndex(pParse, &X, &D, 
001413                       sqlite3SrcListAppend(pParse,0,&Y,0), Z, U,
001414                        &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
001415    if( IN_RENAME_OBJECT && pParse->pNewIndex ){
001416      sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y);
001417    }
001418  }
001419  
001420  %type uniqueflag {int}
001421  uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
001422  uniqueflag(A) ::= .        {A = OE_None;}
001423  
001424  
001425  // The eidlist non-terminal (Expression Id List) generates an ExprList
001426  // from a list of identifiers.  The identifier names are in ExprList.a[].zName.
001427  // This list is stored in an ExprList rather than an IdList so that it
001428  // can be easily sent to sqlite3ColumnsExprList().
001429  //
001430  // eidlist is grouped with CREATE INDEX because it used to be the non-terminal
001431  // used for the arguments to an index.  That is just an historical accident.
001432  //
001433  // IMPORTANT COMPATIBILITY NOTE:  Some prior versions of SQLite accepted
001434  // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate
001435  // places - places that might have been stored in the sqlite_schema table.
001436  // Those extra features were ignored.  But because they might be in some
001437  // (busted) old databases, we need to continue parsing them when loading
001438  // historical schemas.
001439  //
001440  %type eidlist {ExprList*}
001441  %destructor eidlist {sqlite3ExprListDelete(pParse->db, $$);}
001442  %type eidlist_opt {ExprList*}
001443  %destructor eidlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
001444  
001445  %include {
001446    /* Add a single new term to an ExprList that is used to store a
001447    ** list of identifiers.  Report an error if the ID list contains
001448    ** a COLLATE clause or an ASC or DESC keyword, except ignore the
001449    ** error while parsing a legacy schema.
001450    */
001451    static ExprList *parserAddExprIdListTerm(
001452      Parse *pParse,
001453      ExprList *pPrior,
001454      Token *pIdToken,
001455      int hasCollate,
001456      int sortOrder
001457    ){
001458      ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
001459      if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
001460          && pParse->db->init.busy==0
001461      ){
001462        sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
001463                           pIdToken->n, pIdToken->z);
001464      }
001465      sqlite3ExprListSetName(pParse, p, pIdToken, 1);
001466      return p;
001467    }
001468  } // end %include
001469  
001470  eidlist_opt(A) ::= .                         {A = 0;}
001471  eidlist_opt(A) ::= LP eidlist(X) RP.         {A = X;}
001472  eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z).  {
001473    A = parserAddExprIdListTerm(pParse, A, &Y, C, Z);
001474  }
001475  eidlist(A) ::= nm(Y) collate(C) sortorder(Z). {
001476    A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/
001477  }
001478  
001479  %type collate {int}
001480  collate(C) ::= .              {C = 0;}
001481  collate(C) ::= COLLATE ids.   {C = 1;}
001482  
001483  
001484  ///////////////////////////// The DROP INDEX command /////////////////////////
001485  //
001486  cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
001487  
001488  ///////////////////////////// The VACUUM command /////////////////////////////
001489  //
001490  %if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH
001491  %type vinto {Expr*}
001492  %destructor vinto {sqlite3ExprDelete(pParse->db, $$);}
001493  cmd ::= VACUUM vinto(Y).                {sqlite3Vacuum(pParse,0,Y);}
001494  cmd ::= VACUUM nm(X) vinto(Y).          {sqlite3Vacuum(pParse,&X,Y);}
001495  vinto(A) ::= INTO expr(X).              {A = X;}
001496  vinto(A) ::= .                          {A = 0;}
001497  %endif
001498  
001499  ///////////////////////////// The PRAGMA command /////////////////////////////
001500  //
001501  %ifndef SQLITE_OMIT_PRAGMA
001502  cmd ::= PRAGMA nm(X) dbnm(Z).                {sqlite3Pragma(pParse,&X,&Z,0,0);}
001503  cmd ::= PRAGMA nm(X) dbnm(Z) EQ nmnum(Y).    {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
001504  cmd ::= PRAGMA nm(X) dbnm(Z) LP nmnum(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,0);}
001505  cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). 
001506                                               {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
001507  cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP.
001508                                               {sqlite3Pragma(pParse,&X,&Z,&Y,1);}
001509  
001510  nmnum(A) ::= plus_num(A).
001511  nmnum(A) ::= nm(A).
001512  nmnum(A) ::= ON(A).
001513  nmnum(A) ::= DELETE(A).
001514  nmnum(A) ::= DEFAULT(A).
001515  %endif SQLITE_OMIT_PRAGMA
001516  %token_class number INTEGER|FLOAT.
001517  plus_num(A) ::= PLUS number(X).       {A = X;}
001518  plus_num(A) ::= number(A).
001519  minus_num(A) ::= MINUS number(X).     {A = X;}
001520  //////////////////////////// The CREATE TRIGGER command /////////////////////
001521  
001522  %ifndef SQLITE_OMIT_TRIGGER
001523  
001524  cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
001525    Token all;
001526    all.z = A.z;
001527    all.n = (int)(Z.z - A.z) + Z.n;
001528    sqlite3FinishTrigger(pParse, S, &all);
001529  }
001530  
001531  trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) 
001532                      trigger_time(C) trigger_event(D)
001533                      ON fullname(E) foreach_clause when_clause(G). {
001534    sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR);
001535    A = (Z.n==0?B:Z); /*A-overwrites-T*/
001536  }
001537  
001538  %type trigger_time {int}
001539  trigger_time(A) ::= BEFORE|AFTER(X).  { A = @X; /*A-overwrites-X*/ }
001540  trigger_time(A) ::= INSTEAD OF.  { A = TK_INSTEAD;}
001541  trigger_time(A) ::= .            { A = TK_BEFORE; }
001542  
001543  %type trigger_event {struct TrigEvent}
001544  %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);}
001545  trigger_event(A) ::= DELETE|INSERT(X).   {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
001546  trigger_event(A) ::= UPDATE(X).          {A.a = @X; /*A-overwrites-X*/ A.b = 0;}
001547  trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;}
001548  
001549  foreach_clause ::= .
001550  foreach_clause ::= FOR EACH ROW.
001551  
001552  %type when_clause {Expr*}
001553  %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
001554  when_clause(A) ::= .             { A = 0; }
001555  when_clause(A) ::= WHEN expr(X). { A = X; }
001556  
001557  %type trigger_cmd_list {TriggerStep*}
001558  %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
001559  trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
001560    assert( A!=0 );
001561    A->pLast->pNext = X;
001562    A->pLast = X;
001563  }
001564  trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. { 
001565    assert( A!=0 );
001566    A->pLast = A;
001567  }
001568  
001569  // Disallow qualified table names on INSERT, UPDATE, and DELETE statements
001570  // within a trigger.  The table to INSERT, UPDATE, or DELETE is always in 
001571  // the same database as the table that the trigger fires on.
001572  //
001573  %type trnm {Token}
001574  trnm(A) ::= nm(A).
001575  trnm(A) ::= nm DOT nm(X). {
001576    A = X;
001577    sqlite3ErrorMsg(pParse, 
001578          "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
001579          "statements within triggers");
001580  }
001581  
001582  // Disallow the INDEX BY and NOT INDEXED clauses on UPDATE and DELETE
001583  // statements within triggers.  We make a specific error message for this
001584  // since it is an exception to the default grammar rules.
001585  //
001586  tridxby ::= .
001587  tridxby ::= INDEXED BY nm. {
001588    sqlite3ErrorMsg(pParse,
001589          "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
001590          "within triggers");
001591  }
001592  tridxby ::= NOT INDEXED. {
001593    sqlite3ErrorMsg(pParse,
001594          "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
001595          "within triggers");
001596  }
001597  
001598  
001599  
001600  %type trigger_cmd {TriggerStep*}
001601  %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
001602  // UPDATE 
001603  trigger_cmd(A) ::=
001604     UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E).  
001605     {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);}
001606  
001607  // INSERT
001608  trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
001609                        trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). {
001610     A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/
001611  }
001612  // DELETE
001613  trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
001614     {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);}
001615  
001616  // SELECT
001617  trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
001618     {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
001619  
001620  // The special RAISE expression that may occur in trigger programs
001621  expr(A) ::= RAISE LP IGNORE RP.  {
001622    A = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
001623    if( A ){
001624      A->affExpr = OE_Ignore;
001625    }
001626  }
001627  expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP.  {
001628    A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1);
001629    if( A ) {
001630      A->affExpr = (char)T;
001631    }
001632  }
001633  %endif  !SQLITE_OMIT_TRIGGER
001634  
001635  %type raisetype {int}
001636  raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
001637  raisetype(A) ::= ABORT.     {A = OE_Abort;}
001638  raisetype(A) ::= FAIL.      {A = OE_Fail;}
001639  
001640  
001641  ////////////////////////  DROP TRIGGER statement //////////////////////////////
001642  %ifndef SQLITE_OMIT_TRIGGER
001643  cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
001644    sqlite3DropTrigger(pParse,X,NOERR);
001645  }
001646  %endif  !SQLITE_OMIT_TRIGGER
001647  
001648  //////////////////////// ATTACH DATABASE file AS name /////////////////////////
001649  %ifndef SQLITE_OMIT_ATTACH
001650  cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
001651    sqlite3Attach(pParse, F, D, K);
001652  }
001653  cmd ::= DETACH database_kw_opt expr(D). {
001654    sqlite3Detach(pParse, D);
001655  }
001656  
001657  %type key_opt {Expr*}
001658  %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
001659  key_opt(A) ::= .                     { A = 0; }
001660  key_opt(A) ::= KEY expr(X).          { A = X; }
001661  
001662  database_kw_opt ::= DATABASE.
001663  database_kw_opt ::= .
001664  %endif SQLITE_OMIT_ATTACH
001665  
001666  ////////////////////////// REINDEX collation //////////////////////////////////
001667  %ifndef SQLITE_OMIT_REINDEX
001668  cmd ::= REINDEX.                {sqlite3Reindex(pParse, 0, 0);}
001669  cmd ::= REINDEX nm(X) dbnm(Y).  {sqlite3Reindex(pParse, &X, &Y);}
001670  %endif  SQLITE_OMIT_REINDEX
001671  
001672  /////////////////////////////////// ANALYZE ///////////////////////////////////
001673  %ifndef SQLITE_OMIT_ANALYZE
001674  cmd ::= ANALYZE.                {sqlite3Analyze(pParse, 0, 0);}
001675  cmd ::= ANALYZE nm(X) dbnm(Y).  {sqlite3Analyze(pParse, &X, &Y);}
001676  %endif
001677  
001678  //////////////////////// ALTER TABLE table ... ////////////////////////////////
001679  %ifndef SQLITE_OMIT_ALTERTABLE 
001680  %ifndef SQLITE_OMIT_VIRTUALTABLE
001681  cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). {
001682    sqlite3AlterRenameTable(pParse,X,&Z);
001683  }
001684  cmd ::= ALTER TABLE add_column_fullname
001685          ADD kwcolumn_opt columnname(Y) carglist. {
001686    Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n;
001687    sqlite3AlterFinishAddColumn(pParse, &Y);
001688  }
001689  cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). {
001690    sqlite3AlterDropColumn(pParse, X, &Y);
001691  }
001692  
001693  add_column_fullname ::= fullname(X). {
001694    disableLookaside(pParse);
001695    sqlite3AlterBeginAddColumn(pParse, X);
001696  }
001697  cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). {
001698    sqlite3AlterRenameColumn(pParse, X, &Y, &Z);
001699  }
001700  
001701  kwcolumn_opt ::= .
001702  kwcolumn_opt ::= COLUMNKW.
001703  
001704  %endif SQLITE_OMIT_VIRTUALTABLE
001705  %endif SQLITE_OMIT_ALTERTABLE
001706  
001707  //////////////////////// CREATE VIRTUAL TABLE ... /////////////////////////////
001708  %ifndef SQLITE_OMIT_VIRTUALTABLE
001709  cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
001710  cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
001711  create_vtab ::= createkw VIRTUAL TABLE ifnotexists(E)
001712                  nm(X) dbnm(Y) USING nm(Z). {
001713      sqlite3VtabBeginParse(pParse, &X, &Y, &Z, E);
001714  }
001715  vtabarglist ::= vtabarg.
001716  vtabarglist ::= vtabarglist COMMA vtabarg.
001717  vtabarg ::= .                       {sqlite3VtabArgInit(pParse);}
001718  vtabarg ::= vtabarg vtabargtoken.
001719  vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
001720  vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
001721  lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
001722  anylist ::= .
001723  anylist ::= anylist LP anylist RP.
001724  anylist ::= anylist ANY.
001725  %endif  SQLITE_OMIT_VIRTUALTABLE
001726  
001727  
001728  //////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
001729  %type wqlist {With*}
001730  %destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
001731  %type wqitem {Cte*}
001732  // %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable
001733  
001734  with ::= .
001735  %ifndef SQLITE_OMIT_CTE
001736  with ::= WITH wqlist(W).              { sqlite3WithPush(pParse, W, 1); }
001737  with ::= WITH RECURSIVE wqlist(W).    { sqlite3WithPush(pParse, W, 1); }
001738  
001739  %type wqas {u8}
001740  wqas(A)   ::= AS.                  {A = M10d_Any;}
001741  wqas(A)   ::= AS MATERIALIZED.     {A = M10d_Yes;}
001742  wqas(A)   ::= AS NOT MATERIALIZED. {A = M10d_No;}
001743  wqitem(A) ::= nm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. {
001744    A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/
001745  }
001746  wqlist(A) ::= wqitem(X). {
001747    A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/
001748  }
001749  wqlist(A) ::= wqlist(A) COMMA wqitem(X). {
001750    A = sqlite3WithAdd(pParse, A, X);
001751  }
001752  %endif  SQLITE_OMIT_CTE
001753  
001754  //////////////////////// WINDOW FUNCTION EXPRESSIONS /////////////////////////
001755  // These must be at the end of this file. Specifically, the rules that
001756  // introduce tokens WINDOW, OVER and FILTER must appear last. This causes 
001757  // the integer values assigned to these tokens to be larger than all other 
001758  // tokens that may be output by the tokenizer except TK_SPACE and TK_ILLEGAL.
001759  //
001760  %ifndef SQLITE_OMIT_WINDOWFUNC
001761  %type windowdefn_list {Window*}
001762  %destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);}
001763  windowdefn_list(A) ::= windowdefn(A).
001764  windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). {
001765    assert( Z!=0 );
001766    sqlite3WindowChain(pParse, Z, Y);
001767    Z->pNextWin = Y;
001768    A = Z;
001769  }
001770  
001771  %type windowdefn {Window*}
001772  %destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);}
001773  windowdefn(A) ::= nm(X) AS LP window(Y) RP. {
001774    if( ALWAYS(Y) ){
001775      Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n);
001776    }
001777    A = Y;
001778  }
001779  
001780  %type window {Window*}
001781  %destructor window {sqlite3WindowDelete(pParse->db, $$);}
001782  
001783  %type frame_opt {Window*}
001784  %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
001785  
001786  %type part_opt {ExprList*}
001787  %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
001788  
001789  %type filter_clause {Expr*}
001790  %destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
001791  
001792  %type over_clause {Window*}
001793  %destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
001794  
001795  %type filter_over {Window*}
001796  %destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
001797  
001798  %type range_or_rows {int}
001799  
001800  %type frame_bound {struct FrameBound}
001801  %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
001802  %type frame_bound_s {struct FrameBound}
001803  %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
001804  %type frame_bound_e {struct FrameBound}
001805  %destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);}
001806  
001807  window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
001808    A = sqlite3WindowAssemble(pParse, Z, X, Y, 0);
001809  }
001810  window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). {
001811    A = sqlite3WindowAssemble(pParse, Z, X, Y, &W);
001812  }
001813  window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). {
001814    A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0);
001815  }
001816  window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). {
001817    A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W);
001818  }
001819  window(A) ::= frame_opt(A).
001820  window(A) ::= nm(W) frame_opt(Z). {
001821    A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W);
001822  }
001823  
001824  frame_opt(A) ::= .                             { 
001825    A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
001826  }
001827  frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). { 
001828    A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z);
001829  }
001830  frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND
001831                            frame_bound_e(Z) frame_exclude_opt(W). { 
001832    A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W);
001833  }
001834  
001835  range_or_rows(A) ::= RANGE|ROWS|GROUPS(X).   {A = @X; /*A-overwrites-X*/}
001836  
001837  frame_bound_s(A) ::= frame_bound(X).         {A = X;}
001838  frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;}
001839  frame_bound_e(A) ::= frame_bound(X).         {A = X;}
001840  frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;}
001841  
001842  frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y).
001843                                               {A.eType = @Y; A.pExpr = X;}
001844  frame_bound(A) ::= CURRENT(X) ROW.           {A.eType = @X; A.pExpr = 0;}
001845  
001846  %type frame_exclude_opt {u8}
001847  frame_exclude_opt(A) ::= . {A = 0;}
001848  frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;}
001849  
001850  %type frame_exclude {u8}
001851  frame_exclude(A) ::= NO(X) OTHERS.   {A = @X; /*A-overwrites-X*/}
001852  frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/}
001853  frame_exclude(A) ::= GROUP|TIES(X).  {A = @X; /*A-overwrites-X*/}
001854  
001855  
001856  %type window_clause {Window*}
001857  %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
001858  window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
001859  
001860  filter_over(A) ::= filter_clause(F) over_clause(O). {
001861    if( O ){
001862      O->pFilter = F;
001863    }else{
001864      sqlite3ExprDelete(pParse->db, F);
001865    }
001866    A = O;
001867  }
001868  filter_over(A) ::= over_clause(O). {
001869    A = O;
001870  }
001871  filter_over(A) ::= filter_clause(F). {
001872    A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
001873    if( A ){
001874      A->eFrmType = TK_FILTER;
001875      A->pFilter = F;
001876    }else{
001877      sqlite3ExprDelete(pParse->db, F);
001878    }
001879  }
001880  
001881  over_clause(A) ::= OVER LP window(Z) RP. {
001882    A = Z;
001883    assert( A!=0 );
001884  }
001885  over_clause(A) ::= OVER nm(Z). {
001886    A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
001887    if( A ){
001888      A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
001889    }
001890  }
001891  
001892  filter_clause(A) ::= FILTER LP WHERE expr(X) RP.  { A = X; }
001893  %endif /* SQLITE_OMIT_WINDOWFUNC */
001894  
001895  /*
001896  ** The code generator needs some extra TK_ token values for tokens that
001897  ** are synthesized and do not actually appear in the grammar:
001898  */
001899  %token
001900    COLUMN          /* Reference to a table column */
001901    AGG_FUNCTION    /* An aggregate function */
001902    AGG_COLUMN      /* An aggregated column */
001903    TRUEFALSE       /* True or false keyword */
001904    ISNOT           /* Combination of IS and NOT */
001905    FUNCTION        /* A function invocation */
001906    UMINUS          /* Unary minus */
001907    UPLUS           /* Unary plus */
001908    TRUTH           /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
001909    REGISTER        /* Reference to a VDBE register */
001910    VECTOR          /* Vector */
001911    SELECT_COLUMN   /* Choose a single column from a multi-column SELECT */
001912    IF_NULL_ROW     /* the if-null-row operator */
001913    ASTERISK        /* The "*" in count(*) and similar */
001914    SPAN            /* The span operator */
001915    ERROR           /* An expression containing an error */
001916  .
001917  /* There must be no more than 255 tokens defined above.  If this grammar
001918  ** is extended with new rules and tokens, they must either be so few in
001919  ** number that TK_SPAN is no more than 255, or else the new tokens must
001920  ** appear after this line.
001921  */
001922  %include {
001923  #if TK_SPAN>255
001924  # error too many tokens in the grammar
001925  #endif
001926  }
001927  
001928  /*
001929  ** The TK_SPACE and TK_ILLEGAL tokens must be the last two tokens.  The
001930  ** parser depends on this.  Those tokens are not used in any grammar rule.
001931  ** They are only used by the tokenizer.  Declare them last so that they
001932  ** are guaranteed to be the last two tokens
001933  */
001934  %token SPACE ILLEGAL.