SQLite Android Bindings
Check-in [e77aed0138]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Update this project to SQLite version 3.28.0.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e77aed0138cb4d38bae6b1dc5e97dc014ed6321d
User & Date: dan 2019-04-17 11:26:16
Context
2019-07-10
19:39
Update this project to version 3.29.0. check-in: 28c307dbdb user: dan tags: trunk
2019-04-17
11:26
Update this project to SQLite version 3.28.0. check-in: e77aed0138 user: dan tags: trunk
2019-02-26
13:54
Upgrade this project to 3.27.2. check-in: b649c3c208 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to sqlite3/src/main/jni/sqlite/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.27.2.  By combining all the individual C code files into this
            3  +** version 3.28.0.  By combining all the individual C code files into this
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
  1158   1158   ** been edited in any way since it was last checked in, then the last
  1159   1159   ** four hexadecimal digits of the hash may be modified.
  1160   1160   **
  1161   1161   ** See also: [sqlite3_libversion()],
  1162   1162   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1163   1163   ** [sqlite_version()] and [sqlite_source_id()].
  1164   1164   */
  1165         -#define SQLITE_VERSION        "3.27.2"
  1166         -#define SQLITE_VERSION_NUMBER 3027002
  1167         -#define SQLITE_SOURCE_ID      "2019-02-25 16:06:06 bd49a8271d650fa89e446b42e513b595a717b9212c91dd384aab871fc1d0f6d7"
         1165  +#define SQLITE_VERSION        "3.28.0"
         1166  +#define SQLITE_VERSION_NUMBER 3028000
         1167  +#define SQLITE_SOURCE_ID      "2019-04-16 19:49:53 884b4b7e502b4e991677b53971277adfaf0a04a284f8e483e2553d0f83156b50"
  1168   1168   
  1169   1169   /*
  1170   1170   ** CAPI3REF: Run-Time Library Version Numbers
  1171   1171   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1172   1172   **
  1173   1173   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1174   1174   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1224   1224   **
  1225   1225   ** See also: SQL functions [sqlite_compileoption_used()] and
  1226   1226   ** [sqlite_compileoption_get()] and the [compile_options pragma].
  1227   1227   */
  1228   1228   #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  1229   1229   SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
  1230   1230   SQLITE_API const char *sqlite3_compileoption_get(int N);
         1231  +#else
         1232  +# define sqlite3_compileoption_used(X) 0
         1233  +# define sqlite3_compileoption_get(X)  ((void*)0)
  1231   1234   #endif
  1232   1235   
  1233   1236   /*
  1234   1237   ** CAPI3REF: Test To See If The Library Is Threadsafe
  1235   1238   **
  1236   1239   ** ^The sqlite3_threadsafe() function returns zero if and only if
  1237   1240   ** SQLite was compiled with mutexing code omitted due to the
................................................................................
  3121   3124   ** The second parameter is a pointer to an integer into which
  3122   3125   ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  3123   3126   ** following this call.  The second parameter may be a NULL pointer, in
  3124   3127   ** which case the trigger setting is not reported back. </dd>
  3125   3128   **
  3126   3129   ** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
  3127   3130   ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
  3128         -** <dd> ^This option is used to enable or disable the two-argument
  3129         -** version of the [fts3_tokenizer()] function which is part of the
         3131  +** <dd> ^This option is used to enable or disable the
         3132  +** [fts3_tokenizer()] function which is part of the
  3130   3133   ** [FTS3] full-text search engine extension.
  3131   3134   ** There should be two additional arguments.
  3132   3135   ** The first argument is an integer which is 0 to disable fts3_tokenizer() or
  3133   3136   ** positive to enable fts3_tokenizer() or negative to leave the setting
  3134   3137   ** unchanged.
  3135   3138   ** The second parameter is a pointer to an integer into which
  3136   3139   ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
................................................................................
  3234   3237   ** features include but are not limited to the following:
  3235   3238   ** <ul>
  3236   3239   ** <li> The [PRAGMA writable_schema=ON] statement.
  3237   3240   ** <li> Writes to the [sqlite_dbpage] virtual table.
  3238   3241   ** <li> Direct writes to [shadow tables].
  3239   3242   ** </ul>
  3240   3243   ** </dd>
         3244  +**
         3245  +** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
         3246  +** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
         3247  +** "writable_schema" flag. This has the same effect and is logically equivalent
         3248  +** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
         3249  +** The first argument to this setting is an integer which is 0 to disable 
         3250  +** the writable_schema, positive to enable writable_schema, or negative to
         3251  +** leave the setting unchanged. The second parameter is a pointer to an
         3252  +** integer into which is written 0 or 1 to indicate whether the writable_schema
         3253  +** is enabled or disabled following this call.
         3254  +** </dd>
  3241   3255   ** </dl>
  3242   3256   */
  3243   3257   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  3244   3258   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  3245   3259   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  3246   3260   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  3247   3261   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  3248   3262   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  3249   3263   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  3250   3264   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  3251   3265   #define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
  3252   3266   #define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
  3253   3267   #define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
  3254         -#define SQLITE_DBCONFIG_MAX                   1010 /* Largest DBCONFIG */
         3268  +#define SQLITE_DBCONFIG_WRITABLE_SCHEMA       1011 /* int int* */
         3269  +#define SQLITE_DBCONFIG_MAX                   1011 /* Largest DBCONFIG */
  3255   3270   
  3256   3271   /*
  3257   3272   ** CAPI3REF: Enable Or Disable Extended Result Codes
  3258   3273   ** METHOD: sqlite3
  3259   3274   **
  3260   3275   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  3261   3276   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  4929   4944   ** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
  4930   4945   ** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
  4931   4946   ** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
  4932   4947   ** sqlite3_stmt_readonly() returns false for those commands.
  4933   4948   */
  4934   4949   SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  4935   4950   
         4951  +/*
         4952  +** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
         4953  +** METHOD: sqlite3_stmt
         4954  +**
         4955  +** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
         4956  +** prepared statement S is an EXPLAIN statement, or 2 if the
         4957  +** statement S is an EXPLAIN QUERY PLAN.
         4958  +** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
         4959  +** an ordinary statement or a NULL pointer.
         4960  +*/
         4961  +SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
         4962  +
  4936   4963   /*
  4937   4964   ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
  4938   4965   ** METHOD: sqlite3_stmt
  4939   4966   **
  4940   4967   ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
  4941   4968   ** [prepared statement] S has been stepped at least once using 
  4942   4969   ** [sqlite3_step(S)] but has neither run to completion (returned
................................................................................
  5068   5095   ** the value of the fourth parameter then the resulting string value will
  5069   5096   ** contain embedded NULs.  The result of expressions involving strings
  5070   5097   ** with embedded NULs is undefined.
  5071   5098   **
  5072   5099   ** ^The fifth argument to the BLOB and string binding interfaces
  5073   5100   ** is a destructor used to dispose of the BLOB or
  5074   5101   ** string after SQLite has finished with it.  ^The destructor is called
  5075         -** to dispose of the BLOB or string even if the call to bind API fails.
         5102  +** to dispose of the BLOB or string even if the call to the bind API fails,
         5103  +** except the destructor is not called if the third parameter is a NULL
         5104  +** pointer or the fourth parameter is negative.
  5076   5105   ** ^If the fifth argument is
  5077   5106   ** the special value [SQLITE_STATIC], then SQLite assumes that the
  5078   5107   ** information is in static, unmanaged space and does not need to be freed.
  5079   5108   ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  5080   5109   ** SQLite makes its own private copy of the data immediately, before
  5081   5110   ** the sqlite3_bind_*() routine returns.
  5082   5111   **
................................................................................
  5985   6014   ** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
  5986   6015   ** datatype of the value
  5987   6016   ** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
  5988   6017   ** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
  5989   6018   ** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
  5990   6019   ** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
  5991   6020   ** against a virtual table.
         6021  +** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
         6022  +** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
  5992   6023   ** </table></blockquote>
  5993   6024   **
  5994   6025   ** <b>Details:</b>
  5995   6026   **
  5996   6027   ** These routines extract type, size, and content information from
  5997   6028   ** [protected sqlite3_value] objects.  Protected sqlite3_value objects
  5998   6029   ** are used to pass parameter information into implementation of
................................................................................
  6045   6076   ** the value for that column returned without setting a result (probably
  6046   6077   ** because it queried [sqlite3_vtab_nochange()] and found that the column
  6047   6078   ** was unchanging).  ^Within an [xUpdate] method, any value for which
  6048   6079   ** sqlite3_value_nochange(X) is true will in all other respects appear
  6049   6080   ** to be a NULL value.  If sqlite3_value_nochange(X) is invoked anywhere other
  6050   6081   ** than within an [xUpdate] method call for an UPDATE statement, then
  6051   6082   ** the return value is arbitrary and meaningless.
         6083  +**
         6084  +** ^The sqlite3_value_frombind(X) interface returns non-zero if the
         6085  +** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
         6086  +** interfaces.  ^If X comes from an SQL literal value, or a table column,
         6087  +** and expression, then sqlite3_value_frombind(X) returns zero.
  6052   6088   **
  6053   6089   ** Please pay particular attention to the fact that the pointer returned
  6054   6090   ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
  6055   6091   ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
  6056   6092   ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
  6057   6093   ** or [sqlite3_value_text16()].
  6058   6094   **
................................................................................
  6091   6127   SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
  6092   6128   SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
  6093   6129   SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
  6094   6130   SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
  6095   6131   SQLITE_API int sqlite3_value_type(sqlite3_value*);
  6096   6132   SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
  6097   6133   SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
         6134  +SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
  6098   6135   
  6099   6136   /*
  6100   6137   ** CAPI3REF: Finding The Subtype Of SQL Values
  6101   6138   ** METHOD: sqlite3_value
  6102   6139   **
  6103   6140   ** The sqlite3_value_subtype(V) function returns the subtype for
  6104   6141   ** an [application-defined SQL function] argument V.  The subtype
................................................................................
  6826   6863   ** CAPI3REF: Return The Filename For A Database Connection
  6827   6864   ** METHOD: sqlite3
  6828   6865   **
  6829   6866   ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  6830   6867   ** associated with database N of connection D.  ^The main database file
  6831   6868   ** has the name "main".  If there is no attached database N on the database
  6832   6869   ** connection D, or if database N is a temporary or in-memory database, then
  6833         -** a NULL pointer is returned.
         6870  +** this function will return either a NULL pointer or an empty string.
  6834   6871   **
  6835   6872   ** ^The filename returned by this function is the output of the
  6836   6873   ** xFullPathname method of the [VFS].  ^In other words, the filename
  6837   6874   ** will be an absolute pathname, even if the filename used
  6838   6875   ** to open the database originally was a URI or relative pathname.
  6839   6876   */
  6840   6877   SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
................................................................................
 11927  11964   ** CAPI3REF: Rebase a changeset
 11928  11965   ** EXPERIMENTAL
 11929  11966   **
 11930  11967   ** Argument pIn must point to a buffer containing a changeset nIn bytes
 11931  11968   ** in size. This function allocates and populates a buffer with a copy
 11932  11969   ** of the changeset rebased rebased according to the configuration of the
 11933  11970   ** rebaser object passed as the first argument. If successful, (*ppOut)
 11934         -** is set to point to the new buffer containing the rebased changset and 
        11971  +** is set to point to the new buffer containing the rebased changeset and 
 11935  11972   ** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
 11936  11973   ** responsibility of the caller to eventually free the new buffer using
 11937  11974   ** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
 11938  11975   ** are set to zero and an SQLite error code returned.
 11939  11976   */
 11940  11977   SQLITE_API int sqlite3rebaser_rebase(
 11941  11978     sqlite3_rebaser*,
................................................................................
 12336  12373   **
 12337  12374   **
 12338  12375   ** xSetAuxdata(pFts5, pAux, xDelete)
 12339  12376   **
 12340  12377   **   Save the pointer passed as the second argument as the extension functions 
 12341  12378   **   "auxiliary data". The pointer may then be retrieved by the current or any
 12342  12379   **   future invocation of the same fts5 extension function made as part of
 12343         -**   of the same MATCH query using the xGetAuxdata() API.
        12380  +**   the same MATCH query using the xGetAuxdata() API.
 12344  12381   **
 12345  12382   **   Each extension function is allocated a single auxiliary data slot for
 12346  12383   **   each FTS query (MATCH expression). If the extension function is invoked 
 12347  12384   **   more than once for a single FTS query, then all invocations share a 
 12348  12385   **   single auxiliary data context.
 12349  12386   **
 12350  12387   **   If there is already an auxiliary data pointer when this function is
................................................................................
 12351  12388   **   invoked, then it is replaced by the new pointer. If an xDelete callback
 12352  12389   **   was specified along with the original pointer, it is invoked at this
 12353  12390   **   point.
 12354  12391   **
 12355  12392   **   The xDelete callback, if one is specified, is also invoked on the
 12356  12393   **   auxiliary data pointer after the FTS5 query has finished.
 12357  12394   **
 12358         -**   If an error (e.g. an OOM condition) occurs within this function, an
        12395  +**   If an error (e.g. an OOM condition) occurs within this function,
 12359  12396   **   the auxiliary data is set to NULL and an error code returned. If the
 12360  12397   **   xDelete parameter was not NULL, it is invoked on the auxiliary data
 12361  12398   **   pointer before returning.
 12362  12399   **
 12363  12400   **
 12364  12401   ** xGetAuxdata(pFts5, bClear)
 12365  12402   **
................................................................................
 13377  13414   ** the hash table.
 13378  13415   */
 13379  13416   struct Hash {
 13380  13417     unsigned int htsize;      /* Number of buckets in the hash table */
 13381  13418     unsigned int count;       /* Number of entries in this table */
 13382  13419     HashElem *first;          /* The first element of the array */
 13383  13420     struct _ht {              /* the hash table */
 13384         -    int count;                 /* Number of entries with this hash */
        13421  +    unsigned int count;        /* Number of entries with this hash */
 13385  13422       HashElem *chain;           /* Pointer to first entry with this hash */
 13386  13423     } *ht;
 13387  13424   };
 13388  13425   
 13389  13426   /* Each element in the hash table is an instance of the following 
 13390  13427   ** structure.  All elements are stored on a single doubly-linked list.
 13391  13428   **
................................................................................
 13518  13555   #define TK_WITH                            81
 13519  13556   #define TK_CURRENT                         82
 13520  13557   #define TK_FOLLOWING                       83
 13521  13558   #define TK_PARTITION                       84
 13522  13559   #define TK_PRECEDING                       85
 13523  13560   #define TK_RANGE                           86
 13524  13561   #define TK_UNBOUNDED                       87
 13525         -#define TK_REINDEX                         88
 13526         -#define TK_RENAME                          89
 13527         -#define TK_CTIME_KW                        90
 13528         -#define TK_ANY                             91
 13529         -#define TK_BITAND                          92
 13530         -#define TK_BITOR                           93
 13531         -#define TK_LSHIFT                          94
 13532         -#define TK_RSHIFT                          95
 13533         -#define TK_PLUS                            96
 13534         -#define TK_MINUS                           97
 13535         -#define TK_STAR                            98
 13536         -#define TK_SLASH                           99
 13537         -#define TK_REM                            100
 13538         -#define TK_CONCAT                         101
 13539         -#define TK_COLLATE                        102
 13540         -#define TK_BITNOT                         103
 13541         -#define TK_ON                             104
 13542         -#define TK_INDEXED                        105
 13543         -#define TK_STRING                         106
 13544         -#define TK_JOIN_KW                        107
 13545         -#define TK_CONSTRAINT                     108
 13546         -#define TK_DEFAULT                        109
 13547         -#define TK_NULL                           110
 13548         -#define TK_PRIMARY                        111
 13549         -#define TK_UNIQUE                         112
 13550         -#define TK_CHECK                          113
 13551         -#define TK_REFERENCES                     114
 13552         -#define TK_AUTOINCR                       115
 13553         -#define TK_INSERT                         116
 13554         -#define TK_DELETE                         117
 13555         -#define TK_UPDATE                         118
 13556         -#define TK_SET                            119
 13557         -#define TK_DEFERRABLE                     120
 13558         -#define TK_FOREIGN                        121
 13559         -#define TK_DROP                           122
 13560         -#define TK_UNION                          123
 13561         -#define TK_ALL                            124
 13562         -#define TK_EXCEPT                         125
 13563         -#define TK_INTERSECT                      126
 13564         -#define TK_SELECT                         127
 13565         -#define TK_VALUES                         128
 13566         -#define TK_DISTINCT                       129
 13567         -#define TK_DOT                            130
 13568         -#define TK_FROM                           131
 13569         -#define TK_JOIN                           132
 13570         -#define TK_USING                          133
 13571         -#define TK_ORDER                          134
 13572         -#define TK_GROUP                          135
 13573         -#define TK_HAVING                         136
 13574         -#define TK_LIMIT                          137
 13575         -#define TK_WHERE                          138
 13576         -#define TK_INTO                           139
 13577         -#define TK_NOTHING                        140
 13578         -#define TK_FLOAT                          141
 13579         -#define TK_BLOB                           142
 13580         -#define TK_INTEGER                        143
 13581         -#define TK_VARIABLE                       144
 13582         -#define TK_CASE                           145
 13583         -#define TK_WHEN                           146
 13584         -#define TK_THEN                           147
 13585         -#define TK_ELSE                           148
 13586         -#define TK_INDEX                          149
 13587         -#define TK_ALTER                          150
 13588         -#define TK_ADD                            151
 13589         -#define TK_WINDOW                         152
 13590         -#define TK_OVER                           153
 13591         -#define TK_FILTER                         154
 13592         -#define TK_TRUEFALSE                      155
 13593         -#define TK_ISNOT                          156
 13594         -#define TK_FUNCTION                       157
 13595         -#define TK_COLUMN                         158
 13596         -#define TK_AGG_FUNCTION                   159
 13597         -#define TK_AGG_COLUMN                     160
 13598         -#define TK_UMINUS                         161
 13599         -#define TK_UPLUS                          162
 13600         -#define TK_TRUTH                          163
 13601         -#define TK_REGISTER                       164
 13602         -#define TK_VECTOR                         165
 13603         -#define TK_SELECT_COLUMN                  166
 13604         -#define TK_IF_NULL_ROW                    167
 13605         -#define TK_ASTERISK                       168
 13606         -#define TK_SPAN                           169
 13607         -#define TK_END_OF_FILE                    170
 13608         -#define TK_UNCLOSED_STRING                171
 13609         -#define TK_SPACE                          172
 13610         -#define TK_ILLEGAL                        173
 13611         -
 13612         -/* The token codes above must all fit in 8 bits */
 13613         -#define TKFLG_MASK           0xff  
 13614         -
 13615         -/* Flags that can be added to a token code when it is not
 13616         -** being stored in a u8: */
 13617         -#define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
        13562  +#define TK_EXCLUDE                         88
        13563  +#define TK_GROUPS                          89
        13564  +#define TK_OTHERS                          90
        13565  +#define TK_TIES                            91
        13566  +#define TK_REINDEX                         92
        13567  +#define TK_RENAME                          93
        13568  +#define TK_CTIME_KW                        94
        13569  +#define TK_ANY                             95
        13570  +#define TK_BITAND                          96
        13571  +#define TK_BITOR                           97
        13572  +#define TK_LSHIFT                          98
        13573  +#define TK_RSHIFT                          99
        13574  +#define TK_PLUS                           100
        13575  +#define TK_MINUS                          101
        13576  +#define TK_STAR                           102
        13577  +#define TK_SLASH                          103
        13578  +#define TK_REM                            104
        13579  +#define TK_CONCAT                         105
        13580  +#define TK_COLLATE                        106
        13581  +#define TK_BITNOT                         107
        13582  +#define TK_ON                             108
        13583  +#define TK_INDEXED                        109
        13584  +#define TK_STRING                         110
        13585  +#define TK_JOIN_KW                        111
        13586  +#define TK_CONSTRAINT                     112
        13587  +#define TK_DEFAULT                        113
        13588  +#define TK_NULL                           114
        13589  +#define TK_PRIMARY                        115
        13590  +#define TK_UNIQUE                         116
        13591  +#define TK_CHECK                          117
        13592  +#define TK_REFERENCES                     118
        13593  +#define TK_AUTOINCR                       119
        13594  +#define TK_INSERT                         120
        13595  +#define TK_DELETE                         121
        13596  +#define TK_UPDATE                         122
        13597  +#define TK_SET                            123
        13598  +#define TK_DEFERRABLE                     124
        13599  +#define TK_FOREIGN                        125
        13600  +#define TK_DROP                           126
        13601  +#define TK_UNION                          127
        13602  +#define TK_ALL                            128
        13603  +#define TK_EXCEPT                         129
        13604  +#define TK_INTERSECT                      130
        13605  +#define TK_SELECT                         131
        13606  +#define TK_VALUES                         132
        13607  +#define TK_DISTINCT                       133
        13608  +#define TK_DOT                            134
        13609  +#define TK_FROM                           135
        13610  +#define TK_JOIN                           136
        13611  +#define TK_USING                          137
        13612  +#define TK_ORDER                          138
        13613  +#define TK_GROUP                          139
        13614  +#define TK_HAVING                         140
        13615  +#define TK_LIMIT                          141
        13616  +#define TK_WHERE                          142
        13617  +#define TK_INTO                           143
        13618  +#define TK_NOTHING                        144
        13619  +#define TK_FLOAT                          145
        13620  +#define TK_BLOB                           146
        13621  +#define TK_INTEGER                        147
        13622  +#define TK_VARIABLE                       148
        13623  +#define TK_CASE                           149
        13624  +#define TK_WHEN                           150
        13625  +#define TK_THEN                           151
        13626  +#define TK_ELSE                           152
        13627  +#define TK_INDEX                          153
        13628  +#define TK_ALTER                          154
        13629  +#define TK_ADD                            155
        13630  +#define TK_WINDOW                         156
        13631  +#define TK_OVER                           157
        13632  +#define TK_FILTER                         158
        13633  +#define TK_TRUEFALSE                      159
        13634  +#define TK_ISNOT                          160
        13635  +#define TK_FUNCTION                       161
        13636  +#define TK_COLUMN                         162
        13637  +#define TK_AGG_FUNCTION                   163
        13638  +#define TK_AGG_COLUMN                     164
        13639  +#define TK_UMINUS                         165
        13640  +#define TK_UPLUS                          166
        13641  +#define TK_TRUTH                          167
        13642  +#define TK_REGISTER                       168
        13643  +#define TK_VECTOR                         169
        13644  +#define TK_SELECT_COLUMN                  170
        13645  +#define TK_IF_NULL_ROW                    171
        13646  +#define TK_ASTERISK                       172
        13647  +#define TK_SPAN                           173
        13648  +#define TK_SPACE                          174
        13649  +#define TK_ILLEGAL                        175
 13618  13650   
 13619  13651   /************** End of parse.h ***********************************************/
 13620  13652   /************** Continuing where we left off in sqliteInt.h ******************/
 13621  13653   #include <stdio.h>
 13622  13654   #include <stdlib.h>
 13623  13655   #include <string.h>
 13624  13656   #include <assert.h>
................................................................................
 14542  14574     int nData;              /* Size of pData.  0 if none. */
 14543  14575     int nZero;              /* Extra zero data appended after pData,nData */
 14544  14576   };
 14545  14577   
 14546  14578   SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
 14547  14579                          int flags, int seekResult);
 14548  14580   SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
 14549         -#ifndef SQLITE_OMIT_WINDOWFUNC
 14550         -SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor*);
 14551         -#endif
 14552  14581   SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
 14553  14582   SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
 14554  14583   SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
 14555  14584   SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
 14556  14585   SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
 14557  14586   #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
 14558  14587   SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
................................................................................
 14902  14931   #define OP_Cast           85 /* synopsis: affinity(r[P1])                  */
 14903  14932   #define OP_Permutation    86
 14904  14933   #define OP_Compare        87 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 14905  14934   #define OP_IsTrue         88 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
 14906  14935   #define OP_Offset         89 /* synopsis: r[P3] = sqlite_offset(P1)        */
 14907  14936   #define OP_Column         90 /* synopsis: r[P3]=PX                         */
 14908  14937   #define OP_Affinity       91 /* synopsis: affinity(r[P1@P2])               */
 14909         -#define OP_BitAnd         92 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
 14910         -#define OP_BitOr          93 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
 14911         -#define OP_ShiftLeft      94 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
 14912         -#define OP_ShiftRight     95 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
 14913         -#define OP_Add            96 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
 14914         -#define OP_Subtract       97 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 14915         -#define OP_Multiply       98 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 14916         -#define OP_Divide         99 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 14917         -#define OP_Remainder     100 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 14918         -#define OP_Concat        101 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 14919         -#define OP_MakeRecord    102 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 14920         -#define OP_BitNot        103 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
 14921         -#define OP_Count         104 /* synopsis: r[P2]=count()                    */
 14922         -#define OP_ReadCookie    105
 14923         -#define OP_String8       106 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 14924         -#define OP_SetCookie     107
 14925         -#define OP_ReopenIdx     108 /* synopsis: root=P2 iDb=P3                   */
 14926         -#define OP_OpenRead      109 /* synopsis: root=P2 iDb=P3                   */
 14927         -#define OP_OpenWrite     110 /* synopsis: root=P2 iDb=P3                   */
        14938  +#define OP_MakeRecord     92 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        14939  +#define OP_Count          93 /* synopsis: r[P2]=count()                    */
        14940  +#define OP_ReadCookie     94
        14941  +#define OP_SetCookie      95
        14942  +#define OP_BitAnd         96 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
        14943  +#define OP_BitOr          97 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
        14944  +#define OP_ShiftLeft      98 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
        14945  +#define OP_ShiftRight     99 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
        14946  +#define OP_Add           100 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
        14947  +#define OP_Subtract      101 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
        14948  +#define OP_Multiply      102 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
        14949  +#define OP_Divide        103 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
        14950  +#define OP_Remainder     104 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
        14951  +#define OP_Concat        105 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
        14952  +#define OP_ReopenIdx     106 /* synopsis: root=P2 iDb=P3                   */
        14953  +#define OP_BitNot        107 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
        14954  +#define OP_OpenRead      108 /* synopsis: root=P2 iDb=P3                   */
        14955  +#define OP_OpenWrite     109 /* synopsis: root=P2 iDb=P3                   */
        14956  +#define OP_String8       110 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 14928  14957   #define OP_OpenDup       111
 14929  14958   #define OP_OpenAutoindex 112 /* synopsis: nColumn=P2                       */
 14930  14959   #define OP_OpenEphemeral 113 /* synopsis: nColumn=P2                       */
 14931  14960   #define OP_SorterOpen    114
 14932  14961   #define OP_SequenceTest  115 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 14933  14962   #define OP_OpenPseudo    116 /* synopsis: P3 columns in r[P2]              */
 14934  14963   #define OP_Close         117
................................................................................
 14951  14980   #define OP_DeferredSeek  134 /* synopsis: Move P3 to P1.rowid if needed    */
 14952  14981   #define OP_IdxRowid      135 /* synopsis: r[P2]=rowid                      */
 14953  14982   #define OP_Destroy       136
 14954  14983   #define OP_Clear         137
 14955  14984   #define OP_ResetSorter   138
 14956  14985   #define OP_CreateBtree   139 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 14957  14986   #define OP_SqlExec       140
 14958         -#define OP_Real          141 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 14959         -#define OP_ParseSchema   142
 14960         -#define OP_LoadAnalysis  143
 14961         -#define OP_DropTable     144
 14962         -#define OP_DropIndex     145
        14987  +#define OP_ParseSchema   141
        14988  +#define OP_LoadAnalysis  142
        14989  +#define OP_DropTable     143
        14990  +#define OP_DropIndex     144
        14991  +#define OP_Real          145 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 14963  14992   #define OP_DropTrigger   146
 14964  14993   #define OP_IntegrityCk   147
 14965  14994   #define OP_RowSetAdd     148 /* synopsis: rowset(P1)=r[P2]                 */
 14966  14995   #define OP_Param         149
 14967  14996   #define OP_FkCounter     150 /* synopsis: fkctr[P1]+=P2                    */
 14968  14997   #define OP_MemMax        151 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 14969  14998   #define OP_OffsetLimit   152 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
................................................................................
 15006  15035   /*  32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
 15007  15036   /*  40 */ 0x01, 0x23, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03,\
 15008  15037   /*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 15009  15038   /*  56 */ 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,\
 15010  15039   /*  64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\
 15011  15040   /*  72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
 15012  15041   /*  80 */ 0x10, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
 15013         -/*  88 */ 0x12, 0x20, 0x00, 0x00, 0x26, 0x26, 0x26, 0x26,\
 15014         -/*  96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 15015         -/* 104 */ 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
        15042  +/*  88 */ 0x12, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
        15043  +/*  96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
        15044  +/* 104 */ 0x26, 0x26, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00,\
 15016  15045   /* 112 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 15017  15046   /* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 15018  15047   /* 128 */ 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\
 15019         -/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00,\
 15020         -/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
        15048  +/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
        15049  +/* 144 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
 15021  15050   /* 152 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 15022  15051   /* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
 15023  15052   /* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00,}
 15024  15053   
 15025  15054   /* The sqlite3P2Values() routine is able to run faster if it knows
 15026  15055   ** the value of the largest JUMP opcode.  The smaller the maximum
 15027  15056   ** JUMP opcode the better, so the mkopcodeh.tcl script that
................................................................................
 16321  16350   #endif
 16322  16351     void *pCommitArg;                 /* Argument to xCommitCallback() */
 16323  16352     int (*xCommitCallback)(void*);    /* Invoked at every commit. */
 16324  16353     void *pRollbackArg;               /* Argument to xRollbackCallback() */
 16325  16354     void (*xRollbackCallback)(void*); /* Invoked at every commit. */
 16326  16355     void *pUpdateArg;
 16327  16356     void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
        16357  +  Parse *pParse;                /* Current parse */
 16328  16358   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 16329  16359     void *pPreUpdateArg;          /* First argument to xPreUpdateCallback */
 16330  16360     void (*xPreUpdateCallback)(   /* Registered using sqlite3_preupdate_hook() */
 16331  16361       void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
 16332  16362     );
 16333  16363     PreUpdate *pPreUpdate;        /* Context for active pre-update callback */
 16334  16364   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
................................................................................
 16454  16484   
 16455  16485   /*
 16456  16486   ** Allowed values for sqlite3.mDbFlags
 16457  16487   */
 16458  16488   #define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
 16459  16489   #define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
 16460  16490   #define DBFLAG_Vacuum         0x0004  /* Currently in a VACUUM */
 16461         -#define DBFLAG_SchemaKnownOk  0x0008  /* Schema is known to be valid */
        16491  +#define DBFLAG_VacuumInto     0x0008  /* Currently running VACUUM INTO */
        16492  +#define DBFLAG_SchemaKnownOk  0x0010  /* Schema is known to be valid */
 16462  16493   
 16463  16494   /*
 16464  16495   ** Bits of the sqlite3.dbOptFlags field that are used by the
 16465  16496   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
 16466  16497   ** selectively disable various optimizations.
 16467  16498   */
 16468  16499   #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
 16469         -                          /*  0x0002   available for reuse */
        16500  +#define SQLITE_WindowFunc     0x0002   /* Use xInverse for window functions */
 16470  16501   #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
 16471  16502   #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
 16472  16503   #define SQLITE_DistinctOpt    0x0010   /* DISTINCT using indexes */
 16473  16504   #define SQLITE_CoverIdxScan   0x0020   /* Covering index scans */
 16474  16505   #define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
 16475  16506   #define SQLITE_Transitive     0x0080   /* Transitive constraints */
 16476  16507   #define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
................................................................................
 16580  16611   #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
 16581  16612   #define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
 16582  16613   #define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
 16583  16614                                       ** single query - might change over time */
 16584  16615   #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
 16585  16616   #define SQLITE_FUNC_OFFSET   0x8000 /* Built-in sqlite_offset() function */
 16586  16617   #define SQLITE_FUNC_WINDOW   0x00010000 /* Built-in window-only function */
 16587         -#define SQLITE_FUNC_WINDOW_SIZE 0x20000 /* Requires partition size as arg. */
 16588  16618   #define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
 16589  16619   
 16590  16620   /*
 16591  16621   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 16592  16622   ** used to create the initializers for the FuncDef structures.
 16593  16623   **
 16594  16624   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
................................................................................
 17386  17416         int regReturn;         /* Register used to hold return address */
 17387  17417       } sub;
 17388  17418     } y;
 17389  17419   };
 17390  17420   
 17391  17421   /*
 17392  17422   ** The following are the meanings of bits in the Expr.flags field.
        17423  +** Value restrictions:
        17424  +**
        17425  +**          EP_Agg == NC_HasAgg == SF_HasAgg
        17426  +**          EP_Win == NC_HasWin
 17393  17427   */
 17394  17428   #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
 17395         -#define EP_Agg       0x000002 /* Contains one or more aggregate functions */
        17429  +#define EP_Distinct  0x000002 /* Aggregate function with DISTINCT keyword */
 17396  17430   #define EP_HasFunc   0x000004 /* Contains one or more functions of any kind */
 17397  17431   #define EP_FixedCol  0x000008 /* TK_Column with a known fixed value */
 17398         -#define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
        17432  +#define EP_Agg       0x000010 /* Contains one or more aggregate functions */
 17399  17433   #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
 17400  17434   #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
 17401  17435   #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
 17402  17436   #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
 17403  17437   #define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
 17404  17438   #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
 17405  17439   #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
 17406  17440   #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
 17407  17441   #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
 17408  17442   #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
 17409         -#define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
        17443  +#define EP_Win       0x008000 /* Contains window functions */
 17410  17444   #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
 17411  17445   #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
 17412  17446   #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
 17413  17447   #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
 17414  17448   #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
 17415  17449   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
 17416  17450   #define EP_Alias     0x400000 /* Is an alias for a result set column */
 17417  17451   #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
 17418  17452   #define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
 17419  17453   #define EP_Subrtn   0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
 17420  17454   #define EP_Quoted   0x4000000 /* TK_ID was originally quoted */
        17455  +#define EP_Static   0x8000000 /* Held in memory not obtained from malloc() */
 17421  17456   
 17422  17457   /*
 17423  17458   ** The EP_Propagate mask is a set of properties that automatically propagate
 17424  17459   ** upwards into parent nodes.
 17425  17460   */
 17426  17461   #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
 17427  17462   
................................................................................
 17653  17688     Select *pWinSelect;  /* SELECT statement for any window functions */
 17654  17689   };
 17655  17690   
 17656  17691   /*
 17657  17692   ** Allowed values for the NameContext, ncFlags field.
 17658  17693   **
 17659  17694   ** Value constraints (all checked via assert()):
 17660         -**    NC_HasAgg    == SF_HasAgg
        17695  +**    NC_HasAgg    == SF_HasAgg    == EP_Agg
 17661  17696   **    NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
        17697  +**    NC_HasWin    == EP_Win
 17662  17698   **
 17663  17699   */
 17664  17700   #define NC_AllowAgg  0x0001  /* Aggregate functions are allowed here */
 17665  17701   #define NC_PartIdx   0x0002  /* True if resolving a partial index WHERE */
 17666  17702   #define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
 17667  17703   #define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
 17668  17704   #define NC_HasAgg    0x0010  /* One or more aggregate functions seen */
................................................................................
 17670  17706   #define NC_VarSelect 0x0040  /* A correlated subquery has been seen */
 17671  17707   #define NC_UEList    0x0080  /* True if uNC.pEList is used */
 17672  17708   #define NC_UAggInfo  0x0100  /* True if uNC.pAggInfo is used */
 17673  17709   #define NC_UUpsert   0x0200  /* True if uNC.pUpsert is used */
 17674  17710   #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
 17675  17711   #define NC_Complex   0x2000  /* True if a function or subquery seen */
 17676  17712   #define NC_AllowWin  0x4000  /* Window functions are allowed here */
        17713  +#define NC_HasWin    0x8000  /* One or more window functions seen */
 17677  17714   
 17678  17715   /*
 17679  17716   ** An instance of the following object describes a single ON CONFLICT
 17680  17717   ** clause in an upsert.
 17681  17718   **
 17682  17719   ** The pUpsertTarget field is only set if the ON CONFLICT clause includes
 17683  17720   ** conflict-target clause.  (In "ON CONFLICT(a,b)" the "(a,b)" is the
................................................................................
 17984  18021   #ifndef SQLITE_OMIT_SHARED_CACHE
 17985  18022     int nTableLock;        /* Number of locks in aTableLock */
 17986  18023     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 17987  18024   #endif
 17988  18025     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 17989  18026     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
 17990  18027     Table *pTriggerTab;  /* Table triggers are being coded for */
        18028  +  Parse *pParentParse; /* Parent parser if this parser is nested */
 17991  18029     int addrCrTab;       /* Address of OP_CreateBtree opcode on CREATE TABLE */
 17992  18030     u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
 17993  18031     u32 oldmask;         /* Mask of old.* columns referenced */
 17994  18032     u32 newmask;         /* Mask of new.* columns referenced */
 17995  18033     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
 17996  18034     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
 17997  18035     u8 disableTriggers;  /* True to disable triggers */
................................................................................
 18424  18462   struct TreeView {
 18425  18463     int iLevel;             /* Which level of the tree we are on */
 18426  18464     u8  bLine[100];         /* Draw vertical in column i if bLine[i] is true */
 18427  18465   };
 18428  18466   #endif /* SQLITE_DEBUG */
 18429  18467   
 18430  18468   /*
 18431         -** This object is used in varioius ways, all related to window functions
        18469  +** This object is used in various ways, all related to window functions
 18432  18470   **
 18433  18471   **   (1) A single instance of this structure is attached to the
 18434  18472   **       the Expr.pWin field for each window function in an expression tree.
 18435  18473   **       This object holds the information contained in the OVER clause,
 18436  18474   **       plus additional fields used during code generation.
 18437  18475   **
 18438  18476   **   (2) All window functions in a single SELECT form a linked-list
................................................................................
 18439  18477   **       attached to Select.pWin.  The Window.pFunc and Window.pExpr
 18440  18478   **       fields point back to the expression that is the window function.
 18441  18479   **
 18442  18480   **   (3) The terms of the WINDOW clause of a SELECT are instances of this
 18443  18481   **       object on a linked list attached to Select.pWinDefn.
 18444  18482   **
 18445  18483   ** The uses (1) and (2) are really the same Window object that just happens
 18446         -** to be accessible in two different ways.  Use (3) is are separate objects.
        18484  +** to be accessible in two different ways.  Use case (3) are separate objects.
 18447  18485   */
 18448  18486   struct Window {
 18449  18487     char *zName;            /* Name of window (may be NULL) */
        18488  +  char *zBase;            /* Name of base window for chaining (may be NULL) */
 18450  18489     ExprList *pPartition;   /* PARTITION BY clause */
 18451  18490     ExprList *pOrderBy;     /* ORDER BY clause */
 18452         -  u8 eType;               /* TK_RANGE or TK_ROWS */
        18491  +  u8 eFrmType;            /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
 18453  18492     u8 eStart;              /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
 18454  18493     u8 eEnd;                /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
        18494  +  u8 bImplicitFrame;      /* True if frame was implicitly specified */
        18495  +  u8 eExclude;            /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
 18455  18496     Expr *pStart;           /* Expression for "<expr> PRECEDING" */
 18456  18497     Expr *pEnd;             /* Expression for "<expr> FOLLOWING" */
 18457  18498     Window *pNextWin;       /* Next window function belonging to this SELECT */
 18458  18499     Expr *pFilter;          /* The FILTER expression */
 18459  18500     FuncDef *pFunc;         /* The function */
 18460  18501     int iEphCsr;            /* Partition buffer or Peer buffer */
 18461  18502     int regAccum;
 18462  18503     int regResult;
 18463  18504     int csrApp;             /* Function cursor (used by min/max) */
 18464  18505     int regApp;             /* Function register (also used by min/max) */
 18465         -  int regPart;            /* First in a set of registers holding PARTITION BY
 18466         -                          ** and ORDER BY values for the window */
        18506  +  int regPart;            /* Array of registers for PARTITION BY values */
 18467  18507     Expr *pOwner;           /* Expression object this window is attached to */
 18468  18508     int nBufferCol;         /* Number of columns in buffer table */
 18469  18509     int iArgCol;            /* Offset of first argument for this function */
        18510  +  int regOne;             /* Register containing constant value 1 */
        18511  +  int regStartRowid;
        18512  +  int regEndRowid;
 18470  18513   };
 18471  18514   
 18472  18515   #ifndef SQLITE_OMIT_WINDOWFUNC
 18473  18516   SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
 18474  18517   SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
 18475         -SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*);
        18518  +SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
 18476  18519   SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
 18477  18520   SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*);
 18478  18521   SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Window*);
 18479  18522   SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
 18480  18523   SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
 18481  18524   SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
 18482  18525   SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
 18483  18526   SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
 18484  18527   SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
 18485  18528   SQLITE_PRIVATE void sqlite3WindowFunctions(void);
        18529  +SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
        18530  +SQLITE_PRIVATE Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
 18486  18531   #else
 18487  18532   # define sqlite3WindowDelete(a,b)
 18488  18533   # define sqlite3WindowFunctions()
 18489  18534   # define sqlite3WindowAttach(a,b,c)
 18490  18535   #endif
 18491  18536   
 18492  18537   /*
................................................................................
 18708  18753   SQLITE_PRIVATE   void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
 18709  18754   #endif
 18710  18755   #endif
 18711  18756   
 18712  18757   
 18713  18758   SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
 18714  18759   SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
        18760  +SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int);
 18715  18761   SQLITE_PRIVATE void sqlite3Dequote(char*);
 18716  18762   SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
 18717  18763   SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
 18718  18764   SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
 18719  18765   SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
 18720  18766   SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
 18721  18767   SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
................................................................................
 20131  20177   */
 20132  20178   #define MEM_Null      0x0001   /* Value is NULL (or a pointer) */
 20133  20179   #define MEM_Str       0x0002   /* Value is a string */
 20134  20180   #define MEM_Int       0x0004   /* Value is an integer */
 20135  20181   #define MEM_Real      0x0008   /* Value is a real number */
 20136  20182   #define MEM_Blob      0x0010   /* Value is a BLOB */
 20137  20183   #define MEM_AffMask   0x001f   /* Mask of affinity bits */
 20138         -/* Available          0x0020   */
        20184  +#define MEM_FromBind  0x0020   /* Value originates from sqlite3_bind() */
 20139  20185   /* Available          0x0040   */
 20140  20186   #define MEM_Undefined 0x0080   /* Value is undefined */
 20141  20187   #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
 20142         -#define MEM_TypeMask  0xc1ff   /* Mask of type bits */
        20188  +#define MEM_TypeMask  0xc1df   /* Mask of type bits */
 20143  20189   
 20144  20190   
 20145  20191   /* Whenever Mem contains a valid string or blob representation, one of
 20146  20192   ** the following flags must be set to determine the memory management
 20147  20193   ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
 20148  20194   ** string is \000 or \u0000 terminated
 20149  20195   */
................................................................................
 20167  20213   
 20168  20214   /*
 20169  20215   ** Clear any existing type flags from a Mem and replace them with f
 20170  20216   */
 20171  20217   #define MemSetTypeFlag(p, f) \
 20172  20218      ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
 20173  20219   
        20220  +/*
        20221  +** True if Mem X is a NULL-nochng type.
        20222  +*/
        20223  +#define MemNullNochng(X) \
        20224  +  ((X)->flags==(MEM_Null|MEM_Zero) && (X)->n==0 && (X)->u.nZero==0)
        20225  +
 20174  20226   /*
 20175  20227   ** Return true if a memory cell is not marked as invalid.  This macro
 20176  20228   ** is for use inside assert() statements only.
 20177  20229   */
 20178  20230   #ifdef SQLITE_DEBUG
 20179  20231   #define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
 20180  20232   #endif
................................................................................
 27116  27168   SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
 27117  27169     if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
 27118  27170       db->mallocFailed = 1;
 27119  27171       if( db->nVdbeExec>0 ){
 27120  27172         db->u1.isInterrupted = 1;
 27121  27173       }
 27122  27174       db->lookaside.bDisable++;
        27175  +    if( db->pParse ){
        27176  +      db->pParse->rc = SQLITE_NOMEM_BKPT;
        27177  +    }
 27123  27178     }
 27124  27179   }
 27125  27180   
 27126  27181   /*
 27127  27182   ** This routine reactivates the memory allocator and clears the
 27128  27183   ** db->mallocFailed flag as necessary.
 27129  27184   **
................................................................................
 27309  27364   
 27310  27365   /*
 27311  27366   ** Set the StrAccum object to an error mode.
 27312  27367   */
 27313  27368   static void setStrAccumError(StrAccum *p, u8 eError){
 27314  27369     assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
 27315  27370     p->accError = eError;
 27316         -  p->nAlloc = 0;
        27371  +  if( p->mxAlloc ) sqlite3_str_reset(p);
        27372  +  if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
 27317  27373   }
 27318  27374   
 27319  27375   /*
 27320  27376   ** Extra argument values from a PrintfArguments object
 27321  27377   */
 27322  27378   static sqlite3_int64 getIntArg(PrintfArguments *p){
 27323  27379     if( p->nArg<=p->nUsed ) return 0;
................................................................................
 27339  27395   ** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
 27340  27396   ** Do the size check before the memory allocation to prevent rogue
 27341  27397   ** SQL from requesting large allocations using the precision or width
 27342  27398   ** field of the printf() function.
 27343  27399   */
 27344  27400   static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
 27345  27401     char *z;
        27402  +  if( pAccum->accError ) return 0;
 27346  27403     if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
 27347  27404       setStrAccumError(pAccum, SQLITE_TOOBIG);
 27348  27405       return 0;
 27349  27406     }
 27350  27407     z = sqlite3DbMallocRaw(pAccum->db, n);
 27351  27408     if( z==0 ){
 27352  27409       setStrAccumError(pAccum, SQLITE_NOMEM);
................................................................................
 28058  28115     assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
 28059  28116     if( p->accError ){
 28060  28117       testcase(p->accError==SQLITE_TOOBIG);
 28061  28118       testcase(p->accError==SQLITE_NOMEM);
 28062  28119       return 0;
 28063  28120     }
 28064  28121     if( p->mxAlloc==0 ){
 28065         -    N = p->nAlloc - p->nChar - 1;
 28066  28122       setStrAccumError(p, SQLITE_TOOBIG);
 28067         -    return N;
        28123  +    return p->nAlloc - p->nChar - 1;
 28068  28124     }else{
 28069  28125       char *zOld = isMalloced(p) ? p->zText : 0;
 28070  28126       i64 szNew = p->nChar;
 28071  28127       szNew += N + 1;
 28072  28128       if( szNew+p->nChar<=p->mxAlloc ){
 28073  28129         /* Force exponential buffer size growth as long as it does not overflow,
 28074  28130         ** to avoid having to call this routine too often */
................................................................................
 28132  28188   ** Append N bytes of text from z to the StrAccum object.  Increase the
 28133  28189   ** size of the memory allocation for StrAccum if necessary.
 28134  28190   */
 28135  28191   SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
 28136  28192     assert( z!=0 || N==0 );
 28137  28193     assert( p->zText!=0 || p->nChar==0 || p->accError );
 28138  28194     assert( N>=0 );
 28139         -  assert( p->accError==0 || p->nAlloc==0 );
        28195  +  assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
 28140  28196     if( p->nChar+N >= p->nAlloc ){
 28141  28197       enlargeAndAppend(p,z,N);
 28142  28198     }else if( N ){
 28143  28199       assert( p->zText );
 28144  28200       p->nChar += N;
 28145  28201       memcpy(&p->zText[p->nChar-N], z, N);
 28146  28202     }
................................................................................
 28765  28821   #endif /* SQLITE_OMIT_WINDOWFUNC */
 28766  28822   
 28767  28823   #ifndef SQLITE_OMIT_WINDOWFUNC
 28768  28824   /*
 28769  28825   ** Generate a human-readable explanation for a Window object
 28770  28826   */
 28771  28827   SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
        28828  +  int nElement = 0;
        28829  +  if( pWin->pFilter ){
        28830  +    sqlite3TreeViewItem(pView, "FILTER", 1);
        28831  +    sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
        28832  +    sqlite3TreeViewPop(pView);
        28833  +  }
 28772  28834     pView = sqlite3TreeViewPush(pView, more);
 28773  28835     if( pWin->zName ){
 28774         -    sqlite3TreeViewLine(pView, "OVER %s", pWin->zName);
        28836  +    sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
 28775  28837     }else{
 28776         -    sqlite3TreeViewLine(pView, "OVER");
        28838  +    sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
        28839  +  }
        28840  +  if( pWin->zBase )    nElement++;
        28841  +  if( pWin->pOrderBy ) nElement++;
        28842  +  if( pWin->eFrmType ) nElement++;
        28843  +  if( pWin->eExclude ) nElement++;
        28844  +  if( pWin->zBase ){
        28845  +    sqlite3TreeViewPush(pView, (--nElement)>0);
        28846  +    sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
        28847  +    sqlite3TreeViewPop(pView);
 28777  28848     }
 28778  28849     if( pWin->pPartition ){
 28779         -    sqlite3TreeViewExprList(pView, pWin->pPartition, 1, "PARTITION-BY");
        28850  +    sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
 28780  28851     }
 28781  28852     if( pWin->pOrderBy ){
 28782         -    sqlite3TreeViewExprList(pView, pWin->pOrderBy, 1, "ORDER-BY");
        28853  +    sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
 28783  28854     }
 28784         -  if( pWin->eType ){
 28785         -    sqlite3TreeViewItem(pView, pWin->eType==TK_RANGE ? "RANGE" : "ROWS", 0);
        28855  +  if( pWin->eFrmType ){
        28856  +    char zBuf[30];
        28857  +    const char *zFrmType = "ROWS";
        28858  +    if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
        28859  +    if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
        28860  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
        28861  +        pWin->bImplicitFrame ? " (implied)" : "");
        28862  +    sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
 28786  28863       sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
 28787  28864       sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
 28788  28865       sqlite3TreeViewPop(pView);
 28789  28866     }
        28867  +  if( pWin->eExclude ){
        28868  +    char zBuf[30];
        28869  +    const char *zExclude;
        28870  +    switch( pWin->eExclude ){
        28871  +      case TK_NO:      zExclude = "NO OTHERS";   break;
        28872  +      case TK_CURRENT: zExclude = "CURRENT ROW"; break;
        28873  +      case TK_GROUP:   zExclude = "GROUP";       break;
        28874  +      case TK_TIES:    zExclude = "TIES";        break;
        28875  +      default:
        28876  +        sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
        28877  +        zExclude = zBuf;
        28878  +        break;
        28879  +    }
        28880  +    sqlite3TreeViewPush(pView, 0);
        28881  +    sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
        28882  +    sqlite3TreeViewPop(pView);
        28883  +  }
 28790  28884     sqlite3TreeViewPop(pView);
 28791  28885   }
 28792  28886   #endif /* SQLITE_OMIT_WINDOWFUNC */
 28793  28887   
 28794  28888   #ifndef SQLITE_OMIT_WINDOWFUNC
 28795  28889   /*
 28796  28890   ** Generate a human-readable explanation for a Window Function object
................................................................................
 29762  29856   #ifndef SQLITE_OMIT_UTF16
 29763  29857   /*
 29764  29858   ** This routine transforms the internal text encoding used by pMem to
 29765  29859   ** desiredEnc. It is an error if the string is already of the desired
 29766  29860   ** encoding, or if *pMem does not contain a string value.
 29767  29861   */
 29768  29862   SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
 29769         -  int len;                    /* Maximum length of output string in bytes */
 29770         -  unsigned char *zOut;                  /* Output buffer */
 29771         -  unsigned char *zIn;                   /* Input iterator */
 29772         -  unsigned char *zTerm;                 /* End of input */
 29773         -  unsigned char *z;                     /* Output iterator */
        29863  +  sqlite3_int64 len;          /* Maximum length of output string in bytes */
        29864  +  unsigned char *zOut;        /* Output buffer */
        29865  +  unsigned char *zIn;         /* Input iterator */
        29866  +  unsigned char *zTerm;       /* End of input */
        29867  +  unsigned char *z;           /* Output iterator */
 29774  29868     unsigned int c;
 29775  29869   
 29776  29870     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 29777  29871     assert( pMem->flags&MEM_Str );
 29778  29872     assert( pMem->enc!=desiredEnc );
 29779  29873     assert( pMem->enc!=0 );
 29780  29874     assert( pMem->n>=0 );
................................................................................
 29815  29909     if( desiredEnc==SQLITE_UTF8 ){
 29816  29910       /* When converting from UTF-16, the maximum growth results from
 29817  29911       ** translating a 2-byte character to a 4-byte UTF-8 character.
 29818  29912       ** A single byte is required for the output string
 29819  29913       ** nul-terminator.
 29820  29914       */
 29821  29915       pMem->n &= ~1;
 29822         -    len = pMem->n * 2 + 1;
        29916  +    len = 2 * (sqlite3_int64)pMem->n + 1;
 29823  29917     }else{
 29824  29918       /* When converting from UTF-8 to UTF-16 the maximum growth is caused
 29825  29919       ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
 29826  29920       ** character. Two bytes are required in the output buffer for the
 29827  29921       ** nul-terminator.
 29828  29922       */
 29829         -    len = pMem->n * 2 + 2;
        29923  +    len = 2 * (sqlite3_int64)pMem->n + 2;
 29830  29924     }
 29831  29925   
 29832  29926     /* Set zIn to point at the start of the input buffer and zTerm to point 1
 29833  29927     ** byte past the end.
 29834  29928     **
 29835  29929     ** Variable zOut is set to point at the output buffer, space obtained
 29836  29930     ** from sqlite3_malloc().
................................................................................
 30129  30223   SQLITE_PRIVATE void sqlite3Coverage(int x){
 30130  30224     static unsigned dummy = 0;
 30131  30225     dummy += (unsigned)x;
 30132  30226   }
 30133  30227   #endif
 30134  30228   
 30135  30229   /*
 30136         -** Give a callback to the test harness that can be used to simulate faults
 30137         -** in places where it is difficult or expensive to do so purely by means
 30138         -** of inputs.
        30230  +** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
        30231  +** or to bypass normal error detection during testing in order to let 
        30232  +** execute proceed futher downstream.
 30139  30233   **
 30140         -** The intent of the integer argument is to let the fault simulator know
 30141         -** which of multiple sqlite3FaultSim() calls has been hit.
        30234  +** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0).  The
        30235  +** sqlite3FaultSim() function only returns non-zero during testing.
 30142  30236   **
 30143         -** Return whatever integer value the test callback returns, or return
 30144         -** SQLITE_OK if no test callback is installed.
        30237  +** During testing, if the test harness has set a fault-sim callback using
        30238  +** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
        30239  +** each call to sqlite3FaultSim() is relayed to that application-supplied
        30240  +** callback and the integer return value form the application-supplied
        30241  +** callback is returned by sqlite3FaultSim().
        30242  +**
        30243  +** The integer argument to sqlite3FaultSim() is a code to identify which
        30244  +** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
        30245  +** should have a unique code.  To prevent legacy testing applications from
        30246  +** breaking, the codes should not be changed or reused.
 30145  30247   */
 30146  30248   #ifndef SQLITE_UNTESTABLE
 30147  30249   SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
 30148  30250     int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
 30149  30251     return xCallback ? xCallback(iTest) : SQLITE_OK;
 30150  30252   }
 30151  30253   #endif
................................................................................
 30321  30423     }else{
 30322  30424       pParse->nErr++;
 30323  30425       sqlite3DbFree(db, pParse->zErrMsg);
 30324  30426       pParse->zErrMsg = zMsg;
 30325  30427       pParse->rc = SQLITE_ERROR;
 30326  30428     }
 30327  30429   }
        30430  +
        30431  +/*
        30432  +** If database connection db is currently parsing SQL, then transfer
        30433  +** error code errCode to that parser if the parser has not already
        30434  +** encountered some other kind of error.
        30435  +*/
        30436  +SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3 *db, int errCode){
        30437  +  Parse *pParse;
        30438  +  if( db==0 || (pParse = db->pParse)==0 ) return errCode;
        30439  +  pParse->rc = errCode;
        30440  +  pParse->nErr++;
        30441  +  return errCode;
        30442  +}
 30328  30443   
 30329  30444   /*
 30330  30445   ** Convert an SQL-style quoted string into a normal string by removing
 30331  30446   ** the quote characters.  The conversion is done in-place.  If the
 30332  30447   ** input does not begin with a quote character, then this routine
 30333  30448   ** is a no-op.
 30334  30449   **
................................................................................
 31673  31788     char *z;               /* Pointer to where zName will be stored */
 31674  31789     int i;                 /* Index in pIn[] where zName is stored */
 31675  31790   
 31676  31791     nInt = nName/4 + 3;
 31677  31792     assert( pIn==0 || pIn[0]>=3 );  /* Verify ok to add new elements */
 31678  31793     if( pIn==0 || pIn[1]+nInt > pIn[0] ){
 31679  31794       /* Enlarge the allocation */
 31680         -    int nAlloc = (pIn ? pIn[0]*2 : 10) + nInt;
        31795  +    sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
 31681  31796       VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
 31682  31797       if( pOut==0 ) return pIn;
 31683  31798       if( pIn==0 ) pOut[1] = 2;
 31684  31799       pIn = pOut;
 31685  31800       pIn[0] = nAlloc;
 31686  31801     }
 31687  31802     i = pIn[1];
................................................................................
 31879  31994   */
 31880  31995   static HashElem *findElementWithHash(
 31881  31996     const Hash *pH,     /* The pH to be searched */
 31882  31997     const char *pKey,   /* The key we are searching for */
 31883  31998     unsigned int *pHash /* Write the hash value here */
 31884  31999   ){
 31885  32000     HashElem *elem;                /* Used to loop thru the element list */
 31886         -  int count;                     /* Number of elements left to test */
        32001  +  unsigned int count;            /* Number of elements left to test */
 31887  32002     unsigned int h;                /* The computed hash */
 31888  32003     static HashElem nullElement = { 0, 0, 0, 0 };
 31889  32004   
 31890  32005     if( pH->ht ){   /*OPTIMIZATION-IF-TRUE*/
 31891  32006       struct _ht *pEntry;
 31892  32007       h = strHash(pKey) % pH->htsize;
 31893  32008       pEntry = &pH->ht[h];
................................................................................
 31927  32042       elem->next->prev = elem->prev;
 31928  32043     }
 31929  32044     if( pH->ht ){
 31930  32045       pEntry = &pH->ht[h];
 31931  32046       if( pEntry->chain==elem ){
 31932  32047         pEntry->chain = elem->next;
 31933  32048       }
        32049  +    assert( pEntry->count>0 );
 31934  32050       pEntry->count--;
 31935         -    assert( pEntry->count>=0 );
 31936  32051     }
 31937  32052     sqlite3_free( elem );
 31938  32053     pH->count--;
 31939  32054     if( pH->count==0 ){
 31940  32055       assert( pH->first==0 );
 31941  32056       assert( pH->count==0 );
 31942  32057       sqlite3HashClear(pH);
................................................................................
 32103  32218       /*  85 */ "Cast"             OpHelp("affinity(r[P1])"),
 32104  32219       /*  86 */ "Permutation"      OpHelp(""),
 32105  32220       /*  87 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 32106  32221       /*  88 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
 32107  32222       /*  89 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
 32108  32223       /*  90 */ "Column"           OpHelp("r[P3]=PX"),
 32109  32224       /*  91 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 32110         -    /*  92 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 32111         -    /*  93 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 32112         -    /*  94 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 32113         -    /*  95 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 32114         -    /*  96 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 32115         -    /*  97 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 32116         -    /*  98 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 32117         -    /*  99 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 32118         -    /* 100 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 32119         -    /* 101 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 32120         -    /* 102 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 32121         -    /* 103 */ "BitNot"           OpHelp("r[P2]= ~r[P1]"),
 32122         -    /* 104 */ "Count"            OpHelp("r[P2]=count()"),
 32123         -    /* 105 */ "ReadCookie"       OpHelp(""),
 32124         -    /* 106 */ "String8"          OpHelp("r[P2]='P4'"),
 32125         -    /* 107 */ "SetCookie"        OpHelp(""),
 32126         -    /* 108 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 32127         -    /* 109 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 32128         -    /* 110 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        32225  +    /*  92 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        32226  +    /*  93 */ "Count"            OpHelp("r[P2]=count()"),
        32227  +    /*  94 */ "ReadCookie"       OpHelp(""),
        32228  +    /*  95 */ "SetCookie"        OpHelp(""),
        32229  +    /*  96 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
        32230  +    /*  97 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
        32231  +    /*  98 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
        32232  +    /*  99 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
        32233  +    /* 100 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
        32234  +    /* 101 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
        32235  +    /* 102 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
        32236  +    /* 103 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
        32237  +    /* 104 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
        32238  +    /* 105 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
        32239  +    /* 106 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        32240  +    /* 107 */ "BitNot"           OpHelp("r[P2]= ~r[P1]"),
        32241  +    /* 108 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        32242  +    /* 109 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        32243  +    /* 110 */ "String8"          OpHelp("r[P2]='P4'"),
 32129  32244       /* 111 */ "OpenDup"          OpHelp(""),
 32130  32245       /* 112 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 32131  32246       /* 113 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 32132  32247       /* 114 */ "SorterOpen"       OpHelp(""),
 32133  32248       /* 115 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 32134  32249       /* 116 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 32135  32250       /* 117 */ "Close"            OpHelp(""),
................................................................................
 32152  32267       /* 134 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 32153  32268       /* 135 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 32154  32269       /* 136 */ "Destroy"          OpHelp(""),
 32155  32270       /* 137 */ "Clear"            OpHelp(""),
 32156  32271       /* 138 */ "ResetSorter"      OpHelp(""),
 32157  32272       /* 139 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 32158  32273       /* 140 */ "SqlExec"          OpHelp(""),
 32159         -    /* 141 */ "Real"             OpHelp("r[P2]=P4"),
 32160         -    /* 142 */ "ParseSchema"      OpHelp(""),
 32161         -    /* 143 */ "LoadAnalysis"     OpHelp(""),
 32162         -    /* 144 */ "DropTable"        OpHelp(""),
 32163         -    /* 145 */ "DropIndex"        OpHelp(""),
        32274  +    /* 141 */ "ParseSchema"      OpHelp(""),
        32275  +    /* 142 */ "LoadAnalysis"     OpHelp(""),
        32276  +    /* 143 */ "DropTable"        OpHelp(""),
        32277  +    /* 144 */ "DropIndex"        OpHelp(""),
        32278  +    /* 145 */ "Real"             OpHelp("r[P2]=P4"),
 32164  32279       /* 146 */ "DropTrigger"      OpHelp(""),
 32165  32280       /* 147 */ "IntegrityCk"      OpHelp(""),
 32166  32281       /* 148 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 32167  32282       /* 149 */ "Param"            OpHelp(""),
 32168  32283       /* 150 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 32169  32284       /* 151 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 32170  32285       /* 152 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
................................................................................
 49012  49127   
 49013  49128   /*
 49014  49129   ** Malloc function used by SQLite to obtain space from the buffer configured
 49015  49130   ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
 49016  49131   ** exists, this function falls back to sqlite3Malloc().
 49017  49132   */
 49018  49133   SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
 49019         -  /* During rebalance operations on a corrupt database file, it is sometimes
 49020         -  ** (rarely) possible to overread the temporary page buffer by a few bytes.
 49021         -  ** Enlarge the allocation slightly so that this does not cause problems. */
        49134  +  assert( sz<=65536+8 ); /* These allocations are never very large */
 49022  49135     return pcache1Alloc(sz);
 49023  49136   }
 49024  49137   
 49025  49138   /*
 49026  49139   ** Free an allocated buffer obtained from sqlite3PageMalloc().
 49027  49140   */
 49028  49141   SQLITE_PRIVATE void sqlite3PageFree(void *p){
................................................................................
 51298  51411   **   * the database file is open,
 51299  51412   **   * there are no dirty pages in the cache, and
 51300  51413   **   * the desired page is not currently in the wal file.
 51301  51414   */
 51302  51415   SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
 51303  51416     if( pPager->fd->pMethods==0 ) return 0;
 51304  51417     if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
        51418  +#ifdef SQLITE_HAS_CODEC
        51419  +  if( pPager->xCodec!=0 ) return 0;
        51420  +#endif
 51305  51421   #ifndef SQLITE_OMIT_WAL
 51306  51422     if( pPager->pWal ){
 51307  51423       u32 iRead = 0;
 51308  51424       int rc;
 51309  51425       rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
 51310  51426       return (rc==SQLITE_OK && iRead==0);
 51311  51427     }
................................................................................
 54247  54363       char *pNew = NULL;             /* New temp space */
 54248  54364       i64 nByte = 0;
 54249  54365   
 54250  54366       if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
 54251  54367         rc = sqlite3OsFileSize(pPager->fd, &nByte);
 54252  54368       }
 54253  54369       if( rc==SQLITE_OK ){
 54254         -      pNew = (char *)sqlite3PageMalloc(pageSize);
 54255         -      if( !pNew ) rc = SQLITE_NOMEM_BKPT;
        54370  +      /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
        54371  +      * cell header parser will never run off the end of the allocation */
        54372  +      pNew = (char *)sqlite3PageMalloc(pageSize+8);
        54373  +      if( !pNew ){
        54374  +        rc = SQLITE_NOMEM_BKPT;
        54375  +      }else{
        54376  +        memset(pNew+pageSize, 0, 8);
        54377  +      }
 54256  54378       }
 54257  54379   
 54258  54380       if( rc==SQLITE_OK ){
 54259  54381         pager_reset(pPager);
 54260  54382         rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
 54261  54383       }
 54262  54384       if( rc==SQLITE_OK ){
................................................................................
 57629  57751     /* If the cache contains a page with page-number pgno, remove it
 57630  57752     ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
 57631  57753     ** page pgno before the 'move' operation, it needs to be retained 
 57632  57754     ** for the page moved there.
 57633  57755     */
 57634  57756     pPg->flags &= ~PGHDR_NEED_SYNC;
 57635  57757     pPgOld = sqlite3PagerLookup(pPager, pgno);
 57636         -  assert( !pPgOld || pPgOld->nRef==1 );
        57758  +  assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
 57637  57759     if( pPgOld ){
        57760  +    if( pPgOld->nRef>1 ){
        57761  +      sqlite3PagerUnrefNotNull(pPgOld);
        57762  +      return SQLITE_CORRUPT_BKPT;
        57763  +    }
 57638  57764       pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
 57639  57765       if( pPager->tempFile ){
 57640  57766         /* Do not discard pages from an in-memory database since we might
 57641  57767         ** need to rollback later.  Just move the page out of the way. */
 57642  57768         sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
 57643  57769       }else{
 57644  57770         sqlite3PcacheDrop(pPgOld);
................................................................................
 58158  58284   
 58159  58285   /*
 58160  58286   ** Release a lock obtained by an earlier successful call to
 58161  58287   ** sqlite3PagerSnapshotCheck().
 58162  58288   */
 58163  58289   SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
 58164  58290     assert( pPager->pWal );
 58165         -  return sqlite3WalSnapshotUnlock(pPager->pWal);
        58291  +  sqlite3WalSnapshotUnlock(pPager->pWal);
 58166  58292   }
 58167  58293   
 58168  58294   #endif /* SQLITE_ENABLE_SNAPSHOT */
 58169  58295   #endif /* !SQLITE_OMIT_WAL */
 58170  58296   
 58171  58297   #ifdef SQLITE_ENABLE_ZIPVFS
 58172  58298   /*
................................................................................
 58759  58885     int iPage,               /* The page we seek */
 58760  58886     volatile u32 **ppPage    /* Write the page pointer here */
 58761  58887   ){
 58762  58888     int rc = SQLITE_OK;
 58763  58889   
 58764  58890     /* Enlarge the pWal->apWiData[] array if required */
 58765  58891     if( pWal->nWiData<=iPage ){
 58766         -    int nByte = sizeof(u32*)*(iPage+1);
        58892  +    sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
 58767  58893       volatile u32 **apNew;
 58768  58894       apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
 58769  58895       if( !apNew ){
 58770  58896         *ppPage = 0;
 58771  58897         return SQLITE_NOMEM_BKPT;
 58772  58898       }
 58773  58899       memset((void*)&apNew[pWal->nWiData], 0,
................................................................................
 58863  58989       s2 = aIn[1];
 58864  58990     }else{
 58865  58991       s1 = s2 = 0;
 58866  58992     }
 58867  58993   
 58868  58994     assert( nByte>=8 );
 58869  58995     assert( (nByte&0x00000007)==0 );
        58996  +  assert( nByte<=65536 );
 58870  58997   
 58871  58998     if( nativeCksum ){
 58872  58999       do {
 58873  59000         s1 += *aData++ + s2;
 58874  59001         s2 += *aData++ + s1;
 58875  59002       }while( aData<aEnd );
 58876  59003     }else{
................................................................................
 59170  59297   ** actually needed.
 59171  59298   */
 59172  59299   static void walCleanupHash(Wal *pWal){
 59173  59300     WalHashLoc sLoc;                /* Hash table location */
 59174  59301     int iLimit = 0;                 /* Zero values greater than this */
 59175  59302     int nByte;                      /* Number of bytes to zero in aPgno[] */
 59176  59303     int i;                          /* Used to iterate through aHash[] */
        59304  +  int rc;                         /* Return code form walHashGet() */
 59177  59305   
 59178  59306     assert( pWal->writeLock );
 59179  59307     testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
 59180  59308     testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
 59181  59309     testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
 59182  59310   
 59183  59311     if( pWal->hdr.mxFrame==0 ) return;
 59184  59312   
 59185  59313     /* Obtain pointers to the hash-table and page-number array containing 
 59186  59314     ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
 59187         -  ** that the page said hash-table and array reside on is already mapped.
        59315  +  ** that the page said hash-table and array reside on is already mapped.(1)
 59188  59316     */
 59189  59317     assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
 59190  59318     assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
 59191         -  walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
        59319  +  rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
        59320  +  if( NEVER(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */
 59192  59321   
 59193  59322     /* Zero all hash-table entries that correspond to frame numbers greater
 59194  59323     ** than pWal->hdr.mxFrame.
 59195  59324     */
 59196  59325     iLimit = pWal->hdr.mxFrame - sLoc.iZero;
 59197  59326     assert( iLimit>0 );
 59198  59327     for(i=0; i<HASHTABLE_NSLOT; i++){
................................................................................
 59798  59927   ** The calling routine should invoke walIteratorFree() to destroy the
 59799  59928   ** WalIterator object when it has finished with it.
 59800  59929   */
 59801  59930   static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
 59802  59931     WalIterator *p;                 /* Return value */
 59803  59932     int nSegment;                   /* Number of segments to merge */
 59804  59933     u32 iLast;                      /* Last frame in log */
 59805         -  int nByte;                      /* Number of bytes to allocate */
        59934  +  sqlite3_int64 nByte;            /* Number of bytes to allocate */
 59806  59935     int i;                          /* Iterator variable */
 59807  59936     ht_slot *aTmp;                  /* Temp space used by merge-sort */
 59808  59937     int rc = SQLITE_OK;             /* Return Code */
 59809  59938   
 59810  59939     /* This routine only runs while holding the checkpoint lock. And
 59811  59940     ** it only runs if there is actually content in the log (mxFrame>0).
 59812  59941     */
................................................................................
 62334  62463     u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
 62335  62464     u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
 62336  62465     u8 max1bytePayload;  /* min(maxLocal,127) */
 62337  62466     u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
 62338  62467     u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
 62339  62468     u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
 62340  62469     u16 cellOffset;      /* Index in aData of first cell pointer */
 62341         -  u16 nFree;           /* Number of free bytes on the page */
        62470  +  int nFree;           /* Number of free bytes on the page. -1 for unknown */
 62342  62471     u16 nCell;           /* Number of cells on this page, local and ovfl */
 62343  62472     u16 maskPage;        /* Mask for page offset */
 62344  62473     u16 aiOvfl[4];       /* Insert the i-th overflow cell before the aiOvfl-th
 62345  62474                          ** non-overflow cell */
 62346  62475     u8 *apOvfl[4];       /* Pointers to the body of overflow cells */
 62347  62476     BtShared *pBt;       /* Pointer to BtShared that this page is part of */
 62348  62477     u8 *aData;           /* Pointer to disk image of the page data */
................................................................................
 63888  64017   ** when saveCursorPosition() was called. Note that this call deletes the 
 63889  64018   ** saved position info stored by saveCursorPosition(), so there can be
 63890  64019   ** at most one effective restoreCursorPosition() call after each 
 63891  64020   ** saveCursorPosition().
 63892  64021   */
 63893  64022   static int btreeRestoreCursorPosition(BtCursor *pCur){
 63894  64023     int rc;
 63895         -  int skipNext;
        64024  +  int skipNext = 0;
 63896  64025     assert( cursorOwnsBtShared(pCur) );
 63897  64026     assert( pCur->eState>=CURSOR_REQUIRESEEK );
 63898  64027     if( pCur->eState==CURSOR_FAULT ){
 63899  64028       return pCur->skipNext;
 63900  64029     }
 63901  64030     pCur->eState = CURSOR_INVALID;
 63902         -  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
        64031  +  if( sqlite3FaultSim(410) ){
        64032  +    rc = SQLITE_IOERR;
        64033  +  }else{
        64034  +    rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
        64035  +  }
 63903  64036     if( rc==SQLITE_OK ){
 63904  64037       sqlite3_free(pCur->pKey);
 63905  64038       pCur->pKey = 0;
 63906  64039       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
 63907  64040       if( skipNext ) pCur->skipNext = skipNext;
 63908  64041       if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
 63909  64042         pCur->eState = CURSOR_SKIPNEXT;
................................................................................
 64476  64609     assert( pPage->nOverflow==0 );
 64477  64610     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 64478  64611     temp = 0;
 64479  64612     src = data = pPage->aData;
 64480  64613     hdr = pPage->hdrOffset;
 64481  64614     cellOffset = pPage->cellOffset;
 64482  64615     nCell = pPage->nCell;
 64483         -  assert( nCell==get2byte(&data[hdr+3]) );
        64616  +  assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
 64484  64617     iCellFirst = cellOffset + 2*nCell;
 64485  64618     usableSize = pPage->pBt->usableSize;
 64486  64619   
 64487  64620     /* This block handles pages with two or fewer free blocks and nMaxFrag
 64488  64621     ** or fewer fragmented bytes. In this case it is faster to move the
 64489  64622     ** two (or one) blocks of cells using memmove() and add the required
 64490  64623     ** offsets to each pointer in the cell-pointer array than it is to 
 64491  64624     ** reconstruct the entire page.  */
 64492  64625     if( (int)data[hdr+7]<=nMaxFrag ){
 64493  64626       int iFree = get2byte(&data[hdr+1]);
 64494         -
 64495         -    /* If the initial freeblock offset were out of bounds, that would
 64496         -    ** have been detected by btreeInitPage() when it was computing the
 64497         -    ** number of free bytes on the page. */
 64498         -    assert( iFree<=usableSize-4 );
        64627  +    if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
 64499  64628       if( iFree ){
 64500  64629         int iFree2 = get2byte(&data[iFree]);
 64501  64630         if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
 64502  64631         if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
 64503  64632           u8 *pEnd = &data[cellOffset + nCell*2];
 64504  64633           u8 *pAddr;
 64505  64634           int sz2 = 0;
................................................................................
 64510  64639           }
 64511  64640           if( iFree2 ){
 64512  64641             if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
 64513  64642             sz2 = get2byte(&data[iFree2+2]);
 64514  64643             if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
 64515  64644             memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
 64516  64645             sz += sz2;
        64646  +        }else if( iFree+sz>usableSize ){
        64647  +          return SQLITE_CORRUPT_PAGE(pPage);
 64517  64648           }
        64649  +
 64518  64650           cbrk = top+sz;
 64519  64651           assert( cbrk+(iFree-top) <= usableSize );
 64520  64652           memmove(&data[cbrk], &data[top], iFree-top);
 64521  64653           for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
 64522  64654             pc = get2byte(pAddr);
 64523  64655             if( pc<iFree ){ put2byte(pAddr, pc+sz); }
 64524  64656             else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
................................................................................
 64561  64693         src = temp;
 64562  64694       }
 64563  64695       memcpy(&data[cbrk], &src[pc], size);
 64564  64696     }
 64565  64697     data[hdr+7] = 0;
 64566  64698   
 64567  64699    defragment_out:
        64700  +  assert( pPage->nFree>=0 );
 64568  64701     if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
 64569  64702       return SQLITE_CORRUPT_PAGE(pPage);
 64570  64703     }
 64571  64704     assert( cbrk>=iCellFirst );
 64572  64705     put2byte(&data[hdr+5], cbrk);
 64573  64706     data[hdr+1] = 0;
 64574  64707     data[hdr+2] = 0;
................................................................................
 64588  64721   ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
 64589  64722   **
 64590  64723   ** Slots on the free list that are between 1 and 3 bytes larger than nByte
 64591  64724   ** will be ignored if adding the extra space to the fragmentation count
 64592  64725   ** causes the fragmentation count to exceed 60.
 64593  64726   */
 64594  64727   static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
 64595         -  const int hdr = pPg->hdrOffset;
 64596         -  u8 * const aData = pPg->aData;
 64597         -  int iAddr = hdr + 1;
 64598         -  int pc = get2byte(&aData[iAddr]);
 64599         -  int x;
 64600         -  int usableSize = pPg->pBt->usableSize;
 64601         -  int size;            /* Size of the free slot */
        64728  +  const int hdr = pPg->hdrOffset;            /* Offset to page header */
        64729  +  u8 * const aData = pPg->aData;             /* Page data */
        64730  +  int iAddr = hdr + 1;                       /* Address of ptr to pc */
        64731  +  int pc = get2byte(&aData[iAddr]);          /* Address of a free slot */
        64732  +  int x;                                     /* Excess size of the slot */
        64733  +  int maxPC = pPg->pBt->usableSize - nByte;  /* Max address for a usable slot */
        64734  +  int size;                                  /* Size of the free slot */
 64602  64735   
 64603  64736     assert( pc>0 );
 64604         -  while( pc<=usableSize-4 ){
        64737  +  while( pc<=maxPC ){
 64605  64738       /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
 64606  64739       ** freeblock form a big-endian integer which is the size of the freeblock
 64607  64740       ** in bytes, including the 4-byte header. */
 64608  64741       size = get2byte(&aData[pc+2]);
 64609  64742       if( (x = size - nByte)>=0 ){
 64610  64743         testcase( x==4 );
 64611  64744         testcase( x==3 );
 64612         -      if( size+pc > usableSize ){
 64613         -        *pRc = SQLITE_CORRUPT_PAGE(pPg);
 64614         -        return 0;
 64615         -      }else if( x<4 ){
        64745  +      if( x<4 ){
 64616  64746           /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
 64617  64747           ** number of bytes in fragments may not exceed 60. */
 64618  64748           if( aData[hdr+7]>57 ) return 0;
 64619  64749   
 64620  64750           /* Remove the slot from the free-list. Update the number of
 64621  64751           ** fragmented bytes within the page. */
 64622  64752           memcpy(&aData[iAddr], &aData[pc], 2);
 64623  64753           aData[hdr+7] += (u8)x;
        64754  +      }else if( x+pc > maxPC ){
        64755  +        /* This slot extends off the end of the usable part of the page */
        64756  +        *pRc = SQLITE_CORRUPT_PAGE(pPg);
        64757  +        return 0;
 64624  64758         }else{
 64625  64759           /* The slot remains on the free-list. Reduce its size to account
 64626         -         ** for the portion used by the new allocation. */
        64760  +        ** for the portion used by the new allocation. */
 64627  64761           put2byte(&aData[pc+2], x);
 64628  64762         }
 64629  64763         return &aData[pc + x];
 64630  64764       }
 64631  64765       iAddr = pc;
 64632  64766       pc = get2byte(&aData[pc]);
 64633         -    if( pc<iAddr+size ) break;
        64767  +    if( pc<=iAddr+size ){
        64768  +      if( pc ){
        64769  +        /* The next slot in the chain is not past the end of the current slot */
        64770  +        *pRc = SQLITE_CORRUPT_PAGE(pPg);
        64771  +      }
        64772  +      return 0;
        64773  +    }
 64634  64774     }
 64635         -  if( pc ){
        64775  +  if( pc>maxPC+nByte-4 ){
        64776  +    /* The free slot chain extends off the end of the page */
 64636  64777       *pRc = SQLITE_CORRUPT_PAGE(pPg);
 64637  64778     }
 64638         -
 64639  64779     return 0;
 64640  64780   }
 64641  64781   
 64642  64782   /*
 64643  64783   ** Allocate nByte bytes of space from within the B-Tree page passed
 64644  64784   ** as the first argument. Write into *pIdx the index into pPage->aData[]
 64645  64785   ** of the first byte of allocated space. Return either SQLITE_OK or
................................................................................
 64681  64821       if( top==0 && pPage->pBt->usableSize==65536 ){
 64682  64822         top = 65536;
 64683  64823       }else{
 64684  64824         return SQLITE_CORRUPT_PAGE(pPage);
 64685  64825       }
 64686  64826     }
 64687  64827   
 64688         -  /* If there is enough space between gap and top for one more cell pointer
 64689         -  ** array entry offset, and if the freelist is not empty, then search the
 64690         -  ** freelist looking for a free slot big enough to satisfy the request.
        64828  +  /* If there is enough space between gap and top for one more cell pointer,
        64829  +  ** and if the freelist is not empty, then search the
        64830  +  ** freelist looking for a slot big enough to satisfy the request.
 64691  64831     */
 64692  64832     testcase( gap+2==top );
 64693  64833     testcase( gap+1==top );
 64694  64834     testcase( gap==top );
 64695  64835     if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
 64696  64836       u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
 64697  64837       if( pSpace ){
................................................................................
 64705  64845   
 64706  64846     /* The request could not be fulfilled using a freelist slot.  Check
 64707  64847     ** to see if defragmentation is necessary.
 64708  64848     */
 64709  64849     testcase( gap+2+nByte==top );
 64710  64850     if( gap+2+nByte>top ){
 64711  64851       assert( pPage->nCell>0 || CORRUPT_DB );
        64852  +    assert( pPage->nFree>=0 );
 64712  64853       rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
 64713  64854       if( rc ) return rc;
 64714  64855       top = get2byteNotZero(&data[hdr+5]);
 64715  64856       assert( gap+2+nByte<=top );
 64716  64857     }
 64717  64858   
 64718  64859   
 64719  64860     /* Allocate memory from the gap in between the cell pointer array
 64720         -  ** and the cell content area.  The btreeInitPage() call has already
        64861  +  ** and the cell content area.  The btreeComputeFreeSpace() call has already
 64721  64862     ** validated the freelist.  Given that the freelist is valid, there
 64722  64863     ** is no way that the allocation can extend off the end of the page.
 64723  64864     ** The assert() below verifies the previous sentence.
 64724  64865     */
 64725  64866     top -= nByte;
 64726  64867     put2byte(&data[hdr+5], top);
 64727  64868     assert( top+nByte <= (int)pPage->pBt->usableSize );
................................................................................
 64732  64873   /*
 64733  64874   ** Return a section of the pPage->aData to the freelist.
 64734  64875   ** The first byte of the new free block is pPage->aData[iStart]
 64735  64876   ** and the size of the block is iSize bytes.
 64736  64877   **
 64737  64878   ** Adjacent freeblocks are coalesced.
 64738  64879   **
 64739         -** Note that even though the freeblock list was checked by btreeInitPage(),
        64880  +** Even though the freeblock list was checked by btreeComputeFreeSpace(),
 64740  64881   ** that routine will not detect overlap between cells or freeblocks.  Nor
 64741  64882   ** does it detect cells or freeblocks that encrouch into the reserved bytes
 64742  64883   ** at the end of the page.  So do additional corruption checks inside this
 64743  64884   ** routine and return SQLITE_CORRUPT if any problems are found.
 64744  64885   */
 64745  64886   static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
 64746  64887     u16 iPtr;                             /* Address of ptr to next freeblock */
................................................................................
 64894  65035       return SQLITE_CORRUPT_PAGE(pPage);
 64895  65036     }
 64896  65037     pPage->max1bytePayload = pBt->max1bytePayload;
 64897  65038     return SQLITE_OK;
 64898  65039   }
 64899  65040   
 64900  65041   /*
 64901         -** Initialize the auxiliary information for a disk block.
 64902         -**
 64903         -** Return SQLITE_OK on success.  If we see that the page does
 64904         -** not contain a well-formed database page, then return 
 64905         -** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
 64906         -** guarantee that the page is well-formed.  It only shows that
 64907         -** we failed to detect any corruption.
        65042  +** Compute the amount of freespace on the page.  In other words, fill
        65043  +** in the pPage->nFree field.
 64908  65044   */
 64909         -static int btreeInitPage(MemPage *pPage){
        65045  +static int btreeComputeFreeSpace(MemPage *pPage){
 64910  65046     int pc;            /* Address of a freeblock within pPage->aData[] */
 64911  65047     u8 hdr;            /* Offset to beginning of page header */
 64912  65048     u8 *data;          /* Equal to pPage->aData */
 64913         -  BtShared *pBt;        /* The main btree structure */
 64914  65049     int usableSize;    /* Amount of usable space on each page */
 64915         -  u16 cellOffset;    /* Offset from start of page to first cell pointer */
 64916  65050     int nFree;         /* Number of unused bytes on the page */
 64917  65051     int top;           /* First byte of the cell content area */
 64918  65052     int iCellFirst;    /* First allowable cell or freeblock offset */
 64919  65053     int iCellLast;     /* Last possible cell or freeblock offset */
 64920  65054   
 64921  65055     assert( pPage->pBt!=0 );
 64922  65056     assert( pPage->pBt->db!=0 );
 64923  65057     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 64924  65058     assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
 64925  65059     assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
 64926  65060     assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
 64927         -  assert( pPage->isInit==0 );
        65061  +  assert( pPage->isInit==1 );
        65062  +  assert( pPage->nFree<0 );
 64928  65063   
 64929         -  pBt = pPage->pBt;
        65064  +  usableSize = pPage->pBt->usableSize;
 64930  65065     hdr = pPage->hdrOffset;
 64931  65066     data = pPage->aData;
 64932         -  /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
 64933         -  ** the b-tree page type. */
 64934         -  if( decodeFlags(pPage, data[hdr]) ){
 64935         -    return SQLITE_CORRUPT_PAGE(pPage);
 64936         -  }
 64937         -  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 64938         -  pPage->maskPage = (u16)(pBt->pageSize - 1);
 64939         -  pPage->nOverflow = 0;
 64940         -  usableSize = pBt->usableSize;
 64941         -  pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
 64942         -  pPage->aDataEnd = &data[usableSize];
 64943         -  pPage->aCellIdx = &data[cellOffset];
 64944         -  pPage->aDataOfst = &data[pPage->childPtrSize];
 64945  65067     /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
 64946  65068     ** the start of the cell content area. A zero value for this integer is
 64947  65069     ** interpreted as 65536. */
 64948  65070     top = get2byteNotZero(&data[hdr+5]);
 64949         -  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
 64950         -  ** number of cells on the page. */
 64951         -  pPage->nCell = get2byte(&data[hdr+3]);
 64952         -  if( pPage->nCell>MX_CELL(pBt) ){
 64953         -    /* To many cells for a single page.  The page must be corrupt */
 64954         -    return SQLITE_CORRUPT_PAGE(pPage);
 64955         -  }
 64956         -  testcase( pPage->nCell==MX_CELL(pBt) );
 64957         -  /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
 64958         -  ** possible for a root page of a table that contains no rows) then the
 64959         -  ** offset to the cell content area will equal the page size minus the
 64960         -  ** bytes of reserved space. */
 64961         -  assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
 64962         -
 64963         -  /* A malformed database page might cause us to read past the end
 64964         -  ** of page when parsing a cell.  
 64965         -  **
 64966         -  ** The following block of code checks early to see if a cell extends
 64967         -  ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
 64968         -  ** returned if it does.
 64969         -  */
 64970         -  iCellFirst = cellOffset + 2*pPage->nCell;
        65071  +  iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
 64971  65072     iCellLast = usableSize - 4;
 64972         -  if( pBt->db->flags & SQLITE_CellSizeCk ){
 64973         -    int i;            /* Index into the cell pointer array */
 64974         -    int sz;           /* Size of a cell */
 64975         -
 64976         -    if( !pPage->leaf ) iCellLast--;
 64977         -    for(i=0; i<pPage->nCell; i++){
 64978         -      pc = get2byteAligned(&data[cellOffset+i*2]);
 64979         -      testcase( pc==iCellFirst );
 64980         -      testcase( pc==iCellLast );
 64981         -      if( pc<iCellFirst || pc>iCellLast ){
 64982         -        return SQLITE_CORRUPT_PAGE(pPage);
 64983         -      }
 64984         -      sz = pPage->xCellSize(pPage, &data[pc]);
 64985         -      testcase( pc+sz==usableSize );
 64986         -      if( pc+sz>usableSize ){
 64987         -        return SQLITE_CORRUPT_PAGE(pPage);
 64988         -      }
 64989         -    }
 64990         -    if( !pPage->leaf ) iCellLast++;
 64991         -  }  
 64992  65073   
 64993  65074     /* Compute the total free space on the page
 64994  65075     ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
 64995  65076     ** start of the first freeblock on the page, or is zero if there are no
 64996  65077     ** freeblocks. */
 64997  65078     pc = get2byte(&data[hdr+1]);
 64998  65079     nFree = data[hdr+7] + top;  /* Init nFree to non-freeblock free space */
................................................................................
 65032  65113     ** serves to verify that the offset to the start of the cell-content
 65033  65114     ** area, according to the page header, lies within the page.
 65034  65115     */
 65035  65116     if( nFree>usableSize ){
 65036  65117       return SQLITE_CORRUPT_PAGE(pPage);
 65037  65118     }
 65038  65119     pPage->nFree = (u16)(nFree - iCellFirst);
        65120  +  return SQLITE_OK;
        65121  +}
        65122  +
        65123  +/*
        65124  +** Do additional sanity check after btreeInitPage() if
        65125  +** PRAGMA cell_size_check=ON 
        65126  +*/
        65127  +static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
        65128  +  int iCellFirst;    /* First allowable cell or freeblock offset */
        65129  +  int iCellLast;     /* Last possible cell or freeblock offset */
        65130  +  int i;             /* Index into the cell pointer array */
        65131  +  int sz;            /* Size of a cell */
        65132  +  int pc;            /* Address of a freeblock within pPage->aData[] */
        65133  +  u8 *data;          /* Equal to pPage->aData */
        65134  +  int usableSize;    /* Maximum usable space on the page */
        65135  +  int cellOffset;    /* Start of cell content area */
        65136  +
        65137  +  iCellFirst = pPage->cellOffset + 2*pPage->nCell;
        65138  +  usableSize = pPage->pBt->usableSize;
        65139  +  iCellLast = usableSize - 4;
        65140  +  data = pPage->aData;
        65141  +  cellOffset = pPage->cellOffset;
        65142  +  if( !pPage->leaf ) iCellLast--;
        65143  +  for(i=0; i<pPage->nCell; i++){
        65144  +    pc = get2byteAligned(&data[cellOffset+i*2]);
        65145  +    testcase( pc==iCellFirst );
        65146  +    testcase( pc==iCellLast );
        65147  +    if( pc<iCellFirst || pc>iCellLast ){
        65148  +      return SQLITE_CORRUPT_PAGE(pPage);
        65149  +    }
        65150  +    sz = pPage->xCellSize(pPage, &data[pc]);
        65151  +    testcase( pc+sz==usableSize );
        65152  +    if( pc+sz>usableSize ){
        65153  +      return SQLITE_CORRUPT_PAGE(pPage);
        65154  +    }
        65155  +  }
        65156  +  return SQLITE_OK;
        65157  +}
        65158  +
        65159  +/*
        65160  +** Initialize the auxiliary information for a disk block.
        65161  +**
        65162  +** Return SQLITE_OK on success.  If we see that the page does
        65163  +** not contain a well-formed database page, then return 
        65164  +** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
        65165  +** guarantee that the page is well-formed.  It only shows that
        65166  +** we failed to detect any corruption.
        65167  +*/
        65168  +static int btreeInitPage(MemPage *pPage){
        65169  +  u8 *data;          /* Equal to pPage->aData */
        65170  +  BtShared *pBt;        /* The main btree structure */
        65171  +
        65172  +  assert( pPage->pBt!=0 );
        65173  +  assert( pPage->pBt->db!=0 );
        65174  +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
        65175  +  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
        65176  +  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
        65177  +  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
        65178  +  assert( pPage->isInit==0 );
        65179  +
        65180  +  pBt = pPage->pBt;
        65181  +  data = pPage->aData + pPage->hdrOffset;
        65182  +  /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
        65183  +  ** the b-tree page type. */
        65184  +  if( decodeFlags(pPage, data[0]) ){
        65185  +    return SQLITE_CORRUPT_PAGE(pPage);
        65186  +  }
        65187  +  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
        65188  +  pPage->maskPage = (u16)(pBt->pageSize - 1);
        65189  +  pPage->nOverflow = 0;
        65190  +  pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
        65191  +  pPage->aCellIdx = data + pPage->childPtrSize + 8;
        65192  +  pPage->aDataEnd = pPage->aData + pBt->usableSize;
        65193  +  pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
        65194  +  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
        65195  +  ** number of cells on the page. */
        65196  +  pPage->nCell = get2byte(&data[3]);
        65197  +  if( pPage->nCell>MX_CELL(pBt) ){
        65198  +    /* To many cells for a single page.  The page must be corrupt */
        65199  +    return SQLITE_CORRUPT_PAGE(pPage);
        65200  +  }
        65201  +  testcase( pPage->nCell==MX_CELL(pBt) );
        65202  +  /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
        65203  +  ** possible for a root page of a table that contains no rows) then the
        65204  +  ** offset to the cell content area will equal the page size minus the
        65205  +  ** bytes of reserved space. */
        65206  +  assert( pPage->nCell>0
        65207  +       || get2byteNotZero(&data[5])==(int)pBt->usableSize
        65208  +       || CORRUPT_DB );
        65209  +  pPage->nFree = -1;  /* Indicate that this value is yet uncomputed */
 65039  65210     pPage->isInit = 1;
        65211  +  if( pBt->db->flags & SQLITE_CellSizeCk ){
        65212  +    return btreeCellSizeCheck(pPage);
        65213  +  }
 65040  65214     return SQLITE_OK;
 65041  65215   }
 65042  65216   
 65043  65217   /*
 65044  65218   ** Set up a raw page so that it looks like a database page holding
 65045  65219   ** no entries.
 65046  65220   */
................................................................................
 65175  65349     assert( sqlite3_mutex_held(pBt->mutex) );
 65176  65350     assert( pCur==0 || ppPage==&pCur->pPage );
 65177  65351     assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
 65178  65352     assert( pCur==0 || pCur->iPage>0 );
 65179  65353   
 65180  65354     if( pgno>btreePagecount(pBt) ){
 65181  65355       rc = SQLITE_CORRUPT_BKPT;
 65182         -    goto getAndInitPage_error;
        65356  +    goto getAndInitPage_error1;
 65183  65357     }
 65184  65358     rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
 65185  65359     if( rc ){
 65186         -    goto getAndInitPage_error;
        65360  +    goto getAndInitPage_error1;
 65187  65361     }
 65188  65362     *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
 65189  65363     if( (*ppPage)->isInit==0 ){
 65190  65364       btreePageFromDbPage(pDbPage, pgno, pBt);
 65191  65365       rc = btreeInitPage(*ppPage);
 65192  65366       if( rc!=SQLITE_OK ){
 65193         -      releasePage(*ppPage);
 65194         -      goto getAndInitPage_error;
        65367  +      goto getAndInitPage_error2;
 65195  65368       }
 65196  65369     }
 65197  65370     assert( (*ppPage)->pgno==pgno );
 65198  65371     assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
 65199  65372   
 65200  65373     /* If obtaining a child page for a cursor, we must verify that the page is
 65201  65374     ** compatible with the root page. */
 65202  65375     if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
 65203  65376       rc = SQLITE_CORRUPT_PGNO(pgno);
 65204         -    releasePage(*ppPage);
 65205         -    goto getAndInitPage_error;
        65377  +    goto getAndInitPage_error2;
 65206  65378     }
 65207  65379     return SQLITE_OK;
 65208  65380   
 65209         -getAndInitPage_error:
        65381  +getAndInitPage_error2:
        65382  +  releasePage(*ppPage);
        65383  +getAndInitPage_error1:
 65210  65384     if( pCur ){
 65211  65385       pCur->iPage--;
 65212  65386       pCur->pPage = pCur->apPage[pCur->iPage];
 65213  65387     }
 65214  65388     testcase( pgno==0 );
 65215  65389     assert( pgno!=0 || rc==SQLITE_CORRUPT );
 65216  65390     return rc;
................................................................................
 68283  68457       assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
 68284  68458       *pRes = 1;
 68285  68459       rc = SQLITE_OK;
 68286  68460     }
 68287  68461     return rc;
 68288  68462   }
 68289  68463   
 68290         -/*
 68291         -** This function is a no-op if cursor pCur does not point to a valid row.
 68292         -** Otherwise, if pCur is valid, configure it so that the next call to
 68293         -** sqlite3BtreeNext() is a no-op.
 68294         -*/
 68295         -#ifndef SQLITE_OMIT_WINDOWFUNC
 68296         -SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor *pCur){
 68297         -  /* We believe that the cursor must always be in the valid state when
 68298         -  ** this routine is called, but the proof is difficult, so we add an
 68299         -  ** ALWaYS() test just in case we are wrong. */
 68300         -  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
 68301         -    pCur->eState = CURSOR_SKIPNEXT;
 68302         -    pCur->skipNext = 1;
 68303         -  }
 68304         -}
 68305         -#endif /* SQLITE_OMIT_WINDOWFUNC */
 68306         -
 68307  68464   /* Move the cursor to the last entry in the table.  Return SQLITE_OK
 68308  68465   ** on success.  Set *pRes to 0 if the cursor actually points to something
 68309  68466   ** or set *pRes to 1 if the table is empty.
 68310  68467   */
 68311  68468   SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
 68312  68469     int rc;
 68313  68470    
................................................................................
 68565  68722             pCur->ix = (u16)idx;
 68566  68723             rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
 68567  68724             pCur->curFlags &= ~BTCF_ValidOvfl;
 68568  68725             if( rc ){
 68569  68726               sqlite3_free(pCellKey);
 68570  68727               goto moveto_finish;
 68571  68728             }
 68572         -          c = xRecordCompare(nCell, pCellKey, pIdxKey);
        68729  +          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
 68573  68730             sqlite3_free(pCellKey);
 68574  68731           }
 68575  68732           assert( 
 68576  68733               (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
 68577  68734            && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
 68578  68735           );
 68579  68736           if( c<0 ){
................................................................................
 69197  69354   */
 69198  69355   static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
 69199  69356     MemPage *pTrunk = 0;                /* Free-list trunk page */
 69200  69357     Pgno iTrunk = 0;                    /* Page number of free-list trunk page */ 
 69201  69358     MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
 69202  69359     MemPage *pPage;                     /* Page being freed. May be NULL. */
 69203  69360     int rc;                             /* Return Code */
 69204         -  int nFree;                          /* Initial number of pages on free-list */
        69361  +  u32 nFree;                          /* Initial number of pages on free-list */
 69205  69362   
 69206  69363     assert( sqlite3_mutex_held(pBt->mutex) );
 69207  69364     assert( CORRUPT_DB || iPage>1 );
 69208  69365     assert( !pMemPage || pMemPage->pgno==iPage );
 69209  69366   
 69210         -  if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
        69367  +  if( iPage<2 || iPage>pBt->nPage ){
        69368  +    return SQLITE_CORRUPT_BKPT;
        69369  +  }
 69211  69370     if( pMemPage ){
 69212  69371       pPage = pMemPage;
 69213  69372       sqlite3PagerRef(pPage->pDbPage);
 69214  69373     }else{
 69215  69374       pPage = btreePageLookup(pBt, iPage);
 69216  69375     }
 69217  69376   
................................................................................
 69614  69773     int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
 69615  69774   
 69616  69775     if( *pRC ) return;
 69617  69776     assert( idx>=0 && idx<pPage->nCell );
 69618  69777     assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
 69619  69778     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 69620  69779     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
        69780  +  assert( pPage->nFree>=0 );
 69621  69781     data = pPage->aData;
 69622  69782     ptr = &pPage->aCellIdx[2*idx];
 69623  69783     pc = get2byte(ptr);
 69624  69784     hdr = pPage->hdrOffset;
 69625  69785     testcase( pc==get2byte(&data[hdr+5]) );
 69626  69786     testcase( pc+sz==pPage->pBt->usableSize );
 69627  69787     if( pc+sz > pPage->pBt->usableSize ){
................................................................................
 69684  69844     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 69685  69845     /* The cell should normally be sized correctly.  However, when moving a
 69686  69846     ** malformed cell from a leaf page to an interior page, if the cell size
 69687  69847     ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
 69688  69848     ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
 69689  69849     ** the term after the || in the following assert(). */
 69690  69850     assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
        69851  +  assert( pPage->nFree>=0 );
 69691  69852     if( pPage->nOverflow || sz+2>pPage->nFree ){
 69692  69853       if( pTemp ){
 69693  69854         memcpy(pTemp, pCell, sz);
 69694  69855         pCell = pTemp;
 69695  69856       }
 69696  69857       if( iChild ){
 69697  69858         put4byte(pCell, iChild);
................................................................................
 69741  69902       }
 69742  69903       pIns = pPage->aCellIdx + i*2;
 69743  69904       memmove(pIns+2, pIns, 2*(pPage->nCell - i));
 69744  69905       put2byte(pIns, idx);
 69745  69906       pPage->nCell++;
 69746  69907       /* increment the cell count */
 69747  69908       if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
 69748         -    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
        69909  +    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
 69749  69910   #ifndef SQLITE_OMIT_AUTOVACUUM
 69750  69911       if( pPage->pBt->autoVacuum ){
 69751  69912         /* The cell may contain a pointer to an overflow page. If so, write
 69752  69913         ** the entry for the overflow page into the pointer map.
 69753  69914         */
 69754  69915         ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
 69755  69916       }
................................................................................
 69828  69989   **    ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
 69829  69990   **    ixNx[4] = Total number of cells.
 69830  69991   **
 69831  69992   ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
 69832  69993   ** are used and they point to the leaf pages only, and the ixNx value are:
 69833  69994   **
 69834  69995   **    ixNx[0] = Number of cells in Child-1.
 69835         -**    ixNx[1] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
 69836         -**    ixNx[2] = Number of cells in Child-1 and Child-2 + both divider cells
        69996  +**    ixNx[1] = Number of cells in Child-1 and Child-2.
        69997  +**    ixNx[2] = Total number of cells.
        69998  +**
        69999  +** Sometimes when deleting, a child page can have zero cells.  In those
        70000  +** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
        70001  +** entries, shift down.  The end result is that each ixNx[] entry should
        70002  +** be larger than the previous
 69837  70003   */
 69838  70004   typedef struct CellArray CellArray;
 69839  70005   struct CellArray {
 69840  70006     int nCell;              /* Number of cells in apCell[] */
 69841  70007     MemPage *pRef;          /* Reference page */
 69842  70008     u8 **apCell;            /* All cells begin balanced */
 69843  70009     u16 *szCell;            /* Local size of all cells in apCell[] */
................................................................................
 70158  70324     }
 70159  70325   
 70160  70326     /* Add any overflow cells */
 70161  70327     for(i=0; i<pPg->nOverflow; i++){
 70162  70328       int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
 70163  70329       if( iCell>=0 && iCell<nNew ){
 70164  70330         pCellptr = &pPg->aCellIdx[iCell * 2];
 70165         -      assert( nCell>=iCell );
 70166         -      memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
        70331  +      if( nCell>iCell ){
        70332  +        memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
        70333  +      }
 70167  70334         nCell++;
 70168  70335         if( pageInsertArray(
 70169  70336               pPg, pBegin, &pData, pCellptr,
 70170  70337               iCell+iNew, 1, pCArray
 70171  70338         ) ) goto editpage_fail;
 70172  70339       }
 70173  70340     }
................................................................................
 70235  70402     MemPage *pNew;                       /* Newly allocated page */
 70236  70403     int rc;                              /* Return Code */
 70237  70404     Pgno pgnoNew;                        /* Page number of pNew */
 70238  70405   
 70239  70406     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 70240  70407     assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 70241  70408     assert( pPage->nOverflow==1 );
 70242         -
        70409  +  
 70243  70410     if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;  /* dbfuzz001.test */
        70411  +  assert( pPage->nFree>=0 );
        70412  +  assert( pParent->nFree>=0 );
 70244  70413   
 70245  70414     /* Allocate a new page. This page will become the right-sibling of 
 70246  70415     ** pPage. Make the parent page writable, so that the new divider cell
 70247  70416     ** may be inserted. If both these operations are successful, proceed.
 70248  70417     */
 70249  70418     rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
 70250  70419   
................................................................................
 70406  70575       /* Reinitialize page pTo so that the contents of the MemPage structure
 70407  70576       ** match the new data. The initialization of pTo can actually fail under
 70408  70577       ** fairly obscure circumstances, even though it is a copy of initialized 
 70409  70578       ** page pFrom.
 70410  70579       */
 70411  70580       pTo->isInit = 0;
 70412  70581       rc = btreeInitPage(pTo);
        70582  +    if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo);
 70413  70583       if( rc!=SQLITE_OK ){
 70414  70584         *pRC = rc;
 70415  70585         return;
 70416  70586       }
 70417  70587     
 70418  70588       /* If this is an auto-vacuum database, update the pointer-map entries
 70419  70589       ** for any b-tree or overflow pages that pTo now contains the pointers to.
................................................................................
 70514  70684     */
 70515  70685     assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
 70516  70686     assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
 70517  70687   
 70518  70688     if( !aOvflSpace ){
 70519  70689       return SQLITE_NOMEM_BKPT;
 70520  70690     }
        70691  +  assert( pParent->nFree>=0 );
 70521  70692   
 70522  70693     /* Find the sibling pages to balance. Also locate the cells in pParent 
 70523  70694     ** that divide the siblings. An attempt is made to find NN siblings on 
 70524  70695     ** either side of pPage. More siblings are taken from one side, however, 
 70525  70696     ** if there are fewer than NN siblings on the other side. If pParent
 70526  70697     ** has NB or fewer children then all children of pParent are taken.  
 70527  70698     **
................................................................................
 70553  70724     pgno = get4byte(pRight);
 70554  70725     while( 1 ){
 70555  70726       rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
 70556  70727       if( rc ){
 70557  70728         memset(apOld, 0, (i+1)*sizeof(MemPage*));
 70558  70729         goto balance_cleanup;
 70559  70730       }
 70560         -    nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
        70731  +    if( apOld[i]->nFree<0 ){
        70732  +      rc = btreeComputeFreeSpace(apOld[i]);
        70733  +      if( rc ){
        70734  +        memset(apOld, 0, (i)*sizeof(MemPage*));
        70735  +        goto balance_cleanup;
        70736  +      }
        70737  +    }
 70561  70738       if( (i--)==0 ) break;
 70562  70739   
 70563  70740       if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
 70564  70741         apDiv[i] = pParent->apOvfl[0];
 70565  70742         pgno = get4byte(apDiv[i]);
 70566  70743         szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
 70567  70744         pParent->nOverflow = 0;
................................................................................
 70597  70774         }
 70598  70775         dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
 70599  70776       }
 70600  70777     }
 70601  70778   
 70602  70779     /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
 70603  70780     ** alignment */
        70781  +  nMaxCells = nOld*(MX_CELL(pBt) + ArraySize(pParent->apOvfl));
 70604  70782     nMaxCells = (nMaxCells + 3)&~3;
 70605  70783   
 70606  70784     /*
 70607  70785     ** Allocate space for memory structures
 70608  70786     */
 70609  70787     szScratch =
 70610  70788          nMaxCells*sizeof(u8*)                       /* b.apCell */
 70611  70789        + nMaxCells*sizeof(u16)                       /* b.szCell */
 70612  70790        + pBt->pageSize;                              /* aSpace1 */
 70613  70791   
 70614         -  assert( szScratch<=6*(int)pBt->pageSize );
        70792  +  assert( szScratch<=7*(int)pBt->pageSize );
 70615  70793     b.apCell = sqlite3StackAllocRaw(0, szScratch );
 70616  70794     if( b.apCell==0 ){
 70617  70795       rc = SQLITE_NOMEM_BKPT;
 70618  70796       goto balance_cleanup;
 70619  70797     }
 70620  70798     b.szCell = (u16*)&b.apCell[nMaxCells];
 70621  70799     aSpace1 = (u8*)&b.szCell[nMaxCells];
................................................................................
 70747  70925     ** 
 70748  70926     */
 70749  70927     usableSpace = pBt->usableSize - 12 + leafCorrection;
 70750  70928     for(i=k=0; i<nOld; i++, k++){
 70751  70929       MemPage *p = apOld[i];
 70752  70930       b.apEnd[k] = p->aDataEnd;
 70753  70931       b.ixNx[k] = cntOld[i];
        70932  +    if( k && b.ixNx[k]==b.ixNx[k-1] ){
        70933  +      k--;  /* Omit b.ixNx[] entry for child pages with no cells */
        70934  +    }
 70754  70935       if( !leafData ){
 70755  70936         k++;
 70756  70937         b.apEnd[k] = pParent->aDataEnd;
 70757  70938         b.ixNx[k] = cntOld[i]+1;
 70758  70939       }
        70940  +    assert( p->nFree>=0 );
 70759  70941       szNew[i] = usableSpace - p->nFree;
 70760  70942       for(j=0; j<p->nOverflow; j++){
 70761  70943         szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
 70762  70944       }
 70763  70945       cntNew[i] = cntOld[i];
 70764  70946     }
 70765  70947     k = nOld;
................................................................................
 70977  71159     ** associated with the right-child of each sibling may also need to be 
 70978  71160     ** updated. This happens below, after the sibling pages have been 
 70979  71161     ** populated, not here.
 70980  71162     */
 70981  71163     if( ISAUTOVACUUM ){
 70982  71164       MemPage *pOld;
 70983  71165       MemPage *pNew = pOld = apNew[0];
 70984         -    u8 *aOld = pNew->aData;
 70985  71166       int cntOldNext = pNew->nCell + pNew->nOverflow;
 70986         -    int usableSize = pBt->usableSize;
 70987  71167       int iNew = 0;
 70988  71168       int iOld = 0;
 70989  71169   
 70990  71170       for(i=0; i<b.nCell; i++){
 70991  71171         u8 *pCell = b.apCell[i];
 70992         -      if( i==cntOldNext ){
 70993         -        pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
        71172  +      while( i==cntOldNext ){
        71173  +        iOld++;
        71174  +        assert( iOld<nNew || iOld<nOld );
        71175  +        pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
 70994  71176           cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
 70995         -        aOld = pOld->aData;
 70996  71177         }
 70997  71178         if( i==cntNew[iNew] ){
 70998  71179           pNew = apNew[++iNew];
 70999  71180           if( !leafData ) continue;
 71000  71181         }
 71001  71182   
 71002  71183         /* Cell pCell is destined for new sibling page pNew. Originally, it
................................................................................
 71003  71184         ** was either part of sibling page iOld (possibly an overflow cell), 
 71004  71185         ** or else the divider cell to the left of sibling page iOld. So,
 71005  71186         ** if sibling page iOld had the same page number as pNew, and if
 71006  71187         ** pCell really was a part of sibling page iOld (not a divider or
 71007  71188         ** overflow cell), we can skip updating the pointer map entries.  */
 71008  71189         if( iOld>=nNew
 71009  71190          || pNew->pgno!=aPgno[iOld]
 71010         -       || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize])
        71191  +       || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
 71011  71192         ){
 71012  71193           if( !leafCorrection ){
 71013  71194             ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
 71014  71195           }
 71015  71196           if( cachedCellSize(&b,i)>pNew->minLocal ){
 71016  71197             ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
 71017  71198           }
................................................................................
 71154  71335       ** by smaller than the child due to the database header, and so all the
 71155  71336       ** free space needs to be up front.
 71156  71337       */
 71157  71338       assert( nNew==1 || CORRUPT_DB );
 71158  71339       rc = defragmentPage(apNew[0], -1);
 71159  71340       testcase( rc!=SQLITE_OK );
 71160  71341       assert( apNew[0]->nFree == 
 71161         -        (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
        71342  +        (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
        71343  +          - apNew[0]->nCell*2)
 71162  71344         || rc!=SQLITE_OK
 71163  71345       );
 71164  71346       copyNodeContent(apNew[0], pParent, &rc);
 71165  71347       freePage(apNew[0], &rc);
 71166  71348     }else if( ISAUTOVACUUM && !leafCorrection ){
 71167  71349       /* Fix the pointer map entries associated with the right-child of each
 71168  71350       ** sibling page. All other pointer map entries have already been taken
................................................................................
 71253  71435     if( rc ){
 71254  71436       *ppChild = 0;
 71255  71437       releasePage(pChild);
 71256  71438       return rc;
 71257  71439     }
 71258  71440     assert( sqlite3PagerIswriteable(pChild->pDbPage) );
 71259  71441     assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
 71260         -  assert( pChild->nCell==pRoot->nCell );
        71442  +  assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
 71261  71443   
 71262  71444     TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
 71263  71445   
 71264  71446     /* Copy the overflow cells from pRoot to pChild */
 71265  71447     memcpy(pChild->aiOvfl, pRoot->aiOvfl,
 71266  71448            pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
 71267  71449     memcpy(pChild->apOvfl, pRoot->apOvfl,
................................................................................
 71295  71477     VVA_ONLY( int balance_quick_called = 0 );
 71296  71478     VVA_ONLY( int balance_deeper_called = 0 );
 71297  71479   
 71298  71480     do {
 71299  71481       int iPage = pCur->iPage;
 71300  71482       MemPage *pPage = pCur->pPage;
 71301  71483   
        71484  +    if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
 71302  71485       if( iPage==0 ){
 71303  71486         if( pPage->nOverflow ){
 71304  71487           /* The root page of the b-tree is overfull. In this case call the
 71305  71488           ** balance_deeper() function to create a new child for the root-page
 71306  71489           ** and copy the current contents of the root-page to it. The
 71307  71490           ** next iteration of the do-loop will balance the child page.
 71308  71491           */ 
................................................................................
 71323  71506       }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
 71324  71507         break;
 71325  71508       }else{
 71326  71509         MemPage * const pParent = pCur->apPage[iPage-1];
 71327  71510         int const iIdx = pCur->aiIdx[iPage-1];
 71328  71511   
 71329  71512         rc = sqlite3PagerWrite(pParent->pDbPage);
        71513  +      if( rc==SQLITE_OK && pParent->nFree<0 ){
        71514  +        rc = btreeComputeFreeSpace(pParent);
        71515  +      }
 71330  71516         if( rc==SQLITE_OK ){
 71331  71517   #ifndef SQLITE_OMIT_QUICKBALANCE
 71332  71518           if( pPage->intKeyLeaf
 71333  71519            && pPage->nOverflow==1
 71334  71520            && pPage->aiOvfl[0]==pPage->nCell
 71335  71521            && pParent->pgno!=1
 71336  71522            && pParent->nCell==iIdx
................................................................................
 71669  71855   
 71670  71856     }
 71671  71857     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 71672  71858   
 71673  71859     pPage = pCur->pPage;
 71674  71860     assert( pPage->intKey || pX->nKey>=0 );
 71675  71861     assert( pPage->leaf || !pPage->intKey );
        71862  +  if( pPage->nFree<0 ){
        71863  +    rc = btreeComputeFreeSpace(pPage);
        71864  +    if( rc ) return rc;
        71865  +  }
 71676  71866   
 71677  71867     TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
 71678  71868             pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
 71679  71869             loc==0 ? "overwrite" : "new entry"));
 71680  71870     assert( pPage->isInit );
 71681  71871     newCell = pBt->pTmpSpace;
 71682  71872     assert( newCell!=0 );
................................................................................
 71811  72001   
 71812  72002     assert( cursorOwnsBtShared(pCur) );
 71813  72003     assert( pBt->inTransaction==TRANS_WRITE );
 71814  72004     assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 71815  72005     assert( pCur->curFlags & BTCF_WriteFlag );
 71816  72006     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 71817  72007     assert( !hasReadConflicts(p, pCur->pgnoRoot) );
 71818         -  assert( pCur->ix<pCur->pPage->nCell );
 71819         -  assert( pCur->eState==CURSOR_VALID );
 71820  72008     assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
        72009  +  if( pCur->eState==CURSOR_REQUIRESEEK ){
        72010  +    rc = btreeRestoreCursorPosition(pCur);
        72011  +    if( rc ) return rc;
        72012  +  }
        72013  +  assert( pCur->eState==CURSOR_VALID );
 71821  72014   
 71822  72015     iCellDepth = pCur->iPage;
 71823  72016     iCellIdx = pCur->ix;
 71824  72017     pPage = pCur->pPage;
 71825  72018     pCell = findCell(pPage, iCellIdx);
        72019  +  if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT;
 71826  72020   
 71827  72021     /* If the bPreserve flag is set to true, then the cursor position must
 71828  72022     ** be preserved following this delete operation. If the current delete
 71829  72023     ** will cause a b-tree rebalance, then this is done by saving the cursor
 71830  72024     ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
 71831  72025     ** returning. 
 71832  72026     **
................................................................................
 71889  72083     ** node to replace the deleted cell.  */
 71890  72084     if( !pPage->leaf ){
 71891  72085       MemPage *pLeaf = pCur->pPage;
 71892  72086       int nCell;
 71893  72087       Pgno n;
 71894  72088       unsigned char *pTmp;
 71895  72089   
        72090  +    if( pLeaf->nFree<0 ){
        72091  +      rc = btreeComputeFreeSpace(pLeaf);
        72092  +      if( rc ) return rc;
        72093  +    }
 71896  72094       if( iCellDepth<pCur->iPage-1 ){
 71897  72095         n = pCur->apPage[iCellDepth+1]->pgno;
 71898  72096       }else{
 71899  72097         n = pCur->pPage->pgno;
 71900  72098       }
 71901  72099       pCell = findCell(pLeaf, pLeaf->nCell-1);
 71902  72100       if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
................................................................................
 72247  72445     int rc;
 72248  72446     MemPage *pPage = 0;
 72249  72447     BtShared *pBt = p->pBt;
 72250  72448   
 72251  72449     assert( sqlite3BtreeHoldsMutex(p) );
 72252  72450     assert( p->inTrans==TRANS_WRITE );
 72253  72451     assert( iTable>=2 );
        72452  +  if( iTable>btreePagecount(pBt) ){
        72453  +    return SQLITE_CORRUPT_BKPT;
        72454  +  }
 72254  72455   
 72255  72456     rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
 72256  72457     if( rc ) return rc;
 72257  72458     rc = sqlite3BtreeClearTable(p, iTable, 0);
 72258  72459     if( rc ){
 72259  72460       releasePage(pPage);
 72260  72461       return rc;
................................................................................
 72595  72796   ** Check the integrity of the freelist or of an overflow page list.
 72596  72797   ** Verify that the number of pages on the list is N.
 72597  72798   */
 72598  72799   static void checkList(
 72599  72800     IntegrityCk *pCheck,  /* Integrity checking context */
 72600  72801     int isFreeList,       /* True for a freelist.  False for overflow page list */
 72601  72802     int iPage,            /* Page number for first page in the list */
 72602         -  int N                 /* Expected number of pages in the list */
        72803  +  u32 N                 /* Expected number of pages in the list */
 72603  72804   ){
 72604  72805     int i;
 72605         -  int expected = N;
        72806  +  u32 expected = N;
 72606  72807     int nErrAtStart = pCheck->nErr;
 72607  72808     while( iPage!=0 && pCheck->mxErr ){
 72608  72809       DbPage *pOvflPage;
 72609  72810       unsigned char *pOvflData;
 72610  72811       if( checkRef(pCheck, iPage) ) break;
 72611  72812       N--;
 72612  72813       if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
................................................................................
 72780  72981     pPage->isInit = 0;
 72781  72982     if( (rc = btreeInitPage(pPage))!=0 ){
 72782  72983       assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
 72783  72984       checkAppendMsg(pCheck,
 72784  72985                      "btreeInitPage() returns error code %d", rc);
 72785  72986       goto end_of_check;
 72786  72987     }
        72988  +  if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
        72989  +    assert( rc==SQLITE_CORRUPT );
        72990  +    checkAppendMsg(pCheck, "free space corruption", rc);
        72991  +    goto end_of_check;
        72992  +  }
 72787  72993     data = pPage->aData;
 72788  72994     hdr = pPage->hdrOffset;
 72789  72995   
 72790  72996     /* Set up for cell analysis */
 72791  72997     pCheck->zPfx = "On tree page %d cell %d: ";
 72792  72998     contentOffset = get2byteNotZero(&data[hdr+5]);
 72793  72999     assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
................................................................................
 72852  73058         }
 72853  73059         maxKey = info.nKey;
 72854  73060         keyCanBeEqual = 0;     /* Only the first key on the page may ==maxKey */
 72855  73061       }
 72856  73062   
 72857  73063       /* Check the content overflow list */
 72858  73064       if( info.nPayload>info.nLocal ){
 72859         -      int nPage;       /* Number of pages on the overflow chain */
        73065  +      u32 nPage;       /* Number of pages on the overflow chain */
 72860  73066         Pgno pgnoOvfl;   /* First page of the overflow chain */
 72861  73067         assert( pc + info.nSize - 4 <= usableSize );
 72862  73068         nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
 72863  73069         pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
 72864  73070   #ifndef SQLITE_OMIT_AUTOVACUUM
 72865  73071         if( pBt->autoVacuum ){
 72866  73072           checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
................................................................................
 72912  73118       ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
 72913  73119       ** is the offset of the first freeblock, or zero if there are no
 72914  73120       ** freeblocks on the page. 
 72915  73121       */
 72916  73122       i = get2byte(&data[hdr+1]);
 72917  73123       while( i>0 ){
 72918  73124         int size, j;
 72919         -      assert( (u32)i<=usableSize-4 );     /* Enforced by btreeInitPage() */
        73125  +      assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
 72920  73126         size = get2byte(&data[i+2]);
 72921         -      assert( (u32)(i+size)<=usableSize );  /* Enforced by btreeInitPage() */
        73127  +      assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
 72922  73128         btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
 72923  73129         /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
 72924  73130         ** big-endian integer which is the offset in the b-tree page of the next
 72925  73131         ** freeblock in the chain, or zero if the freeblock is the last on the
 72926  73132         ** chain. */
 72927  73133         j = get2byte(&data[i]);
 72928  73134         /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
 72929  73135         ** increasing offset. */
 72930         -      assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
 72931         -      assert( (u32)j<=usableSize-4 );   /* Enforced by btreeInitPage() */
        73136  +      assert( j==0 || j>i+size );     /* Enforced by btreeComputeFreeSpace() */
        73137  +      assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
 72932  73138         i = j;
 72933  73139       }
 72934  73140       /* Analyze the min-heap looking for overlap between cells and/or 
 72935  73141       ** freeblocks, and counting the number of untracked bytes in nFrag.
 72936  73142       ** 
 72937  73143       ** Each min-heap entry is of the form:    (start_address<<16)|end_address.
 72938  73144       ** There is an implied first entry the covers the page header, the cell
................................................................................
 74268  74474         /* This is a pointer type.  There may be a flag to indicate what to
 74269  74475         ** do with the pointer. */
 74270  74476         assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
 74271  74477                 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
 74272  74478                 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
 74273  74479   
 74274  74480         /* No other bits set */
 74275         -      assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype
        74481  +      assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
 74276  74482                              |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
 74277  74483       }else{
 74278  74484         /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
 74279  74485         ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
 74280  74486       }
 74281  74487     }else{
 74282  74488       /* The MEM_Cleared bit is only allowed on NULLs */
................................................................................
 74389  74595     assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
 74390  74596     assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
 74391  74597     return rc;
 74392  74598   #endif
 74393  74599   }
 74394  74600   
 74395  74601   /*
 74396         -** Make sure pMem->z points to a writable allocation of at least 
 74397         -** min(n,32) bytes.
        74602  +** Make sure pMem->z points to a writable allocation of at least n bytes.
 74398  74603   **
 74399  74604   ** If the bPreserve argument is true, then copy of the content of
 74400  74605   ** pMem->z into the new allocation.  pMem must be either a string or
 74401  74606   ** blob if bPreserve is true.  If bPreserve is false, any prior content
 74402  74607   ** in pMem->z is discarded.
 74403  74608   */
 74404  74609   SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
................................................................................
 74409  74614     /* If the bPreserve flag is set to true, then the memory cell must already
 74410  74615     ** contain a valid string or blob value.  */
 74411  74616     assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
 74412  74617     testcase( bPreserve && pMem->z==0 );
 74413  74618   
 74414  74619     assert( pMem->szMalloc==0
 74415  74620          || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
 74416         -  if( n<32 ) n = 32;
 74417  74621     if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
 74418  74622       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 74419  74623       bPreserve = 0;
 74420  74624     }else{
 74421  74625       if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
 74422  74626       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
 74423  74627     }
................................................................................
 74511  74715   ** If the given Mem* has a zero-filled tail, turn it into an ordinary
 74512  74716   ** blob stored in dynamically allocated space.
 74513  74717   */
 74514  74718   #ifndef SQLITE_OMIT_INCRBLOB
 74515  74719   SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
 74516  74720     int nByte;
 74517  74721     assert( pMem->flags & MEM_Zero );
 74518         -  assert( pMem->flags&MEM_Blob );
        74722  +  assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
        74723  +  testcase( sqlite3_value_nochange(pMem) );
 74519  74724     assert( !sqlite3VdbeMemIsRowSet(pMem) );
 74520  74725     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 74521  74726   
 74522  74727     /* Set nByte to the number of bytes required to store the expanded blob. */
 74523  74728     nByte = pMem->n + pMem->u.nZero;
 74524  74729     if( nByte<=0 ){
        74730  +    if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
 74525  74731       nByte = 1;
 74526  74732     }
 74527  74733     if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
 74528  74734       return SQLITE_NOMEM_BKPT;
 74529  74735     }
 74530  74736   
 74531  74737     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
................................................................................
 75258  75464       iLimit = SQLITE_MAX_LENGTH;
 75259  75465     }
 75260  75466     flags = (enc==0?MEM_Blob:MEM_Str);
 75261  75467     if( nByte<0 ){
 75262  75468       assert( enc!=0 );
 75263  75469       if( enc==SQLITE_UTF8 ){
 75264  75470         nByte = 0x7fffffff & (int)strlen(z);
 75265         -      if( nByte>iLimit ) nByte = iLimit+1;
 75266  75471       }else{
 75267  75472         for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
 75268  75473       }
 75269  75474       flags |= MEM_Term;
 75270  75475     }
 75271  75476   
 75272  75477     /* The following block sets the new values of Mem.z and Mem.xDel. It
 75273  75478     ** also sets a flag in local variable "flags" to indicate the memory
 75274  75479     ** management (one of MEM_Dyn or MEM_Static).
 75275  75480     */
 75276  75481     if( xDel==SQLITE_TRANSIENT ){
 75277         -    int nAlloc = nByte;
        75482  +    u32 nAlloc = nByte;
 75278  75483       if( flags&MEM_Term ){
 75279  75484         nAlloc += (enc==SQLITE_UTF8?1:2);
 75280  75485       }
 75281  75486       if( nByte>iLimit ){
 75282         -      return SQLITE_TOOBIG;
        75487  +      return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
 75283  75488       }
 75284  75489       testcase( nAlloc==0 );
 75285  75490       testcase( nAlloc==31 );
 75286  75491       testcase( nAlloc==32 );
 75287         -    if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
        75492  +    if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
 75288  75493         return SQLITE_NOMEM_BKPT;
 75289  75494       }
 75290  75495       memcpy(pMem->z, z, nAlloc);
 75291         -  }else if( xDel==SQLITE_DYNAMIC ){
 75292         -    sqlite3VdbeMemRelease(pMem);
 75293         -    pMem->zMalloc = pMem->z = (char *)z;
 75294         -    pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
 75295  75496     }else{
 75296  75497       sqlite3VdbeMemRelease(pMem);
 75297  75498       pMem->z = (char *)z;
 75298         -    pMem->xDel = xDel;
 75299         -    flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
        75499  +    if( xDel==SQLITE_DYNAMIC ){
        75500  +      pMem->zMalloc = pMem->z;
        75501  +      pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
        75502  +    }else{
        75503  +      pMem->xDel = xDel;
        75504  +      flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
        75505  +    }
 75300  75506     }
 75301  75507   
 75302  75508     pMem->n = nByte;
 75303  75509     pMem->flags = flags;
 75304  75510     pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
 75305  75511   
 75306  75512   #ifndef SQLITE_OMIT_UTF16
................................................................................
 76260  76466     ** more frequent reallocs and hence provide more opportunities for 
 76261  76467     ** simulated OOM faults.  SQLITE_TEST_REALLOC_STRESS is generally used
 76262  76468     ** during testing only.  With SQLITE_TEST_REALLOC_STRESS grow the op array
 76263  76469     ** by the minimum* amount required until the size reaches 512.  Normal
 76264  76470     ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
 76265  76471     ** size of the op array or add 1KB of space, whichever is smaller. */
 76266  76472   #ifdef SQLITE_TEST_REALLOC_STRESS
 76267         -  int nNew = (v->nOpAlloc>=512 ? v->nOpAlloc*2 : v->nOpAlloc+nOp);
        76473  +  sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
        76474  +                        : (sqlite3_int64)v->nOpAlloc+nOp);
 76268  76475   #else
 76269         -  int nNew = (v->nOpAlloc ? v->nOpAlloc*2 : (int)(1024/sizeof(Op)));
        76476  +  sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
        76477  +                        : (sqlite3_int64)(1024/sizeof(Op)));
 76270  76478     UNUSED_PARAMETER(nOp);
 76271  76479   #endif
 76272  76480   
 76273  76481     /* Ensure that the size of a VDBE does not grow too large */
 76274  76482     if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
 76275  76483       sqlite3OomFault(p->db);
 76276  76484       return SQLITE_NOMEM;
................................................................................
 76742  76950     memset(&sIter, 0, sizeof(sIter));
 76743  76951     sIter.v = v;
 76744  76952   
 76745  76953     while( (pOp = opIterNext(&sIter))!=0 ){
 76746  76954       int opcode = pOp->opcode;
 76747  76955       if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
 76748  76956        || opcode==OP_VDestroy
        76957  +     || (opcode==OP_Function0 && pOp->p4.pFunc->funcFlags&SQLITE_FUNC_INTERNAL)
 76749  76958        || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
 76750  76959         && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
 76751  76960       ){
 76752  76961         hasAbort = 1;
 76753  76962         break;
 76754  76963       }
 76755  76964       if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
................................................................................
 77049  77258     Vdbe *p,                        /* VM to add scanstatus() to */
 77050  77259     int addrExplain,                /* Address of OP_Explain (or 0) */
 77051  77260     int addrLoop,                   /* Address of loop counter */ 
 77052  77261     int addrVisit,                  /* Address of rows visited counter */
 77053  77262     LogEst nEst,                    /* Estimated number of output rows */
 77054  77263     const char *zName               /* Name of table or index being scanned */
 77055  77264   ){
 77056         -  int nByte = (p->nScan+1) * sizeof(ScanStatus);
        77265  +  sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
 77057  77266     ScanStatus *aNew;
 77058  77267     aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
 77059  77268     if( aNew ){
 77060  77269       ScanStatus *pNew = &aNew[p->nScan++];
 77061  77270       pNew->addrExplain = addrExplain;
 77062  77271       pNew->addrLoop = addrLoop;
 77063  77272       pNew->addrVisit = addrVisit;
................................................................................
 78170  78379   #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
 78171  78380   
 78172  78381   /* An instance of this object describes bulk memory available for use
 78173  78382   ** by subcomponents of a prepared statement.  Space is allocated out
 78174  78383   ** of a ReusableSpace object by the allocSpace() routine below.
 78175  78384   */
 78176  78385   struct ReusableSpace {
 78177         -  u8 *pSpace;          /* Available memory */
 78178         -  int nFree;           /* Bytes of available memory */
 78179         -  int nNeeded;         /* Total bytes that could not be allocated */
        78386  +  u8 *pSpace;            /* Available memory */
        78387  +  sqlite3_int64 nFree;   /* Bytes of available memory */
        78388  +  sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
 78180  78389   };
 78181  78390   
 78182  78391   /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
 78183  78392   ** from the ReusableSpace object.  Return a pointer to the allocated
 78184  78393   ** memory on success.  If insufficient memory is available in the
 78185  78394   ** ReusableSpace object, increase the ReusableSpace.nNeeded
 78186  78395   ** value by the amount needed and return NULL.
................................................................................
 78192  78401   ** This allocator is employed to repurpose unused slots at the end of the
 78193  78402   ** opcode array of prepared state for other memory needs of the prepared
 78194  78403   ** statement.
 78195  78404   */
 78196  78405   static void *allocSpace(
 78197  78406     struct ReusableSpace *p,  /* Bulk memory available for allocation */
 78198  78407     void *pBuf,               /* Pointer to a prior allocation */
 78199         -  int nByte                 /* Bytes of memory needed */
        78408  +  sqlite3_int64 nByte       /* Bytes of memory needed */
 78200  78409   ){
 78201  78410     assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
 78202  78411     if( pBuf==0 ){
 78203  78412       nByte = ROUND8(nByte);
 78204  78413       if( nByte <= p->nFree ){
 78205  78414         p->nFree -= nByte;
 78206  78415         pBuf = &p->pSpace[p->nFree];
................................................................................
 81149  81358     sqlite3_int64 iElapse;
 81150  81359     assert( p->startTime>0 );
 81151  81360     assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
 81152  81361     assert( db->init.busy==0 );
 81153  81362     assert( p->zSql!=0 );
 81154  81363     sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
 81155  81364     iElapse = (iNow - p->startTime)*1000000;
 81156         -#ifndef SQLITE_OMIT_DEPRECATED  	
        81365  +#ifndef SQLITE_OMIT_DEPRECATED
 81157  81366     if( db->xProfile ){
 81158  81367       db->xProfile(db->pProfileArg, p->zSql, iElapse);
 81159  81368     }
 81160  81369   #endif
 81161  81370     if( db->mTrace & SQLITE_TRACE_PROFILE ){
 81162  81371       db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
 81163  81372     }
................................................................................
 81356  81565     return aType[pVal->flags&MEM_AffMask];
 81357  81566   }
 81358  81567   
 81359  81568   /* Return true if a parameter to xUpdate represents an unchanged column */
 81360  81569   SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
 81361  81570     return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
 81362  81571   }
        81572  +
        81573  +/* Return true if a parameter value originated from an sqlite3_bind() */
        81574  +SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
        81575  +  return (pVal->flags&MEM_FromBind)!=0;
        81576  +}
 81363  81577   
 81364  81578   /* Make a copy of an sqlite3_value object
 81365  81579   */
 81366  81580   SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
 81367  81581     sqlite3_value *pNew;
 81368  81582     if( pOrig==0 ) return 0;
 81369  81583     pNew = sqlite3_malloc( sizeof(*pNew) );
................................................................................
 82202  82416   **    3      The name of the table that the column derives from
 82203  82417   **    4      The name of the table column that the result column derives from
 82204  82418   **
 82205  82419   ** If the result is not a simple column reference (if it is an expression
 82206  82420   ** or a constant) then useTypes 2, 3, and 4 return NULL.
 82207  82421   */
 82208  82422   static const void *columnName(
 82209         -  sqlite3_stmt *pStmt,
 82210         -  int N,
 82211         -  const void *(*xFunc)(Mem*),
 82212         -  int useType
        82423  +  sqlite3_stmt *pStmt,     /* The statement */
        82424  +  int N,                   /* Which column to get the name for */
        82425  +  int useUtf16,            /* True to return the name as UTF16 */
        82426  +  int useType              /* What type of name */
 82213  82427   ){
 82214  82428     const void *ret;
 82215  82429     Vdbe *p;
 82216  82430     int n;
 82217  82431     sqlite3 *db;
 82218  82432   #ifdef SQLITE_ENABLE_API_ARMOR
 82219  82433     if( pStmt==0 ){
................................................................................
 82226  82440     db = p->db;
 82227  82441     assert( db!=0 );
 82228  82442     n = sqlite3_column_count(pStmt);
 82229  82443     if( N<n && N>=0 ){
 82230  82444       N += useType*n;
 82231  82445       sqlite3_mutex_enter(db->mutex);
 82232  82446       assert( db->mallocFailed==0 );
 82233         -    ret = xFunc(&p->aColName[N]);
 82234         -     /* A malloc may have failed inside of the xFunc() call. If this
        82447  +#ifndef SQLITE_OMIT_UTF16
        82448  +    if( useUtf16 ){
        82449  +      ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
        82450  +    }else
        82451  +#endif
        82452  +    {
        82453  +      ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
        82454  +    }
        82455  +    /* A malloc may have failed inside of the _text() call. If this
 82235  82456       ** is the case, clear the mallocFailed flag and return NULL.
 82236  82457       */
 82237  82458       if( db->mallocFailed ){
 82238  82459         sqlite3OomClear(db);
 82239  82460         ret = 0;
 82240  82461       }
 82241  82462       sqlite3_mutex_leave(db->mutex);
................................................................................
 82244  82465   }
 82245  82466   
 82246  82467   /*
 82247  82468   ** Return the name of the Nth column of the result set returned by SQL
 82248  82469   ** statement pStmt.
 82249  82470   */
 82250  82471   SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
 82251         -  return columnName(
 82252         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
        82472  +  return columnName(pStmt, N, 0, COLNAME_NAME);
 82253  82473   }
 82254  82474   #ifndef SQLITE_OMIT_UTF16
 82255  82475   SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
 82256         -  return columnName(
 82257         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
        82476  +  return columnName(pStmt, N, 1, COLNAME_NAME);
 82258  82477   }
 82259  82478   #endif
 82260  82479   
 82261  82480   /*
 82262  82481   ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
 82263  82482   ** not define OMIT_DECLTYPE.
 82264  82483   */
................................................................................
 82269  82488   
 82270  82489   #ifndef SQLITE_OMIT_DECLTYPE
 82271  82490   /*
 82272  82491   ** Return the column declaration type (if applicable) of the 'i'th column
 82273  82492   ** of the result set of SQL statement pStmt.
 82274  82493   */
 82275  82494   SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
 82276         -  return columnName(
 82277         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
        82495  +  return columnName(pStmt, N, 0, COLNAME_DECLTYPE);
 82278  82496   }
 82279  82497   #ifndef SQLITE_OMIT_UTF16
 82280  82498   SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
 82281         -  return columnName(
 82282         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
        82499  +  return columnName(pStmt, N, 1, COLNAME_DECLTYPE);
 82283  82500   }
 82284  82501   #endif /* SQLITE_OMIT_UTF16 */
 82285  82502   #endif /* SQLITE_OMIT_DECLTYPE */
 82286  82503   
 82287  82504   #ifdef SQLITE_ENABLE_COLUMN_METADATA
 82288  82505   /*
 82289  82506   ** Return the name of the database from which a result column derives.
 82290  82507   ** NULL is returned if the result column is an expression or constant or
 82291  82508   ** anything else which is not an unambiguous reference to a database column.
 82292  82509   */
 82293  82510   SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
 82294         -  return columnName(
 82295         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
        82511  +  return columnName(pStmt, N, 0, COLNAME_DATABASE);
 82296  82512   }
 82297  82513   #ifndef SQLITE_OMIT_UTF16
 82298  82514   SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
 82299         -  return columnName(
 82300         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
        82515  +  return columnName(pStmt, N, 1, COLNAME_DATABASE);
 82301  82516   }
 82302  82517   #endif /* SQLITE_OMIT_UTF16 */
 82303  82518   
 82304  82519   /*
 82305  82520   ** Return the name of the table from which a result column derives.
 82306  82521   ** NULL is returned if the result column is an expression or constant or
 82307  82522   ** anything else which is not an unambiguous reference to a database column.
 82308  82523   */
 82309  82524   SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
 82310         -  return columnName(
 82311         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
        82525  +  return columnName(pStmt, N, 0, COLNAME_TABLE);
 82312  82526   }
 82313  82527   #ifndef SQLITE_OMIT_UTF16
 82314  82528   SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
 82315         -  return columnName(
 82316         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
        82529  +  return columnName(pStmt, N, 1, COLNAME_TABLE);
 82317  82530   }
 82318  82531   #endif /* SQLITE_OMIT_UTF16 */
 82319  82532   
 82320  82533   /*
 82321  82534   ** Return the name of the table column from which a result column derives.
 82322  82535   ** NULL is returned if the result column is an expression or constant or
 82323  82536   ** anything else which is not an unambiguous reference to a database column.
 82324  82537   */
 82325  82538   SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
 82326         -  return columnName(
 82327         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
        82539  +  return columnName(pStmt, N, 0, COLNAME_COLUMN);
 82328  82540   }
 82329  82541   #ifndef SQLITE_OMIT_UTF16
 82330  82542   SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
 82331         -  return columnName(
 82332         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
        82543  +  return columnName(pStmt, N, 1, COLNAME_COLUMN);
 82333  82544   }
 82334  82545   #endif /* SQLITE_OMIT_UTF16 */
 82335  82546   #endif /* SQLITE_ENABLE_COLUMN_METADATA */
 82336  82547   
 82337  82548   
 82338  82549   /******************************* sqlite3_bind_  ***************************
 82339  82550   ** 
................................................................................
 82692  82903   /*
 82693  82904   ** Return true if the prepared statement is guaranteed to not modify the
 82694  82905   ** database.
 82695  82906   */
 82696  82907   SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
 82697  82908     return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
 82698  82909   }
        82910  +
        82911  +/*
        82912  +** Return 1 if the statement is an EXPLAIN and return 2 if the
        82913  +** statement is an EXPLAIN QUERY PLAN
        82914  +*/
        82915  +SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
        82916  +  return pStmt ? ((Vdbe*)pStmt)->explain : 0;
        82917  +}
 82699  82918   
 82700  82919   /*
 82701  82920   ** Return true if the prepared statement is in need of being reset.
 82702  82921   */
 82703  82922   SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
 82704  82923     Vdbe *v = (Vdbe*)pStmt;
 82705  82924     return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
................................................................................
 83382  83601   #endif
 83383  83602   
 83384  83603   /*
 83385  83604   ** Invoke the VDBE coverage callback, if that callback is defined.  This
 83386  83605   ** feature is used for test suite validation only and does not appear an
 83387  83606   ** production builds.
 83388  83607   **
 83389         -** M is an integer between 2 and 4.  2 indicates a ordinary two-way
 83390         -** branch (I=0 means fall through and I=1 means taken).  3 indicates
 83391         -** a 3-way branch where the third way is when one of the operands is
 83392         -** NULL.  4 indicates the OP_Jump instruction which has three destinations
 83393         -** depending on whether the first operand is less than, equal to, or greater
 83394         -** than the second. 
        83608  +** M is the type of branch.  I is the direction taken for this instance of
        83609  +** the branch.
        83610  +**
        83611  +**   M: 2 - two-way branch (I=0: fall-thru   1: jump                )
        83612  +**      3 - two-way + NULL (I=0: fall-thru   1: jump      2: NULL   )
        83613  +**      4 - OP_Jump        (I=0: jump p1     1: jump p2   2: jump p3)
        83614  +**
        83615  +** In other words, if M is 2, then I is either 0 (for fall-through) or
        83616  +** 1 (for when the branch is taken).  If M is 3, the I is 0 for an
        83617  +** ordinary fall-through, I is 1 if the branch was taken, and I is 2 
        83618  +** if the result of comparison is NULL.  For M=3, I=2 the jump may or
        83619  +** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
        83620  +** When M is 4, that means that an OP_Jump is being run.  I is 0, 1, or 2
        83621  +** depending on if the operands are less than, equal, or greater than.
 83395  83622   **
 83396  83623   ** iSrcLine is the source code line (from the __LINE__ macro) that
 83397  83624   ** generated the VDBE instruction combined with flag bits.  The source
 83398  83625   ** code line number is in the lower 24 bits of iSrcLine and the upper
 83399  83626   ** 8 bytes are flags.  The lower three bits of the flags indicate
 83400  83627   ** values for I that should never occur.  For example, if the branch is
 83401  83628   ** always taken, the flags should be 0x05 since the fall-through and
 83402  83629   ** alternate branch are never taken.  If a branch is never taken then
 83403  83630   ** flags should be 0x06 since only the fall-through approach is allowed.
 83404  83631   **
 83405         -** Bit 0x04 of the flags indicates an OP_Jump opcode that is only
        83632  +** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
 83406  83633   ** interested in equal or not-equal.  In other words, I==0 and I==2
 83407         -** should be treated the same.
        83634  +** should be treated as equivalent
 83408  83635   **
 83409  83636   ** Since only a line number is retained, not the filename, this macro
 83410  83637   ** only works for amalgamation builds.  But that is ok, since these macros
 83411  83638   ** should be no-ops except for special builds used to measure test coverage.
 83412  83639   */
 83413  83640   #if !defined(SQLITE_VDBE_COVERAGE)
 83414  83641   # define VdbeBranchTaken(I,M)
................................................................................
 83424  83651       /* The upper 8 bits of iSrcLine are flags.  The lower three bits of
 83425  83652       ** the flags indicate directions that the branch can never go.  If
 83426  83653       ** a branch really does go in one of those directions, assert right
 83427  83654       ** away. */
 83428  83655       mNever = iSrcLine >> 24;
 83429  83656       assert( (I & mNever)==0 );
 83430  83657       if( sqlite3GlobalConfig.xVdbeBranch==0 ) return;  /*NO_TEST*/
        83658  +    /* Invoke the branch coverage callback with three arguments:
        83659  +    **    iSrcLine - the line number of the VdbeCoverage() macro, with
        83660  +    **               flags removed.
        83661  +    **    I        - Mask of bits 0x07 indicating which cases are are
        83662  +    **               fulfilled by this instance of the jump.  0x01 means
        83663  +    **               fall-thru, 0x02 means taken, 0x04 means NULL.  Any
        83664  +    **               impossible cases (ex: if the comparison is never NULL)
        83665  +    **               are filled in automatically so that the coverage
        83666  +    **               measurement logic does not flag those impossible cases
        83667  +    **               as missed coverage.
        83668  +    **    M        - Type of jump.  Same as M argument above
        83669  +    */
 83431  83670       I |= mNever;
 83432  83671       if( M==2 ) I |= 0x04;
 83433  83672       if( M==4 ){
 83434  83673         I |= 0x08;
 83435  83674         if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
 83436  83675       }
 83437  83676       sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
................................................................................
 83971  84210   #ifdef VDBE_PROFILE
 83972  84211     u64 start;                 /* CPU clock count at start of opcode */
 83973  84212   #endif
 83974  84213     /*** INSERT STACK UNION HERE ***/
 83975  84214   
 83976  84215     assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 83977  84216     sqlite3VdbeEnter(p);
        84217  +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
        84218  +  if( db->xProgress ){
        84219  +    u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
        84220  +    assert( 0 < db->nProgressOps );
        84221  +    nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
        84222  +  }else{
        84223  +    nProgressLimit = 0xffffffff;
        84224  +  }
        84225  +#endif
 83978  84226     if( p->rc==SQLITE_NOMEM ){
 83979  84227       /* This happens if a malloc() inside a call to sqlite3_column_text() or
 83980  84228       ** sqlite3_column_text16() failed.  */
 83981  84229       goto no_mem;
 83982  84230     }
 83983  84231     assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
 83984  84232     assert( p->bIsReader || p->readOnly!=0 );
 83985  84233     p->iCurrentTime = 0;
 83986  84234     assert( p->explain==0 );
 83987  84235     p->pResultSet = 0;
 83988  84236     db->busyHandler.nBusy = 0;
 83989  84237     if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 83990  84238     sqlite3VdbeIOTraceSql(p);
 83991         -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 83992         -  if( db->xProgress ){
 83993         -    u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
 83994         -    assert( 0 < db->nProgressOps );
 83995         -    nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
 83996         -  }else{
 83997         -    nProgressLimit = 0xffffffff;
 83998         -  }
 83999         -#endif
 84000  84239   #ifdef SQLITE_DEBUG
 84001  84240     sqlite3BeginBenignMalloc();
 84002  84241     if( p->pc==0
 84003  84242      && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
 84004  84243     ){
 84005  84244       int i;
 84006  84245       int once = 1;
................................................................................
 84168  84407   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 84169  84408     /* Call the progress callback if it is configured and the required number
 84170  84409     ** of VDBE ops have been executed (either since this invocation of
 84171  84410     ** sqlite3VdbeExec() or since last time the progress callback was called).
 84172  84411     ** If the progress callback returns non-zero, exit the virtual machine with
 84173  84412     ** a return code SQLITE_ABORT.
 84174  84413     */
 84175         -  if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
        84414  +  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
 84176  84415       assert( db->nProgressOps!=0 );
 84177         -    nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
        84416  +    nProgressLimit += db->nProgressOps;
 84178  84417       if( db->xProgress(db->pProgressArg) ){
        84418  +      nProgressLimit = 0xffffffff;
 84179  84419         rc = SQLITE_INTERRUPT;
 84180  84420         goto abort_due_to_error;
 84181  84421       }
 84182  84422     }
 84183  84423   #endif
 84184  84424     
 84185  84425     break;
................................................................................
 84450  84690     pOp->opcode = OP_String;
 84451  84691     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
 84452  84692   
 84453  84693   #ifndef SQLITE_OMIT_UTF16
 84454  84694     if( encoding!=SQLITE_UTF8 ){
 84455  84695       rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
 84456  84696       assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
        84697  +    if( rc ) goto too_big;
 84457  84698       if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
 84458  84699       assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
 84459  84700       assert( VdbeMemDynamic(pOut)==0 );
 84460  84701       pOut->szMalloc = 0;
 84461  84702       pOut->flags |= MEM_Static;
 84462  84703       if( pOp->p4type==P4_DYNAMIC ){
 84463  84704         sqlite3DbFree(db, pOp->p4.z);
 84464  84705       }
 84465  84706       pOp->p4type = P4_DYNAMIC;
 84466  84707       pOp->p4.z = pOut->z;
 84467  84708       pOp->p1 = pOut->n;
 84468  84709     }
 84469         -  testcase( rc==SQLITE_TOOBIG );
 84470  84710   #endif
 84471  84711     if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 84472  84712       goto too_big;
 84473  84713     }
 84474  84714     assert( rc==SQLITE_OK );
 84475  84715     /* Fall through to the next case, OP_String */
 84476  84716   }
................................................................................
 84584  84824     assert( pOp->p1>0 && pOp->p1<=p->nVar );
 84585  84825     assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
 84586  84826     pVar = &p->aVar[pOp->p1 - 1];
 84587  84827     if( sqlite3VdbeMemTooBig(pVar) ){
 84588  84828       goto too_big;
 84589  84829     }
 84590  84830     pOut = &aMem[pOp->p2];
 84591         -  sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
        84831  +  if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
        84832  +  memcpy(pOut, pVar, MEMCELLSIZE);
        84833  +  pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
        84834  +  pOut->flags |= MEM_Static|MEM_FromBind;
 84592  84835     UPDATE_MAX_BLOBSIZE(pOut);
 84593  84836     break;
 84594  84837   }
 84595  84838   
 84596  84839   /* Opcode: Move P1 P2 P3 * *
 84597  84840   ** Synopsis: r[P2@P3]=r[P1@P3]
 84598  84841   **
................................................................................
 84717  84960   case OP_ResultRow: {
 84718  84961     Mem *pMem;
 84719  84962     int i;
 84720  84963     assert( p->nResColumn==pOp->p2 );
 84721  84964     assert( pOp->p1>0 );
 84722  84965     assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
 84723  84966   
 84724         -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 84725         -  /* Run the progress counter just before returning.
 84726         -  */
 84727         -  if( db->xProgress!=0
 84728         -   && nVmStep>=nProgressLimit 
 84729         -   && db->xProgress(db->pProgressArg)!=0
 84730         -  ){
 84731         -    rc = SQLITE_INTERRUPT;
 84732         -    goto abort_due_to_error;
 84733         -  }
 84734         -#endif
 84735         -
 84736  84967     /* If this statement has violated immediate foreign key constraints, do
 84737  84968     ** not return the number of rows modified. And do not RELEASE the statement
 84738  84969     ** transaction. It needs to be rolled back.  */
 84739  84970     if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
 84740  84971       assert( db->flags&SQLITE_CountRows );
 84741  84972       assert( p->usesStmtJournal );
 84742  84973       goto abort_due_to_error;
................................................................................
 85094  85325   ** without data loss, then jump immediately to P2, or if P2==0
 85095  85326   ** raise an SQLITE_MISMATCH exception.
 85096  85327   */
 85097  85328   case OP_MustBeInt: {            /* jump, in1 */
 85098  85329     pIn1 = &aMem[pOp->p1];
 85099  85330     if( (pIn1->flags & MEM_Int)==0 ){
 85100  85331       applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
 85101         -    VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
 85102  85332       if( (pIn1->flags & MEM_Int)==0 ){
        85333  +      VdbeBranchTaken(1, 2);
 85103  85334         if( pOp->p2==0 ){
 85104  85335           rc = SQLITE_MISMATCH;
 85105  85336           goto abort_due_to_error;
 85106  85337         }else{
 85107  85338           goto jump_to_p2;
 85108  85339         }
 85109  85340       }
 85110  85341     }
        85342  +  VdbeBranchTaken(0, 2);
 85111  85343     MemSetTypeFlag(pIn1, MEM_Int);
 85112  85344     break;
 85113  85345   }
 85114  85346   
 85115  85347   #ifndef SQLITE_OMIT_FLOATING_POINT
 85116  85348   /* Opcode: RealAffinity P1 * * * *
 85117  85349   **
................................................................................
 85278  85510     if( (flags1 | flags3)&MEM_Null ){
 85279  85511       /* One or both operands are NULL */
 85280  85512       if( pOp->p5 & SQLITE_NULLEQ ){
 85281  85513         /* If SQLITE_NULLEQ is set (which will only happen if the operator is
 85282  85514         ** OP_Eq or OP_Ne) then take the jump or not depending on whether
 85283  85515         ** or not both operands are null.
 85284  85516         */
 85285         -      assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
 85286  85517         assert( (flags1 & MEM_Cleared)==0 );
 85287  85518         assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
 85288  85519         testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
 85289  85520         if( (flags1&flags3&MEM_Null)!=0
 85290  85521          && (flags3&MEM_Cleared)==0
 85291  85522         ){
 85292  85523           res = 0;  /* Operands are equal */
 85293  85524         }else{
 85294         -        res = 1;  /* Operands are not equal */
        85525  +        res = ((flags3 & MEM_Null) ? -1 : +1);  /* Operands are not equal */
 85295  85526         }
 85296  85527       }else{
 85297  85528         /* SQLITE_NULLEQ is clear and at least one operand is NULL,
 85298  85529         ** then the result is always NULL.
 85299  85530         ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
 85300  85531         */
 85301  85532         if( pOp->p5 & SQLITE_STOREP2 ){
................................................................................
 85405  85636         if( (pOp->opcode==OP_Eq)==res2 ) break;
 85406  85637       }
 85407  85638       memAboutToChange(p, pOut);
 85408  85639       MemSetTypeFlag(pOut, MEM_Int);
 85409  85640       pOut->u.i = res2;
 85410  85641       REGISTER_TRACE(pOp->p2, pOut);
 85411  85642     }else{
 85412         -    VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
        85643  +    VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
 85413  85644       if( res2 ){
 85414  85645         goto jump_to_p2;
 85415  85646       }
 85416  85647     }
 85417  85648     break;
 85418  85649   }
 85419  85650   
................................................................................
 85955  86186       op_column_read_header:
 85956  86187         i = pC->nHdrParsed;
 85957  86188         offset64 = aOffset[i];
 85958  86189         zHdr = zData + pC->iHdrOffset;
 85959  86190         zEndHdr = zData + aOffset[0];
 85960  86191         testcase( zHdr>=zEndHdr );
 85961  86192         do{
 85962         -        if( (t = zHdr[0])<0x80 ){
        86193  +        if( (pC->aType[i] = t = zHdr[0])<0x80 ){
 85963  86194             zHdr++;
 85964  86195             offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
 85965  86196           }else{
 85966  86197             zHdr += sqlite3GetVarint32(zHdr, &t);
        86198  +          pC->aType[i] = t;
 85967  86199             offset64 += sqlite3VdbeSerialTypeLen(t);
 85968  86200           }
 85969         -        pC->aType[i++] = t;
 85970         -        aOffset[i] = (u32)(offset64 & 0xffffffff);
        86201  +        aOffset[++i] = (u32)(offset64 & 0xffffffff);
 85971  86202         }while( i<=p2 && zHdr<zEndHdr );
 85972  86203   
 85973  86204         /* The record is corrupt if any of the following are true:
 85974  86205         ** (1) the bytes of the header extend past the declared header size
 85975  86206         ** (2) the entire header was used but not all data was used
 85976  86207         ** (3) the end of the data extends beyond the end of the record.
 85977  86208         */
................................................................................
 86966  87197     pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
 86967  87198     if( pCx==0 ) goto no_mem;
 86968  87199     pCx->nullRow = 1;
 86969  87200     pCx->isEphemeral = 1;
 86970  87201     pCx->pKeyInfo = pOrig->pKeyInfo;
 86971  87202     pCx->isTable = pOrig->isTable;
 86972  87203     pCx->pgnoRoot = pOrig->pgnoRoot;
        87204  +  pCx->isOrdered = pOrig->isOrdered;
 86973  87205     rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
 86974  87206                             pCx->pKeyInfo, pCx->uc.pCursor);
 86975  87207     /* The sqlite3BtreeCursor() routine can only fail for the first cursor
 86976  87208     ** opened for a database.  Since there is already an open cursor when this
 86977  87209     ** opcode is run, the sqlite3BtreeCursor() cannot fail */
 86978  87210     assert( rc==SQLITE_OK );
 86979  87211     break;
................................................................................
 88474  88706   #ifdef SQLITE_TEST
 88475  88707     sqlite3_sort_count++;
 88476  88708     sqlite3_search_count--;
 88477  88709   #endif
 88478  88710     p->aCounter[SQLITE_STMTSTATUS_SORT]++;
 88479  88711     /* Fall through into OP_Rewind */
 88480  88712   }
 88481         -/* Opcode: Rewind P1 P2 * * P5
        88713  +/* Opcode: Rewind P1 P2 * * *
 88482  88714   **
 88483  88715   ** The next use of the Rowid or Column or Next instruction for P1 
 88484  88716   ** will refer to the first entry in the database table or index.
 88485  88717   ** If the table or index is empty, jump immediately to P2.
 88486  88718   ** If the table or index is not empty, fall through to the following 
 88487  88719   ** instruction.
 88488  88720   **
 88489         -** If P5 is non-zero and the table is not empty, then the "skip-next"
 88490         -** flag is set on the cursor so that the next OP_Next instruction 
 88491         -** executed on it is a no-op.
 88492         -**
 88493  88721   ** This opcode leaves the cursor configured to move in forward order,
 88494  88722   ** from the beginning toward the end.  In other words, the cursor is
 88495  88723   ** configured to use Next, not Prev.
 88496  88724   */
 88497  88725   case OP_Rewind: {        /* jump */
 88498  88726     VdbeCursor *pC;
 88499  88727     BtCursor *pCrsr;
 88500  88728     int res;
 88501  88729   
 88502  88730     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
        88731  +  assert( pOp->p5==0 );
 88503  88732     pC = p->apCsr[pOp->p1];
 88504  88733     assert( pC!=0 );
 88505  88734     assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
 88506  88735     res = 1;
 88507  88736   #ifdef SQLITE_DEBUG
 88508  88737     pC->seekOp = OP_Rewind;
 88509  88738   #endif
................................................................................
 88510  88739     if( isSorter(pC) ){
 88511  88740       rc = sqlite3VdbeSorterRewind(pC, &res);
 88512  88741     }else{
 88513  88742       assert( pC->eCurType==CURTYPE_BTREE );
 88514  88743       pCrsr = pC->uc.pCursor;
 88515  88744       assert( pCrsr );
 88516  88745       rc = sqlite3BtreeFirst(pCrsr, &res);
 88517         -#ifndef SQLITE_OMIT_WINDOWFUNC
 88518         -    if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr);
 88519         -#endif
 88520  88746       pC->deferredMoveto = 0;
 88521  88747       pC->cacheStatus = CACHE_STALE;
 88522  88748     }
 88523  88749     if( rc ) goto abort_due_to_error;
 88524  88750     pC->nullRow = (u8)res;
 88525  88751     assert( pOp->p2>0 && pOp->p2<p->nOp );
 88526  88752     VdbeBranchTaken(res!=0,2);
................................................................................
 89522  89748       for(i=0; i<p->nMem; i++){
 89523  89749         aMem[i].pScopyFrom = 0;  /* Prevent false-positive AboutToChange() errs */
 89524  89750         aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
 89525  89751       }
 89526  89752     }
 89527  89753   #endif
 89528  89754     pOp = &aOp[-1];
 89529         -
 89530         -  break;
        89755  +  goto check_for_interrupt;
 89531  89756   }
 89532  89757   
 89533  89758   /* Opcode: Param P1 P2 * * *
 89534  89759   **
 89535  89760   ** This opcode is only ever present in sub-programs called via the 
 89536  89761   ** OP_Program instruction. Copy a value currently stored in a memory 
 89537  89762   ** cell of the calling (parent) frame to cell P2 in the current frames 
................................................................................
 89895  90120     Mem *pMem;
 89896  90121     assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
 89897  90122     assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
 89898  90123     pMem = &aMem[pOp->p1];
 89899  90124     assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
 89900  90125   #ifndef SQLITE_OMIT_WINDOWFUNC
 89901  90126     if( pOp->p3 ){
        90127  +    memAboutToChange(p, &aMem[pOp->p3]);
 89902  90128       rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
 89903  90129       pMem = &aMem[pOp->p3];
 89904  90130     }else
 89905  90131   #endif
 89906  90132     {
 89907  90133       rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
 89908  90134     }
................................................................................
 90932  91158       sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
 90933  91159     }
 90934  91160   
 90935  91161     /* This is the only way out of this procedure.  We have to
 90936  91162     ** release the mutexes on btrees that were acquired at the
 90937  91163     ** top. */
 90938  91164   vdbe_return:
 90939         -  testcase( nVmStep>0 );
        91165  +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
        91166  +  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
        91167  +    nProgressLimit += db->nProgressOps;
        91168  +    if( db->xProgress(db->pProgressArg) ){
        91169  +      nProgressLimit = 0xffffffff;
        91170  +      rc = SQLITE_INTERRUPT;
        91171  +      goto abort_due_to_error;
        91172  +    }
        91173  +  }
        91174  +#endif
 90940  91175     p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
 90941  91176     sqlite3VdbeLeave(p);
 90942  91177     assert( rc!=SQLITE_OK || nExtraDelete==0 
 90943  91178          || sqlite3_strlike("DELETE%",p->zSql,0)!=0 
 90944  91179     );
 90945  91180     return rc;
 90946  91181   
................................................................................
 92019  92254       ** In this case, allocate space at p->aAlloc[] to copy the requested
 92020  92255       ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
 92021  92256       int nRem;                     /* Bytes remaining to copy */
 92022  92257   
 92023  92258       /* Extend the p->aAlloc[] allocation if required. */
 92024  92259       if( p->nAlloc<nByte ){
 92025  92260         u8 *aNew;
 92026         -      int nNew = MAX(128, p->nAlloc*2);
        92261  +      sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
 92027  92262         while( nByte>nNew ) nNew = nNew*2;
 92028  92263         aNew = sqlite3Realloc(p->aAlloc, nNew);
 92029  92264         if( !aNew ) return SQLITE_NOMEM_BKPT;
 92030  92265         p->nAlloc = nNew;
 92031  92266         p->aAlloc = aNew;
 92032  92267       }
 92033  92268   
................................................................................
 93310  93545     }
 93311  93546   
 93312  93547     if( pSorter->list.aMemory ){
 93313  93548       int nMin = pSorter->iMemory + nReq;
 93314  93549   
 93315  93550       if( nMin>pSorter->nMemory ){
 93316  93551         u8 *aNew;
 93317         -      int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
 93318         -      int nNew = pSorter->nMemory * 2;
        93552  +      sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
        93553  +      int iListOff = -1;
        93554  +      if( pSorter->list.pList ){
        93555  +        iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
        93556  +      }
 93319  93557         while( nNew < nMin ) nNew = nNew*2;
 93320  93558         if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
 93321  93559         if( nNew < nMin ) nNew = nMin;
 93322         -
 93323  93560         aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
 93324  93561         if( !aNew ) return SQLITE_NOMEM_BKPT;
 93325         -      pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
        93562  +      if( iListOff>=0 ){
        93563  +        pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
        93564  +      }
 93326  93565         pSorter->list.aMemory = aNew;
 93327  93566         pSorter->nMemory = nNew;
 93328  93567       }
 93329  93568   
 93330  93569       pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
 93331  93570       pSorter->iMemory += ROUND8(nReq);
 93332  93571       if( pSorter->list.pList ){
................................................................................
 95304  95543             assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 95305  95544             assert( pExpr->x.pList==0 );
 95306  95545             assert( pExpr->x.pSelect==0 );
 95307  95546             pOrig = pEList->a[j].pExpr;
 95308  95547             if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
 95309  95548               sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
 95310  95549               return WRC_Abort;
        95550  +          }
        95551  +          if( (pNC->ncFlags&NC_AllowWin)==0 && ExprHasProperty(pOrig, EP_Win) ){
        95552  +            sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
        95553  +            return WRC_Abort;
 95311  95554             }
 95312  95555             if( sqlite3ExprVectorSize(pOrig)!=1 ){
 95313  95556               sqlite3ErrorMsg(pParse, "row value misused");
 95314  95557               return WRC_Abort;
 95315  95558             }
 95316  95559             resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
 95317  95560             cnt = 1;
................................................................................
 95595  95838         int no_such_func = 0;       /* True if no such function exists */
 95596  95839         int wrong_num_args = 0;     /* True if wrong number of arguments */
 95597  95840         int is_agg = 0;             /* True if is an aggregate function */
 95598  95841         int nId;                    /* Number of characters in function name */
 95599  95842         const char *zId;            /* The function name. */
 95600  95843         FuncDef *pDef;              /* Information about the function */
 95601  95844         u8 enc = ENC(pParse->db);   /* The database encoding */
        95845  +      int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
 95602  95846   
 95603  95847         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 95604  95848         zId = pExpr->u.zToken;
 95605  95849         nId = sqlite3Strlen30(zId);
 95606  95850         pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
 95607  95851         if( pDef==0 ){
 95608  95852           pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
................................................................................
 95716  95960             pNC->nErr++;
 95717  95961           }else if( wrong_num_args ){
 95718  95962             sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 95719  95963                  nId, zId);
 95720  95964             pNC->nErr++;
 95721  95965           }
 95722  95966           if( is_agg ){
        95967  +          /* Window functions may not be arguments of aggregate functions.
        95968  +          ** Or arguments of other window functions. But aggregate functions
        95969  +          ** may be arguments for window functions.  */
 95723  95970   #ifndef SQLITE_OMIT_WINDOWFUNC
 95724         -          pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg);
        95971  +          pNC->ncFlags &= ~(NC_AllowWin | (!pExpr->y.pWin ? NC_AllowAgg : 0));
 95725  95972   #else
 95726  95973             pNC->ncFlags &= ~NC_AllowAgg;
 95727  95974   #endif
 95728  95975           }
 95729  95976         }
 95730  95977         sqlite3WalkExprList(pWalker, pList);
 95731  95978         if( is_agg ){
................................................................................
 95738  95985             sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
 95739  95986             if( 0==pSel->pWin 
 95740  95987              || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) 
 95741  95988             ){
 95742  95989               pExpr->y.pWin->pNextWin = pSel->pWin;
 95743  95990               pSel->pWin = pExpr->y.pWin;
 95744  95991             }
 95745         -          pNC->ncFlags |= NC_AllowWin;
        95992  +          pNC->ncFlags |= NC_HasWin;
 95746  95993           }else
 95747  95994   #endif /* SQLITE_OMIT_WINDOWFUNC */
 95748  95995           {
 95749  95996             NameContext *pNC2 = pNC;
 95750  95997             pExpr->op = TK_AGG_FUNCTION;
 95751  95998             pExpr->op2 = 0;
 95752  95999             while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
................................................................................
 95756  96003             assert( pDef!=0 );
 95757  96004             if( pNC2 ){
 95758  96005               assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
 95759  96006               testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
 95760  96007               pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
 95761  96008   
 95762  96009             }
 95763         -          pNC->ncFlags |= NC_AllowAgg;
 95764  96010           }
        96011  +        pNC->ncFlags |= savedAllowFlags;
 95765  96012         }
 95766  96013         /* FIX ME:  Compute pExpr->affinity based on the expected return
 95767  96014         ** type of the function 
 95768  96015         */
 95769  96016         return WRC_Prune;
 95770  96017       }
 95771  96018   #ifndef SQLITE_OMIT_SUBQUERY
................................................................................
 96294  96541         p->pOrderBy = 0;
 96295  96542       }
 96296  96543     
 96297  96544       /* Recursively resolve names in all subqueries
 96298  96545       */
 96299  96546       for(i=0; i<p->pSrc->nSrc; i++){
 96300  96547         struct SrcList_item *pItem = &p->pSrc->a[i];
 96301         -      if( pItem->pSelect ){
        96548  +      if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
 96302  96549           NameContext *pNC;         /* Used to iterate name contexts */
 96303  96550           int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
 96304  96551           const char *zSavedContext = pParse->zAuthContext;
 96305  96552   
 96306  96553           /* Count the total number of references to pOuterNC and all of its
 96307  96554           ** parent contexts. After resolving references to expressions in
 96308  96555           ** pItem->pSelect, check if this value has changed. If so, then
................................................................................
 96426  96673             sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
 96427  96674                 "the GROUP BY clause");
 96428  96675             return WRC_Abort;
 96429  96676           }
 96430  96677         }
 96431  96678       }
 96432  96679   
        96680  +#ifndef SQLITE_OMIT_WINDOWFUNC
 96433  96681       if( IN_RENAME_OBJECT ){
 96434  96682         Window *pWin;
 96435  96683         for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
 96436  96684           if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
 96437  96685            || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
 96438  96686           ){
 96439  96687             return WRC_Abort;
 96440  96688           }
 96441  96689         }
 96442  96690       }
        96691  +#endif
 96443  96692   
 96444  96693       /* If this is part of a compound SELECT, check that it has the right
 96445  96694       ** number of expressions in the select list. */
 96446  96695       if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
 96447  96696         sqlite3SelectWrongNumTermsError(pParse, p->pNext);
 96448  96697         return WRC_Abort;
 96449  96698       }
................................................................................
 96516  96765     NameContext *pNC,       /* Namespace to resolve expressions in. */
 96517  96766     Expr *pExpr             /* The expression to be analyzed. */
 96518  96767   ){
 96519  96768     u16 savedHasAgg;
 96520  96769     Walker w;
 96521  96770   
 96522  96771     if( pExpr==0 ) return SQLITE_OK;
 96523         -  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
 96524         -  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
        96772  +  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
        96773  +  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
 96525  96774     w.pParse = pNC->pParse;
 96526  96775     w.xExprCallback = resolveExprStep;
 96527  96776     w.xSelectCallback = resolveSelectStep;
 96528  96777     w.xSelectCallback2 = 0;
 96529  96778     w.u.pNC = pNC;
 96530  96779   #if SQLITE_MAX_EXPR_DEPTH>0
 96531  96780     w.pParse->nHeight += pExpr->nHeight;
................................................................................
 96533  96782       return SQLITE_ERROR;
 96534  96783     }
 96535  96784   #endif
 96536  96785     sqlite3WalkExpr(&w, pExpr);
 96537  96786   #if SQLITE_MAX_EXPR_DEPTH>0
 96538  96787     w.pParse->nHeight -= pExpr->nHeight;
 96539  96788   #endif
 96540         -  if( pNC->ncFlags & NC_HasAgg ){
 96541         -    ExprSetProperty(pExpr, EP_Agg);
 96542         -  }
        96789  +  assert( EP_Agg==NC_HasAgg );
        96790  +  assert( EP_Win==NC_HasWin );
        96791  +  testcase( pNC->ncFlags & NC_HasAgg );
        96792  +  testcase( pNC->ncFlags & NC_HasWin );
        96793  +  ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
 96543  96794     pNC->ncFlags |= savedHasAgg;
 96544  96795     return pNC->nErr>0 || w.pParse->nErr>0;
 96545  96796   }
 96546  96797   
 96547  96798   /*
 96548  96799   ** Resolve all names for all expression in an expression list.  This is
 96549  96800   ** just like sqlite3ResolveExprNames() except that it works for an expression
................................................................................
 97491  97742     if( op==TK_AND && pParse->nErr==0 && !IN_RENAME_OBJECT ){
 97492  97743       /* Take advantage of short-circuit false optimization for AND */
 97493  97744       p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
 97494  97745     }else{
 97495  97746       p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
 97496  97747       if( p ){
 97497  97748         memset(p, 0, sizeof(Expr));
 97498         -      p->op = op & TKFLG_MASK;
        97749  +      p->op = op & 0xff;
 97499  97750         p->iAgg = -1;
 97500  97751       }
 97501  97752       sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
 97502  97753     }
 97503  97754     if( p ) {
 97504  97755       sqlite3ExprCheckHeight(pParse, p->nHeight);
 97505  97756     }
................................................................................
 97956  98207   ** argument. If an OOM condition is encountered, NULL is returned
 97957  98208   ** and the db->mallocFailed flag set.
 97958  98209   */
 97959  98210   #ifndef SQLITE_OMIT_CTE
 97960  98211   static With *withDup(sqlite3 *db, With *p){
 97961  98212     With *pRet = 0;
 97962  98213     if( p ){
 97963         -    int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
        98214  +    sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
 97964  98215       pRet = sqlite3DbMallocZero(db, nByte);
 97965  98216       if( pRet ){
 97966  98217         int i;
 97967  98218         pRet->nCte = p->nCte;
 97968  98219         for(i=0; i<p->nCte; i++){
 97969  98220           pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
 97970  98221           pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
................................................................................
 98221  98472       if( pList==0 ){
 98222  98473         goto no_mem;
 98223  98474       }
 98224  98475       pList->nExpr = 0;
 98225  98476     }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
 98226  98477       ExprList *pNew;
 98227  98478       pNew = sqlite3DbRealloc(db, pList, 
 98228         -             sizeof(*pList)+(2*pList->nExpr - 1)*sizeof(pList->a[0]));
        98479  +         sizeof(*pList)+(2*(sqlite3_int64)pList->nExpr-1)*sizeof(pList->a[0]));
 98229  98480       if( pNew==0 ){
 98230  98481         goto no_mem;
 98231  98482       }
 98232  98483       pList = pNew;
 98233  98484     }
 98234  98485     pItem = &pList->a[pList->nExpr++];
 98235  98486     assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) );
................................................................................
101666 101917   ** an ordinary JOIN.  The p argument is the WHERE clause.  If the WHERE
101667 101918   ** clause requires that some column of the right table of the LEFT JOIN
101668 101919   ** be non-NULL, then the LEFT JOIN can be safely converted into an
101669 101920   ** ordinary join.
101670 101921   */
101671 101922   SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
101672 101923     Walker w;
       101924  +  p = sqlite3ExprSkipCollate(p);
       101925  +  while( p ){
       101926  +    if( p->op==TK_NOTNULL ){
       101927  +      p = p->pLeft;
       101928  +    }else if( p->op==TK_AND ){
       101929  +      if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
       101930  +      p = p->pRight;
       101931  +    }else{
       101932  +      break;
       101933  +    }
       101934  +  }
101673 101935     w.xExprCallback = impliesNotNullRow;
101674 101936     w.xSelectCallback = 0;
101675 101937     w.xSelectCallback2 = 0;
101676 101938     w.eCode = 0;
101677 101939     w.u.iCur = iTab;
101678 101940     sqlite3WalkExpr(&w, p);
101679 101941     return w.eCode;
................................................................................
102247 102509       pVTab = sqlite3GetVTable(db, pTab);
102248 102510       if( pVTab->pVtab->pModule->xRename==0 ){
102249 102511         pVTab = 0;
102250 102512       }
102251 102513     }
102252 102514   #endif
102253 102515   
102254         -  /* Begin a transaction for database iDb. 
102255         -  ** Then modify the schema cookie (since the ALTER TABLE modifies the
102256         -  ** schema). Open a statement transaction if the table is a virtual
102257         -  ** table.
102258         -  */
       102516  +  /* Begin a transaction for database iDb. Then modify the schema cookie
       102517  +  ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
       102518  +  ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the 
       102519  +  ** nested SQL may raise an exception.  */
102259 102520     v = sqlite3GetVdbe(pParse);
102260 102521     if( v==0 ){
102261 102522       goto exit_rename_table;
102262 102523     }
       102524  +  sqlite3MayAbort(pParse);
102263 102525   
102264 102526     /* figure out how many UTF-8 characters are in zName */
102265 102527     zTabName = pTab->zName;
102266 102528     nTabName = sqlite3Utf8CharLen(zTabName, -1);
102267 102529   
102268 102530     /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
102269 102531     ** the schema to use the new table name.  */
................................................................................
102324 102586     ** SQLite tables) that are identified by the name of the virtual table.
102325 102587     */
102326 102588   #ifndef SQLITE_OMIT_VIRTUALTABLE
102327 102589     if( pVTab ){
102328 102590       int i = ++pParse->nMem;
102329 102591       sqlite3VdbeLoadString(v, i, zName);
102330 102592       sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
102331         -    sqlite3MayAbort(pParse);
102332 102593     }
102333 102594   #endif
102334 102595   
102335 102596     renameReloadSchema(pParse, iDb);
102336 102597     renameTestSchema(pParse, zDb, iDb==1);
102337 102598   
102338 102599   exit_rename_table:
................................................................................
102645 102906       goto exit_rename_column;
102646 102907     }
102647 102908   
102648 102909     /* Do the rename operation using a recursive UPDATE statement that
102649 102910     ** uses the sqlite_rename_column() SQL function to compute the new
102650 102911     ** CREATE statement text for the sqlite_master table.
102651 102912     */
       102913  +  sqlite3MayAbort(pParse);
102652 102914     zNew = sqlite3NameFromToken(db, pNew);
102653 102915     if( !zNew ) goto exit_rename_column;
102654 102916     assert( pNew->n>0 );
102655 102917     bQuote = sqlite3Isquote(pNew->z[0]);
102656 102918     sqlite3NestedParse(pParse, 
102657 102919         "UPDATE \"%w\".%s SET "
102658 102920         "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
................................................................................
105899 106161     ** remove the entry from the db->aDb[] array. i.e. put everything back the
105900 106162     ** way we found it.
105901 106163     */
105902 106164     if( rc==SQLITE_OK ){
105903 106165       sqlite3BtreeEnterAll(db);
105904 106166       db->init.iDb = 0;
105905 106167       db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
105906         -    rc = sqlite3Init(db, &zErrDyn);
       106168  +    if( !REOPEN_AS_MEMDB(db) ){
       106169  +      rc = sqlite3Init(db, &zErrDyn);
       106170  +    }
105907 106171       sqlite3BtreeLeaveAll(db);
105908 106172       assert( zErrDyn==0 || rc!=SQLITE_OK );
105909 106173     }
105910 106174   #ifdef SQLITE_USER_AUTHENTICATION
105911         -  if( rc==SQLITE_OK ){
       106175  +  if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
105912 106176       u8 newAuth = 0;
105913 106177       rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
105914 106178       if( newAuth<db->auth.authLevel ){
105915 106179         rc = SQLITE_AUTH_USER;
105916 106180       }
105917 106181     }
105918 106182   #endif
................................................................................
106833 107097   
106834 107098     if( pParse->nErr ) return;
106835 107099     assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
106836 107100     va_start(ap, zFormat);
106837 107101     zSql = sqlite3VMPrintf(db, zFormat, ap);
106838 107102     va_end(ap);
106839 107103     if( zSql==0 ){
106840         -    return;   /* A malloc must have failed */
       107104  +    /* This can result either from an OOM or because the formatted string
       107105  +    ** exceeds SQLITE_LIMIT_LENGTH.  In the latter case, we need to set
       107106  +    ** an error */
       107107  +    if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
       107108  +    pParse->nErr++;
       107109  +    return;
106841 107110     }
106842 107111     pParse->nested++;
106843 107112     memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
106844 107113     memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
106845 107114     sqlite3RunParser(pParse, zSql, &zErrMsg);
106846 107115     sqlite3DbFree(db, zErrMsg);
106847 107116     sqlite3DbFree(db, zSql);
................................................................................
107973 108242     }
107974 108243     if( nTerm==1
107975 108244      && pCol
107976 108245      && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
107977 108246      && sortOrder!=SQLITE_SO_DESC
107978 108247     ){
107979 108248       if( IN_RENAME_OBJECT && pList ){
107980         -      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr);
       108249  +      Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
       108250  +      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
107981 108251       }
107982 108252       pTab->iPKey = iCol;
107983 108253       pTab->keyConf = (u8)onError;
107984 108254       assert( autoInc==0 || autoInc==1 );
107985 108255       pTab->tabFlags |= autoInc*TF_Autoincrement;
107986 108256       if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
107987 108257     }else if( autoInc ){
................................................................................
108394 108664       sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
108395 108665                          SQLITE_IDXTYPE_PRIMARYKEY);
108396 108666       if( db->mallocFailed || pParse->nErr ) return;
108397 108667       pPk = sqlite3PrimaryKeyIndex(pTab);
108398 108668       pTab->iPKey = -1;
108399 108669     }else{
108400 108670       pPk = sqlite3PrimaryKeyIndex(pTab);
       108671  +    assert( pPk!=0 );
108401 108672   
108402 108673       /*
108403 108674       ** Remove all redundant columns from the PRIMARY KEY.  For example, change
108404 108675       ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
108405 108676       ** code assumes the PRIMARY KEY contains no repeated columns.
108406 108677       */
108407 108678       for(i=j=1; i<pPk->nKeyCol; i++){
................................................................................
108563 108834         sqlite3ErrorMsg(pParse, "");
108564 108835         return;
108565 108836       }
108566 108837       p->tnum = db->init.newTnum;
108567 108838       if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
108568 108839     }
108569 108840   
       108841  +  assert( (p->tabFlags & TF_HasPrimaryKey)==0
       108842  +       || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
       108843  +  assert( (p->tabFlags & TF_HasPrimaryKey)!=0
       108844  +       || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
       108845  +
108570 108846     /* Special processing for WITHOUT ROWID Tables */
108571 108847     if( tabOpts & TF_WithoutRowid ){
108572 108848       if( (p->tabFlags & TF_Autoincrement) ){
108573 108849         sqlite3ErrorMsg(pParse,
108574 108850             "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
108575 108851         return;
108576 108852       }
................................................................................
109716 109992     }
109717 109993     pDb = &db->aDb[iDb];
109718 109994   
109719 109995     assert( pTab!=0 );
109720 109996     assert( pParse->nErr==0 );
109721 109997     if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
109722 109998          && db->init.busy==0
       109999  +       && pTblName!=0
109723 110000   #if SQLITE_USER_AUTHENTICATION
109724 110001          && sqlite3UserAuthTable(pTab->zName)==0
109725 110002   #endif
109726 110003   #ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
109727 110004          && sqlite3StrICmp(&pTab->zName[7],"master")!=0
109728 110005   #endif
109729         -       && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
109730 110006    ){
109731 110007       sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
109732 110008       goto exit_create_index;
109733 110009     }
109734 110010   #ifndef SQLITE_OMIT_VIEW
109735 110011     if( pTab->pSelect ){
109736 110012       sqlite3ErrorMsg(pParse, "views may not be indexed");
................................................................................
109826 110102       pList = sqlite3ExprListAppend(pParse, 0,
109827 110103                 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
109828 110104       if( pList==0 ) goto exit_create_index;
109829 110105       assert( pList->nExpr==1 );
109830 110106       sqlite3ExprListSetSortOrder(pList, sortOrder);
109831 110107     }else{
109832 110108       sqlite3ExprListCheckLength(pParse, pList, "index");
       110109  +    if( pParse->nErr ) goto exit_create_index;
109833 110110     }
109834 110111   
109835 110112     /* Figure out how many bytes of space are required to store explicitly
109836 110113     ** specified collation sequence names.
109837 110114     */
109838 110115     for(i=0; i<pList->nExpr; i++){
109839 110116       Expr *pExpr = pList->a[i].pExpr;
................................................................................
109844 110121     }
109845 110122   
109846 110123     /* 
109847 110124     ** Allocate the index structure. 
109848 110125     */
109849 110126     nName = sqlite3Strlen30(zName);
109850 110127     nExtraCol = pPk ? pPk->nKeyCol : 1;
       110128  +  assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
109851 110129     pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
109852 110130                                         nName + nExtra + 1, &zExtra);
109853 110131     if( db->mallocFailed ){
109854 110132       goto exit_create_index;
109855 110133     }
109856 110134     assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
109857 110135     assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
................................................................................
110327 110605     sqlite3 *db,      /* Connection to notify of malloc failures */
110328 110606     void *pArray,     /* Array of objects.  Might be reallocated */
110329 110607     int szEntry,      /* Size of each object in the array */
110330 110608     int *pnEntry,     /* Number of objects currently in use */
110331 110609     int *pIdx         /* Write the index of a new slot here */
110332 110610   ){
110333 110611     char *z;
110334         -  int n = *pnEntry;
       110612  +  sqlite3_int64 n = *pIdx = *pnEntry;
110335 110613     if( (n & (n-1))==0 ){
110336         -    int sz = (n==0) ? 1 : 2*n;
       110614  +    sqlite3_int64 sz = (n==0) ? 1 : 2*n;
110337 110615       void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
110338 110616       if( pNew==0 ){
110339 110617         *pIdx = -1;
110340 110618         return pArray;
110341 110619       }
110342 110620       pArray = pNew;
110343 110621     }
110344 110622     z = (char*)pArray;
110345 110623     memset(&z[n * szEntry], 0, szEntry);
110346         -  *pIdx = n;
110347 110624     ++*pnEntry;
110348 110625     return pArray;
110349 110626   }
110350 110627   
110351 110628   /*
110352 110629   ** Append a new element to the given IdList.  Create a new IdList if
110353 110630   ** need be.
................................................................................
110450 110727     assert( nExtra>=1 );
110451 110728     assert( pSrc!=0 );
110452 110729     assert( iStart<=pSrc->nSrc );
110453 110730   
110454 110731     /* Allocate additional space if needed */
110455 110732     if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
110456 110733       SrcList *pNew;
110457         -    int nAlloc = pSrc->nSrc*2+nExtra;
       110734  +    sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
110458 110735       sqlite3 *db = pParse->db;
110459 110736   
110460 110737       if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
110461 110738         sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
110462 110739                         SQLITE_MAX_SRCLIST);
110463 110740         return 0;
110464 110741       }
................................................................................
110957 111234     Index *pIdx       /* The index that triggers the constraint */
110958 111235   ){
110959 111236     char *zErr;
110960 111237     int j;
110961 111238     StrAccum errMsg;
110962 111239     Table *pTab = pIdx->pTable;
110963 111240   
110964         -  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
       111241  +  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 
       111242  +                      pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
110965 111243     if( pIdx->aColExpr ){
110966 111244       sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
110967 111245     }else{
110968 111246       for(j=0; j<pIdx->nKeyCol; j++){
110969 111247         char *zCol;
110970 111248         assert( pIdx->aiColumn[j]>=0 );
110971 111249         zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
................................................................................
111206 111484         if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
111207 111485           sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
111208 111486         }
111209 111487       }
111210 111488     }
111211 111489   
111212 111490     if( pWith ){
111213         -    int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
       111491  +    sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
111214 111492       pNew = sqlite3DbRealloc(db, pWith, nByte);
111215 111493     }else{
111216 111494       pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
111217 111495     }
111218 111496     assert( (pNew!=0 && zName!=0) || db->mallocFailed );
111219 111497   
111220 111498     if( db->mallocFailed ){
................................................................................
114513 114791   ** Set the LIKEOPT flag on the 2-argument function with the given name.
114514 114792   */
114515 114793   static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
114516 114794     FuncDef *pDef;
114517 114795     pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0);
114518 114796     if( ALWAYS(pDef) ){
114519 114797       pDef->funcFlags |= flagVal;
       114798  +  }
       114799  +  pDef = sqlite3FindFunction(db, zName, 3, SQLITE_UTF8, 0);
       114800  +  if( pDef ){
       114801  +    pDef->funcFlags |= flagVal;
114520 114802     }
114521 114803   }
114522 114804   
114523 114805   /*
114524 114806   ** Register the built-in LIKE and GLOB functions.  The caseSensitive
114525 114807   ** parameter determines whether or not the LIKE operator is case
114526 114808   ** sensitive.  GLOB is always case sensitive.
................................................................................
117836 118118           sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
117837 118119           VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
117838 118120         }
117839 118121       }
117840 118122       sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
117841 118123       VdbeComment((v, "for %s", pIdx->zName));
117842 118124   #ifdef SQLITE_ENABLE_NULL_TRIM
117843         -    if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
       118125  +    if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
       118126  +      sqlite3SetMakeRecordP5(v, pIdx->pTable);
       118127  +    }
117844 118128   #endif
117845 118129   
117846 118130       /* In an UPDATE operation, if this index is the PRIMARY KEY index 
117847 118131       ** of a WITHOUT ROWID table and there has been no change the
117848 118132       ** primary key, then no collision is possible.  The collision detection
117849 118133       ** logic below can all be skipped. */
117850 118134       if( isUpdate && pPk==pIdx && pkChng==0 ){
................................................................................
118440 118724       }
118441 118725       for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
118442 118726         if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
118443 118727       }
118444 118728       if( pSrcIdx==0 ){
118445 118729         return 0;    /* pDestIdx has no corresponding index in pSrc */
118446 118730       }
       118731  +    if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
       118732  +         && sqlite3FaultSim(411)==SQLITE_OK ){
       118733  +      /* The sqlite3FaultSim() call allows this corruption test to be
       118734  +      ** bypassed during testing, in order to exercise other corruption tests
       118735  +      ** further downstream. */
       118736  +      return 0;   /* Corrupt schema - two indexes on the same btree */
       118737  +    }
118447 118738     }
118448 118739   #ifndef SQLITE_OMIT_CHECK
118449 118740     if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
118450 118741       return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
118451 118742     }
118452 118743   #endif
118453 118744   #ifndef SQLITE_OMIT_FOREIGN_KEY
................................................................................
118517 118808         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
118518 118809         sqlite3VdbeVerifyAbortable(v, onError);
118519 118810         addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
118520 118811         VdbeCoverage(v);
118521 118812         sqlite3RowidConstraint(pParse, onError, pDest);
118522 118813         sqlite3VdbeJumpHere(v, addr2);
118523 118814         autoIncStep(pParse, regAutoinc, regRowid);
118524         -    }else if( pDest->pIndex==0 ){
       118815  +    }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
118525 118816         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
118526 118817       }else{
118527 118818         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
118528 118819         assert( (pDest->tabFlags & TF_Autoincrement)==0 );
118529 118820       }
118530 118821       sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
118531 118822       if( db->mDbFlags & DBFLAG_Vacuum ){
................................................................................
118580 118871           if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
118581 118872         }
118582 118873         if( i==pSrcIdx->nColumn ){
118583 118874           idxInsFlags = OPFLAG_USESEEKRESULT;
118584 118875           sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
118585 118876         }
118586 118877       }
118587         -    if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
       118878  +    if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
118588 118879         idxInsFlags |= OPFLAG_NCHANGE;
118589 118880       }
118590 118881       sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
118591 118882       sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
118592 118883       sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
118593 118884       sqlite3VdbeJumpHere(v, addr1);
118594 118885       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
................................................................................
119092 119383                               void (*xStep)(sqlite3_context*,int,sqlite3_value**),
119093 119384                               void (*xFinal)(sqlite3_context*),
119094 119385                               void (*xValue)(sqlite3_context*),
119095 119386                               void (*xInv)(sqlite3_context*,int,sqlite3_value**),
119096 119387                               void(*xDestroy)(void*));
119097 119388     /* Version 3.26.0 and later */
119098 119389     const char *(*normalized_sql)(sqlite3_stmt*);
       119390  +  /* Version 3.28.0 and later */
       119391  +  int (*stmt_isexplain)(sqlite3_stmt*);
       119392  +  int (*value_frombind)(sqlite3_value*);
119099 119393   };
119100 119394   
119101 119395   /*
119102 119396   ** This is the function signature used for all extension entry points.  It
119103 119397   ** is also defined in the file "loadext.c".
119104 119398   */
119105 119399   typedef int (*sqlite3_loadext_entry)(
................................................................................
119381 119675   #define sqlite3_str_errcode            sqlite3_api->str_errcode
119382 119676   #define sqlite3_str_length             sqlite3_api->str_length
119383 119677   #define sqlite3_str_value              sqlite3_api->str_value
119384 119678   /* Version 3.25.0 and later */
119385 119679   #define sqlite3_create_window_function sqlite3_api->create_window_function
119386 119680   /* Version 3.26.0 and later */
119387 119681   #define sqlite3_normalized_sql         sqlite3_api->normalized_sql
       119682  +/* Version 3.28.0 and later */
       119683  +#define sqlite3_stmt_isexplain         sqlite3_api->isexplain
       119684  +#define sqlite3_value_frombind         sqlite3_api->frombind
119388 119685   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
119389 119686   
119390 119687   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
119391 119688     /* This case when the file really is being compiled as a loadable 
119392 119689     ** extension */
119393 119690   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
119394 119691   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
119840 120137     sqlite3_str_errcode,
119841 120138     sqlite3_str_length,
119842 120139     sqlite3_str_value,
119843 120140     /* Version 3.25.0 and later */
119844 120141     sqlite3_create_window_function,
119845 120142     /* Version 3.26.0 and later */
119846 120143   #ifdef SQLITE_ENABLE_NORMALIZE
119847         -  sqlite3_normalized_sql
       120144  +  sqlite3_normalized_sql,
119848 120145   #else
119849         -  0
       120146  +  0,
119850 120147   #endif
       120148  +  /* Version 3.28.0 and later */
       120149  +  sqlite3_stmt_isexplain,
       120150  +  sqlite3_value_frombind
119851 120151   };
119852 120152   
119853 120153   /*
119854 120154   ** Attempt to load an SQLite extension library contained in the file
119855 120155   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
119856 120156   ** default entry point name (sqlite3_extension_init) is used.  Use
119857 120157   ** of the default name is recommended.
................................................................................
129623 129923   
129624 129924   /*
129625 129925   ** Update the accumulator memory cells for an aggregate based on
129626 129926   ** the current cursor position.
129627 129927   **
129628 129928   ** If regAcc is non-zero and there are no min() or max() aggregates
129629 129929   ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
129630         -** registers i register regAcc contains 0. The caller will take care
       129930  +** registers if register regAcc contains 0. The caller will take care
129631 129931   ** of setting and clearing regAcc.
129632 129932   */
129633 129933   static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
129634 129934     Vdbe *v = pParse->pVdbe;
129635 129935     int i;
129636 129936     int regHit = 0;
129637 129937     int addrHitTest = 0;
................................................................................
133715 134015     sqlite3ExprDelete(pParse->db, pInto);
133716 134016     return;
133717 134017   }
133718 134018   
133719 134019   /*
133720 134020   ** This routine implements the OP_Vacuum opcode of the VDBE.
133721 134021   */
133722         -SQLITE_PRIVATE int sqlite3RunVacuum(
       134022  +SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3RunVacuum(
133723 134023     char **pzErrMsg,        /* Write error message here */
133724 134024     sqlite3 *db,            /* Database connection */
133725 134025     int iDb,                /* Which attached DB to vacuum */
133726         -  sqlite3_value *pOut     /* Write results here, if not NULL */
       134026  +  sqlite3_value *pOut     /* Write results here, if not NULL. VACUUM INTO */
133727 134027   ){
133728 134028     int rc = SQLITE_OK;     /* Return code from service routines */
133729 134029     Btree *pMain;           /* The database being vacuumed */
133730 134030     Btree *pTemp;           /* The temporary database we vacuum into */
133731 134031     u32 saved_mDbFlags;     /* Saved value of db->mDbFlags */
133732 134032     u64 saved_flags;        /* Saved value of db->flags */
133733 134033     int saved_nChange;      /* Saved value of db->nChange */
133734 134034     int saved_nTotalChange; /* Saved value of db->nTotalChange */
       134035  +  u32 saved_openFlags;    /* Saved value of db->openFlags */
133735 134036     u8 saved_mTrace;        /* Saved trace settings */
133736 134037     Db *pDb = 0;            /* Database to detach at end of vacuum */
133737 134038     int isMemDb;            /* True if vacuuming a :memory: database */
133738 134039     int nRes;               /* Bytes of reserved space at the end of each page */
133739 134040     int nDb;                /* Number of attached databases */
133740 134041     const char *zDbMain;    /* Schema name of database to vacuum */
133741 134042     const char *zOut;       /* Name of output file */
133742 134043   
133743 134044     if( !db->autoCommit ){
133744 134045       sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
133745         -    return SQLITE_ERROR;
       134046  +    return SQLITE_ERROR; /* IMP: R-12218-18073 */
133746 134047     }
133747 134048     if( db->nVdbeActive>1 ){
133748 134049       sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
133749         -    return SQLITE_ERROR;
       134050  +    return SQLITE_ERROR; /* IMP: R-15610-35227 */
133750 134051     }
       134052  +  saved_openFlags = db->openFlags;
133751 134053     if( pOut ){
133752 134054       if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){
133753 134055         sqlite3SetString(pzErrMsg, db, "non-text filename");
133754 134056         return SQLITE_ERROR;
133755 134057       }
133756 134058       zOut = (const char*)sqlite3_value_text(pOut);
       134059  +    db->openFlags &= ~SQLITE_OPEN_READONLY;
       134060  +    db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
133757 134061     }else{
133758 134062       zOut = "";
133759 134063     }
133760 134064   
133761 134065     /* Save the current value of the database flags so that it can be 
133762 134066     ** restored before returning. Then set the writable-schema flag, and
133763 134067     ** disable CHECK and foreign key constraints.  */
................................................................................
133788 134092     ** actually occurs when doing a vacuum since the vacuum_db is initially
133789 134093     ** empty.  Only the journal header is written.  Apparently it takes more
133790 134094     ** time to parse and run the PRAGMA to turn journalling off than it does
133791 134095     ** to write the journal header file.
133792 134096     */
133793 134097     nDb = db->nDb;
133794 134098     rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
       134099  +  db->openFlags = saved_openFlags;
133795 134100     if( rc!=SQLITE_OK ) goto end_of_vacuum;
133796 134101     assert( (db->nDb-1)==nDb );
133797 134102     pDb = &db->aDb[nDb];
133798 134103     assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
133799 134104     pTemp = pDb->pBt;
133800 134105     if( pOut ){
133801 134106       sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
133802 134107       i64 sz = 0;
133803 134108       if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
133804 134109         rc = SQLITE_ERROR;
133805 134110         sqlite3SetString(pzErrMsg, db, "output file already exists");
133806 134111         goto end_of_vacuum;
133807 134112       }
       134113  +    db->mDbFlags |= DBFLAG_VacuumInto;
133808 134114     }
133809 134115     nRes = sqlite3BtreeGetOptimalReserve(pMain);
133810 134116   
133811 134117     /* A VACUUM cannot change the pagesize of an encrypted database. */
133812 134118   #ifdef SQLITE_HAS_CODEC
133813 134119     if( db->nextPagesize ){
133814 134120       extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
................................................................................
134289 134595   
134290 134596   /*
134291 134597   ** Add a new module argument to pTable->azModuleArg[].
134292 134598   ** The string is not copied - the pointer is stored.  The
134293 134599   ** string will be freed automatically when the table is
134294 134600   ** deleted.
134295 134601   */
134296         -static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
134297         -  int nBytes = sizeof(char *)*(2+pTable->nModuleArg);
       134602  +static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
       134603  +  sqlite3_int64 nBytes = sizeof(char *)*(2+pTable->nModuleArg);
134298 134604     char **azModuleArg;
       134605  +  sqlite3 *db = pParse->db;
       134606  +  if( pTable->nModuleArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
       134607  +    sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
       134608  +  }
134299 134609     azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
134300 134610     if( azModuleArg==0 ){
134301 134611       sqlite3DbFree(db, zArg);
134302 134612     }else{
134303 134613       int i = pTable->nModuleArg++;
134304 134614       azModuleArg[i] = zArg;
134305 134615       azModuleArg[i+1] = 0;
................................................................................
134326 134636     pTable = pParse->pNewTable;
134327 134637     if( pTable==0 ) return;
134328 134638     assert( 0==pTable->pIndex );
134329 134639   
134330 134640     db = pParse->db;
134331 134641   
134332 134642     assert( pTable->nModuleArg==0 );
134333         -  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
134334         -  addModuleArgument(db, pTable, 0);
134335         -  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
       134643  +  addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName));
       134644  +  addModuleArgument(pParse, pTable, 0);
       134645  +  addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
134336 134646     assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
134337 134647          || (pParse->sNameToken.z==pName1->z && pName2->z==0)
134338 134648     );
134339 134649     pParse->sNameToken.n = (int)(
134340 134650         &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
134341 134651     );
134342 134652   
................................................................................
134361 134671   ** virtual table currently under construction in pParse->pTable.
134362 134672   */
134363 134673   static void addArgumentToVtab(Parse *pParse){
134364 134674     if( pParse->sArg.z && pParse->pNewTable ){
134365 134675       const char *z = (const char*)pParse->sArg.z;
134366 134676       int n = pParse->sArg.n;
134367 134677       sqlite3 *db = pParse->db;
134368         -    addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
       134678  +    addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
134369 134679     }
134370 134680   }
134371 134681   
134372 134682   /*
134373 134683   ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
134374 134684   ** has been completely parsed.
134375 134685   */
................................................................................
134650 134960   */
134651 134961   static int growVTrans(sqlite3 *db){
134652 134962     const int ARRAY_INCR = 5;
134653 134963   
134654 134964     /* Grow the sqlite3.aVTrans array if required */
134655 134965     if( (db->nVTrans%ARRAY_INCR)==0 ){
134656 134966       VTable **aVTrans;
134657         -    int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
       134967  +    sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
       134968  +                                 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
134658 134969       aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
134659 134970       if( !aVTrans ){
134660 134971         return SQLITE_NOMEM_BKPT;
134661 134972       }
134662 134973       memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
134663 134974       db->aVTrans = aVTrans;
134664 134975     }
................................................................................
135146 135457       return 0;
135147 135458     }
135148 135459     pMod->pEpoTab = pTab;
135149 135460     pTab->nTabRef = 1;
135150 135461     pTab->pSchema = db->aDb[0].pSchema;
135151 135462     assert( pTab->nModuleArg==0 );
135152 135463     pTab->iPKey = -1;
135153         -  addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
135154         -  addModuleArgument(db, pTab, 0);
135155         -  addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
       135464  +  addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
       135465  +  addModuleArgument(pParse, pTab, 0);
       135466  +  addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
135156 135467     rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
135157 135468     if( rc ){
135158 135469       sqlite3ErrorMsg(pParse, "%s", zErr);
135159 135470       sqlite3DbFree(db, zErr);
135160 135471       sqlite3VtabEponymousTableClear(db, pMod);
135161 135472       return 0;
135162 135473     }
................................................................................
136985 137296       x.iIdxCol = iIdxCol;
136986 137297       x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
136987 137298       sqlite3WalkExpr(&w, pWInfo->pWhere);
136988 137299       sqlite3WalkExprList(&w, pWInfo->pOrderBy);
136989 137300       sqlite3WalkExprList(&w, pWInfo->pResultSet);
136990 137301     }
136991 137302   }
       137303  +
       137304  +/*
       137305  +** The pTruth expression is always true because it is the WHERE clause
       137306  +** a partial index that is driving a query loop.  Look through all of the
       137307  +** WHERE clause terms on the query, and if any of those terms must be
       137308  +** true because pTruth is true, then mark those WHERE clause terms as
       137309  +** coded.
       137310  +*/
       137311  +static void whereApplyPartialIndexConstraints(
       137312  +  Expr *pTruth,
       137313  +  int iTabCur,
       137314  +  WhereClause *pWC
       137315  +){
       137316  +  int i;
       137317  +  WhereTerm *pTerm;
       137318  +  while( pTruth->op==TK_AND ){
       137319  +    whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
       137320  +    pTruth = pTruth->pRight;
       137321  +  }
       137322  +  for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
       137323  +    Expr *pExpr;
       137324  +    if( pTerm->wtFlags & TERM_CODED ) continue;
       137325  +    pExpr = pTerm->pExpr;
       137326  +    if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
       137327  +      pTerm->wtFlags |= TERM_CODED;
       137328  +    }
       137329  +  }
       137330  +}
136992 137331   
136993 137332   /*
136994 137333   ** Generate code for the start of the iLevel-th loop in the WHERE clause
136995 137334   ** implementation described by pWInfo.
136996 137335   */
136997 137336   SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
136998 137337     Parse *pParse,       /* Parsing context */
................................................................................
137594 137933       ** OR clause, since the transformation will become invalid once we
137595 137934       ** move forward to the next index.
137596 137935       ** https://sqlite.org/src/info/4e8e4857d32d401f
137597 137936       */
137598 137937       if( pLevel->iLeftJoin==0 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
137599 137938         whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
137600 137939       }
       137940  +
       137941  +    /* If a partial index is driving the loop, try to eliminate WHERE clause
       137942  +    ** terms from the query that must be true due to the WHERE clause of
       137943  +    ** the partial index
       137944  +    */
       137945  +    if( pIdx->pPartIdxWhere ){
       137946  +      whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
       137947  +    }
137601 137948   
137602 137949       /* Record the instruction used to terminate the loop. */
137603 137950       if( pLoop->wsFlags & WHERE_ONEROW ){
137604 137951         pLevel->op = OP_Noop;
137605 137952       }else if( bRev ){
137606 137953         pLevel->op = OP_Prev;
137607 137954       }else{
................................................................................
137758 138105           if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
137759 138106           if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
137760 138107           testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
137761 138108           pExpr = sqlite3ExprDup(db, pExpr, 0);
137762 138109           pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
137763 138110         }
137764 138111         if( pAndExpr ){
137765         -        pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr);
       138112  +        /* The extra 0x10000 bit on the opcode is masked off and does not
       138113  +        ** become part of the new Expr.op.  However, it does make the
       138114  +        ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
       138115  +        ** prevents sqlite3PExpr() from implementing AND short-circuit 
       138116  +        ** optimization, which we do not want here. */
       138117  +        pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, 0, pAndExpr);
137766 138118         }
137767 138119       }
137768 138120   
137769 138121       /* Run a separate WHERE clause for each term of the OR clause.  After
137770 138122       ** eliminating duplicates from other WHERE clauses, the action for each
137771 138123       ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
137772 138124       */
................................................................................
137988 138340           ** that compares BLOBs. */
137989 138341   #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
137990 138342           continue;
137991 138343   #else
137992 138344           u32 x = pLevel->iLikeRepCntr;
137993 138345           if( x>0 ){
137994 138346             skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
       138347  +          VdbeCoverageIf(v, (x&1)==1);
       138348  +          VdbeCoverageIf(v, (x&1)==0);
137995 138349           }
137996         -        VdbeCoverage(v);
137997 138350   #endif
137998 138351         }
137999 138352   #ifdef WHERETRACE_ENABLED /* 0xffff */
138000 138353         if( sqlite3WhereTrace ){
138001 138354           VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
138002 138355                            pWC->nTerm-j, pTerm, iLoop));
138003 138356         }
................................................................................
139591 139944       assert( p->x.pList==0 );
139592 139945     }else if( ExprHasProperty(p, EP_xIsSelect) ){
139593 139946       if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
139594 139947       mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
139595 139948     }else if( p->x.pList ){
139596 139949       mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
139597 139950     }
       139951  +#ifndef SQLITE_OMIT_WINDOWFUNC
       139952  +  if( p->op==TK_FUNCTION && p->y.pWin ){
       139953  +    mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
       139954  +    mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
       139955  +  }
       139956  +#endif
139598 139957     return mask;
139599 139958   }
139600 139959   SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
139601 139960     return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
139602 139961   }
139603 139962   SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
139604 139963     int i;
................................................................................
143007 143366   
143008 143367     /* First call xBestIndex() with all constraints usable. */
143009 143368     WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
143010 143369     WHERETRACE(0x40, ("  VirtualOne: all usable\n"));
143011 143370     rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
143012 143371   
143013 143372     /* If the call to xBestIndex() with all terms enabled produced a plan
143014         -  ** that does not require any source tables (IOW: a plan with mBest==0),
143015         -  ** then there is no point in making any further calls to xBestIndex() 
143016         -  ** since they will all return the same result (if the xBestIndex()
143017         -  ** implementation is sane). */
143018         -  if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){
       143373  +  ** that does not require any source tables (IOW: a plan with mBest==0)
       143374  +  ** and does not use an IN(...) operator, then there is no point in making 
       143375  +  ** any further calls to xBestIndex() since they will all return the same
       143376  +  ** result (if the xBestIndex() implementation is sane). */
       143377  +  if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
143019 143378       int seenZero = 0;             /* True if a plan with no prereqs seen */
143020 143379       int seenZeroNoIN = 0;         /* Plan with no prereqs and no IN(...) seen */
143021 143380       Bitmask mPrev = 0;
143022 143381       Bitmask mBestNoIn = 0;
143023 143382   
143024 143383       /* If the plan produced by the earlier call uses an IN(...) term, call
143025 143384       ** xBestIndex again, this time with IN(...) terms disabled. */
................................................................................
145244 145603         p->nValue++;
145245 145604         p->nStep = 0;
145246 145605       }
145247 145606       sqlite3_result_int64(pCtx, p->nValue);
145248 145607     }
145249 145608   }
145250 145609   
       145610  +/*
       145611  +** Implementation of built-in window function nth_value(). This
       145612  +** implementation is used in "slow mode" only - when the EXCLUDE clause
       145613  +** is not set to the default value "NO OTHERS".
       145614  +*/
       145615  +struct NthValueCtx {
       145616  +  i64 nStep;
       145617  +  sqlite3_value *pValue;
       145618  +};
       145619  +static void nth_valueStepFunc(
       145620  +  sqlite3_context *pCtx, 
       145621  +  int nArg,
       145622  +  sqlite3_value **apArg
       145623  +){
       145624  +  struct NthValueCtx *p;
       145625  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145626  +  if( p ){
       145627  +    i64 iVal;
       145628  +    switch( sqlite3_value_numeric_type(apArg[1]) ){
       145629  +      case SQLITE_INTEGER:
       145630  +        iVal = sqlite3_value_int64(apArg[1]);
       145631  +        break;
       145632  +      case SQLITE_FLOAT: {
       145633  +        double fVal = sqlite3_value_double(apArg[1]);
       145634  +        if( ((i64)fVal)!=fVal ) goto error_out;
       145635  +        iVal = (i64)fVal;
       145636  +        break;
       145637  +      }
       145638  +      default:
       145639  +        goto error_out;
       145640  +    }
       145641  +    if( iVal<=0 ) goto error_out;
       145642  +
       145643  +    p->nStep++;
       145644  +    if( iVal==p->nStep ){
       145645  +      p->pValue = sqlite3_value_dup(apArg[0]);
       145646  +      if( !p->pValue ){
       145647  +        sqlite3_result_error_nomem(pCtx);
       145648  +      }
       145649  +    }
       145650  +  }
       145651  +  UNUSED_PARAMETER(nArg);
       145652  +  UNUSED_PARAMETER(apArg);
       145653  +  return;
       145654  +
       145655  + error_out:
       145656  +  sqlite3_result_error(
       145657  +      pCtx, "second argument to nth_value must be a positive integer", -1
       145658  +  );
       145659  +}
       145660  +static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
       145661  +  struct NthValueCtx *p;
       145662  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
       145663  +  if( p && p->pValue ){
       145664  +    sqlite3_result_value(pCtx, p->pValue);
       145665  +    sqlite3_value_free(p->pValue);
       145666  +    p->pValue = 0;
       145667  +  }
       145668  +}
       145669  +#define nth_valueInvFunc noopStepFunc
       145670  +#define nth_valueValueFunc noopValueFunc
       145671  +
       145672  +static void first_valueStepFunc(
       145673  +  sqlite3_context *pCtx, 
       145674  +  int nArg,
       145675  +  sqlite3_value **apArg
       145676  +){
       145677  +  struct NthValueCtx *p;
       145678  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145679  +  if( p && p->pValue==0 ){
       145680  +    p->pValue = sqlite3_value_dup(apArg[0]);
       145681  +    if( !p->pValue ){
       145682  +      sqlite3_result_error_nomem(pCtx);
       145683  +    }
       145684  +  }
       145685  +  UNUSED_PARAMETER(nArg);
       145686  +  UNUSED_PARAMETER(apArg);
       145687  +}
       145688  +static void first_valueFinalizeFunc(sqlite3_context *pCtx){
       145689  +  struct NthValueCtx *p;
       145690  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145691  +  if( p && p->pValue ){
       145692  +    sqlite3_result_value(pCtx, p->pValue);
       145693  +    sqlite3_value_free(p->pValue);
       145694  +    p->pValue = 0;
       145695  +  }
       145696  +}
       145697  +#define first_valueInvFunc noopStepFunc
       145698  +#define first_valueValueFunc noopValueFunc
       145699  +
145251 145700   /*
145252 145701   ** Implementation of built-in window function rank(). Assumes that
145253 145702   ** the window frame has been set to:
145254 145703   **
145255 145704   **   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
145256 145705   */
145257 145706   static void rankStepFunc(
................................................................................
145279 145728     }
145280 145729   }
145281 145730   
145282 145731   /*
145283 145732   ** Implementation of built-in window function percent_rank(). Assumes that
145284 145733   ** the window frame has been set to:
145285 145734   **
145286         -**   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
       145735  +**   GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
145287 145736   */
145288 145737   static void percent_rankStepFunc(
145289 145738     sqlite3_context *pCtx, 
145290 145739     int nArg,
145291 145740     sqlite3_value **apArg
145292 145741   ){
145293 145742     struct CallCount *p;
145294         -  UNUSED_PARAMETER(nArg); assert( nArg==1 );
145295         -
       145743  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145744  +  UNUSED_PARAMETER(apArg);
       145745  +  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145746  +  if( p ){
       145747  +    p->nTotal++;
       145748  +  }
       145749  +}
       145750  +static void percent_rankInvFunc(
       145751  +  sqlite3_context *pCtx, 
       145752  +  int nArg,
       145753  +  sqlite3_value **apArg
       145754  +){
       145755  +  struct CallCount *p;
       145756  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145757  +  UNUSED_PARAMETER(apArg);
145296 145758     p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145297         -  if( p ){
145298         -    if( p->nTotal==0 ){
145299         -      p->nTotal = sqlite3_value_int64(apArg[0]);
145300         -    }
145301         -    p->nStep++;
145302         -    if( p->nValue==0 ){
145303         -      p->nValue = p->nStep;
145304         -    }
145305         -  }
       145759  +  p->nStep++;
145306 145760   }
145307 145761   static void percent_rankValueFunc(sqlite3_context *pCtx){
145308 145762     struct CallCount *p;
145309 145763     p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145310 145764     if( p ){
       145765  +    p->nValue = p->nStep;
145311 145766       if( p->nTotal>1 ){
145312         -      double r = (double)(p->nValue-1) / (double)(p->nTotal-1);
       145767  +      double r = (double)p->nValue / (double)(p->nTotal-1);
145313 145768         sqlite3_result_double(pCtx, r);
145314 145769       }else{
145315 145770         sqlite3_result_double(pCtx, 0.0);
145316 145771       }
145317         -    p->nValue = 0;
145318 145772     }
145319 145773   }
       145774  +#define percent_rankFinalizeFunc percent_rankValueFunc
145320 145775   
145321 145776   /*
145322 145777   ** Implementation of built-in window function cume_dist(). Assumes that
145323 145778   ** the window frame has been set to:
145324 145779   **
145325         -**   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
       145780  +**   GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
145326 145781   */
145327 145782   static void cume_distStepFunc(
145328 145783     sqlite3_context *pCtx, 
145329 145784     int nArg,
145330 145785     sqlite3_value **apArg
145331 145786   ){
145332 145787     struct CallCount *p;
145333         -  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
145334         -
       145788  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145789  +  UNUSED_PARAMETER(apArg);
       145790  +  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145791  +  if( p ){
       145792  +    p->nTotal++;
       145793  +  }
       145794  +}
       145795  +static void cume_distInvFunc(
       145796  +  sqlite3_context *pCtx, 
       145797  +  int nArg,
       145798  +  sqlite3_value **apArg
       145799  +){
       145800  +  struct CallCount *p;
       145801  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145802  +  UNUSED_PARAMETER(apArg);
145335 145803     p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145336         -  if( p ){
145337         -    if( p->nTotal==0 ){
145338         -      p->nTotal = sqlite3_value_int64(apArg[0]);
145339         -    }
145340         -    p->nStep++;
145341         -  }
       145804  +  p->nStep++;
145342 145805   }
145343 145806   static void cume_distValueFunc(sqlite3_context *pCtx){
145344 145807     struct CallCount *p;
145345         -  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145346         -  if( p && p->nTotal ){
       145808  +  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
       145809  +  if( p ){
145347 145810       double r = (double)(p->nStep) / (double)(p->nTotal);
145348 145811       sqlite3_result_double(pCtx, r);
145349 145812     }
145350 145813   }
       145814  +#define cume_distFinalizeFunc cume_distValueFunc
145351 145815   
145352 145816   /*
145353 145817   ** Context object for ntile() window function.
145354 145818   */
145355 145819   struct NtileCtx {
145356 145820     i64 nTotal;                     /* Total rows in partition */
145357 145821     i64 nParam;                     /* Parameter passed to ntile(N) */
................................................................................
145358 145822     i64 iRow;                       /* Current row */
145359 145823   };
145360 145824   
145361 145825   /*
145362 145826   ** Implementation of ntile(). This assumes that the window frame has
145363 145827   ** been coerced to:
145364 145828   **
145365         -**   ROWS UNBOUNDED PRECEDING AND CURRENT ROW
       145829  +**   ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
145366 145830   */
145367 145831   static void ntileStepFunc(
145368 145832     sqlite3_context *pCtx, 
145369 145833     int nArg,
145370 145834     sqlite3_value **apArg
145371 145835   ){
145372 145836     struct NtileCtx *p;
145373         -  assert( nArg==2 ); UNUSED_PARAMETER(nArg);
       145837  +  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
145374 145838     p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145375 145839     if( p ){
145376 145840       if( p->nTotal==0 ){
145377 145841         p->nParam = sqlite3_value_int64(apArg[0]);
145378         -      p->nTotal = sqlite3_value_int64(apArg[1]);
145379 145842         if( p->nParam<=0 ){
145380 145843           sqlite3_result_error(
145381 145844               pCtx, "argument of ntile must be a positive integer", -1
145382 145845           );
145383 145846         }
145384 145847       }
145385         -    p->iRow++;
       145848  +    p->nTotal++;
145386 145849     }
       145850  +}
       145851  +static void ntileInvFunc(
       145852  +  sqlite3_context *pCtx, 
       145853  +  int nArg,
       145854  +  sqlite3_value **apArg
       145855  +){
       145856  +  struct NtileCtx *p;
       145857  +  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
       145858  +  UNUSED_PARAMETER(apArg);
       145859  +  p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145860  +  p->iRow++;
145387 145861   }
145388 145862   static void ntileValueFunc(sqlite3_context *pCtx){
145389 145863     struct NtileCtx *p;
145390 145864     p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145391 145865     if( p && p->nParam>0 ){
145392 145866       int nSize = (p->nTotal / p->nParam);
145393 145867       if( nSize==0 ){
145394         -      sqlite3_result_int64(pCtx, p->iRow);
       145868  +      sqlite3_result_int64(pCtx, p->iRow+1);
145395 145869       }else{
145396 145870         i64 nLarge = p->nTotal - p->nParam*nSize;
145397 145871         i64 iSmall = nLarge*(nSize+1);
145398         -      i64 iRow = p->iRow-1;
       145872  +      i64 iRow = p->iRow;
145399 145873   
145400 145874         assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
145401 145875   
145402 145876         if( iRow<iSmall ){
145403 145877           sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
145404 145878         }else{
145405 145879           sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
145406 145880         }
145407 145881       }
145408 145882     }
145409 145883   }
       145884  +#define ntileFinalizeFunc ntileValueFunc
145410 145885   
145411 145886   /*
145412 145887   ** Context object for last_value() window function.
145413 145888   */
145414 145889   struct LastValueCtx {
145415 145890     sqlite3_value *pVal;
145416 145891     int nVal;
................................................................................
145452 145927         sqlite3_value_free(p->pVal);
145453 145928         p->pVal = 0;
145454 145929       }
145455 145930     }
145456 145931   }
145457 145932   static void last_valueValueFunc(sqlite3_context *pCtx){
145458 145933     struct LastValueCtx *p;
145459         -  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145934  +  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
145460 145935     if( p && p->pVal ){
145461 145936       sqlite3_result_value(pCtx, p->pVal);
145462 145937     }
145463 145938   }
145464 145939   static void last_valueFinalizeFunc(sqlite3_context *pCtx){
145465 145940     struct LastValueCtx *p;
145466 145941     p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
................................................................................
145542 146017   ** Register those built-in window functions that are not also aggregates.
145543 146018   */
145544 146019   SQLITE_PRIVATE void sqlite3WindowFunctions(void){
145545 146020     static FuncDef aWindowFuncs[] = {
145546 146021       WINDOWFUNCX(row_number, 0, 0),
145547 146022       WINDOWFUNCX(dense_rank, 0, 0),
145548 146023       WINDOWFUNCX(rank, 0, 0),
145549         -    WINDOWFUNCX(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE),
145550         -    WINDOWFUNCX(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE),
145551         -    WINDOWFUNCX(ntile, 1, SQLITE_FUNC_WINDOW_SIZE),
       146024  +    WINDOWFUNCALL(percent_rank, 0, 0),
       146025  +    WINDOWFUNCALL(cume_dist, 0, 0),
       146026  +    WINDOWFUNCALL(ntile, 1, 0),
145552 146027       WINDOWFUNCALL(last_value, 1, 0),
145553         -    WINDOWFUNCNOOP(nth_value, 2, 0),
145554         -    WINDOWFUNCNOOP(first_value, 1, 0),
       146028  +    WINDOWFUNCALL(nth_value, 2, 0),
       146029  +    WINDOWFUNCALL(first_value, 1, 0),
145555 146030       WINDOWFUNCNOOP(lead, 1, 0),
145556 146031       WINDOWFUNCNOOP(lead, 2, 0),
145557 146032       WINDOWFUNCNOOP(lead, 3, 0),
145558 146033       WINDOWFUNCNOOP(lag, 1, 0),
145559 146034       WINDOWFUNCNOOP(lag, 2, 0),
145560 146035       WINDOWFUNCNOOP(lag, 3, 0),
145561 146036     };
145562 146037     sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
145563 146038   }
       146039  +
       146040  +static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
       146041  +  Window *p;
       146042  +  for(p=pList; p; p=p->pNextWin){
       146043  +    if( sqlite3StrICmp(p->zName, zName)==0 ) break;
       146044  +  }
       146045  +  if( p==0 ){
       146046  +    sqlite3ErrorMsg(pParse, "no such window: %s", zName);
       146047  +  }
       146048  +  return p;
       146049  +}
145564 146050   
145565 146051   /*
145566 146052   ** This function is called immediately after resolving the function name
145567 146053   ** for a window function within a SELECT statement. Argument pList is a
145568 146054   ** linked list of WINDOW definitions for the current SELECT statement.
145569 146055   ** Argument pFunc is the function definition just resolved and pWin
145570 146056   ** is the Window object representing the associated OVER clause. This
................................................................................
145581 146067   */
145582 146068   SQLITE_PRIVATE void sqlite3WindowUpdate(
145583 146069     Parse *pParse, 
145584 146070     Window *pList,                  /* List of named windows for this SELECT */
145585 146071     Window *pWin,                   /* Window frame to update */
145586 146072     FuncDef *pFunc                  /* Window function definition */
145587 146073   ){
145588         -  if( pWin->zName && pWin->eType==0 ){
145589         -    Window *p;
145590         -    for(p=pList; p; p=p->pNextWin){
145591         -      if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break;
145592         -    }
145593         -    if( p==0 ){
145594         -      sqlite3ErrorMsg(pParse, "no such window: %s", pWin->zName);
145595         -      return;
145596         -    }
       146074  +  if( pWin->zName && pWin->eFrmType==0 ){
       146075  +    Window *p = windowFind(pParse, pList, pWin->zName);
       146076  +    if( p==0 ) return;
145597 146077       pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
145598 146078       pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
145599 146079       pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
145600 146080       pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
145601 146081       pWin->eStart = p->eStart;
145602 146082       pWin->eEnd = p->eEnd;
145603         -    pWin->eType = p->eType;
       146083  +    pWin->eFrmType = p->eFrmType;
       146084  +    pWin->eExclude = p->eExclude;
       146085  +  }else{
       146086  +    sqlite3WindowChain(pParse, pWin, pList);
145604 146087     }
       146088  +  if( (pWin->eFrmType==TK_RANGE)
       146089  +   && (pWin->pStart || pWin->pEnd) 
       146090  +   && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
       146091  +  ){
       146092  +    sqlite3ErrorMsg(pParse, 
       146093  +      "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
       146094  +    );
       146095  +  }else
145605 146096     if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
145606 146097       sqlite3 *db = pParse->db;
145607 146098       if( pWin->pFilter ){
145608 146099         sqlite3ErrorMsg(pParse, 
145609 146100             "FILTER clause may only be used with aggregate window functions"
145610 146101         );
145611         -    }else
145612         -    if( pFunc->zName==row_numberName || pFunc->zName==ntileName ){
145613         -      sqlite3ExprDelete(db, pWin->pStart);
145614         -      sqlite3ExprDelete(db, pWin->pEnd);
145615         -      pWin->pStart = pWin->pEnd = 0;
145616         -      pWin->eType = TK_ROWS;
145617         -      pWin->eStart = TK_UNBOUNDED;
145618         -      pWin->eEnd = TK_CURRENT;
145619         -    }else
145620         -
145621         -    if( pFunc->zName==dense_rankName || pFunc->zName==rankName
145622         -     || pFunc->zName==percent_rankName || pFunc->zName==cume_distName
145623         -    ){
145624         -      sqlite3ExprDelete(db, pWin->pStart);
145625         -      sqlite3ExprDelete(db, pWin->pEnd);
145626         -      pWin->pStart = pWin->pEnd = 0;
145627         -      pWin->eType = TK_RANGE;
145628         -      pWin->eStart = TK_UNBOUNDED;
145629         -      pWin->eEnd = TK_CURRENT;
       146102  +    }else{
       146103  +      struct WindowUpdate {
       146104  +        const char *zFunc;
       146105  +        int eFrmType;
       146106  +        int eStart;
       146107  +        int eEnd;
       146108  +      } aUp[] = {
       146109  +        { row_numberName,   TK_ROWS,   TK_UNBOUNDED, TK_CURRENT }, 
       146110  +        { dense_rankName,   TK_RANGE,  TK_UNBOUNDED, TK_CURRENT }, 
       146111  +        { rankName,         TK_RANGE,  TK_UNBOUNDED, TK_CURRENT }, 
       146112  +        { percent_rankName, TK_GROUPS, TK_CURRENT,   TK_UNBOUNDED }, 
       146113  +        { cume_distName,    TK_GROUPS, TK_FOLLOWING, TK_UNBOUNDED }, 
       146114  +        { ntileName,        TK_ROWS,   TK_CURRENT,   TK_UNBOUNDED }, 
       146115  +        { leadName,         TK_ROWS,   TK_UNBOUNDED, TK_UNBOUNDED }, 
       146116  +        { lagName,          TK_ROWS,   TK_UNBOUNDED, TK_CURRENT }, 
       146117  +      };
       146118  +      int i;
       146119  +      for(i=0; i<ArraySize(aUp); i++){
       146120  +        if( pFunc->zName==aUp[i].zFunc ){
       146121  +          sqlite3ExprDelete(db, pWin->pStart);
       146122  +          sqlite3ExprDelete(db, pWin->pEnd);
       146123  +          pWin->pEnd = pWin->pStart = 0;
       146124  +          pWin->eFrmType = aUp[i].eFrmType;
       146125  +          pWin->eStart = aUp[i].eStart;
       146126  +          pWin->eEnd = aUp[i].eEnd;
       146127  +          pWin->eExclude = 0;
       146128  +          if( pWin->eStart==TK_FOLLOWING ){
       146129  +            pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
       146130  +          }
       146131  +          break;
       146132  +        }
       146133  +      }
145630 146134       }
145631 146135     }
145632 146136     pWin->pFunc = pFunc;
145633 146137   }
145634 146138   
145635 146139   /*
145636 146140   ** Context object passed through sqlite3WalkExprList() to
................................................................................
145827 146331         }
145828 146332       }
145829 146333   
145830 146334       /* Assign a cursor number for the ephemeral table used to buffer rows.
145831 146335       ** The OpenEphemeral instruction is coded later, after it is known how
145832 146336       ** many columns the table will have.  */
145833 146337       pMWin->iEphCsr = pParse->nTab++;
       146338  +    pParse->nTab += 3;
145834 146339   
145835 146340       selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, &pSublist);
145836 146341       selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, &pSublist);
145837 146342       pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
145838 146343   
145839 146344       /* Append the PARTITION BY and ORDER BY expressions to the to the 
145840 146345       ** sub-select expression list. They are required to figure out where 
................................................................................
145882 146387         }else{
145883 146388           pSub->selFlags |= SF_Expanded;
145884 146389           p->selFlags &= ~SF_Aggregate;
145885 146390           sqlite3SelectPrep(pParse, pSub, 0);
145886 146391         }
145887 146392   
145888 146393         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
       146394  +      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
       146395  +      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
       146396  +      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
145889 146397       }else{
145890 146398         sqlite3SelectDelete(db, pSub);
145891 146399       }
145892 146400       if( db->mallocFailed ) rc = SQLITE_NOMEM;
145893 146401     }
145894 146402   
145895 146403     return rc;
................................................................................
145902 146410     if( p ){
145903 146411       sqlite3ExprDelete(db, p->pFilter);
145904 146412       sqlite3ExprListDelete(db, p->pPartition);
145905 146413       sqlite3ExprListDelete(db, p->pOrderBy);
145906 146414       sqlite3ExprDelete(db, p->pEnd);
145907 146415       sqlite3ExprDelete(db, p->pStart);
145908 146416       sqlite3DbFree(db, p->zName);
       146417  +    sqlite3DbFree(db, p->zBase);
145909 146418       sqlite3DbFree(db, p);
145910 146419     }
145911 146420   }
145912 146421   
145913 146422   /*
145914 146423   ** Free the linked list of Window objects starting at the second argument.
145915 146424   */
................................................................................
145938 146447   }
145939 146448   
145940 146449   /*
145941 146450   ** Allocate and return a new Window object describing a Window Definition.
145942 146451   */
145943 146452   SQLITE_PRIVATE Window *sqlite3WindowAlloc(
145944 146453     Parse *pParse,    /* Parsing context */
145945         -  int eType,        /* Frame type. TK_RANGE or TK_ROWS */
       146454  +  int eType,        /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
145946 146455     int eStart,       /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
145947 146456     Expr *pStart,     /* Start window size if TK_PRECEDING or FOLLOWING */
145948 146457     int eEnd,         /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
145949         -  Expr *pEnd        /* End window size if TK_FOLLOWING or PRECEDING */
       146458  +  Expr *pEnd,       /* End window size if TK_FOLLOWING or PRECEDING */
       146459  +  u8 eExclude       /* EXCLUDE clause */
145950 146460   ){
145951 146461     Window *pWin = 0;
       146462  +  int bImplicitFrame = 0;
145952 146463   
145953 146464     /* Parser assures the following: */
145954         -  assert( eType==TK_RANGE || eType==TK_ROWS );
       146465  +  assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS );
145955 146466     assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
145956 146467              || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
145957 146468     assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
145958 146469              || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
145959 146470     assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
145960 146471     assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
145961 146472   
145962         -
145963         -  /* If a frame is declared "RANGE" (not "ROWS"), then it may not use
145964         -  ** either "<expr> PRECEDING" or "<expr> FOLLOWING".
145965         -  */
145966         -  if( eType==TK_RANGE && (pStart!=0 || pEnd!=0) ){
145967         -    sqlite3ErrorMsg(pParse, "RANGE must use only UNBOUNDED or CURRENT ROW");
145968         -    goto windowAllocErr;
       146473  +  if( eType==0 ){
       146474  +    bImplicitFrame = 1;
       146475  +    eType = TK_RANGE;
145969 146476     }
145970 146477   
145971 146478     /* Additionally, the
145972 146479     ** starting boundary type may not occur earlier in the following list than
145973 146480     ** the ending boundary type:
145974 146481     **
145975 146482     **   UNBOUNDED PRECEDING
................................................................................
145981 146488     ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
145982 146489     ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
145983 146490     ** frame boundary.
145984 146491     */
145985 146492     if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
145986 146493      || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
145987 146494     ){
145988         -    sqlite3ErrorMsg(pParse, "unsupported frame delimiter for ROWS");
       146495  +    sqlite3ErrorMsg(pParse, "unsupported frame specification");
145989 146496       goto windowAllocErr;
145990 146497     }
145991 146498   
145992 146499     pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
145993 146500     if( pWin==0 ) goto windowAllocErr;
145994         -  pWin->eType = eType;
       146501  +  pWin->eFrmType = eType;
145995 146502     pWin->eStart = eStart;
145996 146503     pWin->eEnd = eEnd;
       146504  +  if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
       146505  +    eExclude = TK_NO;
       146506  +  }
       146507  +  pWin->eExclude = eExclude;
       146508  +  pWin->bImplicitFrame = bImplicitFrame;
145997 146509     pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
145998 146510     pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
145999 146511     return pWin;
146000 146512   
146001 146513   windowAllocErr:
146002 146514     sqlite3ExprDelete(pParse->db, pEnd);
146003 146515     sqlite3ExprDelete(pParse->db, pStart);
146004 146516     return 0;
146005 146517   }
       146518  +
       146519  +/*
       146520  +** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
       146521  +** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
       146522  +** equivalent nul-terminated string.
       146523  +*/
       146524  +SQLITE_PRIVATE Window *sqlite3WindowAssemble(
       146525  +  Parse *pParse, 
       146526  +  Window *pWin, 
       146527  +  ExprList *pPartition, 
       146528  +  ExprList *pOrderBy, 
       146529  +  Token *pBase
       146530  +){
       146531  +  if( pWin ){
       146532  +    pWin->pPartition = pPartition;
       146533  +    pWin->pOrderBy = pOrderBy;
       146534  +    if( pBase ){
       146535  +      pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
       146536  +    }
       146537  +  }else{
       146538  +    sqlite3ExprListDelete(pParse->db, pPartition);
       146539  +    sqlite3ExprListDelete(pParse->db, pOrderBy);
       146540  +  }
       146541  +  return pWin;
       146542  +}
       146543  +
       146544  +/*
       146545  +** Window *pWin has just been created from a WINDOW clause. Tokne pBase
       146546  +** is the base window. Earlier windows from the same WINDOW clause are
       146547  +** stored in the linked list starting at pWin->pNextWin. This function
       146548  +** either updates *pWin according to the base specification, or else
       146549  +** leaves an error in pParse.
       146550  +*/
       146551  +SQLITE_PRIVATE void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
       146552  +  if( pWin->zBase ){
       146553  +    sqlite3 *db = pParse->db;
       146554  +    Window *pExist = windowFind(pParse, pList, pWin->zBase);
       146555  +    if( pExist ){
       146556  +      const char *zErr = 0;
       146557  +      /* Check for errors */
       146558  +      if( pWin->pPartition ){
       146559  +        zErr = "PARTITION clause";
       146560  +      }else if( pExist->pOrderBy && pWin->pOrderBy ){
       146561  +        zErr = "ORDER BY clause";
       146562  +      }else if( pExist->bImplicitFrame==0 ){
       146563  +        zErr = "frame specification";
       146564  +      }
       146565  +      if( zErr ){
       146566  +        sqlite3ErrorMsg(pParse, 
       146567  +            "cannot override %s of window: %s", zErr, pWin->zBase
       146568  +        );
       146569  +      }else{
       146570  +        pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
       146571  +        if( pExist->pOrderBy ){
       146572  +          assert( pWin->pOrderBy==0 );
       146573  +          pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
       146574  +        }
       146575  +        sqlite3DbFree(db, pWin->zBase);
       146576  +        pWin->zBase = 0;
       146577  +      }
       146578  +    }
       146579  +  }
       146580  +}
146006 146581   
146007 146582   /*
146008 146583   ** Attach window object pWin to expression p.
146009 146584   */
146010 146585   SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
146011 146586     if( p ){
146012 146587       assert( p->op==TK_FUNCTION );
................................................................................
146028 146603   }
146029 146604   
146030 146605   /*
146031 146606   ** Return 0 if the two window objects are identical, or non-zero otherwise.
146032 146607   ** Identical window objects can be processed in a single scan.
146033 146608   */
146034 146609   SQLITE_PRIVATE int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
146035         -  if( p1->eType!=p2->eType ) return 1;
       146610  +  if( p1->eFrmType!=p2->eFrmType ) return 1;
146036 146611     if( p1->eStart!=p2->eStart ) return 1;
146037 146612     if( p1->eEnd!=p2->eEnd ) return 1;
       146613  +  if( p1->eExclude!=p2->eExclude ) return 1;
146038 146614     if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
146039 146615     if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
146040 146616     if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
146041 146617     if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
146042 146618     return 0;
146043 146619   }
146044 146620   
................................................................................
146047 146623   ** This is called by code in select.c before it calls sqlite3WhereBegin()
146048 146624   ** to begin iterating through the sub-query results. It is used to allocate
146049 146625   ** and initialize registers and cursors used by sqlite3WindowCodeStep().
146050 146626   */
146051 146627   SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
146052 146628     Window *pWin;
146053 146629     Vdbe *v = sqlite3GetVdbe(pParse);
146054         -  int nPart = (pMWin->pPartition ? pMWin->pPartition->nExpr : 0);
146055         -  nPart += (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
146056         -  if( nPart ){
       146630  +
       146631  +  /* Allocate registers to use for PARTITION BY values, if any. Initialize
       146632  +  ** said registers to NULL.  */
       146633  +  if( pMWin->pPartition ){
       146634  +    int nExpr = pMWin->pPartition->nExpr;
146057 146635       pMWin->regPart = pParse->nMem+1;
146058         -    pParse->nMem += nPart;
146059         -    sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1);
       146636  +    pParse->nMem += nExpr;
       146637  +    sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
       146638  +  }
       146639  +
       146640  +  pMWin->regOne = ++pParse->nMem;
       146641  +  sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
       146642  +
       146643  +  if( pMWin->eExclude ){
       146644  +    pMWin->regStartRowid = ++pParse->nMem;
       146645  +    pMWin->regEndRowid = ++pParse->nMem;
       146646  +    pMWin->csrApp = pParse->nTab++;
       146647  +    sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
       146648  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
       146649  +    sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
       146650  +    return;
146060 146651     }
146061 146652   
146062 146653     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146063 146654       FuncDef *p = pWin->pFunc;
146064 146655       if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
146065 146656         /* The inline versions of min() and max() require a single ephemeral
146066 146657         ** table and 3 registers. The registers are used as follows:
................................................................................
146081 146672         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
146082 146673         sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
146083 146674         sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
146084 146675       }
146085 146676       else if( p->zName==nth_valueName || p->zName==first_valueName ){
146086 146677         /* Allocate two registers at pWin->regApp. These will be used to
146087 146678         ** store the start and end index of the current frame.  */
146088         -      assert( pMWin->iEphCsr );
146089 146679         pWin->regApp = pParse->nMem+1;
146090 146680         pWin->csrApp = pParse->nTab++;
146091 146681         pParse->nMem += 2;
146092 146682         sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
146093 146683       }
146094 146684       else if( p->zName==leadName || p->zName==lagName ){
146095         -      assert( pMWin->iEphCsr );
146096 146685         pWin->csrApp = pParse->nTab++;
146097 146686         sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
146098 146687       }
146099 146688     }
146100 146689   }
       146690  +
       146691  +#define WINDOW_STARTING_INT  0
       146692  +#define WINDOW_ENDING_INT    1
       146693  +#define WINDOW_NTH_VALUE_INT 2
       146694  +#define WINDOW_STARTING_NUM  3
       146695  +#define WINDOW_ENDING_NUM    4
146101 146696   
146102 146697   /*
146103 146698   ** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
146104 146699   ** value of the second argument to nth_value() (eCond==2) has just been
146105 146700   ** evaluated and the result left in register reg. This function generates VM
146106 146701   ** code to check that the value is a non-negative integer and throws an
146107 146702   ** exception if it is not.
146108 146703   */
146109         -static void windowCheckIntValue(Parse *pParse, int reg, int eCond){
       146704  +static void windowCheckValue(Parse *pParse, int reg, int eCond){
146110 146705     static const char *azErr[] = {
146111 146706       "frame starting offset must be a non-negative integer",
146112 146707       "frame ending offset must be a non-negative integer",
146113         -    "second argument to nth_value must be a positive integer"
       146708  +    "second argument to nth_value must be a positive integer",
       146709  +    "frame starting offset must be a non-negative number",
       146710  +    "frame ending offset must be a non-negative number",
146114 146711     };
146115         -  static int aOp[] = { OP_Ge, OP_Ge, OP_Gt };
       146712  +  static int aOp[] = { OP_Ge, OP_Ge, OP_Gt, OP_Ge, OP_Ge };
146116 146713     Vdbe *v = sqlite3GetVdbe(pParse);
146117 146714     int regZero = sqlite3GetTempReg(pParse);
146118         -  assert( eCond==0 || eCond==1 || eCond==2 );
       146715  +  assert( eCond>=0 && eCond<ArraySize(azErr) );
146119 146716     sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
146120         -  sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
146121         -  VdbeCoverageIf(v, eCond==0);
146122         -  VdbeCoverageIf(v, eCond==1);
146123         -  VdbeCoverageIf(v, eCond==2);
       146717  +  if( eCond>=WINDOW_STARTING_NUM ){
       146718  +    int regString = sqlite3GetTempReg(pParse);
       146719  +    sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
       146720  +    sqlite3VdbeAddOp3(v, OP_Ge, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
       146721  +    sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC|SQLITE_JUMPIFNULL);
       146722  +    VdbeCoverage(v);
       146723  +    assert( eCond==3 || eCond==4 );
       146724  +    VdbeCoverageIf(v, eCond==3);
       146725  +    VdbeCoverageIf(v, eCond==4);
       146726  +  }else{
       146727  +    sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
       146728  +    VdbeCoverage(v);
       146729  +    assert( eCond==0 || eCond==1 || eCond==2 );
       146730  +    VdbeCoverageIf(v, eCond==0);
       146731  +    VdbeCoverageIf(v, eCond==1);
       146732  +    VdbeCoverageIf(v, eCond==2);
       146733  +  }
146124 146734     sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
146125         -  VdbeCoverageNeverNullIf(v, eCond==0);
146126         -  VdbeCoverageNeverNullIf(v, eCond==1);
       146735  +  VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
       146736  +  VdbeCoverageNeverNullIf(v, eCond==1); /*   the OP_MustBeInt */
146127 146737     VdbeCoverageNeverNullIf(v, eCond==2);
       146738  +  VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
       146739  +  VdbeCoverageNeverNullIf(v, eCond==4); /*   the OP_Ge */
146128 146740     sqlite3MayAbort(pParse);
146129 146741     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
146130 146742     sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC);
146131 146743     sqlite3ReleaseTempReg(pParse, regZero);
146132 146744   }
146133 146745   
146134 146746   /*
................................................................................
146160 146772   ** number of rows in the current partition.
146161 146773   */
146162 146774   static void windowAggStep(
146163 146775     Parse *pParse, 
146164 146776     Window *pMWin,                  /* Linked list of window functions */
146165 146777     int csr,                        /* Read arguments from this cursor */
146166 146778     int bInverse,                   /* True to invoke xInverse instead of xStep */
146167         -  int reg,                        /* Array of registers */
146168         -  int regPartSize                 /* Register containing size of partition */
       146779  +  int reg                         /* Array of registers */
146169 146780   ){
146170 146781     Vdbe *v = sqlite3GetVdbe(pParse);
146171 146782     Window *pWin;
146172 146783     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146173         -    int flags = pWin->pFunc->funcFlags;
       146784  +    FuncDef *pFunc = pWin->pFunc;
146174 146785       int regArg;
146175 146786       int nArg = windowArgCount(pWin);
       146787  +    int i;
146176 146788   
146177         -    if( csr>=0 ){
146178         -      int i;
146179         -      for(i=0; i<nArg; i++){
       146789  +    for(i=0; i<nArg; i++){
       146790  +      if( i!=1 || pFunc->zName!=nth_valueName ){
146180 146791           sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
       146792  +      }else{
       146793  +        sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
146181 146794         }
146182         -      regArg = reg;
146183         -      if( flags & SQLITE_FUNC_WINDOW_SIZE ){
146184         -        if( nArg==0 ){
146185         -          regArg = regPartSize;
146186         -        }else{
146187         -          sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg);
146188         -        }
146189         -        nArg++;
146190         -      }
146191         -    }else{
146192         -      assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) );
146193         -      regArg = reg + pWin->iArgCol;
146194 146795       }
       146796  +    regArg = reg;
146195 146797   
146196         -    if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
146197         -      && pWin->eStart!=TK_UNBOUNDED 
       146798  +    if( pMWin->regStartRowid==0
       146799  +     && (pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
       146800  +     && (pWin->eStart!=TK_UNBOUNDED)
146198 146801       ){
146199 146802         int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regArg);
146200 146803         VdbeCoverage(v);
146201 146804         if( bInverse==0 ){
146202 146805           sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
146203 146806           sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
146204 146807           sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
................................................................................
146207 146810           sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
146208 146811           VdbeCoverageNeverTaken(v);
146209 146812           sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
146210 146813           sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
146211 146814         }
146212 146815         sqlite3VdbeJumpHere(v, addrIsNull);
146213 146816       }else if( pWin->regApp ){
146214         -      assert( pWin->pFunc->zName==nth_valueName
146215         -           || pWin->pFunc->zName==first_valueName
       146817  +      assert( pFunc->zName==nth_valueName
       146818  +           || pFunc->zName==first_valueName
146216 146819         );
146217 146820         assert( bInverse==0 || bInverse==1 );
146218 146821         sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
146219         -    }else if( pWin->pFunc->zName==leadName
146220         -           || pWin->pFunc->zName==lagName
146221         -    ){
146222         -      /* no-op */
146223         -    }else{
       146822  +    }else if( pFunc->xSFunc!=noopStepFunc ){
146224 146823         int addrIf = 0;
146225 146824         if( pWin->pFilter ){
146226 146825           int regTmp;
146227 146826           assert( nArg==0 || nArg==pWin->pOwner->x.pList->nExpr );
146228 146827           assert( nArg || pWin->pOwner->x.pList==0 );
146229         -        if( csr>0 ){
146230         -          regTmp = sqlite3GetTempReg(pParse);
146231         -          sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
146232         -        }else{
146233         -          regTmp = regArg + nArg;
146234         -        }
       146828  +        regTmp = sqlite3GetTempReg(pParse);
       146829  +        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
146235 146830           addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
146236 146831           VdbeCoverage(v);
146237         -        if( csr>0 ){
146238         -          sqlite3ReleaseTempReg(pParse, regTmp);
146239         -        }
       146832  +        sqlite3ReleaseTempReg(pParse, regTmp);
146240 146833         }
146241         -      if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
       146834  +      if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
146242 146835           CollSeq *pColl;
146243 146836           assert( nArg>0 );
146244 146837           pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
146245 146838           sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
146246 146839         }
146247 146840         sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep, 
146248 146841                           bInverse, regArg, pWin->regAccum);
146249         -      sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
       146842  +      sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
146250 146843         sqlite3VdbeChangeP5(v, (u8)nArg);
146251 146844         if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
146252 146845       }
146253 146846     }
146254 146847   }
146255 146848   
       146849  +typedef struct WindowCodeArg WindowCodeArg;
       146850  +typedef struct WindowCsrAndReg WindowCsrAndReg;
       146851  +struct WindowCsrAndReg {
       146852  +  int csr;
       146853  +  int reg;
       146854  +};
       146855  +
       146856  +struct WindowCodeArg {
       146857  +  Parse *pParse;
       146858  +  Window *pMWin;
       146859  +  Vdbe *pVdbe;
       146860  +  int regGosub;
       146861  +  int addrGosub;
       146862  +  int regArg;
       146863  +  int eDelete;
       146864  +
       146865  +  WindowCsrAndReg start;
       146866  +  WindowCsrAndReg current;
       146867  +  WindowCsrAndReg end;
       146868  +};
       146869  +
146256 146870   /*
146257         -** Generate VM code to invoke either xValue() (bFinal==0) or xFinalize()
146258         -** (bFinal==1) for each window function in the linked list starting at
       146871  +** Values that may be passed as the second argument to windowCodeOp().
       146872  +*/
       146873  +#define WINDOW_RETURN_ROW 1
       146874  +#define WINDOW_AGGINVERSE 2
       146875  +#define WINDOW_AGGSTEP    3
       146876  +
       146877  +/*
       146878  +** Generate VM code to read the window frames peer values from cursor csr into
       146879  +** an array of registers starting at reg.
       146880  +*/
       146881  +static void windowReadPeerValues(
       146882  +  WindowCodeArg *p,
       146883  +  int csr,
       146884  +  int reg
       146885  +){
       146886  +  Window *pMWin = p->pMWin;
       146887  +  ExprList *pOrderBy = pMWin->pOrderBy;
       146888  +  if( pOrderBy ){
       146889  +    Vdbe *v = sqlite3GetVdbe(p->pParse);
       146890  +    ExprList *pPart = pMWin->pPartition;
       146891  +    int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
       146892  +    int i;
       146893  +    for(i=0; i<pOrderBy->nExpr; i++){
       146894  +      sqlite3VdbeAddOp3(v, OP_Column, csr, iColOff+i, reg+i);
       146895  +    }
       146896  +  }
       146897  +}
       146898  +
       146899  +/*
       146900  +** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
       146901  +** (bFin==1) for each window function in the linked list starting at
146259 146902   ** pMWin. Or, for built-in window-functions that do not use the standard
146260 146903   ** API, generate the equivalent VM code.
146261 146904   */
146262         -static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){
       146905  +static void windowAggFinal(WindowCodeArg *p, int bFin){
       146906  +  Parse *pParse = p->pParse;
       146907  +  Window *pMWin = p->pMWin;
146263 146908     Vdbe *v = sqlite3GetVdbe(pParse);
146264 146909     Window *pWin;
146265 146910   
146266 146911     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146267         -    if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
146268         -     && pWin->eStart!=TK_UNBOUNDED 
       146912  +    if( pMWin->regStartRowid==0
       146913  +     && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
       146914  +     && (pWin->eStart!=TK_UNBOUNDED)
146269 146915       ){
146270 146916         sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146271 146917         sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
146272 146918         VdbeCoverage(v);
146273 146919         sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
146274 146920         sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
146275         -      if( bFinal ){
146276         -        sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
146277         -      }
146278 146921       }else if( pWin->regApp ){
       146922  +      assert( pMWin->regStartRowid==0 );
146279 146923       }else{
146280         -      if( bFinal ){
146281         -        sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin));
       146924  +      int nArg = windowArgCount(pWin);
       146925  +      if( bFin ){
       146926  +        sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
146282 146927           sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
146283 146928           sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
146284 146929           sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
146285 146930         }else{
146286         -        sqlite3VdbeAddOp3(v, OP_AggValue, pWin->regAccum, windowArgCount(pWin),
146287         -                             pWin->regResult);
       146931  +        sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
146288 146932           sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
146289 146933         }
146290 146934       }
146291 146935     }
146292 146936   }
146293 146937   
146294 146938   /*
146295         -** This function generates VM code to invoke the sub-routine at address
146296         -** lblFlushPart once for each partition with the entire partition cached in
146297         -** the Window.iEphCsr temp table.
146298         -*/
146299         -static void windowPartitionCache(
146300         -  Parse *pParse,
146301         -  Select *p,                      /* The rewritten SELECT statement */
146302         -  WhereInfo *pWInfo,              /* WhereInfo to call WhereEnd() on */
146303         -  int regFlushPart,               /* Register to use with Gosub lblFlushPart */
146304         -  int lblFlushPart,               /* Subroutine to Gosub to */
146305         -  int *pRegSize                   /* OUT: Register containing partition size */
146306         -){
146307         -  Window *pMWin = p->pWin;
146308         -  Vdbe *v = sqlite3GetVdbe(pParse);
146309         -  int iSubCsr = p->pSrc->a[0].iCursor;
146310         -  int nSub = p->pSrc->a[0].pTab->nCol;
146311         -  int k;
146312         -
146313         -  int reg = pParse->nMem+1;
146314         -  int regRecord = reg+nSub;
146315         -  int regRowid = regRecord+1;
146316         -
146317         -  *pRegSize = regRowid;
146318         -  pParse->nMem += nSub + 2;
146319         -
146320         -  /* Load the column values for the row returned by the sub-select
146321         -  ** into an array of registers starting at reg. */
146322         -  for(k=0; k<nSub; k++){
146323         -    sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
146324         -  }
146325         -  sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);
146326         -
146327         -  /* Check if this is the start of a new partition. If so, call the
146328         -  ** flush_partition sub-routine.  */
146329         -  if( pMWin->pPartition ){
       146939  +** Generate code to calculate the current values of all window functions in the
       146940  +** p->pMWin list by doing a full scan of the current window frame. Store the
       146941  +** results in the Window.regResult registers, ready to return the upper
       146942  +** layer.
       146943  +*/
       146944  +static void windowFullScan(WindowCodeArg *p){
       146945  +  Window *pWin;
       146946  +  Parse *pParse = p->pParse;
       146947  +  Window *pMWin = p->pMWin;
       146948  +  Vdbe *v = p->pVdbe;
       146949  +
       146950  +  int regCRowid = 0;              /* Current rowid value */
       146951  +  int regCPeer = 0;               /* Current peer values */
       146952  +  int regRowid = 0;               /* AggStep rowid value */
       146953  +  int regPeer = 0;                /* AggStep peer values */
       146954  +
       146955  +  int nPeer;
       146956  +  int lblNext;
       146957  +  int lblBrk;
       146958  +  int addrNext;
       146959  +  int csr = pMWin->csrApp;
       146960  +
       146961  +  nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
       146962  +
       146963  +  lblNext = sqlite3VdbeMakeLabel(pParse);
       146964  +  lblBrk = sqlite3VdbeMakeLabel(pParse);
       146965  +
       146966  +  regCRowid = sqlite3GetTempReg(pParse);
       146967  +  regRowid = sqlite3GetTempReg(pParse);
       146968  +  if( nPeer ){
       146969  +    regCPeer = sqlite3GetTempRange(pParse, nPeer);
       146970  +    regPeer = sqlite3GetTempRange(pParse, nPeer);
       146971  +  }
       146972  +
       146973  +  sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
       146974  +  windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
       146975  +
       146976  +  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
       146977  +    sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
       146978  +  }
       146979  +
       146980  +  sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
       146981  +  VdbeCoverage(v);
       146982  +  addrNext = sqlite3VdbeCurrentAddr(v);
       146983  +  sqlite3VdbeAddOp2(v, OP_Rowid, csr, regRowid);
       146984  +  sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
       146985  +  VdbeCoverageNeverNull(v);
       146986  +
       146987  +  if( pMWin->eExclude==TK_CURRENT ){
       146988  +    sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, lblNext, regRowid);
       146989  +    VdbeCoverageNeverNull(v);
       146990  +  }else if( pMWin->eExclude!=TK_NO ){
146330 146991       int addr;
146331         -    ExprList *pPart = pMWin->pPartition;
146332         -    int nPart = pPart->nExpr;
146333         -    int regNewPart = reg + pMWin->nBufferCol;
146334         -    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
146335         -
146336         -    addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
146337         -    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
146338         -    sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
146339         -    VdbeCoverageEqNe(v);
146340         -    sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
146341         -    sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
146342         -    VdbeComment((v, "call flush_partition"));
146343         -  }
146344         -
146345         -  /* Buffer the current row in the ephemeral table. */
146346         -  sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
146347         -  sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
146348         -
146349         -  /* End of the input loop */
146350         -  sqlite3WhereEnd(pWInfo);
146351         -
146352         -  /* Invoke "flush_partition" to deal with the final (or only) partition */
146353         -  sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
146354         -  VdbeComment((v, "call flush_partition"));
       146992  +    int addrEq = 0;
       146993  +    KeyInfo *pKeyInfo = 0;
       146994  +
       146995  +    if( pMWin->pOrderBy ){
       146996  +      pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
       146997  +    }
       146998  +    if( pMWin->eExclude==TK_TIES ){
       146999  +      addrEq = sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, 0, regRowid);
       147000  +      VdbeCoverageNeverNull(v);
       147001  +    }
       147002  +    if( pKeyInfo ){
       147003  +      windowReadPeerValues(p, csr, regPeer);
       147004  +      sqlite3VdbeAddOp3(v, OP_Compare, regPeer, regCPeer, nPeer);
       147005  +      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
       147006  +      addr = sqlite3VdbeCurrentAddr(v)+1;
       147007  +      sqlite3VdbeAddOp3(v, OP_Jump, addr, lblNext, addr);
       147008  +      VdbeCoverageEqNe(v);
       147009  +    }else{
       147010  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, lblNext);
       147011  +    }
       147012  +    if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
       147013  +  }
       147014  +
       147015  +  windowAggStep(pParse, pMWin, csr, 0, p->regArg);
       147016  +
       147017  +  sqlite3VdbeResolveLabel(v, lblNext);
       147018  +  sqlite3VdbeAddOp2(v, OP_Next, csr, addrNext);
       147019  +  VdbeCoverage(v);
       147020  +  sqlite3VdbeJumpHere(v, addrNext-1);
       147021  +  sqlite3VdbeJumpHere(v, addrNext+1);
       147022  +  sqlite3ReleaseTempReg(pParse, regRowid);
       147023  +  sqlite3ReleaseTempReg(pParse, regCRowid);
       147024  +  if( nPeer ){
       147025  +    sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
       147026  +    sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
       147027  +  }
       147028  +
       147029  +  windowAggFinal(p, 1);
146355 147030   }
146356 147031   
146357 147032   /*
146358 147033   ** Invoke the sub-routine at regGosub (generated by code in select.c) to
146359 147034   ** return the current row of Window.iEphCsr. If all window functions are
146360 147035   ** aggregate window functions that use the standard API, a single
146361 147036   ** OP_Gosub instruction is all that this routine generates. Extra VM code
................................................................................
146363 147038   ** functions:
146364 147039   **
146365 147040   **   nth_value()
146366 147041   **   first_value()
146367 147042   **   lag()
146368 147043   **   lead()
146369 147044   */
146370         -static void windowReturnOneRow(
146371         -  Parse *pParse,
146372         -  Window *pMWin,
146373         -  int regGosub,
146374         -  int addrGosub
146375         -){
146376         -  Vdbe *v = sqlite3GetVdbe(pParse);
146377         -  Window *pWin;
146378         -  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146379         -    FuncDef *pFunc = pWin->pFunc;
146380         -    if( pFunc->zName==nth_valueName
146381         -     || pFunc->zName==first_valueName
146382         -    ){
146383         -      int csr = pWin->csrApp;
146384         -      int lbl = sqlite3VdbeMakeLabel(pParse);
146385         -      int tmpReg = sqlite3GetTempReg(pParse);
146386         -      sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146387         -
146388         -      if( pFunc->zName==nth_valueName ){
146389         -        sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg);
146390         -        windowCheckIntValue(pParse, tmpReg, 2);
146391         -      }else{
146392         -        sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
146393         -      }
146394         -      sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
146395         -      sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
146396         -      VdbeCoverageNeverNull(v);
146397         -      sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
146398         -      VdbeCoverageNeverTaken(v);
146399         -      sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
146400         -      sqlite3VdbeResolveLabel(v, lbl);
146401         -      sqlite3ReleaseTempReg(pParse, tmpReg);
146402         -    }
146403         -    else if( pFunc->zName==leadName || pFunc->zName==lagName ){
146404         -      int nArg = pWin->pOwner->x.pList->nExpr;
146405         -      int iEph = pMWin->iEphCsr;
146406         -      int csr = pWin->csrApp;
146407         -      int lbl = sqlite3VdbeMakeLabel(pParse);
146408         -      int tmpReg = sqlite3GetTempReg(pParse);
146409         -
146410         -      if( nArg<3 ){
       147045  +static void windowReturnOneRow(WindowCodeArg *p){
       147046  +  Window *pMWin = p->pMWin;
       147047  +  Vdbe *v = p->pVdbe;
       147048  +
       147049  +  if( pMWin->regStartRowid ){
       147050  +    windowFullScan(p);
       147051  +  }else{
       147052  +    Parse *pParse = p->pParse;
       147053  +    Window *pWin;
       147054  +
       147055  +    for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
       147056  +      FuncDef *pFunc = pWin->pFunc;
       147057  +      if( pFunc->zName==nth_valueName
       147058  +       || pFunc->zName==first_valueName
       147059  +      ){
       147060  +        int csr = pWin->csrApp;
       147061  +        int lbl = sqlite3VdbeMakeLabel(pParse);
       147062  +        int tmpReg = sqlite3GetTempReg(pParse);
146411 147063           sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146412         -      }else{
146413         -        sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+2, pWin->regResult);
146414         -      }
146415         -      sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
146416         -      if( nArg<2 ){
146417         -        int val = (pFunc->zName==leadName ? 1 : -1);
146418         -        sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
146419         -      }else{
146420         -        int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
146421         -        int tmpReg2 = sqlite3GetTempReg(pParse);
146422         -        sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
146423         -        sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
146424         -        sqlite3ReleaseTempReg(pParse, tmpReg2);
146425         -      }
146426         -
146427         -      sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
146428         -      VdbeCoverage(v);
146429         -      sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
146430         -      sqlite3VdbeResolveLabel(v, lbl);
146431         -      sqlite3ReleaseTempReg(pParse, tmpReg);
146432         -    }
146433         -  }
146434         -  sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
146435         -}
146436         -
146437         -/*
146438         -** Invoke the code generated by windowReturnOneRow() and, optionally, the
146439         -** xInverse() function for each window function, for one or more rows
146440         -** from the Window.iEphCsr temp table. This routine generates VM code
146441         -** similar to:
146442         -**
146443         -**   while( regCtr>0 ){
146444         -**     regCtr--;
146445         -**     windowReturnOneRow()
146446         -**     if( bInverse ){
146447         -**       AggInverse
146448         -**     }
146449         -**     Next (Window.iEphCsr)
146450         -**   }
146451         -*/
146452         -static void windowReturnRows(
146453         -  Parse *pParse,
146454         -  Window *pMWin,                  /* List of window functions */
146455         -  int regCtr,                     /* Register containing number of rows */
146456         -  int regGosub,                   /* Register for Gosub addrGosub */
146457         -  int addrGosub,                  /* Address of sub-routine for ReturnOneRow */
146458         -  int regInvArg,                  /* Array of registers for xInverse args */
146459         -  int regInvSize                  /* Register containing size of partition */
146460         -){
146461         -  int addr;
146462         -  Vdbe *v = sqlite3GetVdbe(pParse);
146463         -  windowAggFinal(pParse, pMWin, 0);
146464         -  addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1);
146465         -  VdbeCoverage(v);
146466         -  sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
146467         -  windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
146468         -  if( regInvArg ){
146469         -    windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize);
146470         -  }
146471         -  sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr);
146472         -  VdbeCoverage(v);
146473         -  sqlite3VdbeJumpHere(v, addr+1);   /* The OP_Goto */
       147064  +  
       147065  +        if( pFunc->zName==nth_valueName ){
       147066  +          sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
       147067  +          windowCheckValue(pParse, tmpReg, 2);
       147068  +        }else{
       147069  +          sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
       147070  +        }
       147071  +        sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
       147072  +        sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
       147073  +        VdbeCoverageNeverNull(v);
       147074  +        sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
       147075  +        VdbeCoverageNeverTaken(v);
       147076  +        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
       147077  +        sqlite3VdbeResolveLabel(v, lbl);
       147078  +        sqlite3ReleaseTempReg(pParse, tmpReg);
       147079  +      }
       147080  +      else if( pFunc->zName==leadName || pFunc->zName==lagName ){
       147081  +        int nArg = pWin->pOwner->x.pList->nExpr;
       147082  +        int csr = pWin->csrApp;
       147083  +        int lbl = sqlite3VdbeMakeLabel(pParse);
       147084  +        int tmpReg = sqlite3GetTempReg(pParse);
       147085  +        int iEph = pMWin->iEphCsr;
       147086  +  
       147087  +        if( nArg<3 ){
       147088  +          sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
       147089  +        }else{
       147090  +          sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
       147091  +        }
       147092  +        sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
       147093  +        if( nArg<2 ){
       147094  +          int val = (pFunc->zName==leadName ? 1 : -1);
       147095  +          sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
       147096  +        }else{
       147097  +          int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
       147098  +          int tmpReg2 = sqlite3GetTempReg(pParse);
       147099  +          sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
       147100  +          sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
       147101  +          sqlite3ReleaseTempReg(pParse, tmpReg2);
       147102  +        }
       147103  +  
       147104  +        sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
       147105  +        VdbeCoverage(v);
       147106  +        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
       147107  +        sqlite3VdbeResolveLabel(v, lbl);
       147108  +        sqlite3ReleaseTempReg(pParse, tmpReg);
       147109  +      }
       147110  +    }
       147111  +  }
       147112  +  sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
146474 147113   }
146475 147114   
146476 147115   /*
146477 147116   ** Generate code to set the accumulator register for each window function
146478 147117   ** in the linked list passed as the second argument to NULL. And perform
146479 147118   ** any equivalent initialization required by any built-in window functions
146480 147119   ** in the list.
................................................................................
146484 147123     int regArg;
146485 147124     int nArg = 0;
146486 147125     Window *pWin;
146487 147126     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146488 147127       FuncDef *pFunc = pWin->pFunc;
146489 147128       sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
146490 147129       nArg = MAX(nArg, windowArgCount(pWin));
146491         -    if( pFunc->zName==nth_valueName
146492         -     || pFunc->zName==first_valueName
146493         -    ){
146494         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
146495         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
146496         -    }
       147130  +    if( pMWin->regStartRowid==0 ){
       147131  +      if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
       147132  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
       147133  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
       147134  +      }
146497 147135   
146498         -    if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
146499         -      assert( pWin->eStart!=TK_UNBOUNDED );
146500         -      sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
146501         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
       147136  +      if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
       147137  +        assert( pWin->eStart!=TK_UNBOUNDED );
       147138  +        sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
       147139  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
       147140  +      }
146502 147141       }
146503 147142     }
146504 147143     regArg = pParse->nMem+1;
146505 147144     pParse->nMem += nArg;
146506 147145     return regArg;
146507 147146   }
146508 147147   
146509         -
146510         -/*
146511         -** This function does the work of sqlite3WindowCodeStep() for all "ROWS"
146512         -** window frame types except for "BETWEEN UNBOUNDED PRECEDING AND CURRENT
146513         -** ROW". Pseudo-code for each follows.
146514         -**
146515         -** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
146516         -**
146517         -**     ...
146518         -**       if( new partition ){
146519         -**         Gosub flush_partition
146520         -**       }
146521         -**       Insert (record in eph-table)
146522         -**     sqlite3WhereEnd()
146523         -**     Gosub flush_partition
146524         -**  
146525         -**   flush_partition:
146526         -**     Once {
146527         -**       OpenDup (iEphCsr -> csrStart)
146528         -**       OpenDup (iEphCsr -> csrEnd)
146529         -**     }
146530         -**     regStart = <expr1>                // PRECEDING expression
146531         -**     regEnd = <expr2>                  // FOLLOWING expression
146532         -**     if( regStart<0 || regEnd<0 ){ error! }
146533         -**     Rewind (csr,csrStart,csrEnd)      // if EOF goto flush_partition_done
146534         -**       Next(csrEnd)                    // if EOF skip Aggstep
146535         -**       Aggstep (csrEnd)
146536         -**       if( (regEnd--)<=0 ){
146537         -**         AggFinal (xValue)
146538         -**         Gosub addrGosub
146539         -**         Next(csr)                // if EOF goto flush_partition_done
146540         -**         if( (regStart--)<=0 ){
146541         -**           AggInverse (csrStart)
146542         -**           Next(csrStart)
146543         -**         }
146544         -**       }
146545         -**   flush_partition_done:
146546         -**     ResetSorter (csr)
146547         -**     Return
146548         -**
146549         -** ROWS BETWEEN <expr> PRECEDING    AND CURRENT ROW
146550         -** ROWS BETWEEN CURRENT ROW         AND <expr> FOLLOWING
146551         -** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING
146552         -**
146553         -**   These are similar to the above. For "CURRENT ROW", intialize the
146554         -**   register to 0. For "UNBOUNDED PRECEDING" to infinity.
146555         -**
146556         -** ROWS BETWEEN <expr> PRECEDING    AND UNBOUNDED FOLLOWING
146557         -** ROWS BETWEEN CURRENT ROW         AND UNBOUNDED FOLLOWING
146558         -**
146559         -**     Rewind (csr,csrStart,csrEnd)    // if EOF goto flush_partition_done
146560         -**     while( 1 ){
146561         -**       Next(csrEnd)                  // Exit while(1) at EOF
146562         -**       Aggstep (csrEnd)
146563         -**     }
146564         -**     while( 1 ){
146565         -**       AggFinal (xValue)
146566         -**       Gosub addrGosub
146567         -**       Next(csr)                     // if EOF goto flush_partition_done
146568         -**       if( (regStart--)<=0 ){
146569         -**         AggInverse (csrStart)
146570         -**         Next(csrStart)
146571         -**       }
146572         -**     }
146573         -**
146574         -**   For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if() 
146575         -**   condition is always true (as if regStart were initialized to 0).
146576         -**
146577         -** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146578         -** 
146579         -**   This is the only RANGE case handled by this routine. It modifies the
146580         -**   second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to
146581         -**   be:
146582         -**
146583         -**     while( 1 ){
146584         -**       AggFinal (xValue)
146585         -**       while( 1 ){
146586         -**         regPeer++
146587         -**         Gosub addrGosub
146588         -**         Next(csr)                     // if EOF goto flush_partition_done
146589         -**         if( new peer ) break;
146590         -**       }
146591         -**       while( (regPeer--)>0 ){
146592         -**         AggInverse (csrStart)
146593         -**         Next(csrStart)
146594         -**       }
146595         -**     }
146596         -**
146597         -** ROWS BETWEEN <expr> FOLLOWING    AND <expr> FOLLOWING
146598         -**
146599         -**   regEnd = regEnd - regStart
146600         -**   Rewind (csr,csrStart,csrEnd)   // if EOF goto flush_partition_done
146601         -**     Aggstep (csrEnd)
146602         -**     Next(csrEnd)                 // if EOF fall-through
146603         -**     if( (regEnd--)<=0 ){
146604         -**       if( (regStart--)<=0 ){
146605         -**         AggFinal (xValue)
146606         -**         Gosub addrGosub
146607         -**         Next(csr)              // if EOF goto flush_partition_done
146608         -**       }
146609         -**       AggInverse (csrStart)
146610         -**       Next (csrStart)
146611         -**     }
146612         -**
146613         -** ROWS BETWEEN <expr> PRECEDING    AND <expr> PRECEDING
146614         -**
146615         -**   Replace the bit after "Rewind" in the above with:
146616         -**
146617         -**     if( (regEnd--)<=0 ){
146618         -**       AggStep (csrEnd)
146619         -**       Next (csrEnd)
146620         -**     }
146621         -**     AggFinal (xValue)
146622         -**     Gosub addrGosub
146623         -**     Next(csr)                  // if EOF goto flush_partition_done
146624         -**     if( (regStart--)<=0 ){
146625         -**       AggInverse (csr2)
146626         -**       Next (csr2)
146627         -**     }
146628         -**
146629         -*/
146630         -static void windowCodeRowExprStep(
146631         -  Parse *pParse, 
146632         -  Select *p,
146633         -  WhereInfo *pWInfo,
146634         -  int regGosub, 
146635         -  int addrGosub
146636         -){
146637         -  Window *pMWin = p->pWin;
       147148  +/* 
       147149  +** Return true if the current frame should be cached in the ephemeral table,
       147150  +** even if there are no xInverse() calls required.
       147151  +*/
       147152  +static int windowCacheFrame(Window *pMWin){
       147153  +  Window *pWin;
       147154  +  if( pMWin->regStartRowid ) return 1;
       147155  +  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
       147156  +    FuncDef *pFunc = pWin->pFunc;
       147157  +    if( (pFunc->zName==nth_valueName)
       147158  +     || (pFunc->zName==first_valueName)
       147159  +     || (pFunc->zName==leadName)
       147160  +     || (pFunc->zName==lagName)
       147161  +    ){
       147162  +      return 1;
       147163  +    }
       147164  +  }
       147165  +  return 0;
       147166  +}
       147167  +
       147168  +/*
       147169  +** regOld and regNew are each the first register in an array of size
       147170  +** pOrderBy->nExpr. This function generates code to compare the two
       147171  +** arrays of registers using the collation sequences and other comparison
       147172  +** parameters specified by pOrderBy. 
       147173  +**
       147174  +** If the two arrays are not equal, the contents of regNew is copied to 
       147175  +** regOld and control falls through. Otherwise, if the contents of the arrays
       147176  +** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
       147177  +*/
       147178  +static void windowIfNewPeer(
       147179  +  Parse *pParse,
       147180  +  ExprList *pOrderBy,
       147181  +  int regNew,                     /* First in array of new values */
       147182  +  int regOld,                     /* First in array of old values */
       147183  +  int addr                        /* Jump here */
       147184  +){
       147185  +  Vdbe *v = sqlite3GetVdbe(pParse);
       147186  +  if( pOrderBy ){
       147187  +    int nVal = pOrderBy->nExpr;
       147188  +    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
       147189  +    sqlite3VdbeAddOp3(v, OP_Compare, regOld, regNew, nVal);
       147190  +    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
       147191  +    sqlite3VdbeAddOp3(v, OP_Jump, 
       147192  +      sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1
       147193  +    );
       147194  +    VdbeCoverageEqNe(v);
       147195  +    sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
       147196  +  }else{
       147197  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
       147198  +  }
       147199  +}
       147200  +
       147201  +/*
       147202  +** This function is called as part of generating VM programs for RANGE
       147203  +** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
       147204  +** the ORDER BY term in the window, it generates code equivalent to:
       147205  +**
       147206  +**   if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
       147207  +**
       147208  +** A special type of arithmetic is used such that if csr.peerVal is not
       147209  +** a numeric type (real or integer), then the result of the addition is
       147210  +** a copy of csr1.peerVal.
       147211  +*/
       147212  +static void windowCodeRangeTest(
       147213  +  WindowCodeArg *p, 
       147214  +  int op,                          /* OP_Ge or OP_Gt */
       147215  +  int csr1, 
       147216  +  int regVal, 
       147217  +  int csr2,
       147218  +  int lbl
       147219  +){
       147220  +  Parse *pParse = p->pParse;
146638 147221     Vdbe *v = sqlite3GetVdbe(pParse);
146639         -  int regFlushPart;               /* Register for "Gosub flush_partition" */
146640         -  int lblFlushPart;               /* Label for "Gosub flush_partition" */
146641         -  int lblFlushDone;               /* Label for "Gosub flush_partition_done" */
146642         -
146643         -  int regArg;
146644         -  int addr;
146645         -  int csrStart = pParse->nTab++;
146646         -  int csrEnd = pParse->nTab++;
146647         -  int regStart;                    /* Value of <expr> PRECEDING */
146648         -  int regEnd;                      /* Value of <expr> FOLLOWING */
146649         -  int addrGoto;
146650         -  int addrTop;
146651         -  int addrIfPos1 = 0;
146652         -  int addrIfPos2 = 0;
146653         -  int regSize = 0;
146654         -
146655         -  assert( pMWin->eStart==TK_PRECEDING 
146656         -       || pMWin->eStart==TK_CURRENT 
146657         -       || pMWin->eStart==TK_FOLLOWING 
146658         -       || pMWin->eStart==TK_UNBOUNDED 
146659         -  );
146660         -  assert( pMWin->eEnd==TK_FOLLOWING 
146661         -       || pMWin->eEnd==TK_CURRENT 
146662         -       || pMWin->eEnd==TK_UNBOUNDED 
146663         -       || pMWin->eEnd==TK_PRECEDING 
146664         -  );
146665         -
146666         -  /* Allocate register and label for the "flush_partition" sub-routine. */
146667         -  regFlushPart = ++pParse->nMem;
146668         -  lblFlushPart = sqlite3VdbeMakeLabel(pParse);
146669         -  lblFlushDone = sqlite3VdbeMakeLabel(pParse);
146670         -
146671         -  regStart = ++pParse->nMem;
146672         -  regEnd = ++pParse->nMem;
146673         -
146674         -  windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
146675         -
146676         -  addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146677         -
146678         -  /* Start of "flush_partition" */
146679         -  sqlite3VdbeResolveLabel(v, lblFlushPart);
146680         -  sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3);
       147222  +  int reg1 = sqlite3GetTempReg(pParse);
       147223  +  int reg2 = sqlite3GetTempReg(pParse);
       147224  +  int arith = OP_Add;
       147225  +  int addrGe;
       147226  +
       147227  +  int regString = ++pParse->nMem;
       147228  +
       147229  +  assert( op==OP_Ge || op==OP_Gt || op==OP_Le );
       147230  +  assert( p->pMWin->pOrderBy && p->pMWin->pOrderBy->nExpr==1 );
       147231  +  if( p->pMWin->pOrderBy->a[0].sortOrder ){
       147232  +    switch( op ){
       147233  +      case OP_Ge: op = OP_Le; break;
       147234  +      case OP_Gt: op = OP_Lt; break;
       147235  +      default: assert( op==OP_Le ); op = OP_Ge; break;
       147236  +    }
       147237  +    arith = OP_Subtract;
       147238  +  }
       147239  +
       147240  +  windowReadPeerValues(p, csr1, reg1);
       147241  +  windowReadPeerValues(p, csr2, reg2);
       147242  +
       147243  +  /* Check if the peer value for csr1 value is a text or blob by comparing
       147244  +  ** it to the smallest possible string - ''. If it is, jump over the
       147245  +  ** OP_Add or OP_Subtract operation and proceed directly to the comparison. */
       147246  +  sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
       147247  +  addrGe = sqlite3VdbeAddOp3(v, OP_Ge, regString, 0, reg1);
146681 147248     VdbeCoverage(v);
146682         -  VdbeComment((v, "Flush_partition subroutine"));
146683         -  sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr);
146684         -  sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr);
146685         -
146686         -  /* If either regStart or regEnd are not non-negative integers, throw 
146687         -  ** an exception.  */
146688         -  if( pMWin->pStart ){
146689         -    sqlite3ExprCode(pParse, pMWin->pStart, regStart);
146690         -    windowCheckIntValue(pParse, regStart, 0);
146691         -  }
146692         -  if( pMWin->pEnd ){
146693         -    sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
146694         -    windowCheckIntValue(pParse, regEnd, 1);
146695         -  }
146696         -
146697         -  /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
146698         -  **
146699         -  **   if( regEnd<regStart ){
146700         -  **     // The frame always consists of 0 rows
146701         -  **     regStart = regSize;
146702         -  **   }
146703         -  **   regEnd = regEnd - regStart;
146704         -  */
146705         -  if( pMWin->pEnd && pMWin->eStart==TK_FOLLOWING ){
146706         -    assert( pMWin->pStart!=0 );
146707         -    assert( pMWin->eEnd==TK_FOLLOWING );
146708         -    sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd);
146709         -    VdbeCoverageNeverNull(v);
146710         -    sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
146711         -    sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd);
146712         -  }
146713         -
146714         -  if( pMWin->pStart && pMWin->eEnd==TK_PRECEDING ){
146715         -    assert( pMWin->pEnd!=0 );
146716         -    assert( pMWin->eStart==TK_PRECEDING );
146717         -    sqlite3VdbeAddOp3(v, OP_Le, regStart, sqlite3VdbeCurrentAddr(v)+3, regEnd);
146718         -    VdbeCoverageNeverNull(v);
146719         -    sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
146720         -    sqlite3VdbeAddOp2(v, OP_Copy, regSize, regEnd);
146721         -  }
146722         -
146723         -  /* Initialize the accumulator register for each window function to NULL */
146724         -  regArg = windowInitAccum(pParse, pMWin);
146725         -
146726         -  sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone);
146727         -  VdbeCoverage(v);
146728         -  sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone);
146729         -  VdbeCoverageNeverTaken(v);
146730         -  sqlite3VdbeChangeP5(v, 1);
146731         -  sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone);
146732         -  VdbeCoverageNeverTaken(v);
146733         -  sqlite3VdbeChangeP5(v, 1);
146734         -
146735         -  /* Invoke AggStep function for each window function using the row that
146736         -  ** csrEnd currently points to. Or, if csrEnd is already at EOF,
146737         -  ** do nothing.  */
146738         -  addrTop = sqlite3VdbeCurrentAddr(v);
146739         -  if( pMWin->eEnd==TK_PRECEDING ){
146740         -    addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
146741         -    VdbeCoverage(v);
146742         -  }
146743         -  sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
146744         -  VdbeCoverage(v);
146745         -  addr = sqlite3VdbeAddOp0(v, OP_Goto);
146746         -  windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
146747         -  if( pMWin->eEnd==TK_UNBOUNDED ){
146748         -    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
146749         -    sqlite3VdbeJumpHere(v, addr);
146750         -    addrTop = sqlite3VdbeCurrentAddr(v);
146751         -  }else{
146752         -    sqlite3VdbeJumpHere(v, addr);
146753         -    if( pMWin->eEnd==TK_PRECEDING ){
146754         -      sqlite3VdbeJumpHere(v, addrIfPos1);
146755         -    }
146756         -  }
146757         -
146758         -  if( pMWin->eEnd==TK_FOLLOWING ){
146759         -    addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
146760         -    VdbeCoverage(v);
146761         -  }
146762         -  if( pMWin->eStart==TK_FOLLOWING ){
146763         -    addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
146764         -    VdbeCoverage(v);
146765         -  }
146766         -  windowAggFinal(pParse, pMWin, 0);
146767         -  windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
146768         -  sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2);
146769         -  VdbeCoverage(v);
146770         -  sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone);
146771         -  if( pMWin->eStart==TK_FOLLOWING ){
146772         -    sqlite3VdbeJumpHere(v, addrIfPos2);
146773         -  }
146774         -
146775         -  if( pMWin->eStart==TK_CURRENT 
146776         -   || pMWin->eStart==TK_PRECEDING 
146777         -   || pMWin->eStart==TK_FOLLOWING 
146778         -  ){
146779         -    int lblSkipInverse = sqlite3VdbeMakeLabel(pParse);;
146780         -    if( pMWin->eStart==TK_PRECEDING ){
146781         -      sqlite3VdbeAddOp3(v, OP_IfPos, regStart, lblSkipInverse, 1);
       147249  +  sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1);
       147250  +  sqlite3VdbeJumpHere(v, addrGe);
       147251  +  sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
       147252  +  sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
       147253  +  assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le );
       147254  +  testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
       147255  +  testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
       147256  +  testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
       147257  +  testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
       147258  +
       147259  +  sqlite3ReleaseTempReg(pParse, reg1);
       147260  +  sqlite3ReleaseTempReg(pParse, reg2);
       147261  +}
       147262  +
       147263  +/*
       147264  +** Helper function for sqlite3WindowCodeStep(). Each call to this function
       147265  +** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE 
       147266  +** operation. Refer to the header comment for sqlite3WindowCodeStep() for
       147267  +** details.
       147268  +*/
       147269  +static int windowCodeOp(
       147270  + WindowCodeArg *p,                /* Context object */
       147271  + int op,                          /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
       147272  + int regCountdown,                /* Register for OP_IfPos countdown */
       147273  + int jumpOnEof                    /* Jump here if stepped cursor reaches EOF */
       147274  +){
       147275  +  int csr, reg;
       147276  +  Parse *pParse = p->pParse;
       147277  +  Window *pMWin = p->pMWin;
       147278  +  int ret = 0;
       147279  +  Vdbe *v = p->pVdbe;
       147280  +  int addrIf = 0; 
       147281  +  int addrContinue = 0;
       147282  +  int addrGoto = 0;
       147283  +  int bPeer = (pMWin->eFrmType!=TK_ROWS);
       147284  +
       147285  +  int lblDone = sqlite3VdbeMakeLabel(pParse);
       147286  +  int addrNextRange = 0;
       147287  +
       147288  +  /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
       147289  +  ** starts with UNBOUNDED PRECEDING. */
       147290  +  if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
       147291  +    assert( regCountdown==0 && jumpOnEof==0 );
       147292  +    return 0;
       147293  +  }
       147294  +
       147295  +  if( regCountdown>0 ){
       147296  +    if( pMWin->eFrmType==TK_RANGE ){
       147297  +      addrNextRange = sqlite3VdbeCurrentAddr(v);
       147298  +      assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP );
       147299  +      if( op==WINDOW_AGGINVERSE ){
       147300  +        if( pMWin->eStart==TK_FOLLOWING ){
       147301  +          windowCodeRangeTest(
       147302  +              p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
       147303  +          );
       147304  +        }else{
       147305  +          windowCodeRangeTest(
       147306  +              p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
       147307  +          );
       147308  +        }
       147309  +      }else{
       147310  +        windowCodeRangeTest(
       147311  +            p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
       147312  +        );
       147313  +      }
       147314  +    }else{
       147315  +      addrIf = sqlite3VdbeAddOp3(v, OP_IfPos, regCountdown, 0, 1);
146782 147316         VdbeCoverage(v);
146783 147317       }
146784         -    if( pMWin->eStart==TK_FOLLOWING ){
146785         -      sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+2);
146786         -      VdbeCoverage(v);
146787         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, lblSkipInverse);
146788         -    }else{
146789         -      sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
146790         -      VdbeCoverageAlwaysTaken(v);
146791         -    }
146792         -    windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
146793         -    sqlite3VdbeResolveLabel(v, lblSkipInverse);
146794         -  }
146795         -  if( pMWin->eEnd==TK_FOLLOWING ){
146796         -    sqlite3VdbeJumpHere(v, addrIfPos1);
146797         -  }
146798         -  sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
146799         -
146800         -  /* flush_partition_done: */
146801         -  sqlite3VdbeResolveLabel(v, lblFlushDone);
146802         -  sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
146803         -  sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
146804         -  VdbeComment((v, "end flush_partition subroutine"));
146805         -
146806         -  /* Jump to here to skip over flush_partition */
146807         -  sqlite3VdbeJumpHere(v, addrGoto);
146808         -}
146809         -
146810         -/*
146811         -** This function does the work of sqlite3WindowCodeStep() for cases that
146812         -** would normally be handled by windowCodeDefaultStep() when there are
146813         -** one or more built-in window-functions that require the entire partition
146814         -** to be cached in a temp table before any rows can be returned. Additionally.
146815         -** "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is always handled by
146816         -** this function.
146817         -**
146818         -** Pseudo-code corresponding to the VM code generated by this function
146819         -** for each type of window follows.
146820         -**
146821         -** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146822         -**
146823         -**   flush_partition:
146824         -**     Once {
146825         -**       OpenDup (iEphCsr -> csrLead)
146826         -**     }
146827         -**     Integer ctr 0
146828         -**     foreach row (csrLead){
146829         -**       if( new peer ){
146830         -**         AggFinal (xValue)
146831         -**         for(i=0; i<ctr; i++){
146832         -**           Gosub addrGosub
146833         -**           Next iEphCsr
146834         -**         }
146835         -**         Integer ctr 0
146836         -**       }
146837         -**       AggStep (csrLead)
146838         -**       Incr ctr
146839         -**     }
146840         -**
146841         -**     AggFinal (xFinalize)
146842         -**     for(i=0; i<ctr; i++){
146843         -**       Gosub addrGosub
146844         -**       Next iEphCsr
146845         -**     }
146846         -**
146847         -**     ResetSorter (csr)
146848         -**     Return
146849         -**
146850         -** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146851         -**
146852         -**   As above, except that the "if( new peer )" branch is always taken.
146853         -**
146854         -** RANGE BETWEEN CURRENT ROW AND CURRENT ROW 
146855         -**
146856         -**   As above, except that each of the for() loops becomes:
146857         -**
146858         -**         for(i=0; i<ctr; i++){
146859         -**           Gosub addrGosub
146860         -**           AggInverse (iEphCsr)
146861         -**           Next iEphCsr
146862         -**         }
146863         -**
146864         -** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
146865         -**
146866         -**   flush_partition:
146867         -**     Once {
146868         -**       OpenDup (iEphCsr -> csrLead)
146869         -**     }
146870         -**     foreach row (csrLead) {
146871         -**       AggStep (csrLead)
146872         -**     }
146873         -**     foreach row (iEphCsr) {
146874         -**       Gosub addrGosub
146875         -**     }
146876         -** 
146877         -** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146878         -**
146879         -**   flush_partition:
146880         -**     Once {
146881         -**       OpenDup (iEphCsr -> csrLead)
146882         -**     }
146883         -**     foreach row (csrLead){
146884         -**       AggStep (csrLead)
146885         -**     }
146886         -**     Rewind (csrLead)
146887         -**     Integer ctr 0
146888         -**     foreach row (csrLead){
146889         -**       if( new peer ){
146890         -**         AggFinal (xValue)
146891         -**         for(i=0; i<ctr; i++){
146892         -**           Gosub addrGosub
146893         -**           AggInverse (iEphCsr)
146894         -**           Next iEphCsr
146895         -**         }
146896         -**         Integer ctr 0
146897         -**       }
146898         -**       Incr ctr
146899         -**     }
146900         -**
146901         -**     AggFinal (xFinalize)
146902         -**     for(i=0; i<ctr; i++){
146903         -**       Gosub addrGosub
146904         -**       Next iEphCsr
146905         -**     }
146906         -**
146907         -**     ResetSorter (csr)
146908         -**     Return
146909         -*/
146910         -static void windowCodeCacheStep(
146911         -  Parse *pParse, 
146912         -  Select *p,
146913         -  WhereInfo *pWInfo,
146914         -  int regGosub, 
146915         -  int addrGosub
146916         -){
146917         -  Window *pMWin = p->pWin;
146918         -  Vdbe *v = sqlite3GetVdbe(pParse);
146919         -  int k;
146920         -  int addr;
146921         -  ExprList *pPart = pMWin->pPartition;
146922         -  ExprList *pOrderBy = pMWin->pOrderBy;
146923         -  int nPeer = pOrderBy ? pOrderBy->nExpr : 0;
146924         -  int regNewPeer;
146925         -
146926         -  int addrGoto;                   /* Address of Goto used to jump flush_par.. */
146927         -  int addrNext;                   /* Jump here for next iteration of loop */
146928         -  int regFlushPart;
146929         -  int lblFlushPart;
146930         -  int csrLead;
146931         -  int regCtr;
146932         -  int regArg;                     /* Register array to martial function args */
146933         -  int regSize;
146934         -  int lblEmpty;
146935         -  int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT 
146936         -          && pMWin->eEnd==TK_UNBOUNDED;
146937         -
146938         -  assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT) 
146939         -       || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED) 
146940         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT) 
146941         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED) 
146942         -  );
146943         -
146944         -  lblEmpty = sqlite3VdbeMakeLabel(pParse);
146945         -  regNewPeer = pParse->nMem+1;
146946         -  pParse->nMem += nPeer;
146947         -
146948         -  /* Allocate register and label for the "flush_partition" sub-routine. */
146949         -  regFlushPart = ++pParse->nMem;
146950         -  lblFlushPart = sqlite3VdbeMakeLabel(pParse);
146951         -
146952         -  csrLead = pParse->nTab++;
146953         -  regCtr = ++pParse->nMem;
146954         -
146955         -  windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
146956         -  addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146957         -
146958         -  /* Start of "flush_partition" */
146959         -  sqlite3VdbeResolveLabel(v, lblFlushPart);
146960         -  sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2);
146961         -  VdbeCoverage(v);
146962         -  sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr);
146963         -
146964         -  /* Initialize the accumulator register for each window function to NULL */
146965         -  regArg = windowInitAccum(pParse, pMWin);
146966         -
146967         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr);
146968         -  sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
146969         -  VdbeCoverage(v);
146970         -  sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty);
146971         -  VdbeCoverageNeverTaken(v);
146972         -
146973         -  if( bReverse ){
146974         -    int addr2 = sqlite3VdbeCurrentAddr(v);
146975         -    windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
146976         -    sqlite3VdbeAddOp2(v, OP_Next, csrLead, addr2);
       147318  +  }
       147319  +
       147320  +  if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
       147321  +    windowAggFinal(p, 0);
       147322  +  }
       147323  +  addrContinue = sqlite3VdbeCurrentAddr(v);
       147324  +  switch( op ){
       147325  +    case WINDOW_RETURN_ROW:
       147326  +      csr = p->current.csr;
       147327  +      reg = p->current.reg;
       147328  +      windowReturnOneRow(p);
       147329  +      break;
       147330  +
       147331  +    case WINDOW_AGGINVERSE:
       147332  +      csr = p->start.csr;
       147333  +      reg = p->start.reg;
       147334  +      if( pMWin->regStartRowid ){
       147335  +        assert( pMWin->regEndRowid );
       147336  +        sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
       147337  +      }else{
       147338  +        windowAggStep(pParse, pMWin, csr, 1, p->regArg);
       147339  +      }
       147340  +      break;
       147341  +
       147342  +    default:
       147343  +      assert( op==WINDOW_AGGSTEP );
       147344  +      csr = p->end.csr;
       147345  +      reg = p->end.reg;
       147346  +      if( pMWin->regStartRowid ){
       147347  +        assert( pMWin->regEndRowid );
       147348  +        sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
       147349  +      }else{
       147350  +        windowAggStep(pParse, pMWin, csr, 0, p->regArg);
       147351  +      }
       147352  +      break;
       147353  +  }
       147354  +
       147355  +  if( op==p->eDelete ){
       147356  +    sqlite3VdbeAddOp1(v, OP_Delete, csr);
       147357  +    sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
       147358  +  }
       147359  +
       147360  +  if( jumpOnEof ){
       147361  +    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+2);
       147362  +    VdbeCoverage(v);
       147363  +    ret = sqlite3VdbeAddOp0(v, OP_Goto);
       147364  +  }else{
       147365  +    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer);
146977 147366       VdbeCoverage(v);
146978         -    sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
146979         -    VdbeCoverageNeverTaken(v);
146980         -  }
146981         -  addrNext = sqlite3VdbeCurrentAddr(v);
146982         -
146983         -  if( pOrderBy && (pMWin->eEnd==TK_CURRENT || pMWin->eStart==TK_CURRENT) ){
146984         -    int bCurrent = (pMWin->eStart==TK_CURRENT);
146985         -    int addrJump = 0;             /* Address of OP_Jump below */
146986         -    if( pMWin->eType==TK_RANGE ){
146987         -      int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
146988         -      int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0);
146989         -      KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
146990         -      for(k=0; k<nPeer; k++){
146991         -        sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k);
146992         -      }
146993         -      addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
146994         -      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
146995         -      addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
146996         -      VdbeCoverage(v);
146997         -      sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1);
146998         -    }
146999         -
147000         -    windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 
147001         -        (bCurrent ? regArg : 0), (bCurrent ? regSize : 0)
147002         -    );
147003         -    if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147004         -  }
147005         -
147006         -  if( bReverse==0 ){
147007         -    windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
147008         -  }
147009         -  sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
147010         -  sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext);
147011         -  VdbeCoverage(v);
147012         -
147013         -  windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0);
147014         -
147015         -  sqlite3VdbeResolveLabel(v, lblEmpty);
147016         -  sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
147017         -  sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
147018         -
147019         -  /* Jump to here to skip over flush_partition */
147020         -  sqlite3VdbeJumpHere(v, addrGoto);
147021         -}
147022         -
147023         -
147024         -/*
147025         -** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147026         -**
147027         -**   ...
147028         -**     if( new partition ){
147029         -**       AggFinal (xFinalize)
147030         -**       Gosub addrGosub
147031         -**       ResetSorter eph-table
147032         -**     }
147033         -**     else if( new peer ){
147034         -**       AggFinal (xValue)
147035         -**       Gosub addrGosub
147036         -**       ResetSorter eph-table
147037         -**     }
147038         -**     AggStep
147039         -**     Insert (record into eph-table)
147040         -**   sqlite3WhereEnd()
147041         -**   AggFinal (xFinalize)
147042         -**   Gosub addrGosub
147043         -**
147044         -** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
147045         -**
147046         -**   As above, except take no action for a "new peer". Invoke
147047         -**   the sub-routine once only for each partition.
147048         -**
147049         -** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
147050         -**
147051         -**   As above, except that the "new peer" condition is handled in the
147052         -**   same way as "new partition" (so there is no "else if" block).
147053         -**
147054         -** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147055         -** 
147056         -**   As above, except assume every row is a "new peer".
147057         -*/
147058         -static void windowCodeDefaultStep(
147059         -  Parse *pParse, 
147060         -  Select *p,
147061         -  WhereInfo *pWInfo,
147062         -  int regGosub, 
147063         -  int addrGosub
147064         -){
147065         -  Window *pMWin = p->pWin;
147066         -  Vdbe *v = sqlite3GetVdbe(pParse);
147067         -  int k;
147068         -  int iSubCsr = p->pSrc->a[0].iCursor;
147069         -  int nSub = p->pSrc->a[0].pTab->nCol;
147070         -  int reg = pParse->nMem+1;
147071         -  int regRecord = reg+nSub;
147072         -  int regRowid = regRecord+1;
147073         -  int addr;
147074         -  ExprList *pPart = pMWin->pPartition;
147075         -  ExprList *pOrderBy = pMWin->pOrderBy;
147076         -
147077         -  assert( pMWin->eType==TK_RANGE 
147078         -      || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
147079         -  );
147080         -
147081         -  assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
147082         -       || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
147083         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
147084         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy)
147085         -  );
147086         -
147087         -  if( pMWin->eEnd==TK_UNBOUNDED ){
147088         -    pOrderBy = 0;
147089         -  }
147090         -
147091         -  pParse->nMem += nSub + 2;
147092         -
147093         -  /* Load the individual column values of the row returned by
147094         -  ** the sub-select into an array of registers. */
147095         -  for(k=0; k<nSub; k++){
147096         -    sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
147097         -  }
147098         -
147099         -  /* Check if this is the start of a new partition or peer group. */
147100         -  if( pPart || pOrderBy ){
147101         -    int nPart = (pPart ? pPart->nExpr : 0);
147102         -    int addrGoto = 0;
147103         -    int addrJump = 0;
147104         -    int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
147105         -
147106         -    if( pPart ){
147107         -      int regNewPart = reg + pMWin->nBufferCol;
147108         -      KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
147109         -      addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
147110         -      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
147111         -      addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
147112         -      VdbeCoverageEqNe(v);
147113         -      windowAggFinal(pParse, pMWin, 1);
147114         -      if( pOrderBy ){
147115         -        addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
147116         -      }
147117         -    }
147118         -
147119         -    if( pOrderBy ){
147120         -      int regNewPeer = reg + pMWin->nBufferCol + nPart;
147121         -      int regPeer = pMWin->regPart + nPart;
147122         -
147123         -      if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147124         -      if( pMWin->eType==TK_RANGE ){
147125         -        KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
147126         -        addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
147127         -        sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
147128         -        addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
147129         -        VdbeCoverage(v);
147130         -      }else{
147131         -        addrJump = 0;
147132         -      }
147133         -      windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT);
147134         -      if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
147135         -    }
147136         -
147137         -    sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
147138         -    VdbeCoverage(v);
147139         -    sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
147140         -    sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
147141         -    VdbeCoverage(v);
147142         -
147143         -    sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
147144         -    sqlite3VdbeAddOp3(
147145         -        v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1
147146         -    );
147147         -
147148         -    if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147149         -  }
147150         -
147151         -  /* Invoke step function for window functions */
147152         -  windowAggStep(pParse, pMWin, -1, 0, reg, 0);
147153         -
147154         -  /* Buffer the current row in the ephemeral table. */
147155         -  if( pMWin->nBufferCol>0 ){
147156         -    sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord);
147157         -  }else{
147158         -    sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord);
147159         -    sqlite3VdbeAppendP4(v, (void*)"", 0);
147160         -  }
147161         -  sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
147162         -  sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
147163         -
147164         -  /* End the database scan loop. */
147165         -  sqlite3WhereEnd(pWInfo);
147166         -
147167         -  windowAggFinal(pParse, pMWin, 1);
147168         -  sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
147169         -  VdbeCoverage(v);
147170         -  sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
147171         -  sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
147172         -  VdbeCoverage(v);
147173         -}
       147367  +    if( bPeer ){
       147368  +      addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
       147369  +    }
       147370  +  }
       147371  +
       147372  +  if( bPeer ){
       147373  +    int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
       147374  +    int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
       147375  +    windowReadPeerValues(p, csr, regTmp);
       147376  +    windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
       147377  +    sqlite3ReleaseTempRange(pParse, regTmp, nReg);
       147378  +  }
       147379  +
       147380  +  if( addrNextRange ){
       147381  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNextRange);
       147382  +  }
       147383  +  sqlite3VdbeResolveLabel(v, lblDone);
       147384  +  if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
       147385  +  if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
       147386  +  return ret;
       147387  +}
       147388  +
147174 147389   
147175 147390   /*
147176 147391   ** Allocate and return a duplicate of the Window object indicated by the
147177 147392   ** third argument. Set the Window.pOwner field of the new object to
147178 147393   ** pOwner.
147179 147394   */
147180 147395   SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
................................................................................
147183 147398       pNew = sqlite3DbMallocZero(db, sizeof(Window));
147184 147399       if( pNew ){
147185 147400         pNew->zName = sqlite3DbStrDup(db, p->zName);
147186 147401         pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
147187 147402         pNew->pFunc = p->pFunc;
147188 147403         pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
147189 147404         pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
147190         -      pNew->eType = p->eType;
       147405  +      pNew->eFrmType = p->eFrmType;
147191 147406         pNew->eEnd = p->eEnd;
147192 147407         pNew->eStart = p->eStart;
       147408  +      pNew->eExclude = p->eExclude;
147193 147409         pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
147194 147410         pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
147195 147411         pNew->pOwner = pOwner;
147196 147412       }
147197 147413     }
147198 147414     return pNew;
147199 147415   }
................................................................................
147211 147427       *pp = sqlite3WindowDup(db, 0, pWin);
147212 147428       if( *pp==0 ) break;
147213 147429       pp = &((*pp)->pNextWin);
147214 147430     }
147215 147431   
147216 147432     return pRet;
147217 147433   }
       147434  +
       147435  +/*
       147436  +** Return true if it can be determined at compile time that expression 
       147437  +** pExpr evaluates to a value that, when cast to an integer, is greater 
       147438  +** than zero. False otherwise.
       147439  +**
       147440  +** If an OOM error occurs, this function sets the Parse.db.mallocFailed 
       147441  +** flag and returns zero.
       147442  +*/
       147443  +static int windowExprGtZero(Parse *pParse, Expr *pExpr){
       147444  +  int ret = 0;
       147445  +  sqlite3 *db = pParse->db;
       147446  +  sqlite3_value *pVal = 0;
       147447  +  sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
       147448  +  if( pVal && sqlite3_value_int(pVal)>0 ){
       147449  +    ret = 1;
       147450  +  }
       147451  +  sqlite3ValueFree(pVal);
       147452  +  return ret;
       147453  +}
147218 147454   
147219 147455   /*
147220 147456   ** sqlite3WhereBegin() has already been called for the SELECT statement 
147221 147457   ** passed as the second argument when this function is invoked. It generates
147222         -** code to populate the Window.regResult register for each window function and
147223         -** invoke the sub-routine at instruction addrGosub once for each row.
147224         -** This function calls sqlite3WhereEnd() before returning. 
       147458  +** code to populate the Window.regResult register for each window function 
       147459  +** and invoke the sub-routine at instruction addrGosub once for each row.
       147460  +** sqlite3WhereEnd() is always called before returning. 
       147461  +**
       147462  +** This function handles several different types of window frames, which
       147463  +** require slightly different processing. The following pseudo code is
       147464  +** used to implement window frames of the form:
       147465  +**
       147466  +**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
       147467  +**
       147468  +** Other window frame types use variants of the following:
       147469  +**
       147470  +**     ... loop started by sqlite3WhereBegin() ...
       147471  +**       if( new partition ){
       147472  +**         Gosub flush
       147473  +**       }
       147474  +**       Insert new row into eph table.
       147475  +**       
       147476  +**       if( first row of partition ){
       147477  +**         // Rewind three cursors, all open on the eph table.
       147478  +**         Rewind(csrEnd);
       147479  +**         Rewind(csrStart);
       147480  +**         Rewind(csrCurrent);
       147481  +**       
       147482  +**         regEnd = <expr2>          // FOLLOWING expression
       147483  +**         regStart = <expr1>        // PRECEDING expression
       147484  +**       }else{
       147485  +**         // First time this branch is taken, the eph table contains two 
       147486  +**         // rows. The first row in the partition, which all three cursors
       147487  +**         // currently point to, and the following row.
       147488  +**         AGGSTEP
       147489  +**         if( (regEnd--)<=0 ){
       147490  +**           RETURN_ROW
       147491  +**           if( (regStart--)<=0 ){
       147492  +**             AGGINVERSE
       147493  +**           }
       147494  +**         }
       147495  +**       }
       147496  +**     }
       147497  +**     flush:
       147498  +**       AGGSTEP
       147499  +**       while( 1 ){
       147500  +**         RETURN ROW
       147501  +**         if( csrCurrent is EOF ) break;
       147502  +**         if( (regStart--)<=0 ){
       147503  +**           AggInverse(csrStart)
       147504  +**           Next(csrStart)
       147505  +**         }
       147506  +**       }
       147507  +**
       147508  +** The pseudo-code above uses the following shorthand:
       147509  +**
       147510  +**   AGGSTEP:    invoke the aggregate xStep() function for each window function
       147511  +**               with arguments read from the current row of cursor csrEnd, then
       147512  +**               step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
       147513  +**
       147514  +**   RETURN_ROW: return a row to the caller based on the contents of the 
       147515  +**               current row of csrCurrent and the current state of all 
       147516  +**               aggregates. Then step cursor csrCurrent forward one row.
       147517  +**
       147518  +**   AGGINVERSE: invoke the aggregate xInverse() function for each window 
       147519  +**               functions with arguments read from the current row of cursor
       147520  +**               csrStart. Then step csrStart forward one row.
       147521  +**
       147522  +** There are two other ROWS window frames that are handled significantly
       147523  +** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
       147524  +** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special 
       147525  +** cases because they change the order in which the three cursors (csrStart,
       147526  +** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
       147527  +** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
       147528  +** three.
       147529  +**
       147530  +**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
       147531  +**
       147532  +**     ... loop started by sqlite3WhereBegin() ...
       147533  +**       if( new partition ){
       147534  +**         Gosub flush
       147535  +**       }
       147536  +**       Insert new row into eph table.
       147537  +**       if( first row of partition ){
       147538  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147539  +**         regEnd = <expr2>
       147540  +**         regStart = <expr1>
       147541  +**       }else{
       147542  +**         if( (regEnd--)<=0 ){
       147543  +**           AGGSTEP
       147544  +**         }
       147545  +**         RETURN_ROW
       147546  +**         if( (regStart--)<=0 ){
       147547  +**           AGGINVERSE
       147548  +**         }
       147549  +**       }
       147550  +**     }
       147551  +**     flush:
       147552  +**       if( (regEnd--)<=0 ){
       147553  +**         AGGSTEP
       147554  +**       }
       147555  +**       RETURN_ROW
       147556  +**
       147557  +**
       147558  +**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
       147559  +**
       147560  +**     ... loop started by sqlite3WhereBegin() ...
       147561  +**     if( new partition ){
       147562  +**       Gosub flush
       147563  +**     }
       147564  +**     Insert new row into eph table.
       147565  +**     if( first row of partition ){
       147566  +**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147567  +**       regEnd = <expr2>
       147568  +**       regStart = regEnd - <expr1>
       147569  +**     }else{
       147570  +**       AGGSTEP
       147571  +**       if( (regEnd--)<=0 ){
       147572  +**         RETURN_ROW
       147573  +**       }
       147574  +**       if( (regStart--)<=0 ){
       147575  +**         AGGINVERSE
       147576  +**       }
       147577  +**     }
       147578  +**   }
       147579  +**   flush:
       147580  +**     AGGSTEP
       147581  +**     while( 1 ){
       147582  +**       if( (regEnd--)<=0 ){
       147583  +**         RETURN_ROW
       147584  +**         if( eof ) break;
       147585  +**       }
       147586  +**       if( (regStart--)<=0 ){
       147587  +**         AGGINVERSE
       147588  +**         if( eof ) break
       147589  +**       }
       147590  +**     }
       147591  +**     while( !eof csrCurrent ){
       147592  +**       RETURN_ROW
       147593  +**     }
       147594  +**
       147595  +** For the most part, the patterns above are adapted to support UNBOUNDED by
       147596  +** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
       147597  +** CURRENT ROW by assuming that it is equivilent to "0 PRECEDING/FOLLOWING".
       147598  +** This is optimized of course - branches that will never be taken and
       147599  +** conditions that are always true are omitted from the VM code. The only
       147600  +** exceptional case is:
       147601  +**
       147602  +**   ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
       147603  +**
       147604  +**     ... loop started by sqlite3WhereBegin() ...
       147605  +**     if( new partition ){
       147606  +**       Gosub flush
       147607  +**     }
       147608  +**     Insert new row into eph table.
       147609  +**     if( first row of partition ){
       147610  +**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147611  +**       regStart = <expr1>
       147612  +**     }else{
       147613  +**       AGGSTEP
       147614  +**     }
       147615  +**   }
       147616  +**   flush:
       147617  +**     AGGSTEP
       147618  +**     while( 1 ){
       147619  +**       if( (regStart--)<=0 ){
       147620  +**         AGGINVERSE
       147621  +**         if( eof ) break
       147622  +**       }
       147623  +**       RETURN_ROW
       147624  +**     }
       147625  +**     while( !eof csrCurrent ){
       147626  +**       RETURN_ROW
       147627  +**     }
       147628  +**
       147629  +** Also requiring special handling are the cases:
       147630  +**
       147631  +**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
       147632  +**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
       147633  +**
       147634  +** when (expr1 < expr2). This is detected at runtime, not by this function.
       147635  +** To handle this case, the pseudo-code programs depicted above are modified
       147636  +** slightly to be:
       147637  +**
       147638  +**     ... loop started by sqlite3WhereBegin() ...
       147639  +**     if( new partition ){
       147640  +**       Gosub flush
       147641  +**     }
       147642  +**     Insert new row into eph table.
       147643  +**     if( first row of partition ){
       147644  +**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147645  +**       regEnd = <expr2>
       147646  +**       regStart = <expr1>
       147647  +**       if( regEnd < regStart ){
       147648  +**         RETURN_ROW
       147649  +**         delete eph table contents
       147650  +**         continue
       147651  +**       }
       147652  +**     ...
       147653  +**
       147654  +** The new "continue" statement in the above jumps to the next iteration
       147655  +** of the outer loop - the one started by sqlite3WhereBegin().
       147656  +**
       147657  +** The various GROUPS cases are implemented using the same patterns as
       147658  +** ROWS. The VM code is modified slightly so that:
       147659  +**
       147660  +**   1. The else branch in the main loop is only taken if the row just
       147661  +**      added to the ephemeral table is the start of a new group. In
       147662  +**      other words, it becomes:
       147663  +**
       147664  +**         ... loop started by sqlite3WhereBegin() ...
       147665  +**         if( new partition ){
       147666  +**           Gosub flush
       147667  +**         }
       147668  +**         Insert new row into eph table.
       147669  +**         if( first row of partition ){
       147670  +**           Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147671  +**           regEnd = <expr2>
       147672  +**           regStart = <expr1>
       147673  +**         }else if( new group ){
       147674  +**           ... 
       147675  +**         }
       147676  +**       }
       147677  +**
       147678  +**   2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or 
       147679  +**      AGGINVERSE step processes the current row of the relevant cursor and
       147680  +**      all subsequent rows belonging to the same group.
       147681  +**
       147682  +** RANGE window frames are a little different again. As for GROUPS, the 
       147683  +** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
       147684  +** deal in groups instead of rows. As for ROWS and GROUPS, there are three
       147685  +** basic cases:
       147686  +**
       147687  +**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
       147688  +**
       147689  +**     ... loop started by sqlite3WhereBegin() ...
       147690  +**       if( new partition ){
       147691  +**         Gosub flush
       147692  +**       }
       147693  +**       Insert new row into eph table.
       147694  +**       if( first row of partition ){
       147695  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147696  +**         regEnd = <expr2>
       147697  +**         regStart = <expr1>
       147698  +**       }else{
       147699  +**         AGGSTEP
       147700  +**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
       147701  +**           RETURN_ROW
       147702  +**           while( csrStart.key + regStart) < csrCurrent.key ){
       147703  +**             AGGINVERSE
       147704  +**           }
       147705  +**         }
       147706  +**       }
       147707  +**     }
       147708  +**     flush:
       147709  +**       AGGSTEP
       147710  +**       while( 1 ){
       147711  +**         RETURN ROW
       147712  +**         if( csrCurrent is EOF ) break;
       147713  +**           while( csrStart.key + regStart) < csrCurrent.key ){
       147714  +**             AGGINVERSE
       147715  +**           }
       147716  +**         }
       147717  +**       }
       147718  +**
       147719  +** In the above notation, "csr.key" means the current value of the ORDER BY 
       147720  +** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
       147721  +** or <expr PRECEDING) read from cursor csr.
       147722  +**
       147723  +**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
       147724  +**
       147725  +**     ... loop started by sqlite3WhereBegin() ...
       147726  +**       if( new partition ){
       147727  +**         Gosub flush
       147728  +**       }
       147729  +**       Insert new row into eph table.
       147730  +**       if( first row of partition ){
       147731  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147732  +**         regEnd = <expr2>
       147733  +**         regStart = <expr1>
       147734  +**       }else{
       147735  +**         if( (csrEnd.key + regEnd) <= csrCurrent.key ){
       147736  +**           AGGSTEP
       147737  +**         }
       147738  +**         while( (csrStart.key + regStart) < csrCurrent.key ){
       147739  +**           AGGINVERSE
       147740  +**         }
       147741  +**         RETURN_ROW
       147742  +**       }
       147743  +**     }
       147744  +**     flush:
       147745  +**       while( (csrEnd.key + regEnd) <= csrCurrent.key ){
       147746  +**         AGGSTEP
       147747  +**       }
       147748  +**       while( (csrStart.key + regStart) < csrCurrent.key ){
       147749  +**         AGGINVERSE
       147750  +**       }
       147751  +**       RETURN_ROW
       147752  +**
       147753  +**   RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
       147754  +**
       147755  +**     ... loop started by sqlite3WhereBegin() ...
       147756  +**       if( new partition ){
       147757  +**         Gosub flush
       147758  +**       }
       147759  +**       Insert new row into eph table.
       147760  +**       if( first row of partition ){
       147761  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147762  +**         regEnd = <expr2>
       147763  +**         regStart = <expr1>
       147764  +**       }else{
       147765  +**         AGGSTEP
       147766  +**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
       147767  +**           while( (csrCurrent.key + regStart) > csrStart.key ){
       147768  +**             AGGINVERSE
       147769  +**           }
       147770  +**           RETURN_ROW
       147771  +**         }
       147772  +**       }
       147773  +**     }
       147774  +**     flush:
       147775  +**       AGGSTEP
       147776  +**       while( 1 ){
       147777  +**         while( (csrCurrent.key + regStart) > csrStart.key ){
       147778  +**           AGGINVERSE
       147779  +**           if( eof ) break "while( 1 )" loop.
       147780  +**         }
       147781  +**         RETURN_ROW
       147782  +**       }
       147783  +**       while( !eof csrCurrent ){
       147784  +**         RETURN_ROW
       147785  +**       }
       147786  +**
       147787  +** The text above leaves out many details. Refer to the code and comments
       147788  +** below for a more complete picture.
147225 147789   */
147226 147790   SQLITE_PRIVATE void sqlite3WindowCodeStep(
147227 147791     Parse *pParse,                  /* Parse context */
147228 147792     Select *p,                      /* Rewritten SELECT statement */
147229 147793     WhereInfo *pWInfo,              /* Context returned by sqlite3WhereBegin() */
147230 147794     int regGosub,                   /* Register for OP_Gosub */
147231 147795     int addrGosub                   /* OP_Gosub here to return each row */
147232 147796   ){
147233 147797     Window *pMWin = p->pWin;
147234         -
147235         -  /* There are three different functions that may be used to do the work
147236         -  ** of this one, depending on the window frame and the specific built-in
147237         -  ** window functions used (if any).
147238         -  **
147239         -  ** windowCodeRowExprStep() handles all "ROWS" window frames, except for:
147240         -  **
147241         -  **   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147242         -  **
147243         -  ** The exception is because windowCodeRowExprStep() implements all window
147244         -  ** frame types by caching the entire partition in a temp table, and
147245         -  ** "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" is easy enough to
147246         -  ** implement without such a cache.
147247         -  **
147248         -  ** windowCodeCacheStep() is used for:
147249         -  **
147250         -  **   RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
147251         -  **
147252         -  ** It is also used for anything not handled by windowCodeRowExprStep() 
147253         -  ** that invokes a built-in window function that requires the entire 
147254         -  ** partition to be cached in a temp table before any rows are returned
147255         -  ** (e.g. nth_value() or percent_rank()).
147256         -  **
147257         -  ** Finally, assuming there is no built-in window function that requires
147258         -  ** the partition to be cached, windowCodeDefaultStep() is used for:
147259         -  **
147260         -  **   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
147261         -  **   RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
147262         -  **   RANGE BETWEEN CURRENT ROW AND CURRENT ROW 
147263         -  **   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147264         -  **
147265         -  ** windowCodeDefaultStep() is the only one of the three functions that
147266         -  ** does not cache each partition in a temp table before beginning to
147267         -  ** return rows.
147268         -  */
147269         -  if( pMWin->eType==TK_ROWS 
147270         -   && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy)
147271         -  ){
147272         -    VdbeModuleComment((pParse->pVdbe, "Begin RowExprStep()"));
147273         -    windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub);
147274         -  }else{
147275         -    Window *pWin;
147276         -    int bCache = 0;               /* True to use CacheStep() */
147277         -
147278         -    if( pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED ){
147279         -      bCache = 1;
147280         -    }else{
147281         -      for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147282         -        FuncDef *pFunc = pWin->pFunc;
147283         -        if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
147284         -         || (pFunc->zName==nth_valueName)
147285         -         || (pFunc->zName==first_valueName)
147286         -         || (pFunc->zName==leadName)
147287         -         || (pFunc->zName==lagName)
147288         -        ){
147289         -          bCache = 1;
147290         -          break;
147291         -        }
147292         -      }
147293         -    }
147294         -
147295         -    /* Otherwise, call windowCodeDefaultStep().  */
147296         -    if( bCache ){
147297         -      VdbeModuleComment((pParse->pVdbe, "Begin CacheStep()"));
147298         -      windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
147299         -    }else{
147300         -      VdbeModuleComment((pParse->pVdbe, "Begin DefaultStep()"));
147301         -      windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub);
147302         -    }
       147798  +  ExprList *pOrderBy = pMWin->pOrderBy;
       147799  +  Vdbe *v = sqlite3GetVdbe(pParse);
       147800  +  int csrWrite;                   /* Cursor used to write to eph. table */
       147801  +  int csrInput = p->pSrc->a[0].iCursor;     /* Cursor of sub-select */
       147802  +  int nInput = p->pSrc->a[0].pTab->nCol;    /* Number of cols returned by sub */
       147803  +  int iInput;                               /* To iterate through sub cols */
       147804  +  int addrNe;                     /* Address of OP_Ne */
       147805  +  int addrGosubFlush = 0;         /* Address of OP_Gosub to flush: */
       147806  +  int addrInteger = 0;            /* Address of OP_Integer */
       147807  +  int addrEmpty;                  /* Address of OP_Rewind in flush: */
       147808  +  int regStart = 0;               /* Value of <expr> PRECEDING */
       147809  +  int regEnd = 0;                 /* Value of <expr> FOLLOWING */
       147810  +  int regNew;                     /* Array of registers holding new input row */
       147811  +  int regRecord;                  /* regNew array in record form */
       147812  +  int regRowid;                   /* Rowid for regRecord in eph table */
       147813  +  int regNewPeer = 0;             /* Peer values for new row (part of regNew) */
       147814  +  int regPeer = 0;                /* Peer values for current row */
       147815  +  int regFlushPart = 0;           /* Register for "Gosub flush_partition" */
       147816  +  WindowCodeArg s;                /* Context object for sub-routines */
       147817  +  int lblWhereEnd;                /* Label just before sqlite3WhereEnd() code */
       147818  +
       147819  +  assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT 
       147820  +       || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED 
       147821  +  );
       147822  +  assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT 
       147823  +       || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING 
       147824  +  );
       147825  +  assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
       147826  +       || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
       147827  +       || pMWin->eExclude==TK_NO
       147828  +  );
       147829  +
       147830  +  lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
       147831  +
       147832  +  /* Fill in the context object */
       147833  +  memset(&s, 0, sizeof(WindowCodeArg));
       147834  +  s.pParse = pParse;
       147835  +  s.pMWin = pMWin;
       147836  +  s.pVdbe = v;
       147837  +  s.regGosub = regGosub;
       147838  +  s.addrGosub = addrGosub;
       147839  +  s.current.csr = pMWin->iEphCsr;
       147840  +  csrWrite = s.current.csr+1;
       147841  +  s.start.csr = s.current.csr+2;
       147842  +  s.end.csr = s.current.csr+3;
       147843  +
       147844  +  /* Figure out when rows may be deleted from the ephemeral table. There
       147845  +  ** are four options - they may never be deleted (eDelete==0), they may 
       147846  +  ** be deleted as soon as they are no longer part of the window frame
       147847  +  ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row 
       147848  +  ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
       147849  +  ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
       147850  +  switch( pMWin->eStart ){
       147851  +    case TK_FOLLOWING:
       147852  +      if( pMWin->eFrmType!=TK_RANGE
       147853  +       && windowExprGtZero(pParse, pMWin->pStart)
       147854  +      ){
       147855  +        s.eDelete = WINDOW_RETURN_ROW;
       147856  +      }
       147857  +      break;
       147858  +    case TK_UNBOUNDED:
       147859  +      if( windowCacheFrame(pMWin)==0 ){
       147860  +        if( pMWin->eEnd==TK_PRECEDING ){
       147861  +          if( pMWin->eFrmType!=TK_RANGE
       147862  +           && windowExprGtZero(pParse, pMWin->pEnd)
       147863  +          ){
       147864  +            s.eDelete = WINDOW_AGGSTEP;
       147865  +          }
       147866  +        }else{
       147867  +          s.eDelete = WINDOW_RETURN_ROW;
       147868  +        }
       147869  +      }
       147870  +      break;
       147871  +    default:
       147872  +      s.eDelete = WINDOW_AGGINVERSE;
       147873  +      break;
       147874  +  }
       147875  +
       147876  +  /* Allocate registers for the array of values from the sub-query, the
       147877  +  ** samve values in record form, and the rowid used to insert said record
       147878  +  ** into the ephemeral table.  */
       147879  +  regNew = pParse->nMem+1;
       147880  +  pParse->nMem += nInput;
       147881  +  regRecord = ++pParse->nMem;
       147882  +  regRowid = ++pParse->nMem;
       147883  +
       147884  +  /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
       147885  +  ** clause, allocate registers to store the results of evaluating each
       147886  +  ** <expr>.  */
       147887  +  if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
       147888  +    regStart = ++pParse->nMem;
       147889  +  }
       147890  +  if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
       147891  +    regEnd = ++pParse->nMem;
       147892  +  }
       147893  +
       147894  +  /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
       147895  +  ** registers to store copies of the ORDER BY expressions (peer values) 
       147896  +  ** for the main loop, and for each cursor (start, current and end). */
       147897  +  if( pMWin->eFrmType!=TK_ROWS ){
       147898  +    int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
       147899  +    regNewPeer = regNew + pMWin->nBufferCol;
       147900  +    if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
       147901  +    regPeer = pParse->nMem+1;       pParse->nMem += nPeer;
       147902  +    s.start.reg = pParse->nMem+1;   pParse->nMem += nPeer;
       147903  +    s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
       147904  +    s.end.reg = pParse->nMem+1;     pParse->nMem += nPeer;
       147905  +  }
       147906  +
       147907  +  /* Load the column values for the row returned by the sub-select
       147908  +  ** into an array of registers starting at regNew. Assemble them into
       147909  +  ** a record in register regRecord. */
       147910  +  for(iInput=0; iInput<nInput; iInput++){
       147911  +    sqlite3VdbeAddOp3(v, OP_Column, csrInput, iInput, regNew+iInput);
       147912  +  }
       147913  +  sqlite3VdbeAddOp3(v, OP_MakeRecord, regNew, nInput, regRecord);
       147914  +
       147915  +  /* An input row has just been read into an array of registers starting
       147916  +  ** at regNew. If the window has a PARTITION clause, this block generates 
       147917  +  ** VM code to check if the input row is the start of a new partition.
       147918  +  ** If so, it does an OP_Gosub to an address to be filled in later. The
       147919  +  ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
       147920  +  if( pMWin->pPartition ){
       147921  +    int addr;
       147922  +    ExprList *pPart = pMWin->pPartition;
       147923  +    int nPart = pPart->nExpr;
       147924  +    int regNewPart = regNew + pMWin->nBufferCol;
       147925  +    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
       147926  +
       147927  +    regFlushPart = ++pParse->nMem;
       147928  +    addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
       147929  +    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
       147930  +    sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
       147931  +    VdbeCoverageEqNe(v);
       147932  +    addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub, regFlushPart);
       147933  +    VdbeComment((v, "call flush_partition"));
       147934  +    sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
       147935  +  }
       147936  +
       147937  +  /* Insert the new row into the ephemeral table */
       147938  +  sqlite3VdbeAddOp2(v, OP_NewRowid, csrWrite, regRowid);
       147939  +  sqlite3VdbeAddOp3(v, OP_Insert, csrWrite, regRecord, regRowid);
       147940  +  addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, regRowid);
       147941  +  VdbeCoverageNeverNull(v);
       147942  +
       147943  +  /* This block is run for the first row of each partition */
       147944  +  s.regArg = windowInitAccum(pParse, pMWin);
       147945  +
       147946  +  if( regStart ){
       147947  +    sqlite3ExprCode(pParse, pMWin->pStart, regStart);
       147948  +    windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
       147949  +  }
       147950  +  if( regEnd ){
       147951  +    sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
       147952  +    windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
       147953  +  }
       147954  +
       147955  +  if( pMWin->eStart==pMWin->eEnd && regStart ){
       147956  +    int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
       147957  +    int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
       147958  +    VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
       147959  +    VdbeCoverageNeverNullIf(v, op==OP_Le); /*   values previously checked */
       147960  +    windowAggFinal(&s, 0);
       147961  +    sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
       147962  +    VdbeCoverageNeverTaken(v);
       147963  +    windowReturnOneRow(&s);
       147964  +    sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
       147965  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
       147966  +    sqlite3VdbeJumpHere(v, addrGe);
       147967  +  }
       147968  +  if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
       147969  +    assert( pMWin->eEnd==TK_FOLLOWING );
       147970  +    sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regStart);
       147971  +  }
       147972  +
       147973  +  if( pMWin->eStart!=TK_UNBOUNDED ){
       147974  +    sqlite3VdbeAddOp2(v, OP_Rewind, s.start.csr, 1);
       147975  +    VdbeCoverageNeverTaken(v);
       147976  +  }
       147977  +  sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
       147978  +  VdbeCoverageNeverTaken(v);
       147979  +  sqlite3VdbeAddOp2(v, OP_Rewind, s.end.csr, 1);
       147980  +  VdbeCoverageNeverTaken(v);
       147981  +  if( regPeer && pOrderBy ){
       147982  +    sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
       147983  +    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
       147984  +    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
       147985  +    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
       147986  +  }
       147987  +
       147988  +  sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
       147989  +
       147990  +  sqlite3VdbeJumpHere(v, addrNe);
       147991  +
       147992  +  /* Beginning of the block executed for the second and subsequent rows. */
       147993  +  if( regPeer ){
       147994  +    windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd);
       147995  +  }
       147996  +  if( pMWin->eStart==TK_FOLLOWING ){
       147997  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       147998  +    if( pMWin->eEnd!=TK_UNBOUNDED ){
       147999  +      if( pMWin->eFrmType==TK_RANGE ){
       148000  +        int lbl = sqlite3VdbeMakeLabel(pParse);
       148001  +        int addrNext = sqlite3VdbeCurrentAddr(v);
       148002  +        windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
       148003  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148004  +        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148005  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
       148006  +        sqlite3VdbeResolveLabel(v, lbl);
       148007  +      }else{
       148008  +        windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 0);
       148009  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148010  +      }
       148011  +    }
       148012  +  }else
       148013  +  if( pMWin->eEnd==TK_PRECEDING ){
       148014  +    int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
       148015  +    windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
       148016  +    if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148017  +    windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148018  +    if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148019  +  }else{
       148020  +    int addr = 0;
       148021  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       148022  +    if( pMWin->eEnd!=TK_UNBOUNDED ){
       148023  +      if( pMWin->eFrmType==TK_RANGE ){
       148024  +        int lbl = 0;
       148025  +        addr = sqlite3VdbeCurrentAddr(v);
       148026  +        if( regEnd ){
       148027  +          lbl = sqlite3VdbeMakeLabel(pParse);
       148028  +          windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
       148029  +        }
       148030  +        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148031  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148032  +        if( regEnd ){
       148033  +          sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
       148034  +          sqlite3VdbeResolveLabel(v, lbl);
       148035  +        }
       148036  +      }else{
       148037  +        if( regEnd ){
       148038  +          addr = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
       148039  +          VdbeCoverage(v);
       148040  +        }
       148041  +        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148042  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148043  +        if( regEnd ) sqlite3VdbeJumpHere(v, addr);
       148044  +      }
       148045  +    }
       148046  +  }
       148047  +
       148048  +  /* End of the main input loop */
       148049  +  sqlite3VdbeResolveLabel(v, lblWhereEnd);
       148050  +  sqlite3WhereEnd(pWInfo);
       148051  +
       148052  +  /* Fall through */
       148053  +  if( pMWin->pPartition ){
       148054  +    addrInteger = sqlite3VdbeAddOp2(v, OP_Integer, 0, regFlushPart);
       148055  +    sqlite3VdbeJumpHere(v, addrGosubFlush);
       148056  +  }
       148057  +
       148058  +  addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind, csrWrite);
       148059  +  VdbeCoverage(v);
       148060  +  if( pMWin->eEnd==TK_PRECEDING ){
       148061  +    int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
       148062  +    windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
       148063  +    if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148064  +    windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148065  +  }else if( pMWin->eStart==TK_FOLLOWING ){
       148066  +    int addrStart;
       148067  +    int addrBreak1;
       148068  +    int addrBreak2;
       148069  +    int addrBreak3;
       148070  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       148071  +    if( pMWin->eFrmType==TK_RANGE ){
       148072  +      addrStart = sqlite3VdbeCurrentAddr(v);
       148073  +      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
       148074  +      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
       148075  +    }else
       148076  +    if( pMWin->eEnd==TK_UNBOUNDED ){
       148077  +      addrStart = sqlite3VdbeCurrentAddr(v);
       148078  +      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regStart, 1);
       148079  +      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, 0, 1);
       148080  +    }else{
       148081  +      assert( pMWin->eEnd==TK_FOLLOWING );
       148082  +      addrStart = sqlite3VdbeCurrentAddr(v);
       148083  +      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 1);
       148084  +      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
       148085  +    }
       148086  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
       148087  +    sqlite3VdbeJumpHere(v, addrBreak2);
       148088  +    addrStart = sqlite3VdbeCurrentAddr(v);
       148089  +    addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
       148090  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
       148091  +    sqlite3VdbeJumpHere(v, addrBreak1);
       148092  +    sqlite3VdbeJumpHere(v, addrBreak3);
       148093  +  }else{
       148094  +    int addrBreak;
       148095  +    int addrStart;
       148096  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       148097  +    addrStart = sqlite3VdbeCurrentAddr(v);
       148098  +    addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
       148099  +    windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148100  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
       148101  +    sqlite3VdbeJumpHere(v, addrBreak);
       148102  +  }
       148103  +  sqlite3VdbeJumpHere(v, addrEmpty);
       148104  +
       148105  +  sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
       148106  +  if( pMWin->pPartition ){
       148107  +    if( pMWin->regStartRowid ){
       148108  +      sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
       148109  +      sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
       148110  +    }
       148111  +    sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
       148112  +    sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
147303 148113     }
147304 148114   }
147305 148115   
147306 148116   #endif /* SQLITE_OMIT_WINDOWFUNC */
147307 148117   
147308 148118   /************** End of window.c **********************************************/
147309 148119   /************** Begin file parse.c *******************************************/
................................................................................
147484 148294       ){
147485 148295         sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
147486 148296                            pIdToken->n, pIdToken->z);
147487 148297       }
147488 148298       sqlite3ExprListSetName(pParse, p, pIdToken, 1);
147489 148299       return p;
147490 148300     }
       148301  +
       148302  +#if TK_SPAN>255
       148303  +# error too many tokens in the grammar
       148304  +#endif
147491 148305   /**************** End of %include directives **********************************/
147492 148306   /* These constants specify the various numeric values for terminal symbols
147493 148307   ** in a format understandable to "makeheaders".  This section is blank unless
147494 148308   ** "lemon" is run with the "-m" command-line option.
147495 148309   ***************** Begin makeheaders token definitions *************************/
147496 148310   /**************** End makeheaders token definitions ***************************/
147497 148311   
................................................................................
147547 148361   **    YY_MAX_REDUCE      Maximum value for reduce actions
147548 148362   */
147549 148363   #ifndef INTERFACE
147550 148364   # define INTERFACE 1
147551 148365   #endif
147552 148366   /************* Begin control #defines *****************************************/
147553 148367   #define YYCODETYPE unsigned short int
147554         -#define YYNOCODE 278
       148368  +#define YYNOCODE 301
147555 148369   #define YYACTIONTYPE unsigned short int
147556         -#define YYWILDCARD 91
       148370  +#define YYWILDCARD 95
147557 148371   #define sqlite3ParserTOKENTYPE Token
147558 148372   typedef union {
147559 148373     int yyinit;
147560 148374     sqlite3ParserTOKENTYPE yy0;
147561         -  ExprList* yy42;
147562         -  int yy96;
147563         -  TriggerStep* yy119;
147564         -  Window* yy147;
147565         -  SrcList* yy167;
147566         -  Upsert* yy266;
147567         -  struct FrameBound yy317;
147568         -  IdList* yy336;
147569         -  struct TrigEvent yy350;
147570         -  struct {int value; int mask;} yy367;
147571         -  Select* yy423;
147572         -  const char* yy464;
147573         -  Expr* yy490;
147574         -  With* yy499;
       148375  +  With* yy59;
       148376  +  IdList* yy62;
       148377  +  struct TrigEvent yy90;
       148378  +  Upsert* yy136;
       148379  +  struct FrameBound yy201;
       148380  +  u8 yy238;
       148381  +  const char* yy294;
       148382  +  Window* yy295;
       148383  +  struct {int value; int mask;} yy355;
       148384  +  ExprList* yy434;
       148385  +  TriggerStep* yy455;
       148386  +  Select* yy457;
       148387  +  SrcList* yy483;
       148388  +  int yy494;
       148389  +  Expr* yy524;
147575 148390   } YYMINORTYPE;
147576 148391   #ifndef YYSTACKDEPTH
147577 148392   #define YYSTACKDEPTH 100
147578 148393   #endif
147579 148394   #define sqlite3ParserARG_SDECL
147580 148395   #define sqlite3ParserARG_PDECL
147581 148396   #define sqlite3ParserARG_PARAM
................................................................................
147583 148398   #define sqlite3ParserARG_STORE
147584 148399   #define sqlite3ParserCTX_SDECL Parse *pParse;
147585 148400   #define sqlite3ParserCTX_PDECL ,Parse *pParse
147586 148401   #define sqlite3ParserCTX_PARAM ,pParse
147587 148402   #define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
147588 148403   #define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
147589 148404   #define YYFALLBACK 1
147590         -#define YYNSTATE             524
147591         -#define YYNRULE              369
147592         -#define YYNTOKEN             155
147593         -#define YY_MAX_SHIFT         523
147594         -#define YY_MIN_SHIFTREDUCE   760
147595         -#define YY_MAX_SHIFTREDUCE   1128
147596         -#define YY_ERROR_ACTION      1129
147597         -#define YY_ACCEPT_ACTION     1130
147598         -#define YY_NO_ACTION         1131
147599         -#define YY_MIN_REDUCE        1132
147600         -#define YY_MAX_REDUCE        1500
       148405  +#define YYNSTATE             541
       148406  +#define YYNRULE              375
       148407  +#define YYNTOKEN             176
       148408  +#define YY_MAX_SHIFT         540
       148409  +#define YY_MIN_SHIFTREDUCE   784
       148410  +#define YY_MAX_SHIFTREDUCE   1158
       148411  +#define YY_ERROR_ACTION      1159
       148412  +#define YY_ACCEPT_ACTION     1160
       148413  +#define YY_NO_ACTION         1161
       148414  +#define YY_MIN_REDUCE        1162
       148415  +#define YY_MAX_REDUCE        1536
147601 148416   /************* End control #defines *******************************************/
147602 148417   #define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
147603 148418   
147604 148419   /* Define the yytestcase() macro to be a no-op if is not already defined
147605 148420   ** otherwise.
147606 148421   **
147607 148422   ** Applications can choose to define yytestcase() in the %include section
................................................................................
147660 148475   **  yy_shift_ofst[]    For each state, the offset into yy_action for
147661 148476   **                     shifting terminals.
147662 148477   **  yy_reduce_ofst[]   For each state, the offset into yy_action for
147663 148478   **                     shifting non-terminals after a reduce.
147664 148479   **  yy_default[]       Default action for each state.
147665 148480   **
147666 148481   *********** Begin parsing tables **********************************************/
147667         -#define YY_ACTTAB_COUNT (2009)
       148482  +#define YY_ACTTAB_COUNT (2142)
147668 148483   static const YYACTIONTYPE yy_action[] = {
147669         - /*     0 */   377,  518,  371,  107,  104,  200, 1293,  518, 1130,    1,
147670         - /*    10 */     1,  523,    2, 1134,  518, 1203, 1203, 1262,  277,  373,
147671         - /*    20 */   129,  495,   37,   37, 1397, 1201, 1201, 1211,   65,   65,
147672         - /*    30 */   480,  891,  107,  104,  200,   37,   37, 1043, 1494,  892,
147673         - /*    40 */   346, 1494,  342,  114,  115,  105, 1106, 1106,  957,  960,
147674         - /*    50 */   950,  950,  112,  112,  113,  113,  113,  113,  285,  254,
147675         - /*    60 */   254,  518,  254,  254,  500,  518,  495,  518,  107,  104,
147676         - /*    70 */   200, 1085,  515,  481,  386,  515, 1464,  442,  501,  230,
147677         - /*    80 */   197,  439,   37,   37, 1172,  210,   65,   65,   65,   65,
147678         - /*    90 */   254,  254,  111,  111,  111,  111,  110,  110,  109,  109,
147679         - /*   100 */   109,  108,  404,  515,  404,  155, 1041,  431,  401,  400,
147680         - /*   110 */   254,  254,  373, 1431, 1427,  408, 1110, 1085, 1086, 1087,
147681         - /*   120 */   284, 1112,  500,  515,  500,  368, 1433, 1421, 1428, 1111,
147682         - /*   130 */  1261,  499,  373,  502,  108,  404,  114,  115,  105, 1106,
147683         - /*   140 */  1106,  957,  960,  950,  950,  112,  112,  113,  113,  113,
147684         - /*   150 */   113,  276,  509, 1113,  369, 1113,  114,  115,  105, 1106,
147685         - /*   160 */  1106,  957,  960,  950,  950,  112,  112,  113,  113,  113,
147686         - /*   170 */   113,  496, 1420, 1431,  493, 1468, 1065,  260, 1063,  433,
147687         - /*   180 */    74,  107,  104,  200,  498,  111,  111,  111,  111,  110,
147688         - /*   190 */   110,  109,  109,  109,  108,  404,  373,  113,  113,  113,
147689         - /*   200 */   113,  106,  131,   91, 1361,  111,  111,  111,  111,  110,
147690         - /*   210 */   110,  109,  109,  109,  108,  404,  113,  113,  113,  113,
147691         - /*   220 */   114,  115,  105, 1106, 1106,  957,  960,  950,  950,  112,
147692         - /*   230 */   112,  113,  113,  113,  113,  111,  111,  111,  111,  110,
147693         - /*   240 */   110,  109,  109,  109,  108,  404,  116,  110,  110,  109,
147694         - /*   250 */   109,  109,  108,  404,  111,  111,  111,  111,  110,  110,
147695         - /*   260 */   109,  109,  109,  108,  404,  917,  512,  512,  512,  111,
147696         - /*   270 */   111,  111,  111,  110,  110,  109,  109,  109,  108,  404,
147697         - /*   280 */   517, 1198, 1177,  181,  109,  109,  109,  108,  404,  373,
147698         - /*   290 */  1198,  402,  402,  402,   75,  360,  111,  111,  111,  111,
147699         - /*   300 */   110,  110,  109,  109,  109,  108,  404,  382,  299,  419,
147700         - /*   310 */   287,  170,  518,  114,  115,  105, 1106, 1106,  957,  960,
147701         - /*   320 */   950,  950,  112,  112,  113,  113,  113,  113, 1444,  523,
147702         - /*   330 */     2, 1134,  518,   13,   13,  337,  277, 1085,  129,  226,
147703         - /*   340 */   937, 1058, 1000,  471,  917, 1211,  453,  384, 1085,  395,
147704         - /*   350 */   162, 1057,  155,   45,   45,  416,  928,  401,  400,  479,
147705         - /*   360 */   927,   12,  111,  111,  111,  111,  110,  110,  109,  109,
147706         - /*   370 */   109,  108,  404,  226,  286,  254,  254,  254,  254,  518,
147707         - /*   380 */    16,   16,  373, 1085, 1086, 1087,  314,  299,  515,  472,
147708         - /*   390 */   515,  927,  927,  929, 1085, 1086, 1087,  378,  276,  509,
147709         - /*   400 */    65,   65, 1113,  210, 1113, 1085,  114,  115,  105, 1106,
147710         - /*   410 */  1106,  957,  960,  950,  950,  112,  112,  113,  113,  113,
147711         - /*   420 */   113, 1448,  222, 1134, 1089,  461,  458,  457,  277,  180,
147712         - /*   430 */   129,  378,  392,  408,  423,  456,  500, 1211,  240,  257,
147713         - /*   440 */   324,  464,  319,  463,  227,  470,   12,  317,  424,  300,
147714         - /*   450 */   317, 1085, 1086, 1087,  485,  111,  111,  111,  111,  110,
147715         - /*   460 */   110,  109,  109,  109,  108,  404,  181,  118, 1085,  254,
147716         - /*   470 */   254, 1089,  518,   90,  351,  373,  518, 1181,  365,  798,
147717         - /*   480 */  1440,  339,  515,  248,  248,   77,  325,  133, 1085,  249,
147718         - /*   490 */   424,  300,  794,   49,   49,  210,  515,   65,   65,  114,
147719         - /*   500 */   115,  105, 1106, 1106,  957,  960,  950,  950,  112,  112,
147720         - /*   510 */   113,  113,  113,  113, 1085, 1086, 1087,  222, 1085,  438,
147721         - /*   520 */   461,  458,  457,  937,  787,  408,  171,  857,  362, 1021,
147722         - /*   530 */   456,  136,  198,  486, 1085, 1086, 1087,  448,  794,  928,
147723         - /*   540 */     5,  193,  192,  927, 1022,  107,  104,  200,  111,  111,
147724         - /*   550 */   111,  111,  110,  110,  109,  109,  109,  108,  404, 1023,
147725         - /*   560 */   254,  254,  803, 1085, 1085, 1086, 1087,  437,  373, 1085,
147726         - /*   570 */   344,  787,  791,  515,  927,  927,  929, 1085, 1408, 1396,
147727         - /*   580 */   832, 1085,  176,    3,  852, 1085,  518, 1439,  429,  851,
147728         - /*   590 */   833,  518,  114,  115,  105, 1106, 1106,  957,  960,  950,
147729         - /*   600 */   950,  112,  112,  113,  113,  113,  113,   13,   13, 1085,
147730         - /*   610 */  1086, 1087,   13,   13,  518, 1085, 1086, 1087, 1496,  358,
147731         - /*   620 */  1085,  389, 1234, 1085, 1086, 1087,  391, 1085, 1086, 1087,
147732         - /*   630 */   448, 1085, 1086, 1087,  518,   65,   65,  947,  947,  958,
147733         - /*   640 */   961,  111,  111,  111,  111,  110,  110,  109,  109,  109,
147734         - /*   650 */   108,  404,  518,  382,  878,   13,   13,  518,  877,  518,
147735         - /*   660 */   263,  373,  518,  431,  448, 1070, 1085, 1086, 1087,  267,
147736         - /*   670 */   448,  488, 1360,   64,   64,  431,  812,  155,   50,   50,
147737         - /*   680 */    65,   65,  518,   65,   65,  114,  115,  105, 1106, 1106,
147738         - /*   690 */   957,  960,  950,  950,  112,  112,  113,  113,  113,  113,
147739         - /*   700 */   518,  951,  382,   13,   13,  415,  411,  462,  414, 1085,
147740         - /*   710 */  1366,  777, 1210,  292,  297,  813,  399,  497,  181,  403,
147741         - /*   720 */   261,   15,   15,  276,  509,  414,  413, 1366, 1368,  410,
147742         - /*   730 */   372,  345, 1209,  264,  111,  111,  111,  111,  110,  110,
147743         - /*   740 */   109,  109,  109,  108,  404,  265,  254,  254,  229, 1405,
147744         - /*   750 */   268, 1215,  268, 1103,  373, 1085, 1086, 1087,  938,  515,
147745         - /*   760 */   393,  409,  876,  515,  254,  254, 1152,  482,  473,  262,
147746         - /*   770 */   422,  476,  325,  503,  289,  518,  291,  515,  114,  115,
147747         - /*   780 */   105, 1106, 1106,  957,  960,  950,  950,  112,  112,  113,
147748         - /*   790 */   113,  113,  113,  414, 1021, 1366,   39,   39,  254,  254,
147749         - /*   800 */   254,  254,  980,  254,  254,  254,  254,  255,  255, 1022,
147750         - /*   810 */   279,  515,  516,  515,  846,  846,  515,  138,  515,  518,
147751         - /*   820 */   515, 1043, 1495,  251, 1023, 1495,  876,  111,  111,  111,
147752         - /*   830 */   111,  110,  110,  109,  109,  109,  108,  404,  518, 1353,
147753         - /*   840 */    51,   51,  518,  199,  518,  506,  290,  373,  518,  276,
147754         - /*   850 */   509,  922,    9,  483,  233, 1005, 1005,  445,  189,   52,
147755         - /*   860 */    52,  325,  280,   53,   53,   54,   54,  373,  876,   55,
147756         - /*   870 */    55,  114,  115,  105, 1106, 1106,  957,  960,  950,  950,
147757         - /*   880 */   112,  112,  113,  113,  113,  113,   97,  518,   95, 1104,
147758         - /*   890 */  1041,  114,  115,  105, 1106, 1106,  957,  960,  950,  950,
147759         - /*   900 */   112,  112,  113,  113,  113,  113,  135,  199,   56,   56,
147760         - /*   910 */   765,  766,  767,  225,  224,  223,  518,  283,  437,  233,
147761         - /*   920 */   111,  111,  111,  111,  110,  110,  109,  109,  109,  108,
147762         - /*   930 */   404, 1002,  876,  326,  518, 1002, 1104,   40,   40,  518,
147763         - /*   940 */   111,  111,  111,  111,  110,  110,  109,  109,  109,  108,
147764         - /*   950 */   404,  518,  448,  518, 1104,   41,   41,  518,   17,  518,
147765         - /*   960 */    43,   43, 1155,  379,  518,  448,  518,  443,  518,  390,
147766         - /*   970 */   518,  194,   44,   44,   57,   57, 1247,  518,   58,   58,
147767         - /*   980 */    59,   59,  518,  466,  326,   14,   14,   60,   60,  120,
147768         - /*   990 */   120,   61,   61,  449, 1206,   93,  518,  425,   46,   46,
147769         - /*  1000 */   518, 1104,  518,   62,   62,  518,  437,  305,  518,  852,
147770         - /*  1010 */   518,  298,  518, 1246,  851,  373,  518,   63,   63, 1293,
147771         - /*  1020 */   397,   47,   47,  142,  142, 1467,  143,  143,  821,   70,
147772         - /*  1030 */    70,   48,   48,   66,   66,  373,  518,  121,  121,  114,
147773         - /*  1040 */   115,  105, 1106, 1106,  957,  960,  950,  950,  112,  112,
147774         - /*  1050 */   113,  113,  113,  113,  518,  418,  518,   67,   67,  114,
147775         - /*  1060 */   115,  105, 1106, 1106,  957,  960,  950,  950,  112,  112,
147776         - /*  1070 */   113,  113,  113,  113,  312,  122,  122,  123,  123, 1293,
147777         - /*  1080 */   518,  357, 1126,   88,  518,  435,  325,  387,  111,  111,
147778         - /*  1090 */   111,  111,  110,  110,  109,  109,  109,  108,  404,  266,
147779         - /*  1100 */   518,  119,  119,  518, 1293,  141,  141,  518,  111,  111,
147780         - /*  1110 */   111,  111,  110,  110,  109,  109,  109,  108,  404,  518,
147781         - /*  1120 */   801,  140,  140,  518,  127,  127,  511,  379,  126,  126,
147782         - /*  1130 */   518,  137,  518, 1308,  518,  307,  518,  310,  518,  203,
147783         - /*  1140 */   124,  124, 1307,   96,  125,  125,  207,  388, 1441,  468,
147784         - /*  1150 */  1127,   69,   69,   71,   71,   68,   68,   38,   38,   42,
147785         - /*  1160 */    42,  357, 1042,  373, 1293,  276,  509,  801,  185,  469,
147786         - /*  1170 */   494,  436,  444,    6,  380,  156,  253,  197,  469,  134,
147787         - /*  1180 */   426,   33, 1038,  373, 1121,  359, 1411,  114,  115,  105,
147788         - /*  1190 */  1106, 1106,  957,  960,  950,  950,  112,  112,  113,  113,
147789         - /*  1200 */   113,  113,  914,  296,   27,  293,   90,  114,  103,  105,
147790         - /*  1210 */  1106, 1106,  957,  960,  950,  950,  112,  112,  113,  113,
147791         - /*  1220 */   113,  113,  919,  275,  430,  232,  891,  232,  432,  256,
147792         - /*  1230 */  1127,  232,  398,  370,  892,   28,  111,  111,  111,  111,
147793         - /*  1240 */   110,  110,  109,  109,  109,  108,  404,  301,  454, 1385,
147794         - /*  1250 */    90,  228,  209,  987,  811,  810,  111,  111,  111,  111,
147795         - /*  1260 */   110,  110,  109,  109,  109,  108,  404,  315,  818,  819,
147796         - /*  1270 */    90,  323,  983,  931,  885,  228,  373,  232,  999,  849,
147797         - /*  1280 */   999,  322,  102,  998, 1384,  998,  785,  850,  440,  132,
147798         - /*  1290 */   102,  302, 1243,  306,  309,  311,  373,  313, 1194, 1180,
147799         - /*  1300 */   987,  115,  105, 1106, 1106,  957,  960,  950,  950,  112,
147800         - /*  1310 */   112,  113,  113,  113,  113, 1178, 1179,  318,  327,  328,
147801         - /*  1320 */   931, 1255,  105, 1106, 1106,  957,  960,  950,  950,  112,
147802         - /*  1330 */   112,  113,  113,  113,  113, 1292, 1230, 1457,  273, 1241,
147803         - /*  1340 */   504,  505, 1298,  100,  510,  246,    4, 1161, 1154,  111,
147804         - /*  1350 */   111,  111,  111,  110,  110,  109,  109,  109,  108,  404,
147805         - /*  1360 */   513, 1143,  187, 1142,  202, 1144, 1451,  356, 1227,  111,
147806         - /*  1370 */   111,  111,  111,  110,  110,  109,  109,  109,  108,  404,
147807         - /*  1380 */    11, 1277,  330,  405,  332,  334,  191, 1285,  364,  195,
147808         - /*  1390 */   295,  417,  288,  100,  510,  507,    4,  434,  459,  321,
147809         - /*  1400 */  1177,  349, 1357, 1356,  336,  155,  190, 1454, 1121,  158,
147810         - /*  1410 */   513,  508,  235, 1404,  937, 1402, 1118,  381,   77,  428,
147811         - /*  1420 */    98,   98,    8, 1282,  168,   30,  152,   99,  160,  405,
147812         - /*  1430 */   520,  519,   88,  405,  927, 1362, 1274,  420,  163,   73,
147813         - /*  1440 */   164,   76,  165,  166,  421,  507,  452,  212,  361,  363,
147814         - /*  1450 */   427,  276,  509,   31, 1288,  172,  491,  441,  216, 1351,
147815         - /*  1460 */    82,  490,  447, 1373,  937,  927,  927,  929,  930,   24,
147816         - /*  1470 */    98,   98,  304,  247,  218,  177,  308,   99,  219,  405,
147817         - /*  1480 */   520,  519,  450, 1145,  927,  220,  366, 1197,  100,  510,
147818         - /*  1490 */   465,    4, 1188, 1196, 1195,  394,  803, 1169, 1187,  367,
147819         - /*  1500 */  1168,  396,  484,  320, 1167,  513, 1466,   87,  475,  100,
147820         - /*  1510 */   510,  271,    4,  272,  478,  927,  927,  929,  930,   24,
147821         - /*  1520 */  1443, 1074,  407, 1238, 1239,  258,  513,  329,  405,  331,
147822         - /*  1530 */   355,  355,  354,  243,  352,  234,  489,  774,  498,  184,
147823         - /*  1540 */   507,  338, 1422,  339,  117, 1220,   10,  341,  333,  405,
147824         - /*  1550 */   204,  491,  282, 1219, 1237, 1236,  492,  335,  343,  937,
147825         - /*  1560 */   281,  507,   94, 1337,  186,   98,   98,  347,   89,  487,
147826         - /*  1570 */   348,  241,   99,   29,  405,  520,  519,  274, 1151,  927,
147827         - /*  1580 */   937,  521, 1080,  245,  242,  244,   98,   98,  856,  522,
147828         - /*  1590 */   206, 1140, 1135,   99,  144,  405,  520,  519,  147,  375,
147829         - /*  1600 */   927,  149,  376,  157, 1389, 1390, 1388, 1387,  205,  145,
147830         - /*  1610 */   927,  927,  929,  930,   24,  146,  130,  761, 1165, 1164,
147831         - /*  1620 */    72,  100,  510, 1162,    4,  269,  406,  188,  278,  201,
147832         - /*  1630 */   259,  927,  927,  929,  930,   24,  128,  911,  513,  997,
147833         - /*  1640 */   995,  159,  374,  208,  148,  161,  835,  276,  509,  211,
147834         - /*  1650 */   294, 1011,  915,  167,  150,  383,  169,   78,  385,   79,
147835         - /*  1660 */    80,  405,   81,  151, 1014,  213,  214, 1010,  139,   18,
147836         - /*  1670 */   412,  215,  303,  507,  232, 1115, 1003,  446,  173,  217,
147837         - /*  1680 */   174,   32,  776,  451,  491,  322,  221,  175,  814,  490,
147838         - /*  1690 */    83,  455,  937,   19,  460,  316,   20,   84,   98,   98,
147839         - /*  1700 */   270,  182,   85,  467,  153,   99,  154,  405,  520,  519,
147840         - /*  1710 */  1074,  407,  927,  183,  258,  963, 1046,   86,   34,  355,
147841         - /*  1720 */   355,  354,  243,  352,  474, 1047,  774,   35,  477,  196,
147842         - /*  1730 */   250,  100,  510,  252,    4,  884,  178,  231, 1060,  204,
147843         - /*  1740 */    21,  282,  102,  927,  927,  929,  930,   24,  513,  281,
147844         - /*  1750 */   879,   22, 1064, 1062, 1051,    7,  340,   23,  978,  179,
147845         - /*  1760 */    90,   92,  510,  964,    4,  236,  962,  966, 1020, 1019,
147846         - /*  1770 */   237,  405,  967,   25,   36,  514,  932,  786,  513,  206,
147847         - /*  1780 */   101,   26,  845,  507,  238,  239, 1459,  147,  350, 1458,
147848         - /*  1790 */   149,  353, 1075, 1131, 1131, 1131, 1131,  205, 1131, 1131,
147849         - /*  1800 */  1131,  405,  937, 1131, 1131, 1131, 1131, 1131,   98,   98,
147850         - /*  1810 */  1131, 1131, 1131,  507, 1131,   99, 1131,  405,  520,  519,
147851         - /*  1820 */  1131, 1131,  927, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147852         - /*  1830 */  1131,  374,  937, 1131, 1131, 1131,  276,  509,   98,   98,
147853         - /*  1840 */  1131, 1131, 1131, 1131, 1131,   99, 1131,  405,  520,  519,
147854         - /*  1850 */  1131, 1131,  927,  927,  927,  929,  930,   24, 1131,  412,
147855         - /*  1860 */  1131, 1131, 1131,  258, 1131, 1131, 1131, 1131,  355,  355,
147856         - /*  1870 */   354,  243,  352, 1131, 1131,  774, 1131, 1131, 1131, 1131,
147857         - /*  1880 */  1131, 1131, 1131,  927,  927,  929,  930,   24,  204, 1131,
147858         - /*  1890 */   282, 1131, 1131, 1131, 1131, 1131, 1131, 1131,  281, 1131,
147859         - /*  1900 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147860         - /*  1910 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147861         - /*  1920 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,  206, 1131,
147862         - /*  1930 */  1131, 1131, 1131, 1131, 1131, 1131,  147, 1131, 1131,  149,
147863         - /*  1940 */  1131, 1131, 1131, 1131, 1131, 1131,  205, 1131, 1131, 1131,
147864         - /*  1950 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147865         - /*  1960 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147866         - /*  1970 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147867         - /*  1980 */   374, 1131, 1131, 1131, 1131,  276,  509, 1131, 1131, 1131,
147868         - /*  1990 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147869         - /*  2000 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,  412,
       148484  + /*     0 */   535, 1323,  112,  109,  209,  112,  109,  209, 1160,    1,
       148485  + /*    10 */     1,  540,    2, 1164,  535, 1292, 1228, 1207,  289,  384,
       148486  + /*    20 */   134,   42,   42, 1427,  382, 1228,    9, 1241,  242,  492,
       148487  + /*    30 */  1291,  915,  373,  379, 1026,   70,   70,  427, 1026,  916,
       148488  + /*    40 */   529,  529,  529,  119,  120,  110, 1136, 1136,  981,  984,
       148489  + /*    50 */   974,  974,  117,  117,  118,  118,  118,  118,  380,  264,
       148490  + /*    60 */   264,  264,  264, 1134,  264,  264,  112,  109,  209,  397,
       148491  + /*    70 */   454,  517,  532,  491,  532, 1233, 1233,  532,  239,  206,
       148492  + /*    80 */   493,  112,  109,  209,  464,  219,  118,  118,  118,  118,
       148493  + /*    90 */   111,  393,  440,  444,   16,   16,  116,  116,  116,  116,
       148494  + /*   100 */   115,  115,  114,  114,  114,  113,  415,  971,  971,  982,
       148495  + /*   110 */   985,  235, 1463,  351, 1134,  419,  384,  116,  116,  116,
       148496  + /*   120 */   116,  115,  115,  114,  114,  114,  113,  415,  116,  116,
       148497  + /*   130 */   116,  116,  115,  115,  114,  114,  114,  113,  415,  961,
       148498  + /*   140 */   119,  120,  110, 1136, 1136,  981,  984,  974,  974,  117,
       148499  + /*   150 */   117,  118,  118,  118,  118,  952,  415,  941,  298,  951,
       148500  + /*   160 */   941, 1480,  540,    2, 1164, 1115,  535, 1458,  160,  289,
       148501  + /*   170 */     6,  134, 1504,  389,  406,  975,  338, 1024, 1241,  337,
       148502  + /*   180 */  1089, 1476, 1089,  118,  118,  118,  118,   42,   42,  329,
       148503  + /*   190 */   951,  951,  953,  116,  116,  116,  116,  115,  115,  114,
       148504  + /*   200 */   114,  114,  113,  415,  311,  430,  299,  311,  881,  160,
       148505  + /*   210 */   264,  264,  401,  384,  324, 1115, 1116, 1117,  288,  526,
       148506  + /*   220 */    96,  159, 1441,  532,  141,  116,  116,  116,  116,  115,
       148507  + /*   230 */   115,  114,  114,  114,  113,  415,  219,  119,  120,  110,
       148508  + /*   240 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148509  + /*   250 */   118,  118,  115,  115,  114,  114,  114,  113,  415,  288,
       148510  + /*   260 */   526,  403,  533,  121,  870,  870,  419,  250,  267,  336,
       148511  + /*   270 */   475,  331,  474,  236,  160,  319, 1084,  322, 1465,  329,
       148512  + /*   280 */   350,   12,  535,  384,  502, 1115, 1084,  435,  312, 1084,
       148513  + /*   290 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148514  + /*   300 */   415,  535,  836,   42,   42,  138,  426,  119,  120,  110,
       148515  + /*   310 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148516  + /*   320 */   118,  118,   70,   70,  288,  526,  412,  411,  480, 1457,
       148517  + /*   330 */   335,   79,    6,  473, 1140, 1115, 1116, 1117,  501, 1142,
       148518  + /*   340 */   334,  837,  811, 1484,  512, 1164,  534, 1141,  123,  187,
       148519  + /*   350 */   289,  384,  134,  448,  434, 1115,   80,  349,  498, 1241,
       148520  + /*   360 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148521  + /*   370 */   415, 1143, 1115, 1143,  459,  119,  120,  110, 1136, 1136,
       148522  + /*   380 */   981,  984,  974,  974,  117,  117,  118,  118,  118,  118,
       148523  + /*   390 */   404,  264,  264,  811, 1463,  506,  368, 1156,  535,  114,
       148524  + /*   400 */   114,  114,  113,  415,  532, 1115, 1116, 1117,  231,  518,
       148525  + /*   410 */  1500,  472,  469,  468,  175,  497,  422,  219, 1202,   70,
       148526  + /*   420 */    70,  467, 1115, 1116, 1117,  176,  201,  200,  116,  116,
       148527  + /*   430 */   116,  116,  115,  115,  114,  114,  114,  113,  415,  535,
       148528  + /*   440 */  1115,  264,  264,  435,  312, 1115,  273,  419,  384,  513,
       148529  + /*   450 */  1450, 1115,  326, 1084,  532,  517,   82, 1084,  167,  388,
       148530  + /*   460 */    69,   69, 1115, 1084,  519,  509, 1084, 1084,   12, 1157,
       148531  + /*   470 */  1084,  420,  119,  120,  110, 1136, 1136,  981,  984,  974,
       148532  + /*   480 */   974,  117,  117,  118,  118,  118,  118,  258,  258,  535,
       148533  + /*   490 */  1115, 1116, 1117, 1045,  535, 1115, 1116, 1117, 1323,  535,
       148534  + /*   500 */   532, 1115, 1116, 1117,  296,  483, 1211,  818, 1046,  448,
       148535  + /*   510 */    70,   70, 1115, 1116, 1117,   50,   50,  448,  356,  500,
       148536  + /*   520 */    70,   70,  207, 1047,   32,  116,  116,  116,  116,  115,
       148537  + /*   530 */   115,  114,  114,  114,  113,  415,  453,  264,  264, 1115,
       148538  + /*   540 */   450,  449,  961,  508,  856,  384,  517,    5,  900,  822,
       148539  + /*   550 */   532,  484,  181, 1115,  857,  516,  517,  818,  952,  507,
       148540  + /*   560 */     3, 1115,  951, 1231, 1231,  482,  398, 1115, 1095,  119,
       148541  + /*   570 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
       148542  + /*   580 */   118,  118,  118,  118, 1115,  535,  238, 1115, 1391, 1115,
       148543  + /*   590 */  1116, 1117,  159,  951,  951,  953,  231, 1115,  259,  472,
       148544  + /*   600 */   469,  468,  310, 1115, 1116, 1117,   13,   13,  297,  467,
       148545  + /*   610 */   276, 1115, 1116, 1117,  412,  411, 1095, 1115, 1116, 1117,
       148546  + /*   620 */   395,  355,  116,  116,  116,  116,  115,  115,  114,  114,
       148547  + /*   630 */   114,  113,  415,  208, 1115, 1116, 1117, 1115, 1116, 1117,
       148548  + /*   640 */   264,  264,  384,  337,  902,  393,  815, 1115, 1116, 1117,
       148549  + /*   650 */   413,  413,  413,  532,  112,  109,  209,  309,  900, 1143,
       148550  + /*   660 */   535, 1143,  535,  393,  901, 1210,  119,  120,  110, 1136,
       148551  + /*   670 */  1136,  981,  984,  974,  974,  117,  117,  118,  118,  118,
       148552  + /*   680 */   118,   13,   13,   13,   13,  265,  265,  535,  143,  264,
       148553  + /*   690 */   264,  288,  526,  535, 1119,  400,  535,  402,  532,  510,
       148554  + /*   700 */  1457,  512,  532,    6,  113,  415, 1067, 1530,   70,   70,
       148555  + /*   710 */  1530,  535,  271,  535,   70,   70,  535,   13,   13,  116,
       148556  + /*   720 */   116,  116,  116,  115,  115,  114,  114,  114,  113,  415,
       148557  + /*   730 */   272,  277,   13,   13,   13,   13,  535,   13,   13,  384,
       148558  + /*   740 */   535,  304,  425, 1100,  284, 1119,  184,  801,  185,  338,
       148559  + /*   750 */   285,  514, 1532,  369, 1239, 1438, 1182,   70,   70,  425,
       148560  + /*   760 */   424,   70,   70,  119,  120,  110, 1136, 1136,  981,  984,
       148561  + /*   770 */   974,  974,  117,  117,  118,  118,  118,  118,  190, 1065,
       148562  + /*   780 */  1067, 1531,  442,  107, 1531,  408,  264,  264,  264,  264,
       148563  + /*   790 */   383, 1396,  261,  410,   95,  900,  485,  414,  421,  532,
       148564  + /*   800 */  1045,  532,  301, 1133,  303,  488,  433, 1451, 1396, 1398,
       148565  + /*   810 */   278,  535,  278,  520, 1435, 1046,  116,  116,  116,  116,
       148566  + /*   820 */   115,  115,  114,  114,  114,  113,  415,  425,  264,  264,
       148567  + /*   830 */  1047,  190,   54,   54,  535,  291,  384,  264,  264,  362,
       148568  + /*   840 */   962,  532, 1004,  376, 1084,  264,  264, 1029, 1029,  456,
       148569  + /*   850 */   532,  523,  270, 1065, 1084,   55,   55, 1084,  532,  442,
       148570  + /*   860 */   119,  120,  110, 1136, 1136,  981,  984,  974,  974,  117,
       148571  + /*   870 */   117,  118,  118,  118,  118,  535, 1396,  190,  302, 1383,
       148572  + /*   880 */   208,  535,  789,  790,  791,  535,  515,  535, 1323,  371,
       148573  + /*   890 */   337,  234,  233,  232,  459,  515,   15,   15,  459,  477,
       148574  + /*   900 */   459,  459,   44,   44,  136,  900,   56,   56,   57,   57,
       148575  + /*   910 */  1185,  390,  197,  116,  116,  116,  116,  115,  115,  114,
       148576  + /*   920 */   114,  114,  113,  415,  535,  876,  535,  442,  535,  274,
       148577  + /*   930 */   875, 1323,  357,  384,  353,  140, 1426,  946, 1455, 1323,
       148578  + /*   940 */  1390,    6, 1240, 1236,  292,   58,   58,   59,   59,   60,
       148579  + /*   950 */    60,  535, 1456,  384,  535,    6,  399,  119,  120,  110,
       148580  + /*   960 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148581  + /*   970 */   118,  118,   61,   61,  535,   45,   45,  119,  120,  110,
       148582  + /*   980 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148583  + /*   990 */   118,  118, 1477,  479,  202,   46,   46,  275,   95,  455,
       148584  + /*  1000 */   535,  212,  535,  337,  535, 1454,  535,  409,    6,  242,
       148585  + /*  1010 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148586  + /*  1020 */   415,   48,   48,   49,   49,   62,   62,   63,   63,  535,
       148587  + /*  1030 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148588  + /*  1040 */   415,  535,  459,  535, 1134,  535, 1151,  535,  142,  535,
       148589  + /*  1050 */    64,   64,  535, 1338,  535,  494,  535,  446,  535, 1264,
       148590  + /*  1060 */   535, 1337,   14,   14,   65,   65,  125,  125,   66,   66,
       148591  + /*  1070 */    51,   51,  535,   67,   67,   68,   68,   52,   52,  147,
       148592  + /*  1080 */   147,  148,  148, 1453,  317,   98,    6,  535, 1245,  481,
       148593  + /*  1090 */   535,  827,  535,   75,   75, 1134,  102,  481,  100,  535,
       148594  + /*  1100 */   532,  535,  368, 1066, 1503,  384,  535,  845,   53,   53,
       148595  + /*  1110 */    93,   71,   71,  126,  126,  295,  528,  390,  288,  526,
       148596  + /*  1120 */    72,   72,  127,  127,  139,  384,   38,  128,  128,  119,
       148597  + /*  1130 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
       148598  + /*  1140 */   118,  118,  118,  118,  535,  495,  535,  447,  535,  119,
       148599  + /*  1150 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
       148600  + /*  1160 */   118,  118,  118,  118,  235,  124,  124,  146,  146,  145,
       148601  + /*  1170 */   145,  287,  535, 1277,  535, 1157,  535,  391,  161,  263,
       148602  + /*  1180 */   206,  381,  116,  116,  116,  116,  115,  115,  114,  114,
       148603  + /*  1190 */   114,  113,  415,  132,  132,  131,  131,  129,  129,  535,
       148604  + /*  1200 */    30,  535,  116,  116,  116,  116,  115,  115,  114,  114,
       148605  + /*  1210 */   114,  113,  415,  535,  216, 1062, 1276,  535,  370,  535,
       148606  + /*  1220 */   130,  130,   74,   74,  535,  915,  389,  876,   17,  437,
       148607  + /*  1230 */   429,   31,  875,  916,   76,   76,  266,  101,   73,   73,
       148608  + /*  1240 */    43,   43,  835,  834,  308,   47,   47,   95,  825,  943,
       148609  + /*  1250 */   441,  938,  241,  241,  305,  443,  313,  384,  241,   95,
       148610  + /*  1260 */   842,  843,  193,  465, 1209,  327,  237,  436,   95, 1011,
       148611  + /*  1270 */  1007,  909,  873,  237,  241,  107, 1023,  384, 1023,  955,
       148612  + /*  1280 */  1415,  119,  120,  110, 1136, 1136,  981,  984,  974,  974,
       148613  + /*  1290 */   117,  117,  118,  118,  118,  118, 1022,  809, 1022,  825,
       148614  + /*  1300 */   137,  119,  108,  110, 1136, 1136,  981,  984,  974,  974,
       148615  + /*  1310 */   117,  117,  118,  118,  118,  118,  874, 1414,  451,  107,
       148616  + /*  1320 */  1011,  314, 1273,  318,  218,  321,  323,  325, 1224, 1208,
       148617  + /*  1330 */   955,  330,  339,  340,  116,  116,  116,  116,  115,  115,
       148618  + /*  1340 */   114,  114,  114,  113,  415, 1285, 1322, 1260, 1493, 1470,
       148619  + /*  1350 */  1271,  283,  521, 1328,  116,  116,  116,  116,  115,  115,
       148620  + /*  1360 */   114,  114,  114,  113,  415, 1191, 1184, 1173, 1172, 1174,
       148621  + /*  1370 */   522, 1487,  211,  460,  384,  256,  199,  367, 1257,  342,
       148622  + /*  1380 */   195,  470,  307,  344,   11,  333,  525,  445, 1307, 1315,
       148623  + /*  1390 */   375,  203, 1207, 1151,  384,  346, 1387,  188,  360,  120,
       148624  + /*  1400 */   110, 1136, 1136,  981,  984,  974,  974,  117,  117,  118,
       148625  + /*  1410 */   118,  118,  118, 1386,  428, 1490,  245,  300,  348, 1148,
       148626  + /*  1420 */   110, 1136, 1136,  981,  984,  974,  974,  117,  117,  118,
       148627  + /*  1430 */   118,  118,  118,  189,  198, 1434, 1432,   78,   81,  163,
       148628  + /*  1440 */    82,  392,  439, 1392,  173,  105,  527,   35,    4,  157,
       148629  + /*  1450 */  1312,  116,  116,  116,  116,  115,  115,  114,  114,  114,
       148630  + /*  1460 */   113,  415,  530,  165,   93, 1304,  431,  432,  168,  463,
       148631  + /*  1470 */   221,  116,  116,  116,  116,  115,  115,  114,  114,  114,
       148632  + /*  1480 */   113,  415,  169,  452,  170,  416,  171,  374,  372,  438,
       148633  + /*  1490 */    36, 1318,  177,  225, 1381,   87,  458,  524, 1403,  316,
       148634  + /*  1500 */   257,  105,  527,  227,    4,  182,  461,  160,  320,  228,
       148635  + /*  1510 */   377, 1175,  476,  229, 1227, 1226,  405, 1225,  530, 1218,
       148636  + /*  1520 */   961,  378, 1199, 1198,  827,  332,  103,  103, 1197,  407,
       148637  + /*  1530 */     8, 1217, 1502,  104,  487,  416,  537,  536,  281,  282,
       148638  + /*  1540 */   951,  416,  490, 1268,  496,   92,  341,  243, 1269,  343,
       148639  + /*  1550 */   244, 1267,  122,  524,  345, 1461,  515,  288,  526,   10,
       148640  + /*  1560 */   354, 1266, 1460,  352,  504, 1250,   99, 1367,   94,  503,
       148641  + /*  1570 */   499,  951,  951,  953,  954,   27,  961,  347, 1249,  194,
       148642  + /*  1580 */   251,  358,  103,  103,  359, 1181,   34,  538, 1110,  104,
       148643  + /*  1590 */   255,  416,  537,  536,  286,  252,  951,  254,  539,  149,
       148644  + /*  1600 */  1170, 1419, 1165, 1420, 1418,  150, 1417,  135,  279,  785,
       148645  + /*  1610 */   151,  417, 1195,  196,  290,  210,  386, 1194,  269,  387,
       148646  + /*  1620 */   162, 1021,  133,   77, 1192, 1019,  935,  951,  951,  953,
       148647  + /*  1630 */   954,   27, 1479, 1104,  418,  164,  153,  268,  217,  166,
       148648  + /*  1640 */   859,  306,  366,  366,  365,  253,  363,  220, 1035,  798,
       148649  + /*  1650 */   172,  939,  105,  527,  155,    4,  394,  174,  396,  156,
       148650  + /*  1660 */    83, 1038,  213,   84,  294,   85,   86,  223,  222,  530,
       148651  + /*  1670 */  1034,  144,  293,   18,  224,  315,  241, 1027, 1145,  178,
       148652  + /*  1680 */   457,  226,  179,   37,  800,  334,  462,  230,  328,  466,
       148653  + /*  1690 */   180,  471,  416,   88,   19,   20,   89,  280,  838,  158,
       148654  + /*  1700 */   191,   90,  215,  478,  524, 1097,  204,  192,  987,   91,
       148655  + /*  1710 */   152, 1070,   39,  154, 1071,  504,  486,   40,  489,  205,
       148656  + /*  1720 */   505,  260,  105,  527,  214,    4,  908,  961,  262,  183,
       148657  + /*  1730 */   240,   21,  903,  103,  103,  107,   22, 1086,   23,  530,
       148658  + /*  1740 */   104, 1088,  416,  537,  536,   24, 1093,  951,   25, 1074,
       148659  + /*  1750 */  1090, 1094,    7,   33,  511,  186,   26, 1002,  385,   95,
       148660  + /*  1760 */   988,  986,  416,  288,  526,  990, 1044,  246, 1043,  247,
       148661  + /*  1770 */   991,   28,   41,  106,  524,  956,  810,   29,  951,  951,
       148662  + /*  1780 */   953,  954,   27,  531,  361,  504,  423,  248,  869,  249,
       148663  + /*  1790 */   503, 1495,  364, 1105, 1161, 1494, 1161,  961, 1161, 1161,
       148664  + /*  1800 */  1161, 1161, 1161,  103,  103, 1161, 1161, 1161, 1161, 1161,
       148665  + /*  1810 */   104, 1161,  416,  537,  536, 1104,  418,  951, 1161,  268,
       148666  + /*  1820 */  1161, 1161, 1161, 1161,  366,  366,  365,  253,  363, 1161,
       148667  + /*  1830 */  1161,  798, 1161, 1161, 1161, 1161,  105,  527, 1161,    4,
       148668  + /*  1840 */  1161, 1161, 1161, 1161,  213, 1161,  294, 1161,  951,  951,
       148669  + /*  1850 */   953,  954,   27,  530,  293, 1161, 1161, 1161, 1161, 1161,
       148670  + /*  1860 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148671  + /*  1870 */  1161, 1161, 1161, 1161, 1161, 1161,  416, 1161, 1161, 1161,
       148672  + /*  1880 */  1161, 1161, 1161, 1161,  215, 1161, 1161, 1161,  524, 1161,
       148673  + /*  1890 */  1161, 1161,  152, 1161, 1161,  154,  105,  527, 1161,    4,
       148674  + /*  1900 */  1161, 1161, 1161, 1161, 1161, 1161,  214, 1161, 1161, 1161,
       148675  + /*  1910 */  1161,  961, 1161,  530, 1161, 1161, 1161,  103,  103,  880,
       148676  + /*  1920 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
       148677  + /*  1930 */  1161,  951, 1161, 1161, 1161, 1161,  416, 1161, 1161, 1161,
       148678  + /*  1940 */   385, 1161, 1161, 1161, 1161,  288,  526, 1161,  524, 1161,
       148679  + /*  1950 */  1161, 1161, 1161, 1161, 1161, 1161,   97,  527, 1161,    4,
       148680  + /*  1960 */  1161, 1161,  951,  951,  953,  954,   27, 1161,  423, 1161,
       148681  + /*  1970 */  1161,  961, 1161,  530, 1161, 1161, 1161,  103,  103, 1161,
       148682  + /*  1980 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
       148683  + /*  1990 */  1161,  951,  268, 1161, 1161, 1161,  416,  366,  366,  365,
       148684  + /*  2000 */   253,  363, 1161, 1161,  798, 1161, 1161, 1161,  524, 1161,
       148685  + /*  2010 */  1161, 1161, 1161, 1161, 1161, 1161, 1161,  213, 1161,  294,
       148686  + /*  2020 */  1161, 1161,  951,  951,  953,  954,   27,  293, 1161, 1161,
       148687  + /*  2030 */  1161,  961, 1161, 1161, 1161, 1161, 1161,  103,  103, 1161,
       148688  + /*  2040 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
       148689  + /*  2050 */  1161,  951, 1161, 1161, 1161, 1161, 1161,  215, 1161, 1161,
       148690  + /*  2060 */  1161, 1161, 1161, 1161, 1161,  152, 1161, 1161,  154, 1161,
       148691  + /*  2070 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,  214,
       148692  + /*  2080 */  1161, 1161,  951,  951,  953,  954,   27, 1161, 1161, 1161,
       148693  + /*  2090 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148694  + /*  2100 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148695  + /*  2110 */  1161, 1161, 1161,  385, 1161, 1161, 1161, 1161,  288,  526,
       148696  + /*  2120 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148697  + /*  2130 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148698  + /*  2140 */  1161,  423,
147870 148699   };
147871 148700   static const YYCODETYPE yy_lookahead[] = {
147872         - /*     0 */   168,  163,  184,  238,  239,  240,  163,  163,  155,  156,
147873         - /*    10 */   157,  158,  159,  160,  163,  202,  203,  187,  165,   19,
147874         - /*    20 */   167,  163,  184,  185,  259,  202,  203,  174,  184,  185,
147875         - /*    30 */   174,   31,  238,  239,  240,  184,  185,   22,   23,   39,
147876         - /*    40 */   216,   26,  218,   43,   44,   45,   46,   47,   48,   49,
147877         - /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  174,  206,
147878         - /*    60 */   207,  163,  206,  207,  220,  163,  163,  163,  238,  239,
147879         - /*    70 */   240,   59,  219,  229,  231,  219,  183,  245,  174,  223,
147880         - /*    80 */   224,  249,  184,  185,  191,  232,  184,  185,  184,  185,
147881         - /*    90 */   206,  207,   92,   93,   94,   95,   96,   97,   98,   99,
147882         - /*   100 */   100,  101,  102,  219,  102,   81,   91,  163,   96,   97,
147883         - /*   110 */   206,  207,   19,  275,  276,  262,  104,  105,  106,  107,
147884         - /*   120 */   163,  109,  220,  219,  220,  184,  275,  269,  277,  117,
147885         - /*   130 */   187,  229,   19,  229,  101,  102,   43,   44,   45,   46,
147886         - /*   140 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
147887         - /*   150 */    57,  127,  128,  141,  184,  143,   43,   44,   45,   46,
147888         - /*   160 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
147889         - /*   170 */    57,  268,  269,  275,  276,  197,   83,  233,   85,  163,
147890         - /*   180 */    67,  238,  239,  240,  134,   92,   93,   94,   95,   96,
147891         - /*   190 */    97,   98,   99,  100,  101,  102,   19,   54,   55,   56,
147892         - /*   200 */    57,   58,  152,   26,  247,   92,   93,   94,   95,   96,
147893         - /*   210 */    97,   98,   99,  100,  101,  102,   54,   55,   56,   57,
147894         - /*   220 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
147895         - /*   230 */    53,   54,   55,   56,   57,   92,   93,   94,   95,   96,
147896         - /*   240 */    97,   98,   99,  100,  101,  102,   69,   96,   97,   98,
147897         - /*   250 */    99,  100,  101,  102,   92,   93,   94,   95,   96,   97,
147898         - /*   260 */    98,   99,  100,  101,  102,   73,  179,  180,  181,   92,
147899         - /*   270 */    93,   94,   95,   96,   97,   98,   99,  100,  101,  102,
147900         - /*   280 */   163,  191,  192,  163,   98,   99,  100,  101,  102,   19,
147901         - /*   290 */   200,  179,  180,  181,   24,  175,   92,   93,   94,   95,
147902         - /*   300 */    96,   97,   98,   99,  100,  101,  102,  163,  116,  117,
147903         - /*   310 */   118,   22,  163,   43,   44,   45,   46,   47,   48,   49,
147904         - /*   320 */    50,   51,   52,   53,   54,   55,   56,   57,  157,  158,
147905         - /*   330 */   159,  160,  163,  184,  185,  163,  165,   59,  167,   46,
147906         - /*   340 */    90,   76,   11,  174,   73,  174,   19,  198,   59,   19,
147907         - /*   350 */    72,   86,   81,  184,  185,  234,  106,   96,   97,  163,
147908         - /*   360 */   110,  182,   92,   93,   94,   95,   96,   97,   98,   99,
147909         - /*   370 */   100,  101,  102,   46,  230,  206,  207,  206,  207,  163,
147910         - /*   380 */   184,  185,   19,  105,  106,  107,   23,  116,  219,  220,
147911         - /*   390 */   219,  141,  142,  143,  105,  106,  107,  104,  127,  128,
147912         - /*   400 */   184,  185,  141,  232,  143,   59,   43,   44,   45,   46,
147913         - /*   410 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
147914         - /*   420 */    57,  158,  108,  160,   59,  111,  112,  113,  165,  250,
147915         - /*   430 */   167,  104,  102,  262,  255,  121,  220,  174,  108,  109,
147916         - /*   440 */   110,  111,  112,  113,  114,  229,  182,  120,  117,  118,
147917         - /*   450 */   120,  105,  106,  107,  163,   92,   93,   94,   95,   96,
147918         - /*   460 */    97,   98,   99,  100,  101,  102,  163,   22,   59,  206,
147919         - /*   470 */   207,  106,  163,   26,  171,   19,  163,  193,  175,   23,
147920         - /*   480 */   163,   22,  219,  206,  207,  139,  163,   22,   59,  182,
147921         - /*   490 */   117,  118,   59,  184,  185,  232,  219,  184,  185,   43,
147922         - /*   500 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
147923         - /*   510 */    54,   55,   56,   57,  105,  106,  107,  108,   59,  255,
147924         - /*   520 */   111,  112,  113,   90,   59,  262,   22,   98,  174,   12,
147925         - /*   530 */   121,  208,  163,  220,  105,  106,  107,  163,  105,  106,
147926         - /*   540 */    22,   96,   97,  110,   27,  238,  239,  240,   92,   93,
147927         - /*   550 */    94,   95,   96,   97,   98,   99,  100,  101,  102,   42,
147928         - /*   560 */   206,  207,  115,   59,  105,  106,  107,  163,   19,   59,
147929         - /*   570 */   163,  106,   23,  219,  141,  142,  143,   59,  163,  205,
147930         - /*   580 */    63,   59,   72,   22,  124,   59,  163,  270,  234,  129,
147931         - /*   590 */    73,  163,   43,   44,   45,   46,   47,   48,   49,   50,
147932         - /*   600 */    51,   52,   53,   54,   55,   56,   57,  184,  185,  105,
147933         - /*   610 */   106,  107,  184,  185,  163,  105,  106,  107,  265,  266,
147934         - /*   620 */    59,  198,  225,  105,  106,  107,  198,  105,  106,  107,
147935         - /*   630 */   163,  105,  106,  107,  163,  184,  185,   46,   47,   48,
147936         - /*   640 */    49,   92,   93,   94,   95,   96,   97,   98,   99,  100,
147937         - /*   650 */   101,  102,  163,  163,  132,  184,  185,  163,  132,  163,
147938         - /*   660 */   256,   19,  163,  163,  163,   23,  105,  106,  107,  198,
147939         - /*   670 */   163,  220,  205,  184,  185,  163,   35,   81,  184,  185,
147940         - /*   680 */   184,  185,  163,  184,  185,   43,   44,   45,   46,   47,
147941         - /*   690 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
147942         - /*   700 */   163,  110,  163,  184,  185,  109,  205,   66,  163,   59,
147943         - /*   710 */   163,   21,  205,   16,  174,   74,  220,  198,  163,  220,
147944         - /*   720 */   230,  184,  185,  127,  128,  180,  181,  180,  181,  163,
147945         - /*   730 */   175,  242,  174,  233,   92,   93,   94,   95,   96,   97,
147946         - /*   740 */    98,   99,  100,  101,  102,  233,  206,  207,   26,  163,
147947         - /*   750 */   195,  207,  197,   26,   19,  105,  106,  107,   23,  219,
147948         - /*   760 */   119,  260,   26,  219,  206,  207,  174,   19,  174,  230,
147949         - /*   770 */    80,  174,  163,  174,   77,  163,   79,  219,   43,   44,
147950         - /*   780 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
147951         - /*   790 */    55,   56,   57,  248,   12,  248,  184,  185,  206,  207,
147952         - /*   800 */   206,  207,  112,  206,  207,  206,  207,  206,  207,   27,
147953         - /*   810 */   163,  219,  123,  219,  125,  126,  219,  208,  219,  163,
147954         - /*   820 */   219,   22,   23,   23,   42,   26,   26,   92,   93,   94,
147955         - /*   830 */    95,   96,   97,   98,   99,  100,  101,  102,  163,  149,
147956         - /*   840 */   184,  185,  163,  107,  163,   63,  149,   19,  163,  127,
147957         - /*   850 */   128,   23,   22,  105,   24,  116,  117,  118,  131,  184,
147958         - /*   860 */   185,  163,  163,  184,  185,  184,  185,   19,  132,  184,
147959         - /*   870 */   185,   43,   44,   45,   46,   47,   48,   49,   50,   51,
147960         - /*   880 */    52,   53,   54,   55,   56,   57,  146,  163,  148,   59,
147961         - /*   890 */    91,   43,   44,   45,   46,   47,   48,   49,   50,   51,
147962         - /*   900 */    52,   53,   54,   55,   56,   57,  208,  107,  184,  185,
147963         - /*   910 */     7,    8,    9,  116,  117,  118,  163,  163,  163,   24,
147964         - /*   920 */    92,   93,   94,   95,   96,   97,   98,   99,  100,  101,
147965         - /*   930 */   102,   29,  132,  163,  163,   33,  106,  184,  185,  163,
147966         - /*   940 */    92,   93,   94,   95,   96,   97,   98,   99,  100,  101,
147967         - /*   950 */   102,  163,  163,  163,   59,  184,  185,  163,   22,  163,
147968         - /*   960 */   184,  185,  177,  178,  163,  163,  163,   65,  163,  199,
147969         - /*   970 */   163,   26,  184,  185,  184,  185,  163,  163,  184,  185,
147970         - /*   980 */   184,  185,  163,   98,  163,  184,  185,  184,  185,  184,
147971         - /*   990 */   185,  184,  185,  252,  205,  147,  163,   61,  184,  185,
147972         - /*  1000 */   163,  106,  163,  184,  185,  163,  163,  205,  163,  124,
147973         - /*  1010 */   163,  256,  163,  163,  129,   19,  163,  184,  185,  163,
147974         - /*  1020 */   199,  184,  185,  184,  185,   23,  184,  185,   26,  184,
147975         - /*  1030 */   185,  184,  185,  184,  185,   19,  163,  184,  185,   43,
147976         - /*  1040 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
147977         - /*  1050 */    54,   55,   56,   57,  163,  163,  163,  184,  185,   43,
147978         - /*  1060 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
147979         - /*  1070 */    54,   55,   56,   57,   16,  184,  185,  184,  185,  163,
147980         - /*  1080 */   163,   22,   23,  138,  163,   19,  163,  231,   92,   93,
147981         - /*  1090 */    94,   95,   96,   97,   98,   99,  100,  101,  102,  256,
147982         - /*  1100 */   163,  184,  185,  163,  163,  184,  185,  163,   92,   93,
147983         - /*  1110 */    94,   95,   96,   97,   98,   99,  100,  101,  102,  163,
147984         - /*  1120 */    59,  184,  185,  163,  184,  185,  177,  178,  184,  185,
147985         - /*  1130 */   163,  208,  163,  237,  163,   77,  163,   79,  163,   15,
147986         - /*  1140 */   184,  185,  237,  147,  184,  185,   24,  231,  153,  154,
147987         - /*  1150 */    91,  184,  185,  184,  185,  184,  185,  184,  185,  184,
147988         - /*  1160 */   185,   22,   23,   19,  163,  127,  128,  106,   24,  273,
147989         - /*  1170 */   271,  105,  231,  274,  263,  264,  223,  224,  273,   22,
147990         - /*  1180 */   118,   24,   23,   19,   60,   26,  163,   43,   44,   45,
147991         - /*  1190 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
147992         - /*  1200 */    56,   57,  140,   23,   22,  163,   26,   43,   44,   45,
147993         - /*  1210 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
147994         - /*  1220 */    56,   57,   23,  211,   23,   26,   31,   26,   23,   22,
147995         - /*  1230 */    91,   26,  231,  221,   39,   53,   92,   93,   94,   95,
147996         - /*  1240 */    96,   97,   98,   99,  100,  101,  102,   23,   23,  163,
147997         - /*  1250 */    26,   26,  130,   59,  109,  110,   92,   93,   94,   95,
147998         - /*  1260 */    96,   97,   98,   99,  100,  101,  102,   23,    7,    8,
147999         - /*  1270 */    26,  110,   23,   59,   23,   26,   19,   26,  141,   23,
148000         - /*  1280 */   143,  120,   26,  141,  163,  143,   23,   23,  163,   26,
148001         - /*  1290 */    26,  163,  163,  163,  163,  163,   19,  163,  163,  193,
148002         - /*  1300 */   106,   44,   45,   46,   47,   48,   49,   50,   51,   52,
148003         - /*  1310 */    53,   54,   55,   56,   57,  163,  193,  163,  163,  163,
148004         - /*  1320 */   106,  163,   45,   46,   47,   48,   49,   50,   51,   52,
148005         - /*  1330 */    53,   54,   55,   56,   57,  163,  163,  130,  222,  163,
148006         - /*  1340 */   163,  203,  163,   19,   20,  251,   22,  163,  163,   92,
148007         - /*  1350 */    93,   94,   95,   96,   97,   98,   99,  100,  101,  102,
148008         - /*  1360 */    36,  163,  209,  163,  261,  163,  163,  161,  222,   92,
148009         - /*  1370 */    93,   94,   95,   96,   97,   98,   99,  100,  101,  102,
148010         - /*  1380 */   210,  213,  222,   59,  222,  222,  182,  213,  213,  196,
148011         - /*  1390 */   257,  226,  226,   19,   20,   71,   22,  257,  188,  187,
148012         - /*  1400 */   192,  212,  187,  187,  226,   81,  210,  166,   60,  261,
148013         - /*  1410 */    36,  244,  130,  170,   90,  170,   38,  170,  139,  104,
148014         - /*  1420 */    96,   97,   48,  236,   22,  235,   43,  103,  201,  105,
148015         - /*  1430 */   106,  107,  138,   59,  110,  247,  213,   18,  204,  258,
148016         - /*  1440 */   204,  258,  204,  204,  170,   71,   18,  169,  213,  236,
148017         - /*  1450 */   213,  127,  128,  235,  201,  201,   82,  170,  169,  213,
148018         - /*  1460 */   146,   87,   62,  254,   90,  141,  142,  143,  144,  145,
148019         - /*  1470 */    96,   97,  253,  170,  169,   22,  170,  103,  169,  105,
148020         - /*  1480 */   106,  107,  189,  170,  110,  169,  189,  186,   19,   20,
148021         - /*  1490 */   104,   22,  194,  186,  186,   64,  115,  186,  194,  189,
148022         - /*  1500 */   188,  102,  133,  186,  186,   36,  186,  104,  189,   19,
148023         - /*  1510 */    20,  246,   22,  246,  189,  141,  142,  143,  144,  145,
148024         - /*  1520 */     0,    1,    2,  228,  228,    5,   36,  227,   59,  227,
148025         - /*  1530 */    10,   11,   12,   13,   14,  170,   84,   17,  134,  216,
148026         - /*  1540 */    71,  272,  270,   22,  137,  217,   22,  216,  227,   59,
148027         - /*  1550 */    30,   82,   32,  217,  228,  228,   87,  227,  170,   90,
148028         - /*  1560 */    40,   71,  146,  241,  215,   96,   97,  214,  136,  135,
148029         - /*  1570 */   213,   25,  103,   26,  105,  106,  107,  243,  173,  110,
148030         - /*  1580 */    90,  172,   13,    6,  164,  164,   96,   97,   98,  162,
148031         - /*  1590 */    70,  162,  162,  103,  176,  105,  106,  107,   78,  267,
148032         - /*  1600 */   110,   81,  267,  264,  182,  182,  182,  182,   88,  176,
148033         - /*  1610 */   141,  142,  143,  144,  145,  176,  190,    4,  182,  182,
148034         - /*  1620 */   182,   19,   20,  182,   22,  190,    3,   22,  151,   15,
148035         - /*  1630 */    89,  141,  142,  143,  144,  145,   16,  128,   36,   23,
148036         - /*  1640 */    23,  139,  122,   24,  119,  131,   20,  127,  128,  133,
148037         - /*  1650 */    16,    1,  140,  131,  119,   61,  139,   53,   37,   53,
148038         - /*  1660 */    53,   59,   53,  119,  105,   34,  130,    1,    5,   22,
148039         - /*  1670 */   150,  104,  149,   71,   26,   75,   68,   41,   68,  130,
148040         - /*  1680 */   104,   24,   20,   19,   82,  120,  114,   22,   28,   87,
148041         - /*  1690 */    22,   67,   90,   22,   67,   23,   22,   22,   96,   97,
148042         - /*  1700 */    67,   23,  138,   22,   37,  103,  153,  105,  106,  107,
148043         - /*  1710 */     1,    2,  110,   23,    5,   23,   23,   26,   22,   10,
148044         - /*  1720 */    11,   12,   13,   14,   24,   23,   17,   22,   24,  130,
148045         - /*  1730 */    23,   19,   20,   23,   22,  105,   22,   34,   85,   30,
148046         - /*  1740 */    34,   32,   26,  141,  142,  143,  144,  145,   36,   40,
148047         - /*  1750 */   132,   34,   75,   83,   23,   44,   24,   34,   23,   26,
148048         - /*  1760 */    26,   19,   20,   23,   22,   26,   23,   23,   23,   23,
148049         - /*  1770 */    22,   59,   11,   22,   22,   26,   23,   23,   36,   70,
148050         - /*  1780 */    22,   22,  124,   71,  130,  130,  130,   78,   23,  130,
148051         - /*  1790 */    81,   15,    1,  278,  278,  278,  278,   88,  278,  278,
148052         - /*  1800 */   278,   59,   90,  278,  278,  278,  278,  278,   96,   97,
148053         - /*  1810 */   278,  278,  278,   71,  278,  103,  278,  105,  106,  107,
148054         - /*  1820 */   278,  278,  110,  278,  278,  278,  278,  278,  278,  278,
148055         - /*  1830 */   278,  122,   90,  278,  278,  278,  127,  128,   96,   97,
148056         - /*  1840 */   278,  278,  278,  278,  278,  103,  278,  105,  106,  107,
148057         - /*  1850 */   278,  278,  110,  141,  142,  143,  144,  145,  278,  150,
148058         - /*  1860 */   278,  278,  278,    5,  278,  278,  278,  278,   10,   11,
148059         - /*  1870 */    12,   13,   14,  278,  278,   17,  278,  278,  278,  278,
148060         - /*  1880 */   278,  278,  278,  141,  142,  143,  144,  145,   30,  278,
148061         - /*  1890 */    32,  278,  278,  278,  278,  278,  278,  278,   40,  278,
148062         - /*  1900 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148063         - /*  1910 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148064         - /*  1920 */   278,  278,  278,  278,  278,  278,  278,  278,   70,  278,
148065         - /*  1930 */   278,  278,  278,  278,  278,  278,   78,  278,  278,   81,
148066         - /*  1940 */   278,  278,  278,  278,  278,  278,   88,  278,  278,  278,
148067         - /*  1950 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148068         - /*  1960 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148069         - /*  1970 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148070         - /*  1980 */   122,  278,  278,  278,  278,  127,  128,  278,  278,  278,
148071         - /*  1990 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148072         - /*  2000 */   278,  278,  278,  278,  278,  278,  278,  278,  150,  278,
148073         - /*  2010 */   278,  278,  278,  278,  278,  278,  278,  278,  278,
148074         -};
148075         -#define YY_SHIFT_COUNT    (523)
       148701  + /*     0 */   184,  184,  259,  260,  261,  259,  260,  261,  176,  177,
       148702  + /*    10 */   178,  179,  180,  181,  184,  208,  212,  213,  186,   19,
       148703  + /*    20 */   188,  205,  206,  280,  205,  221,   22,  195,   24,  195,
       148704  + /*    30 */   208,   31,  195,  205,   29,  205,  206,  255,   33,   39,
       148705  + /*    40 */   200,  201,  202,   43,   44,   45,   46,   47,   48,   49,
       148706  + /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  205,  227,
       148707  + /*    60 */   228,  227,  228,   59,  227,  228,  259,  260,  261,  252,
       148708  + /*    70 */    65,  241,  240,  184,  240,  223,  224,  240,  244,  245,
       148709  + /*    80 */   250,  259,  260,  261,   19,  253,   54,   55,   56,   57,
       148710  + /*    90 */    58,  184,  255,  184,  205,  206,   96,   97,   98,   99,
       148711  + /*   100 */   100,  101,  102,  103,  104,  105,  106,   46,   47,   48,
       148712  + /*   110 */    49,   46,  296,  297,  110,  283,   19,   96,   97,   98,
       148713  + /*   120 */    99,  100,  101,  102,  103,  104,  105,  106,   96,   97,
       148714  + /*   130 */    98,   99,  100,  101,  102,  103,  104,  105,  106,   94,
       148715  + /*   140 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
       148716  + /*   150 */    53,   54,   55,   56,   57,  110,  106,   73,  251,  114,
       148717  + /*   160 */    73,  178,  179,  180,  181,   59,  184,  292,   81,  186,
       148718  + /*   170 */   295,  188,  218,  108,   19,  114,  184,   11,  195,  184,
       148719  + /*   180 */    83,  184,   85,   54,   55,   56,   57,  205,  206,  124,
       148720  + /*   190 */   145,  146,  147,   96,   97,   98,   99,  100,  101,  102,
       148721  + /*   200 */   103,  104,  105,  106,  120,  121,  122,  120,  102,   81,
       148722  + /*   210 */   227,  228,  220,   19,   16,  109,  110,  111,  131,  132,
       148723  + /*   220 */    26,  184,  184,  240,  229,   96,   97,   98,   99,  100,
       148724  + /*   230 */   101,  102,  103,  104,  105,  106,  253,   43,   44,   45,
       148725  + /*   240 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148726  + /*   250 */    56,   57,  100,  101,  102,  103,  104,  105,  106,  131,
       148727  + /*   260 */   132,  106,  127,   69,  129,  130,  283,  112,  113,  114,
       148728  + /*   270 */   115,  116,  117,  118,   81,   77,   76,   79,  296,  124,
       148729  + /*   280 */   298,  203,  184,   19,   84,   59,   86,  121,  122,   89,
       148730  + /*   290 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148731  + /*   300 */   106,  184,   35,  205,  206,   22,  113,   43,   44,   45,
       148732  + /*   310 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148733  + /*   320 */    56,   57,  205,  206,  131,  132,  100,  101,  291,  292,
       148734  + /*   330 */   114,   67,  295,   66,  108,  109,  110,  111,  138,  113,
       148735  + /*   340 */   124,   74,   59,  179,  184,  181,  184,  121,   22,  271,
       148736  + /*   350 */   186,   19,  188,  184,  276,   59,   24,  184,  241,  195,
       148737  + /*   360 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148738  + /*   370 */   106,  145,   59,  147,  184,   43,   44,   45,   46,   47,
       148739  + /*   380 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
       148740  + /*   390 */   123,  227,  228,  110,  296,  297,   22,   23,  184,  102,
       148741  + /*   400 */   103,  104,  105,  106,  240,  109,  110,  111,  112,  195,
       148742  + /*   410 */   204,  115,  116,  117,   22,  184,  226,  253,  212,  205,
       148743  + /*   420 */   206,  125,  109,  110,  111,   22,  100,  101,   96,   97,
       148744  + /*   430 */    98,   99,  100,  101,  102,  103,  104,  105,  106,  184,
       148745  + /*   440 */    59,  227,  228,  121,  122,   59,  277,  283,   19,  289,
       148746  + /*   450 */   290,   59,   23,   76,  240,  241,  143,   76,   72,  189,
       148747  + /*   460 */   205,  206,   59,   86,  250,   84,   89,   86,  203,   95,
       148748  + /*   470 */    89,  281,   43,   44,   45,   46,   47,   48,   49,   50,
       148749  + /*   480 */    51,   52,   53,   54,   55,   56,   57,  227,  228,  184,
       148750  + /*   490 */   109,  110,  111,   12,  184,  109,  110,  111,  184,  184,
       148751  + /*   500 */   240,  109,  110,  111,  184,  195,  214,   59,   27,  184,
       148752  + /*   510 */   205,  206,  109,  110,  111,  205,  206,  184,  263,  138,
       148753  + /*   520 */   205,  206,  184,   42,   22,   96,   97,   98,   99,  100,
       148754  + /*   530 */   101,  102,  103,  104,  105,  106,  266,  227,  228,   59,
       148755  + /*   540 */   270,  276,   94,   66,   63,   19,  241,   22,   26,   23,
       148756  + /*   550 */   240,  241,   72,   59,   73,  250,  241,  109,  110,   82,
       148757  + /*   560 */    22,   59,  114,  223,  224,  250,  252,   59,   91,   43,
       148758  + /*   570 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       148759  + /*   580 */    54,   55,   56,   57,   59,  184,   26,   59,  268,  109,
       148760  + /*   590 */   110,  111,  184,  145,  146,  147,  112,   59,  203,  115,
       148761  + /*   600 */   116,  117,  277,  109,  110,  111,  205,  206,  195,  125,
       148762  + /*   610 */   277,  109,  110,  111,  100,  101,  139,  109,  110,  111,
       148763  + /*   620 */   219,  184,   96,   97,   98,   99,  100,  101,  102,  103,
       148764  + /*   630 */   104,  105,  106,  111,  109,  110,  111,  109,  110,  111,
       148765  + /*   640 */   227,  228,   19,  184,  136,  184,   23,  109,  110,  111,
       148766  + /*   650 */   200,  201,  202,  240,  259,  260,  261,  195,  136,  145,
       148767  + /*   660 */   184,  147,  184,  184,  136,  214,   43,   44,   45,   46,
       148768  + /*   670 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
       148769  + /*   680 */    57,  205,  206,  205,  206,  227,  228,  184,  229,  227,
       148770  + /*   690 */   228,  131,  132,  184,   59,  219,  184,  219,  240,  291,
       148771  + /*   700 */   292,  184,  240,  295,  105,  106,   22,   23,  205,  206,
       148772  + /*   710 */    26,  184,  251,  184,  205,  206,  184,  205,  206,   96,
       148773  + /*   720 */    97,   98,   99,  100,  101,  102,  103,  104,  105,  106,
       148774  + /*   730 */   251,  219,  205,  206,  205,  206,  184,  205,  206,   19,
       148775  + /*   740 */   184,   16,  184,   23,  241,  110,  219,   21,  219,  184,
       148776  + /*   750 */   241,  219,  286,  287,  195,  184,  195,  205,  206,  201,
       148777  + /*   760 */   202,  205,  206,   43,   44,   45,   46,   47,   48,   49,
       148778  + /*   770 */    50,   51,   52,   53,   54,   55,   56,   57,  184,   95,
       148779  + /*   780 */    22,   23,  184,   26,   26,  220,  227,  228,  227,  228,
       148780  + /*   790 */   196,  184,   23,  241,   26,   26,  195,  241,  184,  240,
       148781  + /*   800 */    12,  240,   77,   26,   79,  195,   80,  290,  201,  202,
       148782  + /*   810 */   216,  184,  218,  195,  184,   27,   96,   97,   98,   99,
       148783  + /*   820 */   100,  101,  102,  103,  104,  105,  106,  269,  227,  228,
       148784  + /*   830 */    42,  184,  205,  206,  184,  184,   19,  227,  228,  192,
       148785  + /*   840 */    23,  240,  116,  196,   76,  227,  228,  120,  121,  122,
       148786  + /*   850 */   240,   63,  254,   95,   86,  205,  206,   89,  240,  184,
       148787  + /*   860 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
       148788  + /*   870 */    53,   54,   55,   56,   57,  184,  269,  184,  153,  153,
       148789  + /*   880 */   111,  184,    7,    8,    9,  184,  138,  184,  184,  196,
       148790  + /*   890 */   184,  120,  121,  122,  184,  138,  205,  206,  184,  102,
       148791  + /*   900 */   184,  184,  205,  206,  156,  136,  205,  206,  205,  206,
       148792  + /*   910 */   198,  199,  135,   96,   97,   98,   99,  100,  101,  102,
       148793  + /*   920 */   103,  104,  105,  106,  184,  128,  184,  184,  184,  254,
       148794  + /*   930 */   133,  184,  237,   19,  239,  229,  226,   23,  292,  184,
       148795  + /*   940 */   226,  295,  226,  226,  184,  205,  206,  205,  206,  205,
       148796  + /*   950 */   206,  184,  292,   19,  184,  295,  252,   43,   44,   45,
       148797  + /*   960 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148798  + /*   970 */    56,   57,  205,  206,  184,  205,  206,   43,   44,   45,
       148799  + /*   980 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148800  + /*   990 */    56,   57,  157,  158,   26,  205,  206,  254,   26,  252,
       148801  + /*  1000 */   184,   15,  184,  184,  184,  292,  184,  252,  295,   24,
       148802  + /*  1010 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148803  + /*  1020 */   106,  205,  206,  205,  206,  205,  206,  205,  206,  184,
       148804  + /*  1030 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148805  + /*  1040 */   106,  184,  184,  184,   59,  184,   60,  184,  229,  184,
       148806  + /*  1050 */   205,  206,  184,  258,  184,   19,  184,   19,  184,  246,
       148807  + /*  1060 */   184,  258,  205,  206,  205,  206,  205,  206,  205,  206,
       148808  + /*  1070 */   205,  206,  184,  205,  206,  205,  206,  205,  206,  205,
       148809  + /*  1080 */   206,  205,  206,  292,  226,  151,  295,  184,  228,  294,
       148810  + /*  1090 */   184,  119,  184,  205,  206,  110,  150,  294,  152,  184,
       148811  + /*  1100 */   240,  184,   22,   23,   23,   19,  184,   26,  205,  206,
       148812  + /*  1110 */   142,  205,  206,  205,  206,  184,  198,  199,  131,  132,
       148813  + /*  1120 */   205,  206,  205,  206,   22,   19,   24,  205,  206,   43,
       148814  + /*  1130 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       148815  + /*  1140 */    54,   55,   56,   57,  184,  109,  184,  109,  184,   43,
       148816  + /*  1150 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       148817  + /*  1160 */    54,   55,   56,   57,   46,  205,  206,  205,  206,  205,
       148818  + /*  1170 */   206,  232,  184,  184,  184,   95,  184,  284,  285,  244,
       148819  + /*  1180 */   245,  242,   96,   97,   98,   99,  100,  101,  102,  103,
       148820  + /*  1190 */   104,  105,  106,  205,  206,  205,  206,  205,  206,  184,
       148821  + /*  1200 */    22,  184,   96,   97,   98,   99,  100,  101,  102,  103,
       148822  + /*  1210 */   104,  105,  106,  184,   24,   23,  184,  184,   26,  184,
       148823  + /*  1220 */   205,  206,  205,  206,  184,   31,  108,  128,   22,  122,
       148824  + /*  1230 */   184,   53,  133,   39,  205,  206,   22,  151,  205,  206,
       148825  + /*  1240 */   205,  206,  113,  114,   23,  205,  206,   26,   59,   23,
       148826  + /*  1250 */    23,  144,   26,   26,  184,   23,   23,   19,   26,   26,
       148827  + /*  1260 */     7,    8,   24,   23,  214,   23,   26,   61,   26,   59,
       148828  + /*  1270 */    23,   23,   23,   26,   26,   26,  145,   19,  147,   59,
       148829  + /*  1280 */   184,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       148830  + /*  1290 */    52,   53,   54,   55,   56,   57,  145,   23,  147,  110,
       148831  + /*  1300 */    26,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       148832  + /*  1310 */    52,   53,   54,   55,   56,   57,   23,  184,  184,   26,
       148833  + /*  1320 */   110,  184,  184,  184,  134,  184,  184,  184,  184,  184,
       148834  + /*  1330 */   110,  184,  184,  184,   96,   97,   98,   99,  100,  101,
       148835  + /*  1340 */   102,  103,  104,  105,  106,  184,  184,  184,  134,  300,
       148836  + /*  1350 */   184,  243,  184,  184,   96,   97,   98,   99,  100,  101,
       148837  + /*  1360 */   102,  103,  104,  105,  106,  184,  184,  184,  184,  184,
       148838  + /*  1370 */   224,  184,  282,  273,   19,  272,  203,  182,  243,  243,
       148839  + /*  1380 */   230,  209,  278,  243,  231,  208,  265,  278,  234,  234,
       148840  + /*  1390 */   234,  217,  213,   60,   19,  243,  208,  237,  233,   44,
       148841  + /*  1400 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
       148842  + /*  1410 */    55,   56,   57,  208,  247,  187,  134,  247,  247,   38,
       148843  + /*  1420 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
       148844  + /*  1430 */    55,   56,   57,  237,  231,  191,  191,  279,  279,  282,
       148845  + /*  1440 */   143,  191,  108,  268,   22,   19,   20,  256,   22,   43,
       148846  + /*  1450 */   257,   96,   97,   98,   99,  100,  101,  102,  103,  104,
       148847  + /*  1460 */   105,  106,   36,  222,  142,  234,   18,  191,  225,   18,
       148848  + /*  1470 */   190,   96,   97,   98,   99,  100,  101,  102,  103,  104,
       148849  + /*  1480 */   105,  106,  225,  191,  225,   59,  225,  257,  234,  234,
       148850  + /*  1490 */   256,  222,  222,  190,  234,  150,   62,   71,  275,  274,
       148851  + /*  1500 */   191,   19,   20,  190,   22,   22,  210,   81,  191,  190,
       148852  + /*  1510 */   210,  191,  108,  190,  207,  207,   64,  207,   36,  215,
       148853  + /*  1520 */    94,  210,  207,  209,  119,  207,  100,  101,  207,  106,
       148854  + /*  1530 */    48,  215,  207,  107,  210,  109,  110,  111,  267,  267,
       148855  + /*  1540 */   114,   59,  210,  249,  137,  108,  248,  191,  249,  248,
       148856  + /*  1550 */    88,  249,  141,   71,  248,  299,  138,  131,  132,   22,
       148857  + /*  1560 */   191,  249,  299,  237,   82,  238,  150,  262,  140,   87,
       148858  + /*  1570 */   139,  145,  146,  147,  148,  149,   94,  248,  238,  236,
       148859  + /*  1580 */    25,  235,  100,  101,  234,  194,   26,  193,   13,  107,
       148860  + /*  1590 */     6,  109,  110,  111,  264,  185,  114,  185,  183,  197,
       148861  + /*  1600 */   183,  203,  183,  203,  203,  197,  203,  211,  211,    4,
       148862  + /*  1610 */   197,    3,  203,   22,  155,   15,  288,  203,   93,  288,
       148863  + /*  1620 */   285,   23,   16,  203,  203,   23,  132,  145,  146,  147,
       148864  + /*  1630 */   148,  149,    0,    1,    2,  143,  123,    5,   24,  135,
       148865  + /*  1640 */    20,   16,   10,   11,   12,   13,   14,  137,    1,   17,
       148866  + /*  1650 */   135,  144,   19,   20,  123,   22,   61,  143,   37,  123,
       148867  + /*  1660 */    53,  109,   30,   53,   32,   53,   53,  134,   34,   36,
       148868  + /*  1670 */     1,    5,   40,   22,  108,  153,   26,   68,   75,   68,
       148869  + /*  1680 */    41,  134,  108,   24,   20,  124,   19,  118,   23,   67,
       148870  + /*  1690 */    22,   67,   59,   22,   22,   22,   22,   67,   28,   37,
       148871  + /*  1700 */    23,  142,   70,   22,   71,   23,  157,   23,   23,   26,
       148872  + /*  1710 */    78,   23,   22,   81,   23,   82,   24,   22,   24,  134,
       148873  + /*  1720 */    87,   23,   19,   20,   92,   22,  109,   94,   23,   22,
       148874  + /*  1730 */    34,   34,  136,  100,  101,   26,   34,   85,   34,   36,
       148875  + /*  1740 */   107,   83,  109,  110,  111,   34,   90,  114,   34,   23,
       148876  + /*  1750 */    75,   75,   44,   22,   24,   26,   34,   23,  126,   26,
       148877  + /*  1760 */    23,   23,   59,  131,  132,   23,   23,   26,   23,   22,
       148878  + /*  1770 */    11,   22,   22,   22,   71,   23,   23,   22,  145,  146,
       148879  + /*  1780 */   147,  148,  149,   26,   23,   82,  154,  134,  128,  134,
       148880  + /*  1790 */    87,  134,   15,    1,  301,  134,  301,   94,  301,  301,
       148881  + /*  1800 */   301,  301,  301,  100,  101,  301,  301,  301,  301,  301,
       148882  + /*  1810 */   107,  301,  109,  110,  111,    1,    2,  114,  301,    5,
       148883  + /*  1820 */   301,  301,  301,  301,   10,   11,   12,   13,   14,  301,
       148884  + /*  1830 */   301,   17,  301,  301,  301,  301,   19,   20,  301,   22,
       148885  + /*  1840 */   301,  301,  301,  301,   30,  301,   32,  301,  145,  146,
       148886  + /*  1850 */   147,  148,  149,   36,   40,  301,  301,  301,  301,  301,
       148887  + /*  1860 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148888  + /*  1870 */   301,  301,  301,  301,  301,  301,   59,  301,  301,  301,
       148889  + /*  1880 */   301,  301,  301,  301,   70,  301,  301,  301,   71,  301,
       148890  + /*  1890 */   301,  301,   78,  301,  301,   81,   19,   20,  301,   22,
       148891  + /*  1900 */   301,  301,  301,  301,  301,  301,   92,  301,  301,  301,
       148892  + /*  1910 */   301,   94,  301,   36,  301,  301,  301,  100,  101,  102,
       148893  + /*  1920 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
       148894  + /*  1930 */   301,  114,  301,  301,  301,  301,   59,  301,  301,  301,
       148895  + /*  1940 */   126,  301,  301,  301,  301,  131,  132,  301,   71,  301,
       148896  + /*  1950 */   301,  301,  301,  301,  301,  301,   19,   20,  301,   22,
       148897  + /*  1960 */   301,  301,  145,  146,  147,  148,  149,  301,  154,  301,
       148898  + /*  1970 */   301,   94,  301,   36,  301,  301,  301,  100,  101,  301,
       148899  + /*  1980 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
       148900  + /*  1990 */   301,  114,    5,  301,  301,  301,   59,   10,   11,   12,
       148901  + /*  2000 */    13,   14,  301,  301,   17,  301,  301,  301,   71,  301,
       148902  + /*  2010 */   301,  301,  301,  301,  301,  301,  301,   30,  301,   32,
       148903  + /*  2020 */   301,  301,  145,  146,  147,  148,  149,   40,  301,  301,
       148904  + /*  2030 */   301,   94,  301,  301,  301,  301,  301,  100,  101,  301,
       148905  + /*  2040 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
       148906  + /*  2050 */   301,  114,  301,  301,  301,  301,  301,   70,  301,  301,
       148907  + /*  2060 */   301,  301,  301,  301,  301,   78,  301,  301,   81,  301,
       148908  + /*  2070 */   301,  301,  301,  301,  301,  301,  301,  301,  301,   92,
       148909  + /*  2080 */   301,  301,  145,  146,  147,  148,  149,  301,  301,  301,
       148910  + /*  2090 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148911  + /*  2100 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148912  + /*  2110 */   301,  301,  301,  126,  301,  301,  301,  301,  131,  132,
       148913  + /*  2120 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148914  + /*  2130 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148915  + /*  2140 */   301,  154,  301,  301,  301,  301,  301,  301,  301,  301,
       148916  + /*  2150 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148917  + /*  2160 */   301,  301,  301,  301,  301,  301,  301,  301,  301,
       148918  +};
       148919  +#define YY_SHIFT_COUNT    (540)
148076 148920   #define YY_SHIFT_MIN      (0)
148077         -#define YY_SHIFT_MAX      (1858)
       148921  +#define YY_SHIFT_MAX      (1987)
148078 148922   static const unsigned short int yy_shift_ofst[] = {
148079         - /*     0 */  1709, 1520, 1858, 1324, 1324,   24, 1374, 1469, 1602, 1712,
148080         - /*    10 */  1712, 1712,  271,    0,    0,  113, 1016, 1712, 1712, 1712,
148081         - /*    20 */  1712, 1712, 1712, 1712, 1712, 1712, 1712,   12,   12,  409,
148082         - /*    30 */   596,   24,   24,   24,   24,   24,   24,   93,  177,  270,
148083         - /*    40 */   363,  456,  549,  642,  735,  828,  848,  996, 1144, 1016,
148084         - /*    50 */  1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016,
148085         - /*    60 */  1016, 1016, 1016, 1016, 1016, 1016, 1016, 1164, 1016, 1257,
148086         - /*    70 */  1277, 1277, 1490, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
148087         - /*    80 */  1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
148088         - /*    90 */  1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
148089         - /*   100 */  1712, 1712, 1712, 1712, 1712, 1742, 1712, 1712, 1712, 1712,
148090         - /*   110 */  1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,  143,
148091         - /*   120 */   162,  162,  162,  162,  162,  204,  151,  186,  650,  690,
148092         - /*   130 */   327,  650,  261,  261,  650,  722,  722,  722,  722,  373,
148093         - /*   140 */    33,    2, 2009, 2009,  330,  330,  330,  346,  289,  278,
148094         - /*   150 */   289,  289,  517,  517,  459,  510,   15,  799,  650,  650,
148095         - /*   160 */   650,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148096         - /*   170 */   650,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148097         - /*   180 */   331,  365,  995,  995,  265,  365,   50, 1038, 2009, 2009,
148098         - /*   190 */  2009,  433,  250,  250,  504,  314,  429,  518,  522,  526,
148099         - /*   200 */   561,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148100         - /*   210 */   192,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148101         - /*   220 */   650,  650,  650,  641,  641,  641,  650,  650,  650,  650,
148102         - /*   230 */   800,  650,  650,  650,  830,  650,  650,  782,  650,  650,
148103         - /*   240 */   650,  650,  650,  650,  650,  650,  739,  902,  689,  895,
148104         - /*   250 */   895,  895,  895,  736,  689,  689,  885,  445,  903, 1124,
148105         - /*   260 */   945,  748,  748, 1066,  945,  945, 1066,  447, 1002,  293,
148106         - /*   270 */  1195, 1195, 1195,  748,  740,  727,  460, 1157, 1348, 1282,
148107         - /*   280 */  1282, 1378, 1378, 1282, 1279, 1315, 1402, 1383, 1294, 1419,
148108         - /*   290 */  1419, 1419, 1419, 1282, 1428, 1294, 1294, 1315, 1402, 1383,
148109         - /*   300 */  1383, 1294, 1282, 1428, 1314, 1400, 1282, 1428, 1453, 1282,
148110         - /*   310 */  1428, 1282, 1428, 1453, 1386, 1386, 1386, 1431, 1453, 1386,
148111         - /*   320 */  1381, 1386, 1431, 1386, 1386, 1453, 1399, 1399, 1453, 1369,
148112         - /*   330 */  1403, 1369, 1403, 1369, 1403, 1369, 1403, 1282, 1404, 1452,
148113         - /*   340 */  1521, 1407, 1404, 1524, 1282, 1416, 1407, 1432, 1434, 1294,
148114         - /*   350 */  1546, 1547, 1569, 1569, 1577, 1577, 1577, 2009, 2009, 2009,
148115         - /*   360 */  2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
148116         - /*   370 */  2009, 2009, 2009,  591,  697, 1059, 1139, 1058,  797,  465,
148117         - /*   380 */  1159, 1182, 1122, 1062, 1180,  936, 1199, 1201, 1205, 1224,
148118         - /*   390 */  1225, 1244, 1061, 1145, 1261, 1161, 1194, 1249, 1251, 1256,
148119         - /*   400 */  1137, 1142, 1263, 1264, 1214, 1207, 1613, 1623, 1605, 1477,
148120         - /*   410 */  1614, 1541, 1620, 1616, 1617, 1509, 1502, 1525, 1619, 1514,
148121         - /*   420 */  1626, 1516, 1634, 1650, 1522, 1512, 1535, 1594, 1621, 1517,
148122         - /*   430 */  1604, 1606, 1607, 1609, 1544, 1559, 1631, 1536, 1666, 1663,
148123         - /*   440 */  1647, 1567, 1523, 1608, 1648, 1610, 1600, 1636, 1549, 1576,
148124         - /*   450 */  1657, 1662, 1664, 1565, 1572, 1665, 1624, 1668, 1671, 1672,
148125         - /*   460 */  1674, 1627, 1660, 1675, 1633, 1667, 1678, 1564, 1681, 1553,
148126         - /*   470 */  1690, 1692, 1691, 1693, 1696, 1700, 1702, 1705, 1704, 1599,
148127         - /*   480 */  1707, 1710, 1630, 1703, 1714, 1618, 1716, 1706, 1716, 1717,
148128         - /*   490 */  1653, 1677, 1670, 1711, 1731, 1732, 1733, 1734, 1723, 1735,
148129         - /*   500 */  1716, 1740, 1743, 1744, 1745, 1739, 1746, 1748, 1761, 1751,
148130         - /*   510 */  1752, 1753, 1754, 1758, 1759, 1749, 1658, 1654, 1655, 1656,
148131         - /*   520 */  1659, 1765, 1776, 1791,
148132         -};
148133         -#define YY_REDUCE_COUNT (372)
148134         -#define YY_REDUCE_MIN   (-235)
148135         -#define YY_REDUCE_MAX   (1441)
       148923  + /*     0 */  1814, 1632, 1987, 1426, 1426,  128, 1482, 1633, 1703, 1877,
       148924  + /*    10 */  1877, 1877,   87,    0,    0,  264, 1106, 1877, 1877, 1877,
       148925  + /*    20 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148926  + /*    30 */   226,  226,  381,  381,  296,  193,  128,  128,  128,  128,
       148927  + /*    40 */   128,  128,   97,  194,  332,  429,  526,  623,  720,  817,
       148928  + /*    50 */   914,  934, 1086, 1238, 1106, 1106, 1106, 1106, 1106, 1106,
       148929  + /*    60 */  1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106,
       148930  + /*    70 */  1106, 1106, 1258, 1106, 1355, 1375, 1375, 1817, 1877, 1877,
       148931  + /*    80 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148932  + /*    90 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148933  + /*   100 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148934  + /*   110 */  1937, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148935  + /*   120 */  1877, 1877, 1877, 1877,   32,  129,  129,  129,  129,  129,
       148936  + /*   130 */    21,  152,  297,  494,  726,   65,  494,  514,  514,  494,
       148937  + /*   140 */   560,  560,  560,  560,  322,  599,   50, 2142, 2142,  155,
       148938  + /*   150 */   155,  155,  313,  392,  386,  392,  392,  481,  481,  200,
       148939  + /*   160 */   480,  684,  758,  494,  494,  494,  494,  494,  494,  494,
       148940  + /*   170 */   494,  494,  494,  494,  494,  494,  494,  494,  494,  494,
       148941  + /*   180 */   494,  494,  494,  494,  768,  768,  494,  166,  377,  377,
       148942  + /*   190 */   635,  835,  835,  635,  748,  987, 2142, 2142, 2142,  448,
       148943  + /*   200 */    45,   45,  403,  484,  502,  106,  525,  508,  528,  538,
       148944  + /*   210 */   494,  494,  494,  494,  494,  494,  494,  494,  494,   84,
       148945  + /*   220 */   494,  494,  494,  494,  494,  494,  494,  494,  494,  494,
       148946  + /*   230 */   494,  494,  267,  267,  267,  494,  494,  494,  494,  769,
       148947  + /*   240 */   494,  494,  494,    4,  477,  494,  494,  788,  494,  494,
       148948  + /*   250 */   494,  494,  494,  494,  494,  494,  727,    5,  135,  985,
       148949  + /*   260 */   985,  985,  985,  522,  135,  135,  797,  326,  875,  986,
       148950  + /*   270 */   968, 1036, 1036, 1038,  968,  968, 1038,  972, 1081, 1118,
       148951  + /*   280 */  1194, 1194, 1194, 1036,  757,  757,  946,  777, 1099, 1102,
       148952  + /*   290 */  1333, 1282, 1282, 1381, 1381, 1282, 1297, 1334, 1422, 1406,
       148953  + /*   300 */  1322, 1448, 1448, 1448, 1448, 1282, 1451, 1322, 1322, 1334,
       148954  + /*   310 */  1422, 1406, 1406, 1322, 1282, 1451, 1345, 1434, 1282, 1451,
       148955  + /*   320 */  1483, 1282, 1451, 1282, 1451, 1483, 1404, 1404, 1404, 1452,
       148956  + /*   330 */  1483, 1404, 1405, 1404, 1452, 1404, 1404, 1483, 1423, 1423,
       148957  + /*   340 */  1483, 1407, 1437, 1407, 1437, 1407, 1437, 1407, 1437, 1282,
       148958  + /*   350 */  1462, 1462, 1411, 1418, 1537, 1282, 1416, 1411, 1428, 1431,
       148959  + /*   360 */  1322, 1555, 1560, 1575, 1575, 1584, 1584, 1584, 2142, 2142,
       148960  + /*   370 */  2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142,
       148961  + /*   380 */  2142, 2142, 2142, 2142,   61,  725,  374, 1080,  198,  771,
       148962  + /*   390 */   283, 1192, 1178, 1190, 1107, 1221, 1206, 1226, 1227, 1232,
       148963  + /*   400 */  1233, 1240, 1242, 1189, 1129, 1253,  216, 1210, 1247, 1248,
       148964  + /*   410 */  1249, 1131, 1151, 1274, 1293, 1220, 1214, 1605, 1608, 1591,
       148965  + /*   420 */  1459, 1600, 1525, 1606, 1598, 1602, 1494, 1492, 1513, 1614,
       148966  + /*   430 */  1504, 1620, 1510, 1625, 1647, 1515, 1507, 1531, 1595, 1621,
       148967  + /*   440 */  1514, 1607, 1610, 1612, 1613, 1536, 1552, 1634, 1533, 1669,
       148968  + /*   450 */  1666, 1651, 1566, 1522, 1609, 1650, 1611, 1603, 1639, 1547,
       148969  + /*   460 */  1574, 1659, 1664, 1667, 1561, 1569, 1668, 1622, 1671, 1672,
       148970  + /*   470 */  1665, 1673, 1624, 1670, 1674, 1630, 1662, 1677, 1559, 1681,
       148971  + /*   480 */  1682, 1549, 1684, 1685, 1683, 1688, 1690, 1692, 1691, 1695,
       148972  + /*   490 */  1694, 1585, 1698, 1705, 1617, 1696, 1707, 1596, 1709, 1697,
       148973  + /*   500 */  1702, 1704, 1711, 1652, 1675, 1658, 1708, 1676, 1656, 1714,
       148974  + /*   510 */  1726, 1731, 1730, 1729, 1733, 1722, 1734, 1709, 1737, 1738,
       148975  + /*   520 */  1742, 1743, 1741, 1745, 1747, 1759, 1749, 1750, 1752, 1753,
       148976  + /*   530 */  1751, 1755, 1757, 1660, 1653, 1655, 1657, 1661, 1761, 1777,
       148977  + /*   540 */  1792,
       148978  +};
       148979  +#define YY_REDUCE_COUNT (383)
       148980  +#define YY_REDUCE_MIN   (-257)
       148981  +#define YY_REDUCE_MAX   (1421)
148136 148982   static const short yy_reduce_ofst[] = {
148137         - /*     0 */  -147,  171,  263,  -96,  169, -144, -162, -149, -102, -156,
148138         - /*    10 */   -98,  216,  354, -170,  -57, -235,  307,  149,  423,  428,
148139         - /*    20 */   471,  313,  451,  519,  489,  496,  499,  545,  547,  555,
148140         - /*    30 */  -116,  540,  558,  592,  594,  597,  599, -206, -206, -206,
148141         - /*    40 */  -206, -206, -206, -206, -206, -206, -206, -206, -206, -206,
148142         - /*    50 */  -206, -206, -206, -206, -206, -206, -206, -206, -206, -206,
148143         - /*    60 */  -206, -206, -206, -206, -206, -206, -206, -206, -206, -206,
148144         - /*    70 */  -206, -206,  196,  309,  494,  537,  612,  656,  675,  679,
148145         - /*    80 */   681,  685,  724,  753,  771,  776,  788,  790,  794,  796,
148146         - /*    90 */   801,  803,  805,  807,  814,  819,  833,  837,  839,  842,
148147         - /*   100 */   845,  847,  849,  853,  873,  891,  893,  917,  921,  937,
148148         - /*   110 */   940,  944,  956,  960,  967,  969,  971,  973,  975, -206,
148149         - /*   120 */  -206, -206, -206, -206, -206, -206, -206, -206,  501, -168,
148150         - /*   130 */    90,  -97,   87,  112,  303,  277,  601,  277,  601,  179,
148151         - /*   140 */  -206, -206, -206, -206, -107, -107, -107,  -43,  -56,  323,
148152         - /*   150 */   500,  512, -187, -177,  317,  609,  353,  353,  120,  144,
148153         - /*   160 */   490,  539,  698,  374,  467,  507,  789,  404, -157,  755,
148154         - /*   170 */   856,  916,  843,  941,  802,  770,  923,  821, 1001, -142,
148155         - /*   180 */   264,  785,  896,  905,  899,  949, -176,  544,  911,  953,
148156         - /*   190 */  1012, -182,  -59,  -30,   16,  -22,  117,  172,  291,  369,
148157         - /*   200 */   407,  415,  566,  586,  647,  699,  754,  813,  850,  892,
148158         - /*   210 */   121, 1023, 1042, 1086, 1121, 1125, 1128, 1129, 1130, 1131,
148159         - /*   220 */  1132, 1134, 1135,  284, 1106, 1123, 1152, 1154, 1155, 1156,
148160         - /*   230 */   397, 1158, 1172, 1173, 1116, 1176, 1177, 1138, 1179,  117,
148161         - /*   240 */  1184, 1185, 1198, 1200, 1202, 1203,  741, 1094, 1153, 1146,
148162         - /*   250 */  1160, 1162, 1163,  397, 1153, 1153, 1170, 1204, 1206, 1103,
148163         - /*   260 */  1168, 1165, 1166, 1133, 1174, 1175, 1140, 1210, 1193, 1208,
148164         - /*   270 */  1212, 1215, 1216, 1178, 1167, 1189, 1196, 1241, 1148, 1243,
148165         - /*   280 */  1245, 1181, 1183, 1247, 1188, 1187, 1190, 1227, 1223, 1234,
148166         - /*   290 */  1236, 1238, 1239, 1274, 1278, 1235, 1237, 1213, 1218, 1253,
148167         - /*   300 */  1254, 1246, 1287, 1289, 1209, 1219, 1303, 1305, 1293, 1306,
148168         - /*   310 */  1309, 1313, 1316, 1297, 1301, 1307, 1308, 1298, 1310, 1311,
148169         - /*   320 */  1312, 1317, 1304, 1318, 1320, 1319, 1265, 1267, 1325, 1295,
148170         - /*   330 */  1300, 1296, 1302, 1326, 1321, 1327, 1330, 1365, 1323, 1269,
148171         - /*   340 */  1272, 1328, 1331, 1322, 1388, 1334, 1336, 1349, 1353, 1357,
148172         - /*   350 */  1405, 1409, 1420, 1421, 1427, 1429, 1430, 1332, 1335, 1339,
148173         - /*   360 */  1418, 1422, 1423, 1424, 1425, 1433, 1426, 1435, 1436, 1437,
148174         - /*   370 */  1438, 1441, 1439,
       148983  + /*     0 */  -168,  -17,  164,  214,  310, -166, -184,  -18,   98, -170,
       148984  + /*    10 */   305,  315, -163, -193, -178, -257,  395,  401,  476,  478,
       148985  + /*    20 */   512,  117,  527,  529,  503,  509,  532,  255,  552,  556,
       148986  + /*    30 */   558,  607,   37,  408,  594,  413,  462,  559,  561,  601,
       148987  + /*    40 */   610,  618, -254, -254, -254, -254, -254, -254, -254, -254,
       148988  + /*    50 */  -254, -254, -254, -254, -254, -254, -254, -254, -254, -254,
       148989  + /*    60 */  -254, -254, -254, -254, -254, -254, -254, -254, -254, -254,
       148990  + /*    70 */  -254, -254, -254, -254, -254, -254, -254, -111,  627,  650,
       148991  + /*    80 */   691,  697,  701,  703,  740,  742,  744,  767,  770,  790,
       148992  + /*    90 */   816,  818,  820,  822,  845,  857,  859,  861,  863,  865,
       148993  + /*   100 */   868,  870,  872,  874,  876,  888,  903,  906,  908,  915,
       148994  + /*   110 */   917,  922,  960,  962,  964,  988,  990,  992, 1015, 1017,
       148995  + /*   120 */  1029, 1033, 1035, 1040, -254, -254, -254, -254, -254, -254,
       148996  + /*   130 */  -254, -254, -254,  190,  270, -196,  160, -160,  450,  647,
       148997  + /*   140 */   260,  458,  260,  458,   78, -254, -254, -254, -254,  206,
       148998  + /*   150 */   206,  206,  320,  598,   -5,  675,  743, -148,  340, -125,
       148999  + /*   160 */   459,  466,  466,  693,  -93,  461,  479,  706,  710,  714,
       149000  + /*   170 */   716,  717,  169, -183,  325,  314,  704,  333,  747,  858,
       149001  + /*   180 */    -8,  819,  565,  755,  646,  660,  517,  265,  713,  791,
       149002  + /*   190 */   712,  795,  803,  918,  695,  860,  893,  935,  939, -181,
       149003  + /*   200 */  -172, -147,  -91,  -46,   -3,  162,  173,  231,  338,  437,
       149004  + /*   210 */   571,  614,  630,  651,  760,  931,  989, 1032, 1046, -218,
       149005  + /*   220 */    38, 1070, 1096, 1133, 1134, 1137, 1138, 1139, 1141, 1142,
       149006  + /*   230 */  1143, 1144,  292,  451, 1050, 1145, 1147, 1148, 1149,  813,
       149007  + /*   240 */  1161, 1162, 1163, 1108, 1049, 1166, 1168, 1146, 1169,  162,
       149008  + /*   250 */  1181, 1182, 1183, 1184, 1185, 1187, 1100, 1103, 1150, 1135,
       149009  + /*   260 */  1136, 1140, 1152,  813, 1150, 1150, 1153, 1173, 1195, 1090,
       149010  + /*   270 */  1154, 1167, 1170, 1104, 1155, 1156, 1109, 1172, 1174, 1179,
       149011  + /*   280 */  1177, 1188, 1205, 1171, 1160, 1196, 1121, 1165, 1203, 1228,
       149012  + /*   290 */  1157, 1244, 1245, 1158, 1159, 1250, 1175, 1193, 1191, 1241,
       149013  + /*   300 */  1231, 1243, 1257, 1259, 1261, 1276, 1280, 1254, 1255, 1230,
       149014  + /*   310 */  1234, 1269, 1270, 1260, 1292, 1303, 1223, 1225, 1309, 1313,
       149015  + /*   320 */  1296, 1317, 1319, 1320, 1323, 1300, 1307, 1308, 1310, 1304,
       149016  + /*   330 */  1311, 1315, 1314, 1318, 1316, 1321, 1325, 1324, 1271, 1272,
       149017  + /*   340 */  1332, 1294, 1298, 1299, 1301, 1302, 1306, 1312, 1329, 1356,
       149018  + /*   350 */  1256, 1263, 1327, 1326, 1305, 1369, 1330, 1340, 1343, 1346,
       149019  + /*   360 */  1350, 1391, 1394, 1410, 1412, 1415, 1417, 1419, 1328, 1331,
       149020  + /*   370 */  1335, 1402, 1398, 1400, 1401, 1403, 1408, 1396, 1397, 1409,
       149021  + /*   380 */  1414, 1420, 1421, 1413,
148175 149022   };
148176 149023   static const YYACTIONTYPE yy_default[] = {
148177         - /*     0 */  1500, 1500, 1500, 1346, 1129, 1235, 1129, 1129, 1129, 1346,
148178         - /*    10 */  1346, 1346, 1129, 1265, 1265, 1399, 1160, 1129, 1129, 1129,
148179         - /*    20 */  1129, 1129, 1129, 1129, 1345, 1129, 1129, 1129, 1129, 1129,
148180         - /*    30 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1271, 1129,
148181         - /*    40 */  1129, 1129, 1129, 1129, 1347, 1348, 1129, 1129, 1129, 1398,
148182         - /*    50 */  1400, 1363, 1281, 1280, 1279, 1278, 1381, 1252, 1276, 1269,
148183         - /*    60 */  1273, 1341, 1342, 1340, 1344, 1348, 1347, 1129, 1272, 1312,
148184         - /*    70 */  1326, 1311, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148185         - /*    80 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148186         - /*    90 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148187         - /*   100 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148188         - /*   110 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1320,
148189         - /*   120 */  1325, 1331, 1324, 1321, 1314, 1313, 1315, 1316, 1129, 1150,
148190         - /*   130 */  1199, 1129, 1129, 1129, 1129, 1417, 1416, 1129, 1129, 1160,
148191         - /*   140 */  1317, 1318, 1328, 1327, 1406, 1456, 1455, 1364, 1129, 1129,
148192         - /*   150 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148193         - /*   160 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148194         - /*   170 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148195         - /*   180 */  1160, 1156, 1306, 1305, 1426, 1156, 1259, 1129, 1412, 1235,
148196         - /*   190 */  1226, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148197         - /*   200 */  1129, 1129, 1129, 1129, 1403, 1401, 1129, 1129, 1129, 1129,
148198         - /*   210 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148199         - /*   220 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148200         - /*   230 */  1129, 1129, 1129, 1129, 1231, 1129, 1129, 1129, 1129, 1129,
148201         - /*   240 */  1129, 1129, 1129, 1129, 1129, 1450, 1129, 1376, 1213, 1231,
148202         - /*   250 */  1231, 1231, 1231, 1233, 1214, 1212, 1225, 1160, 1136, 1492,
148203         - /*   260 */  1275, 1254, 1254, 1489, 1275, 1275, 1489, 1174, 1470, 1171,
148204         - /*   270 */  1265, 1265, 1265, 1254, 1343, 1232, 1225, 1129, 1492, 1240,
148205         - /*   280 */  1240, 1491, 1491, 1240, 1364, 1284, 1290, 1202, 1275, 1208,
148206         - /*   290 */  1208, 1208, 1208, 1240, 1147, 1275, 1275, 1284, 1290, 1202,
148207         - /*   300 */  1202, 1275, 1240, 1147, 1380, 1486, 1240, 1147, 1354, 1240,
148208         - /*   310 */  1147, 1240, 1147, 1354, 1200, 1200, 1200, 1189, 1354, 1200,
148209         - /*   320 */  1174, 1200, 1189, 1200, 1200, 1354, 1358, 1358, 1354, 1258,
148210         - /*   330 */  1253, 1258, 1253, 1258, 1253, 1258, 1253, 1240, 1259, 1425,
148211         - /*   340 */  1129, 1270, 1259, 1349, 1240, 1129, 1270, 1268, 1266, 1275,
148212         - /*   350 */  1153, 1192, 1453, 1453, 1449, 1449, 1449, 1497, 1497, 1412,
148213         - /*   360 */  1465, 1160, 1160, 1160, 1160, 1465, 1176, 1176, 1160, 1160,
148214         - /*   370 */  1160, 1160, 1465, 1129, 1129, 1129, 1129, 1129, 1129, 1460,
148215         - /*   380 */  1129, 1365, 1244, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148216         - /*   390 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148217         - /*   400 */  1129, 1129, 1129, 1129, 1129, 1295, 1129, 1132, 1409, 1129,
148218         - /*   410 */  1129, 1407, 1129, 1129, 1129, 1129, 1129, 1129, 1245, 1129,
148219         - /*   420 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148220         - /*   430 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1488, 1129, 1129,
148221         - /*   440 */  1129, 1129, 1129, 1129, 1379, 1378, 1129, 1129, 1242, 1129,
148222         - /*   450 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148223         - /*   460 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148224         - /*   470 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148225         - /*   480 */  1129, 1129, 1129, 1129, 1129, 1129, 1267, 1129, 1424, 1129,
148226         - /*   490 */  1129, 1129, 1129, 1129, 1129, 1129, 1438, 1260, 1129, 1129,
148227         - /*   500 */  1479, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148228         - /*   510 */  1129, 1129, 1129, 1129, 1129, 1474, 1216, 1297, 1129, 1296,
148229         - /*   520 */  1300, 1129, 1141, 1129,
       149024  + /*     0 */  1536, 1536, 1536, 1376, 1159, 1265, 1159, 1159, 1159, 1376,
       149025  + /*    10 */  1376, 1376, 1159, 1295, 1295, 1429, 1190, 1159, 1159, 1159,
       149026  + /*    20 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1375, 1159, 1159,
       149027  + /*    30 */  1159, 1159, 1459, 1459, 1159, 1159, 1159, 1159, 1159, 1159,
       149028  + /*    40 */  1159, 1159, 1159, 1301, 1159, 1159, 1159, 1159, 1159, 1377,
       149029  + /*    50 */  1378, 1159, 1159, 1159, 1428, 1430, 1393, 1311, 1310, 1309,
       149030  + /*    60 */  1308, 1411, 1282, 1306, 1299, 1303, 1371, 1372, 1370, 1374,
       149031  + /*    70 */  1378, 1377, 1159, 1302, 1342, 1356, 1341, 1159, 1159, 1159,
       149032  + /*    80 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149033  + /*    90 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149034  + /*   100 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149035  + /*   110 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149036  + /*   120 */  1159, 1159, 1159, 1159, 1350, 1355, 1361, 1354, 1351, 1344,
       149037  + /*   130 */  1343, 1345, 1346, 1159, 1180, 1229, 1159, 1159, 1159, 1159,
       149038  + /*   140 */  1447, 1446, 1159, 1159, 1190, 1347, 1348, 1358, 1357, 1436,
       149039  + /*   150 */  1492, 1491, 1394, 1159, 1159, 1159, 1159, 1159, 1159, 1459,
       149040  + /*   160 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149041  + /*   170 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149042  + /*   180 */  1159, 1159, 1159, 1159, 1459, 1459, 1159, 1190, 1459, 1459,
       149043  + /*   190 */  1186, 1336, 1335, 1186, 1289, 1159, 1442, 1265, 1256, 1159,
       149044  + /*   200 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149045  + /*   210 */  1159, 1159, 1159, 1433, 1431, 1159, 1159, 1159, 1159, 1159,
       149046  + /*   220 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149047  + /*   230 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149048  + /*   240 */  1159, 1159, 1159, 1261, 1159, 1159, 1159, 1159, 1159, 1159,
       149049  + /*   250 */  1159, 1159, 1159, 1159, 1159, 1486, 1159, 1406, 1243, 1261,
       149050  + /*   260 */  1261, 1261, 1261, 1263, 1244, 1242, 1255, 1190, 1166, 1528,
       149051  + /*   270 */  1305, 1284, 1284, 1525, 1305, 1305, 1525, 1204, 1506, 1201,
       149052  + /*   280 */  1295, 1295, 1295, 1284, 1289, 1289, 1373, 1262, 1255, 1159,
       149053  + /*   290 */  1528, 1270, 1270, 1527, 1527, 1270, 1394, 1314, 1320, 1232,
       149054  + /*   300 */  1305, 1238, 1238, 1238, 1238, 1270, 1177, 1305, 1305, 1314,
       149055  + /*   310 */  1320, 1232, 1232, 1305, 1270, 1177, 1410, 1522, 1270, 1177,
       149056  + /*   320 */  1384, 1270, 1177, 1270, 1177, 1384, 1230, 1230, 1230, 1219,
       149057  + /*   330 */  1384, 1230, 1204, 1230, 1219, 1230, 1230, 1384, 1388, 1388,
       149058  + /*   340 */  1384, 1288, 1283, 1288, 1283, 1288, 1283, 1288, 1283, 1270,
       149059  + /*   350 */  1469, 1469, 1300, 1289, 1379, 1270, 1159, 1300, 1298, 1296,
       149060  + /*   360 */  1305, 1183, 1222, 1489, 1489, 1485, 1485, 1485, 1533, 1533,
       149061  + /*   370 */  1442, 1501, 1190, 1190, 1190, 1190, 1501, 1206, 1206, 1190,
       149062  + /*   380 */  1190, 1190, 1190, 1501, 1159, 1159, 1159, 1159, 1159, 1159,
       149063  + /*   390 */  1496, 1159, 1395, 1274, 1159, 1159, 1159, 1159, 1159, 1159,
       149064  + /*   400 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149065  + /*   410 */  1159, 1159, 1159, 1159, 1159, 1159, 1325, 1159, 1162, 1439,
       149066  + /*   420 */  1159, 1159, 1437, 1159, 1159, 1159, 1159, 1159, 1159, 1275,
       149067  + /*   430 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149068  + /*   440 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1524, 1159,
       149069  + /*   450 */  1159, 1159, 1159, 1159, 1159, 1409, 1408, 1159, 1159, 1272,
       149070  + /*   460 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149071  + /*   470 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149072  + /*   480 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149073  + /*   490 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1297, 1159,
       149074  + /*   500 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149075  + /*   510 */  1159, 1159, 1159, 1474, 1290, 1159, 1159, 1515, 1159, 1159,
       149076  + /*   520 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149077  + /*   530 */  1159, 1159, 1510, 1246, 1327, 1159, 1326, 1330, 1159, 1171,
       149078  + /*   540 */  1159,
148230 149079   };
148231 149080   /********** End of lemon-generated parsing tables *****************************/
148232 149081   
148233 149082   /* The next table maps tokens (terminal symbols) into fallback tokens.  
148234 149083   ** If a construct like the following:
148235 149084   ** 
148236 149085   **      %fallback ID X Y Z.
................................................................................
148330 149179      59,  /*       WITH => ID */
148331 149180      59,  /*    CURRENT => ID */
148332 149181      59,  /*  FOLLOWING => ID */
148333 149182      59,  /*  PARTITION => ID */
148334 149183      59,  /*  PRECEDING => ID */
148335 149184      59,  /*      RANGE => ID */
148336 149185      59,  /*  UNBOUNDED => ID */
       149186  +   59,  /*    EXCLUDE => ID */
       149187  +   59,  /*     GROUPS => ID */
       149188  +   59,  /*     OTHERS => ID */
       149189  +   59,  /*       TIES => ID */
148337 149190      59,  /*    REINDEX => ID */
148338 149191      59,  /*     RENAME => ID */
148339 149192      59,  /*   CTIME_KW => ID */
148340 149193   };
148341 149194   #endif /* YYFALLBACK */
148342 149195   
148343 149196   /* The following structure represents a single element of the
................................................................................
148508 149361     /*   81 */ "WITH",
148509 149362     /*   82 */ "CURRENT",
148510 149363     /*   83 */ "FOLLOWING",
148511 149364     /*   84 */ "PARTITION",
148512 149365     /*   85 */ "PRECEDING",
148513 149366     /*   86 */ "RANGE",
148514 149367     /*   87 */ "UNBOUNDED",
148515         -  /*   88 */ "REINDEX",
148516         -  /*   89 */ "RENAME",
148517         -  /*   90 */ "CTIME_KW",
148518         -  /*   91 */ "ANY",
148519         -  /*   92 */ "BITAND",
148520         -  /*   93 */ "BITOR",
148521         -  /*   94 */ "LSHIFT",
148522         -  /*   95 */ "RSHIFT",
148523         -  /*   96 */ "PLUS",
148524         -  /*   97 */ "MINUS",
148525         -  /*   98 */ "STAR",
148526         -  /*   99 */ "SLASH",
148527         -  /*  100 */ "REM",
148528         -  /*  101 */ "CONCAT",
148529         -  /*  102 */ "COLLATE",
148530         -  /*  103 */ "BITNOT",
148531         -  /*  104 */ "ON",
148532         -  /*  105 */ "INDEXED",
148533         -  /*  106 */ "STRING",
148534         -  /*  107 */ "JOIN_KW",
148535         -  /*  108 */ "CONSTRAINT",
148536         -  /*  109 */ "DEFAULT",
148537         -  /*  110 */ "NULL",
148538         -  /*  111 */ "PRIMARY",
148539         -  /*  112 */ "UNIQUE",
148540         -  /*  113 */ "CHECK",
148541         -  /*  114 */ "REFERENCES",
148542         -  /*  115 */ "AUTOINCR",
148543         -  /*  116 */ "INSERT",
148544         -  /*  117 */ "DELETE",
148545         -  /*  118 */ "UPDATE",
148546         -  /*  119 */ "SET",
148547         -  /*  120 */ "DEFERRABLE",
148548         -  /*  121 */ "FOREIGN",
148549         -  /*  122 */ "DROP",
148550         -  /*  123 */ "UNION",
148551         -  /*  124 */ "ALL",
148552         -  /*  125 */ "EXCEPT",
148553         -  /*  126 */ "INTERSECT",
148554         -  /*  127 */ "SELECT",
148555         -  /*  128 */ "VALUES",
148556         -  /*  129 */ "DISTINCT",
148557         -  /*  130 */ "DOT",
148558         -  /*  131 */ "FROM",
148559         -  /*  132 */ "JOIN",
148560         -  /*  133 */ "USING",
148561         -  /*  134 */ "ORDER",
148562         -  /*  135 */ "GROUP",
148563         -  /*  136 */ "HAVING",
148564         -  /*  137 */ "LIMIT",
148565         -  /*  138 */ "WHERE",
148566         -  /*  139 */ "INTO",
148567         -  /*  140 */ "NOTHING",
148568         -  /*  141 */ "FLOAT",
148569         -  /*  142 */ "BLOB",
148570         -  /*  143 */ "INTEGER",
148571         -  /*  144 */ "VARIABLE",
148572         -  /*  145 */ "CASE",
148573         -  /*  146 */ "WHEN",
148574         -  /*  147 */ "THEN",
148575         -  /*  148 */ "ELSE",
148576         -  /*  149 */ "INDEX",
148577         -  /*  150 */ "ALTER",
148578         -  /*  151 */ "ADD",
148579         -  /*  152 */ "WINDOW",
148580         -  /*  153 */ "OVER",
148581         -  /*  154 */ "FILTER",
148582         -  /*  155 */ "input",
148583         -  /*  156 */ "cmdlist",
148584         -  /*  157 */ "ecmd",
148585         -  /*  158 */ "cmdx",
148586         -  /*  159 */ "explain",
148587         -  /*  160 */ "cmd",
148588         -  /*  161 */ "transtype",
148589         -  /*  162 */ "trans_opt",
148590         -  /*  163 */ "nm",
148591         -  /*  164 */ "savepoint_opt",
148592         -  /*  165 */ "create_table",
148593         -  /*  166 */ "create_table_args",
148594         -  /*  167 */ "createkw",
148595         -  /*  168 */ "temp",
148596         -  /*  169 */ "ifnotexists",
148597         -  /*  170 */ "dbnm",
148598         -  /*  171 */ "columnlist",
148599         -  /*  172 */ "conslist_opt",
148600         -  /*  173 */ "table_options",
148601         -  /*  174 */ "select",
148602         -  /*  175 */ "columnname",
148603         -  /*  176 */ "carglist",
148604         -  /*  177 */ "typetoken",
148605         -  /*  178 */ "typename",
148606         -  /*  179 */ "signed",
148607         -  /*  180 */ "plus_num",
148608         -  /*  181 */ "minus_num",
148609         -  /*  182 */ "scanpt",
148610         -  /*  183 */ "ccons",
148611         -  /*  184 */ "term",
148612         -  /*  185 */ "expr",
148613         -  /*  186 */ "onconf",
148614         -  /*  187 */ "sortorder",
148615         -  /*  188 */ "autoinc",
148616         -  /*  189 */ "eidlist_opt",
148617         -  /*  190 */ "refargs",
148618         -  /*  191 */ "defer_subclause",
148619         -  /*  192 */ "refarg",
148620         -  /*  193 */ "refact",
148621         -  /*  194 */ "init_deferred_pred_opt",
148622         -  /*  195 */ "conslist",
148623         -  /*  196 */ "tconscomma",
148624         -  /*  197 */ "tcons",
148625         -  /*  198 */ "sortlist",
148626         -  /*  199 */ "eidlist",
148627         -  /*  200 */ "defer_subclause_opt",
148628         -  /*  201 */ "orconf",
148629         -  /*  202 */ "resolvetype",
148630         -  /*  203 */ "raisetype",
148631         -  /*  204 */ "ifexists",
148632         -  /*  205 */ "fullname",
148633         -  /*  206 */ "selectnowith",
148634         -  /*  207 */ "oneselect",
148635         -  /*  208 */ "wqlist",
148636         -  /*  209 */ "multiselect_op",
148637         -  /*  210 */ "distinct",
148638         -  /*  211 */ "selcollist",
148639         -  /*  212 */ "from",
148640         -  /*  213 */ "where_opt",
148641         -  /*  214 */ "groupby_opt",
148642         -  /*  215 */ "having_opt",
148643         -  /*  216 */ "orderby_opt",
148644         -  /*  217 */ "limit_opt",
148645         -  /*  218 */ "window_clause",
148646         -  /*  219 */ "values",
148647         -  /*  220 */ "nexprlist",
148648         -  /*  221 */ "sclp",
148649         -  /*  222 */ "as",
148650         -  /*  223 */ "seltablist",
148651         -  /*  224 */ "stl_prefix",
148652         -  /*  225 */ "joinop",
148653         -  /*  226 */ "indexed_opt",
148654         -  /*  227 */ "on_opt",
148655         -  /*  228 */ "using_opt",
148656         -  /*  229 */ "exprlist",
148657         -  /*  230 */ "xfullname",
148658         -  /*  231 */ "idlist",
148659         -  /*  232 */ "with",
148660         -  /*  233 */ "setlist",
148661         -  /*  234 */ "insert_cmd",
148662         -  /*  235 */ "idlist_opt",
148663         -  /*  236 */ "upsert",
148664         -  /*  237 */ "over_clause",
148665         -  /*  238 */ "likeop",
148666         -  /*  239 */ "between_op",
148667         -  /*  240 */ "in_op",
148668         -  /*  241 */ "paren_exprlist",
148669         -  /*  242 */ "case_operand",
148670         -  /*  243 */ "case_exprlist",
148671         -  /*  244 */ "case_else",
148672         -  /*  245 */ "uniqueflag",
148673         -  /*  246 */ "collate",
148674         -  /*  247 */ "vinto",
148675         -  /*  248 */ "nmnum",
148676         -  /*  249 */ "trigger_decl",
148677         -  /*  250 */ "trigger_cmd_list",
148678         -  /*  251 */ "trigger_time",
148679         -  /*  252 */ "trigger_event",
148680         -  /*  253 */ "foreach_clause",
148681         -  /*  254 */ "when_clause",
148682         -  /*  255 */ "trigger_cmd",
148683         -  /*  256 */ "trnm",
148684         -  /*  257 */ "tridxby",
148685         -  /*  258 */ "database_kw_opt",
148686         -  /*  259 */ "key_opt",
148687         -  /*  260 */ "add_column_fullname",
148688         -  /*  261 */ "kwcolumn_opt",
148689         -  /*  262 */ "create_vtab",
148690         -  /*  263 */ "vtabarglist",
148691         -  /*  264 */ "vtabarg",
148692         -  /*  265 */ "vtabargtoken",
148693         -  /*  266 */ "lp",
148694         -  /*  267 */ "anylist",
148695         -  /*  268 */ "windowdefn_list",
148696         -  /*  269 */ "windowdefn",
148697         -  /*  270 */ "window",
148698         -  /*  271 */ "frame_opt",
148699         -  /*  272 */ "part_opt",
148700         -  /*  273 */ "filter_opt",
148701         -  /*  274 */ "range_or_rows",
148702         -  /*  275 */ "frame_bound",
148703         -  /*  276 */ "frame_bound_s",
148704         -  /*  277 */ "frame_bound_e",
       149368  +  /*   88 */ "EXCLUDE",
       149369  +  /*   89 */ "GROUPS",
       149370  +  /*   90 */ "OTHERS",
       149371  +  /*   91 */ "TIES",
       149372  +  /*   92 */ "REINDEX",
       149373  +  /*   93 */ "RENAME",
       149374  +  /*   94 */ "CTIME_KW",
       149375  +  /*   95 */ "ANY",
       149376  +  /*   96 */ "BITAND",
       149377  +  /*   97 */ "BITOR",
       149378  +  /*   98 */ "LSHIFT",
       149379  +  /*   99 */ "RSHIFT",
       149380  +  /*  100 */ "PLUS",
       149381  +  /*  101 */ "MINUS",
       149382  +  /*  102 */ "STAR",
       149383  +  /*  103 */ "SLASH",
       149384  +  /*  104 */ "REM",
       149385  +  /*  105 */ "CONCAT",
       149386  +  /*  106 */ "COLLATE",
       149387  +  /*  107 */ "BITNOT",
       149388  +  /*  108 */ "ON",
       149389  +  /*  109 */ "INDEXED",
       149390  +  /*  110 */ "STRING",
       149391  +  /*  111 */ "JOIN_KW",
       149392  +  /*  112 */ "CONSTRAINT",
       149393  +  /*  113 */ "DEFAULT",
       149394  +  /*  114 */ "NULL",
       149395  +  /*  115 */ "PRIMARY",
       149396  +  /*  116 */ "UNIQUE",
       149397  +  /*  117 */ "CHECK",
       149398  +  /*  118 */ "REFERENCES",
       149399  +  /*  119 */ "AUTOINCR",
       149400  +  /*  120 */ "INSERT",
       149401  +  /*  121 */ "DELETE",
       149402  +  /*  122 */ "UPDATE",
       149403  +  /*  123 */ "SET",
       149404  +  /*  124 */ "DEFERRABLE",
       149405  +  /*  125 */ "FOREIGN",
       149406  +  /*  126 */ "DROP",
       149407  +  /*  127 */ "UNION",
       149408  +  /*  128 */ "ALL",
       149409  +  /*  129 */ "EXCEPT",
       149410  +  /*  130 */ "INTERSECT",
       149411  +  /*  131 */ "SELECT",
       149412  +  /*  132 */ "VALUES",
       149413  +  /*  133 */ "DISTINCT",
       149414  +  /*  134 */ "DOT",
       149415  +  /*  135 */ "FROM",
       149416  +  /*  136 */ "JOIN",
       149417  +  /*  137 */ "USING",
       149418  +  /*  138 */ "ORDER",
       149419  +  /*  139 */ "GROUP",
       149420  +  /*  140 */ "HAVING",
       149421  +  /*  141 */ "LIMIT",
       149422  +  /*  142 */ "WHERE",
       149423  +  /*  143 */ "INTO",
       149424  +  /*  144 */ "NOTHING",
       149425  +  /*  145 */ "FLOAT",
       149426  +  /*  146 */ "BLOB",
       149427  +  /*  147 */ "INTEGER",
       149428  +  /*  148 */ "VARIABLE",
       149429  +  /*  149 */ "CASE",
       149430  +  /*  150 */ "WHEN",
       149431  +  /*  151 */ "THEN",
       149432  +  /*  152 */ "ELSE",
       149433  +  /*  153 */ "INDEX",
       149434  +  /*  154 */ "ALTER",
       149435  +  /*  155 */ "ADD",
       149436  +  /*  156 */ "WINDOW",
       149437  +  /*  157 */ "OVER",
       149438  +  /*  158 */ "FILTER",
       149439  +  /*  159 */ "TRUEFALSE",
       149440  +  /*  160 */ "ISNOT",
       149441  +  /*  161 */ "FUNCTION",
       149442  +  /*  162 */ "COLUMN",
       149443  +  /*  163 */ "AGG_FUNCTION",
       149444  +  /*  164 */ "AGG_COLUMN",
       149445  +  /*  165 */ "UMINUS",
       149446  +  /*  166 */ "UPLUS",
       149447  +  /*  167 */ "TRUTH",
       149448  +  /*  168 */ "REGISTER",
       149449  +  /*  169 */ "VECTOR",
       149450  +  /*  170 */ "SELECT_COLUMN",
       149451  +  /*  171 */ "IF_NULL_ROW",
       149452  +  /*  172 */ "ASTERISK",
       149453  +  /*  173 */ "SPAN",
       149454  +  /*  174 */ "SPACE",
       149455  +  /*  175 */ "ILLEGAL",
       149456  +  /*  176 */ "input",
       149457  +  /*  177 */ "cmdlist",
       149458  +  /*  178 */ "ecmd",
       149459  +  /*  179 */ "cmdx",
       149460  +  /*  180 */ "explain",
       149461  +  /*  181 */ "cmd",
       149462  +  /*  182 */ "transtype",
       149463  +  /*  183 */ "trans_opt",
       149464  +  /*  184 */ "nm",
       149465  +  /*  185 */ "savepoint_opt",
       149466  +  /*  186 */ "create_table",
       149467  +  /*  187 */ "create_table_args",
       149468  +  /*  188 */ "createkw",
       149469  +  /*  189 */ "temp",
       149470  +  /*  190 */ "ifnotexists",
       149471  +  /*  191 */ "dbnm",
       149472  +  /*  192 */ "columnlist",
       149473  +  /*  193 */ "conslist_opt",
       149474  +  /*  194 */ "table_options",
       149475  +  /*  195 */ "select",
       149476  +  /*  196 */ "columnname",
       149477  +  /*  197 */ "carglist",
       149478  +  /*  198 */ "typetoken",
       149479  +  /*  199 */ "typename",
       149480  +  /*  200 */ "signed",
       149481  +  /*  201 */ "plus_num",
       149482  +  /*  202 */ "minus_num",
       149483  +  /*  203 */ "scanpt",
       149484  +  /*  204 */ "ccons",
       149485  +  /*  205 */ "term",
       149486  +  /*  206 */ "expr",
       149487  +  /*  207 */ "onconf",
       149488  +  /*  208 */ "sortorder",
       149489  +  /*  209 */ "autoinc",
       149490  +  /*  210 */ "eidlist_opt",
       149491  +  /*  211 */ "refargs",
       149492  +  /*  212 */ "defer_subclause",
       149493  +  /*  213 */ "refarg",
       149494  +  /*  214 */ "refact",
       149495  +  /*  215 */ "init_deferred_pred_opt",
       149496  +  /*  216 */ "conslist",
       149497  +  /*  217 */ "tconscomma",
       149498  +  /*  218 */ "tcons",
       149499  +  /*  219 */ "sortlist",
       149500  +  /*  220 */ "eidlist",
       149501  +  /*  221 */ "defer_subclause_opt",
       149502  +  /*  222 */ "orconf",
       149503  +  /*  223 */ "resolvetype",
       149504  +  /*  224 */ "raisetype",
       149505  +  /*  225 */ "ifexists",
       149506  +  /*  226 */ "fullname",
       149507  +  /*  227 */ "selectnowith",
       149508  +  /*  228 */ "oneselect",
       149509  +  /*  229 */ "wqlist",
       149510  +  /*  230 */ "multiselect_op",
       149511  +  /*  231 */ "distinct",
       149512  +  /*  232 */ "selcollist",
       149513  +  /*  233 */ "from",
       149514  +  /*  234 */ "where_opt",
       149515  +  /*  235 */ "groupby_opt",
       149516  +  /*  236 */ "having_opt",
       149517  +  /*  237 */ "orderby_opt",
       149518  +  /*  238 */ "limit_opt",
       149519  +  /*  239 */ "window_clause",
       149520  +  /*  240 */ "values",
       149521  +  /*  241 */ "nexprlist",
       149522  +  /*  242 */ "sclp",
       149523  +  /*  243 */ "as",
       149524  +  /*  244 */ "seltablist",
       149525  +  /*  245 */ "stl_prefix",
       149526  +  /*  246 */ "joinop",
       149527  +  /*  247 */ "indexed_opt",
       149528  +  /*  248 */ "on_opt",
       149529  +  /*  249 */ "using_opt",
       149530  +  /*  250 */ "exprlist",
       149531  +  /*  251 */ "xfullname",
       149532  +  /*  252 */ "idlist",
       149533  +  /*  253 */ "with",
       149534  +  /*  254 */ "setlist",
       149535  +  /*  255 */ "insert_cmd",
       149536  +  /*  256 */ "idlist_opt",
       149537  +  /*  257 */ "upsert",
       149538  +  /*  258 */ "over_clause",
       149539  +  /*  259 */ "likeop",
       149540  +  /*  260 */ "between_op",
       149541  +  /*  261 */ "in_op",
       149542  +  /*  262 */ "paren_exprlist",
       149543  +  /*  263 */ "case_operand",
       149544  +  /*  264 */ "case_exprlist",
       149545  +  /*  265 */ "case_else",
       149546  +  /*  266 */ "uniqueflag",
       149547  +  /*  267 */ "collate",
       149548  +  /*  268 */ "vinto",
       149549  +  /*  269 */ "nmnum",
       149550  +  /*  270 */ "trigger_decl",
       149551  +  /*  271 */ "trigger_cmd_list",
       149552  +  /*  272 */ "trigger_time",
       149553  +  /*  273 */ "trigger_event",
       149554  +  /*  274 */ "foreach_clause",
       149555  +  /*  275 */ "when_clause",
       149556  +  /*  276 */ "trigger_cmd",
       149557  +  /*  277 */ "trnm",
       149558  +  /*  278 */ "tridxby",
       149559  +  /*  279 */ "database_kw_opt",
       149560  +  /*  280 */ "key_opt",
       149561  +  /*  281 */ "add_column_fullname",
       149562  +  /*  282 */ "kwcolumn_opt",
       149563  +  /*  283 */ "create_vtab",
       149564  +  /*  284 */ "vtabarglist",
       149565  +  /*  285 */ "vtabarg",
       149566  +  /*  286 */ "vtabargtoken",
       149567  +  /*  287 */ "lp",
       149568  +  /*  288 */ "anylist",
       149569  +  /*  289 */ "windowdefn_list",
       149570  +  /*  290 */ "windowdefn",
       149571  +  /*  291 */ "window",
       149572  +  /*  292 */ "frame_opt",
       149573  +  /*  293 */ "part_opt",
       149574  +  /*  294 */ "filter_opt",
       149575  +  /*  295 */ "range_or_rows",
       149576  +  /*  296 */ "frame_bound",
       149577  +  /*  297 */ "frame_bound_s",
       149578  +  /*  298 */ "frame_bound_e",
       149579  +  /*  299 */ "frame_exclude_opt",
       149580  +  /*  300 */ "frame_exclude",
148705 149581   };
148706 149582   #endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
148707 149583   
148708 149584   #ifndef NDEBUG
148709 149585   /* For tracing reduce actions, the names of all rules are required.
148710 149586   */
148711 149587   static const char *const yyRuleName[] = {
................................................................................
148995 149871    /* 283 */ "lp ::= LP",
148996 149872    /* 284 */ "with ::= WITH wqlist",
148997 149873    /* 285 */ "with ::= WITH RECURSIVE wqlist",
148998 149874    /* 286 */ "wqlist ::= nm eidlist_opt AS LP select RP",
148999 149875    /* 287 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
149000 149876    /* 288 */ "windowdefn_list ::= windowdefn",
149001 149877    /* 289 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
149002         - /* 290 */ "windowdefn ::= nm AS window",
149003         - /* 291 */ "window ::= LP part_opt orderby_opt frame_opt RP",
149004         - /* 292 */ "part_opt ::= PARTITION BY nexprlist",
149005         - /* 293 */ "part_opt ::=",
149006         - /* 294 */ "frame_opt ::=",
149007         - /* 295 */ "frame_opt ::= range_or_rows frame_bound_s",
149008         - /* 296 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e",
149009         - /* 297 */ "range_or_rows ::= RANGE",
149010         - /* 298 */ "range_or_rows ::= ROWS",
149011         - /* 299 */ "frame_bound_s ::= frame_bound",
149012         - /* 300 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
149013         - /* 301 */ "frame_bound_e ::= frame_bound",
149014         - /* 302 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
149015         - /* 303 */ "frame_bound ::= expr PRECEDING",
149016         - /* 304 */ "frame_bound ::= CURRENT ROW",
149017         - /* 305 */ "frame_bound ::= expr FOLLOWING",
149018         - /* 306 */ "window_clause ::= WINDOW windowdefn_list",
149019         - /* 307 */ "over_clause ::= filter_opt OVER window",
149020         - /* 308 */ "over_clause ::= filter_opt OVER nm",
149021         - /* 309 */ "filter_opt ::=",
149022         - /* 310 */ "filter_opt ::= FILTER LP WHERE expr RP",
149023         - /* 311 */ "input ::= cmdlist",
149024         - /* 312 */ "cmdlist ::= cmdlist ecmd",
149025         - /* 313 */ "cmdlist ::= ecmd",
149026         - /* 314 */ "ecmd ::= SEMI",
149027         - /* 315 */ "ecmd ::= cmdx SEMI",
149028         - /* 316 */ "ecmd ::= explain cmdx",
149029         - /* 317 */ "trans_opt ::=",
149030         - /* 318 */ "trans_opt ::= TRANSACTION",
149031         - /* 319 */ "trans_opt ::= TRANSACTION nm",
149032         - /* 320 */ "savepoint_opt ::= SAVEPOINT",
149033         - /* 321 */ "savepoint_opt ::=",
149034         - /* 322 */ "cmd ::= create_table create_table_args",
149035         - /* 323 */ "columnlist ::= columnlist COMMA columnname carglist",
149036         - /* 324 */ "columnlist ::= columnname carglist",
149037         - /* 325 */ "nm ::= ID|INDEXED",
149038         - /* 326 */ "nm ::= STRING",
149039         - /* 327 */ "nm ::= JOIN_KW",
149040         - /* 328 */ "typetoken ::= typename",
149041         - /* 329 */ "typename ::= ID|STRING",
149042         - /* 330 */ "signed ::= plus_num",
149043         - /* 331 */ "signed ::= minus_num",
149044         - /* 332 */ "carglist ::= carglist ccons",
149045         - /* 333 */ "carglist ::=",
149046         - /* 334 */ "ccons ::= NULL onconf",
149047         - /* 335 */ "conslist_opt ::= COMMA conslist",
149048         - /* 336 */ "conslist ::= conslist tconscomma tcons",
149049         - /* 337 */ "conslist ::= tcons",
149050         - /* 338 */ "tconscomma ::=",
149051         - /* 339 */ "defer_subclause_opt ::= defer_subclause",
149052         - /* 340 */ "resolvetype ::= raisetype",
149053         - /* 341 */ "selectnowith ::= oneselect",
149054         - /* 342 */ "oneselect ::= values",
149055         - /* 343 */ "sclp ::= selcollist COMMA",
149056         - /* 344 */ "as ::= ID|STRING",
149057         - /* 345 */ "expr ::= term",
149058         - /* 346 */ "likeop ::= LIKE_KW|MATCH",
149059         - /* 347 */ "exprlist ::= nexprlist",
149060         - /* 348 */ "nmnum ::= plus_num",
149061         - /* 349 */ "nmnum ::= nm",
149062         - /* 350 */ "nmnum ::= ON",
149063         - /* 351 */ "nmnum ::= DELETE",
149064         - /* 352 */ "nmnum ::= DEFAULT",
149065         - /* 353 */ "plus_num ::= INTEGER|FLOAT",
149066         - /* 354 */ "foreach_clause ::=",
149067         - /* 355 */ "foreach_clause ::= FOR EACH ROW",
149068         - /* 356 */ "trnm ::= nm",
149069         - /* 357 */ "tridxby ::=",
149070         - /* 358 */ "database_kw_opt ::= DATABASE",
149071         - /* 359 */ "database_kw_opt ::=",
149072         - /* 360 */ "kwcolumn_opt ::=",
149073         - /* 361 */ "kwcolumn_opt ::= COLUMNKW",
149074         - /* 362 */ "vtabarglist ::= vtabarg",
149075         - /* 363 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
149076         - /* 364 */ "vtabarg ::= vtabarg vtabargtoken",
149077         - /* 365 */ "anylist ::=",
149078         - /* 366 */ "anylist ::= anylist LP anylist RP",
149079         - /* 367 */ "anylist ::= anylist ANY",
149080         - /* 368 */ "with ::=",
       149878  + /* 290 */ "windowdefn ::= nm AS LP window RP",
       149879  + /* 291 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
       149880  + /* 292 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
       149881  + /* 293 */ "window ::= ORDER BY sortlist frame_opt",
       149882  + /* 294 */ "window ::= nm ORDER BY sortlist frame_opt",
       149883  + /* 295 */ "window ::= frame_opt",
       149884  + /* 296 */ "window ::= nm frame_opt",
       149885  + /* 297 */ "frame_opt ::=",
       149886  + /* 298 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
       149887  + /* 299 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
       149888  + /* 300 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
       149889  + /* 301 */ "frame_bound_s ::= frame_bound",
       149890  + /* 302 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
       149891  + /* 303 */ "frame_bound_e ::= frame_bound",
       149892  + /* 304 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
       149893  + /* 305 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
       149894  + /* 306 */ "frame_bound ::= CURRENT ROW",
       149895  + /* 307 */ "frame_exclude_opt ::=",
       149896  + /* 308 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
       149897  + /* 309 */ "frame_exclude ::= NO OTHERS",
       149898  + /* 310 */ "frame_exclude ::= CURRENT ROW",
       149899  + /* 311 */ "frame_exclude ::= GROUP|TIES",
       149900  + /* 312 */ "window_clause ::= WINDOW windowdefn_list",
       149901  + /* 313 */ "over_clause ::= filter_opt OVER LP window RP",
       149902  + /* 314 */ "over_clause ::= filter_opt OVER nm",
       149903  + /* 315 */ "filter_opt ::=",
       149904  + /* 316 */ "filter_opt ::= FILTER LP WHERE expr RP",
       149905  + /* 317 */ "input ::= cmdlist",
       149906  + /* 318 */ "cmdlist ::= cmdlist ecmd",
       149907  + /* 319 */ "cmdlist ::= ecmd",
       149908  + /* 320 */ "ecmd ::= SEMI",
       149909  + /* 321 */ "ecmd ::= cmdx SEMI",
       149910  + /* 322 */ "ecmd ::= explain cmdx",
       149911  + /* 323 */ "trans_opt ::=",
       149912  + /* 324 */ "trans_opt ::= TRANSACTION",
       149913  + /* 325 */ "trans_opt ::= TRANSACTION nm",
       149914  + /* 326 */ "savepoint_opt ::= SAVEPOINT",
       149915  + /* 327 */ "savepoint_opt ::=",
       149916  + /* 328 */ "cmd ::= create_table create_table_args",
       149917  + /* 329 */ "columnlist ::= columnlist COMMA columnname carglist",
       149918  + /* 330 */ "columnlist ::= columnname carglist",
       149919  + /* 331 */ "nm ::= ID|INDEXED",
       149920  + /* 332 */ "nm ::= STRING",
       149921  + /* 333 */ "nm ::= JOIN_KW",
       149922  + /* 334 */ "typetoken ::= typename",
       149923  + /* 335 */ "typename ::= ID|STRING",
       149924  + /* 336 */ "signed ::= plus_num",
       149925  + /* 337 */ "signed ::= minus_num",
       149926  + /* 338 */ "carglist ::= carglist ccons",
       149927  + /* 339 */ "carglist ::=",
       149928  + /* 340 */ "ccons ::= NULL onconf",
       149929  + /* 341 */ "conslist_opt ::= COMMA conslist",
       149930  + /* 342 */ "conslist ::= conslist tconscomma tcons",
       149931  + /* 343 */ "conslist ::= tcons",
       149932  + /* 344 */ "tconscomma ::=",
       149933  + /* 345 */ "defer_subclause_opt ::= defer_subclause",
       149934  + /* 346 */ "resolvetype ::= raisetype",
       149935  + /* 347 */ "selectnowith ::= oneselect",
       149936  + /* 348 */ "oneselect ::= values",
       149937  + /* 349 */ "sclp ::= selcollist COMMA",
       149938  + /* 350 */ "as ::= ID|STRING",
       149939  + /* 351 */ "expr ::= term",
       149940  + /* 352 */ "likeop ::= LIKE_KW|MATCH",
       149941  + /* 353 */ "exprlist ::= nexprlist",
       149942  + /* 354 */ "nmnum ::= plus_num",
       149943  + /* 355 */ "nmnum ::= nm",
       149944  + /* 356 */ "nmnum ::= ON",
       149945  + /* 357 */ "nmnum ::= DELETE",
       149946  + /* 358 */ "nmnum ::= DEFAULT",
       149947  + /* 359 */ "plus_num ::= INTEGER|FLOAT",
       149948  + /* 360 */ "foreach_clause ::=",
       149949  + /* 361 */ "foreach_clause ::= FOR EACH ROW",
       149950  + /* 362 */ "trnm ::= nm",
       149951  + /* 363 */ "tridxby ::=",
       149952  + /* 364 */ "database_kw_opt ::= DATABASE",
       149953  + /* 365 */ "database_kw_opt ::=",
       149954  + /* 366 */ "kwcolumn_opt ::=",
       149955  + /* 367 */ "kwcolumn_opt ::= COLUMNKW",
       149956  + /* 368 */ "vtabarglist ::= vtabarg",
       149957  + /* 369 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
       149958  + /* 370 */ "vtabarg ::= vtabarg vtabargtoken",
       149959  + /* 371 */ "anylist ::=",
       149960  + /* 372 */ "anylist ::= anylist LP anylist RP",
       149961  + /* 373 */ "anylist ::= anylist ANY",
       149962  + /* 374 */ "with ::=",
149081 149963   };
149082 149964   #endif /* NDEBUG */
149083 149965   
149084 149966   
149085 149967   #if YYSTACKDEPTH<=0
149086 149968   /*
149087 149969   ** Try to increase the size of the parser stack.  Return the number
................................................................................
149199 150081       ** being destroyed before it is finished parsing.
149200 150082       **
149201 150083       ** Note: during a reduce, the only symbols destroyed are those
149202 150084       ** which appear on the RHS of the rule, but which are *not* used
149203 150085       ** inside the C code.
149204 150086       */
149205 150087   /********* Begin destructor definitions ***************************************/
149206         -    case 174: /* select */
149207         -    case 206: /* selectnowith */
149208         -    case 207: /* oneselect */
149209         -    case 219: /* values */
149210         -{
149211         -sqlite3SelectDelete(pParse->db, (yypminor->yy423));
149212         -}
149213         -      break;
149214         -    case 184: /* term */
149215         -    case 185: /* expr */
149216         -    case 213: /* where_opt */
149217         -    case 215: /* having_opt */
149218         -    case 227: /* on_opt */
149219         -    case 242: /* case_operand */
149220         -    case 244: /* case_else */
149221         -    case 247: /* vinto */
149222         -    case 254: /* when_clause */
149223         -    case 259: /* key_opt */
149224         -    case 273: /* filter_opt */
149225         -{
149226         -sqlite3ExprDelete(pParse->db, (yypminor->yy490));
149227         -}
149228         -      break;
149229         -    case 189: /* eidlist_opt */
149230         -    case 198: /* sortlist */
149231         -    case 199: /* eidlist */
149232         -    case 211: /* selcollist */
149233         -    case 214: /* groupby_opt */
149234         -    case 216: /* orderby_opt */
149235         -    case 220: /* nexprlist */
149236         -    case 221: /* sclp */
149237         -    case 229: /* exprlist */
149238         -    case 233: /* setlist */
149239         -    case 241: /* paren_exprlist */
149240         -    case 243: /* case_exprlist */
149241         -    case 272: /* part_opt */
149242         -{
149243         -sqlite3ExprListDelete(pParse->db, (yypminor->yy42));
149244         -}
149245         -      break;
149246         -    case 205: /* fullname */
149247         -    case 212: /* from */
149248         -    case 223: /* seltablist */
149249         -    case 224: /* stl_prefix */
149250         -    case 230: /* xfullname */
149251         -{
149252         -sqlite3SrcListDelete(pParse->db, (yypminor->yy167));
149253         -}
149254         -      break;
149255         -    case 208: /* wqlist */
149256         -{
149257         -sqlite3WithDelete(pParse->db, (yypminor->yy499));
149258         -}
149259         -      break;
149260         -    case 218: /* window_clause */
149261         -    case 268: /* windowdefn_list */
149262         -{
149263         -sqlite3WindowListDelete(pParse->db, (yypminor->yy147));
149264         -}
149265         -      break;
149266         -    case 228: /* using_opt */
149267         -    case 231: /* idlist */
149268         -    case 235: /* idlist_opt */
149269         -{
149270         -sqlite3IdListDelete(pParse->db, (yypminor->yy336));
149271         -}
149272         -      break;
149273         -    case 237: /* over_clause */
149274         -    case 269: /* windowdefn */
149275         -    case 270: /* window */
149276         -    case 271: /* frame_opt */
149277         -{
149278         -sqlite3WindowDelete(pParse->db, (yypminor->yy147));
149279         -}
149280         -      break;
149281         -    case 250: /* trigger_cmd_list */
149282         -    case 255: /* trigger_cmd */
149283         -{
149284         -sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy119));
149285         -}
149286         -      break;
149287         -    case 252: /* trigger_event */
149288         -{
149289         -sqlite3IdListDelete(pParse->db, (yypminor->yy350).b);
149290         -}
149291         -      break;
149292         -    case 275: /* frame_bound */
149293         -    case 276: /* frame_bound_s */
149294         -    case 277: /* frame_bound_e */
149295         -{
149296         -sqlite3ExprDelete(pParse->db, (yypminor->yy317).pExpr);
       150088  +    case 195: /* select */
       150089  +    case 227: /* selectnowith */
       150090  +    case 228: /* oneselect */
       150091  +    case 240: /* values */
       150092  +{
       150093  +sqlite3SelectDelete(pParse->db, (yypminor->yy457));
       150094  +}
       150095  +      break;
       150096  +    case 205: /* term */
       150097  +    case 206: /* expr */
       150098  +    case 234: /* where_opt */
       150099  +    case 236: /* having_opt */
       150100  +    case 248: /* on_opt */
       150101  +    case 263: /* case_operand */
       150102  +    case 265: /* case_else */
       150103  +    case 268: /* vinto */
       150104  +    case 275: /* when_clause */
       150105  +    case 280: /* key_opt */
       150106  +    case 294: /* filter_opt */
       150107  +{
       150108  +sqlite3ExprDelete(pParse->db, (yypminor->yy524));
       150109  +}
       150110  +      break;
       150111  +    case 210: /* eidlist_opt */
       150112  +    case 219: /* sortlist */
       150113  +    case 220: /* eidlist */
       150114  +    case 232: /* selcollist */
       150115  +    case 235: /* groupby_opt */
       150116  +    case 237: /* orderby_opt */
       150117  +    case 241: /* nexprlist */
       150118  +    case 242: /* sclp */
       150119  +    case 250: /* exprlist */
       150120  +    case 254: /* setlist */
       150121  +    case 262: /* paren_exprlist */
       150122  +    case 264: /* case_exprlist */
       150123  +    case 293: /* part_opt */
       150124  +{
       150125  +sqlite3ExprListDelete(pParse->db, (yypminor->yy434));
       150126  +}
       150127  +      break;
       150128  +    case 226: /* fullname */
       150129  +    case 233: /* from */
       150130  +    case 244: /* seltablist */
       150131  +    case 245: /* stl_prefix */
       150132  +    case 251: /* xfullname */
       150133  +{
       150134  +sqlite3SrcListDelete(pParse->db, (yypminor->yy483));
       150135  +}
       150136  +      break;
       150137  +    case 229: /* wqlist */
       150138  +{
       150139  +sqlite3WithDelete(pParse->db, (yypminor->yy59));
       150140  +}
       150141  +      break;
       150142  +    case 239: /* window_clause */
       150143  +    case 289: /* windowdefn_list */
       150144  +{
       150145  +sqlite3WindowListDelete(pParse->db, (yypminor->yy295));
       150146  +}
       150147  +      break;
       150148  +    case 249: /* using_opt */
       150149  +    case 252: /* idlist */
       150150  +    case 256: /* idlist_opt */
       150151  +{
       150152  +sqlite3IdListDelete(pParse->db, (yypminor->yy62));
       150153  +}
       150154  +      break;
       150155  +    case 258: /* over_clause */
       150156  +    case 290: /* windowdefn */
       150157  +    case 291: /* window */
       150158  +    case 292: /* frame_opt */
       150159  +{
       150160  +sqlite3WindowDelete(pParse->db, (yypminor->yy295));
       150161  +}
       150162  +      break;
       150163  +    case 271: /* trigger_cmd_list */
       150164  +    case 276: /* trigger_cmd */
       150165  +{
       150166  +sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy455));
       150167  +}
       150168  +      break;
       150169  +    case 273: /* trigger_event */
       150170  +{
       150171  +sqlite3IdListDelete(pParse->db, (yypminor->yy90).b);
       150172  +}
       150173  +      break;
       150174  +    case 296: /* frame_bound */
       150175  +    case 297: /* frame_bound_s */
       150176  +    case 298: /* frame_bound_e */
       150177  +{
       150178  +sqlite3ExprDelete(pParse->db, (yypminor->yy201).pExpr);
149297 150179   }
149298 150180         break;
149299 150181   /********* End destructor definitions *****************************************/
149300 150182       default:  break;   /* If no destructor action specified: do nothing */
149301 150183     }
149302 150184   }
149303 150185   
................................................................................
149584 150466     yytos->minor.yy0 = yyMinor;
149585 150467     yyTraceShift(yypParser, yyNewState, "Shift");
149586 150468   }
149587 150469   
149588 150470   /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
149589 150471   ** of that rule */
149590 150472   static const YYCODETYPE yyRuleInfoLhs[] = {
149591         -   159,  /* (0) explain ::= EXPLAIN */
149592         -   159,  /* (1) explain ::= EXPLAIN QUERY PLAN */
149593         -   158,  /* (2) cmdx ::= cmd */
149594         -   160,  /* (3) cmd ::= BEGIN transtype trans_opt */
149595         -   161,  /* (4) transtype ::= */
149596         -   161,  /* (5) transtype ::= DEFERRED */
149597         -   161,  /* (6) transtype ::= IMMEDIATE */
149598         -   161,  /* (7) transtype ::= EXCLUSIVE */
149599         -   160,  /* (8) cmd ::= COMMIT|END trans_opt */
149600         -   160,  /* (9) cmd ::= ROLLBACK trans_opt */
149601         -   160,  /* (10) cmd ::= SAVEPOINT nm */
149602         -   160,  /* (11) cmd ::= RELEASE savepoint_opt nm */
149603         -   160,  /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
149604         -   165,  /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
149605         -   167,  /* (14) createkw ::= CREATE */
149606         -   169,  /* (15) ifnotexists ::= */
149607         -   169,  /* (16) ifnotexists ::= IF NOT EXISTS */
149608         -   168,  /* (17) temp ::= TEMP */
149609         -   168,  /* (18) temp ::= */
149610         -   166,  /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
149611         -   166,  /* (20) create_table_args ::= AS select */
149612         -   173,  /* (21) table_options ::= */
149613         -   173,  /* (22) table_options ::= WITHOUT nm */
149614         -   175,  /* (23) columnname ::= nm typetoken */
149615         -   177,  /* (24) typetoken ::= */
149616         -   177,  /* (25) typetoken ::= typename LP signed RP */
149617         -   177,  /* (26) typetoken ::= typename LP signed COMMA signed RP */
149618         -   178,  /* (27) typename ::= typename ID|STRING */
149619         -   182,  /* (28) scanpt ::= */
149620         -   183,  /* (29) ccons ::= CONSTRAINT nm */
149621         -   183,  /* (30) ccons ::= DEFAULT scanpt term scanpt */
149622         -   183,  /* (31) ccons ::= DEFAULT LP expr RP */
149623         -   183,  /* (32) ccons ::= DEFAULT PLUS term scanpt */
149624         -   183,  /* (33) ccons ::= DEFAULT MINUS term scanpt */
149625         -   183,  /* (34) ccons ::= DEFAULT scanpt ID|INDEXED */
149626         -   183,  /* (35) ccons ::= NOT NULL onconf */
149627         -   183,  /* (36) ccons ::= PRIMARY KEY sortorder onconf autoinc */
149628         -   183,  /* (37) ccons ::= UNIQUE onconf */
149629         -   183,  /* (38) ccons ::= CHECK LP expr RP */
149630         -   183,  /* (39) ccons ::= REFERENCES nm eidlist_opt refargs */
149631         -   183,  /* (40) ccons ::= defer_subclause */
149632         -   183,  /* (41) ccons ::= COLLATE ID|STRING */
149633         -   188,  /* (42) autoinc ::= */
149634         -   188,  /* (43) autoinc ::= AUTOINCR */
149635         -   190,  /* (44) refargs ::= */
149636         -   190,  /* (45) refargs ::= refargs refarg */
149637         -   192,  /* (46) refarg ::= MATCH nm */
149638         -   192,  /* (47) refarg ::= ON INSERT refact */
149639         -   192,  /* (48) refarg ::= ON DELETE refact */
149640         -   192,  /* (49) refarg ::= ON UPDATE refact */
149641         -   193,  /* (50) refact ::= SET NULL */
149642         -   193,  /* (51) refact ::= SET DEFAULT */
149643         -   193,  /* (52) refact ::= CASCADE */
149644         -   193,  /* (53) refact ::= RESTRICT */
149645         -   193,  /* (54) refact ::= NO ACTION */
149646         -   191,  /* (55) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
149647         -   191,  /* (56) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
149648         -   194,  /* (57) init_deferred_pred_opt ::= */
149649         -   194,  /* (58) init_deferred_pred_opt ::= INITIALLY DEFERRED */
149650         -   194,  /* (59) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
149651         -   172,  /* (60) conslist_opt ::= */
149652         -   196,  /* (61) tconscomma ::= COMMA */
149653         -   197,  /* (62) tcons ::= CONSTRAINT nm */
149654         -   197,  /* (63) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
149655         -   197,  /* (64) tcons ::= UNIQUE LP sortlist RP onconf */
149656         -   197,  /* (65) tcons ::= CHECK LP expr RP onconf */
149657         -   197,  /* (66) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
149658         -   200,  /* (67) defer_subclause_opt ::= */
149659         -   186,  /* (68) onconf ::= */
149660         -   186,  /* (69) onconf ::= ON CONFLICT resolvetype */
149661         -   201,  /* (70) orconf ::= */
149662         -   201,  /* (71) orconf ::= OR resolvetype */
149663         -   202,  /* (72) resolvetype ::= IGNORE */
149664         -   202,  /* (73) resolvetype ::= REPLACE */
149665         -   160,  /* (74) cmd ::= DROP TABLE ifexists fullname */
149666         -   204,  /* (75) ifexists ::= IF EXISTS */
149667         -   204,  /* (76) ifexists ::= */
149668         -   160,  /* (77) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
149669         -   160,  /* (78) cmd ::= DROP VIEW ifexists fullname */
149670         -   160,  /* (79) cmd ::= select */
149671         -   174,  /* (80) select ::= WITH wqlist selectnowith */