Wapp

Check-in [58675822f3]
Login

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

Overview
Comment:Update the built-in SQLite to the latest 3.28.0 alpha.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 58675822f39b614a58f339847e4840d32a358553c9fd828e887b336c0bd909ac
User & Date: drh 2019-04-09 13:11:31
Context
2019-04-10
15:22
Improved JSON string literal encoding. check-in: 4b187296d9 user: drh tags: trunk
2019-04-09
13:11
Update the built-in SQLite to the latest 3.28.0 alpha. check-in: 58675822f3 user: drh tags: trunk
2019-04-08
20:14
For the %string() substitution, encode newlines and carriage returns using backslash escapes. check-in: 6385090072 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to tclsqlite3.c.

     1      1   #ifndef USE_SYSTEM_SQLITE
     2      2   /******************************************************************************
     3      3   ** This file is an amalgamation of many separate C source files from SQLite
     4         -** version 3.27.0.  By combining all the individual C code files into this
            4  +** version 3.28.0.  By combining all the individual C code files into this
     5      5   ** single large file, the entire code can be compiled as a single translation
     6      6   ** unit.  This allows many compilers to do optimizations that would not be
     7      7   ** possible if the files were compiled separately.  Performance improvements
     8      8   ** of 5% or more are commonly seen when SQLite is compiled as a single
     9      9   ** translation unit.
    10     10   **
    11     11   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
  1159   1159   ** been edited in any way since it was last checked in, then the last
  1160   1160   ** four hexadecimal digits of the hash may be modified.
  1161   1161   **
  1162   1162   ** See also: [sqlite3_libversion()],
  1163   1163   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1164   1164   ** [sqlite_version()] and [sqlite_source_id()].
  1165   1165   */
  1166         -#define SQLITE_VERSION        "3.27.0"
  1167         -#define SQLITE_VERSION_NUMBER 3027000
  1168         -#define SQLITE_SOURCE_ID      "2019-02-05 20:51:41 4d0a949fd92e19fbf243a2e3a1a7c2cdb111f9a6943949d2420dd846bc7d9285"
         1166  +#define SQLITE_VERSION        "3.28.0"
         1167  +#define SQLITE_VERSION_NUMBER 3028000
         1168  +#define SQLITE_SOURCE_ID      "2019-04-09 01:26:31 8820408597341344b308277ce6ef41b47c7514d6c613520789b05ee52cbedae8"
  1169   1169   
  1170   1170   /*
  1171   1171   ** CAPI3REF: Run-Time Library Version Numbers
  1172   1172   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1173   1173   **
  1174   1174   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1175   1175   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1225   1225   **
  1226   1226   ** See also: SQL functions [sqlite_compileoption_used()] and
  1227   1227   ** [sqlite_compileoption_get()] and the [compile_options pragma].
  1228   1228   */
  1229   1229   #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  1230   1230   SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
  1231   1231   SQLITE_API const char *sqlite3_compileoption_get(int N);
         1232  +#else
         1233  +# define sqlite3_compileoption_used(X) 0
         1234  +# define sqlite3_compileoption_get(X)  ((void*)0)
  1232   1235   #endif
  1233   1236   
  1234   1237   /*
  1235   1238   ** CAPI3REF: Test To See If The Library Is Threadsafe
  1236   1239   **
  1237   1240   ** ^The sqlite3_threadsafe() function returns zero if and only if
  1238   1241   ** SQLite was compiled with mutexing code omitted due to the
................................................................................
  3122   3125   ** The second parameter is a pointer to an integer into which
  3123   3126   ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  3124   3127   ** following this call.  The second parameter may be a NULL pointer, in
  3125   3128   ** which case the trigger setting is not reported back. </dd>
  3126   3129   **
  3127   3130   ** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
  3128   3131   ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
  3129         -** <dd> ^This option is used to enable or disable the two-argument
  3130         -** version of the [fts3_tokenizer()] function which is part of the
         3132  +** <dd> ^This option is used to enable or disable the
         3133  +** [fts3_tokenizer()] function which is part of the
  3131   3134   ** [FTS3] full-text search engine extension.
  3132   3135   ** There should be two additional arguments.
  3133   3136   ** The first argument is an integer which is 0 to disable fts3_tokenizer() or
  3134   3137   ** positive to enable fts3_tokenizer() or negative to leave the setting
  3135   3138   ** unchanged.
  3136   3139   ** The second parameter is a pointer to an integer into which
  3137   3140   ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
................................................................................
  3235   3238   ** features include but are not limited to the following:
  3236   3239   ** <ul>
  3237   3240   ** <li> The [PRAGMA writable_schema=ON] statement.
  3238   3241   ** <li> Writes to the [sqlite_dbpage] virtual table.
  3239   3242   ** <li> Direct writes to [shadow tables].
  3240   3243   ** </ul>
  3241   3244   ** </dd>
         3245  +**
         3246  +** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
         3247  +** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
         3248  +** "writable_schema" flag. This has the same effect and is logically equivalent
         3249  +** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
         3250  +** The first argument to this setting is an integer which is 0 to disable 
         3251  +** the writable_schema, positive to enable writable_schema, or negative to
         3252  +** leave the setting unchanged. The second parameter is a pointer to an
         3253  +** integer into which is written 0 or 1 to indicate whether the writable_schema
         3254  +** is enabled or disabled following this call.
         3255  +** </dd>
  3242   3256   ** </dl>
  3243   3257   */
  3244   3258   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  3245   3259   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  3246   3260   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  3247   3261   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  3248   3262   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  3249   3263   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  3250   3264   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  3251   3265   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  3252   3266   #define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
  3253   3267   #define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
  3254   3268   #define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
  3255         -#define SQLITE_DBCONFIG_MAX                   1010 /* Largest DBCONFIG */
         3269  +#define SQLITE_DBCONFIG_WRITABLE_SCHEMA       1011 /* int int* */
         3270  +#define SQLITE_DBCONFIG_MAX                   1011 /* Largest DBCONFIG */
  3256   3271   
  3257   3272   /*
  3258   3273   ** CAPI3REF: Enable Or Disable Extended Result Codes
  3259   3274   ** METHOD: sqlite3
  3260   3275   **
  3261   3276   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  3262   3277   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  3405   3420   ** does not affect the value returned by sqlite3_total_changes().
  3406   3421   ** 
  3407   3422   ** ^Changes made as part of [foreign key actions] are included in the
  3408   3423   ** count, but those made as part of REPLACE constraint resolution are
  3409   3424   ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers 
  3410   3425   ** are not counted.
  3411   3426   **
  3412         -** This the [sqlite3_total_changes(D)] interface only reports the number
         3427  +** The [sqlite3_total_changes(D)] interface only reports the number
  3413   3428   ** of rows that changed due to SQL statement run against database
  3414   3429   ** connection D.  Any changes by other database connections are ignored.
  3415   3430   ** To detect changes against a database file from other database
  3416   3431   ** connections use the [PRAGMA data_version] command or the
  3417   3432   ** [SQLITE_FCNTL_DATA_VERSION] [file control].
  3418   3433   ** 
  3419   3434   ** If a separate thread makes changes on the same database connection
................................................................................
  4930   4945   ** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
  4931   4946   ** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
  4932   4947   ** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
  4933   4948   ** sqlite3_stmt_readonly() returns false for those commands.
  4934   4949   */
  4935   4950   SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  4936   4951   
         4952  +/*
         4953  +** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
         4954  +** METHOD: sqlite3_stmt
         4955  +**
         4956  +** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
         4957  +** prepared statement S is an EXPLAIN statement, or 2 if the
         4958  +** statement S is an EXPLAIN QUERY PLAN.
         4959  +** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
         4960  +** an ordinary statement or a NULL pointer.
         4961  +*/
         4962  +SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
         4963  +
  4937   4964   /*
  4938   4965   ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
  4939   4966   ** METHOD: sqlite3_stmt
  4940   4967   **
  4941   4968   ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
  4942   4969   ** [prepared statement] S has been stepped at least once using 
  4943   4970   ** [sqlite3_step(S)] but has neither run to completion (returned
................................................................................
  5069   5096   ** the value of the fourth parameter then the resulting string value will
  5070   5097   ** contain embedded NULs.  The result of expressions involving strings
  5071   5098   ** with embedded NULs is undefined.
  5072   5099   **
  5073   5100   ** ^The fifth argument to the BLOB and string binding interfaces
  5074   5101   ** is a destructor used to dispose of the BLOB or
  5075   5102   ** string after SQLite has finished with it.  ^The destructor is called
  5076         -** to dispose of the BLOB or string even if the call to bind API fails.
         5103  +** to dispose of the BLOB or string even if the call to the bind API fails,
         5104  +** except the destructor is not called if the third parameter is a NULL
         5105  +** pointer or the fourth parameter is negative.
  5077   5106   ** ^If the fifth argument is
  5078   5107   ** the special value [SQLITE_STATIC], then SQLite assumes that the
  5079   5108   ** information is in static, unmanaged space and does not need to be freed.
  5080   5109   ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  5081   5110   ** SQLite makes its own private copy of the data immediately, before
  5082   5111   ** the sqlite3_bind_*() routine returns.
  5083   5112   **
................................................................................
  5986   6015   ** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
  5987   6016   ** datatype of the value
  5988   6017   ** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
  5989   6018   ** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
  5990   6019   ** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
  5991   6020   ** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
  5992   6021   ** against a virtual table.
         6022  +** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
         6023  +** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
  5993   6024   ** </table></blockquote>
  5994   6025   **
  5995   6026   ** <b>Details:</b>
  5996   6027   **
  5997   6028   ** These routines extract type, size, and content information from
  5998   6029   ** [protected sqlite3_value] objects.  Protected sqlite3_value objects
  5999   6030   ** are used to pass parameter information into implementation of
................................................................................
  6046   6077   ** the value for that column returned without setting a result (probably
  6047   6078   ** because it queried [sqlite3_vtab_nochange()] and found that the column
  6048   6079   ** was unchanging).  ^Within an [xUpdate] method, any value for which
  6049   6080   ** sqlite3_value_nochange(X) is true will in all other respects appear
  6050   6081   ** to be a NULL value.  If sqlite3_value_nochange(X) is invoked anywhere other
  6051   6082   ** than within an [xUpdate] method call for an UPDATE statement, then
  6052   6083   ** the return value is arbitrary and meaningless.
         6084  +**
         6085  +** ^The sqlite3_value_frombind(X) interface returns non-zero if the
         6086  +** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
         6087  +** interfaces.  ^If X comes from an SQL literal value, or a table column,
         6088  +** and expression, then sqlite3_value_frombind(X) returns zero.
  6053   6089   **
  6054   6090   ** Please pay particular attention to the fact that the pointer returned
  6055   6091   ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
  6056   6092   ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
  6057   6093   ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
  6058   6094   ** or [sqlite3_value_text16()].
  6059   6095   **
................................................................................
  6092   6128   SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
  6093   6129   SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
  6094   6130   SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
  6095   6131   SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
  6096   6132   SQLITE_API int sqlite3_value_type(sqlite3_value*);
  6097   6133   SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
  6098   6134   SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
         6135  +SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
  6099   6136   
  6100   6137   /*
  6101   6138   ** CAPI3REF: Finding The Subtype Of SQL Values
  6102   6139   ** METHOD: sqlite3_value
  6103   6140   **
  6104   6141   ** The sqlite3_value_subtype(V) function returns the subtype for
  6105   6142   ** an [application-defined SQL function] argument V.  The subtype
................................................................................
  6827   6864   ** CAPI3REF: Return The Filename For A Database Connection
  6828   6865   ** METHOD: sqlite3
  6829   6866   **
  6830   6867   ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  6831   6868   ** associated with database N of connection D.  ^The main database file
  6832   6869   ** has the name "main".  If there is no attached database N on the database
  6833   6870   ** connection D, or if database N is a temporary or in-memory database, then
  6834         -** a NULL pointer is returned.
         6871  +** this function will return either a NULL pointer or an empty string.
  6835   6872   **
  6836   6873   ** ^The filename returned by this function is the output of the
  6837   6874   ** xFullPathname method of the [VFS].  ^In other words, the filename
  6838   6875   ** will be an absolute pathname, even if the filename used
  6839   6876   ** to open the database originally was a URI or relative pathname.
  6840   6877   */
  6841   6878   SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
................................................................................
 11928  11965   ** CAPI3REF: Rebase a changeset
 11929  11966   ** EXPERIMENTAL
 11930  11967   **
 11931  11968   ** Argument pIn must point to a buffer containing a changeset nIn bytes
 11932  11969   ** in size. This function allocates and populates a buffer with a copy
 11933  11970   ** of the changeset rebased rebased according to the configuration of the
 11934  11971   ** rebaser object passed as the first argument. If successful, (*ppOut)
 11935         -** is set to point to the new buffer containing the rebased changset and 
        11972  +** is set to point to the new buffer containing the rebased changeset and 
 11936  11973   ** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
 11937  11974   ** responsibility of the caller to eventually free the new buffer using
 11938  11975   ** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
 11939  11976   ** are set to zero and an SQLite error code returned.
 11940  11977   */
 11941  11978   SQLITE_API int sqlite3rebaser_rebase(
 11942  11979     sqlite3_rebaser*,
................................................................................
 12337  12374   **
 12338  12375   **
 12339  12376   ** xSetAuxdata(pFts5, pAux, xDelete)
 12340  12377   **
 12341  12378   **   Save the pointer passed as the second argument as the extension functions 
 12342  12379   **   "auxiliary data". The pointer may then be retrieved by the current or any
 12343  12380   **   future invocation of the same fts5 extension function made as part of
 12344         -**   of the same MATCH query using the xGetAuxdata() API.
        12381  +**   the same MATCH query using the xGetAuxdata() API.
 12345  12382   **
 12346  12383   **   Each extension function is allocated a single auxiliary data slot for
 12347  12384   **   each FTS query (MATCH expression). If the extension function is invoked 
 12348  12385   **   more than once for a single FTS query, then all invocations share a 
 12349  12386   **   single auxiliary data context.
 12350  12387   **
 12351  12388   **   If there is already an auxiliary data pointer when this function is
................................................................................
 12352  12389   **   invoked, then it is replaced by the new pointer. If an xDelete callback
 12353  12390   **   was specified along with the original pointer, it is invoked at this
 12354  12391   **   point.
 12355  12392   **
 12356  12393   **   The xDelete callback, if one is specified, is also invoked on the
 12357  12394   **   auxiliary data pointer after the FTS5 query has finished.
 12358  12395   **
 12359         -**   If an error (e.g. an OOM condition) occurs within this function, an
        12396  +**   If an error (e.g. an OOM condition) occurs within this function,
 12360  12397   **   the auxiliary data is set to NULL and an error code returned. If the
 12361  12398   **   xDelete parameter was not NULL, it is invoked on the auxiliary data
 12362  12399   **   pointer before returning.
 12363  12400   **
 12364  12401   **
 12365  12402   ** xGetAuxdata(pFts5, bClear)
 12366  12403   **
................................................................................
 13519  13556   #define TK_WITH                            81
 13520  13557   #define TK_CURRENT                         82
 13521  13558   #define TK_FOLLOWING                       83
 13522  13559   #define TK_PARTITION                       84
 13523  13560   #define TK_PRECEDING                       85
 13524  13561   #define TK_RANGE                           86
 13525  13562   #define TK_UNBOUNDED                       87
 13526         -#define TK_REINDEX                         88
 13527         -#define TK_RENAME                          89
 13528         -#define TK_CTIME_KW                        90
 13529         -#define TK_ANY                             91
 13530         -#define TK_BITAND                          92
 13531         -#define TK_BITOR                           93
 13532         -#define TK_LSHIFT                          94
 13533         -#define TK_RSHIFT                          95
 13534         -#define TK_PLUS                            96
 13535         -#define TK_MINUS                           97
 13536         -#define TK_STAR                            98
 13537         -#define TK_SLASH                           99
 13538         -#define TK_REM                            100
 13539         -#define TK_CONCAT                         101
 13540         -#define TK_COLLATE                        102
 13541         -#define TK_BITNOT                         103
 13542         -#define TK_ON                             104
 13543         -#define TK_INDEXED                        105
 13544         -#define TK_STRING                         106
 13545         -#define TK_JOIN_KW                        107
 13546         -#define TK_CONSTRAINT                     108
 13547         -#define TK_DEFAULT                        109
 13548         -#define TK_NULL                           110
 13549         -#define TK_PRIMARY                        111
 13550         -#define TK_UNIQUE                         112
 13551         -#define TK_CHECK                          113
 13552         -#define TK_REFERENCES                     114
 13553         -#define TK_AUTOINCR                       115
 13554         -#define TK_INSERT                         116
 13555         -#define TK_DELETE                         117
 13556         -#define TK_UPDATE                         118
 13557         -#define TK_SET                            119
 13558         -#define TK_DEFERRABLE                     120
 13559         -#define TK_FOREIGN                        121
 13560         -#define TK_DROP                           122
 13561         -#define TK_UNION                          123
 13562         -#define TK_ALL                            124
 13563         -#define TK_EXCEPT                         125
 13564         -#define TK_INTERSECT                      126
 13565         -#define TK_SELECT                         127
 13566         -#define TK_VALUES                         128
 13567         -#define TK_DISTINCT                       129
 13568         -#define TK_DOT                            130
 13569         -#define TK_FROM                           131
 13570         -#define TK_JOIN                           132
 13571         -#define TK_USING                          133
 13572         -#define TK_ORDER                          134
 13573         -#define TK_GROUP                          135
 13574         -#define TK_HAVING                         136
 13575         -#define TK_LIMIT                          137
 13576         -#define TK_WHERE                          138
 13577         -#define TK_INTO                           139
 13578         -#define TK_NOTHING                        140
 13579         -#define TK_FLOAT                          141
 13580         -#define TK_BLOB                           142
 13581         -#define TK_INTEGER                        143
 13582         -#define TK_VARIABLE                       144
 13583         -#define TK_CASE                           145
 13584         -#define TK_WHEN                           146
 13585         -#define TK_THEN                           147
 13586         -#define TK_ELSE                           148
 13587         -#define TK_INDEX                          149
 13588         -#define TK_ALTER                          150
 13589         -#define TK_ADD                            151
 13590         -#define TK_WINDOW                         152
 13591         -#define TK_OVER                           153
 13592         -#define TK_FILTER                         154
 13593         -#define TK_TRUEFALSE                      155
 13594         -#define TK_ISNOT                          156
 13595         -#define TK_FUNCTION                       157
 13596         -#define TK_COLUMN                         158
 13597         -#define TK_AGG_FUNCTION                   159
 13598         -#define TK_AGG_COLUMN                     160
 13599         -#define TK_UMINUS                         161
 13600         -#define TK_UPLUS                          162
 13601         -#define TK_TRUTH                          163
 13602         -#define TK_REGISTER                       164
 13603         -#define TK_VECTOR                         165
 13604         -#define TK_SELECT_COLUMN                  166
 13605         -#define TK_IF_NULL_ROW                    167
 13606         -#define TK_ASTERISK                       168
 13607         -#define TK_SPAN                           169
 13608         -#define TK_END_OF_FILE                    170
 13609         -#define TK_UNCLOSED_STRING                171
 13610         -#define TK_SPACE                          172
 13611         -#define TK_ILLEGAL                        173
 13612         -
 13613         -/* The token codes above must all fit in 8 bits */
 13614         -#define TKFLG_MASK           0xff  
 13615         -
 13616         -/* Flags that can be added to a token code when it is not
 13617         -** being stored in a u8: */
 13618         -#define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
        13563  +#define TK_EXCLUDE                         88
        13564  +#define TK_GROUPS                          89
        13565  +#define TK_OTHERS                          90
        13566  +#define TK_TIES                            91
        13567  +#define TK_REINDEX                         92
        13568  +#define TK_RENAME                          93
        13569  +#define TK_CTIME_KW                        94
        13570  +#define TK_ANY                             95
        13571  +#define TK_BITAND                          96
        13572  +#define TK_BITOR                           97
        13573  +#define TK_LSHIFT                          98
        13574  +#define TK_RSHIFT                          99
        13575  +#define TK_PLUS                           100
        13576  +#define TK_MINUS                          101
        13577  +#define TK_STAR                           102
        13578  +#define TK_SLASH                          103
        13579  +#define TK_REM                            104
        13580  +#define TK_CONCAT                         105
        13581  +#define TK_COLLATE                        106
        13582  +#define TK_BITNOT                         107
        13583  +#define TK_ON                             108
        13584  +#define TK_INDEXED                        109
        13585  +#define TK_STRING                         110
        13586  +#define TK_JOIN_KW                        111
        13587  +#define TK_CONSTRAINT                     112
        13588  +#define TK_DEFAULT                        113
        13589  +#define TK_NULL                           114
        13590  +#define TK_PRIMARY                        115
        13591  +#define TK_UNIQUE                         116
        13592  +#define TK_CHECK                          117
        13593  +#define TK_REFERENCES                     118
        13594  +#define TK_AUTOINCR                       119
        13595  +#define TK_INSERT                         120
        13596  +#define TK_DELETE                         121
        13597  +#define TK_UPDATE                         122
        13598  +#define TK_SET                            123
        13599  +#define TK_DEFERRABLE                     124
        13600  +#define TK_FOREIGN                        125
        13601  +#define TK_DROP                           126
        13602  +#define TK_UNION                          127
        13603  +#define TK_ALL                            128
        13604  +#define TK_EXCEPT                         129
        13605  +#define TK_INTERSECT                      130
        13606  +#define TK_SELECT                         131
        13607  +#define TK_VALUES                         132
        13608  +#define TK_DISTINCT                       133
        13609  +#define TK_DOT                            134
        13610  +#define TK_FROM                           135
        13611  +#define TK_JOIN                           136
        13612  +#define TK_USING                          137
        13613  +#define TK_ORDER                          138
        13614  +#define TK_GROUP                          139
        13615  +#define TK_HAVING                         140
        13616  +#define TK_LIMIT                          141
        13617  +#define TK_WHERE                          142
        13618  +#define TK_INTO                           143
        13619  +#define TK_NOTHING                        144
        13620  +#define TK_FLOAT                          145
        13621  +#define TK_BLOB                           146
        13622  +#define TK_INTEGER                        147
        13623  +#define TK_VARIABLE                       148
        13624  +#define TK_CASE                           149
        13625  +#define TK_WHEN                           150
        13626  +#define TK_THEN                           151
        13627  +#define TK_ELSE                           152
        13628  +#define TK_INDEX                          153
        13629  +#define TK_ALTER                          154
        13630  +#define TK_ADD                            155
        13631  +#define TK_WINDOW                         156
        13632  +#define TK_OVER                           157
        13633  +#define TK_FILTER                         158
        13634  +#define TK_TRUEFALSE                      159
        13635  +#define TK_ISNOT                          160
        13636  +#define TK_FUNCTION                       161
        13637  +#define TK_COLUMN                         162
        13638  +#define TK_AGG_FUNCTION                   163
        13639  +#define TK_AGG_COLUMN                     164
        13640  +#define TK_UMINUS                         165
        13641  +#define TK_UPLUS                          166
        13642  +#define TK_TRUTH                          167
        13643  +#define TK_REGISTER                       168
        13644  +#define TK_VECTOR                         169
        13645  +#define TK_SELECT_COLUMN                  170
        13646  +#define TK_IF_NULL_ROW                    171
        13647  +#define TK_ASTERISK                       172
        13648  +#define TK_SPAN                           173
        13649  +#define TK_SPACE                          174
        13650  +#define TK_ILLEGAL                        175
 13619  13651   
 13620  13652   /************** End of parse.h ***********************************************/
 13621  13653   /************** Continuing where we left off in sqliteInt.h ******************/
 13622  13654   #include <stdio.h>
 13623  13655   #include <stdlib.h>
 13624  13656   #include <string.h>
 13625  13657   #include <assert.h>
................................................................................
 14543  14575     int nData;              /* Size of pData.  0 if none. */
 14544  14576     int nZero;              /* Extra zero data appended after pData,nData */
 14545  14577   };
 14546  14578   
 14547  14579   SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
 14548  14580                          int flags, int seekResult);
 14549  14581   SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
 14550         -#ifndef SQLITE_OMIT_WINDOWFUNC
 14551         -SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor*);
 14552         -#endif
 14553  14582   SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
 14554  14583   SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
 14555  14584   SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
 14556  14585   SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
 14557  14586   SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
 14558  14587   #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
 14559  14588   SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
................................................................................
 14903  14932   #define OP_Cast           85 /* synopsis: affinity(r[P1])                  */
 14904  14933   #define OP_Permutation    86
 14905  14934   #define OP_Compare        87 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 14906  14935   #define OP_IsTrue         88 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
 14907  14936   #define OP_Offset         89 /* synopsis: r[P3] = sqlite_offset(P1)        */
 14908  14937   #define OP_Column         90 /* synopsis: r[P3]=PX                         */
 14909  14938   #define OP_Affinity       91 /* synopsis: affinity(r[P1@P2])               */
 14910         -#define OP_BitAnd         92 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
 14911         -#define OP_BitOr          93 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
 14912         -#define OP_ShiftLeft      94 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
 14913         -#define OP_ShiftRight     95 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
 14914         -#define OP_Add            96 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
 14915         -#define OP_Subtract       97 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 14916         -#define OP_Multiply       98 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 14917         -#define OP_Divide         99 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 14918         -#define OP_Remainder     100 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 14919         -#define OP_Concat        101 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 14920         -#define OP_MakeRecord    102 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 14921         -#define OP_BitNot        103 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
 14922         -#define OP_Count         104 /* synopsis: r[P2]=count()                    */
 14923         -#define OP_ReadCookie    105
 14924         -#define OP_String8       106 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 14925         -#define OP_SetCookie     107
 14926         -#define OP_ReopenIdx     108 /* synopsis: root=P2 iDb=P3                   */
 14927         -#define OP_OpenRead      109 /* synopsis: root=P2 iDb=P3                   */
 14928         -#define OP_OpenWrite     110 /* synopsis: root=P2 iDb=P3                   */
        14939  +#define OP_MakeRecord     92 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        14940  +#define OP_Count          93 /* synopsis: r[P2]=count()                    */
        14941  +#define OP_ReadCookie     94
        14942  +#define OP_SetCookie      95
        14943  +#define OP_BitAnd         96 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
        14944  +#define OP_BitOr          97 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
        14945  +#define OP_ShiftLeft      98 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
        14946  +#define OP_ShiftRight     99 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
        14947  +#define OP_Add           100 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
        14948  +#define OP_Subtract      101 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
        14949  +#define OP_Multiply      102 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
        14950  +#define OP_Divide        103 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
        14951  +#define OP_Remainder     104 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
        14952  +#define OP_Concat        105 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
        14953  +#define OP_ReopenIdx     106 /* synopsis: root=P2 iDb=P3                   */
        14954  +#define OP_BitNot        107 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
        14955  +#define OP_OpenRead      108 /* synopsis: root=P2 iDb=P3                   */
        14956  +#define OP_OpenWrite     109 /* synopsis: root=P2 iDb=P3                   */
        14957  +#define OP_String8       110 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 14929  14958   #define OP_OpenDup       111
 14930  14959   #define OP_OpenAutoindex 112 /* synopsis: nColumn=P2                       */
 14931  14960   #define OP_OpenEphemeral 113 /* synopsis: nColumn=P2                       */
 14932  14961   #define OP_SorterOpen    114
 14933  14962   #define OP_SequenceTest  115 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 14934  14963   #define OP_OpenPseudo    116 /* synopsis: P3 columns in r[P2]              */
 14935  14964   #define OP_Close         117
 14936  14965   #define OP_ColumnsUsed   118
 14937  14966   #define OP_SeekHit       119 /* synopsis: seekHit=P2                       */
 14938  14967   #define OP_Sequence      120 /* synopsis: r[P2]=cursor[P1].ctr++           */
 14939  14968   #define OP_NewRowid      121 /* synopsis: r[P2]=rowid                      */
 14940  14969   #define OP_Insert        122 /* synopsis: intkey=r[P3] data=r[P2]          */
 14941         -#define OP_InsertInt     123 /* synopsis: intkey=P3 data=r[P2]             */
 14942         -#define OP_Delete        124
 14943         -#define OP_ResetCount    125
 14944         -#define OP_SorterCompare 126 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 14945         -#define OP_SorterData    127 /* synopsis: r[P2]=data                       */
 14946         -#define OP_RowData       128 /* synopsis: r[P2]=data                       */
 14947         -#define OP_Rowid         129 /* synopsis: r[P2]=rowid                      */
 14948         -#define OP_NullRow       130
 14949         -#define OP_SeekEnd       131
 14950         -#define OP_SorterInsert  132 /* synopsis: key=r[P2]                        */
 14951         -#define OP_IdxInsert     133 /* synopsis: key=r[P2]                        */
 14952         -#define OP_IdxDelete     134 /* synopsis: key=r[P2@P3]                     */
 14953         -#define OP_DeferredSeek  135 /* synopsis: Move P3 to P1.rowid if needed    */
 14954         -#define OP_IdxRowid      136 /* synopsis: r[P2]=rowid                      */
 14955         -#define OP_Destroy       137
 14956         -#define OP_Clear         138
 14957         -#define OP_ResetSorter   139
 14958         -#define OP_CreateBtree   140 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 14959         -#define OP_Real          141 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 14960         -#define OP_SqlExec       142
 14961         -#define OP_ParseSchema   143
 14962         -#define OP_LoadAnalysis  144
 14963         -#define OP_DropTable     145
 14964         -#define OP_DropIndex     146
 14965         -#define OP_DropTrigger   147
 14966         -#define OP_IntegrityCk   148
 14967         -#define OP_RowSetAdd     149 /* synopsis: rowset(P1)=r[P2]                 */
 14968         -#define OP_Param         150
 14969         -#define OP_FkCounter     151 /* synopsis: fkctr[P1]+=P2                    */
 14970         -#define OP_MemMax        152 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 14971         -#define OP_OffsetLimit   153 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 14972         -#define OP_AggInverse    154 /* synopsis: accum=r[P3] inverse(r[P2@P5])    */
 14973         -#define OP_AggStep       155 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 14974         -#define OP_AggStep1      156 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 14975         -#define OP_AggValue      157 /* synopsis: r[P3]=value N=P2                 */
 14976         -#define OP_AggFinal      158 /* synopsis: accum=r[P1] N=P2                 */
 14977         -#define OP_Expire        159
 14978         -#define OP_TableLock     160 /* synopsis: iDb=P1 root=P2 write=P3          */
 14979         -#define OP_VBegin        161
 14980         -#define OP_VCreate       162
 14981         -#define OP_VDestroy      163
 14982         -#define OP_VOpen         164
 14983         -#define OP_VColumn       165 /* synopsis: r[P3]=vcolumn(P2)                */
 14984         -#define OP_VRename       166
 14985         -#define OP_Pagecount     167
 14986         -#define OP_MaxPgcnt      168
 14987         -#define OP_Trace         169
 14988         -#define OP_CursorHint    170
 14989         -#define OP_Noop          171
 14990         -#define OP_Explain       172
 14991         -#define OP_Abortable     173
        14970  +#define OP_Delete        123
        14971  +#define OP_ResetCount    124
        14972  +#define OP_SorterCompare 125 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
        14973  +#define OP_SorterData    126 /* synopsis: r[P2]=data                       */
        14974  +#define OP_RowData       127 /* synopsis: r[P2]=data                       */
        14975  +#define OP_Rowid         128 /* synopsis: r[P2]=rowid                      */
        14976  +#define OP_NullRow       129
        14977  +#define OP_SeekEnd       130
        14978  +#define OP_SorterInsert  131 /* synopsis: key=r[P2]                        */
        14979  +#define OP_IdxInsert     132 /* synopsis: key=r[P2]                        */
        14980  +#define OP_IdxDelete     133 /* synopsis: key=r[P2@P3]                     */
        14981  +#define OP_DeferredSeek  134 /* synopsis: Move P3 to P1.rowid if needed    */
        14982  +#define OP_IdxRowid      135 /* synopsis: r[P2]=rowid                      */
        14983  +#define OP_Destroy       136
        14984  +#define OP_Clear         137
        14985  +#define OP_ResetSorter   138
        14986  +#define OP_CreateBtree   139 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
        14987  +#define OP_SqlExec       140
        14988  +#define OP_ParseSchema   141
        14989  +#define OP_LoadAnalysis  142
        14990  +#define OP_DropTable     143
        14991  +#define OP_DropIndex     144
        14992  +#define OP_Real          145 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
        14993  +#define OP_DropTrigger   146
        14994  +#define OP_IntegrityCk   147
        14995  +#define OP_RowSetAdd     148 /* synopsis: rowset(P1)=r[P2]                 */
        14996  +#define OP_Param         149
        14997  +#define OP_FkCounter     150 /* synopsis: fkctr[P1]+=P2                    */
        14998  +#define OP_MemMax        151 /* synopsis: r[P1]=max(r[P1],r[P2])           */
        14999  +#define OP_OffsetLimit   152 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
        15000  +#define OP_AggInverse    153 /* synopsis: accum=r[P3] inverse(r[P2@P5])    */
        15001  +#define OP_AggStep       154 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        15002  +#define OP_AggStep1      155 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        15003  +#define OP_AggValue      156 /* synopsis: r[P3]=value N=P2                 */
        15004  +#define OP_AggFinal      157 /* synopsis: accum=r[P1] N=P2                 */
        15005  +#define OP_Expire        158
        15006  +#define OP_TableLock     159 /* synopsis: iDb=P1 root=P2 write=P3          */
        15007  +#define OP_VBegin        160
        15008  +#define OP_VCreate       161
        15009  +#define OP_VDestroy      162
        15010  +#define OP_VOpen         163
        15011  +#define OP_VColumn       164 /* synopsis: r[P3]=vcolumn(P2)                */
        15012  +#define OP_VRename       165
        15013  +#define OP_Pagecount     166
        15014  +#define OP_MaxPgcnt      167
        15015  +#define OP_Trace         168
        15016  +#define OP_CursorHint    169
        15017  +#define OP_Noop          170
        15018  +#define OP_Explain       171
        15019  +#define OP_Abortable     172
 14992  15020   
 14993  15021   /* Properties such as "out2" or "jump" that are specified in
 14994  15022   ** comments following the "case" for each opcode in the vdbe.c
 14995  15023   ** are encoded into bitvectors as follows:
 14996  15024   */
 14997  15025   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 14998  15026   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 15008  15036   /*  32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
 15009  15037   /*  40 */ 0x01, 0x23, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03,\
 15010  15038   /*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 15011  15039   /*  56 */ 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,\
 15012  15040   /*  64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\
 15013  15041   /*  72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
 15014  15042   /*  80 */ 0x10, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
 15015         -/*  88 */ 0x12, 0x20, 0x00, 0x00, 0x26, 0x26, 0x26, 0x26,\
 15016         -/*  96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 15017         -/* 104 */ 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
        15043  +/*  88 */ 0x12, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
        15044  +/*  96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
        15045  +/* 104 */ 0x26, 0x26, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00,\
 15018  15046   /* 112 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 15019  15047   /* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 15020         -/* 128 */ 0x00, 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00,\
 15021         -/* 136 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 15022         -/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00,\
 15023         -/* 152 */ 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 15024         -/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
 15025         -/* 168 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,}
        15048  +/* 128 */ 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\
        15049  +/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
        15050  +/* 144 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
        15051  +/* 152 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
        15052  +/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
        15053  +/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00,}
 15026  15054   
 15027  15055   /* The sqlite3P2Values() routine is able to run faster if it knows
 15028  15056   ** the value of the largest JUMP opcode.  The smaller the maximum
 15029  15057   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 15030  15058   ** generated this include file strives to group all JUMP opcodes
 15031  15059   ** together near the beginning of the list.
 15032  15060   */
................................................................................
 16323  16351   #endif
 16324  16352     void *pCommitArg;                 /* Argument to xCommitCallback() */
 16325  16353     int (*xCommitCallback)(void*);    /* Invoked at every commit. */
 16326  16354     void *pRollbackArg;               /* Argument to xRollbackCallback() */
 16327  16355     void (*xRollbackCallback)(void*); /* Invoked at every commit. */
 16328  16356     void *pUpdateArg;
 16329  16357     void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
        16358  +  Parse *pParse;                /* Current parse */
 16330  16359   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 16331  16360     void *pPreUpdateArg;          /* First argument to xPreUpdateCallback */
 16332  16361     void (*xPreUpdateCallback)(   /* Registered using sqlite3_preupdate_hook() */
 16333  16362       void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
 16334  16363     );
 16335  16364     PreUpdate *pPreUpdate;        /* Context for active pre-update callback */
 16336  16365   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
................................................................................
 16456  16485   
 16457  16486   /*
 16458  16487   ** Allowed values for sqlite3.mDbFlags
 16459  16488   */
 16460  16489   #define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
 16461  16490   #define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
 16462  16491   #define DBFLAG_Vacuum         0x0004  /* Currently in a VACUUM */
 16463         -#define DBFLAG_SchemaKnownOk  0x0008  /* Schema is known to be valid */
        16492  +#define DBFLAG_VacuumInto     0x0008  /* Currently running VACUUM INTO */
        16493  +#define DBFLAG_SchemaKnownOk  0x0010  /* Schema is known to be valid */
 16464  16494   
 16465  16495   /*
 16466  16496   ** Bits of the sqlite3.dbOptFlags field that are used by the
 16467  16497   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
 16468  16498   ** selectively disable various optimizations.
 16469  16499   */
 16470  16500   #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
 16471         -                          /*  0x0002   available for reuse */
        16501  +#define SQLITE_WindowFunc     0x0002   /* Use xInverse for window functions */
 16472  16502   #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
 16473  16503   #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
 16474  16504   #define SQLITE_DistinctOpt    0x0010   /* DISTINCT using indexes */
 16475  16505   #define SQLITE_CoverIdxScan   0x0020   /* Covering index scans */
 16476  16506   #define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
 16477  16507   #define SQLITE_Transitive     0x0080   /* Transitive constraints */
 16478  16508   #define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
................................................................................
 16582  16612   #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
 16583  16613   #define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
 16584  16614   #define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
 16585  16615                                       ** single query - might change over time */
 16586  16616   #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
 16587  16617   #define SQLITE_FUNC_OFFSET   0x8000 /* Built-in sqlite_offset() function */
 16588  16618   #define SQLITE_FUNC_WINDOW   0x00010000 /* Built-in window-only function */
 16589         -#define SQLITE_FUNC_WINDOW_SIZE 0x20000 /* Requires partition size as arg. */
 16590  16619   #define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
 16591  16620   
 16592  16621   /*
 16593  16622   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 16594  16623   ** used to create the initializers for the FuncDef structures.
 16595  16624   **
 16596  16625   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
................................................................................
 17388  17417         int regReturn;         /* Register used to hold return address */
 17389  17418       } sub;
 17390  17419     } y;
 17391  17420   };
 17392  17421   
 17393  17422   /*
 17394  17423   ** The following are the meanings of bits in the Expr.flags field.
        17424  +** Value restrictions:
        17425  +**
        17426  +**          EP_Agg == NC_HasAgg == SF_HasAgg
        17427  +**          EP_Win == NC_HasWin
 17395  17428   */
 17396  17429   #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
 17397         -#define EP_Agg       0x000002 /* Contains one or more aggregate functions */
        17430  +#define EP_Distinct  0x000002 /* Aggregate function with DISTINCT keyword */
 17398  17431   #define EP_HasFunc   0x000004 /* Contains one or more functions of any kind */
 17399  17432   #define EP_FixedCol  0x000008 /* TK_Column with a known fixed value */
 17400         -#define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
        17433  +#define EP_Agg       0x000010 /* Contains one or more aggregate functions */
 17401  17434   #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
 17402  17435   #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
 17403  17436   #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
 17404  17437   #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
 17405  17438   #define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
 17406  17439   #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
 17407  17440   #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
 17408  17441   #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
 17409  17442   #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
 17410  17443   #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
 17411         -#define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
        17444  +#define EP_Win       0x008000 /* Contains window functions */
 17412  17445   #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
 17413  17446   #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
 17414  17447   #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
 17415  17448   #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
 17416  17449   #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
 17417  17450   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
 17418  17451   #define EP_Alias     0x400000 /* Is an alias for a result set column */
 17419  17452   #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
 17420  17453   #define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
 17421  17454   #define EP_Subrtn   0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
 17422  17455   #define EP_Quoted   0x4000000 /* TK_ID was originally quoted */
        17456  +#define EP_Static   0x8000000 /* Held in memory not obtained from malloc() */
 17423  17457   
 17424  17458   /*
 17425  17459   ** The EP_Propagate mask is a set of properties that automatically propagate
 17426  17460   ** upwards into parent nodes.
 17427  17461   */
 17428  17462   #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
 17429  17463   
................................................................................
 17655  17689     Select *pWinSelect;  /* SELECT statement for any window functions */
 17656  17690   };
 17657  17691   
 17658  17692   /*
 17659  17693   ** Allowed values for the NameContext, ncFlags field.
 17660  17694   **
 17661  17695   ** Value constraints (all checked via assert()):
 17662         -**    NC_HasAgg    == SF_HasAgg
        17696  +**    NC_HasAgg    == SF_HasAgg    == EP_Agg
 17663  17697   **    NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
        17698  +**    NC_HasWin    == EP_Win
 17664  17699   **
 17665  17700   */
 17666  17701   #define NC_AllowAgg  0x0001  /* Aggregate functions are allowed here */
 17667  17702   #define NC_PartIdx   0x0002  /* True if resolving a partial index WHERE */
 17668  17703   #define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
 17669  17704   #define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
 17670  17705   #define NC_HasAgg    0x0010  /* One or more aggregate functions seen */
................................................................................
 17672  17707   #define NC_VarSelect 0x0040  /* A correlated subquery has been seen */
 17673  17708   #define NC_UEList    0x0080  /* True if uNC.pEList is used */
 17674  17709   #define NC_UAggInfo  0x0100  /* True if uNC.pAggInfo is used */
 17675  17710   #define NC_UUpsert   0x0200  /* True if uNC.pUpsert is used */
 17676  17711   #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
 17677  17712   #define NC_Complex   0x2000  /* True if a function or subquery seen */
 17678  17713   #define NC_AllowWin  0x4000  /* Window functions are allowed here */
        17714  +#define NC_HasWin    0x8000  /* One or more window functions seen */
 17679  17715   
 17680  17716   /*
 17681  17717   ** An instance of the following object describes a single ON CONFLICT
 17682  17718   ** clause in an upsert.
 17683  17719   **
 17684  17720   ** The pUpsertTarget field is only set if the ON CONFLICT clause includes
 17685  17721   ** conflict-target clause.  (In "ON CONFLICT(a,b)" the "(a,b)" is the
................................................................................
 17986  18022   #ifndef SQLITE_OMIT_SHARED_CACHE
 17987  18023     int nTableLock;        /* Number of locks in aTableLock */
 17988  18024     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 17989  18025   #endif
 17990  18026     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 17991  18027     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
 17992  18028     Table *pTriggerTab;  /* Table triggers are being coded for */
        18029  +  Parse *pParentParse; /* Parent parser if this parser is nested */
 17993  18030     int addrCrTab;       /* Address of OP_CreateBtree opcode on CREATE TABLE */
 17994  18031     u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
 17995  18032     u32 oldmask;         /* Mask of old.* columns referenced */
 17996  18033     u32 newmask;         /* Mask of new.* columns referenced */
 17997  18034     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
 17998  18035     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
 17999  18036     u8 disableTriggers;  /* True to disable triggers */
................................................................................
 18426  18463   struct TreeView {
 18427  18464     int iLevel;             /* Which level of the tree we are on */
 18428  18465     u8  bLine[100];         /* Draw vertical in column i if bLine[i] is true */
 18429  18466   };
 18430  18467   #endif /* SQLITE_DEBUG */
 18431  18468   
 18432  18469   /*
 18433         -** This object is used in varioius ways, all related to window functions
        18470  +** This object is used in various ways, all related to window functions
 18434  18471   **
 18435  18472   **   (1) A single instance of this structure is attached to the
 18436  18473   **       the Expr.pWin field for each window function in an expression tree.
 18437  18474   **       This object holds the information contained in the OVER clause,
 18438  18475   **       plus additional fields used during code generation.
 18439  18476   **
 18440  18477   **   (2) All window functions in a single SELECT form a linked-list
................................................................................
 18441  18478   **       attached to Select.pWin.  The Window.pFunc and Window.pExpr
 18442  18479   **       fields point back to the expression that is the window function.
 18443  18480   **
 18444  18481   **   (3) The terms of the WINDOW clause of a SELECT are instances of this
 18445  18482   **       object on a linked list attached to Select.pWinDefn.
 18446  18483   **
 18447  18484   ** The uses (1) and (2) are really the same Window object that just happens
 18448         -** to be accessible in two different ways.  Use (3) is are separate objects.
        18485  +** to be accessible in two different ways.  Use case (3) are separate objects.
 18449  18486   */
 18450  18487   struct Window {
 18451  18488     char *zName;            /* Name of window (may be NULL) */
        18489  +  char *zBase;            /* Name of base window for chaining (may be NULL) */
 18452  18490     ExprList *pPartition;   /* PARTITION BY clause */
 18453  18491     ExprList *pOrderBy;     /* ORDER BY clause */
 18454         -  u8 eType;               /* TK_RANGE or TK_ROWS */
        18492  +  u8 eFrmType;            /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
 18455  18493     u8 eStart;              /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
 18456  18494     u8 eEnd;                /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
        18495  +  u8 bImplicitFrame;      /* True if frame was implicitly specified */
        18496  +  u8 eExclude;            /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
 18457  18497     Expr *pStart;           /* Expression for "<expr> PRECEDING" */
 18458  18498     Expr *pEnd;             /* Expression for "<expr> FOLLOWING" */
 18459  18499     Window *pNextWin;       /* Next window function belonging to this SELECT */
 18460  18500     Expr *pFilter;          /* The FILTER expression */
 18461  18501     FuncDef *pFunc;         /* The function */
 18462  18502     int iEphCsr;            /* Partition buffer or Peer buffer */
 18463  18503     int regAccum;
 18464  18504     int regResult;
 18465  18505     int csrApp;             /* Function cursor (used by min/max) */
 18466  18506     int regApp;             /* Function register (also used by min/max) */
 18467         -  int regPart;            /* First in a set of registers holding PARTITION BY
 18468         -                          ** and ORDER BY values for the window */
        18507  +  int regPart;            /* Array of registers for PARTITION BY values */
 18469  18508     Expr *pOwner;           /* Expression object this window is attached to */
 18470  18509     int nBufferCol;         /* Number of columns in buffer table */
 18471  18510     int iArgCol;            /* Offset of first argument for this function */
        18511  +  int regOne;             /* Register containing constant value 1 */
        18512  +  int regStartRowid;
        18513  +  int regEndRowid;
 18472  18514   };
 18473  18515   
 18474  18516   #ifndef SQLITE_OMIT_WINDOWFUNC
 18475  18517   SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
 18476  18518   SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
 18477         -SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*);
        18519  +SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
 18478  18520   SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
 18479  18521   SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*);
 18480  18522   SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Window*);
 18481  18523   SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
 18482  18524   SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
 18483  18525   SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
 18484  18526   SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
 18485  18527   SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
 18486  18528   SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
 18487  18529   SQLITE_PRIVATE void sqlite3WindowFunctions(void);
        18530  +SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
        18531  +SQLITE_PRIVATE Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
 18488  18532   #else
 18489  18533   # define sqlite3WindowDelete(a,b)
 18490  18534   # define sqlite3WindowFunctions()
 18491  18535   # define sqlite3WindowAttach(a,b,c)
 18492  18536   #endif
 18493  18537   
 18494  18538   /*
................................................................................
 19171  19215   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 19172  19216   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 19173  19217   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 19174  19218   SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
 19175  19219   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 19176  19220   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 19177  19221   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
 19178         -SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int, int);
        19222  +SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
 19179  19223   SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
 19180  19224   SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 19181  19225   SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
 19182  19226   SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
 19183  19227   SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
 19184  19228   SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
 19185  19229   SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
................................................................................
 20133  20177   */
 20134  20178   #define MEM_Null      0x0001   /* Value is NULL (or a pointer) */
 20135  20179   #define MEM_Str       0x0002   /* Value is a string */
 20136  20180   #define MEM_Int       0x0004   /* Value is an integer */
 20137  20181   #define MEM_Real      0x0008   /* Value is a real number */
 20138  20182   #define MEM_Blob      0x0010   /* Value is a BLOB */
 20139  20183   #define MEM_AffMask   0x001f   /* Mask of affinity bits */
 20140         -/* Available          0x0020   */
        20184  +#define MEM_FromBind  0x0020   /* Value originates from sqlite3_bind() */
 20141  20185   /* Available          0x0040   */
 20142  20186   #define MEM_Undefined 0x0080   /* Value is undefined */
 20143  20187   #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
 20144         -#define MEM_TypeMask  0xc1ff   /* Mask of type bits */
        20188  +#define MEM_TypeMask  0xc1df   /* Mask of type bits */
 20145  20189   
 20146  20190   
 20147  20191   /* Whenever Mem contains a valid string or blob representation, one of
 20148  20192   ** the following flags must be set to determine the memory management
 20149  20193   ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
 20150  20194   ** string is \000 or \u0000 terminated
 20151  20195   */
................................................................................
 20169  20213   
 20170  20214   /*
 20171  20215   ** Clear any existing type flags from a Mem and replace them with f
 20172  20216   */
 20173  20217   #define MemSetTypeFlag(p, f) \
 20174  20218      ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
 20175  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  +
 20176  20226   /*
 20177  20227   ** Return true if a memory cell is not marked as invalid.  This macro
 20178  20228   ** is for use inside assert() statements only.
 20179  20229   */
 20180  20230   #ifdef SQLITE_DEBUG
 20181  20231   #define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
 20182  20232   #endif
................................................................................
 27118  27168   SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
 27119  27169     if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
 27120  27170       db->mallocFailed = 1;
 27121  27171       if( db->nVdbeExec>0 ){
 27122  27172         db->u1.isInterrupted = 1;
 27123  27173       }
 27124  27174       db->lookaside.bDisable++;
        27175  +    if( db->pParse ){
        27176  +      db->pParse->rc = SQLITE_NOMEM_BKPT;
        27177  +    }
 27125  27178     }
 27126  27179   }
 27127  27180   
 27128  27181   /*
 27129  27182   ** This routine reactivates the memory allocator and clears the
 27130  27183   ** db->mallocFailed flag as necessary.
 27131  27184   **
................................................................................
 27311  27364   
 27312  27365   /*
 27313  27366   ** Set the StrAccum object to an error mode.
 27314  27367   */
 27315  27368   static void setStrAccumError(StrAccum *p, u8 eError){
 27316  27369     assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
 27317  27370     p->accError = eError;
 27318         -  p->nAlloc = 0;
        27371  +  if( p->mxAlloc ) sqlite3_str_reset(p);
 27319  27372   }
 27320  27373   
 27321  27374   /*
 27322  27375   ** Extra argument values from a PrintfArguments object
 27323  27376   */
 27324  27377   static sqlite3_int64 getIntArg(PrintfArguments *p){
 27325  27378     if( p->nArg<=p->nUsed ) return 0;
................................................................................
 27341  27394   ** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
 27342  27395   ** Do the size check before the memory allocation to prevent rogue
 27343  27396   ** SQL from requesting large allocations using the precision or width
 27344  27397   ** field of the printf() function.
 27345  27398   */
 27346  27399   static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
 27347  27400     char *z;
        27401  +  if( pAccum->accError ) return 0;
 27348  27402     if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
 27349  27403       setStrAccumError(pAccum, SQLITE_TOOBIG);
 27350  27404       return 0;
 27351  27405     }
 27352  27406     z = sqlite3DbMallocRaw(pAccum->db, n);
 27353  27407     if( z==0 ){
 27354  27408       setStrAccumError(pAccum, SQLITE_NOMEM);
................................................................................
 28060  28114     assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
 28061  28115     if( p->accError ){
 28062  28116       testcase(p->accError==SQLITE_TOOBIG);
 28063  28117       testcase(p->accError==SQLITE_NOMEM);
 28064  28118       return 0;
 28065  28119     }
 28066  28120     if( p->mxAlloc==0 ){
 28067         -    N = p->nAlloc - p->nChar - 1;
 28068  28121       setStrAccumError(p, SQLITE_TOOBIG);
 28069         -    return N;
        28122  +    return p->nAlloc - p->nChar - 1;
 28070  28123     }else{
 28071  28124       char *zOld = isMalloced(p) ? p->zText : 0;
 28072  28125       i64 szNew = p->nChar;
 28073  28126       szNew += N + 1;
 28074  28127       if( szNew+p->nChar<=p->mxAlloc ){
 28075  28128         /* Force exponential buffer size growth as long as it does not overflow,
 28076  28129         ** to avoid having to call this routine too often */
................................................................................
 28134  28187   ** Append N bytes of text from z to the StrAccum object.  Increase the
 28135  28188   ** size of the memory allocation for StrAccum if necessary.
 28136  28189   */
 28137  28190   SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
 28138  28191     assert( z!=0 || N==0 );
 28139  28192     assert( p->zText!=0 || p->nChar==0 || p->accError );
 28140  28193     assert( N>=0 );
 28141         -  assert( p->accError==0 || p->nAlloc==0 );
        28194  +  assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
 28142  28195     if( p->nChar+N >= p->nAlloc ){
 28143  28196       enlargeAndAppend(p,z,N);
 28144  28197     }else if( N ){
 28145  28198       assert( p->zText );
 28146  28199       p->nChar += N;
 28147  28200       memcpy(&p->zText[p->nChar-N], z, N);
 28148  28201     }
................................................................................
 28767  28820   #endif /* SQLITE_OMIT_WINDOWFUNC */
 28768  28821   
 28769  28822   #ifndef SQLITE_OMIT_WINDOWFUNC
 28770  28823   /*
 28771  28824   ** Generate a human-readable explanation for a Window object
 28772  28825   */
 28773  28826   SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
        28827  +  int nElement = 0;
        28828  +  if( pWin->pFilter ){
        28829  +    sqlite3TreeViewItem(pView, "FILTER", 1);
        28830  +    sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
        28831  +    sqlite3TreeViewPop(pView);
        28832  +  }
 28774  28833     pView = sqlite3TreeViewPush(pView, more);
 28775  28834     if( pWin->zName ){
 28776         -    sqlite3TreeViewLine(pView, "OVER %s", pWin->zName);
        28835  +    sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
 28777  28836     }else{
 28778         -    sqlite3TreeViewLine(pView, "OVER");
        28837  +    sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
        28838  +  }
        28839  +  if( pWin->zBase )    nElement++;
        28840  +  if( pWin->pOrderBy ) nElement++;
        28841  +  if( pWin->eFrmType ) nElement++;
        28842  +  if( pWin->eExclude ) nElement++;
        28843  +  if( pWin->zBase ){
        28844  +    sqlite3TreeViewPush(pView, (--nElement)>0);
        28845  +    sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
        28846  +    sqlite3TreeViewPop(pView);
 28779  28847     }
 28780  28848     if( pWin->pPartition ){
 28781         -    sqlite3TreeViewExprList(pView, pWin->pPartition, 1, "PARTITION-BY");
        28849  +    sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
 28782  28850     }
 28783  28851     if( pWin->pOrderBy ){
 28784         -    sqlite3TreeViewExprList(pView, pWin->pOrderBy, 1, "ORDER-BY");
        28852  +    sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
 28785  28853     }
 28786         -  if( pWin->eType ){
 28787         -    sqlite3TreeViewItem(pView, pWin->eType==TK_RANGE ? "RANGE" : "ROWS", 0);
        28854  +  if( pWin->eFrmType ){
        28855  +    char zBuf[30];
        28856  +    const char *zFrmType = "ROWS";
        28857  +    if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
        28858  +    if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
        28859  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
        28860  +        pWin->bImplicitFrame ? " (implied)" : "");
        28861  +    sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
 28788  28862       sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
 28789  28863       sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
 28790  28864       sqlite3TreeViewPop(pView);
 28791  28865     }
        28866  +  if( pWin->eExclude ){
        28867  +    char zBuf[30];
        28868  +    const char *zExclude;
        28869  +    switch( pWin->eExclude ){
        28870  +      case TK_NO:      zExclude = "NO OTHERS";   break;
        28871  +      case TK_CURRENT: zExclude = "CURRENT ROW"; break;
        28872  +      case TK_GROUP:   zExclude = "GROUP";       break;
        28873  +      case TK_TIES:    zExclude = "TIES";        break;
        28874  +      default:
        28875  +        sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
        28876  +        zExclude = zBuf;
        28877  +        break;
        28878  +    }
        28879  +    sqlite3TreeViewPush(pView, 0);
        28880  +    sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
        28881  +    sqlite3TreeViewPop(pView);
        28882  +  }
 28792  28883     sqlite3TreeViewPop(pView);
 28793  28884   }
 28794  28885   #endif /* SQLITE_OMIT_WINDOWFUNC */
 28795  28886   
 28796  28887   #ifndef SQLITE_OMIT_WINDOWFUNC
 28797  28888   /*
 28798  28889   ** Generate a human-readable explanation for a Window Function object
................................................................................
 30131  30222   SQLITE_PRIVATE void sqlite3Coverage(int x){
 30132  30223     static unsigned dummy = 0;
 30133  30224     dummy += (unsigned)x;
 30134  30225   }
 30135  30226   #endif
 30136  30227   
 30137  30228   /*
 30138         -** Give a callback to the test harness that can be used to simulate faults
 30139         -** in places where it is difficult or expensive to do so purely by means
 30140         -** of inputs.
        30229  +** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
        30230  +** or to bypass normal error detection during testing in order to let 
        30231  +** execute proceed futher downstream.
 30141  30232   **
 30142         -** The intent of the integer argument is to let the fault simulator know
 30143         -** which of multiple sqlite3FaultSim() calls has been hit.
        30233  +** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0).  The
        30234  +** sqlite3FaultSim() function only returns non-zero during testing.
 30144  30235   **
 30145         -** Return whatever integer value the test callback returns, or return
 30146         -** SQLITE_OK if no test callback is installed.
        30236  +** During testing, if the test harness has set a fault-sim callback using
        30237  +** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
        30238  +** each call to sqlite3FaultSim() is relayed to that application-supplied
        30239  +** callback and the integer return value form the application-supplied
        30240  +** callback is returned by sqlite3FaultSim().
        30241  +**
        30242  +** The integer argument to sqlite3FaultSim() is a code to identify which
        30243  +** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
        30244  +** should have a unique code.  To prevent legacy testing applications from
        30245  +** breaking, the codes should not be changed or reused.
 30147  30246   */
 30148  30247   #ifndef SQLITE_UNTESTABLE
 30149  30248   SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
 30150  30249     int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
 30151  30250     return xCallback ? xCallback(iTest) : SQLITE_OK;
 30152  30251   }
 30153  30252   #endif
................................................................................
 32105  32204       /*  85 */ "Cast"             OpHelp("affinity(r[P1])"),
 32106  32205       /*  86 */ "Permutation"      OpHelp(""),
 32107  32206       /*  87 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 32108  32207       /*  88 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
 32109  32208       /*  89 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
 32110  32209       /*  90 */ "Column"           OpHelp("r[P3]=PX"),
 32111  32210       /*  91 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 32112         -    /*  92 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 32113         -    /*  93 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 32114         -    /*  94 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 32115         -    /*  95 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 32116         -    /*  96 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 32117         -    /*  97 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 32118         -    /*  98 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 32119         -    /*  99 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 32120         -    /* 100 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 32121         -    /* 101 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 32122         -    /* 102 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 32123         -    /* 103 */ "BitNot"           OpHelp("r[P2]= ~r[P1]"),
 32124         -    /* 104 */ "Count"            OpHelp("r[P2]=count()"),
 32125         -    /* 105 */ "ReadCookie"       OpHelp(""),
 32126         -    /* 106 */ "String8"          OpHelp("r[P2]='P4'"),
 32127         -    /* 107 */ "SetCookie"        OpHelp(""),
 32128         -    /* 108 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 32129         -    /* 109 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 32130         -    /* 110 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        32211  +    /*  92 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        32212  +    /*  93 */ "Count"            OpHelp("r[P2]=count()"),
        32213  +    /*  94 */ "ReadCookie"       OpHelp(""),
        32214  +    /*  95 */ "SetCookie"        OpHelp(""),
        32215  +    /*  96 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
        32216  +    /*  97 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
        32217  +    /*  98 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
        32218  +    /*  99 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
        32219  +    /* 100 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
        32220  +    /* 101 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
        32221  +    /* 102 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
        32222  +    /* 103 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
        32223  +    /* 104 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
        32224  +    /* 105 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
        32225  +    /* 106 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        32226  +    /* 107 */ "BitNot"           OpHelp("r[P2]= ~r[P1]"),
        32227  +    /* 108 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        32228  +    /* 109 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        32229  +    /* 110 */ "String8"          OpHelp("r[P2]='P4'"),
 32131  32230       /* 111 */ "OpenDup"          OpHelp(""),
 32132  32231       /* 112 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 32133  32232       /* 113 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 32134  32233       /* 114 */ "SorterOpen"       OpHelp(""),
 32135  32234       /* 115 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 32136  32235       /* 116 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 32137  32236       /* 117 */ "Close"            OpHelp(""),
 32138  32237       /* 118 */ "ColumnsUsed"      OpHelp(""),
 32139  32238       /* 119 */ "SeekHit"          OpHelp("seekHit=P2"),
 32140  32239       /* 120 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 32141  32240       /* 121 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 32142  32241       /* 122 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 32143         -    /* 123 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 32144         -    /* 124 */ "Delete"           OpHelp(""),
 32145         -    /* 125 */ "ResetCount"       OpHelp(""),
 32146         -    /* 126 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 32147         -    /* 127 */ "SorterData"       OpHelp("r[P2]=data"),
 32148         -    /* 128 */ "RowData"          OpHelp("r[P2]=data"),
 32149         -    /* 129 */ "Rowid"            OpHelp("r[P2]=rowid"),
 32150         -    /* 130 */ "NullRow"          OpHelp(""),
 32151         -    /* 131 */ "SeekEnd"          OpHelp(""),
 32152         -    /* 132 */ "SorterInsert"     OpHelp("key=r[P2]"),
 32153         -    /* 133 */ "IdxInsert"        OpHelp("key=r[P2]"),
 32154         -    /* 134 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 32155         -    /* 135 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 32156         -    /* 136 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 32157         -    /* 137 */ "Destroy"          OpHelp(""),
 32158         -    /* 138 */ "Clear"            OpHelp(""),
 32159         -    /* 139 */ "ResetSorter"      OpHelp(""),
 32160         -    /* 140 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 32161         -    /* 141 */ "Real"             OpHelp("r[P2]=P4"),
 32162         -    /* 142 */ "SqlExec"          OpHelp(""),
 32163         -    /* 143 */ "ParseSchema"      OpHelp(""),
 32164         -    /* 144 */ "LoadAnalysis"     OpHelp(""),
 32165         -    /* 145 */ "DropTable"        OpHelp(""),
 32166         -    /* 146 */ "DropIndex"        OpHelp(""),
 32167         -    /* 147 */ "DropTrigger"      OpHelp(""),
 32168         -    /* 148 */ "IntegrityCk"      OpHelp(""),
 32169         -    /* 149 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 32170         -    /* 150 */ "Param"            OpHelp(""),
 32171         -    /* 151 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 32172         -    /* 152 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 32173         -    /* 153 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
 32174         -    /* 154 */ "AggInverse"       OpHelp("accum=r[P3] inverse(r[P2@P5])"),
 32175         -    /* 155 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 32176         -    /* 156 */ "AggStep1"         OpHelp("accum=r[P3] step(r[P2@P5])"),
 32177         -    /* 157 */ "AggValue"         OpHelp("r[P3]=value N=P2"),
 32178         -    /* 158 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 32179         -    /* 159 */ "Expire"           OpHelp(""),
 32180         -    /* 160 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 32181         -    /* 161 */ "VBegin"           OpHelp(""),
 32182         -    /* 162 */ "VCreate"          OpHelp(""),
 32183         -    /* 163 */ "VDestroy"         OpHelp(""),
 32184         -    /* 164 */ "VOpen"            OpHelp(""),
 32185         -    /* 165 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 32186         -    /* 166 */ "VRename"          OpHelp(""),
 32187         -    /* 167 */ "Pagecount"        OpHelp(""),
 32188         -    /* 168 */ "MaxPgcnt"         OpHelp(""),
 32189         -    /* 169 */ "Trace"            OpHelp(""),
 32190         -    /* 170 */ "CursorHint"       OpHelp(""),
 32191         -    /* 171 */ "Noop"             OpHelp(""),
 32192         -    /* 172 */ "Explain"          OpHelp(""),
 32193         -    /* 173 */ "Abortable"        OpHelp(""),
        32242  +    /* 123 */ "Delete"           OpHelp(""),
        32243  +    /* 124 */ "ResetCount"       OpHelp(""),
        32244  +    /* 125 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        32245  +    /* 126 */ "SorterData"       OpHelp("r[P2]=data"),
        32246  +    /* 127 */ "RowData"          OpHelp("r[P2]=data"),
        32247  +    /* 128 */ "Rowid"            OpHelp("r[P2]=rowid"),
        32248  +    /* 129 */ "NullRow"          OpHelp(""),
        32249  +    /* 130 */ "SeekEnd"          OpHelp(""),
        32250  +    /* 131 */ "SorterInsert"     OpHelp("key=r[P2]"),
        32251  +    /* 132 */ "IdxInsert"        OpHelp("key=r[P2]"),
        32252  +    /* 133 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        32253  +    /* 134 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
        32254  +    /* 135 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        32255  +    /* 136 */ "Destroy"          OpHelp(""),
        32256  +    /* 137 */ "Clear"            OpHelp(""),
        32257  +    /* 138 */ "ResetSorter"      OpHelp(""),
        32258  +    /* 139 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
        32259  +    /* 140 */ "SqlExec"          OpHelp(""),
        32260  +    /* 141 */ "ParseSchema"      OpHelp(""),
        32261  +    /* 142 */ "LoadAnalysis"     OpHelp(""),
        32262  +    /* 143 */ "DropTable"        OpHelp(""),
        32263  +    /* 144 */ "DropIndex"        OpHelp(""),
        32264  +    /* 145 */ "Real"             OpHelp("r[P2]=P4"),
        32265  +    /* 146 */ "DropTrigger"      OpHelp(""),
        32266  +    /* 147 */ "IntegrityCk"      OpHelp(""),
        32267  +    /* 148 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        32268  +    /* 149 */ "Param"            OpHelp(""),
        32269  +    /* 150 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        32270  +    /* 151 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        32271  +    /* 152 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
        32272  +    /* 153 */ "AggInverse"       OpHelp("accum=r[P3] inverse(r[P2@P5])"),
        32273  +    /* 154 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
        32274  +    /* 155 */ "AggStep1"         OpHelp("accum=r[P3] step(r[P2@P5])"),
        32275  +    /* 156 */ "AggValue"         OpHelp("r[P3]=value N=P2"),
        32276  +    /* 157 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        32277  +    /* 158 */ "Expire"           OpHelp(""),
        32278  +    /* 159 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        32279  +    /* 160 */ "VBegin"           OpHelp(""),
        32280  +    /* 161 */ "VCreate"          OpHelp(""),
        32281  +    /* 162 */ "VDestroy"         OpHelp(""),
        32282  +    /* 163 */ "VOpen"            OpHelp(""),
        32283  +    /* 164 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        32284  +    /* 165 */ "VRename"          OpHelp(""),
        32285  +    /* 166 */ "Pagecount"        OpHelp(""),
        32286  +    /* 167 */ "MaxPgcnt"         OpHelp(""),
        32287  +    /* 168 */ "Trace"            OpHelp(""),
        32288  +    /* 169 */ "CursorHint"       OpHelp(""),
        32289  +    /* 170 */ "Noop"             OpHelp(""),
        32290  +    /* 171 */ "Explain"          OpHelp(""),
        32291  +    /* 172 */ "Abortable"        OpHelp(""),
 32194  32292     };
 32195  32293     return azName[i];
 32196  32294   }
 32197  32295   #endif
 32198  32296   
 32199  32297   /************** End of opcodes.c *********************************************/
 32200  32298   /************** Begin file os_unix.c *****************************************/
................................................................................
 48618  48716   typedef struct PGroup PGroup;
 48619  48717   
 48620  48718   /*
 48621  48719   ** Each cache entry is represented by an instance of the following 
 48622  48720   ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
 48623  48721   ** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
 48624  48722   ** in memory.
        48723  +**
        48724  +** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
        48725  +** but causes a 2-byte gap in the structure for most architectures (since 
        48726  +** pointers must be either 4 or 8-byte aligned). As this structure is located
        48727  +** in memory directly after the associated page data, if the database is
        48728  +** corrupt, code at the b-tree layer may overread the page buffer and 
        48729  +** read part of this structure before the corruption is detected. This
        48730  +** can cause a valgrind error if the unitialized gap is accessed. Using u16
        48731  +** ensures there is no such gap, and therefore no bytes of unitialized memory
        48732  +** in the structure.
 48625  48733   */
 48626  48734   struct PgHdr1 {
 48627  48735     sqlite3_pcache_page page;      /* Base class. Must be first. pBuf & pExtra */
 48628  48736     unsigned int iKey;             /* Key value (page number) */
 48629         -  u8 isBulkLocal;                /* This page from bulk local storage */
 48630         -  u8 isAnchor;                   /* This is the PGroup.lru element */
        48737  +  u16 isBulkLocal;               /* This page from bulk local storage */
        48738  +  u16 isAnchor;                  /* This is the PGroup.lru element */
 48631  48739     PgHdr1 *pNext;                 /* Next in hash table chain */
 48632  48740     PCache1 *pCache;               /* Cache that currently owns this page */
 48633  48741     PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
 48634  48742     PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
 48635  48743                                    /* NB: pLruPrev is only valid if pLruNext!=0 */
 48636  48744   };
 48637  48745   
................................................................................
 48829  48937       do{
 48830  48938         PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
 48831  48939         pX->page.pBuf = zBulk;
 48832  48940         pX->page.pExtra = &pX[1];
 48833  48941         pX->isBulkLocal = 1;
 48834  48942         pX->isAnchor = 0;
 48835  48943         pX->pNext = pCache->pFree;
        48944  +      pX->pLruPrev = 0;           /* Initializing this saves a valgrind error */
 48836  48945         pCache->pFree = pX;
 48837  48946         zBulk += pCache->szAlloc;
 48838  48947       }while( --nBulk );
 48839  48948     }
 48840  48949     return pCache->pFree!=0;
 48841  48950   }
 48842  48951   
................................................................................
 51290  51399   **   * the database file is open,
 51291  51400   **   * there are no dirty pages in the cache, and
 51292  51401   **   * the desired page is not currently in the wal file.
 51293  51402   */
 51294  51403   SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
 51295  51404     if( pPager->fd->pMethods==0 ) return 0;
 51296  51405     if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
        51406  +#ifdef SQLITE_HAS_CODEC
        51407  +  if( pPager->xCodec!=0 ) return 0;
        51408  +#endif
 51297  51409   #ifndef SQLITE_OMIT_WAL
 51298  51410     if( pPager->pWal ){
 51299  51411       u32 iRead = 0;
 51300  51412       int rc;
 51301  51413       rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
 51302  51414       return (rc==SQLITE_OK && iRead==0);
 51303  51415     }
................................................................................
 54239  54351       char *pNew = NULL;             /* New temp space */
 54240  54352       i64 nByte = 0;
 54241  54353   
 54242  54354       if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
 54243  54355         rc = sqlite3OsFileSize(pPager->fd, &nByte);
 54244  54356       }
 54245  54357       if( rc==SQLITE_OK ){
 54246         -      pNew = (char *)sqlite3PageMalloc(pageSize);
 54247         -      if( !pNew ) rc = SQLITE_NOMEM_BKPT;
        54358  +      /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
        54359  +      * cell header parser will never run off the end of the allocation */
        54360  +      pNew = (char *)sqlite3PageMalloc(pageSize+8);
        54361  +      if( !pNew ){
        54362  +        rc = SQLITE_NOMEM_BKPT;
        54363  +      }else{
        54364  +        memset(pNew+pageSize, 0, 8);
        54365  +      }
 54248  54366       }
 54249  54367   
 54250  54368       if( rc==SQLITE_OK ){
 54251  54369         pager_reset(pPager);
 54252  54370         rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
 54253  54371       }
 54254  54372       if( rc==SQLITE_OK ){
................................................................................
 57621  57739     /* If the cache contains a page with page-number pgno, remove it
 57622  57740     ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
 57623  57741     ** page pgno before the 'move' operation, it needs to be retained 
 57624  57742     ** for the page moved there.
 57625  57743     */
 57626  57744     pPg->flags &= ~PGHDR_NEED_SYNC;
 57627  57745     pPgOld = sqlite3PagerLookup(pPager, pgno);
 57628         -  assert( !pPgOld || pPgOld->nRef==1 );
        57746  +  assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
 57629  57747     if( pPgOld ){
        57748  +    if( pPgOld->nRef>1 ){
        57749  +      sqlite3PagerUnrefNotNull(pPgOld);
        57750  +      return SQLITE_CORRUPT_BKPT;
        57751  +    }
 57630  57752       pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
 57631  57753       if( pPager->tempFile ){
 57632  57754         /* Do not discard pages from an in-memory database since we might
 57633  57755         ** need to rollback later.  Just move the page out of the way. */
 57634  57756         sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
 57635  57757       }else{
 57636  57758         sqlite3PcacheDrop(pPgOld);
................................................................................
 58150  58272   
 58151  58273   /*
 58152  58274   ** Release a lock obtained by an earlier successful call to
 58153  58275   ** sqlite3PagerSnapshotCheck().
 58154  58276   */
 58155  58277   SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
 58156  58278     assert( pPager->pWal );
 58157         -  return sqlite3WalSnapshotUnlock(pPager->pWal);
        58279  +  sqlite3WalSnapshotUnlock(pPager->pWal);
 58158  58280   }
 58159  58281   
 58160  58282   #endif /* SQLITE_ENABLE_SNAPSHOT */
 58161  58283   #endif /* !SQLITE_OMIT_WAL */
 58162  58284   
 58163  58285   #ifdef SQLITE_ENABLE_ZIPVFS
 58164  58286   /*
................................................................................
 59162  59284   ** actually needed.
 59163  59285   */
 59164  59286   static void walCleanupHash(Wal *pWal){
 59165  59287     WalHashLoc sLoc;                /* Hash table location */
 59166  59288     int iLimit = 0;                 /* Zero values greater than this */
 59167  59289     int nByte;                      /* Number of bytes to zero in aPgno[] */
 59168  59290     int i;                          /* Used to iterate through aHash[] */
        59291  +  int rc;                         /* Return code form walHashGet() */
 59169  59292   
 59170  59293     assert( pWal->writeLock );
 59171  59294     testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
 59172  59295     testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
 59173  59296     testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
 59174  59297   
 59175  59298     if( pWal->hdr.mxFrame==0 ) return;
 59176  59299   
 59177  59300     /* Obtain pointers to the hash-table and page-number array containing 
 59178  59301     ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
 59179         -  ** that the page said hash-table and array reside on is already mapped.
        59302  +  ** that the page said hash-table and array reside on is already mapped.(1)
 59180  59303     */
 59181  59304     assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
 59182  59305     assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
 59183         -  walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
        59306  +  rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
        59307  +  if( NEVER(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */
 59184  59308   
 59185  59309     /* Zero all hash-table entries that correspond to frame numbers greater
 59186  59310     ** than pWal->hdr.mxFrame.
 59187  59311     */
 59188  59312     iLimit = pWal->hdr.mxFrame - sLoc.iZero;
 59189  59313     assert( iLimit>0 );
 59190  59314     for(i=0; i<HASHTABLE_NSLOT; i++){
................................................................................
 62326  62450     u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
 62327  62451     u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
 62328  62452     u8 max1bytePayload;  /* min(maxLocal,127) */
 62329  62453     u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
 62330  62454     u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
 62331  62455     u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
 62332  62456     u16 cellOffset;      /* Index in aData of first cell pointer */
 62333         -  u16 nFree;           /* Number of free bytes on the page */
        62457  +  int nFree;           /* Number of free bytes on the page. -1 for unknown */
 62334  62458     u16 nCell;           /* Number of cells on this page, local and ovfl */
 62335  62459     u16 maskPage;        /* Mask for page offset */
 62336  62460     u16 aiOvfl[4];       /* Insert the i-th overflow cell before the aiOvfl-th
 62337  62461                          ** non-overflow cell */
 62338  62462     u8 *apOvfl[4];       /* Pointers to the body of overflow cells */
 62339  62463     BtShared *pBt;       /* Pointer to BtShared that this page is part of */
 62340  62464     u8 *aData;           /* Pointer to disk image of the page data */
................................................................................
 63880  64004   ** when saveCursorPosition() was called. Note that this call deletes the 
 63881  64005   ** saved position info stored by saveCursorPosition(), so there can be
 63882  64006   ** at most one effective restoreCursorPosition() call after each 
 63883  64007   ** saveCursorPosition().
 63884  64008   */
 63885  64009   static int btreeRestoreCursorPosition(BtCursor *pCur){
 63886  64010     int rc;
 63887         -  int skipNext;
        64011  +  int skipNext = 0;
 63888  64012     assert( cursorOwnsBtShared(pCur) );
 63889  64013     assert( pCur->eState>=CURSOR_REQUIRESEEK );
 63890  64014     if( pCur->eState==CURSOR_FAULT ){
 63891  64015       return pCur->skipNext;
 63892  64016     }
 63893  64017     pCur->eState = CURSOR_INVALID;
 63894         -  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
        64018  +  if( sqlite3FaultSim(410) ){
        64019  +    rc = SQLITE_IOERR;
        64020  +  }else{
        64021  +    rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
        64022  +  }
 63895  64023     if( rc==SQLITE_OK ){
 63896  64024       sqlite3_free(pCur->pKey);
 63897  64025       pCur->pKey = 0;
 63898  64026       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
 63899  64027       if( skipNext ) pCur->skipNext = skipNext;
 63900  64028       if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
 63901  64029         pCur->eState = CURSOR_SKIPNEXT;
................................................................................
 64468  64596     assert( pPage->nOverflow==0 );
 64469  64597     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 64470  64598     temp = 0;
 64471  64599     src = data = pPage->aData;
 64472  64600     hdr = pPage->hdrOffset;
 64473  64601     cellOffset = pPage->cellOffset;
 64474  64602     nCell = pPage->nCell;
 64475         -  assert( nCell==get2byte(&data[hdr+3]) );
        64603  +  assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
 64476  64604     iCellFirst = cellOffset + 2*nCell;
 64477  64605     usableSize = pPage->pBt->usableSize;
 64478  64606   
 64479  64607     /* This block handles pages with two or fewer free blocks and nMaxFrag
 64480  64608     ** or fewer fragmented bytes. In this case it is faster to move the
 64481  64609     ** two (or one) blocks of cells using memmove() and add the required
 64482  64610     ** offsets to each pointer in the cell-pointer array than it is to 
 64483  64611     ** reconstruct the entire page.  */
 64484  64612     if( (int)data[hdr+7]<=nMaxFrag ){
 64485  64613       int iFree = get2byte(&data[hdr+1]);
 64486         -
 64487         -    /* If the initial freeblock offset were out of bounds, that would
 64488         -    ** have been detected by btreeInitPage() when it was computing the
 64489         -    ** number of free bytes on the page. */
 64490         -    assert( iFree<=usableSize-4 );
        64614  +    if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
 64491  64615       if( iFree ){
 64492  64616         int iFree2 = get2byte(&data[iFree]);
 64493  64617         if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
 64494  64618         if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
 64495  64619           u8 *pEnd = &data[cellOffset + nCell*2];
 64496  64620           u8 *pAddr;
 64497  64621           int sz2 = 0;
................................................................................
 64502  64626           }
 64503  64627           if( iFree2 ){
 64504  64628             if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
 64505  64629             sz2 = get2byte(&data[iFree2+2]);
 64506  64630             if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
 64507  64631             memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
 64508  64632             sz += sz2;
        64633  +        }else if( iFree+sz>usableSize ){
        64634  +          return SQLITE_CORRUPT_PAGE(pPage);
 64509  64635           }
        64636  +
 64510  64637           cbrk = top+sz;
 64511  64638           assert( cbrk+(iFree-top) <= usableSize );
 64512  64639           memmove(&data[cbrk], &data[top], iFree-top);
 64513  64640           for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
 64514  64641             pc = get2byte(pAddr);
 64515  64642             if( pc<iFree ){ put2byte(pAddr, pc+sz); }
 64516  64643             else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
................................................................................
 64553  64680         src = temp;
 64554  64681       }
 64555  64682       memcpy(&data[cbrk], &src[pc], size);
 64556  64683     }
 64557  64684     data[hdr+7] = 0;
 64558  64685   
 64559  64686    defragment_out:
        64687  +  assert( pPage->nFree>=0 );
 64560  64688     if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
 64561  64689       return SQLITE_CORRUPT_PAGE(pPage);
 64562  64690     }
 64563  64691     assert( cbrk>=iCellFirst );
 64564  64692     put2byte(&data[hdr+5], cbrk);
 64565  64693     data[hdr+1] = 0;
 64566  64694     data[hdr+2] = 0;
................................................................................
 64580  64708   ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
 64581  64709   **
 64582  64710   ** Slots on the free list that are between 1 and 3 bytes larger than nByte
 64583  64711   ** will be ignored if adding the extra space to the fragmentation count
 64584  64712   ** causes the fragmentation count to exceed 60.
 64585  64713   */
 64586  64714   static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
 64587         -  const int hdr = pPg->hdrOffset;
 64588         -  u8 * const aData = pPg->aData;
 64589         -  int iAddr = hdr + 1;
 64590         -  int pc = get2byte(&aData[iAddr]);
 64591         -  int x;
 64592         -  int usableSize = pPg->pBt->usableSize;
 64593         -  int size;            /* Size of the free slot */
        64715  +  const int hdr = pPg->hdrOffset;            /* Offset to page header */
        64716  +  u8 * const aData = pPg->aData;             /* Page data */
        64717  +  int iAddr = hdr + 1;                       /* Address of ptr to pc */
        64718  +  int pc = get2byte(&aData[iAddr]);          /* Address of a free slot */
        64719  +  int x;                                     /* Excess size of the slot */
        64720  +  int maxPC = pPg->pBt->usableSize - nByte;  /* Max address for a usable slot */
        64721  +  int size;                                  /* Size of the free slot */
 64594  64722   
 64595  64723     assert( pc>0 );
 64596         -  while( pc<=usableSize-4 ){
        64724  +  while( pc<=maxPC ){
 64597  64725       /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
 64598  64726       ** freeblock form a big-endian integer which is the size of the freeblock
 64599  64727       ** in bytes, including the 4-byte header. */
 64600  64728       size = get2byte(&aData[pc+2]);
 64601  64729       if( (x = size - nByte)>=0 ){
 64602  64730         testcase( x==4 );
 64603  64731         testcase( x==3 );
 64604         -      if( size+pc > usableSize ){
 64605         -        *pRc = SQLITE_CORRUPT_PAGE(pPg);
 64606         -        return 0;
 64607         -      }else if( x<4 ){
        64732  +      if( x<4 ){
 64608  64733           /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
 64609  64734           ** number of bytes in fragments may not exceed 60. */
 64610  64735           if( aData[hdr+7]>57 ) return 0;
 64611  64736   
 64612  64737           /* Remove the slot from the free-list. Update the number of
 64613  64738           ** fragmented bytes within the page. */
 64614  64739           memcpy(&aData[iAddr], &aData[pc], 2);
 64615  64740           aData[hdr+7] += (u8)x;
        64741  +      }else if( x+pc > maxPC ){
        64742  +        /* This slot extends off the end of the usable part of the page */
        64743  +        *pRc = SQLITE_CORRUPT_PAGE(pPg);
        64744  +        return 0;
 64616  64745         }else{
 64617  64746           /* The slot remains on the free-list. Reduce its size to account
 64618         -         ** for the portion used by the new allocation. */
        64747  +        ** for the portion used by the new allocation. */
 64619  64748           put2byte(&aData[pc+2], x);
 64620  64749         }
 64621  64750         return &aData[pc + x];
 64622  64751       }
 64623  64752       iAddr = pc;
 64624  64753       pc = get2byte(&aData[pc]);
 64625         -    if( pc<iAddr+size ) break;
        64754  +    if( pc<=iAddr+size ){
        64755  +      if( pc ){
        64756  +        /* The next slot in the chain is not past the end of the current slot */
        64757  +        *pRc = SQLITE_CORRUPT_PAGE(pPg);
        64758  +      }
        64759  +      return 0;
        64760  +    }
 64626  64761     }
 64627         -  if( pc ){
        64762  +  if( pc>maxPC+nByte-4 ){
        64763  +    /* The free slot chain extends off the end of the page */
 64628  64764       *pRc = SQLITE_CORRUPT_PAGE(pPg);
 64629  64765     }
 64630         -
 64631  64766     return 0;
 64632  64767   }
 64633  64768   
 64634  64769   /*
 64635  64770   ** Allocate nByte bytes of space from within the B-Tree page passed
 64636  64771   ** as the first argument. Write into *pIdx the index into pPage->aData[]
 64637  64772   ** of the first byte of allocated space. Return either SQLITE_OK or
................................................................................
 64673  64808       if( top==0 && pPage->pBt->usableSize==65536 ){
 64674  64809         top = 65536;
 64675  64810       }else{
 64676  64811         return SQLITE_CORRUPT_PAGE(pPage);
 64677  64812       }
 64678  64813     }
 64679  64814   
 64680         -  /* If there is enough space between gap and top for one more cell pointer
 64681         -  ** array entry offset, and if the freelist is not empty, then search the
 64682         -  ** freelist looking for a free slot big enough to satisfy the request.
        64815  +  /* If there is enough space between gap and top for one more cell pointer,
        64816  +  ** and if the freelist is not empty, then search the
        64817  +  ** freelist looking for a slot big enough to satisfy the request.
 64683  64818     */
 64684  64819     testcase( gap+2==top );
 64685  64820     testcase( gap+1==top );
 64686  64821     testcase( gap==top );
 64687  64822     if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
 64688  64823       u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
 64689  64824       if( pSpace ){
................................................................................
 64697  64832   
 64698  64833     /* The request could not be fulfilled using a freelist slot.  Check
 64699  64834     ** to see if defragmentation is necessary.
 64700  64835     */
 64701  64836     testcase( gap+2+nByte==top );
 64702  64837     if( gap+2+nByte>top ){
 64703  64838       assert( pPage->nCell>0 || CORRUPT_DB );
        64839  +    assert( pPage->nFree>=0 );
 64704  64840       rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
 64705  64841       if( rc ) return rc;
 64706  64842       top = get2byteNotZero(&data[hdr+5]);
 64707  64843       assert( gap+2+nByte<=top );
 64708  64844     }
 64709  64845   
 64710  64846   
 64711  64847     /* Allocate memory from the gap in between the cell pointer array
 64712         -  ** and the cell content area.  The btreeInitPage() call has already
        64848  +  ** and the cell content area.  The btreeComputeFreeSpace() call has already
 64713  64849     ** validated the freelist.  Given that the freelist is valid, there
 64714  64850     ** is no way that the allocation can extend off the end of the page.
 64715  64851     ** The assert() below verifies the previous sentence.
 64716  64852     */
 64717  64853     top -= nByte;
 64718  64854     put2byte(&data[hdr+5], top);
 64719  64855     assert( top+nByte <= (int)pPage->pBt->usableSize );
................................................................................
 64724  64860   /*
 64725  64861   ** Return a section of the pPage->aData to the freelist.
 64726  64862   ** The first byte of the new free block is pPage->aData[iStart]
 64727  64863   ** and the size of the block is iSize bytes.
 64728  64864   **
 64729  64865   ** Adjacent freeblocks are coalesced.
 64730  64866   **
 64731         -** Note that even though the freeblock list was checked by btreeInitPage(),
        64867  +** Even though the freeblock list was checked by btreeComputeFreeSpace(),
 64732  64868   ** that routine will not detect overlap between cells or freeblocks.  Nor
 64733  64869   ** does it detect cells or freeblocks that encrouch into the reserved bytes
 64734  64870   ** at the end of the page.  So do additional corruption checks inside this
 64735  64871   ** routine and return SQLITE_CORRUPT if any problems are found.
 64736  64872   */
 64737  64873   static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
 64738  64874     u16 iPtr;                             /* Address of ptr to next freeblock */
................................................................................
 64886  65022       return SQLITE_CORRUPT_PAGE(pPage);
 64887  65023     }
 64888  65024     pPage->max1bytePayload = pBt->max1bytePayload;
 64889  65025     return SQLITE_OK;
 64890  65026   }
 64891  65027   
 64892  65028   /*
 64893         -** Initialize the auxiliary information for a disk block.
 64894         -**
 64895         -** Return SQLITE_OK on success.  If we see that the page does
 64896         -** not contain a well-formed database page, then return 
 64897         -** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
 64898         -** guarantee that the page is well-formed.  It only shows that
 64899         -** we failed to detect any corruption.
        65029  +** Compute the amount of freespace on the page.  In other words, fill
        65030  +** in the pPage->nFree field.
 64900  65031   */
 64901         -static int btreeInitPage(MemPage *pPage){
        65032  +static int btreeComputeFreeSpace(MemPage *pPage){
 64902  65033     int pc;            /* Address of a freeblock within pPage->aData[] */
 64903  65034     u8 hdr;            /* Offset to beginning of page header */
 64904  65035     u8 *data;          /* Equal to pPage->aData */
 64905         -  BtShared *pBt;        /* The main btree structure */
 64906  65036     int usableSize;    /* Amount of usable space on each page */
 64907         -  u16 cellOffset;    /* Offset from start of page to first cell pointer */
 64908  65037     int nFree;         /* Number of unused bytes on the page */
 64909  65038     int top;           /* First byte of the cell content area */
 64910  65039     int iCellFirst;    /* First allowable cell or freeblock offset */
 64911  65040     int iCellLast;     /* Last possible cell or freeblock offset */
 64912  65041   
 64913  65042     assert( pPage->pBt!=0 );
 64914  65043     assert( pPage->pBt->db!=0 );
 64915  65044     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 64916  65045     assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
 64917  65046     assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
 64918  65047     assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
 64919         -  assert( pPage->isInit==0 );
        65048  +  assert( pPage->isInit==1 );
        65049  +  assert( pPage->nFree<0 );
 64920  65050   
 64921         -  pBt = pPage->pBt;
        65051  +  usableSize = pPage->pBt->usableSize;
 64922  65052     hdr = pPage->hdrOffset;
 64923  65053     data = pPage->aData;
 64924         -  /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
 64925         -  ** the b-tree page type. */
 64926         -  if( decodeFlags(pPage, data[hdr]) ){
 64927         -    return SQLITE_CORRUPT_PAGE(pPage);
 64928         -  }
 64929         -  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 64930         -  pPage->maskPage = (u16)(pBt->pageSize - 1);
 64931         -  pPage->nOverflow = 0;
 64932         -  usableSize = pBt->usableSize;
 64933         -  pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
 64934         -  pPage->aDataEnd = &data[usableSize];
 64935         -  pPage->aCellIdx = &data[cellOffset];
 64936         -  pPage->aDataOfst = &data[pPage->childPtrSize];
 64937  65054     /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
 64938  65055     ** the start of the cell content area. A zero value for this integer is
 64939  65056     ** interpreted as 65536. */
 64940  65057     top = get2byteNotZero(&data[hdr+5]);
 64941         -  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
 64942         -  ** number of cells on the page. */
 64943         -  pPage->nCell = get2byte(&data[hdr+3]);
 64944         -  if( pPage->nCell>MX_CELL(pBt) ){
 64945         -    /* To many cells for a single page.  The page must be corrupt */
 64946         -    return SQLITE_CORRUPT_PAGE(pPage);
 64947         -  }
 64948         -  testcase( pPage->nCell==MX_CELL(pBt) );
 64949         -  /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
 64950         -  ** possible for a root page of a table that contains no rows) then the
 64951         -  ** offset to the cell content area will equal the page size minus the
 64952         -  ** bytes of reserved space. */
 64953         -  assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
 64954         -
 64955         -  /* A malformed database page might cause us to read past the end
 64956         -  ** of page when parsing a cell.  
 64957         -  **
 64958         -  ** The following block of code checks early to see if a cell extends
 64959         -  ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
 64960         -  ** returned if it does.
 64961         -  */
 64962         -  iCellFirst = cellOffset + 2*pPage->nCell;
        65058  +  iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
 64963  65059     iCellLast = usableSize - 4;
 64964         -  if( pBt->db->flags & SQLITE_CellSizeCk ){
 64965         -    int i;            /* Index into the cell pointer array */
 64966         -    int sz;           /* Size of a cell */
 64967         -
 64968         -    if( !pPage->leaf ) iCellLast--;
 64969         -    for(i=0; i<pPage->nCell; i++){
 64970         -      pc = get2byteAligned(&data[cellOffset+i*2]);
 64971         -      testcase( pc==iCellFirst );
 64972         -      testcase( pc==iCellLast );
 64973         -      if( pc<iCellFirst || pc>iCellLast ){
 64974         -        return SQLITE_CORRUPT_PAGE(pPage);
 64975         -      }
 64976         -      sz = pPage->xCellSize(pPage, &data[pc]);
 64977         -      testcase( pc+sz==usableSize );
 64978         -      if( pc+sz>usableSize ){
 64979         -        return SQLITE_CORRUPT_PAGE(pPage);
 64980         -      }
 64981         -    }
 64982         -    if( !pPage->leaf ) iCellLast++;
 64983         -  }  
 64984  65060   
 64985  65061     /* Compute the total free space on the page
 64986  65062     ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
 64987  65063     ** start of the first freeblock on the page, or is zero if there are no
 64988  65064     ** freeblocks. */
 64989  65065     pc = get2byte(&data[hdr+1]);
 64990  65066     nFree = data[hdr+7] + top;  /* Init nFree to non-freeblock free space */
................................................................................
 65024  65100     ** serves to verify that the offset to the start of the cell-content
 65025  65101     ** area, according to the page header, lies within the page.
 65026  65102     */
 65027  65103     if( nFree>usableSize ){
 65028  65104       return SQLITE_CORRUPT_PAGE(pPage);
 65029  65105     }
 65030  65106     pPage->nFree = (u16)(nFree - iCellFirst);
        65107  +  return SQLITE_OK;
        65108  +}
        65109  +
        65110  +/*
        65111  +** Do additional sanity check after btreeInitPage() if
        65112  +** PRAGMA cell_size_check=ON 
        65113  +*/
        65114  +static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
        65115  +  int iCellFirst;    /* First allowable cell or freeblock offset */
        65116  +  int iCellLast;     /* Last possible cell or freeblock offset */
        65117  +  int i;             /* Index into the cell pointer array */
        65118  +  int sz;            /* Size of a cell */
        65119  +  int pc;            /* Address of a freeblock within pPage->aData[] */
        65120  +  u8 *data;          /* Equal to pPage->aData */
        65121  +  int usableSize;    /* Maximum usable space on the page */
        65122  +  int cellOffset;    /* Start of cell content area */
        65123  +
        65124  +  iCellFirst = pPage->cellOffset + 2*pPage->nCell;
        65125  +  usableSize = pPage->pBt->usableSize;
        65126  +  iCellLast = usableSize - 4;
        65127  +  data = pPage->aData;
        65128  +  cellOffset = pPage->cellOffset;
        65129  +  if( !pPage->leaf ) iCellLast--;
        65130  +  for(i=0; i<pPage->nCell; i++){
        65131  +    pc = get2byteAligned(&data[cellOffset+i*2]);
        65132  +    testcase( pc==iCellFirst );
        65133  +    testcase( pc==iCellLast );
        65134  +    if( pc<iCellFirst || pc>iCellLast ){
        65135  +      return SQLITE_CORRUPT_PAGE(pPage);
        65136  +    }
        65137  +    sz = pPage->xCellSize(pPage, &data[pc]);
        65138  +    testcase( pc+sz==usableSize );
        65139  +    if( pc+sz>usableSize ){
        65140  +      return SQLITE_CORRUPT_PAGE(pPage);
        65141  +    }
        65142  +  }
        65143  +  return SQLITE_OK;
        65144  +}
        65145  +
        65146  +/*
        65147  +** Initialize the auxiliary information for a disk block.
        65148  +**
        65149  +** Return SQLITE_OK on success.  If we see that the page does
        65150  +** not contain a well-formed database page, then return 
        65151  +** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
        65152  +** guarantee that the page is well-formed.  It only shows that
        65153  +** we failed to detect any corruption.
        65154  +*/
        65155  +static int btreeInitPage(MemPage *pPage){
        65156  +  u8 *data;          /* Equal to pPage->aData */
        65157  +  BtShared *pBt;        /* The main btree structure */
        65158  +
        65159  +  assert( pPage->pBt!=0 );
        65160  +  assert( pPage->pBt->db!=0 );
        65161  +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
        65162  +  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
        65163  +  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
        65164  +  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
        65165  +  assert( pPage->isInit==0 );
        65166  +
        65167  +  pBt = pPage->pBt;
        65168  +  data = pPage->aData + pPage->hdrOffset;
        65169  +  /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
        65170  +  ** the b-tree page type. */
        65171  +  if( decodeFlags(pPage, data[0]) ){
        65172  +    return SQLITE_CORRUPT_PAGE(pPage);
        65173  +  }
        65174  +  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
        65175  +  pPage->maskPage = (u16)(pBt->pageSize - 1);
        65176  +  pPage->nOverflow = 0;
        65177  +  pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
        65178  +  pPage->aCellIdx = data + pPage->childPtrSize + 8;
        65179  +  pPage->aDataEnd = pPage->aData + pBt->usableSize;
        65180  +  pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
        65181  +  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
        65182  +  ** number of cells on the page. */
        65183  +  pPage->nCell = get2byte(&data[3]);
        65184  +  if( pPage->nCell>MX_CELL(pBt) ){
        65185  +    /* To many cells for a single page.  The page must be corrupt */
        65186  +    return SQLITE_CORRUPT_PAGE(pPage);
        65187  +  }
        65188  +  testcase( pPage->nCell==MX_CELL(pBt) );
        65189  +  /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
        65190  +  ** possible for a root page of a table that contains no rows) then the
        65191  +  ** offset to the cell content area will equal the page size minus the
        65192  +  ** bytes of reserved space. */
        65193  +  assert( pPage->nCell>0
        65194  +       || get2byteNotZero(&data[5])==(int)pBt->usableSize
        65195  +       || CORRUPT_DB );
        65196  +  pPage->nFree = -1;  /* Indicate that this value is yet uncomputed */
 65031  65197     pPage->isInit = 1;
        65198  +  if( pBt->db->flags & SQLITE_CellSizeCk ){
        65199  +    return btreeCellSizeCheck(pPage);
        65200  +  }
 65032  65201     return SQLITE_OK;
 65033  65202   }
 65034  65203   
 65035  65204   /*
 65036  65205   ** Set up a raw page so that it looks like a database page holding
 65037  65206   ** no entries.
 65038  65207   */
................................................................................
 65167  65336     assert( sqlite3_mutex_held(pBt->mutex) );
 65168  65337     assert( pCur==0 || ppPage==&pCur->pPage );
 65169  65338     assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
 65170  65339     assert( pCur==0 || pCur->iPage>0 );
 65171  65340   
 65172  65341     if( pgno>btreePagecount(pBt) ){
 65173  65342       rc = SQLITE_CORRUPT_BKPT;
 65174         -    goto getAndInitPage_error;
        65343  +    goto getAndInitPage_error1;
 65175  65344     }
 65176  65345     rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
 65177  65346     if( rc ){
 65178         -    goto getAndInitPage_error;
        65347  +    goto getAndInitPage_error1;
 65179  65348     }
 65180  65349     *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
 65181  65350     if( (*ppPage)->isInit==0 ){
 65182  65351       btreePageFromDbPage(pDbPage, pgno, pBt);
 65183  65352       rc = btreeInitPage(*ppPage);
 65184  65353       if( rc!=SQLITE_OK ){
 65185         -      releasePage(*ppPage);
 65186         -      goto getAndInitPage_error;
        65354  +      goto getAndInitPage_error2;
 65187  65355       }
 65188  65356     }
 65189  65357     assert( (*ppPage)->pgno==pgno );
 65190  65358     assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
 65191  65359   
 65192  65360     /* If obtaining a child page for a cursor, we must verify that the page is
 65193  65361     ** compatible with the root page. */
 65194  65362     if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
 65195  65363       rc = SQLITE_CORRUPT_PGNO(pgno);
 65196         -    releasePage(*ppPage);
 65197         -    goto getAndInitPage_error;
        65364  +    goto getAndInitPage_error2;
 65198  65365     }
 65199  65366     return SQLITE_OK;
 65200  65367   
 65201         -getAndInitPage_error:
        65368  +getAndInitPage_error2:
        65369  +  releasePage(*ppPage);
        65370  +getAndInitPage_error1:
 65202  65371     if( pCur ){
 65203  65372       pCur->iPage--;
 65204  65373       pCur->pPage = pCur->apPage[pCur->iPage];
 65205  65374     }
 65206  65375     testcase( pgno==0 );
 65207  65376     assert( pgno!=0 || rc==SQLITE_CORRUPT );
 65208  65377     return rc;
................................................................................
 68275  68444       assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
 68276  68445       *pRes = 1;
 68277  68446       rc = SQLITE_OK;
 68278  68447     }
 68279  68448     return rc;
 68280  68449   }
 68281  68450   
 68282         -/*
 68283         -** This function is a no-op if cursor pCur does not point to a valid row.
 68284         -** Otherwise, if pCur is valid, configure it so that the next call to
 68285         -** sqlite3BtreeNext() is a no-op.
 68286         -*/
 68287         -#ifndef SQLITE_OMIT_WINDOWFUNC
 68288         -SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor *pCur){
 68289         -  /* We believe that the cursor must always be in the valid state when
 68290         -  ** this routine is called, but the proof is difficult, so we add an
 68291         -  ** ALWaYS() test just in case we are wrong. */
 68292         -  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
 68293         -    pCur->eState = CURSOR_SKIPNEXT;
 68294         -    pCur->skipNext = 1;
 68295         -  }
 68296         -}
 68297         -#endif /* SQLITE_OMIT_WINDOWFUNC */
 68298         -
 68299  68451   /* Move the cursor to the last entry in the table.  Return SQLITE_OK
 68300  68452   ** on success.  Set *pRes to 0 if the cursor actually points to something
 68301  68453   ** or set *pRes to 1 if the table is empty.
 68302  68454   */
 68303  68455   SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
 68304  68456     int rc;
 68305  68457    
................................................................................
 68557  68709             pCur->ix = (u16)idx;
 68558  68710             rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
 68559  68711             pCur->curFlags &= ~BTCF_ValidOvfl;
 68560  68712             if( rc ){
 68561  68713               sqlite3_free(pCellKey);
 68562  68714               goto moveto_finish;
 68563  68715             }
 68564         -          c = xRecordCompare(nCell, pCellKey, pIdxKey);
        68716  +          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
 68565  68717             sqlite3_free(pCellKey);
 68566  68718           }
 68567  68719           assert( 
 68568  68720               (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
 68569  68721            && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
 68570  68722           );
 68571  68723           if( c<0 ){
................................................................................
 69189  69341   */
 69190  69342   static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
 69191  69343     MemPage *pTrunk = 0;                /* Free-list trunk page */
 69192  69344     Pgno iTrunk = 0;                    /* Page number of free-list trunk page */ 
 69193  69345     MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
 69194  69346     MemPage *pPage;                     /* Page being freed. May be NULL. */
 69195  69347     int rc;                             /* Return Code */
 69196         -  int nFree;                          /* Initial number of pages on free-list */
        69348  +  u32 nFree;                          /* Initial number of pages on free-list */
 69197  69349   
 69198  69350     assert( sqlite3_mutex_held(pBt->mutex) );
 69199  69351     assert( CORRUPT_DB || iPage>1 );
 69200  69352     assert( !pMemPage || pMemPage->pgno==iPage );
 69201  69353   
 69202         -  if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
        69354  +  if( iPage<2 || iPage>pBt->nPage ){
        69355  +    return SQLITE_CORRUPT_BKPT;
        69356  +  }
 69203  69357     if( pMemPage ){
 69204  69358       pPage = pMemPage;
 69205  69359       sqlite3PagerRef(pPage->pDbPage);
 69206  69360     }else{
 69207  69361       pPage = btreePageLookup(pBt, iPage);
 69208  69362     }
 69209  69363   
................................................................................
 69606  69760     int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
 69607  69761   
 69608  69762     if( *pRC ) return;
 69609  69763     assert( idx>=0 && idx<pPage->nCell );
 69610  69764     assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
 69611  69765     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 69612  69766     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
        69767  +  assert( pPage->nFree>=0 );
 69613  69768     data = pPage->aData;
 69614  69769     ptr = &pPage->aCellIdx[2*idx];
 69615  69770     pc = get2byte(ptr);
 69616  69771     hdr = pPage->hdrOffset;
 69617  69772     testcase( pc==get2byte(&data[hdr+5]) );
 69618  69773     testcase( pc+sz==pPage->pBt->usableSize );
 69619  69774     if( pc+sz > pPage->pBt->usableSize ){
................................................................................
 69676  69831     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 69677  69832     /* The cell should normally be sized correctly.  However, when moving a
 69678  69833     ** malformed cell from a leaf page to an interior page, if the cell size
 69679  69834     ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
 69680  69835     ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
 69681  69836     ** the term after the || in the following assert(). */
 69682  69837     assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
        69838  +  assert( pPage->nFree>=0 );
 69683  69839     if( pPage->nOverflow || sz+2>pPage->nFree ){
 69684  69840       if( pTemp ){
 69685  69841         memcpy(pTemp, pCell, sz);
 69686  69842         pCell = pTemp;
 69687  69843       }
 69688  69844       if( iChild ){
 69689  69845         put4byte(pCell, iChild);
................................................................................
 69733  69889       }
 69734  69890       pIns = pPage->aCellIdx + i*2;
 69735  69891       memmove(pIns+2, pIns, 2*(pPage->nCell - i));
 69736  69892       put2byte(pIns, idx);
 69737  69893       pPage->nCell++;
 69738  69894       /* increment the cell count */
 69739  69895       if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
 69740         -    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
        69896  +    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
 69741  69897   #ifndef SQLITE_OMIT_AUTOVACUUM
 69742  69898       if( pPage->pBt->autoVacuum ){
 69743  69899         /* The cell may contain a pointer to an overflow page. If so, write
 69744  69900         ** the entry for the overflow page into the pointer map.
 69745  69901         */
 69746  69902         ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
 69747  69903       }
................................................................................
 69820  69976   **    ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
 69821  69977   **    ixNx[4] = Total number of cells.
 69822  69978   **
 69823  69979   ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
 69824  69980   ** are used and they point to the leaf pages only, and the ixNx value are:
 69825  69981   **
 69826  69982   **    ixNx[0] = Number of cells in Child-1.
 69827         -**    ixNx[1] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
 69828         -**    ixNx[2] = Number of cells in Child-1 and Child-2 + both divider cells
        69983  +**    ixNx[1] = Number of cells in Child-1 and Child-2.
        69984  +**    ixNx[2] = Total number of cells.
        69985  +**
        69986  +** Sometimes when deleting, a child page can have zero cells.  In those
        69987  +** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
        69988  +** entries, shift down.  The end result is that each ixNx[] entry should
        69989  +** be larger than the previous
 69829  69990   */
 69830  69991   typedef struct CellArray CellArray;
 69831  69992   struct CellArray {
 69832  69993     int nCell;              /* Number of cells in apCell[] */
 69833  69994     MemPage *pRef;          /* Reference page */
 69834  69995     u8 **apCell;            /* All cells begin balanced */
 69835  69996     u16 *szCell;            /* Local size of all cells in apCell[] */
................................................................................
 70150  70311     }
 70151  70312   
 70152  70313     /* Add any overflow cells */
 70153  70314     for(i=0; i<pPg->nOverflow; i++){
 70154  70315       int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
 70155  70316       if( iCell>=0 && iCell<nNew ){
 70156  70317         pCellptr = &pPg->aCellIdx[iCell * 2];
 70157         -      assert( nCell>=iCell );
 70158         -      memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
        70318  +      if( nCell>iCell ){
        70319  +        memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
        70320  +      }
 70159  70321         nCell++;
 70160  70322         if( pageInsertArray(
 70161  70323               pPg, pBegin, &pData, pCellptr,
 70162  70324               iCell+iNew, 1, pCArray
 70163  70325         ) ) goto editpage_fail;
 70164  70326       }
 70165  70327     }
................................................................................
 70227  70389     MemPage *pNew;                       /* Newly allocated page */
 70228  70390     int rc;                              /* Return Code */
 70229  70391     Pgno pgnoNew;                        /* Page number of pNew */
 70230  70392   
 70231  70393     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 70232  70394     assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 70233  70395     assert( pPage->nOverflow==1 );
 70234         -
        70396  +  
 70235  70397     if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;  /* dbfuzz001.test */
        70398  +  assert( pPage->nFree>=0 );
        70399  +  assert( pParent->nFree>=0 );
 70236  70400   
 70237  70401     /* Allocate a new page. This page will become the right-sibling of 
 70238  70402     ** pPage. Make the parent page writable, so that the new divider cell
 70239  70403     ** may be inserted. If both these operations are successful, proceed.
 70240  70404     */
 70241  70405     rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
 70242  70406   
................................................................................
 70398  70562       /* Reinitialize page pTo so that the contents of the MemPage structure
 70399  70563       ** match the new data. The initialization of pTo can actually fail under
 70400  70564       ** fairly obscure circumstances, even though it is a copy of initialized 
 70401  70565       ** page pFrom.
 70402  70566       */
 70403  70567       pTo->isInit = 0;
 70404  70568       rc = btreeInitPage(pTo);
        70569  +    if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo);
 70405  70570       if( rc!=SQLITE_OK ){
 70406  70571         *pRC = rc;
 70407  70572         return;
 70408  70573       }
 70409  70574     
 70410  70575       /* If this is an auto-vacuum database, update the pointer-map entries
 70411  70576       ** for any b-tree or overflow pages that pTo now contains the pointers to.
................................................................................
 70506  70671     */
 70507  70672     assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
 70508  70673     assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
 70509  70674   
 70510  70675     if( !aOvflSpace ){
 70511  70676       return SQLITE_NOMEM_BKPT;
 70512  70677     }
        70678  +  assert( pParent->nFree>=0 );
 70513  70679   
 70514  70680     /* Find the sibling pages to balance. Also locate the cells in pParent 
 70515  70681     ** that divide the siblings. An attempt is made to find NN siblings on 
 70516  70682     ** either side of pPage. More siblings are taken from one side, however, 
 70517  70683     ** if there are fewer than NN siblings on the other side. If pParent
 70518  70684     ** has NB or fewer children then all children of pParent are taken.  
 70519  70685     **
................................................................................
 70545  70711     pgno = get4byte(pRight);
 70546  70712     while( 1 ){
 70547  70713       rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
 70548  70714       if( rc ){
 70549  70715         memset(apOld, 0, (i+1)*sizeof(MemPage*));
 70550  70716         goto balance_cleanup;
 70551  70717       }
 70552         -    nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
        70718  +    if( apOld[i]->nFree<0 ){
        70719  +      rc = btreeComputeFreeSpace(apOld[i]);
        70720  +      if( rc ){
        70721  +        memset(apOld, 0, (i)*sizeof(MemPage*));
        70722  +        goto balance_cleanup;
        70723  +      }
        70724  +    }
 70553  70725       if( (i--)==0 ) break;
 70554  70726   
 70555  70727       if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
 70556  70728         apDiv[i] = pParent->apOvfl[0];
 70557  70729         pgno = get4byte(apDiv[i]);
 70558  70730         szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
 70559  70731         pParent->nOverflow = 0;
................................................................................
 70589  70761         }
 70590  70762         dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
 70591  70763       }
 70592  70764     }
 70593  70765   
 70594  70766     /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
 70595  70767     ** alignment */
        70768  +  nMaxCells = nOld*(MX_CELL(pBt) + ArraySize(pParent->apOvfl));
 70596  70769     nMaxCells = (nMaxCells + 3)&~3;
 70597  70770   
 70598  70771     /*
 70599  70772     ** Allocate space for memory structures
 70600  70773     */
 70601  70774     szScratch =
 70602  70775          nMaxCells*sizeof(u8*)                       /* b.apCell */
 70603  70776        + nMaxCells*sizeof(u16)                       /* b.szCell */
 70604  70777        + pBt->pageSize;                              /* aSpace1 */
 70605  70778   
 70606         -  assert( szScratch<=6*(int)pBt->pageSize );
        70779  +  assert( szScratch<=7*(int)pBt->pageSize );
 70607  70780     b.apCell = sqlite3StackAllocRaw(0, szScratch );
 70608  70781     if( b.apCell==0 ){
 70609  70782       rc = SQLITE_NOMEM_BKPT;
 70610  70783       goto balance_cleanup;
 70611  70784     }
 70612  70785     b.szCell = (u16*)&b.apCell[nMaxCells];
 70613  70786     aSpace1 = (u8*)&b.szCell[nMaxCells];
................................................................................
 70739  70912     ** 
 70740  70913     */
 70741  70914     usableSpace = pBt->usableSize - 12 + leafCorrection;
 70742  70915     for(i=k=0; i<nOld; i++, k++){
 70743  70916       MemPage *p = apOld[i];
 70744  70917       b.apEnd[k] = p->aDataEnd;
 70745  70918       b.ixNx[k] = cntOld[i];
        70919  +    if( k && b.ixNx[k]==b.ixNx[k-1] ){
        70920  +      k--;  /* Omit b.ixNx[] entry for child pages with no cells */
        70921  +    }
 70746  70922       if( !leafData ){
 70747  70923         k++;
 70748  70924         b.apEnd[k] = pParent->aDataEnd;
 70749  70925         b.ixNx[k] = cntOld[i]+1;
 70750  70926       }
        70927  +    assert( p->nFree>=0 );
 70751  70928       szNew[i] = usableSpace - p->nFree;
 70752  70929       for(j=0; j<p->nOverflow; j++){
 70753  70930         szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
 70754  70931       }
 70755  70932       cntNew[i] = cntOld[i];
 70756  70933     }
 70757  70934     k = nOld;
................................................................................
 70969  71146     ** associated with the right-child of each sibling may also need to be 
 70970  71147     ** updated. This happens below, after the sibling pages have been 
 70971  71148     ** populated, not here.
 70972  71149     */
 70973  71150     if( ISAUTOVACUUM ){
 70974  71151       MemPage *pOld;
 70975  71152       MemPage *pNew = pOld = apNew[0];
 70976         -    u8 *aOld = pNew->aData;
 70977  71153       int cntOldNext = pNew->nCell + pNew->nOverflow;
 70978         -    int usableSize = pBt->usableSize;
 70979  71154       int iNew = 0;
 70980  71155       int iOld = 0;
 70981  71156   
 70982  71157       for(i=0; i<b.nCell; i++){
 70983  71158         u8 *pCell = b.apCell[i];
 70984         -      if( i==cntOldNext ){
 70985         -        pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
        71159  +      while( i==cntOldNext ){
        71160  +        iOld++;
        71161  +        assert( iOld<nNew || iOld<nOld );
        71162  +        pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
 70986  71163           cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
 70987         -        aOld = pOld->aData;
 70988  71164         }
 70989  71165         if( i==cntNew[iNew] ){
 70990  71166           pNew = apNew[++iNew];
 70991  71167           if( !leafData ) continue;
 70992  71168         }
 70993  71169   
 70994  71170         /* Cell pCell is destined for new sibling page pNew. Originally, it
................................................................................
 70995  71171         ** was either part of sibling page iOld (possibly an overflow cell), 
 70996  71172         ** or else the divider cell to the left of sibling page iOld. So,
 70997  71173         ** if sibling page iOld had the same page number as pNew, and if
 70998  71174         ** pCell really was a part of sibling page iOld (not a divider or
 70999  71175         ** overflow cell), we can skip updating the pointer map entries.  */
 71000  71176         if( iOld>=nNew
 71001  71177          || pNew->pgno!=aPgno[iOld]
 71002         -       || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize])
        71178  +       || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
 71003  71179         ){
 71004  71180           if( !leafCorrection ){
 71005  71181             ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
 71006  71182           }
 71007  71183           if( cachedCellSize(&b,i)>pNew->minLocal ){
 71008  71184             ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
 71009  71185           }
................................................................................
 71146  71322       ** by smaller than the child due to the database header, and so all the
 71147  71323       ** free space needs to be up front.
 71148  71324       */
 71149  71325       assert( nNew==1 || CORRUPT_DB );
 71150  71326       rc = defragmentPage(apNew[0], -1);
 71151  71327       testcase( rc!=SQLITE_OK );
 71152  71328       assert( apNew[0]->nFree == 
 71153         -        (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
        71329  +        (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
        71330  +          - apNew[0]->nCell*2)
 71154  71331         || rc!=SQLITE_OK
 71155  71332       );
 71156  71333       copyNodeContent(apNew[0], pParent, &rc);
 71157  71334       freePage(apNew[0], &rc);
 71158  71335     }else if( ISAUTOVACUUM && !leafCorrection ){
 71159  71336       /* Fix the pointer map entries associated with the right-child of each
 71160  71337       ** sibling page. All other pointer map entries have already been taken
................................................................................
 71245  71422     if( rc ){
 71246  71423       *ppChild = 0;
 71247  71424       releasePage(pChild);
 71248  71425       return rc;
 71249  71426     }
 71250  71427     assert( sqlite3PagerIswriteable(pChild->pDbPage) );
 71251  71428     assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
 71252         -  assert( pChild->nCell==pRoot->nCell );
        71429  +  assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
 71253  71430   
 71254  71431     TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
 71255  71432   
 71256  71433     /* Copy the overflow cells from pRoot to pChild */
 71257  71434     memcpy(pChild->aiOvfl, pRoot->aiOvfl,
 71258  71435            pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
 71259  71436     memcpy(pChild->apOvfl, pRoot->apOvfl,
................................................................................
 71287  71464     VVA_ONLY( int balance_quick_called = 0 );
 71288  71465     VVA_ONLY( int balance_deeper_called = 0 );
 71289  71466   
 71290  71467     do {
 71291  71468       int iPage = pCur->iPage;
 71292  71469       MemPage *pPage = pCur->pPage;
 71293  71470   
        71471  +    if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
 71294  71472       if( iPage==0 ){
 71295  71473         if( pPage->nOverflow ){
 71296  71474           /* The root page of the b-tree is overfull. In this case call the
 71297  71475           ** balance_deeper() function to create a new child for the root-page
 71298  71476           ** and copy the current contents of the root-page to it. The
 71299  71477           ** next iteration of the do-loop will balance the child page.
 71300  71478           */ 
................................................................................
 71315  71493       }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
 71316  71494         break;
 71317  71495       }else{
 71318  71496         MemPage * const pParent = pCur->apPage[iPage-1];
 71319  71497         int const iIdx = pCur->aiIdx[iPage-1];
 71320  71498   
 71321  71499         rc = sqlite3PagerWrite(pParent->pDbPage);
        71500  +      if( rc==SQLITE_OK && pParent->nFree<0 ){
        71501  +        rc = btreeComputeFreeSpace(pParent);
        71502  +      }
 71322  71503         if( rc==SQLITE_OK ){
 71323  71504   #ifndef SQLITE_OMIT_QUICKBALANCE
 71324  71505           if( pPage->intKeyLeaf
 71325  71506            && pPage->nOverflow==1
 71326  71507            && pPage->aiOvfl[0]==pPage->nCell
 71327  71508            && pParent->pgno!=1
 71328  71509            && pParent->nCell==iIdx
................................................................................
 71661  71842   
 71662  71843     }
 71663  71844     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 71664  71845   
 71665  71846     pPage = pCur->pPage;
 71666  71847     assert( pPage->intKey || pX->nKey>=0 );
 71667  71848     assert( pPage->leaf || !pPage->intKey );
        71849  +  if( pPage->nFree<0 ){
        71850  +    rc = btreeComputeFreeSpace(pPage);
        71851  +    if( rc ) return rc;
        71852  +  }
 71668  71853   
 71669  71854     TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
 71670  71855             pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
 71671  71856             loc==0 ? "overwrite" : "new entry"));
 71672  71857     assert( pPage->isInit );
 71673  71858     newCell = pBt->pTmpSpace;
 71674  71859     assert( newCell!=0 );
................................................................................
 71803  71988   
 71804  71989     assert( cursorOwnsBtShared(pCur) );
 71805  71990     assert( pBt->inTransaction==TRANS_WRITE );
 71806  71991     assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 71807  71992     assert( pCur->curFlags & BTCF_WriteFlag );
 71808  71993     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 71809  71994     assert( !hasReadConflicts(p, pCur->pgnoRoot) );
 71810         -  assert( pCur->ix<pCur->pPage->nCell );
 71811         -  assert( pCur->eState==CURSOR_VALID );
 71812  71995     assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
        71996  +  if( pCur->eState==CURSOR_REQUIRESEEK ){
        71997  +    rc = btreeRestoreCursorPosition(pCur);
        71998  +    if( rc ) return rc;
        71999  +  }
        72000  +  assert( pCur->eState==CURSOR_VALID );
 71813  72001   
 71814  72002     iCellDepth = pCur->iPage;
 71815  72003     iCellIdx = pCur->ix;
 71816  72004     pPage = pCur->pPage;
 71817  72005     pCell = findCell(pPage, iCellIdx);
        72006  +  if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT;
 71818  72007   
 71819  72008     /* If the bPreserve flag is set to true, then the cursor position must
 71820  72009     ** be preserved following this delete operation. If the current delete
 71821  72010     ** will cause a b-tree rebalance, then this is done by saving the cursor
 71822  72011     ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
 71823  72012     ** returning. 
 71824  72013     **
................................................................................
 71881  72070     ** node to replace the deleted cell.  */
 71882  72071     if( !pPage->leaf ){
 71883  72072       MemPage *pLeaf = pCur->pPage;
 71884  72073       int nCell;
 71885  72074       Pgno n;
 71886  72075       unsigned char *pTmp;
 71887  72076   
        72077  +    if( pLeaf->nFree<0 ){
        72078  +      rc = btreeComputeFreeSpace(pLeaf);
        72079  +      if( rc ) return rc;
        72080  +    }
 71888  72081       if( iCellDepth<pCur->iPage-1 ){
 71889  72082         n = pCur->apPage[iCellDepth+1]->pgno;
 71890  72083       }else{
 71891  72084         n = pCur->pPage->pgno;
 71892  72085       }
 71893  72086       pCell = findCell(pLeaf, pLeaf->nCell-1);
 71894  72087       if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
................................................................................
 72239  72432     int rc;
 72240  72433     MemPage *pPage = 0;
 72241  72434     BtShared *pBt = p->pBt;
 72242  72435   
 72243  72436     assert( sqlite3BtreeHoldsMutex(p) );
 72244  72437     assert( p->inTrans==TRANS_WRITE );
 72245  72438     assert( iTable>=2 );
        72439  +  if( iTable>btreePagecount(pBt) ){
        72440  +    return SQLITE_CORRUPT_BKPT;
        72441  +  }
 72246  72442   
 72247  72443     rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
 72248  72444     if( rc ) return rc;
 72249  72445     rc = sqlite3BtreeClearTable(p, iTable, 0);
 72250  72446     if( rc ){
 72251  72447       releasePage(pPage);
 72252  72448       return rc;
................................................................................
 72587  72783   ** Check the integrity of the freelist or of an overflow page list.
 72588  72784   ** Verify that the number of pages on the list is N.
 72589  72785   */
 72590  72786   static void checkList(
 72591  72787     IntegrityCk *pCheck,  /* Integrity checking context */
 72592  72788     int isFreeList,       /* True for a freelist.  False for overflow page list */
 72593  72789     int iPage,            /* Page number for first page in the list */
 72594         -  int N                 /* Expected number of pages in the list */
        72790  +  u32 N                 /* Expected number of pages in the list */
 72595  72791   ){
 72596  72792     int i;
 72597         -  int expected = N;
        72793  +  u32 expected = N;
 72598  72794     int nErrAtStart = pCheck->nErr;
 72599  72795     while( iPage!=0 && pCheck->mxErr ){
 72600  72796       DbPage *pOvflPage;
 72601  72797       unsigned char *pOvflData;
 72602  72798       if( checkRef(pCheck, iPage) ) break;
 72603  72799       N--;
 72604  72800       if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
................................................................................
 72772  72968     pPage->isInit = 0;
 72773  72969     if( (rc = btreeInitPage(pPage))!=0 ){
 72774  72970       assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
 72775  72971       checkAppendMsg(pCheck,
 72776  72972                      "btreeInitPage() returns error code %d", rc);
 72777  72973       goto end_of_check;
 72778  72974     }
        72975  +  if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
        72976  +    assert( rc==SQLITE_CORRUPT );
        72977  +    checkAppendMsg(pCheck, "free space corruption", rc);
        72978  +    goto end_of_check;
        72979  +  }
 72779  72980     data = pPage->aData;
 72780  72981     hdr = pPage->hdrOffset;
 72781  72982   
 72782  72983     /* Set up for cell analysis */
 72783  72984     pCheck->zPfx = "On tree page %d cell %d: ";
 72784  72985     contentOffset = get2byteNotZero(&data[hdr+5]);
 72785  72986     assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
................................................................................
 72844  73045         }
 72845  73046         maxKey = info.nKey;
 72846  73047         keyCanBeEqual = 0;     /* Only the first key on the page may ==maxKey */
 72847  73048       }
 72848  73049   
 72849  73050       /* Check the content overflow list */
 72850  73051       if( info.nPayload>info.nLocal ){
 72851         -      int nPage;       /* Number of pages on the overflow chain */
        73052  +      u32 nPage;       /* Number of pages on the overflow chain */
 72852  73053         Pgno pgnoOvfl;   /* First page of the overflow chain */
 72853  73054         assert( pc + info.nSize - 4 <= usableSize );
 72854  73055         nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
 72855  73056         pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
 72856  73057   #ifndef SQLITE_OMIT_AUTOVACUUM
 72857  73058         if( pBt->autoVacuum ){
 72858  73059           checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
................................................................................
 72904  73105       ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
 72905  73106       ** is the offset of the first freeblock, or zero if there are no
 72906  73107       ** freeblocks on the page. 
 72907  73108       */
 72908  73109       i = get2byte(&data[hdr+1]);
 72909  73110       while( i>0 ){
 72910  73111         int size, j;
 72911         -      assert( (u32)i<=usableSize-4 );     /* Enforced by btreeInitPage() */
        73112  +      assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
 72912  73113         size = get2byte(&data[i+2]);
 72913         -      assert( (u32)(i+size)<=usableSize );  /* Enforced by btreeInitPage() */
        73114  +      assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
 72914  73115         btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
 72915  73116         /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
 72916  73117         ** big-endian integer which is the offset in the b-tree page of the next
 72917  73118         ** freeblock in the chain, or zero if the freeblock is the last on the
 72918  73119         ** chain. */
 72919  73120         j = get2byte(&data[i]);
 72920  73121         /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
 72921  73122         ** increasing offset. */
 72922         -      assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
 72923         -      assert( (u32)j<=usableSize-4 );   /* Enforced by btreeInitPage() */
        73123  +      assert( j==0 || j>i+size );     /* Enforced by btreeComputeFreeSpace() */
        73124  +      assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
 72924  73125         i = j;
 72925  73126       }
 72926  73127       /* Analyze the min-heap looking for overlap between cells and/or 
 72927  73128       ** freeblocks, and counting the number of untracked bytes in nFrag.
 72928  73129       ** 
 72929  73130       ** Each min-heap entry is of the form:    (start_address<<16)|end_address.
 72930  73131       ** There is an implied first entry the covers the page header, the cell
................................................................................
 74260  74461         /* This is a pointer type.  There may be a flag to indicate what to
 74261  74462         ** do with the pointer. */
 74262  74463         assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
 74263  74464                 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
 74264  74465                 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
 74265  74466   
 74266  74467         /* No other bits set */
 74267         -      assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype
        74468  +      assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
 74268  74469                              |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
 74269  74470       }else{
 74270  74471         /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
 74271  74472         ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
 74272  74473       }
 74273  74474     }else{
 74274  74475       /* The MEM_Cleared bit is only allowed on NULLs */
................................................................................
 74381  74582     assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
 74382  74583     assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
 74383  74584     return rc;
 74384  74585   #endif
 74385  74586   }
 74386  74587   
 74387  74588   /*
 74388         -** Make sure pMem->z points to a writable allocation of at least 
 74389         -** min(n,32) bytes.
        74589  +** Make sure pMem->z points to a writable allocation of at least n bytes.
 74390  74590   **
 74391  74591   ** If the bPreserve argument is true, then copy of the content of
 74392  74592   ** pMem->z into the new allocation.  pMem must be either a string or
 74393  74593   ** blob if bPreserve is true.  If bPreserve is false, any prior content
 74394  74594   ** in pMem->z is discarded.
 74395  74595   */
 74396  74596   SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
 74397  74597     assert( sqlite3VdbeCheckMemInvariants(pMem) );
 74398  74598     assert( !sqlite3VdbeMemIsRowSet(pMem) );
 74399  74599     testcase( pMem->db==0 );
 74400  74600   
 74401  74601     /* If the bPreserve flag is set to true, then the memory cell must already
 74402  74602     ** contain a valid string or blob value.  */
 74403         -  assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
        74603  +  assert( bPreserve==0 
        74604  +       || pMem->flags&(MEM_Blob|MEM_Str)
        74605  +       || MemNullNochng(pMem)
        74606  +  );
 74404  74607     testcase( bPreserve && pMem->z==0 );
 74405  74608   
 74406  74609     assert( pMem->szMalloc==0
 74407  74610          || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
 74408         -  if( n<32 ) n = 32;
 74409  74611     if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
 74410  74612       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 74411  74613       bPreserve = 0;
 74412  74614     }else{
 74413  74615       if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
 74414  74616       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
 74415  74617     }
................................................................................
 74503  74705   ** If the given Mem* has a zero-filled tail, turn it into an ordinary
 74504  74706   ** blob stored in dynamically allocated space.
 74505  74707   */
 74506  74708   #ifndef SQLITE_OMIT_INCRBLOB
 74507  74709   SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
 74508  74710     int nByte;
 74509  74711     assert( pMem->flags & MEM_Zero );
 74510         -  assert( pMem->flags&MEM_Blob );
        74712  +  assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
        74713  +  testcase( sqlite3_value_nochange(pMem) );
 74511  74714     assert( !sqlite3VdbeMemIsRowSet(pMem) );
 74512  74715     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 74513  74716   
 74514  74717     /* Set nByte to the number of bytes required to store the expanded blob. */
 74515  74718     nByte = pMem->n + pMem->u.nZero;
 74516  74719     if( nByte<=0 ){
 74517  74720       nByte = 1;
................................................................................
 75250  75453       iLimit = SQLITE_MAX_LENGTH;
 75251  75454     }
 75252  75455     flags = (enc==0?MEM_Blob:MEM_Str);
 75253  75456     if( nByte<0 ){
 75254  75457       assert( enc!=0 );
 75255  75458       if( enc==SQLITE_UTF8 ){
 75256  75459         nByte = 0x7fffffff & (int)strlen(z);
 75257         -      if( nByte>iLimit ) nByte = iLimit+1;
 75258  75460       }else{
 75259  75461         for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
 75260  75462       }
 75261  75463       flags |= MEM_Term;
 75262  75464     }
 75263  75465   
 75264  75466     /* The following block sets the new values of Mem.z and Mem.xDel. It
 75265  75467     ** also sets a flag in local variable "flags" to indicate the memory
 75266  75468     ** management (one of MEM_Dyn or MEM_Static).
 75267  75469     */
 75268  75470     if( xDel==SQLITE_TRANSIENT ){
 75269         -    int nAlloc = nByte;
        75471  +    u32 nAlloc = nByte;
 75270  75472       if( flags&MEM_Term ){
 75271  75473         nAlloc += (enc==SQLITE_UTF8?1:2);
 75272  75474       }
 75273  75475       if( nByte>iLimit ){
 75274  75476         return SQLITE_TOOBIG;
 75275  75477       }
 75276  75478       testcase( nAlloc==0 );
 75277  75479       testcase( nAlloc==31 );
 75278  75480       testcase( nAlloc==32 );
 75279         -    if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
        75481  +    if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
 75280  75482         return SQLITE_NOMEM_BKPT;
 75281  75483       }
 75282  75484       memcpy(pMem->z, z, nAlloc);
 75283         -  }else if( xDel==SQLITE_DYNAMIC ){
 75284         -    sqlite3VdbeMemRelease(pMem);
 75285         -    pMem->zMalloc = pMem->z = (char *)z;
 75286         -    pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
 75287  75485     }else{
 75288  75486       sqlite3VdbeMemRelease(pMem);
 75289  75487       pMem->z = (char *)z;
 75290         -    pMem->xDel = xDel;
 75291         -    flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
        75488  +    if( xDel==SQLITE_DYNAMIC ){
        75489  +      pMem->zMalloc = pMem->z;
        75490  +      pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
        75491  +    }else{
        75492  +      pMem->xDel = xDel;
        75493  +      flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
        75494  +    }
 75292  75495     }
 75293  75496   
 75294  75497     pMem->n = nByte;
 75295  75498     pMem->flags = flags;
 75296  75499     pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
 75297  75500   
 75298  75501   #ifndef SQLITE_OMIT_UTF16
................................................................................
 76734  76937     memset(&sIter, 0, sizeof(sIter));
 76735  76938     sIter.v = v;
 76736  76939   
 76737  76940     while( (pOp = opIterNext(&sIter))!=0 ){
 76738  76941       int opcode = pOp->opcode;
 76739  76942       if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
 76740  76943        || opcode==OP_VDestroy
        76944  +     || (opcode==OP_Function0 && pOp->p4.pFunc->funcFlags&SQLITE_FUNC_INTERNAL)
 76741  76945        || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
 76742  76946         && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
 76743  76947       ){
 76744  76948         hasAbort = 1;
 76745  76949         break;
 76746  76950       }
 76747  76951       if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
................................................................................
 81348  81552     return aType[pVal->flags&MEM_AffMask];
 81349  81553   }
 81350  81554   
 81351  81555   /* Return true if a parameter to xUpdate represents an unchanged column */
 81352  81556   SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
 81353  81557     return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
 81354  81558   }
        81559  +
        81560  +/* Return true if a parameter value originated from an sqlite3_bind() */
        81561  +SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
        81562  +  return (pVal->flags&MEM_FromBind)!=0;
        81563  +}
 81355  81564   
 81356  81565   /* Make a copy of an sqlite3_value object
 81357  81566   */
 81358  81567   SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
 81359  81568     sqlite3_value *pNew;
 81360  81569     if( pOrig==0 ) return 0;
 81361  81570     pNew = sqlite3_malloc( sizeof(*pNew) );
................................................................................
 82194  82403   **    3      The name of the table that the column derives from
 82195  82404   **    4      The name of the table column that the result column derives from
 82196  82405   **
 82197  82406   ** If the result is not a simple column reference (if it is an expression
 82198  82407   ** or a constant) then useTypes 2, 3, and 4 return NULL.
 82199  82408   */
 82200  82409   static const void *columnName(
 82201         -  sqlite3_stmt *pStmt,
 82202         -  int N,
 82203         -  const void *(*xFunc)(Mem*),
 82204         -  int useType
        82410  +  sqlite3_stmt *pStmt,     /* The statement */
        82411  +  int N,                   /* Which column to get the name for */
        82412  +  int useUtf16,            /* True to return the name as UTF16 */
        82413  +  int useType              /* What type of name */
 82205  82414   ){
 82206  82415     const void *ret;
 82207  82416     Vdbe *p;
 82208  82417     int n;
 82209  82418     sqlite3 *db;
 82210  82419   #ifdef SQLITE_ENABLE_API_ARMOR
 82211  82420     if( pStmt==0 ){
................................................................................
 82218  82427     db = p->db;
 82219  82428     assert( db!=0 );
 82220  82429     n = sqlite3_column_count(pStmt);
 82221  82430     if( N<n && N>=0 ){
 82222  82431       N += useType*n;
 82223  82432       sqlite3_mutex_enter(db->mutex);
 82224  82433       assert( db->mallocFailed==0 );
 82225         -    ret = xFunc(&p->aColName[N]);
 82226         -     /* A malloc may have failed inside of the xFunc() call. If this
        82434  +#ifndef SQLITE_OMIT_UTF16
        82435  +    if( useUtf16 ){
        82436  +      ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
        82437  +    }else
        82438  +#endif
        82439  +    {
        82440  +      ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
        82441  +    }
        82442  +    /* A malloc may have failed inside of the _text() call. If this
 82227  82443       ** is the case, clear the mallocFailed flag and return NULL.
 82228  82444       */
 82229  82445       if( db->mallocFailed ){
 82230  82446         sqlite3OomClear(db);
 82231  82447         ret = 0;
 82232  82448       }
 82233  82449       sqlite3_mutex_leave(db->mutex);
................................................................................
 82236  82452   }
 82237  82453   
 82238  82454   /*
 82239  82455   ** Return the name of the Nth column of the result set returned by SQL
 82240  82456   ** statement pStmt.
 82241  82457   */
 82242  82458   SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
 82243         -  return columnName(
 82244         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
        82459  +  return columnName(pStmt, N, 0, COLNAME_NAME);
 82245  82460   }
 82246  82461   #ifndef SQLITE_OMIT_UTF16
 82247  82462   SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
 82248         -  return columnName(
 82249         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
        82463  +  return columnName(pStmt, N, 1, COLNAME_NAME);
 82250  82464   }
 82251  82465   #endif
 82252  82466   
 82253  82467   /*
 82254  82468   ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
 82255  82469   ** not define OMIT_DECLTYPE.
 82256  82470   */
................................................................................
 82261  82475   
 82262  82476   #ifndef SQLITE_OMIT_DECLTYPE
 82263  82477   /*
 82264  82478   ** Return the column declaration type (if applicable) of the 'i'th column
 82265  82479   ** of the result set of SQL statement pStmt.
 82266  82480   */
 82267  82481   SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
 82268         -  return columnName(
 82269         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
        82482  +  return columnName(pStmt, N, 0, COLNAME_DECLTYPE);
 82270  82483   }
 82271  82484   #ifndef SQLITE_OMIT_UTF16
 82272  82485   SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
 82273         -  return columnName(
 82274         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
        82486  +  return columnName(pStmt, N, 1, COLNAME_DECLTYPE);
 82275  82487   }
 82276  82488   #endif /* SQLITE_OMIT_UTF16 */
 82277  82489   #endif /* SQLITE_OMIT_DECLTYPE */
 82278  82490   
 82279  82491   #ifdef SQLITE_ENABLE_COLUMN_METADATA
 82280  82492   /*
 82281  82493   ** Return the name of the database from which a result column derives.
 82282  82494   ** NULL is returned if the result column is an expression or constant or
 82283  82495   ** anything else which is not an unambiguous reference to a database column.
 82284  82496   */
 82285  82497   SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
 82286         -  return columnName(
 82287         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
        82498  +  return columnName(pStmt, N, 0, COLNAME_DATABASE);
 82288  82499   }
 82289  82500   #ifndef SQLITE_OMIT_UTF16
 82290  82501   SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
 82291         -  return columnName(
 82292         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
        82502  +  return columnName(pStmt, N, 1, COLNAME_DATABASE);
 82293  82503   }
 82294  82504   #endif /* SQLITE_OMIT_UTF16 */
 82295  82505   
 82296  82506   /*
 82297  82507   ** Return the name of the table from which a result column derives.
 82298  82508   ** NULL is returned if the result column is an expression or constant or
 82299  82509   ** anything else which is not an unambiguous reference to a database column.
 82300  82510   */
 82301  82511   SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
 82302         -  return columnName(
 82303         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
        82512  +  return columnName(pStmt, N, 0, COLNAME_TABLE);
 82304  82513   }
 82305  82514   #ifndef SQLITE_OMIT_UTF16
 82306  82515   SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
 82307         -  return columnName(
 82308         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
        82516  +  return columnName(pStmt, N, 1, COLNAME_TABLE);
 82309  82517   }
 82310  82518   #endif /* SQLITE_OMIT_UTF16 */
 82311  82519   
 82312  82520   /*
 82313  82521   ** Return the name of the table column from which a result column derives.
 82314  82522   ** NULL is returned if the result column is an expression or constant or
 82315  82523   ** anything else which is not an unambiguous reference to a database column.
 82316  82524   */
 82317  82525   SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
 82318         -  return columnName(
 82319         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
        82526  +  return columnName(pStmt, N, 0, COLNAME_COLUMN);
 82320  82527   }
 82321  82528   #ifndef SQLITE_OMIT_UTF16
 82322  82529   SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
 82323         -  return columnName(
 82324         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
        82530  +  return columnName(pStmt, N, 1, COLNAME_COLUMN);
 82325  82531   }
 82326  82532   #endif /* SQLITE_OMIT_UTF16 */
 82327  82533   #endif /* SQLITE_ENABLE_COLUMN_METADATA */
 82328  82534   
 82329  82535   
 82330  82536   /******************************* sqlite3_bind_  ***************************
 82331  82537   ** 
................................................................................
 82684  82890   /*
 82685  82891   ** Return true if the prepared statement is guaranteed to not modify the
 82686  82892   ** database.
 82687  82893   */
 82688  82894   SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
 82689  82895     return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
 82690  82896   }
        82897  +
        82898  +/*
        82899  +** Return 1 if the statement is an EXPLAIN and return 2 if the
        82900  +** statement is an EXPLAIN QUERY PLAN
        82901  +*/
        82902  +SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
        82903  +  return pStmt ? ((Vdbe*)pStmt)->explain : 0;
        82904  +}
 82691  82905   
 82692  82906   /*
 82693  82907   ** Return true if the prepared statement is in need of being reset.
 82694  82908   */
 82695  82909   SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
 82696  82910     Vdbe *v = (Vdbe*)pStmt;
 82697  82911     return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
................................................................................
 83374  83588   #endif
 83375  83589   
 83376  83590   /*
 83377  83591   ** Invoke the VDBE coverage callback, if that callback is defined.  This
 83378  83592   ** feature is used for test suite validation only and does not appear an
 83379  83593   ** production builds.
 83380  83594   **
 83381         -** M is an integer between 2 and 4.  2 indicates a ordinary two-way
 83382         -** branch (I=0 means fall through and I=1 means taken).  3 indicates
 83383         -** a 3-way branch where the third way is when one of the operands is
 83384         -** NULL.  4 indicates the OP_Jump instruction which has three destinations
 83385         -** depending on whether the first operand is less than, equal to, or greater
 83386         -** than the second. 
        83595  +** M is the type of branch.  I is the direction taken for this instance of
        83596  +** the branch.
        83597  +**
        83598  +**   M: 2 - two-way branch (I=0: fall-thru   1: jump                )
        83599  +**      3 - two-way + NULL (I=0: fall-thru   1: jump      2: NULL   )
        83600  +**      4 - OP_Jump        (I=0: jump p1     1: jump p2   2: jump p3)
        83601  +**
        83602  +** In other words, if M is 2, then I is either 0 (for fall-through) or
        83603  +** 1 (for when the branch is taken).  If M is 3, the I is 0 for an
        83604  +** ordinary fall-through, I is 1 if the branch was taken, and I is 2 
        83605  +** if the result of comparison is NULL.  For M=3, I=2 the jump may or
        83606  +** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
        83607  +** When M is 4, that means that an OP_Jump is being run.  I is 0, 1, or 2
        83608  +** depending on if the operands are less than, equal, or greater than.
 83387  83609   **
 83388  83610   ** iSrcLine is the source code line (from the __LINE__ macro) that
 83389  83611   ** generated the VDBE instruction combined with flag bits.  The source
 83390  83612   ** code line number is in the lower 24 bits of iSrcLine and the upper
 83391  83613   ** 8 bytes are flags.  The lower three bits of the flags indicate
 83392  83614   ** values for I that should never occur.  For example, if the branch is
 83393  83615   ** always taken, the flags should be 0x05 since the fall-through and
 83394  83616   ** alternate branch are never taken.  If a branch is never taken then
 83395  83617   ** flags should be 0x06 since only the fall-through approach is allowed.
 83396  83618   **
 83397         -** Bit 0x04 of the flags indicates an OP_Jump opcode that is only
        83619  +** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
 83398  83620   ** interested in equal or not-equal.  In other words, I==0 and I==2
 83399         -** should be treated the same.
        83621  +** should be treated as equivalent
 83400  83622   **
 83401  83623   ** Since only a line number is retained, not the filename, this macro
 83402  83624   ** only works for amalgamation builds.  But that is ok, since these macros
 83403  83625   ** should be no-ops except for special builds used to measure test coverage.
 83404  83626   */
 83405  83627   #if !defined(SQLITE_VDBE_COVERAGE)
 83406  83628   # define VdbeBranchTaken(I,M)
................................................................................
 83416  83638       /* The upper 8 bits of iSrcLine are flags.  The lower three bits of
 83417  83639       ** the flags indicate directions that the branch can never go.  If
 83418  83640       ** a branch really does go in one of those directions, assert right
 83419  83641       ** away. */
 83420  83642       mNever = iSrcLine >> 24;
 83421  83643       assert( (I & mNever)==0 );
 83422  83644       if( sqlite3GlobalConfig.xVdbeBranch==0 ) return;  /*NO_TEST*/
        83645  +    /* Invoke the branch coverage callback with three arguments:
        83646  +    **    iSrcLine - the line number of the VdbeCoverage() macro, with
        83647  +    **               flags removed.
        83648  +    **    I        - Mask of bits 0x07 indicating which cases are are
        83649  +    **               fulfilled by this instance of the jump.  0x01 means
        83650  +    **               fall-thru, 0x02 means taken, 0x04 means NULL.  Any
        83651  +    **               impossible cases (ex: if the comparison is never NULL)
        83652  +    **               are filled in automatically so that the coverage
        83653  +    **               measurement logic does not flag those impossible cases
        83654  +    **               as missed coverage.
        83655  +    **    M        - Type of jump.  Same as M argument above
        83656  +    */
 83423  83657       I |= mNever;
 83424  83658       if( M==2 ) I |= 0x04;
 83425  83659       if( M==4 ){
 83426  83660         I |= 0x08;
 83427  83661         if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
 83428  83662       }
 83429  83663       sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
................................................................................
 83963  84197   #ifdef VDBE_PROFILE
 83964  84198     u64 start;                 /* CPU clock count at start of opcode */
 83965  84199   #endif
 83966  84200     /*** INSERT STACK UNION HERE ***/
 83967  84201   
 83968  84202     assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 83969  84203     sqlite3VdbeEnter(p);
        84204  +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
        84205  +  if( db->xProgress ){
        84206  +    u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
        84207  +    assert( 0 < db->nProgressOps );
        84208  +    nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
        84209  +  }else{
        84210  +    nProgressLimit = 0xffffffff;
        84211  +  }
        84212  +#endif
 83970  84213     if( p->rc==SQLITE_NOMEM ){
 83971  84214       /* This happens if a malloc() inside a call to sqlite3_column_text() or
 83972  84215       ** sqlite3_column_text16() failed.  */
 83973  84216       goto no_mem;
 83974  84217     }
 83975  84218     assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
 83976  84219     assert( p->bIsReader || p->readOnly!=0 );
 83977  84220     p->iCurrentTime = 0;
 83978  84221     assert( p->explain==0 );
 83979  84222     p->pResultSet = 0;
 83980  84223     db->busyHandler.nBusy = 0;
 83981  84224     if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 83982  84225     sqlite3VdbeIOTraceSql(p);
 83983         -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 83984         -  if( db->xProgress ){
 83985         -    u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
 83986         -    assert( 0 < db->nProgressOps );
 83987         -    nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
 83988         -  }else{
 83989         -    nProgressLimit = 0xffffffff;
 83990         -  }
 83991         -#endif
 83992  84226   #ifdef SQLITE_DEBUG
 83993  84227     sqlite3BeginBenignMalloc();
 83994  84228     if( p->pc==0
 83995  84229      && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
 83996  84230     ){
 83997  84231       int i;
 83998  84232       int once = 1;
................................................................................
 84160  84394   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 84161  84395     /* Call the progress callback if it is configured and the required number
 84162  84396     ** of VDBE ops have been executed (either since this invocation of
 84163  84397     ** sqlite3VdbeExec() or since last time the progress callback was called).
 84164  84398     ** If the progress callback returns non-zero, exit the virtual machine with
 84165  84399     ** a return code SQLITE_ABORT.
 84166  84400     */
 84167         -  if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
        84401  +  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
 84168  84402       assert( db->nProgressOps!=0 );
 84169         -    nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
        84403  +    nProgressLimit += db->nProgressOps;
 84170  84404       if( db->xProgress(db->pProgressArg) ){
        84405  +      nProgressLimit = 0xffffffff;
 84171  84406         rc = SQLITE_INTERRUPT;
 84172  84407         goto abort_due_to_error;
 84173  84408       }
 84174  84409     }
 84175  84410   #endif
 84176  84411     
 84177  84412     break;
................................................................................
 84442  84677     pOp->opcode = OP_String;
 84443  84678     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
 84444  84679   
 84445  84680   #ifndef SQLITE_OMIT_UTF16
 84446  84681     if( encoding!=SQLITE_UTF8 ){
 84447  84682       rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
 84448  84683       assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
        84684  +    if( rc ) goto too_big;
 84449  84685       if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
 84450  84686       assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
 84451  84687       assert( VdbeMemDynamic(pOut)==0 );
 84452  84688       pOut->szMalloc = 0;
 84453  84689       pOut->flags |= MEM_Static;
 84454  84690       if( pOp->p4type==P4_DYNAMIC ){
 84455  84691         sqlite3DbFree(db, pOp->p4.z);
 84456  84692       }
 84457  84693       pOp->p4type = P4_DYNAMIC;
 84458  84694       pOp->p4.z = pOut->z;
 84459  84695       pOp->p1 = pOut->n;
 84460  84696     }
 84461         -  testcase( rc==SQLITE_TOOBIG );
 84462  84697   #endif
 84463  84698     if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 84464  84699       goto too_big;
 84465  84700     }
 84466  84701     assert( rc==SQLITE_OK );
 84467  84702     /* Fall through to the next case, OP_String */
 84468  84703   }
................................................................................
 84576  84811     assert( pOp->p1>0 && pOp->p1<=p->nVar );
 84577  84812     assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
 84578  84813     pVar = &p->aVar[pOp->p1 - 1];
 84579  84814     if( sqlite3VdbeMemTooBig(pVar) ){
 84580  84815       goto too_big;
 84581  84816     }
 84582  84817     pOut = &aMem[pOp->p2];
 84583         -  sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
        84818  +  if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
        84819  +  memcpy(pOut, pVar, MEMCELLSIZE);
        84820  +  pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
        84821  +  pOut->flags |= MEM_Static|MEM_FromBind;
 84584  84822     UPDATE_MAX_BLOBSIZE(pOut);
 84585  84823     break;
 84586  84824   }
 84587  84825   
 84588  84826   /* Opcode: Move P1 P2 P3 * *
 84589  84827   ** Synopsis: r[P2@P3]=r[P1@P3]
 84590  84828   **
................................................................................
 84709  84947   case OP_ResultRow: {
 84710  84948     Mem *pMem;
 84711  84949     int i;
 84712  84950     assert( p->nResColumn==pOp->p2 );
 84713  84951     assert( pOp->p1>0 );
 84714  84952     assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
 84715  84953   
 84716         -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 84717         -  /* Run the progress counter just before returning.
 84718         -  */
 84719         -  if( db->xProgress!=0
 84720         -   && nVmStep>=nProgressLimit 
 84721         -   && db->xProgress(db->pProgressArg)!=0
 84722         -  ){
 84723         -    rc = SQLITE_INTERRUPT;
 84724         -    goto abort_due_to_error;
 84725         -  }
 84726         -#endif
 84727         -
 84728  84954     /* If this statement has violated immediate foreign key constraints, do
 84729  84955     ** not return the number of rows modified. And do not RELEASE the statement
 84730  84956     ** transaction. It needs to be rolled back.  */
 84731  84957     if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
 84732  84958       assert( db->flags&SQLITE_CountRows );
 84733  84959       assert( p->usesStmtJournal );
 84734  84960       goto abort_due_to_error;
................................................................................
 85086  85312   ** without data loss, then jump immediately to P2, or if P2==0
 85087  85313   ** raise an SQLITE_MISMATCH exception.
 85088  85314   */
 85089  85315   case OP_MustBeInt: {            /* jump, in1 */
 85090  85316     pIn1 = &aMem[pOp->p1];
 85091  85317     if( (pIn1->flags & MEM_Int)==0 ){
 85092  85318       applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
 85093         -    VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
 85094  85319       if( (pIn1->flags & MEM_Int)==0 ){
        85320  +      VdbeBranchTaken(1, 2);
 85095  85321         if( pOp->p2==0 ){
 85096  85322           rc = SQLITE_MISMATCH;
 85097  85323           goto abort_due_to_error;
 85098  85324         }else{
 85099  85325           goto jump_to_p2;
 85100  85326         }
 85101  85327       }
 85102  85328     }
        85329  +  VdbeBranchTaken(0, 2);
 85103  85330     MemSetTypeFlag(pIn1, MEM_Int);
 85104  85331     break;
 85105  85332   }
 85106  85333   
 85107  85334   #ifndef SQLITE_OMIT_FLOATING_POINT
 85108  85335   /* Opcode: RealAffinity P1 * * * *
 85109  85336   **
................................................................................
 85270  85497     if( (flags1 | flags3)&MEM_Null ){
 85271  85498       /* One or both operands are NULL */
 85272  85499       if( pOp->p5 & SQLITE_NULLEQ ){
 85273  85500         /* If SQLITE_NULLEQ is set (which will only happen if the operator is
 85274  85501         ** OP_Eq or OP_Ne) then take the jump or not depending on whether
 85275  85502         ** or not both operands are null.
 85276  85503         */
 85277         -      assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
 85278  85504         assert( (flags1 & MEM_Cleared)==0 );
 85279  85505         assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
 85280  85506         testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
 85281  85507         if( (flags1&flags3&MEM_Null)!=0
 85282  85508          && (flags3&MEM_Cleared)==0
 85283  85509         ){
 85284  85510           res = 0;  /* Operands are equal */
 85285  85511         }else{
 85286         -        res = 1;  /* Operands are not equal */
        85512  +        res = ((flags3 & MEM_Null) ? -1 : +1);  /* Operands are not equal */
 85287  85513         }
 85288  85514       }else{
 85289  85515         /* SQLITE_NULLEQ is clear and at least one operand is NULL,
 85290  85516         ** then the result is always NULL.
 85291  85517         ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
 85292  85518         */
 85293  85519         if( pOp->p5 & SQLITE_STOREP2 ){
................................................................................
 85397  85623         if( (pOp->opcode==OP_Eq)==res2 ) break;
 85398  85624       }
 85399  85625       memAboutToChange(p, pOut);
 85400  85626       MemSetTypeFlag(pOut, MEM_Int);
 85401  85627       pOut->u.i = res2;
 85402  85628       REGISTER_TRACE(pOp->p2, pOut);
 85403  85629     }else{
 85404         -    VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
        85630  +    VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
 85405  85631       if( res2 ){
 85406  85632         goto jump_to_p2;
 85407  85633       }
 85408  85634     }
 85409  85635     break;
 85410  85636   }
 85411  85637   
................................................................................
 85947  86173       op_column_read_header:
 85948  86174         i = pC->nHdrParsed;
 85949  86175         offset64 = aOffset[i];
 85950  86176         zHdr = zData + pC->iHdrOffset;
 85951  86177         zEndHdr = zData + aOffset[0];
 85952  86178         testcase( zHdr>=zEndHdr );
 85953  86179         do{
 85954         -        if( (t = zHdr[0])<0x80 ){
        86180  +        if( (pC->aType[i] = t = zHdr[0])<0x80 ){
 85955  86181             zHdr++;
 85956  86182             offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
 85957  86183           }else{
 85958  86184             zHdr += sqlite3GetVarint32(zHdr, &t);
        86185  +          pC->aType[i] = t;
 85959  86186             offset64 += sqlite3VdbeSerialTypeLen(t);
 85960  86187           }
 85961         -        pC->aType[i++] = t;
 85962         -        aOffset[i] = (u32)(offset64 & 0xffffffff);
        86188  +        aOffset[++i] = (u32)(offset64 & 0xffffffff);
 85963  86189         }while( i<=p2 && zHdr<zEndHdr );
 85964  86190   
 85965  86191         /* The record is corrupt if any of the following are true:
 85966  86192         ** (1) the bytes of the header extend past the declared header size
 85967  86193         ** (2) the entire header was used but not all data was used
 85968  86194         ** (3) the end of the data extends beyond the end of the record.
 85969  86195         */
................................................................................
 86958  87184     pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
 86959  87185     if( pCx==0 ) goto no_mem;
 86960  87186     pCx->nullRow = 1;
 86961  87187     pCx->isEphemeral = 1;
 86962  87188     pCx->pKeyInfo = pOrig->pKeyInfo;
 86963  87189     pCx->isTable = pOrig->isTable;
 86964  87190     pCx->pgnoRoot = pOrig->pgnoRoot;
        87191  +  pCx->isOrdered = pOrig->isOrdered;
 86965  87192     rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
 86966  87193                             pCx->pKeyInfo, pCx->uc.pCursor);
 86967  87194     /* The sqlite3BtreeCursor() routine can only fail for the first cursor
 86968  87195     ** opened for a database.  Since there is already an open cursor when this
 86969  87196     ** opcode is run, the sqlite3BtreeCursor() cannot fail */
 86970  87197     assert( rc==SQLITE_OK );
 86971  87198     break;
................................................................................
 87921  88148   ** and register P2 becomes ephemeral.  If the cursor is changed, the
 87922  88149   ** value of register P2 will then change.  Make sure this does not
 87923  88150   ** cause any problems.)
 87924  88151   **
 87925  88152   ** This instruction only works on tables.  The equivalent instruction
 87926  88153   ** for indices is OP_IdxInsert.
 87927  88154   */
 87928         -/* Opcode: InsertInt P1 P2 P3 P4 P5
 87929         -** Synopsis: intkey=P3 data=r[P2]
 87930         -**
 87931         -** This works exactly like OP_Insert except that the key is the
 87932         -** integer value P3, not the value of the integer stored in register P3.
 87933         -*/
 87934         -case OP_Insert: 
 87935         -case OP_InsertInt: {
        88155  +case OP_Insert: {
 87936  88156     Mem *pData;       /* MEM cell holding data for the record to be inserted */
 87937  88157     Mem *pKey;        /* MEM cell holding key  for the record */
 87938  88158     VdbeCursor *pC;   /* Cursor to table into which insert is written */
 87939  88159     int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 87940  88160     const char *zDb;  /* database name - used by the update hook */
 87941  88161     Table *pTab;      /* Table structure - used by update and pre-update hooks */
 87942  88162     BtreePayload x;   /* Payload to be inserted */
................................................................................
 87949  88169     assert( pC->eCurType==CURTYPE_BTREE );
 87950  88170     assert( pC->uc.pCursor!=0 );
 87951  88171     assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
 87952  88172     assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
 87953  88173     REGISTER_TRACE(pOp->p2, pData);
 87954  88174     sqlite3VdbeIncrWriteCounter(p, pC);
 87955  88175   
 87956         -  if( pOp->opcode==OP_Insert ){
 87957         -    pKey = &aMem[pOp->p3];
 87958         -    assert( pKey->flags & MEM_Int );
 87959         -    assert( memIsValid(pKey) );
 87960         -    REGISTER_TRACE(pOp->p3, pKey);
 87961         -    x.nKey = pKey->u.i;
 87962         -  }else{
 87963         -    assert( pOp->opcode==OP_InsertInt );
 87964         -    x.nKey = pOp->p3;
 87965         -  }
        88176  +  pKey = &aMem[pOp->p3];
        88177  +  assert( pKey->flags & MEM_Int );
        88178  +  assert( memIsValid(pKey) );
        88179  +  REGISTER_TRACE(pOp->p3, pKey);
        88180  +  x.nKey = pKey->u.i;
 87966  88181   
 87967  88182     if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 87968  88183       assert( pC->iDb>=0 );
 87969  88184       zDb = db->aDb[pC->iDb].zDbSName;
 87970  88185       pTab = pOp->p4.pTab;
 87971  88186       assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
 87972  88187     }else{
................................................................................
 88478  88693   #ifdef SQLITE_TEST
 88479  88694     sqlite3_sort_count++;
 88480  88695     sqlite3_search_count--;
 88481  88696   #endif
 88482  88697     p->aCounter[SQLITE_STMTSTATUS_SORT]++;
 88483  88698     /* Fall through into OP_Rewind */
 88484  88699   }
 88485         -/* Opcode: Rewind P1 P2 * * P5
        88700  +/* Opcode: Rewind P1 P2 * * *
 88486  88701   **
 88487  88702   ** The next use of the Rowid or Column or Next instruction for P1 
 88488  88703   ** will refer to the first entry in the database table or index.
 88489  88704   ** If the table or index is empty, jump immediately to P2.
 88490  88705   ** If the table or index is not empty, fall through to the following 
 88491  88706   ** instruction.
 88492  88707   **
 88493         -** If P5 is non-zero and the table is not empty, then the "skip-next"
 88494         -** flag is set on the cursor so that the next OP_Next instruction 
 88495         -** executed on it is a no-op.
 88496         -**
 88497  88708   ** This opcode leaves the cursor configured to move in forward order,
 88498  88709   ** from the beginning toward the end.  In other words, the cursor is
 88499  88710   ** configured to use Next, not Prev.
 88500  88711   */
 88501  88712   case OP_Rewind: {        /* jump */
 88502  88713     VdbeCursor *pC;
 88503  88714     BtCursor *pCrsr;
 88504  88715     int res;
 88505  88716   
 88506  88717     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
        88718  +  assert( pOp->p5==0 );
 88507  88719     pC = p->apCsr[pOp->p1];
 88508  88720     assert( pC!=0 );
 88509  88721     assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
 88510  88722     res = 1;
 88511  88723   #ifdef SQLITE_DEBUG
 88512  88724     pC->seekOp = OP_Rewind;
 88513  88725   #endif
................................................................................
 88514  88726     if( isSorter(pC) ){
 88515  88727       rc = sqlite3VdbeSorterRewind(pC, &res);
 88516  88728     }else{
 88517  88729       assert( pC->eCurType==CURTYPE_BTREE );
 88518  88730       pCrsr = pC->uc.pCursor;
 88519  88731       assert( pCrsr );
 88520  88732       rc = sqlite3BtreeFirst(pCrsr, &res);
 88521         -#ifndef SQLITE_OMIT_WINDOWFUNC
 88522         -    if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr);
 88523         -#endif
 88524  88733       pC->deferredMoveto = 0;
 88525  88734       pC->cacheStatus = CACHE_STALE;
 88526  88735     }
 88527  88736     if( rc ) goto abort_due_to_error;
 88528  88737     pC->nullRow = (u8)res;
 88529  88738     assert( pOp->p2>0 && pOp->p2<p->nOp );
 88530  88739     VdbeBranchTaken(res!=0,2);
................................................................................
 89526  89735       for(i=0; i<p->nMem; i++){
 89527  89736         aMem[i].pScopyFrom = 0;  /* Prevent false-positive AboutToChange() errs */
 89528  89737         aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
 89529  89738       }
 89530  89739     }
 89531  89740   #endif
 89532  89741     pOp = &aOp[-1];
 89533         -
 89534         -  break;
        89742  +  goto check_for_interrupt;
 89535  89743   }
 89536  89744   
 89537  89745   /* Opcode: Param P1 P2 * * *
 89538  89746   **
 89539  89747   ** This opcode is only ever present in sub-programs called via the 
 89540  89748   ** OP_Program instruction. Copy a value currently stored in a memory 
 89541  89749   ** cell of the calling (parent) frame to cell P2 in the current frames 
................................................................................
 89899  90107     Mem *pMem;
 89900  90108     assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
 89901  90109     assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
 89902  90110     pMem = &aMem[pOp->p1];
 89903  90111     assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
 89904  90112   #ifndef SQLITE_OMIT_WINDOWFUNC
 89905  90113     if( pOp->p3 ){
        90114  +    memAboutToChange(p, &aMem[pOp->p3]);
 89906  90115       rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
 89907  90116       pMem = &aMem[pOp->p3];
 89908  90117     }else
 89909  90118   #endif
 89910  90119     {
 89911  90120       rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
 89912  90121     }
................................................................................
 90936  91145       sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
 90937  91146     }
 90938  91147   
 90939  91148     /* This is the only way out of this procedure.  We have to
 90940  91149     ** release the mutexes on btrees that were acquired at the
 90941  91150     ** top. */
 90942  91151   vdbe_return:
 90943         -  testcase( nVmStep>0 );
        91152  +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
        91153  +  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
        91154  +    nProgressLimit += db->nProgressOps;
        91155  +    if( db->xProgress(db->pProgressArg) ){
        91156  +      nProgressLimit = 0xffffffff;
        91157  +      rc = SQLITE_INTERRUPT;
        91158  +      goto abort_due_to_error;
        91159  +    }
        91160  +  }
        91161  +#endif
 90944  91162     p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
 90945  91163     sqlite3VdbeLeave(p);
 90946  91164     assert( rc!=SQLITE_OK || nExtraDelete==0 
 90947  91165          || sqlite3_strlike("DELETE%",p->zSql,0)!=0 
 90948  91166     );
 90949  91167     return rc;
 90950  91168   
................................................................................
 95308  95526             assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 95309  95527             assert( pExpr->x.pList==0 );
 95310  95528             assert( pExpr->x.pSelect==0 );
 95311  95529             pOrig = pEList->a[j].pExpr;
 95312  95530             if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
 95313  95531               sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
 95314  95532               return WRC_Abort;
        95533  +          }
        95534  +          if( (pNC->ncFlags&NC_AllowWin)==0 && ExprHasProperty(pOrig, EP_Win) ){
        95535  +            sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
        95536  +            return WRC_Abort;
 95315  95537             }
 95316  95538             if( sqlite3ExprVectorSize(pOrig)!=1 ){
 95317  95539               sqlite3ErrorMsg(pParse, "row value misused");
 95318  95540               return WRC_Abort;
 95319  95541             }
 95320  95542             resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
 95321  95543             cnt = 1;
................................................................................
 95599  95821         int no_such_func = 0;       /* True if no such function exists */
 95600  95822         int wrong_num_args = 0;     /* True if wrong number of arguments */
 95601  95823         int is_agg = 0;             /* True if is an aggregate function */
 95602  95824         int nId;                    /* Number of characters in function name */
 95603  95825         const char *zId;            /* The function name. */
 95604  95826         FuncDef *pDef;              /* Information about the function */
 95605  95827         u8 enc = ENC(pParse->db);   /* The database encoding */
        95828  +      int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
 95606  95829   
 95607  95830         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 95608  95831         zId = pExpr->u.zToken;
 95609  95832         nId = sqlite3Strlen30(zId);
 95610  95833         pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
 95611  95834         if( pDef==0 ){
 95612  95835           pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
................................................................................
 95720  95943             pNC->nErr++;
 95721  95944           }else if( wrong_num_args ){
 95722  95945             sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 95723  95946                  nId, zId);
 95724  95947             pNC->nErr++;
 95725  95948           }
 95726  95949           if( is_agg ){
        95950  +          /* Window functions may not be arguments of aggregate functions.
        95951  +          ** Or arguments of other window functions. But aggregate functions
        95952  +          ** may be arguments for window functions.  */
 95727  95953   #ifndef SQLITE_OMIT_WINDOWFUNC
 95728         -          pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg);
        95954  +          pNC->ncFlags &= ~(NC_AllowWin | (!pExpr->y.pWin ? NC_AllowAgg : 0));
 95729  95955   #else
 95730  95956             pNC->ncFlags &= ~NC_AllowAgg;
 95731  95957   #endif
 95732  95958           }
 95733  95959         }
 95734  95960         sqlite3WalkExprList(pWalker, pList);
 95735  95961         if( is_agg ){
................................................................................
 95742  95968             sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
 95743  95969             if( 0==pSel->pWin 
 95744  95970              || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) 
 95745  95971             ){
 95746  95972               pExpr->y.pWin->pNextWin = pSel->pWin;
 95747  95973               pSel->pWin = pExpr->y.pWin;
 95748  95974             }
 95749         -          pNC->ncFlags |= NC_AllowWin;
        95975  +          pNC->ncFlags |= NC_HasWin;
 95750  95976           }else
 95751  95977   #endif /* SQLITE_OMIT_WINDOWFUNC */
 95752  95978           {
 95753  95979             NameContext *pNC2 = pNC;
 95754  95980             pExpr->op = TK_AGG_FUNCTION;
 95755  95981             pExpr->op2 = 0;
 95756  95982             while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
................................................................................
 95760  95986             assert( pDef!=0 );
 95761  95987             if( pNC2 ){
 95762  95988               assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
 95763  95989               testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
 95764  95990               pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
 95765  95991   
 95766  95992             }
 95767         -          pNC->ncFlags |= NC_AllowAgg;
 95768  95993           }
        95994  +        pNC->ncFlags |= savedAllowFlags;
 95769  95995         }
 95770  95996         /* FIX ME:  Compute pExpr->affinity based on the expected return
 95771  95997         ** type of the function 
 95772  95998         */
 95773  95999         return WRC_Prune;
 95774  96000       }
 95775  96001   #ifndef SQLITE_OMIT_SUBQUERY
................................................................................
 96117  96343         resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
 96118  96344                      zType,0);
 96119  96345       }
 96120  96346     }
 96121  96347     return 0;
 96122  96348   }
 96123  96349   
        96350  +#ifndef SQLITE_OMIT_WINDOWFUNC
        96351  +/*
        96352  +** Walker callback for resolveRemoveWindows().
        96353  +*/
        96354  +static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
        96355  +  if( ExprHasProperty(pExpr, EP_WinFunc) ){
        96356  +    Window **pp;
        96357  +    for(pp=&pWalker->u.pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
        96358  +      if( *pp==pExpr->y.pWin ){
        96359  +        *pp = (*pp)->pNextWin;
        96360  +        break;
        96361  +      }    
        96362  +    }
        96363  +  }
        96364  +  return WRC_Continue;
        96365  +}
        96366  +
        96367  +/*
        96368  +** Remove any Window objects owned by the expression pExpr from the
        96369  +** Select.pWin list of Select object pSelect.
        96370  +*/
        96371  +static void resolveRemoveWindows(Select *pSelect, Expr *pExpr){
        96372  +  Walker sWalker;
        96373  +  memset(&sWalker, 0, sizeof(Walker));
        96374  +  sWalker.xExprCallback = resolveRemoveWindowsCb;
        96375  +  sWalker.u.pSelect = pSelect;
        96376  +  sqlite3WalkExpr(&sWalker, pExpr);
        96377  +}
        96378  +#else
        96379  +# define resolveRemoveWindows(x,y)
        96380  +#endif
        96381  +
 96124  96382   /*
 96125  96383   ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
 96126  96384   ** The Name context of the SELECT statement is pNC.  zType is either
 96127  96385   ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
 96128  96386   **
 96129  96387   ** This routine resolves each term of the clause into an expression.
 96130  96388   ** If the order-by term is an integer I between 1 and N (where N is the
................................................................................
 96183  96441       /* Otherwise, treat the ORDER BY term as an ordinary expression */
 96184  96442       pItem->u.x.iOrderByCol = 0;
 96185  96443       if( sqlite3ResolveExprNames(pNC, pE) ){
 96186  96444         return 1;
 96187  96445       }
 96188  96446       for(j=0; j<pSelect->pEList->nExpr; j++){
 96189  96447         if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
 96190         -#ifndef SQLITE_OMIT_WINDOWFUNC
 96191         -        if( ExprHasProperty(pE, EP_WinFunc) ){
 96192         -          /* Since this window function is being changed into a reference
 96193         -          ** to the same window function the result set, remove the instance
 96194         -          ** of this window function from the Select.pWin list. */
 96195         -          Window **pp;
 96196         -          for(pp=&pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
 96197         -            if( *pp==pE->y.pWin ){
 96198         -              *pp = (*pp)->pNextWin;
 96199         -            }    
 96200         -          }
 96201         -        }
 96202         -#endif
        96448  +        /* Since this expresion is being changed into a reference
        96449  +        ** to an identical expression in the result set, remove all Window
        96450  +        ** objects belonging to the expression from the Select.pWin list. */
        96451  +        resolveRemoveWindows(pSelect, pE);
 96203  96452           pItem->u.x.iOrderByCol = j+1;
 96204  96453         }
 96205  96454       }
 96206  96455     }
 96207  96456     return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
 96208  96457   }
 96209  96458   
................................................................................
 96275  96524         p->pOrderBy = 0;
 96276  96525       }
 96277  96526     
 96278  96527       /* Recursively resolve names in all subqueries
 96279  96528       */
 96280  96529       for(i=0; i<p->pSrc->nSrc; i++){
 96281  96530         struct SrcList_item *pItem = &p->pSrc->a[i];
 96282         -      if( pItem->pSelect ){
        96531  +      if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
 96283  96532           NameContext *pNC;         /* Used to iterate name contexts */
 96284  96533           int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
 96285  96534           const char *zSavedContext = pParse->zAuthContext;
 96286  96535   
 96287  96536           /* Count the total number of references to pOuterNC and all of its
 96288  96537           ** parent contexts. After resolving references to expressions in
 96289  96538           ** pItem->pSelect, check if this value has changed. If so, then
................................................................................
 96407  96656             sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
 96408  96657                 "the GROUP BY clause");
 96409  96658             return WRC_Abort;
 96410  96659           }
 96411  96660         }
 96412  96661       }
 96413  96662   
        96663  +#ifndef SQLITE_OMIT_WINDOWFUNC
 96414  96664       if( IN_RENAME_OBJECT ){
 96415  96665         Window *pWin;
 96416  96666         for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
 96417  96667           if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
 96418  96668            || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
 96419  96669           ){
 96420  96670             return WRC_Abort;
 96421  96671           }
 96422  96672         }
 96423  96673       }
        96674  +#endif
 96424  96675   
 96425  96676       /* If this is part of a compound SELECT, check that it has the right
 96426  96677       ** number of expressions in the select list. */
 96427  96678       if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
 96428  96679         sqlite3SelectWrongNumTermsError(pParse, p->pNext);
 96429  96680         return WRC_Abort;
 96430  96681       }
................................................................................
 96497  96748     NameContext *pNC,       /* Namespace to resolve expressions in. */
 96498  96749     Expr *pExpr             /* The expression to be analyzed. */
 96499  96750   ){
 96500  96751     u16 savedHasAgg;
 96501  96752     Walker w;
 96502  96753   
 96503  96754     if( pExpr==0 ) return SQLITE_OK;
 96504         -  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
 96505         -  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
        96755  +  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
        96756  +  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
 96506  96757     w.pParse = pNC->pParse;
 96507  96758     w.xExprCallback = resolveExprStep;
 96508  96759     w.xSelectCallback = resolveSelectStep;
 96509  96760     w.xSelectCallback2 = 0;
 96510  96761     w.u.pNC = pNC;
 96511  96762   #if SQLITE_MAX_EXPR_DEPTH>0
 96512  96763     w.pParse->nHeight += pExpr->nHeight;
................................................................................
 96514  96765       return SQLITE_ERROR;
 96515  96766     }
 96516  96767   #endif
 96517  96768     sqlite3WalkExpr(&w, pExpr);
 96518  96769   #if SQLITE_MAX_EXPR_DEPTH>0
 96519  96770     w.pParse->nHeight -= pExpr->nHeight;
 96520  96771   #endif
 96521         -  if( pNC->ncFlags & NC_HasAgg ){
 96522         -    ExprSetProperty(pExpr, EP_Agg);
 96523         -  }
        96772  +  assert( EP_Agg==NC_HasAgg );
        96773  +  assert( EP_Win==NC_HasWin );
        96774  +  testcase( pNC->ncFlags & NC_HasAgg );
        96775  +  testcase( pNC->ncFlags & NC_HasWin );
        96776  +  ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
 96524  96777     pNC->ncFlags |= savedHasAgg;
 96525  96778     return pNC->nErr>0 || w.pParse->nErr>0;
 96526  96779   }
 96527  96780   
 96528  96781   /*
 96529  96782   ** Resolve all names for all expression in an expression list.  This is
 96530  96783   ** just like sqlite3ResolveExprNames() except that it works for an expression
................................................................................
 97472  97725     if( op==TK_AND && pParse->nErr==0 && !IN_RENAME_OBJECT ){
 97473  97726       /* Take advantage of short-circuit false optimization for AND */
 97474  97727       p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
 97475  97728     }else{
 97476  97729       p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
 97477  97730       if( p ){
 97478  97731         memset(p, 0, sizeof(Expr));
 97479         -      p->op = op & TKFLG_MASK;
        97732  +      p->op = op & 0xff;
 97480  97733         p->iAgg = -1;
 97481  97734       }
 97482  97735       sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
 97483  97736     }
 97484  97737     if( p ) {
 97485  97738       sqlite3ExprCheckHeight(pParse, p->nHeight);
 97486  97739     }
................................................................................
 99167  99420       ** We will have to generate an ephemeral table to do the job.
 99168  99421       */
 99169  99422       u32 savedNQueryLoop = pParse->nQueryLoop;
 99170  99423       int rMayHaveNull = 0;
 99171  99424       eType = IN_INDEX_EPH;
 99172  99425       if( inFlags & IN_INDEX_LOOP ){
 99173  99426         pParse->nQueryLoop = 0;
 99174         -      if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
 99175         -        eType = IN_INDEX_ROWID;
 99176         -      }
 99177  99427       }else if( prRhsHasNull ){
 99178  99428         *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
 99179  99429       }
 99180  99430       assert( pX->op==TK_IN );
 99181         -    sqlite3CodeRhsOfIN(pParse, pX, iTab, eType==IN_INDEX_ROWID);
        99431  +    sqlite3CodeRhsOfIN(pParse, pX, iTab);
 99182  99432       if( rMayHaveNull ){
 99183  99433         sqlite3SetHasNullFlag(v, iTab, rMayHaveNull);
 99184  99434       }
 99185  99435       pParse->nQueryLoop = savedNQueryLoop;
 99186  99436     }
 99187  99437   
 99188  99438     if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
................................................................................
 99275  99525   **
 99276  99526   ** The pExpr parameter is the IN operator.  The cursor number for the
 99277  99527   ** constructed ephermeral table is returned.  The first time the ephemeral
 99278  99528   ** table is computed, the cursor number is also stored in pExpr->iTable,
 99279  99529   ** however the cursor number returned might not be the same, as it might
 99280  99530   ** have been duplicated using OP_OpenDup.
 99281  99531   **
 99282         -** If parameter isRowid is non-zero, then LHS of the IN operator is guaranteed
 99283         -** to be a non-null integer. In this case, the ephemeral table can be an
 99284         -** table B-Tree that keyed by only integers.  The more general cases uses
 99285         -** an index B-Tree which can have arbitrary keys, but is slower to both
 99286         -** read and write.
 99287         -**
 99288  99532   ** If the LHS expression ("x" in the examples) is a column value, or
 99289  99533   ** the SELECT statement returns a column value, then the affinity of that
 99290  99534   ** column is used to build the index keys. If both 'x' and the
 99291  99535   ** SELECT... statement are columns, then numeric affinity is used
 99292  99536   ** if either column has NUMERIC or INTEGER affinity. If neither
 99293  99537   ** 'x' nor the SELECT... statement are columns, then numeric affinity
 99294  99538   ** is used.
 99295  99539   */
 99296  99540   SQLITE_PRIVATE void sqlite3CodeRhsOfIN(
 99297  99541     Parse *pParse,          /* Parsing context */
 99298  99542     Expr *pExpr,            /* The IN operator */
 99299         -  int iTab,               /* Use this cursor number */
 99300         -  int isRowid             /* If true, LHS is a rowid */
        99543  +  int iTab                /* Use this cursor number */
 99301  99544   ){
 99302  99545     int addrOnce = 0;           /* Address of the OP_Once instruction at top */
 99303  99546     int addr;                   /* Address of OP_OpenEphemeral instruction */
 99304  99547     Expr *pLeft;                /* the LHS of the IN operator */
 99305  99548     KeyInfo *pKeyInfo = 0;      /* Key information */
 99306  99549     int nVal;                   /* Size of vector pLeft */
 99307  99550     Vdbe *v;                    /* The prepared statement under construction */
................................................................................
 99346  99589   
 99347  99590       addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
 99348  99591     }
 99349  99592   
 99350  99593     /* Check to see if this is a vector IN operator */
 99351  99594     pLeft = pExpr->pLeft;
 99352  99595     nVal = sqlite3ExprVectorSize(pLeft);
 99353         -  assert( !isRowid || nVal==1 );
 99354  99596   
 99355  99597     /* Construct the ephemeral table that will contain the content of
 99356  99598     ** RHS of the IN operator.
 99357  99599     */
 99358  99600     pExpr->iTable = iTab;
 99359         -  addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, 
 99360         -      pExpr->iTable, (isRowid?0:nVal));
        99601  +  addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
 99361  99602   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 99362  99603     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 99363  99604       VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
 99364  99605     }else{
 99365  99606       VdbeComment((v, "RHS of IN operator"));
 99366  99607     }
 99367  99608   #endif
 99368         -  pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
        99609  +  pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
 99369  99610   
 99370  99611     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 99371  99612       /* Case 1:     expr IN (SELECT ...)
 99372  99613       **
 99373  99614       ** Generate code to write the results of the select into the temporary
 99374  99615       ** table allocated and opened above.
 99375  99616       */
 99376  99617       Select *pSelect = pExpr->x.pSelect;
 99377  99618       ExprList *pEList = pSelect->pEList;
 99378  99619   
 99379  99620       ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",
 99380  99621           addrOnce?"":"CORRELATED ", pSelect->selId
 99381  99622       ));
 99382         -    assert( !isRowid );
 99383  99623       /* If the LHS and RHS of the IN operator do not match, that
 99384  99624       ** error will have been caught long before we reach this point. */
 99385  99625       if( ALWAYS(pEList->nExpr==nVal) ){
 99386  99626         SelectDest dest;
 99387  99627         int i;
 99388  99628         sqlite3SelectDestInit(&dest, SRT_Set, iTab);
 99389  99629         dest.zAffSdst = exprINAffinity(pParse, pExpr);
................................................................................
 99428  99668         assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 99429  99669         pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 99430  99670       }
 99431  99671   
 99432  99672       /* Loop through each expression in <exprlist>. */
 99433  99673       r1 = sqlite3GetTempReg(pParse);
 99434  99674       r2 = sqlite3GetTempReg(pParse);
 99435         -    if( isRowid ) sqlite3VdbeAddOp4(v, OP_Blob, 0, r2, 0, "", P4_STATIC);
 99436  99675       for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
 99437  99676         Expr *pE2 = pItem->pExpr;
 99438         -      int iValToIns;
 99439  99677   
 99440  99678         /* If the expression is not constant then we will need to
 99441  99679         ** disable the test that was generated above that makes sure
 99442  99680         ** this code only executes once.  Because for a non-constant
 99443  99681         ** expression we need to rerun this code each time.
 99444  99682         */
 99445  99683         if( addrOnce && !sqlite3ExprIsConstant(pE2) ){
 99446  99684           sqlite3VdbeChangeToNoop(v, addrOnce);
 99447  99685           addrOnce = 0;
 99448  99686         }
 99449  99687   
 99450  99688         /* Evaluate the expression and insert it into the temp table */
 99451         -      if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
 99452         -        sqlite3VdbeAddOp3(v, OP_InsertInt, iTab, r2, iValToIns);
 99453         -      }else{
 99454         -        r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
 99455         -        if( isRowid ){
 99456         -          sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
 99457         -                            sqlite3VdbeCurrentAddr(v)+2);
 99458         -          VdbeCoverage(v);
 99459         -          sqlite3VdbeAddOp3(v, OP_Insert, iTab, r2, r3);
 99460         -        }else{
 99461         -          sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
 99462         -          sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r3, 1);
 99463         -        }
 99464         -      }
        99689  +      r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
        99690  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
        99691  +      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r3, 1);
 99465  99692       }
 99466  99693       sqlite3ReleaseTempReg(pParse, r1);
 99467  99694       sqlite3ReleaseTempReg(pParse, r2);
 99468  99695     }
 99469  99696     if( pKeyInfo ){
 99470  99697       sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
 99471  99698     }
................................................................................
101673 101900   ** an ordinary JOIN.  The p argument is the WHERE clause.  If the WHERE
101674 101901   ** clause requires that some column of the right table of the LEFT JOIN
101675 101902   ** be non-NULL, then the LEFT JOIN can be safely converted into an
101676 101903   ** ordinary join.
101677 101904   */
101678 101905   SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
101679 101906     Walker w;
       101907  +  p = sqlite3ExprSkipCollate(p);
       101908  +  while( p ){
       101909  +    if( p->op==TK_NOTNULL ){
       101910  +      p = p->pLeft;
       101911  +    }else if( p->op==TK_AND ){
       101912  +      if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
       101913  +      p = p->pRight;
       101914  +    }else{
       101915  +      break;
       101916  +    }
       101917  +  }
101680 101918     w.xExprCallback = impliesNotNullRow;
101681 101919     w.xSelectCallback = 0;
101682 101920     w.xSelectCallback2 = 0;
101683 101921     w.eCode = 0;
101684 101922     w.u.iCur = iTab;
101685 101923     sqlite3WalkExpr(&w, p);
101686 101924     return w.eCode;
................................................................................
102254 102492       pVTab = sqlite3GetVTable(db, pTab);
102255 102493       if( pVTab->pVtab->pModule->xRename==0 ){
102256 102494         pVTab = 0;
102257 102495       }
102258 102496     }
102259 102497   #endif
102260 102498   
102261         -  /* Begin a transaction for database iDb. 
102262         -  ** Then modify the schema cookie (since the ALTER TABLE modifies the
102263         -  ** schema). Open a statement transaction if the table is a virtual
102264         -  ** table.
102265         -  */
       102499  +  /* Begin a transaction for database iDb. Then modify the schema cookie
       102500  +  ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
       102501  +  ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the 
       102502  +  ** nested SQL may raise an exception.  */
102266 102503     v = sqlite3GetVdbe(pParse);
102267 102504     if( v==0 ){
102268 102505       goto exit_rename_table;
102269 102506     }
       102507  +  sqlite3MayAbort(pParse);
102270 102508   
102271 102509     /* figure out how many UTF-8 characters are in zName */
102272 102510     zTabName = pTab->zName;
102273 102511     nTabName = sqlite3Utf8CharLen(zTabName, -1);
102274 102512   
102275 102513     /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
102276 102514     ** the schema to use the new table name.  */
................................................................................
102331 102569     ** SQLite tables) that are identified by the name of the virtual table.
102332 102570     */
102333 102571   #ifndef SQLITE_OMIT_VIRTUALTABLE
102334 102572     if( pVTab ){
102335 102573       int i = ++pParse->nMem;
102336 102574       sqlite3VdbeLoadString(v, i, zName);
102337 102575       sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
102338         -    sqlite3MayAbort(pParse);
102339 102576     }
102340 102577   #endif
102341 102578   
102342 102579     renameReloadSchema(pParse, iDb);
102343 102580     renameTestSchema(pParse, zDb, iDb==1);
102344 102581   
102345 102582   exit_rename_table:
................................................................................
102652 102889       goto exit_rename_column;
102653 102890     }
102654 102891   
102655 102892     /* Do the rename operation using a recursive UPDATE statement that
102656 102893     ** uses the sqlite_rename_column() SQL function to compute the new
102657 102894     ** CREATE statement text for the sqlite_master table.
102658 102895     */
       102896  +  sqlite3MayAbort(pParse);
102659 102897     zNew = sqlite3NameFromToken(db, pNew);
102660 102898     if( !zNew ) goto exit_rename_column;
102661 102899     assert( pNew->n>0 );
102662 102900     bQuote = sqlite3Isquote(pNew->z[0]);
102663 102901     sqlite3NestedParse(pParse, 
102664 102902         "UPDATE \"%w\".%s SET "
102665 102903         "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
................................................................................
105906 106144     ** remove the entry from the db->aDb[] array. i.e. put everything back the
105907 106145     ** way we found it.
105908 106146     */
105909 106147     if( rc==SQLITE_OK ){
105910 106148       sqlite3BtreeEnterAll(db);
105911 106149       db->init.iDb = 0;
105912 106150       db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
105913         -    rc = sqlite3Init(db, &zErrDyn);
       106151  +    if( !REOPEN_AS_MEMDB(db) ){
       106152  +      rc = sqlite3Init(db, &zErrDyn);
       106153  +    }
105914 106154       sqlite3BtreeLeaveAll(db);
105915 106155       assert( zErrDyn==0 || rc!=SQLITE_OK );
105916 106156     }
105917 106157   #ifdef SQLITE_USER_AUTHENTICATION
105918 106158     if( rc==SQLITE_OK ){
105919 106159       u8 newAuth = 0;
105920 106160       rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
................................................................................
106840 107080   
106841 107081     if( pParse->nErr ) return;
106842 107082     assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
106843 107083     va_start(ap, zFormat);
106844 107084     zSql = sqlite3VMPrintf(db, zFormat, ap);
106845 107085     va_end(ap);
106846 107086     if( zSql==0 ){
106847         -    return;   /* A malloc must have failed */
       107087  +    /* This can result either from an OOM or because the formatted string
       107088  +    ** exceeds SQLITE_LIMIT_LENGTH.  In the latter case, we need to set
       107089  +    ** an error */
       107090  +    if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
       107091  +    pParse->nErr++;
       107092  +    return;
106848 107093     }
106849 107094     pParse->nested++;
106850 107095     memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
106851 107096     memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
106852 107097     sqlite3RunParser(pParse, zSql, &zErrMsg);
106853 107098     sqlite3DbFree(db, zErrMsg);
106854 107099     sqlite3DbFree(db, zSql);
................................................................................
107980 108225     }
107981 108226     if( nTerm==1
107982 108227      && pCol
107983 108228      && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
107984 108229      && sortOrder!=SQLITE_SO_DESC
107985 108230     ){
107986 108231       if( IN_RENAME_OBJECT && pList ){
107987         -      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr);
       108232  +      Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
       108233  +      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
107988 108234       }
107989 108235       pTab->iPKey = iCol;
107990 108236       pTab->keyConf = (u8)onError;
107991 108237       assert( autoInc==0 || autoInc==1 );
107992 108238       pTab->tabFlags |= autoInc*TF_Autoincrement;
107993 108239       if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
107994 108240     }else if( autoInc ){
................................................................................
108401 108647       sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
108402 108648                          SQLITE_IDXTYPE_PRIMARYKEY);
108403 108649       if( db->mallocFailed || pParse->nErr ) return;
108404 108650       pPk = sqlite3PrimaryKeyIndex(pTab);
108405 108651       pTab->iPKey = -1;
108406 108652     }else{
108407 108653       pPk = sqlite3PrimaryKeyIndex(pTab);
       108654  +    assert( pPk!=0 );
108408 108655   
108409 108656       /*
108410 108657       ** Remove all redundant columns from the PRIMARY KEY.  For example, change
108411 108658       ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
108412 108659       ** code assumes the PRIMARY KEY contains no repeated columns.
108413 108660       */
108414 108661       for(i=j=1; i<pPk->nKeyCol; i++){
................................................................................
108570 108817         sqlite3ErrorMsg(pParse, "");
108571 108818         return;
108572 108819       }
108573 108820       p->tnum = db->init.newTnum;
108574 108821       if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
108575 108822     }
108576 108823   
       108824  +  assert( (p->tabFlags & TF_HasPrimaryKey)==0
       108825  +       || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
       108826  +  assert( (p->tabFlags & TF_HasPrimaryKey)!=0
       108827  +       || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
       108828  +
108577 108829     /* Special processing for WITHOUT ROWID Tables */
108578 108830     if( tabOpts & TF_WithoutRowid ){
108579 108831       if( (p->tabFlags & TF_Autoincrement) ){
108580 108832         sqlite3ErrorMsg(pParse,
108581 108833             "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
108582 108834         return;
108583 108835       }
................................................................................
109723 109975     }
109724 109976     pDb = &db->aDb[iDb];
109725 109977   
109726 109978     assert( pTab!=0 );
109727 109979     assert( pParse->nErr==0 );
109728 109980     if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
109729 109981          && db->init.busy==0
       109982  +       && pTblName!=0
109730 109983   #if SQLITE_USER_AUTHENTICATION
109731 109984          && sqlite3UserAuthTable(pTab->zName)==0
109732 109985   #endif
109733 109986   #ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
109734 109987          && sqlite3StrICmp(&pTab->zName[7],"master")!=0
109735 109988   #endif
109736         -       && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
109737 109989    ){
109738 109990       sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
109739 109991       goto exit_create_index;
109740 109992     }
109741 109993   #ifndef SQLITE_OMIT_VIEW
109742 109994     if( pTab->pSelect ){
109743 109995       sqlite3ErrorMsg(pParse, "views may not be indexed");
................................................................................
109833 110085       pList = sqlite3ExprListAppend(pParse, 0,
109834 110086                 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
109835 110087       if( pList==0 ) goto exit_create_index;
109836 110088       assert( pList->nExpr==1 );
109837 110089       sqlite3ExprListSetSortOrder(pList, sortOrder);
109838 110090     }else{
109839 110091       sqlite3ExprListCheckLength(pParse, pList, "index");
       110092  +    if( pParse->nErr ) goto exit_create_index;
109840 110093     }
109841 110094   
109842 110095     /* Figure out how many bytes of space are required to store explicitly
109843 110096     ** specified collation sequence names.
109844 110097     */
109845 110098     for(i=0; i<pList->nExpr; i++){
109846 110099       Expr *pExpr = pList->a[i].pExpr;
................................................................................
109851 110104     }
109852 110105   
109853 110106     /* 
109854 110107     ** Allocate the index structure. 
109855 110108     */
109856 110109     nName = sqlite3Strlen30(zName);
109857 110110     nExtraCol = pPk ? pPk->nKeyCol : 1;
       110111  +  assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
109858 110112     pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
109859 110113                                         nName + nExtra + 1, &zExtra);
109860 110114     if( db->mallocFailed ){
109861 110115       goto exit_create_index;
109862 110116     }
109863 110117     assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
109864 110118     assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
................................................................................
110458 110712     assert( pSrc!=0 );
110459 110713     assert( iStart<=pSrc->nSrc );
110460 110714   
110461 110715     /* Allocate additional space if needed */
110462 110716     if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
110463 110717       SrcList *pNew;
110464 110718       int nAlloc = pSrc->nSrc*2+nExtra;
110465         -    int nGot;
110466 110719       sqlite3 *db = pParse->db;
110467 110720   
110468 110721       if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
110469 110722         sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
110470 110723                         SQLITE_MAX_SRCLIST);
110471 110724         return 0;
110472 110725       }
................................................................................
110474 110727       pNew = sqlite3DbRealloc(db, pSrc,
110475 110728                  sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
110476 110729       if( pNew==0 ){
110477 110730         assert( db->mallocFailed );
110478 110731         return 0;
110479 110732       }
110480 110733       pSrc = pNew;
110481         -    nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
110482         -    pSrc->nAlloc = nGot;
       110734  +    pSrc->nAlloc = nAlloc;
110483 110735     }
110484 110736   
110485 110737     /* Move existing slots that come after the newly inserted slots
110486 110738     ** out of the way */
110487 110739     for(i=pSrc->nSrc-1; i>=iStart; i--){
110488 110740       pSrc->a[i+nExtra] = pSrc->a[i];
110489 110741     }
................................................................................
114523 114775   */
114524 114776   static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
114525 114777     FuncDef *pDef;
114526 114778     pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0);
114527 114779     if( ALWAYS(pDef) ){
114528 114780       pDef->funcFlags |= flagVal;
114529 114781     }
       114782  +  pDef = sqlite3FindFunction(db, zName, 3, SQLITE_UTF8, 0);
       114783  +  if( pDef ){
       114784  +    pDef->funcFlags |= flagVal;
       114785  +  }
114530 114786   }
114531 114787   
114532 114788   /*
114533 114789   ** Register the built-in LIKE and GLOB functions.  The caseSensitive
114534 114790   ** parameter determines whether or not the LIKE operator is case
114535 114791   ** sensitive.  GLOB is always case sensitive.
114536 114792   */
................................................................................
117845 118101           sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
117846 118102           VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
117847 118103         }
117848 118104       }
117849 118105       sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
117850 118106       VdbeComment((v, "for %s", pIdx->zName));
117851 118107   #ifdef SQLITE_ENABLE_NULL_TRIM
117852         -    if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
       118108  +    if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
       118109  +      sqlite3SetMakeRecordP5(v, pIdx->pTable);
       118110  +    }
117853 118111   #endif
117854 118112   
117855 118113       /* In an UPDATE operation, if this index is the PRIMARY KEY index 
117856 118114       ** of a WITHOUT ROWID table and there has been no change the
117857 118115       ** primary key, then no collision is possible.  The collision detection
117858 118116       ** logic below can all be skipped. */
117859 118117       if( isUpdate && pPk==pIdx && pkChng==0 ){
................................................................................
118095 118353       pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
118096 118354       if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
118097 118355         assert( pParse->nested==0 );
118098 118356         pik_flags |= OPFLAG_NCHANGE;
118099 118357         pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
118100 118358   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
118101 118359         if( update_flags==0 ){
118102         -        sqlite3VdbeAddOp4(v, OP_InsertInt, 
118103         -            iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
       118360  +        int r = sqlite3GetTempReg(pParse);
       118361  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, r);
       118362  +        sqlite3VdbeAddOp4(v, OP_Insert, 
       118363  +            iIdxCur+i, aRegIdx[i], r, (char*)pTab, P4_TABLE
118104 118364           );
118105 118365           sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
       118366  +        sqlite3ReleaseTempReg(pParse, r);
118106 118367         }
118107 118368   #endif
118108 118369       }
118109 118370       sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
118110 118371                            aRegIdx[i]+1,
118111 118372                            pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
118112 118373       sqlite3VdbeChangeP5(v, pik_flags);
................................................................................
118446 118707       }
118447 118708       for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
118448 118709         if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
118449 118710       }
118450 118711       if( pSrcIdx==0 ){
118451 118712         return 0;    /* pDestIdx has no corresponding index in pSrc */
118452 118713       }
       118714  +    if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
       118715  +         && sqlite3FaultSim(411)==SQLITE_OK ){
       118716  +      /* The sqlite3FaultSim() call allows this corruption test to be
       118717  +      ** bypassed during testing, in order to exercise other corruption tests
       118718  +      ** further downstream. */
       118719  +      return 0;   /* Corrupt schema - two indexes on the same btree */
       118720  +    }
118453 118721     }
118454 118722   #ifndef SQLITE_OMIT_CHECK
118455 118723     if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
118456 118724       return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
118457 118725     }
118458 118726   #endif
118459 118727   #ifndef SQLITE_OMIT_FOREIGN_KEY
................................................................................
118523 118791         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
118524 118792         sqlite3VdbeVerifyAbortable(v, onError);
118525 118793         addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
118526 118794         VdbeCoverage(v);
118527 118795         sqlite3RowidConstraint(pParse, onError, pDest);
118528 118796         sqlite3VdbeJumpHere(v, addr2);
118529 118797         autoIncStep(pParse, regAutoinc, regRowid);
118530         -    }else if( pDest->pIndex==0 ){
       118798  +    }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
118531 118799         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
118532 118800       }else{
118533 118801         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
118534 118802         assert( (pDest->tabFlags & TF_Autoincrement)==0 );
118535 118803       }
118536 118804       sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
118537 118805       if( db->mDbFlags & DBFLAG_Vacuum ){
................................................................................
118586 118854           if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
118587 118855         }
118588 118856         if( i==pSrcIdx->nColumn ){
118589 118857           idxInsFlags = OPFLAG_USESEEKRESULT;
118590 118858           sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
118591 118859         }
118592 118860       }
118593         -    if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
       118861  +    if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
118594 118862         idxInsFlags |= OPFLAG_NCHANGE;
118595 118863       }
118596 118864       sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
118597 118865       sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
118598 118866       sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
118599 118867       sqlite3VdbeJumpHere(v, addr1);
118600 118868       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
................................................................................
119098 119366                               void (*xStep)(sqlite3_context*,int,sqlite3_value**),
119099 119367                               void (*xFinal)(sqlite3_context*),
119100 119368                               void (*xValue)(sqlite3_context*),
119101 119369                               void (*xInv)(sqlite3_context*,int,sqlite3_value**),
119102 119370                               void(*xDestroy)(void*));
119103 119371     /* Version 3.26.0 and later */
119104 119372     const char *(*normalized_sql)(sqlite3_stmt*);
       119373  +  /* Version 3.28.0 and later */
       119374  +  int (*stmt_isexplain)(sqlite3_stmt*);
       119375  +  int (*value_frombind)(sqlite3_value*);
119105 119376   };
119106 119377   
119107 119378   /*
119108 119379   ** This is the function signature used for all extension entry points.  It
119109 119380   ** is also defined in the file "loadext.c".
119110 119381   */
119111 119382   typedef int (*sqlite3_loadext_entry)(
................................................................................
119387 119658   #define sqlite3_str_errcode            sqlite3_api->str_errcode
119388 119659   #define sqlite3_str_length             sqlite3_api->str_length
119389 119660   #define sqlite3_str_value              sqlite3_api->str_value
119390 119661   /* Version 3.25.0 and later */
119391 119662   #define sqlite3_create_window_function sqlite3_api->create_window_function
119392 119663   /* Version 3.26.0 and later */
119393 119664   #define sqlite3_normalized_sql         sqlite3_api->normalized_sql
       119665  +/* Version 3.28.0 and later */
       119666  +#define sqlite3_stmt_isexplain         sqlite3_api->isexplain
       119667  +#define sqlite3_value_frombind         sqlite3_api->frombind
119394 119668   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
119395 119669   
119396 119670   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
119397 119671     /* This case when the file really is being compiled as a loadable 
119398 119672     ** extension */
119399 119673   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
119400 119674   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
119846 120120     sqlite3_str_errcode,
119847 120121     sqlite3_str_length,
119848 120122     sqlite3_str_value,
119849 120123     /* Version 3.25.0 and later */
119850 120124     sqlite3_create_window_function,
119851 120125     /* Version 3.26.0 and later */
119852 120126   #ifdef SQLITE_ENABLE_NORMALIZE
119853         -  sqlite3_normalized_sql
       120127  +  sqlite3_normalized_sql,
119854 120128   #else
119855         -  0
       120129  +  0,
119856 120130   #endif
       120131  +  /* Version 3.28.0 and later */
       120132  +  sqlite3_stmt_isexplain,
       120133  +  sqlite3_value_frombind
119857 120134   };
119858 120135   
119859 120136   /*
119860 120137   ** Attempt to load an SQLite extension library contained in the file
119861 120138   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
119862 120139   ** default entry point name (sqlite3_extension_init) is used.  Use
119863 120140   ** of the default name is recommended.
................................................................................
120461 120738    {/* zName:     */ "compile_options",
120462 120739     /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
120463 120740     /* ePragFlg:  */ PragFlg_Result0,
120464 120741     /* ColNames:  */ 0, 0,
120465 120742     /* iArg:      */ 0 },
120466 120743   #endif
120467 120744   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120468         -#if !defined(SQLITE_OMIT_DEPRECATED)
120469 120745    {/* zName:     */ "count_changes",
120470 120746     /* ePragTyp:  */ PragTyp_FLAG,
120471 120747     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
120472 120748     /* ColNames:  */ 0, 0,
120473 120749     /* iArg:      */ SQLITE_CountRows },
120474         -#endif
120475 120750   #endif
120476 120751   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
120477 120752    {/* zName:     */ "data_store_directory",
120478 120753     /* ePragTyp:  */ PragTyp_DATA_STORE_DIRECTORY,
120479 120754     /* ePragFlg:  */ PragFlg_NoColumns1,
120480 120755     /* ColNames:  */ 0, 0,
120481 120756     /* iArg:      */ 0 },
................................................................................
120490 120765   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
120491 120766    {/* zName:     */ "database_list",
120492 120767     /* ePragTyp:  */ PragTyp_DATABASE_LIST,
120493 120768     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
120494 120769     /* ColNames:  */ 35, 3,
120495 120770     /* iArg:      */ 0 },
120496 120771   #endif
120497         -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
120498         -#if !defined(SQLITE_OMIT_DEPRECATED)
       120772  +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
120499 120773    {/* zName:     */ "default_cache_size",
120500 120774     /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
120501 120775     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
120502 120776     /* ColNames:  */ 45, 1,
120503 120777     /* iArg:      */ 0 },
120504         -#endif
120505 120778   #endif
120506 120779   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120507 120780   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
120508 120781    {/* zName:     */ "defer_foreign_keys",
120509 120782     /* ePragTyp:  */ PragTyp_FLAG,
120510 120783     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
120511 120784     /* ColNames:  */ 0, 0,
120512 120785     /* iArg:      */ SQLITE_DeferFKs },
120513 120786   #endif
120514 120787   #endif
120515 120788   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120516         -#if !defined(SQLITE_OMIT_DEPRECATED)
120517 120789    {/* zName:     */ "empty_result_callbacks",
120518 120790     /* ePragTyp:  */ PragTyp_FLAG,
120519 120791     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
120520 120792     /* ColNames:  */ 0, 0,
120521 120793     /* iArg:      */ SQLITE_NullCallback },
120522         -#endif
120523 120794   #endif
120524 120795   #if !defined(SQLITE_OMIT_UTF16)
120525 120796    {/* zName:     */ "encoding",
120526 120797     /* ePragTyp:  */ PragTyp_ENCODING,
120527 120798     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
120528 120799     /* ColNames:  */ 0, 0,
120529 120800     /* iArg:      */ 0 },
................................................................................
120555 120826    {/* zName:     */ "freelist_count",
120556 120827     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
120557 120828     /* ePragFlg:  */ PragFlg_ReadOnly|PragFlg_Result0,
120558 120829     /* ColNames:  */ 0, 0,
120559 120830     /* iArg:      */ BTREE_FREE_PAGE_COUNT },
120560 120831   #endif
120561 120832   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120562         -#if !defined(SQLITE_OMIT_DEPRECATED)
120563 120833    {/* zName:     */ "full_column_names",
120564 120834     /* ePragTyp:  */ PragTyp_FLAG,
120565 120835     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
120566 120836     /* ColNames:  */ 0, 0,
120567 120837     /* iArg:      */ SQLITE_FullColNames },
120568         -#endif
120569         -#endif
120570         -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120571 120838    {/* zName:     */ "fullfsync",
120572 120839     /* ePragTyp:  */ PragTyp_FLAG,
120573 120840     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
120574 120841     /* ColNames:  */ 0, 0,
120575 120842     /* iArg:      */ SQLITE_FullFSync },
120576 120843   #endif
120577 120844   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
................................................................................
120792 121059    {/* zName:     */ "secure_delete",
120793 121060     /* ePragTyp:  */ PragTyp_SECURE_DELETE,
120794 121061     /* ePragFlg:  */ PragFlg_Result0,
120795 121062     /* ColNames:  */ 0, 0,
120796 121063     /* iArg:      */ 0 },
120797 121064   #endif
120798 121065   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
120799         -#if !defined(SQLITE_OMIT_DEPRECATED)
120800 121066    {/* zName:     */ "short_column_names",
120801 121067     /* ePragTyp:  */ PragTyp_FLAG,
120802 121068     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
120803 121069     /* ColNames:  */ 0, 0,
120804 121070     /* iArg:      */ SQLITE_ShortColNames },
120805         -#endif
120806 121071   #endif
120807 121072    {/* zName:     */ "shrink_memory",
120808 121073     /* ePragTyp:  */ PragTyp_SHRINK_MEMORY,
120809 121074     /* ePragFlg:  */ PragFlg_NoColumns,
120810 121075     /* ColNames:  */ 0, 0,
120811 121076     /* iArg:      */ 0 },
120812 121077    {/* zName:     */ "soft_heap_limit",
................................................................................
129641 129906   
129642 129907   /*
129643 129908   ** Update the accumulator memory cells for an aggregate based on
129644 129909   ** the current cursor position.
129645 129910   **
129646 129911   ** If regAcc is non-zero and there are no min() or max() aggregates
129647 129912   ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
129648         -** registers i register regAcc contains 0. The caller will take care
       129913  +** registers if register regAcc contains 0. The caller will take care
129649 129914   ** of setting and clearing regAcc.
129650 129915   */
129651 129916   static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
129652 129917     Vdbe *v = pParse->pVdbe;
129653 129918     int i;
129654 129919     int regHit = 0;
129655 129920     int addrHitTest = 0;
................................................................................
130078 130343        && (pTabList->nSrc==1
130079 130344            || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
130080 130345       ){
130081 130346         continue;
130082 130347       }
130083 130348   
130084 130349       if( flattenSubquery(pParse, p, i, isAgg) ){
       130350  +      if( pParse->nErr ) goto select_end;
130085 130351         /* This subquery can be absorbed into its parent. */
130086 130352         i = -1;
130087 130353       }
130088 130354       pTabList = p->pSrc;
130089 130355       if( db->mallocFailed ) goto select_end;
130090 130356       if( !IgnorableOrderby(pDest) ){
130091 130357         sSort.pOrderBy = p->pOrderBy;
................................................................................
133732 133998     sqlite3ExprDelete(pParse->db, pInto);
133733 133999     return;
133734 134000   }
133735 134001   
133736 134002   /*
133737 134003   ** This routine implements the OP_Vacuum opcode of the VDBE.
133738 134004   */
133739         -SQLITE_PRIVATE int sqlite3RunVacuum(
       134005  +SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3RunVacuum(
133740 134006     char **pzErrMsg,        /* Write error message here */
133741 134007     sqlite3 *db,            /* Database connection */
133742 134008     int iDb,                /* Which attached DB to vacuum */
133743         -  sqlite3_value *pOut     /* Write results here, if not NULL */
       134009  +  sqlite3_value *pOut     /* Write results here, if not NULL. VACUUM INTO */
133744 134010   ){
133745 134011     int rc = SQLITE_OK;     /* Return code from service routines */
133746 134012     Btree *pMain;           /* The database being vacuumed */
133747 134013     Btree *pTemp;           /* The temporary database we vacuum into */
133748 134014     u32 saved_mDbFlags;     /* Saved value of db->mDbFlags */
133749 134015     u64 saved_flags;        /* Saved value of db->flags */
133750 134016     int saved_nChange;      /* Saved value of db->nChange */
133751 134017     int saved_nTotalChange; /* Saved value of db->nTotalChange */
       134018  +  u32 saved_openFlags;    /* Saved value of db->openFlags */
133752 134019     u8 saved_mTrace;        /* Saved trace settings */
133753 134020     Db *pDb = 0;            /* Database to detach at end of vacuum */
133754 134021     int isMemDb;            /* True if vacuuming a :memory: database */
133755 134022     int nRes;               /* Bytes of reserved space at the end of each page */
133756 134023     int nDb;                /* Number of attached databases */
133757 134024     const char *zDbMain;    /* Schema name of database to vacuum */
133758 134025     const char *zOut;       /* Name of output file */
133759 134026   
133760 134027     if( !db->autoCommit ){
133761 134028       sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
133762         -    return SQLITE_ERROR;
       134029  +    return SQLITE_ERROR; /* IMP: R-12218-18073 */
133763 134030     }
133764 134031     if( db->nVdbeActive>1 ){
133765 134032       sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
133766         -    return SQLITE_ERROR;
       134033  +    return SQLITE_ERROR; /* IMP: R-15610-35227 */
133767 134034     }
       134035  +  saved_openFlags = db->openFlags;
133768 134036     if( pOut ){
133769 134037       if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){
133770 134038         sqlite3SetString(pzErrMsg, db, "non-text filename");
133771 134039         return SQLITE_ERROR;
133772 134040       }
133773 134041       zOut = (const char*)sqlite3_value_text(pOut);
       134042  +    db->openFlags &= ~SQLITE_OPEN_READONLY;
       134043  +    db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
133774 134044     }else{
133775 134045       zOut = "";
133776 134046     }
133777 134047   
133778 134048     /* Save the current value of the database flags so that it can be 
133779 134049     ** restored before returning. Then set the writable-schema flag, and
133780 134050     ** disable CHECK and foreign key constraints.  */
................................................................................
133805 134075     ** actually occurs when doing a vacuum since the vacuum_db is initially
133806 134076     ** empty.  Only the journal header is written.  Apparently it takes more
133807 134077     ** time to parse and run the PRAGMA to turn journalling off than it does
133808 134078     ** to write the journal header file.
133809 134079     */
133810 134080     nDb = db->nDb;
133811 134081     rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
       134082  +  db->openFlags = saved_openFlags;
133812 134083     if( rc!=SQLITE_OK ) goto end_of_vacuum;
133813 134084     assert( (db->nDb-1)==nDb );
133814 134085     pDb = &db->aDb[nDb];
133815 134086     assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
133816 134087     pTemp = pDb->pBt;
133817 134088     if( pOut ){
133818 134089       sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
133819 134090       i64 sz = 0;
133820 134091       if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
133821 134092         rc = SQLITE_ERROR;
133822 134093         sqlite3SetString(pzErrMsg, db, "output file already exists");
133823 134094         goto end_of_vacuum;
133824 134095       }
       134096  +    db->mDbFlags |= DBFLAG_VacuumInto;
133825 134097     }
133826 134098     nRes = sqlite3BtreeGetOptimalReserve(pMain);
133827 134099   
133828 134100     /* A VACUUM cannot change the pagesize of an encrypted database. */
133829 134101   #ifdef SQLITE_HAS_CODEC
133830 134102     if( db->nextPagesize ){
133831 134103       extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
................................................................................
136426 136698       if( pIn ){
136427 136699         int iMap = 0;               /* Index in aiMap[] */
136428 136700         pIn += i;
136429 136701         for(i=iEq;i<pLoop->nLTerm; i++){
136430 136702           if( pLoop->aLTerm[i]->pExpr==pX ){
136431 136703             int iOut = iReg + i - iEq;
136432 136704             if( eType==IN_INDEX_ROWID ){
136433         -            testcase( nEq>1 );  /* Happens with a UNIQUE index on ROWID */
136434 136705               pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
136435 136706             }else{
136436 136707               int iCol = aiMap ? aiMap[iMap++] : 0;
136437 136708               pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
136438 136709             }
136439 136710             sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v);
136440 136711             if( i==iEq ){
................................................................................
137003 137274       x.iIdxCol = iIdxCol;
137004 137275       x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
137005 137276       sqlite3WalkExpr(&w, pWInfo->pWhere);
137006 137277       sqlite3WalkExprList(&w, pWInfo->pOrderBy);
137007 137278       sqlite3WalkExprList(&w, pWInfo->pResultSet);
137008 137279     }
137009 137280   }
       137281  +
       137282  +/*
       137283  +** The pTruth expression is always true because it is the WHERE clause
       137284  +** a partial index that is driving a query loop.  Look through all of the
       137285  +** WHERE clause terms on the query, and if any of those terms must be
       137286  +** true because pTruth is true, then mark those WHERE clause terms as
       137287  +** coded.
       137288  +*/
       137289  +static void whereApplyPartialIndexConstraints(
       137290  +  Expr *pTruth,
       137291  +  int iTabCur,
       137292  +  WhereClause *pWC
       137293  +){
       137294  +  int i;
       137295  +  WhereTerm *pTerm;
       137296  +  while( pTruth->op==TK_AND ){
       137297  +    whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
       137298  +    pTruth = pTruth->pRight;
       137299  +  }
       137300  +  for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
       137301  +    Expr *pExpr;
       137302  +    if( pTerm->wtFlags & TERM_CODED ) continue;
       137303  +    pExpr = pTerm->pExpr;
       137304  +    if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
       137305  +      pTerm->wtFlags |= TERM_CODED;
       137306  +    }
       137307  +  }
       137308  +}
137010 137309   
137011 137310   /*
137012 137311   ** Generate code for the start of the iLevel-th loop in the WHERE clause
137013 137312   ** implementation described by pWInfo.
137014 137313   */
137015 137314   SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
137016 137315     Parse *pParse,       /* Parsing context */
................................................................................
137188 137487       iReleaseReg = ++pParse->nMem;
137189 137488       iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
137190 137489       if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
137191 137490       addrNxt = pLevel->addrNxt;
137192 137491       sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg);
137193 137492       VdbeCoverage(v);
137194 137493       pLevel->op = OP_Noop;
       137494  +    if( (pTerm->prereqAll & pLevel->notReady)==0 ){
       137495  +      pTerm->wtFlags |= TERM_CODED;
       137496  +    }
137195 137497     }else if( (pLoop->wsFlags & WHERE_IPK)!=0
137196 137498            && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
137197 137499     ){
137198 137500       /* Case 3:  We have an inequality comparison against the ROWID field.
137199 137501       */
137200 137502       int testOp = OP_Noop;
137201 137503       int start;
................................................................................
137600 137902       ** into reference to index columns.
137601 137903       **
137602 137904       ** Do not do this for the RHS of a LEFT JOIN. This is because the 
137603 137905       ** expression may be evaluated after OP_NullRow has been executed on
137604 137906       ** the cursor. In this case it is important to do the full evaluation,
137605 137907       ** as the result of the expression may not be NULL, even if all table
137606 137908       ** column values are.  https://www.sqlite.org/src/info/7fa8049685b50b5a
       137909  +    **
       137910  +    ** Also, do not do this when processing one index an a multi-index
       137911  +    ** OR clause, since the transformation will become invalid once we
       137912  +    ** move forward to the next index.
       137913  +    ** https://sqlite.org/src/info/4e8e4857d32d401f
137607 137914       */
137608         -    if( pLevel->iLeftJoin==0 ){
       137915  +    if( pLevel->iLeftJoin==0 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
137609 137916         whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
137610 137917       }
       137918  +
       137919  +    /* If a partial index is driving the loop, try to eliminate WHERE clause
       137920  +    ** terms from the query that must be true due to the WHERE clause of
       137921  +    ** the partial index
       137922  +    */
       137923  +    if( pIdx->pPartIdxWhere ){
       137924  +      whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
       137925  +    }
137611 137926   
137612 137927       /* Record the instruction used to terminate the loop. */
137613 137928       if( pLoop->wsFlags & WHERE_ONEROW ){
137614 137929         pLevel->op = OP_Noop;
137615 137930       }else if( bRev ){
137616 137931         pLevel->op = OP_Prev;
137617 137932       }else{
................................................................................
137768 138083           if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
137769 138084           if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
137770 138085           testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
137771 138086           pExpr = sqlite3ExprDup(db, pExpr, 0);
137772 138087           pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
137773 138088         }
137774 138089         if( pAndExpr ){
137775         -        pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr);
       138090  +        /* The extra 0x10000 bit on the opcode is masked off and does not
       138091  +        ** become part of the new Expr.op.  However, it does make the
       138092  +        ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
       138093  +        ** prevents sqlite3PExpr() from implementing AND short-circuit 
       138094  +        ** optimization, which we do not want here. */
       138095  +        pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, 0, pAndExpr);
137776 138096         }
137777 138097       }
137778 138098   
137779 138099       /* Run a separate WHERE clause for each term of the OR clause.  After
137780 138100       ** eliminating duplicates from other WHERE clauses, the action for each
137781 138101       ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
137782 138102       */
................................................................................
137998 138318           ** that compares BLOBs. */
137999 138319   #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
138000 138320           continue;
138001 138321   #else
138002 138322           u32 x = pLevel->iLikeRepCntr;
138003 138323           if( x>0 ){
138004 138324             skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
       138325  +          VdbeCoverageIf(v, (x&1)==1);
       138326  +          VdbeCoverageIf(v, (x&1)==0);
138005 138327           }
138006         -        VdbeCoverage(v);
138007 138328   #endif
138008 138329         }
138009 138330   #ifdef WHERETRACE_ENABLED /* 0xffff */
138010 138331         if( sqlite3WhereTrace ){
138011 138332           VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
138012 138333                            pWC->nTerm-j, pTerm, iLoop));
138013 138334         }
................................................................................
139601 139922       assert( p->x.pList==0 );
139602 139923     }else if( ExprHasProperty(p, EP_xIsSelect) ){
139603 139924       if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
139604 139925       mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
139605 139926     }else if( p->x.pList ){
139606 139927       mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
139607 139928     }
       139929  +#ifndef SQLITE_OMIT_WINDOWFUNC
       139930  +  if( p->op==TK_FUNCTION && p->y.pWin ){
       139931  +    mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
       139932  +    mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
       139933  +  }
       139934  +#endif
139608 139935     return mask;
139609 139936   }
139610 139937   SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
139611 139938     return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
139612 139939   }
139613 139940   SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
139614 139941     int i;
................................................................................
143017 143344   
143018 143345     /* First call xBestIndex() with all constraints usable. */
143019 143346     WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
143020 143347     WHERETRACE(0x40, ("  VirtualOne: all usable\n"));
143021 143348     rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
143022 143349   
143023 143350     /* If the call to xBestIndex() with all terms enabled produced a plan
143024         -  ** that does not require any source tables (IOW: a plan with mBest==0),
143025         -  ** then there is no point in making any further calls to xBestIndex() 
143026         -  ** since they will all return the same result (if the xBestIndex()
143027         -  ** implementation is sane). */
143028         -  if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){
       143351  +  ** that does not require any source tables (IOW: a plan with mBest==0)
       143352  +  ** and does not use an IN(...) operator, then there is no point in making 
       143353  +  ** any further calls to xBestIndex() since they will all return the same
       143354  +  ** result (if the xBestIndex() implementation is sane). */
       143355  +  if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
143029 143356       int seenZero = 0;             /* True if a plan with no prereqs seen */
143030 143357       int seenZeroNoIN = 0;         /* Plan with no prereqs and no IN(...) seen */
143031 143358       Bitmask mPrev = 0;
143032 143359       Bitmask mBestNoIn = 0;
143033 143360   
143034 143361       /* If the plan produced by the earlier call uses an IN(...) term, call
143035 143362       ** xBestIndex again, this time with IN(...) terms disabled. */
................................................................................
145254 145581         p->nValue++;
145255 145582         p->nStep = 0;
145256 145583       }
145257 145584       sqlite3_result_int64(pCtx, p->nValue);
145258 145585     }
145259 145586   }
145260 145587   
       145588  +/*
       145589  +** Implementation of built-in window function nth_value(). This
       145590  +** implementation is used in "slow mode" only - when the EXCLUDE clause
       145591  +** is not set to the default value "NO OTHERS".
       145592  +*/
       145593  +struct NthValueCtx {
       145594  +  i64 nStep;
       145595  +  sqlite3_value *pValue;
       145596  +};
       145597  +static void nth_valueStepFunc(
       145598  +  sqlite3_context *pCtx, 
       145599  +  int nArg,
       145600  +  sqlite3_value **apArg
       145601  +){
       145602  +  struct NthValueCtx *p;
       145603  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145604  +  if( p ){
       145605  +    i64 iVal;
       145606  +    switch( sqlite3_value_numeric_type(apArg[1]) ){
       145607  +      case SQLITE_INTEGER:
       145608  +        iVal = sqlite3_value_int64(apArg[1]);
       145609  +        break;
       145610  +      case SQLITE_FLOAT: {
       145611  +        double fVal = sqlite3_value_double(apArg[1]);
       145612  +        if( ((i64)fVal)!=fVal ) goto error_out;
       145613  +        iVal = (i64)fVal;
       145614  +        break;
       145615  +      }
       145616  +      default:
       145617  +        goto error_out;
       145618  +    }
       145619  +    if( iVal<=0 ) goto error_out;
       145620  +
       145621  +    p->nStep++;
       145622  +    if( iVal==p->nStep ){
       145623  +      p->pValue = sqlite3_value_dup(apArg[0]);
       145624  +      if( !p->pValue ){
       145625  +        sqlite3_result_error_nomem(pCtx);
       145626  +      }
       145627  +    }
       145628  +  }
       145629  +  UNUSED_PARAMETER(nArg);
       145630  +  UNUSED_PARAMETER(apArg);
       145631  +  return;
       145632  +
       145633  + error_out:
       145634  +  sqlite3_result_error(
       145635  +      pCtx, "second argument to nth_value must be a positive integer", -1
       145636  +  );
       145637  +}
       145638  +static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
       145639  +  struct NthValueCtx *p;
       145640  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
       145641  +  if( p && p->pValue ){
       145642  +    sqlite3_result_value(pCtx, p->pValue);
       145643  +    sqlite3_value_free(p->pValue);
       145644  +    p->pValue = 0;
       145645  +  }
       145646  +}
       145647  +#define nth_valueInvFunc noopStepFunc
       145648  +#define nth_valueValueFunc noopValueFunc
       145649  +
       145650  +static void first_valueStepFunc(
       145651  +  sqlite3_context *pCtx, 
       145652  +  int nArg,
       145653  +  sqlite3_value **apArg
       145654  +){
       145655  +  struct NthValueCtx *p;
       145656  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145657  +  if( p && p->pValue==0 ){
       145658  +    p->pValue = sqlite3_value_dup(apArg[0]);
       145659  +    if( !p->pValue ){
       145660  +      sqlite3_result_error_nomem(pCtx);
       145661  +    }
       145662  +  }
       145663  +  UNUSED_PARAMETER(nArg);
       145664  +  UNUSED_PARAMETER(apArg);
       145665  +}
       145666  +static void first_valueFinalizeFunc(sqlite3_context *pCtx){
       145667  +  struct NthValueCtx *p;
       145668  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145669  +  if( p && p->pValue ){
       145670  +    sqlite3_result_value(pCtx, p->pValue);
       145671  +    sqlite3_value_free(p->pValue);
       145672  +    p->pValue = 0;
       145673  +  }
       145674  +}
       145675  +#define first_valueInvFunc noopStepFunc
       145676  +#define first_valueValueFunc noopValueFunc
       145677  +
145261 145678   /*
145262 145679   ** Implementation of built-in window function rank(). Assumes that
145263 145680   ** the window frame has been set to:
145264 145681   **
145265 145682   **   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
145266 145683   */
145267 145684   static void rankStepFunc(
................................................................................
145289 145706     }
145290 145707   }
145291 145708   
145292 145709   /*
145293 145710   ** Implementation of built-in window function percent_rank(). Assumes that
145294 145711   ** the window frame has been set to:
145295 145712   **
145296         -**   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
       145713  +**   GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
145297 145714   */
145298 145715   static void percent_rankStepFunc(
145299 145716     sqlite3_context *pCtx, 
145300 145717     int nArg,
145301 145718     sqlite3_value **apArg
145302 145719   ){
145303 145720     struct CallCount *p;
145304         -  UNUSED_PARAMETER(nArg); assert( nArg==1 );
145305         -
       145721  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145722  +  UNUSED_PARAMETER(apArg);
       145723  +  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145724  +  if( p ){
       145725  +    p->nTotal++;
       145726  +  }
       145727  +}
       145728  +static void percent_rankInvFunc(
       145729  +  sqlite3_context *pCtx, 
       145730  +  int nArg,
       145731  +  sqlite3_value **apArg
       145732  +){
       145733  +  struct CallCount *p;
       145734  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145735  +  UNUSED_PARAMETER(apArg);
145306 145736     p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145307         -  if( p ){
145308         -    if( p->nTotal==0 ){
145309         -      p->nTotal = sqlite3_value_int64(apArg[0]);
145310         -    }
145311         -    p->nStep++;
145312         -    if( p->nValue==0 ){
145313         -      p->nValue = p->nStep;
145314         -    }
145315         -  }
       145737  +  p->nStep++;
145316 145738   }
145317 145739   static void percent_rankValueFunc(sqlite3_context *pCtx){
145318 145740     struct CallCount *p;
145319 145741     p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145320 145742     if( p ){
       145743  +    p->nValue = p->nStep;
145321 145744       if( p->nTotal>1 ){
145322         -      double r = (double)(p->nValue-1) / (double)(p->nTotal-1);
       145745  +      double r = (double)p->nValue / (double)(p->nTotal-1);
145323 145746         sqlite3_result_double(pCtx, r);
145324 145747       }else{
145325 145748         sqlite3_result_double(pCtx, 0.0);
145326 145749       }
145327         -    p->nValue = 0;
145328 145750     }
145329 145751   }
       145752  +#define percent_rankFinalizeFunc percent_rankValueFunc
145330 145753   
145331 145754   /*
145332 145755   ** Implementation of built-in window function cume_dist(). Assumes that
145333 145756   ** the window frame has been set to:
145334 145757   **
145335         -**   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
       145758  +**   GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
145336 145759   */
145337 145760   static void cume_distStepFunc(
145338 145761     sqlite3_context *pCtx, 
145339 145762     int nArg,
145340 145763     sqlite3_value **apArg
145341 145764   ){
145342 145765     struct CallCount *p;
145343         -  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
145344         -
       145766  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145767  +  UNUSED_PARAMETER(apArg);
       145768  +  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145769  +  if( p ){
       145770  +    p->nTotal++;
       145771  +  }
       145772  +}
       145773  +static void cume_distInvFunc(
       145774  +  sqlite3_context *pCtx, 
       145775  +  int nArg,
       145776  +  sqlite3_value **apArg
       145777  +){
       145778  +  struct CallCount *p;
       145779  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145780  +  UNUSED_PARAMETER(apArg);
145345 145781     p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145346         -  if( p ){
145347         -    if( p->nTotal==0 ){
145348         -      p->nTotal = sqlite3_value_int64(apArg[0]);
145349         -    }
145350         -    p->nStep++;
145351         -  }
       145782  +  p->nStep++;
145352 145783   }
145353 145784   static void cume_distValueFunc(sqlite3_context *pCtx){
145354 145785     struct CallCount *p;
145355         -  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145356         -  if( p && p->nTotal ){
       145786  +  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
       145787  +  if( p ){
145357 145788       double r = (double)(p->nStep) / (double)(p->nTotal);
145358 145789       sqlite3_result_double(pCtx, r);
145359 145790     }
145360 145791   }
       145792  +#define cume_distFinalizeFunc cume_distValueFunc
145361 145793   
145362 145794   /*
145363 145795   ** Context object for ntile() window function.
145364 145796   */
145365 145797   struct NtileCtx {
145366 145798     i64 nTotal;                     /* Total rows in partition */
145367 145799     i64 nParam;                     /* Parameter passed to ntile(N) */
................................................................................
145368 145800     i64 iRow;                       /* Current row */
145369 145801   };
145370 145802   
145371 145803   /*
145372 145804   ** Implementation of ntile(). This assumes that the window frame has
145373 145805   ** been coerced to:
145374 145806   **
145375         -**   ROWS UNBOUNDED PRECEDING AND CURRENT ROW
       145807  +**   ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
145376 145808   */
145377 145809   static void ntileStepFunc(
145378 145810     sqlite3_context *pCtx, 
145379 145811     int nArg,
145380 145812     sqlite3_value **apArg
145381 145813   ){
145382 145814     struct NtileCtx *p;
145383         -  assert( nArg==2 ); UNUSED_PARAMETER(nArg);
       145815  +  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
145384 145816     p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145385 145817     if( p ){
145386 145818       if( p->nTotal==0 ){
145387 145819         p->nParam = sqlite3_value_int64(apArg[0]);
145388         -      p->nTotal = sqlite3_value_int64(apArg[1]);
145389 145820         if( p->nParam<=0 ){
145390 145821           sqlite3_result_error(
145391 145822               pCtx, "argument of ntile must be a positive integer", -1
145392 145823           );
145393 145824         }
145394 145825       }
145395         -    p->iRow++;
       145826  +    p->nTotal++;
145396 145827     }
       145828  +}
       145829  +static void ntileInvFunc(
       145830  +  sqlite3_context *pCtx, 
       145831  +  int nArg,
       145832  +  sqlite3_value **apArg
       145833  +){
       145834  +  struct NtileCtx *p;
       145835  +  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
       145836  +  UNUSED_PARAMETER(apArg);
       145837  +  p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145838  +  p->iRow++;
145397 145839   }
145398 145840   static void ntileValueFunc(sqlite3_context *pCtx){
145399 145841     struct NtileCtx *p;
145400 145842     p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145401 145843     if( p && p->nParam>0 ){
145402 145844       int nSize = (p->nTotal / p->nParam);
145403 145845       if( nSize==0 ){
145404         -      sqlite3_result_int64(pCtx, p->iRow);
       145846  +      sqlite3_result_int64(pCtx, p->iRow+1);
145405 145847       }else{
145406 145848         i64 nLarge = p->nTotal - p->nParam*nSize;
145407 145849         i64 iSmall = nLarge*(nSize+1);
145408         -      i64 iRow = p->iRow-1;
       145850  +      i64 iRow = p->iRow;
145409 145851   
145410 145852         assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
145411 145853   
145412 145854         if( iRow<iSmall ){
145413 145855           sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
145414 145856         }else{
145415 145857           sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
145416 145858         }
145417 145859       }
145418 145860     }
145419 145861   }
       145862  +#define ntileFinalizeFunc ntileValueFunc
145420 145863   
145421 145864   /*
145422 145865   ** Context object for last_value() window function.
145423 145866   */
145424 145867   struct LastValueCtx {
145425 145868     sqlite3_value *pVal;
145426 145869     int nVal;
................................................................................
145462 145905         sqlite3_value_free(p->pVal);
145463 145906         p->pVal = 0;
145464 145907       }
145465 145908     }
145466 145909   }
145467 145910   static void last_valueValueFunc(sqlite3_context *pCtx){
145468 145911     struct LastValueCtx *p;
145469         -  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145912  +  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
145470 145913     if( p && p->pVal ){
145471 145914       sqlite3_result_value(pCtx, p->pVal);
145472 145915     }
145473 145916   }
145474 145917   static void last_valueFinalizeFunc(sqlite3_context *pCtx){
145475 145918     struct LastValueCtx *p;
145476 145919     p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
................................................................................
145552 145995   ** Register those built-in window functions that are not also aggregates.
145553 145996   */
145554 145997   SQLITE_PRIVATE void sqlite3WindowFunctions(void){
145555 145998     static FuncDef aWindowFuncs[] = {
145556 145999       WINDOWFUNCX(row_number, 0, 0),
145557 146000       WINDOWFUNCX(dense_rank, 0, 0),
145558 146001       WINDOWFUNCX(rank, 0, 0),
145559         -    WINDOWFUNCX(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE),
145560         -    WINDOWFUNCX(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE),
145561         -    WINDOWFUNCX(ntile, 1, SQLITE_FUNC_WINDOW_SIZE),
       146002  +    WINDOWFUNCALL(percent_rank, 0, 0),
       146003  +    WINDOWFUNCALL(cume_dist, 0, 0),
       146004  +    WINDOWFUNCALL(ntile, 1, 0),
145562 146005       WINDOWFUNCALL(last_value, 1, 0),
145563         -    WINDOWFUNCNOOP(nth_value, 2, 0),
145564         -    WINDOWFUNCNOOP(first_value, 1, 0),
       146006  +    WINDOWFUNCALL(nth_value, 2, 0),
       146007  +    WINDOWFUNCALL(first_value, 1, 0),
145565 146008       WINDOWFUNCNOOP(lead, 1, 0),
145566 146009       WINDOWFUNCNOOP(lead, 2, 0),
145567 146010       WINDOWFUNCNOOP(lead, 3, 0),
145568 146011       WINDOWFUNCNOOP(lag, 1, 0),
145569 146012       WINDOWFUNCNOOP(lag, 2, 0),
145570 146013       WINDOWFUNCNOOP(lag, 3, 0),
145571 146014     };
145572 146015     sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
145573 146016   }
       146017  +
       146018  +static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
       146019  +  Window *p;
       146020  +  for(p=pList; p; p=p->pNextWin){
       146021  +    if( sqlite3StrICmp(p->zName, zName)==0 ) break;
       146022  +  }
       146023  +  if( p==0 ){
       146024  +    sqlite3ErrorMsg(pParse, "no such window: %s", zName);
       146025  +  }
       146026  +  return p;
       146027  +}
145574 146028   
145575 146029   /*
145576 146030   ** This function is called immediately after resolving the function name
145577 146031   ** for a window function within a SELECT statement. Argument pList is a
145578 146032   ** linked list of WINDOW definitions for the current SELECT statement.
145579 146033   ** Argument pFunc is the function definition just resolved and pWin
145580 146034   ** is the Window object representing the associated OVER clause. This
................................................................................
145591 146045   */
145592 146046   SQLITE_PRIVATE void sqlite3WindowUpdate(
145593 146047     Parse *pParse, 
145594 146048     Window *pList,                  /* List of named windows for this SELECT */
145595 146049     Window *pWin,                   /* Window frame to update */
145596 146050     FuncDef *pFunc                  /* Window function definition */
145597 146051   ){
145598         -  if( pWin->zName && pWin->eType==0 ){
145599         -    Window *p;
145600         -    for(p=pList; p; p=p->pNextWin){
145601         -      if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break;
145602         -    }
145603         -    if( p==0 ){
145604         -      sqlite3ErrorMsg(pParse, "no such window: %s", pWin->zName);
145605         -      return;
145606         -    }
       146052  +  if( pWin->zName && pWin->eFrmType==0 ){
       146053  +    Window *p = windowFind(pParse, pList, pWin->zName);
       146054  +    if( p==0 ) return;
145607 146055       pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
145608 146056       pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
145609 146057       pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
145610 146058       pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
145611 146059       pWin->eStart = p->eStart;
145612 146060       pWin->eEnd = p->eEnd;
145613         -    pWin->eType = p->eType;
       146061  +    pWin->eFrmType = p->eFrmType;
       146062  +    pWin->eExclude = p->eExclude;
       146063  +  }else{
       146064  +    sqlite3WindowChain(pParse, pWin, pList);
145614 146065     }
       146066  +  if( (pWin->eFrmType==TK_RANGE)
       146067  +   && (pWin->pStart || pWin->pEnd) 
       146068  +   && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
       146069  +  ){
       146070  +    sqlite3ErrorMsg(pParse, 
       146071  +      "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
       146072  +    );
       146073  +  }else
145615 146074     if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
145616 146075       sqlite3 *db = pParse->db;
145617 146076       if( pWin->pFilter ){
145618 146077         sqlite3ErrorMsg(pParse, 
145619 146078             "FILTER clause may only be used with aggregate window functions"
145620 146079         );
145621         -    }else
145622         -    if( pFunc->zName==row_numberName || pFunc->zName==ntileName ){
145623         -      sqlite3ExprDelete(db, pWin->pStart);
145624         -      sqlite3ExprDelete(db, pWin->pEnd);
145625         -      pWin->pStart = pWin->pEnd = 0;
145626         -      pWin->eType = TK_ROWS;
145627         -      pWin->eStart = TK_UNBOUNDED;
145628         -      pWin->eEnd = TK_CURRENT;
145629         -    }else
145630         -
145631         -    if( pFunc->zName==dense_rankName || pFunc->zName==rankName
145632         -     || pFunc->zName==percent_rankName || pFunc->zName==cume_distName
145633         -    ){
145634         -      sqlite3ExprDelete(db, pWin->pStart);
145635         -      sqlite3ExprDelete(db, pWin->pEnd);
145636         -      pWin->pStart = pWin->pEnd = 0;
145637         -      pWin->eType = TK_RANGE;
145638         -      pWin->eStart = TK_UNBOUNDED;
145639         -      pWin->eEnd = TK_CURRENT;
       146080  +    }else{
       146081  +      struct WindowUpdate {
       146082  +        const char *zFunc;
       146083  +        int eFrmType;
       146084  +        int eStart;
       146085  +        int eEnd;
       146086  +      } aUp[] = {
       146087  +        { row_numberName,   TK_ROWS,   TK_UNBOUNDED, TK_CURRENT }, 
       146088  +        { dense_rankName,   TK_RANGE,  TK_UNBOUNDED, TK_CURRENT }, 
       146089  +        { rankName,         TK_RANGE,  TK_UNBOUNDED, TK_CURRENT }, 
       146090  +        { percent_rankName, TK_GROUPS, TK_CURRENT,   TK_UNBOUNDED }, 
       146091  +        { cume_distName,    TK_GROUPS, TK_FOLLOWING, TK_UNBOUNDED }, 
       146092  +        { ntileName,        TK_ROWS,   TK_CURRENT,   TK_UNBOUNDED }, 
       146093  +        { leadName,         TK_ROWS,   TK_UNBOUNDED, TK_UNBOUNDED }, 
       146094  +        { lagName,          TK_ROWS,   TK_UNBOUNDED, TK_CURRENT }, 
       146095  +      };
       146096  +      int i;
       146097  +      for(i=0; i<ArraySize(aUp); i++){
       146098  +        if( pFunc->zName==aUp[i].zFunc ){
       146099  +          sqlite3ExprDelete(db, pWin->pStart);
       146100  +          sqlite3ExprDelete(db, pWin->pEnd);
       146101  +          pWin->pEnd = pWin->pStart = 0;
       146102  +          pWin->eFrmType = aUp[i].eFrmType;
       146103  +          pWin->eStart = aUp[i].eStart;
       146104  +          pWin->eEnd = aUp[i].eEnd;
       146105  +          pWin->eExclude = 0;
       146106  +          if( pWin->eStart==TK_FOLLOWING ){
       146107  +            pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
       146108  +          }
       146109  +          break;
       146110  +        }
       146111  +      }
145640 146112       }
145641 146113     }
145642 146114     pWin->pFunc = pFunc;
145643 146115   }
145644 146116   
145645 146117   /*
145646 146118   ** Context object passed through sqlite3WalkExprList() to
................................................................................
145837 146309         }
145838 146310       }
145839 146311   
145840 146312       /* Assign a cursor number for the ephemeral table used to buffer rows.
145841 146313       ** The OpenEphemeral instruction is coded later, after it is known how
145842 146314       ** many columns the table will have.  */
145843 146315       pMWin->iEphCsr = pParse->nTab++;
       146316  +    pParse->nTab += 3;
145844 146317   
145845 146318       selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, &pSublist);
145846 146319       selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, &pSublist);
145847 146320       pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
145848 146321   
145849 146322       /* Append the PARTITION BY and ORDER BY expressions to the to the 
145850 146323       ** sub-select expression list. They are required to figure out where 
................................................................................
145892 146365         }else{
145893 146366           pSub->selFlags |= SF_Expanded;
145894 146367           p->selFlags &= ~SF_Aggregate;
145895 146368           sqlite3SelectPrep(pParse, pSub, 0);
145896 146369         }
145897 146370   
145898 146371         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
       146372  +      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
       146373  +      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
       146374  +      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
145899 146375       }else{
145900 146376         sqlite3SelectDelete(db, pSub);
145901 146377       }
145902 146378       if( db->mallocFailed ) rc = SQLITE_NOMEM;
145903 146379     }
145904 146380   
145905 146381     return rc;
................................................................................
145912 146388     if( p ){
145913 146389       sqlite3ExprDelete(db, p->pFilter);
145914 146390       sqlite3ExprListDelete(db, p->pPartition);
145915 146391       sqlite3ExprListDelete(db, p->pOrderBy);
145916 146392       sqlite3ExprDelete(db, p->pEnd);
145917 146393       sqlite3ExprDelete(db, p->pStart);
145918 146394       sqlite3DbFree(db, p->zName);
       146395  +    sqlite3DbFree(db, p->zBase);
145919 146396       sqlite3DbFree(db, p);
145920 146397     }
145921 146398   }
145922 146399   
145923 146400   /*
145924 146401   ** Free the linked list of Window objects starting at the second argument.
145925 146402   */
................................................................................
145948 146425   }
145949 146426   
145950 146427   /*
145951 146428   ** Allocate and return a new Window object describing a Window Definition.
145952 146429   */
145953 146430   SQLITE_PRIVATE Window *sqlite3WindowAlloc(
145954 146431     Parse *pParse,    /* Parsing context */
145955         -  int eType,        /* Frame type. TK_RANGE or TK_ROWS */
       146432  +  int eType,        /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
145956 146433     int eStart,       /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
145957 146434     Expr *pStart,     /* Start window size if TK_PRECEDING or FOLLOWING */
145958 146435     int eEnd,         /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
145959         -  Expr *pEnd        /* End window size if TK_FOLLOWING or PRECEDING */
       146436  +  Expr *pEnd,       /* End window size if TK_FOLLOWING or PRECEDING */
       146437  +  u8 eExclude       /* EXCLUDE clause */
145960 146438   ){
145961 146439     Window *pWin = 0;
       146440  +  int bImplicitFrame = 0;
145962 146441   
145963 146442     /* Parser assures the following: */
145964         -  assert( eType==TK_RANGE || eType==TK_ROWS );
       146443  +  assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS );
145965 146444     assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
145966 146445              || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
145967 146446     assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
145968 146447              || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
145969 146448     assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
145970 146449     assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
145971 146450   
145972         -
145973         -  /* If a frame is declared "RANGE" (not "ROWS"), then it may not use
145974         -  ** either "<expr> PRECEDING" or "<expr> FOLLOWING".
145975         -  */
145976         -  if( eType==TK_RANGE && (pStart!=0 || pEnd!=0) ){
145977         -    sqlite3ErrorMsg(pParse, "RANGE must use only UNBOUNDED or CURRENT ROW");
145978         -    goto windowAllocErr;
       146451  +  if( eType==0 ){
       146452  +    bImplicitFrame = 1;
       146453  +    eType = TK_RANGE;
145979 146454     }
145980 146455   
145981 146456     /* Additionally, the
145982 146457     ** starting boundary type may not occur earlier in the following list than
145983 146458     ** the ending boundary type:
145984 146459     **
145985 146460     **   UNBOUNDED PRECEDING
................................................................................
145991 146466     ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
145992 146467     ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
145993 146468     ** frame boundary.
145994 146469     */
145995 146470     if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
145996 146471      || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
145997 146472     ){
145998         -    sqlite3ErrorMsg(pParse, "unsupported frame delimiter for ROWS");
       146473  +    sqlite3ErrorMsg(pParse, "unsupported frame specification");
145999 146474       goto windowAllocErr;
146000 146475     }
146001 146476   
146002 146477     pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
146003 146478     if( pWin==0 ) goto windowAllocErr;
146004         -  pWin->eType = eType;
       146479  +  pWin->eFrmType = eType;
146005 146480     pWin->eStart = eStart;
146006 146481     pWin->eEnd = eEnd;
       146482  +  if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
       146483  +    eExclude = TK_NO;
       146484  +  }
       146485  +  pWin->eExclude = eExclude;
       146486  +  pWin->bImplicitFrame = bImplicitFrame;
146007 146487     pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
146008 146488     pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
146009 146489     return pWin;
146010 146490   
146011 146491   windowAllocErr:
146012 146492     sqlite3ExprDelete(pParse->db, pEnd);
146013 146493     sqlite3ExprDelete(pParse->db, pStart);
146014 146494     return 0;
146015 146495   }
       146496  +
       146497  +/*
       146498  +** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
       146499  +** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
       146500  +** equivalent nul-terminated string.
       146501  +*/
       146502  +SQLITE_PRIVATE Window *sqlite3WindowAssemble(
       146503  +  Parse *pParse, 
       146504  +  Window *pWin, 
       146505  +  ExprList *pPartition, 
       146506  +  ExprList *pOrderBy, 
       146507  +  Token *pBase
       146508  +){
       146509  +  if( pWin ){
       146510  +    pWin->pPartition = pPartition;
       146511  +    pWin->pOrderBy = pOrderBy;
       146512  +    if( pBase ){
       146513  +      pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
       146514  +    }
       146515  +  }else{
       146516  +    sqlite3ExprListDelete(pParse->db, pPartition);
       146517  +    sqlite3ExprListDelete(pParse->db, pOrderBy);
       146518  +  }
       146519  +  return pWin;
       146520  +}
       146521  +
       146522  +/*
       146523  +** Window *pWin has just been created from a WINDOW clause. Tokne pBase
       146524  +** is the base window. Earlier windows from the same WINDOW clause are
       146525  +** stored in the linked list starting at pWin->pNextWin. This function
       146526  +** either updates *pWin according to the base specification, or else
       146527  +** leaves an error in pParse.
       146528  +*/
       146529  +SQLITE_PRIVATE void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
       146530  +  if( pWin->zBase ){
       146531  +    sqlite3 *db = pParse->db;
       146532  +    Window *pExist = windowFind(pParse, pList, pWin->zBase);
       146533  +    if( pExist ){
       146534  +      const char *zErr = 0;
       146535  +      /* Check for errors */
       146536  +      if( pWin->pPartition ){
       146537  +        zErr = "PARTITION clause";
       146538  +      }else if( pExist->pOrderBy && pWin->pOrderBy ){
       146539  +        zErr = "ORDER BY clause";
       146540  +      }else if( pExist->bImplicitFrame==0 ){
       146541  +        zErr = "frame specification";
       146542  +      }
       146543  +      if( zErr ){
       146544  +        sqlite3ErrorMsg(pParse, 
       146545  +            "cannot override %s of window: %s", zErr, pWin->zBase
       146546  +        );
       146547  +      }else{
       146548  +        pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
       146549  +        if( pExist->pOrderBy ){
       146550  +          assert( pWin->pOrderBy==0 );
       146551  +          pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
       146552  +        }
       146553  +        sqlite3DbFree(db, pWin->zBase);
       146554  +        pWin->zBase = 0;
       146555  +      }
       146556  +    }
       146557  +  }
       146558  +}
146016 146559   
146017 146560   /*
146018 146561   ** Attach window object pWin to expression p.
146019 146562   */
146020 146563   SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
146021 146564     if( p ){
146022 146565       assert( p->op==TK_FUNCTION );
................................................................................
146038 146581   }
146039 146582   
146040 146583   /*
146041 146584   ** Return 0 if the two window objects are identical, or non-zero otherwise.
146042 146585   ** Identical window objects can be processed in a single scan.
146043 146586   */
146044 146587   SQLITE_PRIVATE int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
146045         -  if( p1->eType!=p2->eType ) return 1;
       146588  +  if( p1->eFrmType!=p2->eFrmType ) return 1;
146046 146589     if( p1->eStart!=p2->eStart ) return 1;
146047 146590     if( p1->eEnd!=p2->eEnd ) return 1;
       146591  +  if( p1->eExclude!=p2->eExclude ) return 1;
146048 146592     if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
146049 146593     if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
146050 146594     if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
146051 146595     if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
146052 146596     return 0;
146053 146597   }
146054 146598   
................................................................................
146057 146601   ** This is called by code in select.c before it calls sqlite3WhereBegin()
146058 146602   ** to begin iterating through the sub-query results. It is used to allocate
146059 146603   ** and initialize registers and cursors used by sqlite3WindowCodeStep().
146060 146604   */
146061 146605   SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
146062 146606     Window *pWin;
146063 146607     Vdbe *v = sqlite3GetVdbe(pParse);
146064         -  int nPart = (pMWin->pPartition ? pMWin->pPartition->nExpr : 0);
146065         -  nPart += (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
146066         -  if( nPart ){
       146608  +
       146609  +  /* Allocate registers to use for PARTITION BY values, if any. Initialize
       146610  +  ** said registers to NULL.  */
       146611  +  if( pMWin->pPartition ){
       146612  +    int nExpr = pMWin->pPartition->nExpr;
146067 146613       pMWin->regPart = pParse->nMem+1;
146068         -    pParse->nMem += nPart;
146069         -    sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1);
       146614  +    pParse->nMem += nExpr;
       146615  +    sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
       146616  +  }
       146617  +
       146618  +  pMWin->regOne = ++pParse->nMem;
       146619  +  sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
       146620  +
       146621  +  if( pMWin->eExclude ){
       146622  +    pMWin->regStartRowid = ++pParse->nMem;
       146623  +    pMWin->regEndRowid = ++pParse->nMem;
       146624  +    pMWin->csrApp = pParse->nTab++;
       146625  +    sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
       146626  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
       146627  +    sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
       146628  +    return;
146070 146629     }
146071 146630   
146072 146631     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146073 146632       FuncDef *p = pWin->pFunc;
146074 146633       if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
146075 146634         /* The inline versions of min() and max() require a single ephemeral
146076 146635         ** table and 3 registers. The registers are used as follows:
................................................................................
146091 146650         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
146092 146651         sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
146093 146652         sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
146094 146653       }
146095 146654       else if( p->zName==nth_valueName || p->zName==first_valueName ){
146096 146655         /* Allocate two registers at pWin->regApp. These will be used to
146097 146656         ** store the start and end index of the current frame.  */
146098         -      assert( pMWin->iEphCsr );
146099 146657         pWin->regApp = pParse->nMem+1;
146100 146658         pWin->csrApp = pParse->nTab++;
146101 146659         pParse->nMem += 2;
146102 146660         sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
146103 146661       }
146104 146662       else if( p->zName==leadName || p->zName==lagName ){
146105         -      assert( pMWin->iEphCsr );
146106 146663         pWin->csrApp = pParse->nTab++;
146107 146664         sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
146108 146665       }
146109 146666     }
146110 146667   }
       146668  +
       146669  +#define WINDOW_STARTING_INT  0
       146670  +#define WINDOW_ENDING_INT    1
       146671  +#define WINDOW_NTH_VALUE_INT 2
       146672  +#define WINDOW_STARTING_NUM  3
       146673  +#define WINDOW_ENDING_NUM    4
146111 146674   
146112 146675   /*
146113 146676   ** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
146114 146677   ** value of the second argument to nth_value() (eCond==2) has just been
146115 146678   ** evaluated and the result left in register reg. This function generates VM
146116 146679   ** code to check that the value is a non-negative integer and throws an
146117 146680   ** exception if it is not.
146118 146681   */
146119         -static void windowCheckIntValue(Parse *pParse, int reg, int eCond){
       146682  +static void windowCheckValue(Parse *pParse, int reg, int eCond){
146120 146683     static const char *azErr[] = {
146121 146684       "frame starting offset must be a non-negative integer",
146122 146685       "frame ending offset must be a non-negative integer",
146123         -    "second argument to nth_value must be a positive integer"
       146686  +    "second argument to nth_value must be a positive integer",
       146687  +    "frame starting offset must be a non-negative number",
       146688  +    "frame ending offset must be a non-negative number",
146124 146689     };
146125         -  static int aOp[] = { OP_Ge, OP_Ge, OP_Gt };
       146690  +  static int aOp[] = { OP_Ge, OP_Ge, OP_Gt, OP_Ge, OP_Ge };
146126 146691     Vdbe *v = sqlite3GetVdbe(pParse);
146127 146692     int regZero = sqlite3GetTempReg(pParse);
146128         -  assert( eCond==0 || eCond==1 || eCond==2 );
       146693  +  assert( eCond>=0 && eCond<ArraySize(azErr) );
146129 146694     sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
146130         -  sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
146131         -  VdbeCoverageIf(v, eCond==0);
146132         -  VdbeCoverageIf(v, eCond==1);
146133         -  VdbeCoverageIf(v, eCond==2);
       146695  +  if( eCond>=WINDOW_STARTING_NUM ){
       146696  +    int regString = sqlite3GetTempReg(pParse);
       146697  +    sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
       146698  +    sqlite3VdbeAddOp3(v, OP_Ge, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
       146699  +    sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC|SQLITE_JUMPIFNULL);
       146700  +    VdbeCoverage(v);
       146701  +    assert( eCond==3 || eCond==4 );
       146702  +    VdbeCoverageIf(v, eCond==3);
       146703  +    VdbeCoverageIf(v, eCond==4);
       146704  +  }else{
       146705  +    sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
       146706  +    VdbeCoverage(v);
       146707  +    assert( eCond==0 || eCond==1 || eCond==2 );
       146708  +    VdbeCoverageIf(v, eCond==0);
       146709  +    VdbeCoverageIf(v, eCond==1);
       146710  +    VdbeCoverageIf(v, eCond==2);
       146711  +  }
146134 146712     sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
146135         -  VdbeCoverageNeverNullIf(v, eCond==0);
146136         -  VdbeCoverageNeverNullIf(v, eCond==1);
       146713  +  VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
       146714  +  VdbeCoverageNeverNullIf(v, eCond==1); /*   the OP_MustBeInt */
146137 146715     VdbeCoverageNeverNullIf(v, eCond==2);
       146716  +  VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
       146717  +  VdbeCoverageNeverNullIf(v, eCond==4); /*   the OP_Ge */
146138 146718     sqlite3MayAbort(pParse);
146139 146719     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
146140 146720     sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC);
146141 146721     sqlite3ReleaseTempReg(pParse, regZero);
146142 146722   }
146143 146723   
146144 146724   /*
................................................................................
146170 146750   ** number of rows in the current partition.
146171 146751   */
146172 146752   static void windowAggStep(
146173 146753     Parse *pParse, 
146174 146754     Window *pMWin,                  /* Linked list of window functions */
146175 146755     int csr,                        /* Read arguments from this cursor */
146176 146756     int bInverse,                   /* True to invoke xInverse instead of xStep */
146177         -  int reg,                        /* Array of registers */
146178         -  int regPartSize                 /* Register containing size of partition */
       146757  +  int reg                         /* Array of registers */
146179 146758   ){
146180 146759     Vdbe *v = sqlite3GetVdbe(pParse);
146181 146760     Window *pWin;
146182 146761     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146183         -    int flags = pWin->pFunc->funcFlags;
       146762  +    FuncDef *pFunc = pWin->pFunc;
146184 146763       int regArg;
146185 146764       int nArg = windowArgCount(pWin);
       146765  +    int i;
146186 146766   
146187         -    if( csr>=0 ){
146188         -      int i;
146189         -      for(i=0; i<nArg; i++){
       146767  +    for(i=0; i<nArg; i++){
       146768  +      if( i!=1 || pFunc->zName!=nth_valueName ){
146190 146769           sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
       146770  +      }else{
       146771  +        sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
146191 146772         }
146192         -      regArg = reg;
146193         -      if( flags & SQLITE_FUNC_WINDOW_SIZE ){
146194         -        if( nArg==0 ){
146195         -          regArg = regPartSize;
146196         -        }else{
146197         -          sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg);
146198         -        }
146199         -        nArg++;
146200         -      }
146201         -    }else{
146202         -      assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) );
146203         -      regArg = reg + pWin->iArgCol;
146204 146773       }
       146774  +    regArg = reg;
146205 146775   
146206         -    if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
146207         -      && pWin->eStart!=TK_UNBOUNDED 
       146776  +    if( pMWin->regStartRowid==0
       146777  +     && (pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
       146778  +     && (pWin->eStart!=TK_UNBOUNDED)
146208 146779       ){
146209 146780         int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regArg);
146210 146781         VdbeCoverage(v);
146211 146782         if( bInverse==0 ){
146212 146783           sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
146213 146784           sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
146214 146785           sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
................................................................................
146217 146788           sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
146218 146789           VdbeCoverageNeverTaken(v);
146219 146790           sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
146220 146791           sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
146221 146792         }
146222 146793         sqlite3VdbeJumpHere(v, addrIsNull);
146223 146794       }else if( pWin->regApp ){
146224         -      assert( pWin->pFunc->zName==nth_valueName
146225         -           || pWin->pFunc->zName==first_valueName
       146795  +      assert( pFunc->zName==nth_valueName
       146796  +           || pFunc->zName==first_valueName
146226 146797         );
146227 146798         assert( bInverse==0 || bInverse==1 );
146228 146799         sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
146229         -    }else if( pWin->pFunc->zName==leadName
146230         -           || pWin->pFunc->zName==lagName
146231         -    ){
146232         -      /* no-op */
146233         -    }else{
       146800  +    }else if( pFunc->xSFunc!=noopStepFunc ){
146234 146801         int addrIf = 0;
146235 146802         if( pWin->pFilter ){
146236 146803           int regTmp;
146237 146804           assert( nArg==0 || nArg==pWin->pOwner->x.pList->nExpr );
146238 146805           assert( nArg || pWin->pOwner->x.pList==0 );
146239         -        if( csr>0 ){
146240         -          regTmp = sqlite3GetTempReg(pParse);
146241         -          sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
146242         -        }else{
146243         -          regTmp = regArg + nArg;
146244         -        }
       146806  +        regTmp = sqlite3GetTempReg(pParse);
       146807  +        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
146245 146808           addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
146246 146809           VdbeCoverage(v);
146247         -        if( csr>0 ){
146248         -          sqlite3ReleaseTempReg(pParse, regTmp);
146249         -        }
       146810  +        sqlite3ReleaseTempReg(pParse, regTmp);
146250 146811         }
146251         -      if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
       146812  +      if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
146252 146813           CollSeq *pColl;
146253 146814           assert( nArg>0 );
146254 146815           pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
146255 146816           sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
146256 146817         }
146257 146818         sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep, 
146258 146819                           bInverse, regArg, pWin->regAccum);
146259         -      sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
       146820  +      sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
146260 146821         sqlite3VdbeChangeP5(v, (u8)nArg);
146261 146822         if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
146262 146823       }
146263 146824     }
146264 146825   }
146265 146826   
       146827  +typedef struct WindowCodeArg WindowCodeArg;
       146828  +typedef struct WindowCsrAndReg WindowCsrAndReg;
       146829  +struct WindowCsrAndReg {
       146830  +  int csr;
       146831  +  int reg;
       146832  +};
       146833  +
       146834  +struct WindowCodeArg {
       146835  +  Parse *pParse;
       146836  +  Window *pMWin;
       146837  +  Vdbe *pVdbe;
       146838  +  int regGosub;
       146839  +  int addrGosub;
       146840  +  int regArg;
       146841  +  int eDelete;
       146842  +
       146843  +  WindowCsrAndReg start;
       146844  +  WindowCsrAndReg current;
       146845  +  WindowCsrAndReg end;
       146846  +};
       146847  +
146266 146848   /*
146267         -** Generate VM code to invoke either xValue() (bFinal==0) or xFinalize()
146268         -** (bFinal==1) for each window function in the linked list starting at
       146849  +** Values that may be passed as the second argument to windowCodeOp().
       146850  +*/
       146851  +#define WINDOW_RETURN_ROW 1
       146852  +#define WINDOW_AGGINVERSE 2
       146853  +#define WINDOW_AGGSTEP    3
       146854  +
       146855  +/*
       146856  +** Generate VM code to read the window frames peer values from cursor csr into
       146857  +** an array of registers starting at reg.
       146858  +*/
       146859  +static void windowReadPeerValues(
       146860  +  WindowCodeArg *p,
       146861  +  int csr,
       146862  +  int reg
       146863  +){
       146864  +  Window *pMWin = p->pMWin;
       146865  +  ExprList *pOrderBy = pMWin->pOrderBy;
       146866  +  if( pOrderBy ){
       146867  +    Vdbe *v = sqlite3GetVdbe(p->pParse);
       146868  +    ExprList *pPart = pMWin->pPartition;
       146869  +    int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
       146870  +    int i;
       146871  +    for(i=0; i<pOrderBy->nExpr; i++){
       146872  +      sqlite3VdbeAddOp3(v, OP_Column, csr, iColOff+i, reg+i);
       146873  +    }
       146874  +  }
       146875  +}
       146876  +
       146877  +/*
       146878  +** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
       146879  +** (bFin==1) for each window function in the linked list starting at
146269 146880   ** pMWin. Or, for built-in window-functions that do not use the standard
146270 146881   ** API, generate the equivalent VM code.
146271 146882   */
146272         -static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){
       146883  +static void windowAggFinal(WindowCodeArg *p, int bFin){
       146884  +  Parse *pParse = p->pParse;
       146885  +  Window *pMWin = p->pMWin;
146273 146886     Vdbe *v = sqlite3GetVdbe(pParse);
146274 146887     Window *pWin;
146275 146888   
146276 146889     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146277         -    if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
146278         -     && pWin->eStart!=TK_UNBOUNDED 
       146890  +    if( pMWin->regStartRowid==0
       146891  +     && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
       146892  +     && (pWin->eStart!=TK_UNBOUNDED)
146279 146893       ){
146280 146894         sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146281 146895         sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
146282 146896         VdbeCoverage(v);
146283 146897         sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
146284 146898         sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
146285         -      if( bFinal ){
146286         -        sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
146287         -      }
146288 146899       }else if( pWin->regApp ){
       146900  +      assert( pMWin->regStartRowid==0 );
146289 146901       }else{
146290         -      if( bFinal ){
146291         -        sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin));
       146902  +      int nArg = windowArgCount(pWin);
       146903  +      if( bFin ){
       146904  +        sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
146292 146905           sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
146293 146906           sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
146294 146907           sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
146295 146908         }else{
146296         -        sqlite3VdbeAddOp3(v, OP_AggValue, pWin->regAccum, windowArgCount(pWin),
146297         -                             pWin->regResult);
       146909  +        sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
146298 146910           sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
146299 146911         }
146300 146912       }
146301 146913     }
146302 146914   }
146303 146915   
146304 146916   /*
146305         -** This function generates VM code to invoke the sub-routine at address
146306         -** lblFlushPart once for each partition with the entire partition cached in
146307         -** the Window.iEphCsr temp table.
146308         -*/
146309         -static void windowPartitionCache(
146310         -  Parse *pParse,
146311         -  Select *p,                      /* The rewritten SELECT statement */
146312         -  WhereInfo *pWInfo,              /* WhereInfo to call WhereEnd() on */
146313         -  int regFlushPart,               /* Register to use with Gosub lblFlushPart */
146314         -  int lblFlushPart,               /* Subroutine to Gosub to */
146315         -  int *pRegSize                   /* OUT: Register containing partition size */
146316         -){
146317         -  Window *pMWin = p->pWin;
146318         -  Vdbe *v = sqlite3GetVdbe(pParse);
146319         -  int iSubCsr = p->pSrc->a[0].iCursor;
146320         -  int nSub = p->pSrc->a[0].pTab->nCol;
146321         -  int k;
146322         -
146323         -  int reg = pParse->nMem+1;
146324         -  int regRecord = reg+nSub;
146325         -  int regRowid = regRecord+1;
146326         -
146327         -  *pRegSize = regRowid;
146328         -  pParse->nMem += nSub + 2;
146329         -
146330         -  /* Load the column values for the row returned by the sub-select
146331         -  ** into an array of registers starting at reg. */
146332         -  for(k=0; k<nSub; k++){
146333         -    sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
146334         -  }
146335         -  sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);
146336         -
146337         -  /* Check if this is the start of a new partition. If so, call the
146338         -  ** flush_partition sub-routine.  */
146339         -  if( pMWin->pPartition ){
       146917  +** Generate code to calculate the current values of all window functions in the
       146918  +** p->pMWin list by doing a full scan of the current window frame. Store the
       146919  +** results in the Window.regResult registers, ready to return the upper
       146920  +** layer.
       146921  +*/
       146922  +static void windowFullScan(WindowCodeArg *p){
       146923  +  Window *pWin;
       146924  +  Parse *pParse = p->pParse;
       146925  +  Window *pMWin = p->pMWin;
       146926  +  Vdbe *v = p->pVdbe;
       146927  +
       146928  +  int regCRowid = 0;              /* Current rowid value */
       146929  +  int regCPeer = 0;               /* Current peer values */
       146930  +  int regRowid = 0;               /* AggStep rowid value */
       146931  +  int regPeer = 0;                /* AggStep peer values */
       146932  +
       146933  +  int nPeer;
       146934  +  int lblNext;
       146935  +  int lblBrk;
       146936  +  int addrNext;
       146937  +  int csr = pMWin->csrApp;
       146938  +
       146939  +  nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
       146940  +
       146941  +  lblNext = sqlite3VdbeMakeLabel(pParse);
       146942  +  lblBrk = sqlite3VdbeMakeLabel(pParse);
       146943  +
       146944  +  regCRowid = sqlite3GetTempReg(pParse);
       146945  +  regRowid = sqlite3GetTempReg(pParse);
       146946  +  if( nPeer ){
       146947  +    regCPeer = sqlite3GetTempRange(pParse, nPeer);
       146948  +    regPeer = sqlite3GetTempRange(pParse, nPeer);
       146949  +  }
       146950  +
       146951  +  sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
       146952  +  windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
       146953  +
       146954  +  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
       146955  +    sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
       146956  +  }
       146957  +
       146958  +  sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
       146959  +  VdbeCoverage(v);
       146960  +  addrNext = sqlite3VdbeCurrentAddr(v);
       146961  +  sqlite3VdbeAddOp2(v, OP_Rowid, csr, regRowid);
       146962  +  sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
       146963  +  VdbeCoverageNeverNull(v);
       146964  +
       146965  +  if( pMWin->eExclude==TK_CURRENT ){
       146966  +    sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, lblNext, regRowid);
       146967  +    VdbeCoverageNeverNull(v);
       146968  +  }else if( pMWin->eExclude!=TK_NO ){
146340 146969       int addr;
146341         -    ExprList *pPart = pMWin->pPartition;
146342         -    int nPart = pPart->nExpr;
146343         -    int regNewPart = reg + pMWin->nBufferCol;
146344         -    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
146345         -
146346         -    addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
146347         -    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
146348         -    sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
146349         -    VdbeCoverageEqNe(v);
146350         -    sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
146351         -    sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
146352         -    VdbeComment((v, "call flush_partition"));
146353         -  }
146354         -
146355         -  /* Buffer the current row in the ephemeral table. */
146356         -  sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
146357         -  sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
146358         -
146359         -  /* End of the input loop */
146360         -  sqlite3WhereEnd(pWInfo);
146361         -
146362         -  /* Invoke "flush_partition" to deal with the final (or only) partition */
146363         -  sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
146364         -  VdbeComment((v, "call flush_partition"));
       146970  +    int addrEq = 0;
       146971  +    KeyInfo *pKeyInfo = 0;
       146972  +
       146973  +    if( pMWin->pOrderBy ){
       146974  +      pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
       146975  +    }
       146976  +    if( pMWin->eExclude==TK_TIES ){
       146977  +      addrEq = sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, 0, regRowid);
       146978  +      VdbeCoverageNeverNull(v);
       146979  +    }
       146980  +    if( pKeyInfo ){
       146981  +      windowReadPeerValues(p, csr, regPeer);
       146982  +      sqlite3VdbeAddOp3(v, OP_Compare, regPeer, regCPeer, nPeer);
       146983  +      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
       146984  +      addr = sqlite3VdbeCurrentAddr(v)+1;
       146985  +      sqlite3VdbeAddOp3(v, OP_Jump, addr, lblNext, addr);
       146986  +      VdbeCoverageEqNe(v);
       146987  +    }else{
       146988  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, lblNext);
       146989  +    }
       146990  +    if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
       146991  +  }
       146992  +
       146993  +  windowAggStep(pParse, pMWin, csr, 0, p->regArg);
       146994  +
       146995  +  sqlite3VdbeResolveLabel(v, lblNext);
       146996  +  sqlite3VdbeAddOp2(v, OP_Next, csr, addrNext);
       146997  +  VdbeCoverage(v);
       146998  +  sqlite3VdbeJumpHere(v, addrNext-1);
       146999  +  sqlite3VdbeJumpHere(v, addrNext+1);
       147000  +  sqlite3ReleaseTempReg(pParse, regRowid);
       147001  +  sqlite3ReleaseTempReg(pParse, regCRowid);
       147002  +  if( nPeer ){
       147003  +    sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
       147004  +    sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
       147005  +  }
       147006  +
       147007  +  windowAggFinal(p, 1);
146365 147008   }
146366 147009   
146367 147010   /*
146368 147011   ** Invoke the sub-routine at regGosub (generated by code in select.c) to
146369 147012   ** return the current row of Window.iEphCsr. If all window functions are
146370 147013   ** aggregate window functions that use the standard API, a single
146371 147014   ** OP_Gosub instruction is all that this routine generates. Extra VM code
................................................................................
146373 147016   ** functions:
146374 147017   **
146375 147018   **   nth_value()
146376 147019   **   first_value()
146377 147020   **   lag()
146378 147021   **   lead()
146379 147022   */
146380         -static void windowReturnOneRow(
146381         -  Parse *pParse,
146382         -  Window *pMWin,
146383         -  int regGosub,
146384         -  int addrGosub
146385         -){
146386         -  Vdbe *v = sqlite3GetVdbe(pParse);
146387         -  Window *pWin;
146388         -  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146389         -    FuncDef *pFunc = pWin->pFunc;
146390         -    if( pFunc->zName==nth_valueName
146391         -     || pFunc->zName==first_valueName
146392         -    ){
146393         -      int csr = pWin->csrApp;
146394         -      int lbl = sqlite3VdbeMakeLabel(pParse);
146395         -      int tmpReg = sqlite3GetTempReg(pParse);
146396         -      sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146397         -
146398         -      if( pFunc->zName==nth_valueName ){
146399         -        sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg);
146400         -        windowCheckIntValue(pParse, tmpReg, 2);
146401         -      }else{
146402         -        sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
146403         -      }
146404         -      sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
146405         -      sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
146406         -      VdbeCoverageNeverNull(v);
146407         -      sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
146408         -      VdbeCoverageNeverTaken(v);
146409         -      sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
146410         -      sqlite3VdbeResolveLabel(v, lbl);
146411         -      sqlite3ReleaseTempReg(pParse, tmpReg);
146412         -    }
146413         -    else if( pFunc->zName==leadName || pFunc->zName==lagName ){
146414         -      int nArg = pWin->pOwner->x.pList->nExpr;
146415         -      int iEph = pMWin->iEphCsr;
146416         -      int csr = pWin->csrApp;
146417         -      int lbl = sqlite3VdbeMakeLabel(pParse);
146418         -      int tmpReg = sqlite3GetTempReg(pParse);
146419         -
146420         -      if( nArg<3 ){
       147023  +static void windowReturnOneRow(WindowCodeArg *p){
       147024  +  Window *pMWin = p->pMWin;
       147025  +  Vdbe *v = p->pVdbe;
       147026  +
       147027  +  if( pMWin->regStartRowid ){
       147028  +    windowFullScan(p);
       147029  +  }else{
       147030  +    Parse *pParse = p->pParse;
       147031  +    Window *pWin;
       147032  +
       147033  +    for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
       147034  +      FuncDef *pFunc = pWin->pFunc;
       147035  +      if( pFunc->zName==nth_valueName
       147036  +       || pFunc->zName==first_valueName
       147037  +      ){
       147038  +        int csr = pWin->csrApp;
       147039  +        int lbl = sqlite3VdbeMakeLabel(pParse);
       147040  +        int tmpReg = sqlite3GetTempReg(pParse);
146421 147041           sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146422         -      }else{
146423         -        sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+2, pWin->regResult);
146424         -      }
146425         -      sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
146426         -      if( nArg<2 ){
146427         -        int val = (pFunc->zName==leadName ? 1 : -1);
146428         -        sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
146429         -      }else{
146430         -        int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
146431         -        int tmpReg2 = sqlite3GetTempReg(pParse);
146432         -        sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
146433         -        sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
146434         -        sqlite3ReleaseTempReg(pParse, tmpReg2);
146435         -      }
146436         -
146437         -      sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
146438         -      VdbeCoverage(v);
146439         -      sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
146440         -      sqlite3VdbeResolveLabel(v, lbl);
146441         -      sqlite3ReleaseTempReg(pParse, tmpReg);
146442         -    }
146443         -  }
146444         -  sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
146445         -}
146446         -
146447         -/*
146448         -** Invoke the code generated by windowReturnOneRow() and, optionally, the
146449         -** xInverse() function for each window function, for one or more rows
146450         -** from the Window.iEphCsr temp table. This routine generates VM code
146451         -** similar to:
146452         -**
146453         -**   while( regCtr>0 ){
146454         -**     regCtr--;
146455         -**     windowReturnOneRow()
146456         -**     if( bInverse ){
146457         -**       AggInverse
146458         -**     }
146459         -**     Next (Window.iEphCsr)
146460         -**   }
146461         -*/
146462         -static void windowReturnRows(
146463         -  Parse *pParse,
146464         -  Window *pMWin,                  /* List of window functions */
146465         -  int regCtr,                     /* Register containing number of rows */
146466         -  int regGosub,                   /* Register for Gosub addrGosub */
146467         -  int addrGosub,                  /* Address of sub-routine for ReturnOneRow */
146468         -  int regInvArg,                  /* Array of registers for xInverse args */
146469         -  int regInvSize                  /* Register containing size of partition */
146470         -){
146471         -  int addr;
146472         -  Vdbe *v = sqlite3GetVdbe(pParse);
146473         -  windowAggFinal(pParse, pMWin, 0);
146474         -  addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1);
146475         -  VdbeCoverage(v);
146476         -  sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
146477         -  windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
146478         -  if( regInvArg ){
146479         -    windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize);
146480         -  }
146481         -  sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr);
146482         -  VdbeCoverage(v);
146483         -  sqlite3VdbeJumpHere(v, addr+1);   /* The OP_Goto */
       147042  +  
       147043  +        if( pFunc->zName==nth_valueName ){
       147044  +          sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
       147045  +          windowCheckValue(pParse, tmpReg, 2);
       147046  +        }else{
       147047  +          sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
       147048  +        }
       147049  +        sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
       147050  +        sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
       147051  +        VdbeCoverageNeverNull(v);
       147052  +        sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
       147053  +        VdbeCoverageNeverTaken(v);
       147054  +        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
       147055  +        sqlite3VdbeResolveLabel(v, lbl);
       147056  +        sqlite3ReleaseTempReg(pParse, tmpReg);
       147057  +      }
       147058  +      else if( pFunc->zName==leadName || pFunc->zName==lagName ){
       147059  +        int nArg = pWin->pOwner->x.pList->nExpr;
       147060  +        int csr = pWin->csrApp;
       147061  +        int lbl = sqlite3VdbeMakeLabel(pParse);
       147062  +        int tmpReg = sqlite3GetTempReg(pParse);
       147063  +        int iEph = pMWin->iEphCsr;
       147064  +  
       147065  +        if( nArg<3 ){
       147066  +          sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
       147067  +        }else{
       147068  +          sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
       147069  +        }
       147070  +        sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
       147071  +        if( nArg<2 ){
       147072  +          int val = (pFunc->zName==leadName ? 1 : -1);
       147073  +          sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
       147074  +        }else{
       147075  +          int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
       147076  +          int tmpReg2 = sqlite3GetTempReg(pParse);
       147077  +          sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
       147078  +          sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
       147079  +          sqlite3ReleaseTempReg(pParse, tmpReg2);
       147080  +        }
       147081  +  
       147082  +        sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
       147083  +        VdbeCoverage(v);
       147084  +        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
       147085  +        sqlite3VdbeResolveLabel(v, lbl);
       147086  +        sqlite3ReleaseTempReg(pParse, tmpReg);
       147087  +      }
       147088  +    }
       147089  +  }
       147090  +  sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
146484 147091   }
146485 147092   
146486 147093   /*
146487 147094   ** Generate code to set the accumulator register for each window function
146488 147095   ** in the linked list passed as the second argument to NULL. And perform
146489 147096   ** any equivalent initialization required by any built-in window functions
146490 147097   ** in the list.
................................................................................
146494 147101     int regArg;
146495 147102     int nArg = 0;
146496 147103     Window *pWin;
146497 147104     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146498 147105       FuncDef *pFunc = pWin->pFunc;
146499 147106       sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
146500 147107       nArg = MAX(nArg, windowArgCount(pWin));
146501         -    if( pFunc->zName==nth_valueName
146502         -     || pFunc->zName==first_valueName
146503         -    ){
146504         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
146505         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
146506         -    }
       147108  +    if( pMWin->regStartRowid==0 ){
       147109  +      if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
       147110  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
       147111  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
       147112  +      }
146507 147113   
146508         -    if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
146509         -      assert( pWin->eStart!=TK_UNBOUNDED );
146510         -      sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
146511         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
       147114  +      if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
       147115  +        assert( pWin->eStart!=TK_UNBOUNDED );
       147116  +        sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
       147117  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
       147118  +      }
146512 147119       }
146513 147120     }
146514 147121     regArg = pParse->nMem+1;
146515 147122     pParse->nMem += nArg;
146516 147123     return regArg;
146517 147124   }
146518 147125   
146519         -
146520         -/*
146521         -** This function does the work of sqlite3WindowCodeStep() for all "ROWS"
146522         -** window frame types except for "BETWEEN UNBOUNDED PRECEDING AND CURRENT
146523         -** ROW". Pseudo-code for each follows.
146524         -**
146525         -** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
146526         -**
146527         -**     ...
146528         -**       if( new partition ){
146529         -**         Gosub flush_partition
146530         -**       }
146531         -**       Insert (record in eph-table)
146532         -**     sqlite3WhereEnd()
146533         -**     Gosub flush_partition
146534         -**  
146535         -**   flush_partition:
146536         -**     Once {
146537         -**       OpenDup (iEphCsr -> csrStart)
146538         -**       OpenDup (iEphCsr -> csrEnd)
146539         -**     }
146540         -**     regStart = <expr1>                // PRECEDING expression
146541         -**     regEnd = <expr2>                  // FOLLOWING expression
146542         -**     if( regStart<0 || regEnd<0 ){ error! }
146543         -**     Rewind (csr,csrStart,csrEnd)      // if EOF goto flush_partition_done
146544         -**       Next(csrEnd)                    // if EOF skip Aggstep
146545         -**       Aggstep (csrEnd)
146546         -**       if( (regEnd--)<=0 ){
146547         -**         AggFinal (xValue)
146548         -**         Gosub addrGosub
146549         -**         Next(csr)                // if EOF goto flush_partition_done
146550         -**         if( (regStart--)<=0 ){
146551         -**           AggInverse (csrStart)
146552         -**           Next(csrStart)
146553         -**         }
146554         -**       }
146555         -**   flush_partition_done:
146556         -**     ResetSorter (csr)
146557         -**     Return
146558         -**
146559         -** ROWS BETWEEN <expr> PRECEDING    AND CURRENT ROW
146560         -** ROWS BETWEEN CURRENT ROW         AND <expr> FOLLOWING
146561         -** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING
146562         -**
146563         -**   These are similar to the above. For "CURRENT ROW", intialize the
146564         -**   register to 0. For "UNBOUNDED PRECEDING" to infinity.
146565         -**
146566         -** ROWS BETWEEN <expr> PRECEDING    AND UNBOUNDED FOLLOWING
146567         -** ROWS BETWEEN CURRENT ROW         AND UNBOUNDED FOLLOWING
146568         -**
146569         -**     Rewind (csr,csrStart,csrEnd)    // if EOF goto flush_partition_done
146570         -**     while( 1 ){
146571         -**       Next(csrEnd)                  // Exit while(1) at EOF
146572         -**       Aggstep (csrEnd)
146573         -**     }
146574         -**     while( 1 ){
146575         -**       AggFinal (xValue)
146576         -**       Gosub addrGosub
146577         -**       Next(csr)                     // if EOF goto flush_partition_done
146578         -**       if( (regStart--)<=0 ){
146579         -**         AggInverse (csrStart)
146580         -**         Next(csrStart)
146581         -**       }
146582         -**     }
146583         -**
146584         -**   For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if() 
146585         -**   condition is always true (as if regStart were initialized to 0).
146586         -**
146587         -** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146588         -** 
146589         -**   This is the only RANGE case handled by this routine. It modifies the
146590         -**   second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to
146591         -**   be:
146592         -**
146593         -**     while( 1 ){
146594         -**       AggFinal (xValue)
146595         -**       while( 1 ){
146596         -**         regPeer++
146597         -**         Gosub addrGosub
146598         -**         Next(csr)                     // if EOF goto flush_partition_done
146599         -**         if( new peer ) break;
146600         -**       }
146601         -**       while( (regPeer--)>0 ){
146602         -**         AggInverse (csrStart)
146603         -**         Next(csrStart)
146604         -**       }
146605         -**     }
146606         -**
146607         -** ROWS BETWEEN <expr> FOLLOWING    AND <expr> FOLLOWING
146608         -**
146609         -**   regEnd = regEnd - regStart
146610         -**   Rewind (csr,csrStart,csrEnd)   // if EOF goto flush_partition_done
146611         -**     Aggstep (csrEnd)
146612         -**     Next(csrEnd)                 // if EOF fall-through
146613         -**     if( (regEnd--)<=0 ){
146614         -**       if( (regStart--)<=0 ){
146615         -**         AggFinal (xValue)
146616         -**         Gosub addrGosub
146617         -**         Next(csr)              // if EOF goto flush_partition_done
146618         -**       }
146619         -**       AggInverse (csrStart)
146620         -**       Next (csrStart)
146621         -**     }
146622         -**
146623         -** ROWS BETWEEN <expr> PRECEDING    AND <expr> PRECEDING
146624         -**
146625         -**   Replace the bit after "Rewind" in the above with:
146626         -**
146627         -**     if( (regEnd--)<=0 ){
146628         -**       AggStep (csrEnd)
146629         -**       Next (csrEnd)
146630         -**     }
146631         -**     AggFinal (xValue)
146632         -**     Gosub addrGosub
146633         -**     Next(csr)                  // if EOF goto flush_partition_done
146634         -**     if( (regStart--)<=0 ){
146635         -**       AggInverse (csr2)
146636         -**       Next (csr2)
146637         -**     }
146638         -**
146639         -*/
146640         -static void windowCodeRowExprStep(
146641         -  Parse *pParse, 
146642         -  Select *p,
146643         -  WhereInfo *pWInfo,
146644         -  int regGosub, 
146645         -  int addrGosub
146646         -){
146647         -  Window *pMWin = p->pWin;
       147126  +/* 
       147127  +** Return true if the current frame should be cached in the ephemeral table,
       147128  +** even if there are no xInverse() calls required.
       147129  +*/
       147130  +static int windowCacheFrame(Window *pMWin){
       147131  +  Window *pWin;
       147132  +  if( pMWin->regStartRowid ) return 1;
       147133  +  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
       147134  +    FuncDef *pFunc = pWin->pFunc;
       147135  +    if( (pFunc->zName==nth_valueName)
       147136  +     || (pFunc->zName==first_valueName)
       147137  +     || (pFunc->zName==leadName)
       147138  +     || (pFunc->zName==lagName)
       147139  +    ){
       147140  +      return 1;
       147141  +    }
       147142  +  }
       147143  +  return 0;
       147144  +}
       147145  +
       147146  +/*
       147147  +** regOld and regNew are each the first register in an array of size
       147148  +** pOrderBy->nExpr. This function generates code to compare the two
       147149  +** arrays of registers using the collation sequences and other comparison
       147150  +** parameters specified by pOrderBy. 
       147151  +**
       147152  +** If the two arrays are not equal, the contents of regNew is copied to 
       147153  +** regOld and control falls through. Otherwise, if the contents of the arrays
       147154  +** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
       147155  +*/
       147156  +static void windowIfNewPeer(
       147157  +  Parse *pParse,
       147158  +  ExprList *pOrderBy,
       147159  +  int regNew,                     /* First in array of new values */
       147160  +  int regOld,                     /* First in array of old values */
       147161  +  int addr                        /* Jump here */
       147162  +){
       147163  +  Vdbe *v = sqlite3GetVdbe(pParse);
       147164  +  if( pOrderBy ){
       147165  +    int nVal = pOrderBy->nExpr;
       147166  +    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
       147167  +    sqlite3VdbeAddOp3(v, OP_Compare, regOld, regNew, nVal);
       147168  +    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
       147169  +    sqlite3VdbeAddOp3(v, OP_Jump, 
       147170  +      sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1
       147171  +    );
       147172  +    VdbeCoverageEqNe(v);
       147173  +    sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
       147174  +  }else{
       147175  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
       147176  +  }
       147177  +}
       147178  +
       147179  +/*
       147180  +** This function is called as part of generating VM programs for RANGE
       147181  +** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
       147182  +** the ORDER BY term in the window, it generates code equivalent to:
       147183  +**
       147184  +**   if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
       147185  +**
       147186  +** A special type of arithmetic is used such that if csr.peerVal is not
       147187  +** a numeric type (real or integer), then the result of the addition is
       147188  +** a copy of csr1.peerVal.
       147189  +*/
       147190  +static void windowCodeRangeTest(
       147191  +  WindowCodeArg *p, 
       147192  +  int op,                          /* OP_Ge or OP_Gt */
       147193  +  int csr1, 
       147194  +  int regVal, 
       147195  +  int csr2,
       147196  +  int lbl
       147197  +){
       147198  +  Parse *pParse = p->pParse;
146648 147199     Vdbe *v = sqlite3GetVdbe(pParse);
146649         -  int regFlushPart;               /* Register for "Gosub flush_partition" */
146650         -  int lblFlushPart;               /* Label for "Gosub flush_partition" */
146651         -  int lblFlushDone;               /* Label for "Gosub flush_partition_done" */
146652         -
146653         -  int regArg;
146654         -  int addr;
146655         -  int csrStart = pParse->nTab++;
146656         -  int csrEnd = pParse->nTab++;
146657         -  int regStart;                    /* Value of <expr> PRECEDING */
146658         -  int regEnd;                      /* Value of <expr> FOLLOWING */
146659         -  int addrGoto;
146660         -  int addrTop;
146661         -  int addrIfPos1 = 0;
146662         -  int addrIfPos2 = 0;
146663         -  int regSize = 0;
146664         -
146665         -  assert( pMWin->eStart==TK_PRECEDING 
146666         -       || pMWin->eStart==TK_CURRENT 
146667         -       || pMWin->eStart==TK_FOLLOWING 
146668         -       || pMWin->eStart==TK_UNBOUNDED 
146669         -  );
146670         -  assert( pMWin->eEnd==TK_FOLLOWING 
146671         -       || pMWin->eEnd==TK_CURRENT 
146672         -       || pMWin->eEnd==TK_UNBOUNDED 
146673         -       || pMWin->eEnd==TK_PRECEDING 
146674         -  );
146675         -
146676         -  /* Allocate register and label for the "flush_partition" sub-routine. */
146677         -  regFlushPart = ++pParse->nMem;
146678         -  lblFlushPart = sqlite3VdbeMakeLabel(pParse);
146679         -  lblFlushDone = sqlite3VdbeMakeLabel(pParse);
146680         -
146681         -  regStart = ++pParse->nMem;
146682         -  regEnd = ++pParse->nMem;
146683         -
146684         -  windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
146685         -
146686         -  addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146687         -
146688         -  /* Start of "flush_partition" */
146689         -  sqlite3VdbeResolveLabel(v, lblFlushPart);
146690         -  sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3);
       147200  +  int reg1 = sqlite3GetTempReg(pParse);
       147201  +  int reg2 = sqlite3GetTempReg(pParse);
       147202  +  int arith = OP_Add;
       147203  +  int addrGe;
       147204  +
       147205  +  int regString = ++pParse->nMem;
       147206  +
       147207  +  assert( op==OP_Ge || op==OP_Gt || op==OP_Le );
       147208  +  assert( p->pMWin->pOrderBy && p->pMWin->pOrderBy->nExpr==1 );
       147209  +  if( p->pMWin->pOrderBy->a[0].sortOrder ){
       147210  +    switch( op ){
       147211  +      case OP_Ge: op = OP_Le; break;
       147212  +      case OP_Gt: op = OP_Lt; break;
       147213  +      default: assert( op==OP_Le ); op = OP_Ge; break;
       147214  +    }
       147215  +    arith = OP_Subtract;
       147216  +  }
       147217  +
       147218  +  windowReadPeerValues(p, csr1, reg1);
       147219  +  windowReadPeerValues(p, csr2, reg2);
       147220  +
       147221  +  /* Check if the peer value for csr1 value is a text or blob by comparing
       147222  +  ** it to the smallest possible string - ''. If it is, jump over the
       147223  +  ** OP_Add or OP_Subtract operation and proceed directly to the comparison. */
       147224  +  sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
       147225  +  addrGe = sqlite3VdbeAddOp3(v, OP_Ge, regString, 0, reg1);
146691 147226     VdbeCoverage(v);
146692         -  VdbeComment((v, "Flush_partition subroutine"));
146693         -  sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr);
146694         -  sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr);
146695         -
146696         -  /* If either regStart or regEnd are not non-negative integers, throw 
146697         -  ** an exception.  */
146698         -  if( pMWin->pStart ){
146699         -    sqlite3ExprCode(pParse, pMWin->pStart, regStart);
146700         -    windowCheckIntValue(pParse, regStart, 0);
146701         -  }
146702         -  if( pMWin->pEnd ){
146703         -    sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
146704         -    windowCheckIntValue(pParse, regEnd, 1);
146705         -  }
146706         -
146707         -  /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
146708         -  **
146709         -  **   if( regEnd<regStart ){
146710         -  **     // The frame always consists of 0 rows
146711         -  **     regStart = regSize;
146712         -  **   }
146713         -  **   regEnd = regEnd - regStart;
146714         -  */
146715         -  if( pMWin->pEnd && pMWin->eStart==TK_FOLLOWING ){
146716         -    assert( pMWin->pStart!=0 );
146717         -    assert( pMWin->eEnd==TK_FOLLOWING );
146718         -    sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd);
146719         -    VdbeCoverageNeverNull(v);
146720         -    sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
146721         -    sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd);
146722         -  }
146723         -
146724         -  if( pMWin->pStart && pMWin->eEnd==TK_PRECEDING ){
146725         -    assert( pMWin->pEnd!=0 );
146726         -    assert( pMWin->eStart==TK_PRECEDING );
146727         -    sqlite3VdbeAddOp3(v, OP_Le, regStart, sqlite3VdbeCurrentAddr(v)+3, regEnd);
146728         -    VdbeCoverageNeverNull(v);
146729         -    sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
146730         -    sqlite3VdbeAddOp2(v, OP_Copy, regSize, regEnd);
146731         -  }
146732         -
146733         -  /* Initialize the accumulator register for each window function to NULL */
146734         -  regArg = windowInitAccum(pParse, pMWin);
146735         -
146736         -  sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone);
146737         -  VdbeCoverage(v);
146738         -  sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone);
146739         -  VdbeCoverageNeverTaken(v);
146740         -  sqlite3VdbeChangeP5(v, 1);
146741         -  sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone);
146742         -  VdbeCoverageNeverTaken(v);
146743         -  sqlite3VdbeChangeP5(v, 1);
146744         -
146745         -  /* Invoke AggStep function for each window function using the row that
146746         -  ** csrEnd currently points to. Or, if csrEnd is already at EOF,
146747         -  ** do nothing.  */
146748         -  addrTop = sqlite3VdbeCurrentAddr(v);
146749         -  if( pMWin->eEnd==TK_PRECEDING ){
146750         -    addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
146751         -    VdbeCoverage(v);
146752         -  }
146753         -  sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
146754         -  VdbeCoverage(v);
146755         -  addr = sqlite3VdbeAddOp0(v, OP_Goto);
146756         -  windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
146757         -  if( pMWin->eEnd==TK_UNBOUNDED ){
146758         -    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
146759         -    sqlite3VdbeJumpHere(v, addr);
146760         -    addrTop = sqlite3VdbeCurrentAddr(v);
146761         -  }else{
146762         -    sqlite3VdbeJumpHere(v, addr);
146763         -    if( pMWin->eEnd==TK_PRECEDING ){
146764         -      sqlite3VdbeJumpHere(v, addrIfPos1);
146765         -    }
146766         -  }
146767         -
146768         -  if( pMWin->eEnd==TK_FOLLOWING ){
146769         -    addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
146770         -    VdbeCoverage(v);
146771         -  }
146772         -  if( pMWin->eStart==TK_FOLLOWING ){
146773         -    addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
146774         -    VdbeCoverage(v);
146775         -  }
146776         -  windowAggFinal(pParse, pMWin, 0);
146777         -  windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
146778         -  sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2);
146779         -  VdbeCoverage(v);
146780         -  sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone);
146781         -  if( pMWin->eStart==TK_FOLLOWING ){
146782         -    sqlite3VdbeJumpHere(v, addrIfPos2);
146783         -  }
146784         -
146785         -  if( pMWin->eStart==TK_CURRENT 
146786         -   || pMWin->eStart==TK_PRECEDING 
146787         -   || pMWin->eStart==TK_FOLLOWING 
146788         -  ){
146789         -    int lblSkipInverse = sqlite3VdbeMakeLabel(pParse);;
146790         -    if( pMWin->eStart==TK_PRECEDING ){
146791         -      sqlite3VdbeAddOp3(v, OP_IfPos, regStart, lblSkipInverse, 1);
       147227  +  sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1);
       147228  +  sqlite3VdbeJumpHere(v, addrGe);
       147229  +  sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
       147230  +  sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
       147231  +  assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le );
       147232  +  testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
       147233  +  testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
       147234  +  testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
       147235  +  testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
       147236  +
       147237  +  sqlite3ReleaseTempReg(pParse, reg1);
       147238  +  sqlite3ReleaseTempReg(pParse, reg2);
       147239  +}
       147240  +
       147241  +/*
       147242  +** Helper function for sqlite3WindowCodeStep(). Each call to this function
       147243  +** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE 
       147244  +** operation. Refer to the header comment for sqlite3WindowCodeStep() for
       147245  +** details.
       147246  +*/
       147247  +static int windowCodeOp(
       147248  + WindowCodeArg *p,                /* Context object */
       147249  + int op,                          /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
       147250  + int regCountdown,                /* Register for OP_IfPos countdown */
       147251  + int jumpOnEof                    /* Jump here if stepped cursor reaches EOF */
       147252  +){
       147253  +  int csr, reg;
       147254  +  Parse *pParse = p->pParse;
       147255  +  Window *pMWin = p->pMWin;
       147256  +  int ret = 0;
       147257  +  Vdbe *v = p->pVdbe;
       147258  +  int addrIf = 0; 
       147259  +  int addrContinue = 0;
       147260  +  int addrGoto = 0;
       147261  +  int bPeer = (pMWin->eFrmType!=TK_ROWS);
       147262  +
       147263  +  int lblDone = sqlite3VdbeMakeLabel(pParse);
       147264  +  int addrNextRange = 0;
       147265  +
       147266  +  /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
       147267  +  ** starts with UNBOUNDED PRECEDING. */
       147268  +  if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
       147269  +    assert( regCountdown==0 && jumpOnEof==0 );
       147270  +    return 0;
       147271  +  }
       147272  +
       147273  +  if( regCountdown>0 ){
       147274  +    if( pMWin->eFrmType==TK_RANGE ){
       147275  +      addrNextRange = sqlite3VdbeCurrentAddr(v);
       147276  +      assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP );
       147277  +      if( op==WINDOW_AGGINVERSE ){
       147278  +        if( pMWin->eStart==TK_FOLLOWING ){
       147279  +          windowCodeRangeTest(
       147280  +              p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
       147281  +          );
       147282  +        }else{
       147283  +          windowCodeRangeTest(
       147284  +              p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
       147285  +          );
       147286  +        }
       147287  +      }else{
       147288  +        windowCodeRangeTest(
       147289  +            p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
       147290  +        );
       147291  +      }
       147292  +    }else{
       147293  +      addrIf = sqlite3VdbeAddOp3(v, OP_IfPos, regCountdown, 0, 1);
146792 147294         VdbeCoverage(v);
146793 147295       }
146794         -    if( pMWin->eStart==TK_FOLLOWING ){
146795         -      sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+2);
146796         -      VdbeCoverage(v);
146797         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, lblSkipInverse);
146798         -    }else{
146799         -      sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
146800         -      VdbeCoverageAlwaysTaken(v);
146801         -    }
146802         -    windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
146803         -    sqlite3VdbeResolveLabel(v, lblSkipInverse);
146804         -  }
146805         -  if( pMWin->eEnd==TK_FOLLOWING ){
146806         -    sqlite3VdbeJumpHere(v, addrIfPos1);
146807         -  }
146808         -  sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
146809         -
146810         -  /* flush_partition_done: */
146811         -  sqlite3VdbeResolveLabel(v, lblFlushDone);
146812         -  sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
146813         -  sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
146814         -  VdbeComment((v, "end flush_partition subroutine"));
146815         -
146816         -  /* Jump to here to skip over flush_partition */
146817         -  sqlite3VdbeJumpHere(v, addrGoto);
146818         -}
146819         -
146820         -/*
146821         -** This function does the work of sqlite3WindowCodeStep() for cases that
146822         -** would normally be handled by windowCodeDefaultStep() when there are
146823         -** one or more built-in window-functions that require the entire partition
146824         -** to be cached in a temp table before any rows can be returned. Additionally.
146825         -** "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is always handled by
146826         -** this function.
146827         -**
146828         -** Pseudo-code corresponding to the VM code generated by this function
146829         -** for each type of window follows.
146830         -**
146831         -** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146832         -**
146833         -**   flush_partition:
146834         -**     Once {
146835         -**       OpenDup (iEphCsr -> csrLead)
146836         -**     }
146837         -**     Integer ctr 0
146838         -**     foreach row (csrLead){
146839         -**       if( new peer ){
146840         -**         AggFinal (xValue)
146841         -**         for(i=0; i<ctr; i++){
146842         -**           Gosub addrGosub
146843         -**           Next iEphCsr
146844         -**         }
146845         -**         Integer ctr 0
146846         -**       }
146847         -**       AggStep (csrLead)
146848         -**       Incr ctr
146849         -**     }
146850         -**
146851         -**     AggFinal (xFinalize)
146852         -**     for(i=0; i<ctr; i++){
146853         -**       Gosub addrGosub
146854         -**       Next iEphCsr
146855         -**     }
146856         -**
146857         -**     ResetSorter (csr)
146858         -**     Return
146859         -**
146860         -** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146861         -**
146862         -**   As above, except that the "if( new peer )" branch is always taken.
146863         -**
146864         -** RANGE BETWEEN CURRENT ROW AND CURRENT ROW 
146865         -**
146866         -**   As above, except that each of the for() loops becomes:
146867         -**
146868         -**         for(i=0; i<ctr; i++){
146869         -**           Gosub addrGosub
146870         -**           AggInverse (iEphCsr)
146871         -**           Next iEphCsr
146872         -**         }
146873         -**
146874         -** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
146875         -**
146876         -**   flush_partition:
146877         -**     Once {
146878         -**       OpenDup (iEphCsr -> csrLead)
146879         -**     }
146880         -**     foreach row (csrLead) {
146881         -**       AggStep (csrLead)
146882         -**     }
146883         -**     foreach row (iEphCsr) {
146884         -**       Gosub addrGosub
146885         -**     }
146886         -** 
146887         -** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146888         -**
146889         -**   flush_partition:
146890         -**     Once {
146891         -**       OpenDup (iEphCsr -> csrLead)
146892         -**     }
146893         -**     foreach row (csrLead){
146894         -**       AggStep (csrLead)
146895         -**     }
146896         -**     Rewind (csrLead)
146897         -**     Integer ctr 0
146898         -**     foreach row (csrLead){
146899         -**       if( new peer ){
146900         -**         AggFinal (xValue)
146901         -**         for(i=0; i<ctr; i++){
146902         -**           Gosub addrGosub
146903         -**           AggInverse (iEphCsr)
146904         -**           Next iEphCsr
146905         -**         }
146906         -**         Integer ctr 0
146907         -**       }
146908         -**       Incr ctr
146909         -**     }
146910         -**
146911         -**     AggFinal (xFinalize)
146912         -**     for(i=0; i<ctr; i++){
146913         -**       Gosub addrGosub
146914         -**       Next iEphCsr
146915         -**     }
146916         -**
146917         -**     ResetSorter (csr)
146918         -**     Return
146919         -*/
146920         -static void windowCodeCacheStep(
146921         -  Parse *pParse, 
146922         -  Select *p,
146923         -  WhereInfo *pWInfo,
146924         -  int regGosub, 
146925         -  int addrGosub
146926         -){
146927         -  Window *pMWin = p->pWin;
146928         -  Vdbe *v = sqlite3GetVdbe(pParse);
146929         -  int k;
146930         -  int addr;
146931         -  ExprList *pPart = pMWin->pPartition;
146932         -  ExprList *pOrderBy = pMWin->pOrderBy;
146933         -  int nPeer = pOrderBy ? pOrderBy->nExpr : 0;
146934         -  int regNewPeer;
146935         -
146936         -  int addrGoto;                   /* Address of Goto used to jump flush_par.. */
146937         -  int addrNext;                   /* Jump here for next iteration of loop */
146938         -  int regFlushPart;
146939         -  int lblFlushPart;
146940         -  int csrLead;
146941         -  int regCtr;
146942         -  int regArg;                     /* Register array to martial function args */
146943         -  int regSize;
146944         -  int lblEmpty;
146945         -  int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT 
146946         -          && pMWin->eEnd==TK_UNBOUNDED;
146947         -
146948         -  assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT) 
146949         -       || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED) 
146950         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT) 
146951         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED) 
146952         -  );
146953         -
146954         -  lblEmpty = sqlite3VdbeMakeLabel(pParse);
146955         -  regNewPeer = pParse->nMem+1;
146956         -  pParse->nMem += nPeer;
146957         -
146958         -  /* Allocate register and label for the "flush_partition" sub-routine. */
146959         -  regFlushPart = ++pParse->nMem;
146960         -  lblFlushPart = sqlite3VdbeMakeLabel(pParse);
146961         -
146962         -  csrLead = pParse->nTab++;
146963         -  regCtr = ++pParse->nMem;
146964         -
146965         -  windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
146966         -  addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146967         -
146968         -  /* Start of "flush_partition" */
146969         -  sqlite3VdbeResolveLabel(v, lblFlushPart);
146970         -  sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2);
146971         -  VdbeCoverage(v);
146972         -  sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr);
146973         -
146974         -  /* Initialize the accumulator register for each window function to NULL */
146975         -  regArg = windowInitAccum(pParse, pMWin);
146976         -
146977         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr);
146978         -  sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
146979         -  VdbeCoverage(v);
146980         -  sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty);
146981         -  VdbeCoverageNeverTaken(v);
146982         -
146983         -  if( bReverse ){
146984         -    int addr2 = sqlite3VdbeCurrentAddr(v);
146985         -    windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
146986         -    sqlite3VdbeAddOp2(v, OP_Next, csrLead, addr2);
       147296  +  }
       147297  +
       147298  +  if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
       147299  +    windowAggFinal(p, 0);
       147300  +  }
       147301  +  addrContinue = sqlite3VdbeCurrentAddr(v);
       147302  +  switch( op ){
       147303  +    case WINDOW_RETURN_ROW:
       147304  +      csr = p->current.csr;
       147305  +      reg = p->current.reg;
       147306  +      windowReturnOneRow(p);
       147307  +      break;
       147308  +
       147309  +    case WINDOW_AGGINVERSE:
       147310  +      csr = p->start.csr;
       147311  +      reg = p->start.reg;
       147312  +      if( pMWin->regStartRowid ){
       147313  +        assert( pMWin->regEndRowid );
       147314  +        sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
       147315  +      }else{
       147316  +        windowAggStep(pParse, pMWin, csr, 1, p->regArg);
       147317  +      }
       147318  +      break;
       147319  +
       147320  +    default:
       147321  +      assert( op==WINDOW_AGGSTEP );
       147322  +      csr = p->end.csr;
       147323  +      reg = p->end.reg;
       147324  +      if( pMWin->regStartRowid ){
       147325  +        assert( pMWin->regEndRowid );
       147326  +        sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
       147327  +      }else{
       147328  +        windowAggStep(pParse, pMWin, csr, 0, p->regArg);
       147329  +      }
       147330  +      break;
       147331  +  }
       147332  +
       147333  +  if( op==p->eDelete ){
       147334  +    sqlite3VdbeAddOp1(v, OP_Delete, csr);
       147335  +    sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
       147336  +  }
       147337  +
       147338  +  if( jumpOnEof ){
       147339  +    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+2);
       147340  +    VdbeCoverage(v);
       147341  +    ret = sqlite3VdbeAddOp0(v, OP_Goto);
       147342  +  }else{
       147343  +    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer);
146987 147344       VdbeCoverage(v);
146988         -    sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
146989         -    VdbeCoverageNeverTaken(v);
146990         -  }
146991         -  addrNext = sqlite3VdbeCurrentAddr(v);
146992         -
146993         -  if( pOrderBy && (pMWin->eEnd==TK_CURRENT || pMWin->eStart==TK_CURRENT) ){
146994         -    int bCurrent = (pMWin->eStart==TK_CURRENT);
146995         -    int addrJump = 0;             /* Address of OP_Jump below */
146996         -    if( pMWin->eType==TK_RANGE ){
146997         -      int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
146998         -      int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0);
146999         -      KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
147000         -      for(k=0; k<nPeer; k++){
147001         -        sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k);
147002         -      }
147003         -      addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
147004         -      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
147005         -      addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
147006         -      VdbeCoverage(v);
147007         -      sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1);
147008         -    }
147009         -
147010         -    windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 
147011         -        (bCurrent ? regArg : 0), (bCurrent ? regSize : 0)
147012         -    );
147013         -    if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147014         -  }
147015         -
147016         -  if( bReverse==0 ){
147017         -    windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
147018         -  }
147019         -  sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
147020         -  sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext);
147021         -  VdbeCoverage(v);
147022         -
147023         -  windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0);
147024         -
147025         -  sqlite3VdbeResolveLabel(v, lblEmpty);
147026         -  sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
147027         -  sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
147028         -
147029         -  /* Jump to here to skip over flush_partition */
147030         -  sqlite3VdbeJumpHere(v, addrGoto);
147031         -}
147032         -
147033         -
147034         -/*
147035         -** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147036         -**
147037         -**   ...
147038         -**     if( new partition ){
147039         -**       AggFinal (xFinalize)
147040         -**       Gosub addrGosub
147041         -**       ResetSorter eph-table
147042         -**     }
147043         -**     else if( new peer ){
147044         -**       AggFinal (xValue)
147045         -**       Gosub addrGosub
147046         -**       ResetSorter eph-table
147047         -**     }
147048         -**     AggStep
147049         -**     Insert (record into eph-table)
147050         -**   sqlite3WhereEnd()
147051         -**   AggFinal (xFinalize)
147052         -**   Gosub addrGosub
147053         -**
147054         -** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
147055         -**
147056         -**   As above, except take no action for a "new peer". Invoke
147057         -**   the sub-routine once only for each partition.
147058         -**
147059         -** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
147060         -**
147061         -**   As above, except that the "new peer" condition is handled in the
147062         -**   same way as "new partition" (so there is no "else if" block).
147063         -**
147064         -** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147065         -** 
147066         -**   As above, except assume every row is a "new peer".
147067         -*/
147068         -static void windowCodeDefaultStep(
147069         -  Parse *pParse, 
147070         -  Select *p,
147071         -  WhereInfo *pWInfo,
147072         -  int regGosub, 
147073         -  int addrGosub
147074         -){
147075         -  Window *pMWin = p->pWin;
147076         -  Vdbe *v = sqlite3GetVdbe(pParse);
147077         -  int k;
147078         -  int iSubCsr = p->pSrc->a[0].iCursor;
147079         -  int nSub = p->pSrc->a[0].pTab->nCol;
147080         -  int reg = pParse->nMem+1;
147081         -  int regRecord = reg+nSub;
147082         -  int regRowid = regRecord+1;
147083         -  int addr;
147084         -  ExprList *pPart = pMWin->pPartition;
147085         -  ExprList *pOrderBy = pMWin->pOrderBy;
147086         -
147087         -  assert( pMWin->eType==TK_RANGE 
147088         -      || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
147089         -  );
147090         -
147091         -  assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
147092         -       || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
147093         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
147094         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy)
147095         -  );
147096         -
147097         -  if( pMWin->eEnd==TK_UNBOUNDED ){
147098         -    pOrderBy = 0;
147099         -  }
147100         -
147101         -  pParse->nMem += nSub + 2;
147102         -
147103         -  /* Load the individual column values of the row returned by
147104         -  ** the sub-select into an array of registers. */
147105         -  for(k=0; k<nSub; k++){
147106         -    sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
147107         -  }
147108         -
147109         -  /* Check if this is the start of a new partition or peer group. */
147110         -  if( pPart || pOrderBy ){
147111         -    int nPart = (pPart ? pPart->nExpr : 0);
147112         -    int addrGoto = 0;
147113         -    int addrJump = 0;
147114         -    int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
147115         -
147116         -    if( pPart ){
147117         -      int regNewPart = reg + pMWin->nBufferCol;
147118         -      KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
147119         -      addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
147120         -      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
147121         -      addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
147122         -      VdbeCoverageEqNe(v);
147123         -      windowAggFinal(pParse, pMWin, 1);
147124         -      if( pOrderBy ){
147125         -        addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
147126         -      }
147127         -    }
147128         -
147129         -    if( pOrderBy ){
147130         -      int regNewPeer = reg + pMWin->nBufferCol + nPart;
147131         -      int regPeer = pMWin->regPart + nPart;
147132         -
147133         -      if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147134         -      if( pMWin->eType==TK_RANGE ){
147135         -        KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
147136         -        addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
147137         -        sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
147138         -        addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
147139         -        VdbeCoverage(v);
147140         -      }else{
147141         -        addrJump = 0;
147142         -      }
147143         -      windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT);
147144         -      if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
147145         -    }
147146         -
147147         -    sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
147148         -    VdbeCoverage(v);
147149         -    sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
147150         -    sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
147151         -    VdbeCoverage(v);
147152         -
147153         -    sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
147154         -    sqlite3VdbeAddOp3(
147155         -        v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1
147156         -    );
147157         -
147158         -    if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147159         -  }
147160         -
147161         -  /* Invoke step function for window functions */
147162         -  windowAggStep(pParse, pMWin, -1, 0, reg, 0);
147163         -
147164         -  /* Buffer the current row in the ephemeral table. */
147165         -  if( pMWin->nBufferCol>0 ){
147166         -    sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord);
147167         -  }else{
147168         -    sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord);
147169         -    sqlite3VdbeAppendP4(v, (void*)"", 0);
147170         -  }
147171         -  sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
147172         -  sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
147173         -
147174         -  /* End the database scan loop. */
147175         -  sqlite3WhereEnd(pWInfo);
147176         -
147177         -  windowAggFinal(pParse, pMWin, 1);
147178         -  sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
147179         -  VdbeCoverage(v);
147180         -  sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
147181         -  sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
147182         -  VdbeCoverage(v);
147183         -}
       147345  +    if( bPeer ){
       147346  +      addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
       147347  +    }
       147348  +  }
       147349  +
       147350  +  if( bPeer ){
       147351  +    int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
       147352  +    int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
       147353  +    windowReadPeerValues(p, csr, regTmp);
       147354  +    windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
       147355  +    sqlite3ReleaseTempRange(pParse, regTmp, nReg);
       147356  +  }
       147357  +
       147358  +  if( addrNextRange ){
       147359  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNextRange);
       147360  +  }
       147361  +  sqlite3VdbeResolveLabel(v, lblDone);
       147362  +  if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
       147363  +  if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
       147364  +  return ret;
       147365  +}
       147366  +
147184 147367   
147185 147368   /*
147186 147369   ** Allocate and return a duplicate of the Window object indicated by the
147187 147370   ** third argument. Set the Window.pOwner field of the new object to
147188 147371   ** pOwner.
147189 147372   */
147190 147373   SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
................................................................................
147193 147376       pNew = sqlite3DbMallocZero(db, sizeof(Window));
147194 147377       if( pNew ){
147195 147378         pNew->zName = sqlite3DbStrDup(db, p->zName);
147196 147379         pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
147197 147380         pNew->pFunc = p->pFunc;
147198 147381         pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
147199 147382         pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
147200         -      pNew->eType = p->eType;
       147383  +      pNew->eFrmType = p->eFrmType;
147201 147384         pNew->eEnd = p->eEnd;
147202 147385         pNew->eStart = p->eStart;
       147386  +      pNew->eExclude = p->eExclude;
147203 147387         pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
147204 147388         pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
147205 147389         pNew->pOwner = pOwner;
147206 147390       }
147207 147391     }
147208 147392     return pNew;
147209 147393   }
................................................................................
147221 147405       *pp = sqlite3WindowDup(db, 0, pWin);
147222 147406       if( *pp==0 ) break;
147223 147407       pp = &((*pp)->pNextWin);
147224 147408     }
147225 147409   
147226 147410     return pRet;
147227 147411   }
       147412  +
       147413  +/*
       147414  +** Return true if it can be determined at compile time that expression 
       147415  +** pExpr evaluates to a value that, when cast to an integer, is greater 
       147416  +** than zero. False otherwise.
       147417  +**
       147418  +** If an OOM error occurs, this function sets the Parse.db.mallocFailed 
       147419  +** flag and returns zero.
       147420  +*/
       147421  +static int windowExprGtZero(Parse *pParse, Expr *pExpr){
       147422  +  int ret = 0;
       147423  +  sqlite3 *db = pParse->db;
       147424  +  sqlite3_value *pVal = 0;
       147425  +  sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
       147426  +  if( pVal && sqlite3_value_int(pVal)>0 ){
       147427  +    ret = 1;
       147428  +  }
       147429  +  sqlite3ValueFree(pVal);
       147430  +  return ret;
       147431  +}
147228 147432   
147229 147433   /*
147230 147434   ** sqlite3WhereBegin() has already been called for the SELECT statement 
147231 147435   ** passed as the second argument when this function is invoked. It generates
147232         -** code to populate the Window.regResult register for each window function and
147233         -** invoke the sub-routine at instruction addrGosub once for each row.
147234         -** This function calls sqlite3WhereEnd() before returning. 
       147436  +** code to populate the Window.regResult register for each window function 
       147437  +** and invoke the sub-routine at instruction addrGosub once for each row.
       147438  +** sqlite3WhereEnd() is always called before returning. 
       147439  +**
       147440  +** This function handles several different types of window frames, which
       147441  +** require slightly different processing. The following pseudo code is
       147442  +** used to implement window frames of the form:
       147443  +**
       147444  +**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
       147445  +**
       147446  +** Other window frame types use variants of the following:
       147447  +**
       147448  +**     ... loop started by sqlite3WhereBegin() ...
       147449  +**       if( new partition ){
       147450  +**         Gosub flush
       147451  +**       }
       147452  +**       Insert new row into eph table.
       147453  +**       
       147454  +**       if( first row of partition ){
       147455  +**         // Rewind three cursors, all open on the eph table.
       147456  +**         Rewind(csrEnd);
       147457  +**         Rewind(csrStart);
       147458  +**         Rewind(csrCurrent);
       147459  +**       
       147460  +**         regEnd = <expr2>          // FOLLOWING expression
       147461  +**         regStart = <expr1>        // PRECEDING expression
       147462  +**       }else{
       147463  +**         // First time this branch is taken, the eph table contains two 
       147464  +**         // rows. The first row in the partition, which all three cursors
       147465  +**         // currently point to, and the following row.
       147466  +**         AGGSTEP
       147467  +**         if( (regEnd--)<=0 ){
       147468  +**           RETURN_ROW
       147469  +**           if( (regStart--)<=0 ){
       147470  +**             AGGINVERSE
       147471  +**           }
       147472  +**         }
       147473  +**       }
       147474  +**     }
       147475  +**     flush:
       147476  +**       AGGSTEP
       147477  +**       while( 1 ){
       147478  +**         RETURN ROW
       147479  +**         if( csrCurrent is EOF ) break;
       147480  +**         if( (regStart--)<=0 ){
       147481  +**           AggInverse(csrStart)
       147482  +**           Next(csrStart)
       147483  +**         }
       147484  +**       }
       147485  +**
       147486  +** The pseudo-code above uses the following shorthand:
       147487  +**
       147488  +**   AGGSTEP:    invoke the aggregate xStep() function for each window function
       147489  +**               with arguments read from the current row of cursor csrEnd, then
       147490  +**               step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
       147491  +**
       147492  +**   RETURN_ROW: return a row to the caller based on the contents of the 
       147493  +**               current row of csrCurrent and the current state of all 
       147494  +**               aggregates. Then step cursor csrCurrent forward one row.
       147495  +**
       147496  +**   AGGINVERSE: invoke the aggregate xInverse() function for each window 
       147497  +**               functions with arguments read from the current row of cursor
       147498  +**               csrStart. Then step csrStart forward one row.
       147499  +**
       147500  +** There are two other ROWS window frames that are handled significantly
       147501  +** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
       147502  +** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special 
       147503  +** cases because they change the order in which the three cursors (csrStart,
       147504  +** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
       147505  +** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
       147506  +** three.
       147507  +**
       147508  +**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
       147509  +**
       147510  +**     ... loop started by sqlite3WhereBegin() ...
       147511  +**       if( new partition ){
       147512  +**         Gosub flush
       147513  +**       }
       147514  +**       Insert new row into eph table.
       147515  +**       if( first row of partition ){
       147516  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147517  +**         regEnd = <expr2>
       147518  +**         regStart = <expr1>
       147519  +**       }else{
       147520  +**         if( (regEnd--)<=0 ){
       147521  +**           AGGSTEP
       147522  +**         }
       147523  +**         RETURN_ROW
       147524  +**         if( (regStart--)<=0 ){
       147525  +**           AGGINVERSE
       147526  +**         }
       147527  +**       }
       147528  +**     }
       147529  +**     flush:
       147530  +**       if( (regEnd--)<=0 ){
       147531  +**         AGGSTEP
       147532  +**       }
       147533  +**       RETURN_ROW
       147534  +**
       147535  +**
       147536  +**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
       147537  +**
       147538  +**     ... loop started by sqlite3WhereBegin() ...
       147539  +**     if( new partition ){
       147540  +**       Gosub flush
       147541  +**     }
       147542  +**     Insert new row into eph table.
       147543  +**     if( first row of partition ){
       147544  +**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147545  +**       regEnd = <expr2>
       147546  +**       regStart = regEnd - <expr1>
       147547  +**     }else{
       147548  +**       AGGSTEP
       147549  +**       if( (regEnd--)<=0 ){
       147550  +**         RETURN_ROW
       147551  +**       }
       147552  +**       if( (regStart--)<=0 ){
       147553  +**         AGGINVERSE
       147554  +**       }
       147555  +**     }
       147556  +**   }
       147557  +**   flush:
       147558  +**     AGGSTEP
       147559  +**     while( 1 ){
       147560  +**       if( (regEnd--)<=0 ){
       147561  +**         RETURN_ROW
       147562  +**         if( eof ) break;
       147563  +**       }
       147564  +**       if( (regStart--)<=0 ){
       147565  +**         AGGINVERSE
       147566  +**         if( eof ) break
       147567  +**       }
       147568  +**     }
       147569  +**     while( !eof csrCurrent ){
       147570  +**       RETURN_ROW
       147571  +**     }
       147572  +**
       147573  +** For the most part, the patterns above are adapted to support UNBOUNDED by
       147574  +** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
       147575  +** CURRENT ROW by assuming that it is equivilent to "0 PRECEDING/FOLLOWING".
       147576  +** This is optimized of course - branches that will never be taken and
       147577  +** conditions that are always true are omitted from the VM code. The only
       147578  +** exceptional case is:
       147579  +**
       147580  +**   ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
       147581  +**
       147582  +**     ... loop started by sqlite3WhereBegin() ...
       147583  +**     if( new partition ){
       147584  +**       Gosub flush
       147585  +**     }
       147586  +**     Insert new row into eph table.
       147587  +**     if( first row of partition ){
       147588  +**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147589  +**       regStart = <expr1>
       147590  +**     }else{
       147591  +**       AGGSTEP
       147592  +**     }
       147593  +**   }
       147594  +**   flush:
       147595  +**     AGGSTEP
       147596  +**     while( 1 ){
       147597  +**       if( (regStart--)<=0 ){
       147598  +**         AGGINVERSE
       147599  +**         if( eof ) break
       147600  +**       }
       147601  +**       RETURN_ROW
       147602  +**     }
       147603  +**     while( !eof csrCurrent ){
       147604  +**       RETURN_ROW
       147605  +**     }
       147606  +**
       147607  +** Also requiring special handling are the cases:
       147608  +**
       147609  +**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
       147610  +**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
       147611  +**
       147612  +** when (expr1 < expr2). This is detected at runtime, not by this function.
       147613  +** To handle this case, the pseudo-code programs depicted above are modified
       147614  +** slightly to be:
       147615  +**
       147616  +**     ... loop started by sqlite3WhereBegin() ...
       147617  +**     if( new partition ){
       147618  +**       Gosub flush
       147619  +**     }
       147620  +**     Insert new row into eph table.
       147621  +**     if( first row of partition ){
       147622  +**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147623  +**       regEnd = <expr2>
       147624  +**       regStart = <expr1>
       147625  +**       if( regEnd < regStart ){
       147626  +**         RETURN_ROW
       147627  +**         delete eph table contents
       147628  +**         continue
       147629  +**       }
       147630  +**     ...
       147631  +**
       147632  +** The new "continue" statement in the above jumps to the next iteration
       147633  +** of the outer loop - the one started by sqlite3WhereBegin().
       147634  +**
       147635  +** The various GROUPS cases are implemented using the same patterns as
       147636  +** ROWS. The VM code is modified slightly so that:
       147637  +**
       147638  +**   1. The else branch in the main loop is only taken if the row just
       147639  +**      added to the ephemeral table is the start of a new group. In
       147640  +**      other words, it becomes:
       147641  +**
       147642  +**         ... loop started by sqlite3WhereBegin() ...
       147643  +**         if( new partition ){
       147644  +**           Gosub flush
       147645  +**         }
       147646  +**         Insert new row into eph table.
       147647  +**         if( first row of partition ){
       147648  +**           Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147649  +**           regEnd = <expr2>
       147650  +**           regStart = <expr1>
       147651  +**         }else if( new group ){
       147652  +**           ... 
       147653  +**         }
       147654  +**       }
       147655  +**
       147656  +**   2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or 
       147657  +**      AGGINVERSE step processes the current row of the relevant cursor and
       147658  +**      all subsequent rows belonging to the same group.
       147659  +**
       147660  +** RANGE window frames are a little different again. As for GROUPS, the 
       147661  +** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
       147662  +** deal in groups instead of rows. As for ROWS and GROUPS, there are three
       147663  +** basic cases:
       147664  +**
       147665  +**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
       147666  +**
       147667  +**     ... loop started by sqlite3WhereBegin() ...
       147668  +**       if( new partition ){
       147669  +**         Gosub flush
       147670  +**       }
       147671  +**       Insert new row into eph table.
       147672  +**       if( first row of partition ){
       147673  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147674  +**         regEnd = <expr2>
       147675  +**         regStart = <expr1>
       147676  +**       }else{
       147677  +**         AGGSTEP
       147678  +**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
       147679  +**           RETURN_ROW
       147680  +**           while( csrStart.key + regStart) < csrCurrent.key ){
       147681  +**             AGGINVERSE
       147682  +**           }
       147683  +**         }
       147684  +**       }
       147685  +**     }
       147686  +**     flush:
       147687  +**       AGGSTEP
       147688  +**       while( 1 ){
       147689  +**         RETURN ROW
       147690  +**         if( csrCurrent is EOF ) break;
       147691  +**           while( csrStart.key + regStart) < csrCurrent.key ){
       147692  +**             AGGINVERSE
       147693  +**           }
       147694  +**         }
       147695  +**       }
       147696  +**
       147697  +** In the above notation, "csr.key" means the current value of the ORDER BY 
       147698  +** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
       147699  +** or <expr PRECEDING) read from cursor csr.
       147700  +**
       147701  +**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
       147702  +**
       147703  +**     ... loop started by sqlite3WhereBegin() ...
       147704  +**       if( new partition ){
       147705  +**         Gosub flush
       147706  +**       }
       147707  +**       Insert new row into eph table.
       147708  +**       if( first row of partition ){
       147709  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147710  +**         regEnd = <expr2>
       147711  +**         regStart = <expr1>
       147712  +**       }else{
       147713  +**         if( (csrEnd.key + regEnd) <= csrCurrent.key ){
       147714  +**           AGGSTEP
       147715  +**         }
       147716  +**         while( (csrStart.key + regStart) < csrCurrent.key ){
       147717  +**           AGGINVERSE
       147718  +**         }
       147719  +**         RETURN_ROW
       147720  +**       }
       147721  +**     }
       147722  +**     flush:
       147723  +**       while( (csrEnd.key + regEnd) <= csrCurrent.key ){
       147724  +**         AGGSTEP
       147725  +**       }
       147726  +**       while( (csrStart.key + regStart) < csrCurrent.key ){
       147727  +**         AGGINVERSE
       147728  +**       }
       147729  +**       RETURN_ROW
       147730  +**
       147731  +**   RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
       147732  +**
       147733  +**     ... loop started by sqlite3WhereBegin() ...
       147734  +**       if( new partition ){
       147735  +**         Gosub flush
       147736  +**       }
       147737  +**       Insert new row into eph table.
       147738  +**       if( first row of partition ){
       147739  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147740  +**         regEnd = <expr2>
       147741  +**         regStart = <expr1>
       147742  +**       }else{
       147743  +**         AGGSTEP
       147744  +**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
       147745  +**           while( (csrCurrent.key + regStart) > csrStart.key ){
       147746  +**             AGGINVERSE
       147747  +**           }
       147748  +**           RETURN_ROW
       147749  +**         }
       147750  +**       }
       147751  +**     }
       147752  +**     flush:
       147753  +**       AGGSTEP
       147754  +**       while( 1 ){
       147755  +**         while( (csrCurrent.key + regStart) > csrStart.key ){
       147756  +**           AGGINVERSE
       147757  +**           if( eof ) break "while( 1 )" loop.
       147758  +**         }
       147759  +**         RETURN_ROW
       147760  +**       }
       147761  +**       while( !eof csrCurrent ){
       147762  +**         RETURN_ROW
       147763  +**       }
       147764  +**
       147765  +** The text above leaves out many details. Refer to the code and comments
       147766  +** below for a more complete picture.
147235 147767   */
147236 147768   SQLITE_PRIVATE void sqlite3WindowCodeStep(
147237 147769     Parse *pParse,                  /* Parse context */
147238 147770     Select *p,                      /* Rewritten SELECT statement */
147239 147771     WhereInfo *pWInfo,              /* Context returned by sqlite3WhereBegin() */
147240 147772     int regGosub,                   /* Register for OP_Gosub */
147241 147773     int addrGosub                   /* OP_Gosub here to return each row */
147242 147774   ){
147243 147775     Window *pMWin = p->pWin;
147244         -
147245         -  /* There are three different functions that may be used to do the work
147246         -  ** of this one, depending on the window frame and the specific built-in
147247         -  ** window functions used (if any).
147248         -  **
147249         -  ** windowCodeRowExprStep() handles all "ROWS" window frames, except for:
147250         -  **
147251         -  **   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147252         -  **
147253         -  ** The exception is because windowCodeRowExprStep() implements all window
147254         -  ** frame types by caching the entire partition in a temp table, and
147255         -  ** "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" is easy enough to
147256         -  ** implement without such a cache.
147257         -  **
147258         -  ** windowCodeCacheStep() is used for:
147259         -  **
147260         -  **   RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
147261         -  **
147262         -  ** It is also used for anything not handled by windowCodeRowExprStep() 
147263         -  ** that invokes a built-in window function that requires the entire 
147264         -  ** partition to be cached in a temp table before any rows are returned
147265         -  ** (e.g. nth_value() or percent_rank()).
147266         -  **
147267         -  ** Finally, assuming there is no built-in window function that requires
147268         -  ** the partition to be cached, windowCodeDefaultStep() is used for:
147269         -  **
147270         -  **   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
147271         -  **   RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
147272         -  **   RANGE BETWEEN CURRENT ROW AND CURRENT ROW 
147273         -  **   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147274         -  **
147275         -  ** windowCodeDefaultStep() is the only one of the three functions that
147276         -  ** does not cache each partition in a temp table before beginning to
147277         -  ** return rows.
147278         -  */
147279         -  if( pMWin->eType==TK_ROWS 
147280         -   && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy)
147281         -  ){
147282         -    VdbeModuleComment((pParse->pVdbe, "Begin RowExprStep()"));
147283         -    windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub);
147284         -  }else{
147285         -    Window *pWin;
147286         -    int bCache = 0;               /* True to use CacheStep() */
147287         -
147288         -    if( pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED ){
147289         -      bCache = 1;
147290         -    }else{
147291         -      for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147292         -        FuncDef *pFunc = pWin->pFunc;
147293         -        if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
147294         -         || (pFunc->zName==nth_valueName)
147295         -         || (pFunc->zName==first_valueName)
147296         -         || (pFunc->zName==leadName)
147297         -         || (pFunc->zName==lagName)
147298         -        ){
147299         -          bCache = 1;
147300         -          break;
147301         -        }
147302         -      }
147303         -    }
147304         -
147305         -    /* Otherwise, call windowCodeDefaultStep().  */
147306         -    if( bCache ){
147307         -      VdbeModuleComment((pParse->pVdbe, "Begin CacheStep()"));
147308         -      windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
147309         -    }else{
147310         -      VdbeModuleComment((pParse->pVdbe, "Begin DefaultStep()"));
147311         -      windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub);
147312         -    }
       147776  +  ExprList *pOrderBy = pMWin->pOrderBy;
       147777  +  Vdbe *v = sqlite3GetVdbe(pParse);
       147778  +  int csrWrite;                   /* Cursor used to write to eph. table */
       147779  +  int csrInput = p->pSrc->a[0].iCursor;     /* Cursor of sub-select */
       147780  +  int nInput = p->pSrc->a[0].pTab->nCol;    /* Number of cols returned by sub */
       147781  +  int iInput;                               /* To iterate through sub cols */
       147782  +  int addrNe;                     /* Address of OP_Ne */
       147783  +  int addrGosubFlush = 0;         /* Address of OP_Gosub to flush: */
       147784  +  int addrInteger = 0;            /* Address of OP_Integer */
       147785  +  int addrEmpty;                  /* Address of OP_Rewind in flush: */
       147786  +  int regStart = 0;               /* Value of <expr> PRECEDING */
       147787  +  int regEnd = 0;                 /* Value of <expr> FOLLOWING */
       147788  +  int regNew;                     /* Array of registers holding new input row */
       147789  +  int regRecord;                  /* regNew array in record form */
       147790  +  int regRowid;                   /* Rowid for regRecord in eph table */
       147791  +  int regNewPeer = 0;             /* Peer values for new row (part of regNew) */
       147792  +  int regPeer = 0;                /* Peer values for current row */
       147793  +  int regFlushPart = 0;           /* Register for "Gosub flush_partition" */
       147794  +  WindowCodeArg s;                /* Context object for sub-routines */
       147795  +  int lblWhereEnd;                /* Label just before sqlite3WhereEnd() code */
       147796  +
       147797  +  assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT 
       147798  +       || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED 
       147799  +  );
       147800  +  assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT 
       147801  +       || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING 
       147802  +  );
       147803  +  assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
       147804  +       || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
       147805  +       || pMWin->eExclude==TK_NO
       147806  +  );
       147807  +
       147808  +  lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
       147809  +
       147810  +  /* Fill in the context object */
       147811  +  memset(&s, 0, sizeof(WindowCodeArg));
       147812  +  s.pParse = pParse;
       147813  +  s.pMWin = pMWin;
       147814  +  s.pVdbe = v;
       147815  +  s.regGosub = regGosub;
       147816  +  s.addrGosub = addrGosub;
       147817  +  s.current.csr = pMWin->iEphCsr;
       147818  +  csrWrite = s.current.csr+1;
       147819  +  s.start.csr = s.current.csr+2;
       147820  +  s.end.csr = s.current.csr+3;
       147821  +
       147822  +  /* Figure out when rows may be deleted from the ephemeral table. There
       147823  +  ** are four options - they may never be deleted (eDelete==0), they may 
       147824  +  ** be deleted as soon as they are no longer part of the window frame
       147825  +  ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row 
       147826  +  ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
       147827  +  ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
       147828  +  switch( pMWin->eStart ){
       147829  +    case TK_FOLLOWING:
       147830  +      if( pMWin->eFrmType!=TK_RANGE
       147831  +       && windowExprGtZero(pParse, pMWin->pStart)
       147832  +      ){
       147833  +        s.eDelete = WINDOW_RETURN_ROW;
       147834  +      }
       147835  +      break;
       147836  +    case TK_UNBOUNDED:
       147837  +      if( windowCacheFrame(pMWin)==0 ){
       147838  +        if( pMWin->eEnd==TK_PRECEDING ){
       147839  +          if( pMWin->eFrmType!=TK_RANGE
       147840  +           && windowExprGtZero(pParse, pMWin->pEnd)
       147841  +          ){
       147842  +            s.eDelete = WINDOW_AGGSTEP;
       147843  +          }
       147844  +        }else{
       147845  +          s.eDelete = WINDOW_RETURN_ROW;
       147846  +        }
       147847  +      }
       147848  +      break;
       147849  +    default:
       147850  +      s.eDelete = WINDOW_AGGINVERSE;
       147851  +      break;
       147852  +  }
       147853  +
       147854  +  /* Allocate registers for the array of values from the sub-query, the
       147855  +  ** samve values in record form, and the rowid used to insert said record
       147856  +  ** into the ephemeral table.  */
       147857  +  regNew = pParse->nMem+1;
       147858  +  pParse->nMem += nInput;
       147859  +  regRecord = ++pParse->nMem;
       147860  +  regRowid = ++pParse->nMem;
       147861  +
       147862  +  /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
       147863  +  ** clause, allocate registers to store the results of evaluating each
       147864  +  ** <expr>.  */
       147865  +  if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
       147866  +    regStart = ++pParse->nMem;
       147867  +  }
       147868  +  if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
       147869  +    regEnd = ++pParse->nMem;
       147870  +  }
       147871  +
       147872  +  /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
       147873  +  ** registers to store copies of the ORDER BY expressions (peer values) 
       147874  +  ** for the main loop, and for each cursor (start, current and end). */
       147875  +  if( pMWin->eFrmType!=TK_ROWS ){
       147876  +    int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
       147877  +    regNewPeer = regNew + pMWin->nBufferCol;
       147878  +    if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
       147879  +    regPeer = pParse->nMem+1;       pParse->nMem += nPeer;
       147880  +    s.start.reg = pParse->nMem+1;   pParse->nMem += nPeer;
       147881  +    s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
       147882  +    s.end.reg = pParse->nMem+1;     pParse->nMem += nPeer;
       147883  +  }
       147884  +
       147885  +  /* Load the column values for the row returned by the sub-select
       147886  +  ** into an array of registers starting at regNew. Assemble them into
       147887  +  ** a record in register regRecord. */
       147888  +  for(iInput=0; iInput<nInput; iInput++){
       147889  +    sqlite3VdbeAddOp3(v, OP_Column, csrInput, iInput, regNew+iInput);
       147890  +  }
       147891  +  sqlite3VdbeAddOp3(v, OP_MakeRecord, regNew, nInput, regRecord);
       147892  +
       147893  +  /* An input row has just been read into an array of registers starting
       147894  +  ** at regNew. If the window has a PARTITION clause, this block generates 
       147895  +  ** VM code to check if the input row is the start of a new partition.
       147896  +  ** If so, it does an OP_Gosub to an address to be filled in later. The
       147897  +  ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
       147898  +  if( pMWin->pPartition ){
       147899  +    int addr;
       147900  +    ExprList *pPart = pMWin->pPartition;
       147901  +    int nPart = pPart->nExpr;
       147902  +    int regNewPart = regNew + pMWin->nBufferCol;
       147903  +    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
       147904  +
       147905  +    regFlushPart = ++pParse->nMem;
       147906  +    addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
       147907  +    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
       147908  +    sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
       147909  +    VdbeCoverageEqNe(v);
       147910  +    addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub, regFlushPart);
       147911  +    VdbeComment((v, "call flush_partition"));
       147912  +    sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
       147913  +  }
       147914  +
       147915  +  /* Insert the new row into the ephemeral table */
       147916  +  sqlite3VdbeAddOp2(v, OP_NewRowid, csrWrite, regRowid);
       147917  +  sqlite3VdbeAddOp3(v, OP_Insert, csrWrite, regRecord, regRowid);
       147918  +  addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, regRowid);
       147919  +  VdbeCoverageNeverNull(v);
       147920  +
       147921  +  /* This block is run for the first row of each partition */
       147922  +  s.regArg = windowInitAccum(pParse, pMWin);
       147923  +
       147924  +  if( regStart ){
       147925  +    sqlite3ExprCode(pParse, pMWin->pStart, regStart);
       147926  +    windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
       147927  +  }
       147928  +  if( regEnd ){
       147929  +    sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
       147930  +    windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
       147931  +  }
       147932  +
       147933  +  if( pMWin->eStart==pMWin->eEnd && regStart ){
       147934  +    int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
       147935  +    int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
       147936  +    VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
       147937  +    VdbeCoverageNeverNullIf(v, op==OP_Le); /*   values previously checked */
       147938  +    windowAggFinal(&s, 0);
       147939  +    sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
       147940  +    VdbeCoverageNeverTaken(v);
       147941  +    windowReturnOneRow(&s);
       147942  +    sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
       147943  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
       147944  +    sqlite3VdbeJumpHere(v, addrGe);
       147945  +  }
       147946  +  if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
       147947  +    assert( pMWin->eEnd==TK_FOLLOWING );
       147948  +    sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regStart);
       147949  +  }
       147950  +
       147951  +  if( pMWin->eStart!=TK_UNBOUNDED ){
       147952  +    sqlite3VdbeAddOp2(v, OP_Rewind, s.start.csr, 1);
       147953  +    VdbeCoverageNeverTaken(v);
       147954  +  }
       147955  +  sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
       147956  +  VdbeCoverageNeverTaken(v);
       147957  +  sqlite3VdbeAddOp2(v, OP_Rewind, s.end.csr, 1);
       147958  +  VdbeCoverageNeverTaken(v);
       147959  +  if( regPeer && pOrderBy ){
       147960  +    sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
       147961  +    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
       147962  +    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
       147963  +    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
       147964  +  }
       147965  +
       147966  +  sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
       147967  +
       147968  +  sqlite3VdbeJumpHere(v, addrNe);
       147969  +
       147970  +  /* Beginning of the block executed for the second and subsequent rows. */
       147971  +  if( regPeer ){
       147972  +    windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd);
       147973  +  }
       147974  +  if( pMWin->eStart==TK_FOLLOWING ){
       147975  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       147976  +    if( pMWin->eEnd!=TK_UNBOUNDED ){
       147977  +      if( pMWin->eFrmType==TK_RANGE ){
       147978  +        int lbl = sqlite3VdbeMakeLabel(pParse);
       147979  +        int addrNext = sqlite3VdbeCurrentAddr(v);
       147980  +        windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
       147981  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       147982  +        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       147983  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
       147984  +        sqlite3VdbeResolveLabel(v, lbl);
       147985  +      }else{
       147986  +        windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 0);
       147987  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       147988  +      }
       147989  +    }
       147990  +  }else
       147991  +  if( pMWin->eEnd==TK_PRECEDING ){
       147992  +    int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
       147993  +    windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
       147994  +    if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       147995  +    windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       147996  +    if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       147997  +  }else{
       147998  +    int addr = 0;
       147999  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       148000  +    if( pMWin->eEnd!=TK_UNBOUNDED ){
       148001  +      if( pMWin->eFrmType==TK_RANGE ){
       148002  +        int lbl = 0;
       148003  +        addr = sqlite3VdbeCurrentAddr(v);
       148004  +        if( regEnd ){
       148005  +          lbl = sqlite3VdbeMakeLabel(pParse);
       148006  +          windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
       148007  +        }
       148008  +        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148009  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148010  +        if( regEnd ){
       148011  +          sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
       148012  +          sqlite3VdbeResolveLabel(v, lbl);
       148013  +        }
       148014  +      }else{
       148015  +        if( regEnd ){
       148016  +          addr = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
       148017  +          VdbeCoverage(v);
       148018  +        }
       148019  +        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148020  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148021  +        if( regEnd ) sqlite3VdbeJumpHere(v, addr);
       148022  +      }
       148023  +    }
       148024  +  }
       148025  +
       148026  +  /* End of the main input loop */
       148027  +  sqlite3VdbeResolveLabel(v, lblWhereEnd);
       148028  +  sqlite3WhereEnd(pWInfo);
       148029  +
       148030  +  /* Fall through */
       148031  +  if( pMWin->pPartition ){
       148032  +    addrInteger = sqlite3VdbeAddOp2(v, OP_Integer, 0, regFlushPart);
       148033  +    sqlite3VdbeJumpHere(v, addrGosubFlush);
       148034  +  }
       148035  +
       148036  +  addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind, csrWrite);
       148037  +  VdbeCoverage(v);
       148038  +  if( pMWin->eEnd==TK_PRECEDING ){
       148039  +    int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
       148040  +    windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
       148041  +    if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148042  +    windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148043  +  }else if( pMWin->eStart==TK_FOLLOWING ){
       148044  +    int addrStart;
       148045  +    int addrBreak1;
       148046  +    int addrBreak2;
       148047  +    int addrBreak3;
       148048  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       148049  +    if( pMWin->eFrmType==TK_RANGE ){
       148050  +      addrStart = sqlite3VdbeCurrentAddr(v);
       148051  +      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
       148052  +      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
       148053  +    }else
       148054  +    if( pMWin->eEnd==TK_UNBOUNDED ){
       148055  +      addrStart = sqlite3VdbeCurrentAddr(v);
       148056  +      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regStart, 1);
       148057  +      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, 0, 1);
       148058  +    }else{
       148059  +      assert( pMWin->eEnd==TK_FOLLOWING );
       148060  +      addrStart = sqlite3VdbeCurrentAddr(v);
       148061  +      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 1);
       148062  +      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
       148063  +    }
       148064  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
       148065  +    sqlite3VdbeJumpHere(v, addrBreak2);
       148066  +    addrStart = sqlite3VdbeCurrentAddr(v);
       148067  +    addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
       148068  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
       148069  +    sqlite3VdbeJumpHere(v, addrBreak1);
       148070  +    sqlite3VdbeJumpHere(v, addrBreak3);
       148071  +  }else{
       148072  +    int addrBreak;
       148073  +    int addrStart;
       148074  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       148075  +    addrStart = sqlite3VdbeCurrentAddr(v);
       148076  +    addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
       148077  +    windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148078  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
       148079  +    sqlite3VdbeJumpHere(v, addrBreak);
       148080  +  }
       148081  +  sqlite3VdbeJumpHere(v, addrEmpty);
       148082  +
       148083  +  sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
       148084  +  if( pMWin->pPartition ){
       148085  +    if( pMWin->regStartRowid ){
       148086  +      sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
       148087  +      sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
       148088  +    }
       148089  +    sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
       148090  +    sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
147313 148091     }
147314 148092   }
147315 148093   
147316 148094   #endif /* SQLITE_OMIT_WINDOWFUNC */
147317 148095   
147318 148096   /************** End of window.c **********************************************/
147319 148097   /************** Begin file parse.c *******************************************/
................................................................................
147494 148272       ){
147495 148273         sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
147496 148274                            pIdToken->n, pIdToken->z);
147497 148275       }
147498 148276       sqlite3ExprListSetName(pParse, p, pIdToken, 1);
147499 148277       return p;
147500 148278     }
       148279  +
       148280  +#if TK_SPAN>255
       148281  +# error too many tokens in the grammar
       148282  +#endif
147501 148283   /**************** End of %include directives **********************************/
147502 148284   /* These constants specify the various numeric values for terminal symbols
147503 148285   ** in a format understandable to "makeheaders".  This section is blank unless
147504 148286   ** "lemon" is run with the "-m" command-line option.
147505 148287   ***************** Begin makeheaders token definitions *************************/
147506 148288   /**************** End makeheaders token definitions ***************************/
147507 148289   
................................................................................
147557 148339   **    YY_MAX_REDUCE      Maximum value for reduce actions
147558 148340   */
147559 148341   #ifndef INTERFACE
147560 148342   # define INTERFACE 1
147561 148343   #endif
147562 148344   /************* Begin control #defines *****************************************/
147563 148345   #define YYCODETYPE unsigned short int
147564         -#define YYNOCODE 278
       148346  +#define YYNOCODE 301
147565 148347   #define YYACTIONTYPE unsigned short int
147566         -#define YYWILDCARD 91
       148348  +#define YYWILDCARD 95
147567 148349   #define sqlite3ParserTOKENTYPE Token
147568 148350   typedef union {
147569 148351     int yyinit;
147570 148352     sqlite3ParserTOKENTYPE yy0;
147571         -  ExprList* yy42;
147572         -  int yy96;
147573         -  TriggerStep* yy119;
147574         -  Window* yy147;
147575         -  SrcList* yy167;
147576         -  Upsert* yy266;
147577         -  struct FrameBound yy317;
147578         -  IdList* yy336;
147579         -  struct TrigEvent yy350;
147580         -  struct {int value; int mask;} yy367;
147581         -  Select* yy423;
147582         -  const char* yy464;
147583         -  Expr* yy490;
147584         -  With* yy499;
       148353  +  With* yy59;
       148354  +  IdList* yy62;
       148355  +  struct TrigEvent yy90;
       148356  +  Upsert* yy136;
       148357  +  struct FrameBound yy201;
       148358  +  u8 yy238;
       148359  +  const char* yy294;
       148360  +  Window* yy295;
       148361  +  struct {int value; int mask;} yy355;
       148362  +  ExprList* yy434;
       148363  +  TriggerStep* yy455;
       148364  +  Select* yy457;
       148365  +  SrcList* yy483;
       148366  +  int yy494;
       148367  +  Expr* yy524;
147585 148368   } YYMINORTYPE;
147586 148369   #ifndef YYSTACKDEPTH
147587 148370   #define YYSTACKDEPTH 100
147588 148371   #endif
147589 148372   #define sqlite3ParserARG_SDECL
147590 148373   #define sqlite3ParserARG_PDECL
147591 148374   #define sqlite3ParserARG_PARAM
................................................................................
147593 148376   #define sqlite3ParserARG_STORE
147594 148377   #define sqlite3ParserCTX_SDECL Parse *pParse;
147595 148378   #define sqlite3ParserCTX_PDECL ,Parse *pParse
147596 148379   #define sqlite3ParserCTX_PARAM ,pParse
147597 148380   #define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
147598 148381   #define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
147599 148382   #define YYFALLBACK 1
147600         -#define YYNSTATE             524
147601         -#define YYNRULE              369
147602         -#define YYNTOKEN             155
147603         -#define YY_MAX_SHIFT         523
147604         -#define YY_MIN_SHIFTREDUCE   760
147605         -#define YY_MAX_SHIFTREDUCE   1128
147606         -#define YY_ERROR_ACTION      1129
147607         -#define YY_ACCEPT_ACTION     1130
147608         -#define YY_NO_ACTION         1131
147609         -#define YY_MIN_REDUCE        1132
147610         -#define YY_MAX_REDUCE        1500
       148383  +#define YYNSTATE             541
       148384  +#define YYNRULE              375
       148385  +#define YYNTOKEN             176
       148386  +#define YY_MAX_SHIFT         540
       148387  +#define YY_MIN_SHIFTREDUCE   784
       148388  +#define YY_MAX_SHIFTREDUCE   1158
       148389  +#define YY_ERROR_ACTION      1159
       148390  +#define YY_ACCEPT_ACTION     1160
       148391  +#define YY_NO_ACTION         1161
       148392  +#define YY_MIN_REDUCE        1162
       148393  +#define YY_MAX_REDUCE        1536
147611 148394   /************* End control #defines *******************************************/
147612 148395   #define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
147613 148396   
147614 148397   /* Define the yytestcase() macro to be a no-op if is not already defined
147615 148398   ** otherwise.
147616 148399   **
147617 148400   ** Applications can choose to define yytestcase() in the %include section
................................................................................
147670 148453   **  yy_shift_ofst[]    For each state, the offset into yy_action for
147671 148454   **                     shifting terminals.
147672 148455   **  yy_reduce_ofst[]   For each state, the offset into yy_action for
147673 148456   **                     shifting non-terminals after a reduce.
147674 148457   **  yy_default[]       Default action for each state.
147675 148458   **
147676 148459   *********** Begin parsing tables **********************************************/
147677         -#define YY_ACTTAB_COUNT (2009)
       148460  +#define YY_ACTTAB_COUNT (2142)
147678 148461   static const YYACTIONTYPE yy_action[] = {
147679         - /*     0 */   377,  518,  371,  107,  104,  200, 1293,  518, 1130,    1,
147680         - /*    10 */     1,  523,    2, 1134,  518, 1203, 1203, 1262,  277,  373,
147681         - /*    20 */   129,  495,   37,   37, 1397, 1201, 1201, 1211,   65,   65,
147682         - /*    30 */   480,  891,  107,  104,  200,   37,   37, 1043, 1494,  892,
147683         - /*    40 */   346, 1494,  342,  114,  115,  105, 1106, 1106,  957,  960,
147684         - /*    50 */   950,  950,  112,  112,  113,  113,  113,  113,  285,  254,
147685         - /*    60 */   254,  518,  254,  254,  500,  518,  495,  518,  107,  104,
147686         - /*    70 */   200, 1085,  515,  481,  386,  515, 1464,  442,  501,  230,
147687         - /*    80 */   197,  439,   37,   37, 1172,  210,   65,   65,   65,   65,
147688         - /*    90 */   254,  254,  111,  111,  111,  111,  110,  110,  109,  109,
147689         - /*   100 */   109,  108,  404,  515,  404,  155, 1041,  431,  401,  400,
147690         - /*   110 */   254,  254,  373, 1431, 1427,  408, 1110, 1085, 1086, 1087,
147691         - /*   120 */   284, 1112,  500,  515,  500,  368, 1433, 1421, 1428, 1111,
147692         - /*   130 */  1261,  499,  373,  502,  108,  404,  114,  115,  105, 1106,
147693         - /*   140 */  1106,  957,  960,  950,  950,  112,  112,  113,  113,  113,
147694         - /*   150 */   113,  276,  509, 1113,  369, 1113,  114,  115,  105, 1106,
147695         - /*   160 */  1106,  957,  960,  950,  950,  112,  112,  113,  113,  113,
147696         - /*   170 */   113,  496, 1420, 1431,  493, 1468, 1065,  260, 1063,  433,
147697         - /*   180 */    74,  107,  104,  200,  498,  111,  111,  111,  111,  110,
147698         - /*   190 */   110,  109,  109,  109,  108,  404,  373,  113,  113,  113,
147699         - /*   200 */   113,  106,  131,   91, 1361,  111,  111,  111,  111,  110,
147700         - /*   210 */   110,  109,  109,  109,  108,  404,  113,  113,  113,  113,
147701         - /*   220 */   114,  115,  105, 1106, 1106,  957,  960,  950,  950,  112,
147702         - /*   230 */   112,  113,  113,  113,  113,  111,  111,  111,  111,  110,
147703         - /*   240 */   110,  109,  109,  109,  108,  404,  116,  110,  110,  109,
147704         - /*   250 */   109,  109,  108,  404,  111,  111,  111,  111,  110,  110,
147705         - /*   260 */   109,  109,  109,  108,  404,  917,  512,  512,  512,  111,
147706         - /*   270 */   111,  111,  111,  110,  110,  109,  109,  109,  108,  404,
147707         - /*   280 */   517, 1198, 1177,  181,  109,  109,  109,  108,  404,  373,
147708         - /*   290 */  1198,  402,  402,  402,   75,  360,  111,  111,  111,  111,
147709         - /*   300 */   110,  110,  109,  109,  109,  108,  404,  382,  299,  419,
147710         - /*   310 */   287,  170,  518,  114,  115,  105, 1106, 1106,  957,  960,
147711         - /*   320 */   950,  950,  112,  112,  113,  113,  113,  113, 1444,  523,
147712         - /*   330 */     2, 1134,  518,   13,   13,  337,  277, 1085,  129,  226,
147713         - /*   340 */   937, 1058, 1000,  471,  917, 1211,  453,  384, 1085,  395,
147714         - /*   350 */   162, 1057,  155,   45,   45,  416,  928,  401,  400,  479,
147715         - /*   360 */   927,   12,  111,  111,  111,  111,  110,  110,  109,  109,
147716         - /*   370 */   109,  108,  404,  226,  286,  254,  254,  254,  254,  518,
147717         - /*   380 */    16,   16,  373, 1085, 1086, 1087,  314,  299,  515,  472,
147718         - /*   390 */   515,  927,  927,  929, 1085, 1086, 1087,  378,  276,  509,
147719         - /*   400 */    65,   65, 1113,  210, 1113, 1085,  114,  115,  105, 1106,
147720         - /*   410 */  1106,  957,  960,  950,  950,  112,  112,  113,  113,  113,
147721         - /*   420 */   113, 1448,  222, 1134, 1089,  461,  458,  457,  277,  180,
147722         - /*   430 */   129,  378,  392,  408,  423,  456,  500, 1211,  240,  257,
147723         - /*   440 */   324,  464,  319,  463,  227,  470,   12,  317,  424,  300,
147724         - /*   450 */   317, 1085, 1086, 1087,  485,  111,  111,  111,  111,  110,
147725         - /*   460 */   110,  109,  109,  109,  108,  404,  181,  118, 1085,  254,
147726         - /*   470 */   254, 1089,  518,   90,  351,  373,  518, 1181,  365,  798,
147727         - /*   480 */  1440,  339,  515,  248,  248,   77,  325,  133, 1085,  249,
147728         - /*   490 */   424,  300,  794,   49,   49,  210,  515,   65,   65,  114,
147729         - /*   500 */   115,  105, 1106, 1106,  957,  960,  950,  950,  112,  112,
147730         - /*   510 */   113,  113,  113,  113, 1085, 1086, 1087,  222, 1085,  438,
147731         - /*   520 */   461,  458,  457,  937,  787,  408,  171,  857,  362, 1021,
147732         - /*   530 */   456,  136,  198,  486, 1085, 1086, 1087,  448,  794,  928,
147733         - /*   540 */     5,  193,  192,  927, 1022,  107,  104,  200,  111,  111,
147734         - /*   550 */   111,  111,  110,  110,  109,  109,  109,  108,  404, 1023,
147735         - /*   560 */   254,  254,  803, 1085, 1085, 1086, 1087,  437,  373, 1085,
147736         - /*   570 */   344,  787,  791,  515,  927,  927,  929, 1085, 1408, 1396,
147737         - /*   580 */   832, 1085,  176,    3,  852, 1085,  518, 1439,  429,  851,
147738         - /*   590 */   833,  518,  114,  115,  105, 1106, 1106,  957,  960,  950,
147739         - /*   600 */   950,  112,  112,  113,  113,  113,  113,   13,   13, 1085,
147740         - /*   610 */  1086, 1087,   13,   13,  518, 1085, 1086, 1087, 1496,  358,
147741         - /*   620 */  1085,  389, 1234, 1085, 1086, 1087,  391, 1085, 1086, 1087,
147742         - /*   630 */   448, 1085, 1086, 1087,  518,   65,   65,  947,  947,  958,
147743         - /*   640 */   961,  111,  111,  111,  111,  110,  110,  109,  109,  109,
147744         - /*   650 */   108,  404,  518,  382,  878,   13,   13,  518,  877,  518,
147745         - /*   660 */   263,  373,  518,  431,  448, 1070, 1085, 1086, 1087,  267,
147746         - /*   670 */   448,  488, 1360,   64,   64,  431,  812,  155,   50,   50,
147747         - /*   680 */    65,   65,  518,   65,   65,  114,  115,  105, 1106, 1106,
147748         - /*   690 */   957,  960,  950,  950,  112,  112,  113,  113,  113,  113,
147749         - /*   700 */   518,  951,  382,   13,   13,  415,  411,  462,  414, 1085,
147750         - /*   710 */  1366,  777, 1210,  292,  297,  813,  399,  497,  181,  403,
147751         - /*   720 */   261,   15,   15,  276,  509,  414,  413, 1366, 1368,  410,
147752         - /*   730 */   372,  345, 1209,  264,  111,  111,  111,  111,  110,  110,
147753         - /*   740 */   109,  109,  109,  108,  404,  265,  254,  254,  229, 1405,
147754         - /*   750 */   268, 1215,  268, 1103,  373, 1085, 1086, 1087,  938,  515,
147755         - /*   760 */   393,  409,  876,  515,  254,  254, 1152,  482,  473,  262,
147756         - /*   770 */   422,  476,  325,  503,  289,  518,  291,  515,  114,  115,
147757         - /*   780 */   105, 1106, 1106,  957,  960,  950,  950,  112,  112,  113,
147758         - /*   790 */   113,  113,  113,  414, 1021, 1366,   39,   39,  254,  254,
147759         - /*   800 */   254,  254,  980,  254,  254,  254,  254,  255,  255, 1022,
147760         - /*   810 */   279,  515,  516,  515,  846,  846,  515,  138,  515,  518,
147761         - /*   820 */   515, 1043, 1495,  251, 1023, 1495,  876,  111,  111,  111,
147762         - /*   830 */   111,  110,  110,  109,  109,  109,  108,  404,  518, 1353,
147763         - /*   840 */    51,   51,  518,  199,  518,  506,  290,  373,  518,  276,
147764         - /*   850 */   509,  922,    9,  483,  233, 1005, 1005,  445,  189,   52,
147765         - /*   860 */    52,  325,  280,   53,   53,   54,   54,  373,  876,   55,
147766         - /*   870 */    55,  114,  115,  105, 1106, 1106,  957,  960,  950,  950,
147767         - /*   880 */   112,  112,  113,  113,  113,  113,   97,  518,   95, 1104,
147768         - /*   890 */  1041,  114,  115,  105, 1106, 1106,  957,  960,  950,  950,
147769         - /*   900 */   112,  112,  113,  113,  113,  113,  135,  199,   56,   56,
147770         - /*   910 */   765,  766,  767,  225,  224,  223,  518,  283,  437,  233,
147771         - /*   920 */   111,  111,  111,  111,  110,  110,  109,  109,  109,  108,
147772         - /*   930 */   404, 1002,  876,  326,  518, 1002, 1104,   40,   40,  518,
147773         - /*   940 */   111,  111,  111,  111,  110,  110,  109,  109,  109,  108,
147774         - /*   950 */   404,  518,  448,  518, 1104,   41,   41,  518,   17,  518,
147775         - /*   960 */    43,   43, 1155,  379,  518,  448,  518,  443,  518,  390,
147776         - /*   970 */   518,  194,   44,   44,   57,   57, 1247,  518,   58,   58,
147777         - /*   980 */    59,   59,  518,  466,  326,   14,   14,   60,   60,  120,
147778         - /*   990 */   120,   61,   61,  449, 1206,   93,  518,  425,   46,   46,
147779         - /*  1000 */   518, 1104,  518,   62,   62,  518,  437,  305,  518,  852,
147780         - /*  1010 */   518,  298,  518, 1246,  851,  373,  518,   63,   63, 1293,
147781         - /*  1020 */   397,   47,   47,  142,  142, 1467,  143,  143,  821,   70,
147782         - /*  1030 */    70,   48,   48,   66,   66,  373,  518,  121,  121,  114,
147783         - /*  1040 */   115,  105, 1106, 1106,  957,  960,  950,  950,  112,  112,
147784         - /*  1050 */   113,  113,  113,  113,  518,  418,  518,   67,   67,  114,
147785         - /*  1060 */   115,  105, 1106, 1106,  957,  960,  950,  950,  112,  112,
147786         - /*  1070 */   113,  113,  113,  113,  312,  122,  122,  123,  123, 1293,
147787         - /*  1080 */   518,  357, 1126,   88,  518,  435,  325,  387,  111,  111,
147788         - /*  1090 */   111,  111,  110,  110,  109,  109,  109,  108,  404,  266,
147789         - /*  1100 */   518,  119,  119,  518, 1293,  141,  141,  518,  111,  111,
147790         - /*  1110 */   111,  111,  110,  110,  109,  109,  109,  108,  404,  518,
147791         - /*  1120 */   801,  140,  140,  518,  127,  127,  511,  379,  126,  126,
147792         - /*  1130 */   518,  137,  518, 1308,  518,  307,  518,  310,  518,  203,
147793         - /*  1140 */   124,  124, 1307,   96,  125,  125,  207,  388, 1441,  468,
147794         - /*  1150 */  1127,   69,   69,   71,   71,   68,   68,   38,   38,   42,
147795         - /*  1160 */    42,  357, 1042,  373, 1293,  276,  509,  801,  185,  469,
147796         - /*  1170 */   494,  436,  444,    6,  380,  156,  253,  197,  469,  134,
147797         - /*  1180 */   426,   33, 1038,  373, 1121,  359, 1411,  114,  115,  105,
147798         - /*  1190 */  1106, 1106,  957,  960,  950,  950,  112,  112,  113,  113,
147799         - /*  1200 */   113,  113,  914,  296,   27,  293,   90,  114,  103,  105,
147800         - /*  1210 */  1106, 1106,  957,  960,  950,  950,  112,  112,  113,  113,
147801         - /*  1220 */   113,  113,  919,  275,  430,  232,  891,  232,  432,  256,
147802         - /*  1230 */  1127,  232,  398,  370,  892,   28,  111,  111,  111,  111,
147803         - /*  1240 */   110,  110,  109,  109,  109,  108,  404,  301,  454, 1385,
147804         - /*  1250 */    90,  228,  209,  987,  811,  810,  111,  111,  111,  111,
147805         - /*  1260 */   110,  110,  109,  109,  109,  108,  404,  315,  818,  819,
147806         - /*  1270 */    90,  323,  983,  931,  885,  228,  373,  232,  999,  849,
147807         - /*  1280 */   999,  322,  102,  998, 1384,  998,  785,  850,  440,  132,
147808         - /*  1290 */   102,  302, 1243,  306,  309,  311,  373,  313, 1194, 1180,
147809         - /*  1300 */   987,  115,  105, 1106, 1106,  957,  960,  950,  950,  112,
147810         - /*  1310 */   112,  113,  113,  113,  113, 1178, 1179,  318,  327,  328,
147811         - /*  1320 */   931, 1255,  105, 1106, 1106,  957,  960,  950,  950,  112,
147812         - /*  1330 */   112,  113,  113,  113,  113, 1292, 1230, 1457,  273, 1241,
147813         - /*  1340 */   504,  505, 1298,  100,  510,  246,    4, 1161, 1154,  111,
147814         - /*  1350 */   111,  111,  111,  110,  110,  109,  109,  109,  108,  404,
147815         - /*  1360 */   513, 1143,  187, 1142,  202, 1144, 1451,  356, 1227,  111,
147816         - /*  1370 */   111,  111,  111,  110,  110,  109,  109,  109,  108,  404,
147817         - /*  1380 */    11, 1277,  330,  405,  332,  334,  191, 1285,  364,  195,
147818         - /*  1390 */   295,  417,  288,  100,  510,  507,    4,  434,  459,  321,
147819         - /*  1400 */  1177,  349, 1357, 1356,  336,  155,  190, 1454, 1121,  158,
147820         - /*  1410 */   513,  508,  235, 1404,  937, 1402, 1118,  381,   77,  428,
147821         - /*  1420 */    98,   98,    8, 1282,  168,   30,  152,   99,  160,  405,
147822         - /*  1430 */   520,  519,   88,  405,  927, 1362, 1274,  420,  163,   73,
147823         - /*  1440 */   164,   76,  165,  166,  421,  507,  452,  212,  361,  363,
147824         - /*  1450 */   427,  276,  509,   31, 1288,  172,  491,  441,  216, 1351,
147825         - /*  1460 */    82,  490,  447, 1373,  937,  927,  927,  929,  930,   24,
147826         - /*  1470 */    98,   98,  304,  247,  218,  177,  308,   99,  219,  405,
147827         - /*  1480 */   520,  519,  450, 1145,  927,  220,  366, 1197,  100,  510,
147828         - /*  1490 */   465,    4, 1188, 1196, 1195,  394,  803, 1169, 1187,  367,
147829         - /*  1500 */  1168,  396,  484,  320, 1167,  513, 1466,   87,  475,  100,
147830         - /*  1510 */   510,  271,    4,  272,  478,  927,  927,  929,  930,   24,
147831         - /*  1520 */  1443, 1074,  407, 1238, 1239,  258,  513,  329,  405,  331,
147832         - /*  1530 */   355,  355,  354,  243,  352,  234,  489,  774,  498,  184,
147833         - /*  1540 */   507,  338, 1422,  339,  117, 1220,   10,  341,  333,  405,
147834         - /*  1550 */   204,  491,  282, 1219, 1237, 1236,  492,  335,  343,  937,
147835         - /*  1560 */   281,  507,   94, 1337,  186,   98,   98,  347,   89,  487,
147836         - /*  1570 */   348,  241,   99,   29,  405,  520,  519,  274, 1151,  927,
147837         - /*  1580 */   937,  521, 1080,  245,  242,  244,   98,   98,  856,  522,
147838         - /*  1590 */   206, 1140, 1135,   99,  144,  405,  520,  519,  147,  375,
147839         - /*  1600 */   927,  149,  376,  157, 1389, 1390, 1388, 1387,  205,  145,
147840         - /*  1610 */   927,  927,  929,  930,   24,  146,  130,  761, 1165, 1164,
147841         - /*  1620 */    72,  100,  510, 1162,    4,  269,  406,  188,  278,  201,
147842         - /*  1630 */   259,  927,  927,  929,  930,   24,  128,  911,  513,  997,
147843         - /*  1640 */   995,  159,  374,  208,  148,  161,  835,  276,  509,  211,
147844         - /*  1650 */   294, 1011,  915,  167,  150,  383,  169,   78,  385,   79,
147845         - /*  1660 */    80,  405,   81,  151, 1014,  213,  214, 1010,  139,   18,
147846         - /*  1670 */   412,  215,  303,  507,  232, 1115, 1003,  446,  173,  217,
147847         - /*  1680 */   174,   32,  776,  451,  491,  322,  221,  175,  814,  490,
147848         - /*  1690 */    83,  455,  937,   19,  460,  316,   20,   84,   98,   98,
147849         - /*  1700 */   270,  182,   85,  467,  153,   99,  154,  405,  520,  519,
147850         - /*  1710 */  1074,  407,  927,  183,  258,  963, 1046,   86,   34,  355,
147851         - /*  1720 */   355,  354,  243,  352,  474, 1047,  774,   35,  477,  196,
147852         - /*  1730 */   250,  100,  510,  252,    4,  884,  178,  231, 1060,  204,
147853         - /*  1740 */    21,  282,  102,  927,  927,  929,  930,   24,  513,  281,
147854         - /*  1750 */   879,   22, 1064, 1062, 1051,    7,  340,   23,  978,  179,
147855         - /*  1760 */    90,   92,  510,  964,    4,  236,  962,  966, 1020, 1019,
147856         - /*  1770 */   237,  405,  967,   25,   36,  514,  932,  786,  513,  206,
147857         - /*  1780 */   101,   26,  845,  507,  238,  239, 1459,  147,  350, 1458,
147858         - /*  1790 */   149,  353, 1075, 1131, 1131, 1131, 1131,  205, 1131, 1131,
147859         - /*  1800 */  1131,  405,  937, 1131, 1131, 1131, 1131, 1131,   98,   98,
147860         - /*  1810 */  1131, 1131, 1131,  507, 1131,   99, 1131,  405,  520,  519,
147861         - /*  1820 */  1131, 1131,  927, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147862         - /*  1830 */  1131,  374,  937, 1131, 1131, 1131,  276,  509,   98,   98,
147863         - /*  1840 */  1131, 1131, 1131, 1131, 1131,   99, 1131,  405,  520,  519,
147864         - /*  1850 */  1131, 1131,  927,  927,  927,  929,  930,   24, 1131,  412,
147865         - /*  1860 */  1131, 1131, 1131,  258, 1131, 1131, 1131, 1131,  355,  355,
147866         - /*  1870 */   354,  243,  352, 1131, 1131,  774, 1131, 1131, 1131, 1131,
147867         - /*  1880 */  1131, 1131, 1131,  927,  927,  929,  930,   24,  204, 1131,
147868         - /*  1890 */   282, 1131, 1131, 1131, 1131, 1131, 1131, 1131,  281, 1131,
147869         - /*  1900 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147870         - /*  1910 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147871         - /*  1920 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,  206, 1131,
147872         - /*  1930 */  1131, 1131, 1131, 1131, 1131, 1131,  147, 1131, 1131,  149,
147873         - /*  1940 */  1131, 1131, 1131, 1131, 1131, 1131,  205, 1131, 1131, 1131,
147874         - /*  1950 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147875         - /*  1960 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147876         - /*  1970 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147877         - /*  1980 */   374, 1131, 1131, 1131, 1131,  276,  509, 1131, 1131, 1131,
147878         - /*  1990 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147879         - /*  2000 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,  412,
       148462  + /*     0 */   535, 1323,  112,  109,  209,  112,  109,  209, 1160,    1,
       148463  + /*    10 */     1,  540,    2, 1164,  535, 1292, 1228, 1207,  289,  384,
       148464  + /*    20 */   134,   42,   42, 1427,  382, 1228,    9, 1241,  242,  492,
       148465  + /*    30 */  1291,  915,  373,  379, 1026,   70,   70,  427, 1026,  916,
       148466  + /*    40 */   529,  529,  529,  119,  120,  110, 1136, 1136,  981,  984,
       148467  + /*    50 */   974,  974,  117,  117,  118,  118,  118,  118,  380,  264,
       148468  + /*    60 */   264,  264,  264, 1134,  264,  264,  112,  109,  209,  397,
       148469  + /*    70 */   454,  517,  532,  491,  532, 1233, 1233,  532,  239,  206,
       148470  + /*    80 */   493,  112,  109,  209,  464,  219,  118,  118,  118,  118,
       148471  + /*    90 */   111,  393,  440,  444,   16,   16,  116,  116,  116,  116,
       148472  + /*   100 */   115,  115,  114,  114,  114,  113,  415,  971,  971,  982,
       148473  + /*   110 */   985,  235, 1463,  351, 1134,  419,  384,  116,  116,  116,
       148474  + /*   120 */   116,  115,  115,  114,  114,  114,  113,  415,  116,  116,
       148475  + /*   130 */   116,  116,  115,  115,  114,  114,  114,  113,  415,  961,
       148476  + /*   140 */   119,  120,  110, 1136, 1136,  981,  984,  974,  974,  117,
       148477  + /*   150 */   117,  118,  118,  118,  118,  952,  415,  941,  298,  951,
       148478  + /*   160 */   941, 1480,  540,    2, 1164, 1115,  535, 1458,  160,  289,
       148479  + /*   170 */     6,  134, 1504,  389,  406,  975,  338, 1024, 1241,  337,
       148480  + /*   180 */  1089, 1476, 1089,  118,  118,  118,  118,   42,   42,  329,
       148481  + /*   190 */   951,  951,  953,  116,  116,  116,  116,  115,  115,  114,
       148482  + /*   200 */   114,  114,  113,  415,  311,  430,  299,  311,  881,  160,
       148483  + /*   210 */   264,  264,  401,  384,  324, 1115, 1116, 1117,  288,  526,
       148484  + /*   220 */    96,  159, 1441,  532,  141,  116,  116,  116,  116,  115,
       148485  + /*   230 */   115,  114,  114,  114,  113,  415,  219,  119,  120,  110,
       148486  + /*   240 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148487  + /*   250 */   118,  118,  115,  115,  114,  114,  114,  113,  415,  288,
       148488  + /*   260 */   526,  403,  533,  121,  870,  870,  419,  250,  267,  336,
       148489  + /*   270 */   475,  331,  474,  236,  160,  319, 1084,  322, 1465,  329,
       148490  + /*   280 */   350,   12,  535,  384,  502, 1115, 1084,  435,  312, 1084,
       148491  + /*   290 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148492  + /*   300 */   415,  535,  836,   42,   42,  138,  426,  119,  120,  110,
       148493  + /*   310 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148494  + /*   320 */   118,  118,   70,   70,  288,  526,  412,  411,  480, 1457,
       148495  + /*   330 */   335,   79,    6,  473, 1140, 1115, 1116, 1117,  501, 1142,
       148496  + /*   340 */   334,  837,  811, 1484,  512, 1164,  534, 1141,  123,  187,
       148497  + /*   350 */   289,  384,  134,  448,  434, 1115,   80,  349,  498, 1241,
       148498  + /*   360 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148499  + /*   370 */   415, 1143, 1115, 1143,  459,  119,  120,  110, 1136, 1136,
       148500  + /*   380 */   981,  984,  974,  974,  117,  117,  118,  118,  118,  118,
       148501  + /*   390 */   404,  264,  264,  811, 1463,  506,  368, 1156,  535,  114,
       148502  + /*   400 */   114,  114,  113,  415,  532, 1115, 1116, 1117,  231,  518,
       148503  + /*   410 */  1500,  472,  469,  468,  175,  497,  422,  219, 1202,   70,
       148504  + /*   420 */    70,  467, 1115, 1116, 1117,  176,  201,  200,  116,  116,
       148505  + /*   430 */   116,  116,  115,  115,  114,  114,  114,  113,  415,  535,
       148506  + /*   440 */  1115,  264,  264,  435,  312, 1115,  273,  419,  384,  513,
       148507  + /*   450 */  1450, 1115,  326, 1084,  532,  517,   82, 1084,  167,  388,
       148508  + /*   460 */    69,   69, 1115, 1084,  519,  509, 1084, 1084,   12, 1157,
       148509  + /*   470 */  1084,  420,  119,  120,  110, 1136, 1136,  981,  984,  974,
       148510  + /*   480 */   974,  117,  117,  118,  118,  118,  118,  258,  258,  535,
       148511  + /*   490 */  1115, 1116, 1117, 1045,  535, 1115, 1116, 1117, 1323,  535,
       148512  + /*   500 */   532, 1115, 1116, 1117,  296,  483, 1211,  818, 1046,  448,
       148513  + /*   510 */    70,   70, 1115, 1116, 1117,   50,   50,  448,  356,  500,
       148514  + /*   520 */    70,   70,  207, 1047,   32,  116,  116,  116,  116,  115,
       148515  + /*   530 */   115,  114,  114,  114,  113,  415,  453,  264,  264, 1115,
       148516  + /*   540 */   450,  449,  961,  508,  856,  384,  517,    5,  900,  822,
       148517  + /*   550 */   532,  484,  181, 1115,  857,  516,  517,  818,  952,  507,
       148518  + /*   560 */     3, 1115,  951, 1231, 1231,  482,  398, 1115, 1095,  119,
       148519  + /*   570 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
       148520  + /*   580 */   118,  118,  118,  118, 1115,  535,  238, 1115, 1391, 1115,
       148521  + /*   590 */  1116, 1117,  159,  951,  951,  953,  231, 1115,  259,  472,
       148522  + /*   600 */   469,  468,  310, 1115, 1116, 1117,   13,   13,  297,  467,
       148523  + /*   610 */   276, 1115, 1116, 1117,  412,  411, 1095, 1115, 1116, 1117,
       148524  + /*   620 */   395,  355,  116,  116,  116,  116,  115,  115,  114,  114,
       148525  + /*   630 */   114,  113,  415,  208, 1115, 1116, 1117, 1115, 1116, 1117,
       148526  + /*   640 */   264,  264,  384,  337,  902,  393,  815, 1115, 1116, 1117,
       148527  + /*   650 */   413,  413,  413,  532,  112,  109,  209,  309,  900, 1143,
       148528  + /*   660 */   535, 1143,  535,  393,  901, 1210,  119,  120,  110, 1136,
       148529  + /*   670 */  1136,  981,  984,  974,  974,  117,  117,  118,  118,  118,
       148530  + /*   680 */   118,   13,   13,   13,   13,  265,  265,  535,  143,  264,
       148531  + /*   690 */   264,  288,  526,  535, 1119,  400,  535,  402,  532,  510,
       148532  + /*   700 */  1457,  512,  532,    6,  113,  415, 1067, 1530,   70,   70,
       148533  + /*   710 */  1530,  535,  271,  535,   70,   70,  535,   13,   13,  116,
       148534  + /*   720 */   116,  116,  116,  115,  115,  114,  114,  114,  113,  415,
       148535  + /*   730 */   272,  277,   13,   13,   13,   13,  535,   13,   13,  384,
       148536  + /*   740 */   535,  304,  425, 1100,  284, 1119,  184,  801,  185,  338,
       148537  + /*   750 */   285,  514, 1532,  369, 1239, 1438, 1182,   70,   70,  425,
       148538  + /*   760 */   424,   70,   70,  119,  120,  110, 1136, 1136,  981,  984,
       148539  + /*   770 */   974,  974,  117,  117,  118,  118,  118,  118,  190, 1065,
       148540  + /*   780 */  1067, 1531,  442,  107, 1531,  408,  264,  264,  264,  264,
       148541  + /*   790 */   383, 1396,  261,  410,   95,  900,  485,  414,  421,  532,
       148542  + /*   800 */  1045,  532,  301, 1133,  303,  488,  433, 1451, 1396, 1398,
       148543  + /*   810 */   278,  535,  278,  520, 1435, 1046,  116,  116,  116,  116,
       148544  + /*   820 */   115,  115,  114,  114,  114,  113,  415,  425,  264,  264,
       148545  + /*   830 */  1047,  190,   54,   54,  535,  291,  384,  264,  264,  362,
       148546  + /*   840 */   962,  532, 1004,  376, 1084,  264,  264, 1029, 1029,  456,
       148547  + /*   850 */   532,  523,  270, 1065, 1084,   55,   55, 1084,  532,  442,
       148548  + /*   860 */   119,  120,  110, 1136, 1136,  981,  984,  974,  974,  117,
       148549  + /*   870 */   117,  118,  118,  118,  118,  535, 1396,  190,  302, 1383,
       148550  + /*   880 */   208,  535,  789,  790,  791,  535,  515,  535, 1323,  371,
       148551  + /*   890 */   337,  234,  233,  232,  459,  515,   15,   15,  459,  477,
       148552  + /*   900 */   459,  459,   44,   44,  136,  900,   56,   56,   57,   57,
       148553  + /*   910 */  1185,  390,  197,  116,  116,  116,  116,  115,  115,  114,
       148554  + /*   920 */   114,  114,  113,  415,  535,  876,  535,  442,  535,  274,
       148555  + /*   930 */   875, 1323,  357,  384,  353,  140, 1426,  946, 1455, 1323,
       148556  + /*   940 */  1390,    6, 1240, 1236,  292,   58,   58,   59,   59,   60,
       148557  + /*   950 */    60,  535, 1456,  384,  535,    6,  399,  119,  120,  110,
       148558  + /*   960 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148559  + /*   970 */   118,  118,   61,   61,  535,   45,   45,  119,  120,  110,
       148560  + /*   980 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148561  + /*   990 */   118,  118, 1477,  479,  202,   46,   46,  275,   95,  455,
       148562  + /*  1000 */   535,  212,  535,  337,  535, 1454,  535,  409,    6,  242,
       148563  + /*  1010 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148564  + /*  1020 */   415,   48,   48,   49,   49,   62,   62,   63,   63,  535,
       148565  + /*  1030 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148566  + /*  1040 */   415,  535,  459,  535, 1134,  535, 1151,  535,  142,  535,
       148567  + /*  1050 */    64,   64,  535, 1338,  535,  494,  535,  446,  535, 1264,
       148568  + /*  1060 */   535, 1337,   14,   14,   65,   65,  125,  125,   66,   66,
       148569  + /*  1070 */    51,   51,  535,   67,   67,   68,   68,   52,   52,  147,
       148570  + /*  1080 */   147,  148,  148, 1453,  317,   98,    6,  535, 1245,  481,
       148571  + /*  1090 */   535,  827,  535,   75,   75, 1134,  102,  481,  100,  535,
       148572  + /*  1100 */   532,  535,  368, 1066, 1503,  384,  535,  845,   53,   53,
       148573  + /*  1110 */    93,   71,   71,  126,  126,  295,  528,  390,  288,  526,
       148574  + /*  1120 */    72,   72,  127,  127,  139,  384,   38,  128,  128,  119,
       148575  + /*  1130 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
       148576  + /*  1140 */   118,  118,  118,  118,  535,  495,  535,  447,  535,  119,
       148577  + /*  1150 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
       148578  + /*  1160 */   118,  118,  118,  118,  235,  124,  124,  146,  146,  145,
       148579  + /*  1170 */   145,  287,  535, 1277,  535, 1157,  535,  391,  161,  263,
       148580  + /*  1180 */   206,  381,  116,  116,  116,  116,  115,  115,  114,  114,
       148581  + /*  1190 */   114,  113,  415,  132,  132,  131,  131,  129,  129,  535,
       148582  + /*  1200 */    30,  535,  116,  116,  116,  116,  115,  115,  114,  114,
       148583  + /*  1210 */   114,  113,  415,  535,  216, 1062, 1276,  535,  370,  535,
       148584  + /*  1220 */   130,  130,   74,   74,  535,  915,  389,  876,   17,  437,
       148585  + /*  1230 */   429,   31,  875,  916,   76,   76,  266,  101,   73,   73,
       148586  + /*  1240 */    43,   43,  835,  834,  308,   47,   47,   95,  825,  943,
       148587  + /*  1250 */   441,  938,  241,  241,  305,  443,  313,  384,  241,   95,
       148588  + /*  1260 */   842,  843,  193,  465, 1209,  327,  237,  436,   95, 1011,
       148589  + /*  1270 */  1007,  909,  873,  237,  241,  107, 1023,  384, 1023,  955,
       148590  + /*  1280 */  1415,  119,  120,  110, 1136, 1136,  981,  984,  974,  974,
       148591  + /*  1290 */   117,  117,  118,  118,  118,  118, 1022,  809, 1022,  825,
       148592  + /*  1300 */   137,  119,  108,  110, 1136, 1136,  981,  984,  974,  974,
       148593  + /*  1310 */   117,  117,  118,  118,  118,  118,  874, 1414,  451,  107,
       148594  + /*  1320 */  1011,  314, 1273,  318,  218,  321,  323,  325, 1224, 1208,
       148595  + /*  1330 */   955,  330,  339,  340,  116,  116,  116,  116,  115,  115,
       148596  + /*  1340 */   114,  114,  114,  113,  415, 1285, 1322, 1260, 1493, 1470,
       148597  + /*  1350 */  1271,  283,  521, 1328,  116,  116,  116,  116,  115,  115,
       148598  + /*  1360 */   114,  114,  114,  113,  415, 1191, 1184, 1173, 1172, 1174,
       148599  + /*  1370 */   522, 1487,  211,  460,  384,  256,  199,  367, 1257,  342,
       148600  + /*  1380 */   195,  470,  307,  344,   11,  333,  525,  445, 1307, 1315,
       148601  + /*  1390 */   375,  203, 1207, 1151,  384,  346, 1387,  188,  360,  120,
       148602  + /*  1400 */   110, 1136, 1136,  981,  984,  974,  974,  117,  117,  118,
       148603  + /*  1410 */   118,  118,  118, 1386,  428, 1490,  245,  300,  348, 1148,
       148604  + /*  1420 */   110, 1136, 1136,  981,  984,  974,  974,  117,  117,  118,
       148605  + /*  1430 */   118,  118,  118,  189,  198, 1434, 1432,   78,   81,  163,
       148606  + /*  1440 */    82,  392,  439, 1392,  173,  105,  527,   35,    4,  157,
       148607  + /*  1450 */  1312,  116,  116,  116,  116,  115,  115,  114,  114,  114,
       148608  + /*  1460 */   113,  415,  530,  165,   93, 1304,  431,  432,  168,  463,
       148609  + /*  1470 */   221,  116,  116,  116,  116,  115,  115,  114,  114,  114,
       148610  + /*  1480 */   113,  415,  169,  452,  170,  416,  171,  374,  372,  438,
       148611  + /*  1490 */    36, 1318,  177,  225, 1381,   87,  458,  524, 1403,  316,
       148612  + /*  1500 */   257,  105,  527,  227,    4,  182,  461,  160,  320,  228,
       148613  + /*  1510 */   377, 1175,  476,  229, 1227, 1226,  405, 1225,  530, 1218,
       148614  + /*  1520 */   961,  378, 1199, 1198,  827,  332,  103,  103, 1197,  407,
       148615  + /*  1530 */     8, 1217, 1502,  104,  487,  416,  537,  536,  281,  282,
       148616  + /*  1540 */   951,  416,  490, 1268,  496,   92,  341,  243, 1269,  343,
       148617  + /*  1550 */   244, 1267,  122,  524,  345, 1461,  515,  288,  526,   10,
       148618  + /*  1560 */   354, 1266, 1460,  352,  504, 1250,   99, 1367,   94,  503,
       148619  + /*  1570 */   499,  951,  951,  953,  954,   27,  961,  347, 1249,  194,
       148620  + /*  1580 */   251,  358,  103,  103,  359, 1181,   34,  538, 1110,  104,
       148621  + /*  1590 */   255,  416,  537,  536,  286,  252,  951,  254,  539,  149,
       148622  + /*  1600 */  1170, 1419, 1165, 1420, 1418,  150, 1417,  135,  279,  785,
       148623  + /*  1610 */   151,  417, 1195,  196,  290,  210,  386, 1194,  269,  387,
       148624  + /*  1620 */   162, 1021,  133,   77, 1192, 1019,  935,  951,  951,  953,
       148625  + /*  1630 */   954,   27, 1479, 1104,  418,  164,  153,  268,  217,  166,
       148626  + /*  1640 */   859,  306,  366,  366,  365,  253,  363,  220, 1035,  798,
       148627  + /*  1650 */   172,  939,  105,  527,  155,    4,  394,  174,  396,  156,
       148628  + /*  1660 */    83, 1038,  213,   84,  294,   85,   86,  223,  222,  530,
       148629  + /*  1670 */  1034,  144,  293,   18,  224,  315,  241, 1027, 1145,  178,
       148630  + /*  1680 */   457,  226,  179,   37,  800,  334,  462,  230,  328,  466,
       148631  + /*  1690 */   180,  471,  416,   88,   19,   20,   89,  280,  838,  158,
       148632  + /*  1700 */   191,   90,  215,  478,  524, 1097,  204,  192,  987,   91,
       148633  + /*  1710 */   152, 1070,   39,  154, 1071,  504,  486,   40,  489,  205,
       148634  + /*  1720 */   505,  260,  105,  527,  214,    4,  908,  961,  262,  183,
       148635  + /*  1730 */   240,   21,  903,  103,  103,  107,   22, 1086,   23,  530,
       148636  + /*  1740 */   104, 1088,  416,  537,  536,   24, 1093,  951,   25, 1074,
       148637  + /*  1750 */  1090, 1094,    7,   33,  511,  186,   26, 1002,  385,   95,
       148638  + /*  1760 */   988,  986,  416,  288,  526,  990, 1044,  246, 1043,  247,
       148639  + /*  1770 */   991,   28,   41,  106,  524,  956,  810,   29,  951,  951,
       148640  + /*  1780 */   953,  954,   27,  531,  361,  504,  423,  248,  869,  249,
       148641  + /*  1790 */   503, 1495,  364, 1105, 1161, 1494, 1161,  961, 1161, 1161,
       148642  + /*  1800 */  1161, 1161, 1161,  103,  103, 1161, 1161, 1161, 1161, 1161,
       148643  + /*  1810 */   104, 1161,  416,  537,  536, 1104,  418,  951, 1161,  268,
       148644  + /*  1820 */  1161, 1161, 1161, 1161,  366,  366,  365,  253,  363, 1161,
       148645  + /*  1830 */  1161,  798, 1161, 1161, 1161, 1161,  105,  527, 1161,    4,
       148646  + /*  1840 */  1161, 1161, 1161, 1161,  213, 1161,  294, 1161,  951,  951,
       148647  + /*  1850 */   953,  954,   27,  530,  293, 1161, 1161, 1161, 1161, 1161,
       148648  + /*  1860 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148649  + /*  1870 */  1161, 1161, 1161, 1161, 1161, 1161,  416, 1161, 1161, 1161,
       148650  + /*  1880 */  1161, 1161, 1161, 1161,  215, 1161, 1161, 1161,  524, 1161,
       148651  + /*  1890 */  1161, 1161,  152, 1161, 1161,  154,  105,  527, 1161,    4,
       148652  + /*  1900 */  1161, 1161, 1161, 1161, 1161, 1161,  214, 1161, 1161, 1161,
       148653  + /*  1910 */  1161,  961, 1161,  530, 1161, 1161, 1161,  103,  103,  880,
       148654  + /*  1920 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
       148655  + /*  1930 */  1161,  951, 1161, 1161, 1161, 1161,  416, 1161, 1161, 1161,
       148656  + /*  1940 */   385, 1161, 1161, 1161, 1161,  288,  526, 1161,  524, 1161,
       148657  + /*  1950 */  1161, 1161, 1161, 1161, 1161, 1161,   97,  527, 1161,    4,
       148658  + /*  1960 */  1161, 1161,  951,  951,  953,  954,   27, 1161,  423, 1161,
       148659  + /*  1970 */  1161,  961, 1161,  530, 1161, 1161, 1161,  103,  103, 1161,
       148660  + /*  1980 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
       148661  + /*  1990 */  1161,  951,  268, 1161, 1161, 1161,  416,  366,  366,  365,
       148662  + /*  2000 */   253,  363, 1161, 1161,  798, 1161, 1161, 1161,  524, 1161,
       148663  + /*  2010 */  1161, 1161, 1161, 1161, 1161, 1161, 1161,  213, 1161,  294,
       148664  + /*  2020 */  1161, 1161,  951,  951,  953,  954,   27,  293, 1161, 1161,
       148665  + /*  2030 */  1161,  961, 1161, 1161, 1161, 1161, 1161,  103,  103, 1161,
       148666  + /*  2040 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
       148667  + /*  2050 */  1161,  951, 1161, 1161, 1161, 1161, 1161,  215, 1161, 1161,
       148668  + /*  2060 */  1161, 1161, 1161, 1161, 1161,  152, 1161, 1161,  154, 1161,
       148669  + /*  2070 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,  214,
       148670  + /*  2080 */  1161, 1161,  951,  951,  953,  954,   27, 1161, 1161, 1161,
       148671  + /*  2090 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148672  + /*  2100 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148673  + /*  2110 */  1161, 1161, 1161,  385, 1161, 1161, 1161, 1161,  288,  526,
       148674  + /*  2120 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148675  + /*  2130 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148676  + /*  2140 */  1161,  423,
147880 148677   };
147881 148678   static const YYCODETYPE yy_lookahead[] = {
147882         - /*     0 */   168,  163,  184,  238,  239,  240,  163,  163,  155,  156,
147883         - /*    10 */   157,  158,  159,  160,  163,  202,  203,  187,  165,   19,
147884         - /*    20 */   167,  163,  184,  185,  259,  202,  203,  174,  184,  185,
147885         - /*    30 */   174,   31,  238,  239,  240,  184,  185,   22,   23,   39,
147886         - /*    40 */   216,   26,  218,   43,   44,   45,   46,   47,   48,   49,
147887         - /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  174,  206,
147888         - /*    60 */   207,  163,  206,  207,  220,  163,  163,  163,  238,  239,
147889         - /*    70 */   240,   59,  219,  229,  231,  219,  183,  245,  174,  223,
147890         - /*    80 */   224,  249,  184,  185,  191,  232,  184,  185,  184,  185,
147891         - /*    90 */   206,  207,   92,   93,   94,   95,   96,   97,   98,   99,
147892         - /*   100 */   100,  101,  102,  219,  102,   81,   91,  163,   96,   97,
147893         - /*   110 */   206,  207,   19,  275,  276,  262,  104,  105,  106,  107,
147894         - /*   120 */   163,  109,  220,  219,  220,  184,  275,  269,  277,  117,
147895         - /*   130 */   187,  229,   19,  229,  101,  102,   43,   44,   45,   46,
147896         - /*   140 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
147897         - /*   150 */    57,  127,  128,  141,  184,  143,   43,   44,   45,   46,
147898         - /*   160 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
147899         - /*   170 */    57,  268,  269,  275,  276,  197,   83,  233,   85,  163,
147900         - /*   180 */    67,  238,  239,  240,  134,   92,   93,   94,   95,   96,
147901         - /*   190 */    97,   98,   99,  100,  101,  102,   19,   54,   55,   56,
147902         - /*   200 */    57,   58,  152,   26,  247,   92,   93,   94,   95,   96,
147903         - /*   210 */    97,   98,   99,  100,  101,  102,   54,   55,   56,   57,
147904         - /*   220 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
147905         - /*   230 */    53,   54,   55,   56,   57,   92,   93,   94,   95,   96,
147906         - /*   240 */    97,   98,   99,  100,  101,  102,   69,   96,   97,   98,
147907         - /*   250 */    99,  100,  101,  102,   92,   93,   94,   95,   96,   97,
147908         - /*   260 */    98,   99,  100,  101,  102,   73,  179,  180,  181,   92,
147909         - /*   270 */    93,   94,   95,   96,   97,   98,   99,  100,  101,  102,
147910         - /*   280 */   163,  191,  192,  163,   98,   99,  100,  101,  102,   19,
147911         - /*   290 */   200,  179,  180,  181,   24,  175,   92,   93,   94,   95,
147912         - /*   300 */    96,   97,   98,   99,  100,  101,  102,  163,  116,  117,
147913         - /*   310 */   118,   22,  163,   43,   44,   45,   46,   47,   48,   49,
147914         - /*   320 */    50,   51,   52,   53,   54,   55,   56,   57,  157,  158,
147915         - /*   330 */   159,  160,  163,  184,  185,  163,  165,   59,  167,   46,
147916         - /*   340 */    90,   76,   11,  174,   73,  174,   19,  198,   59,   19,
147917         - /*   350 */    72,   86,   81,  184,  185,  234,  106,   96,   97,  163,
147918         - /*   360 */   110,  182,   92,   93,   94,   95,   96,   97,   98,   99,
147919         - /*   370 */   100,  101,  102,   46,  230,  206,  207,  206,  207,  163,
147920         - /*   380 */   184,  185,   19,  105,  106,  107,   23,  116,  219,  220,
147921         - /*   390 */   219,  141,  142,  143,  105,  106,  107,  104,  127,  128,
147922         - /*   400 */   184,  185,  141,  232,  143,   59,   43,   44,   45,   46,
147923         - /*   410 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
147924         - /*   420 */    57,  158,  108,  160,   59,  111,  112,  113,  165,  250,
147925         - /*   430 */   167,  104,  102,  262,  255,  121,  220,  174,  108,  109,
147926         - /*   440 */   110,  111,  112,  113,  114,  229,  182,  120,  117,  118,
147927         - /*   450 */   120,  105,  106,  107,  163,   92,   93,   94,   95,   96,
147928         - /*   460 */    97,   98,   99,  100,  101,  102,  163,   22,   59,  206,
147929         - /*   470 */   207,  106,  163,   26,  171,   19,  163,  193,  175,   23,
147930         - /*   480 */   163,   22,  219,  206,  207,  139,  163,   22,   59,  182,
147931         - /*   490 */   117,  118,   59,  184,  185,  232,  219,  184,  185,   43,
147932         - /*   500 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
147933         - /*   510 */    54,   55,   56,   57,  105,  106,  107,  108,   59,  255,
147934         - /*   520 */   111,  112,  113,   90,   59,  262,   22,   98,  174,   12,
147935         - /*   530 */   121,  208,  163,  220,  105,  106,  107,  163,  105,  106,
147936         - /*   540 */    22,   96,   97,  110,   27,  238,  239,  240,   92,   93,
147937         - /*   550 */    94,   95,   96,   97,   98,   99,  100,  101,  102,   42,
147938         - /*   560 */   206,  207,  115,   59,  105,  106,  107,  163,   19,   59,
147939         - /*   570 */   163,  106,   23,  219,  141,  142,  143,   59,  163,  205,
147940         - /*   580 */    63,   59,   72,   22,  124,   59,  163,  270,  234,  129,
147941         - /*   590 */    73,  163,   43,   44,   45,   46,   47,   48,   49,   50,
147942         - /*   600 */    51,   52,   53,   54,   55,   56,   57,  184,  185,  105,
147943         - /*   610 */   106,  107,  184,  185,  163,  105,  106,  107,  265,  266,
147944         - /*   620 */    59,  198,  225,  105,  106,  107,  198,  105,  106,  107,
147945         - /*   630 */   163,  105,  106,  107,  163,  184,  185,   46,   47,   48,
147946         - /*   640 */    49,   92,   93,   94,   95,   96,   97,   98,   99,  100,
147947         - /*   650 */   101,  102,  163,  163,  132,  184,  185,  163,  132,  163,
147948         - /*   660 */   256,   19,  163,  163,  163,   23,  105,  106,  107,  198,
147949         - /*   670 */   163,  220,  205,  184,  185,  163,   35,   81,  184,  185,
147950         - /*   680 */   184,  185,  163,  184,  185,   43,   44,   45,   46,   47,
147951         - /*   690 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
147952         - /*   700 */   163,  110,  163,  184,  185,  109,  205,   66,  163,   59,
147953         - /*   710 */   163,   21,  205,   16,  174,   74,  220,  198,  163,  220,
147954         - /*   720 */   230,  184,  185,  127,  128,  180,  181,  180,  181,  163,
147955         - /*   730 */   175,  242,  174,  233,   92,   93,   94,   95,   96,   97,
147956         - /*   740 */    98,   99,  100,  101,  102,  233,  206,  207,   26,  163,
147957         - /*   750 */   195,  207,  197,   26,   19,  105,  106,  107,   23,  219,
147958         - /*   760 */   119,  260,   26,  219,  206,  207,  174,   19,  174,  230,
147959         - /*   770 */    80,  174,  163,  174,   77,  163,   79,  219,   43,   44,
147960         - /*   780 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
147961         - /*   790 */    55,   56,   57,  248,   12,  248,  184,  185,  206,  207,
147962         - /*   800 */   206,  207,  112,  206,  207,  206,  207,  206,  207,   27,
147963         - /*   810 */   163,  219,  123,  219,  125,  126,  219,  208,  219,  163,
147964         - /*   820 */   219,   22,   23,   23,   42,   26,   26,   92,   93,   94,
147965         - /*   830 */    95,   96,   97,   98,   99,  100,  101,  102,  163,  149,
147966         - /*   840 */   184,  185,  163,  107,  163,   63,  149,   19,  163,  127,
147967         - /*   850 */   128,   23,   22,  105,   24,  116,  117,  118,  131,  184,
147968         - /*   860 */   185,  163,  163,  184,  185,  184,  185,   19,  132,  184,
147969         - /*   870 */   185,   43,   44,   45,   46,   47,   48,   49,   50,   51,
147970         - /*   880 */    52,   53,   54,   55,   56,   57,  146,  163,  148,   59,
147971         - /*   890 */    91,   43,   44,   45,   46,   47,   48,   49,   50,   51,
147972         - /*   900 */    52,   53,   54,   55,   56,   57,  208,  107,  184,  185,
147973         - /*   910 */     7,    8,    9,  116,  117,  118,  163,  163,  163,   24,
147974         - /*   920 */    92,   93,   94,   95,   96,   97,   98,   99,  100,  101,
147975         - /*   930 */   102,   29,  132,  163,  163,   33,  106,  184,  185,  163,
147976         - /*   940 */    92,   93,   94,   95,   96,   97,   98,   99,  100,  101,
147977         - /*   950 */   102,  163,  163,  163,   59,  184,  185,  163,   22,  163,
147978         - /*   960 */   184,  185,  177,  178,  163,  163,  163,   65,  163,  199,
147979         - /*   970 */   163,   26,  184,  185,  184,  185,  163,  163,  184,  185,
147980         - /*   980 */   184,  185,  163,   98,  163,  184,  185,  184,  185,  184,
147981         - /*   990 */   185,  184,  185,  252,  205,  147,  163,   61,  184,  185,
147982         - /*  1000 */   163,  106,  163,  184,  185,  163,  163,  205,  163,  124,
147983         - /*  1010 */   163,  256,  163,  163,  129,   19,  163,  184,  185,  163,
147984         - /*  1020 */   199,  184,  185,  184,  185,   23,  184,  185,   26,  184,
147985         - /*  1030 */   185,  184,  185,  184,  185,   19,  163,  184,  185,   43,
147986         - /*  1040 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
147987         - /*  1050 */    54,   55,   56,   57,  163,  163,  163,  184,  185,   43,
147988         - /*  1060 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
147989         - /*  1070 */    54,   55,   56,   57,   16,  184,  185,  184,  185,  163,
147990         - /*  1080 */   163,   22,   23,  138,  163,   19,  163,  231,   92,   93,
147991         - /*  1090 */    94,   95,   96,   97,   98,   99,  100,  101,  102,  256,
147992         - /*  1100 */   163,  184,  185,  163,  163,  184,  185,  163,   92,   93,
147993         - /*  1110 */    94,   95,   96,   97,   98,   99,  100,  101,  102,  163,
147994         - /*  1120 */    59,  184,  185,  163,  184,  185,  177,  178,  184,  185,
147995         - /*  1130 */   163,  208,  163,  237,  163,   77,  163,   79,  163,   15,
147996         - /*  1140 */   184,  185,  237,  147,  184,  185,   24,  231,  153,  154,
147997         - /*  1150 */    91,  184,  185,  184,  185,  184,  185,  184,  185,  184,
147998         - /*  1160 */   185,   22,   23,   19,  163,  127,  128,  106,   24,  273,
147999         - /*  1170 */   271,  105,  231,  274,  263,  264,  223,  224,  273,   22,
148000         - /*  1180 */   118,   24,   23,   19,   60,   26,  163,   43,   44,   45,
148001         - /*  1190 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
148002         - /*  1200 */    56,   57,  140,   23,   22,  163,   26,   43,   44,   45,
148003         - /*  1210 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
148004         - /*  1220 */    56,   57,   23,  211,   23,   26,   31,   26,   23,   22,
148005         - /*  1230 */    91,   26,  231,  221,   39,   53,   92,   93,   94,   95,
148006         - /*  1240 */    96,   97,   98,   99,  100,  101,  102,   23,   23,  163,
148007         - /*  1250 */    26,   26,  130,   59,  109,  110,   92,   93,   94,   95,
148008         - /*  1260 */    96,   97,   98,   99,  100,  101,  102,   23,    7,    8,
148009         - /*  1270 */    26,  110,   23,   59,   23,   26,   19,   26,  141,   23,
148010         - /*  1280 */   143,  120,   26,  141,  163,  143,   23,   23,  163,   26,
148011         - /*  1290 */    26,  163,  163,  163,  163,  163,   19,  163,  163,  193,
148012         - /*  1300 */   106,   44,   45,   46,   47,   48,   49,   50,   51,   52,
148013         - /*  1310 */    53,   54,   55,   56,   57,  163,  193,  163,  163,  163,
148014         - /*  1320 */   106,  163,   45,   46,   47,   48,   49,   50,   51,   52,
148015         - /*  1330 */    53,   54,   55,   56,   57,  163,  163,  130,  222,  163,
148016         - /*  1340 */   163,  203,  163,   19,   20,  251,   22,  163,  163,   92,
148017         - /*  1350 */    93,   94,   95,   96,   97,   98,   99,  100,  101,  102,
148018         - /*  1360 */    36,  163,  209,  163,  261,  163,  163,  161,  222,   92,
148019         - /*  1370 */    93,   94,   95,   96,   97,   98,   99,  100,  101,  102,
148020         - /*  1380 */   210,  213,  222,   59,  222,  222,  182,  213,  213,  196,
148021         - /*  1390 */   257,  226,  226,   19,   20,   71,   22,  257,  188,  187,
148022         - /*  1400 */   192,  212,  187,  187,  226,   81,  210,  166,   60,  261,
148023         - /*  1410 */    36,  244,  130,  170,   90,  170,   38,  170,  139,  104,
148024         - /*  1420 */    96,   97,   48,  236,   22,  235,   43,  103,  201,  105,
148025         - /*  1430 */   106,  107,  138,   59,  110,  247,  213,   18,  204,  258,
148026         - /*  1440 */   204,  258,  204,  204,  170,   71,   18,  169,  213,  236,
148027         - /*  1450 */   213,  127,  128,  235,  201,  201,   82,  170,  169,  213,
148028         - /*  1460 */   146,   87,   62,  254,   90,  141,  142,  143,  144,  145,
148029         - /*  1470 */    96,   97,  253,  170,  169,   22,  170,  103,  169,  105,
148030         - /*  1480 */   106,  107,  189,  170,  110,  169,  189,  186,   19,   20,
148031         - /*  1490 */   104,   22,  194,  186,  186,   64,  115,  186,  194,  189,
148032         - /*  1500 */   188,  102,  133,  186,  186,   36,  186,  104,  189,   19,
148033         - /*  1510 */    20,  246,   22,  246,  189,  141,  142,  143,  144,  145,
148034         - /*  1520 */     0,    1,    2,  228,  228,    5,   36,  227,   59,  227,
148035         - /*  1530 */    10,   11,   12,   13,   14,  170,   84,   17,  134,  216,
148036         - /*  1540 */    71,  272,  270,   22,  137,  217,   22,  216,  227,   59,
148037         - /*  1550 */    30,   82,   32,  217,  228,  228,   87,  227,  170,   90,
148038         - /*  1560 */    40,   71,  146,  241,  215,   96,   97,  214,  136,  135,
148039         - /*  1570 */   213,   25,  103,   26,  105,  106,  107,  243,  173,  110,
148040         - /*  1580 */    90,  172,   13,    6,  164,  164,   96,   97,   98,  162,
148041         - /*  1590 */    70,  162,  162,  103,  176,  105,  106,  107,   78,  267,
148042         - /*  1600 */   110,   81,  267,  264,  182,  182,  182,  182,   88,  176,
148043         - /*  1610 */   141,  142,  143,  144,  145,  176,  190,    4,  182,  182,
148044         - /*  1620 */   182,   19,   20,  182,   22,  190,    3,   22,  151,   15,
148045         - /*  1630 */    89,  141,  142,  143,  144,  145,   16,  128,   36,   23,
148046         - /*  1640 */    23,  139,  122,   24,  119,  131,   20,  127,  128,  133,
148047         - /*  1650 */    16,    1,  140,  131,  119,   61,  139,   53,   37,   53,
148048         - /*  1660 */    53,   59,   53,  119,  105,   34,  130,    1,    5,   22,
148049         - /*  1670 */   150,  104,  149,   71,   26,   75,   68,   41,   68,  130,
148050         - /*  1680 */   104,   24,   20,   19,   82,  120,  114,   22,   28,   87,
148051         - /*  1690 */    22,   67,   90,   22,   67,   23,   22,   22,   96,   97,
148052         - /*  1700 */    67,   23,  138,   22,   37,  103,  153,  105,  106,  107,
148053         - /*  1710 */     1,    2,  110,   23,    5,   23,   23,   26,   22,   10,
148054         - /*  1720 */    11,   12,   13,   14,   24,   23,   17,   22,   24,  130,
148055         - /*  1730 */    23,   19,   20,   23,   22,  105,   22,   34,   85,   30,
148056         - /*  1740 */    34,   32,   26,  141,  142,  143,  144,  145,   36,   40,
148057         - /*  1750 */   132,   34,   75,   83,   23,   44,   24,   34,   23,   26,
148058         - /*  1760 */    26,   19,   20,   23,   22,   26,   23,   23,   23,   23,
148059         - /*  1770 */    22,   59,   11,   22,   22,   26,   23,   23,   36,   70,
148060         - /*  1780 */    22,   22,  124,   71,  130,  130,  130,   78,   23,  130,
148061         - /*  1790 */    81,   15,    1,  278,  278,  278,  278,   88,  278,  278,
148062         - /*  1800 */   278,   59,   90,  278,  278,  278,  278,  278,   96,   97,
148063         - /*  1810 */   278,  278,  278,   71,  278,  103,  278,  105,  106,  107,
148064         - /*  1820 */   278,  278,  110,  278,  278,  278,  278,  278,  278,  278,
148065         - /*  1830 */   278,  122,   90,  278,  278,  278,  127,  128,   96,   97,
148066         - /*  1840 */   278,  278,  278,  278,  278,  103,  278,  105,  106,  107,
148067         - /*  1850 */   278,  278,  110,  141,  142,  143,  144,  145,  278,  150,
148068         - /*  1860 */   278,  278,  278,    5,  278,  278,  278,  278,   10,   11,
148069         - /*  1870 */    12,   13,   14,  278,  278,   17,  278,  278,  278,  278,
148070         - /*  1880 */   278,  278,  278,  141,  142,  143,  144,  145,   30,  278,
148071         - /*  1890 */    32,  278,  278,  278,  278,  278,  278,  278,   40,  278,
148072         - /*  1900 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148073         - /*  1910 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148074         - /*  1920 */   278,  278,  278,  278,  278,  278,  278,  278,   70,  278,
148075         - /*  1930 */   278,  278,  278,  278,  278,  278,   78,  278,  278,   81,
148076         - /*  1940 */   278,  278,  278,  278,  278,  278,   88,  278,  278,  278,
148077         - /*  1950 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148078         - /*  1960 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148079         - /*  1970 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148080         - /*  1980 */   122,  278,  278,  278,  278,  127,  128,  278,  278,  278,
148081         - /*  1990 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148082         - /*  2000 */   278,  278,  278,  278,  278,  278,  278,  278,  150,  278,
148083         - /*  2010 */   278,  278,  278,  278,  278,  278,  278,  278,  278,
148084         -};
148085         -#define YY_SHIFT_COUNT    (523)
       148679  + /*     0 */   184,  184,  259,  260,  261,  259,  260,  261,  176,  177,
       148680  + /*    10 */   178,  179,  180,  181,  184,  208,  212,  213,  186,   19,
       148681  + /*    20 */   188,  205,  206,  280,  205,  221,   22,  195,   24,  195,
       148682  + /*    30 */   208,   31,  195,  205,   29,  205,  206,  255,   33,   39,
       148683  + /*    40 */   200,  201,  202,   43,   44,   45,   46,   47,   48,   49,
       148684  + /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  205,  227,
       148685  + /*    60 */   228,  227,  228,   59,  227,  228,  259,  260,  261,  252,
       148686  + /*    70 */    65,  241,  240,  184,  240,  223,  224,  240,  244,  245,
       148687  + /*    80 */   250,  259,  260,  261,   19,  253,   54,   55,   56,   57,
       148688  + /*    90 */    58,  184,  255,  184,  205,  206,   96,   97,   98,   99,
       148689  + /*   100 */   100,  101,  102,  103,  104,  105,  106,   46,   47,   48,
       148690  + /*   110 */    49,   46,  296,  297,  110,  283,   19,   96,   97,   98,
       148691  + /*   120 */    99,  100,  101,  102,  103,  104,  105,  106,   96,   97,
       148692  + /*   130 */    98,   99,  100,  101,  102,  103,  104,  105,  106,   94,
       148693  + /*   140 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
       148694  + /*   150 */    53,   54,   55,   56,   57,  110,  106,   73,  251,  114,
       148695  + /*   160 */    73,  178,  179,  180,  181,   59,  184,  292,   81,  186,
       148696  + /*   170 */   295,  188,  218,  108,   19,  114,  184,   11,  195,  184,
       148697  + /*   180 */    83,  184,   85,   54,   55,   56,   57,  205,  206,  124,
       148698  + /*   190 */   145,  146,  147,   96,   97,   98,   99,  100,  101,  102,
       148699  + /*   200 */   103,  104,  105,  106,  120,  121,  122,  120,  102,   81,
       148700  + /*   210 */   227,  228,  220,   19,   16,  109,  110,  111,  131,  132,
       148701  + /*   220 */    26,  184,  184,  240,  229,   96,   97,   98,   99,  100,
       148702  + /*   230 */   101,  102,  103,  104,  105,  106,  253,   43,   44,   45,
       148703  + /*   240 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148704  + /*   250 */    56,   57,  100,  101,  102,  103,  104,  105,  106,  131,
       148705  + /*   260 */   132,  106,  127,   69,  129,  130,  283,  112,  113,  114,
       148706  + /*   270 */   115,  116,  117,  118,   81,   77,   76,   79,  296,  124,
       148707  + /*   280 */   298,  203,  184,   19,   84,   59,   86,  121,  122,   89,
       148708  + /*   290 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148709  + /*   300 */   106,  184,   35,  205,  206,   22,  113,   43,   44,   45,
       148710  + /*   310 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148711  + /*   320 */    56,   57,  205,  206,  131,  132,  100,  101,  291,  292,
       148712  + /*   330 */   114,   67,  295,   66,  108,  109,  110,  111,  138,  113,
       148713  + /*   340 */   124,   74,   59,  179,  184,  181,  184,  121,   22,  271,
       148714  + /*   350 */   186,   19,  188,  184,  276,   59,   24,  184,  241,  195,
       148715  + /*   360 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148716  + /*   370 */   106,  145,   59,  147,  184,   43,   44,   45,   46,   47,
       148717  + /*   380 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
       148718  + /*   390 */   123,  227,  228,  110,  296,  297,   22,   23,  184,  102,
       148719  + /*   400 */   103,  104,  105,  106,  240,  109,  110,  111,  112,  195,
       148720  + /*   410 */   204,  115,  116,  117,   22,  184,  226,  253,  212,  205,
       148721  + /*   420 */   206,  125,  109,  110,  111,   22,  100,  101,   96,   97,
       148722  + /*   430 */    98,   99,  100,  101,  102,  103,  104,  105,  106,  184,
       148723  + /*   440 */    59,  227,  228,  121,  122,   59,  277,  283,   19,  289,
       148724  + /*   450 */   290,   59,   23,   76,  240,  241,  143,   76,   72,  189,
       148725  + /*   460 */   205,  206,   59,   86,  250,   84,   89,   86,  203,   95,
       148726  + /*   470 */    89,  281,   43,   44,   45,   46,   47,   48,   49,   50,
       148727  + /*   480 */    51,   52,   53,   54,   55,   56,   57,  227,  228,  184,
       148728  + /*   490 */   109,  110,  111,   12,  184,  109,  110,  111,  184,  184,
       148729  + /*   500 */   240,  109,  110,  111,  184,  195,  214,   59,   27,  184,
       148730  + /*   510 */   205,  206,  109,  110,  111,  205,  206,  184,  263,  138,
       148731  + /*   520 */   205,  206,  184,   42,   22,   96,   97,   98,   99,  100,
       148732  + /*   530 */   101,  102,  103,  104,  105,  106,  266,  227,  228,   59,
       148733  + /*   540 */   270,  276,   94,   66,   63,   19,  241,   22,   26,   23,
       148734  + /*   550 */   240,  241,   72,   59,   73,  250,  241,  109,  110,   82,
       148735  + /*   560 */    22,   59,  114,  223,  224,  250,  252,   59,   91,   43,
       148736  + /*   570 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       148737  + /*   580 */    54,   55,   56,   57,   59,  184,   26,   59,  268,  109,
       148738  + /*   590 */   110,  111,  184,  145,  146,  147,  112,   59,  203,  115,
       148739  + /*   600 */   116,  117,  277,  109,  110,  111,  205,  206,  195,  125,
       148740  + /*   610 */   277,  109,  110,  111,  100,  101,  139,  109,  110,  111,
       148741  + /*   620 */   219,  184,   96,   97,   98,   99,  100,  101,  102,  103,
       148742  + /*   630 */   104,  105,  106,  111,  109,  110,  111,  109,  110,  111,
       148743  + /*   640 */   227,  228,   19,  184,  136,  184,   23,  109,  110,  111,
       148744  + /*   650 */   200,  201,  202,  240,  259,  260,  261,  195,  136,  145,
       148745  + /*   660 */   184,  147,  184,  184,  136,  214,   43,   44,   45,   46,
       148746  + /*   670 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
       148747  + /*   680 */    57,  205,  206,  205,  206,  227,  228,  184,  229,  227,
       148748  + /*   690 */   228,  131,  132,  184,   59,  219,  184,  219,  240,  291,
       148749  + /*   700 */   292,  184,  240,  295,  105,  106,   22,   23,  205,  206,
       148750  + /*   710 */    26,  184,  251,  184,  205,  206,  184,  205,  206,   96,
       148751  + /*   720 */    97,   98,   99,  100,  101,  102,  103,  104,  105,  106,
       148752  + /*   730 */   251,  219,  205,  206,  205,  206,  184,  205,  206,   19,
       148753  + /*   740 */   184,   16,  184,   23,  241,  110,  219,   21,  219,  184,
       148754  + /*   750 */   241,  219,  286,  287,  195,  184,  195,  205,  206,  201,
       148755  + /*   760 */   202,  205,  206,   43,   44,   45,   46,   47,   48,   49,
       148756  + /*   770 */    50,   51,   52,   53,   54,   55,   56,   57,  184,   95,
       148757  + /*   780 */    22,   23,  184,   26,   26,  220,  227,  228,  227,  228,
       148758  + /*   790 */   196,  184,   23,  241,   26,   26,  195,  241,  184,  240,
       148759  + /*   800 */    12,  240,   77,   26,   79,  195,   80,  290,  201,  202,
       148760  + /*   810 */   216,  184,  218,  195,  184,   27,   96,   97,   98,   99,
       148761  + /*   820 */   100,  101,  102,  103,  104,  105,  106,  269,  227,  228,
       148762  + /*   830 */    42,  184,  205,  206,  184,  184,   19,  227,  228,  192,
       148763  + /*   840 */    23,  240,  116,  196,   76,  227,  228,  120,  121,  122,
       148764  + /*   850 */   240,   63,  254,   95,   86,  205,  206,   89,  240,  184,
       148765  + /*   860 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
       148766  + /*   870 */    53,   54,   55,   56,   57,  184,  269,  184,  153,  153,
       148767  + /*   880 */   111,  184,    7,    8,    9,  184,  138,  184,  184,  196,
       148768  + /*   890 */   184,  120,  121,  122,  184,  138,  205,  206,  184,  102,
       148769  + /*   900 */   184,  184,  205,  206,  156,  136,  205,  206,  205,  206,
       148770  + /*   910 */   198,  199,  135,   96,   97,   98,   99,  100,  101,  102,
       148771  + /*   920 */   103,  104,  105,  106,  184,  128,  184,  184,  184,  254,
       148772  + /*   930 */   133,  184,  237,   19,  239,  229,  226,   23,  292,  184,
       148773  + /*   940 */   226,  295,  226,  226,  184,  205,  206,  205,  206,  205,
       148774  + /*   950 */   206,  184,  292,   19,  184,  295,  252,   43,   44,   45,
       148775  + /*   960 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148776  + /*   970 */    56,   57,  205,  206,  184,  205,  206,   43,   44,   45,
       148777  + /*   980 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148778  + /*   990 */    56,   57,  157,  158,   26,  205,  206,  254,   26,  252,
       148779  + /*  1000 */   184,   15,  184,  184,  184,  292,  184,  252,  295,   24,
       148780  + /*  1010 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148781  + /*  1020 */   106,  205,  206,  205,  206,  205,  206,  205,  206,  184,
       148782  + /*  1030 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148783  + /*  1040 */   106,  184,  184,  184,   59,  184,   60,  184,  229,  184,
       148784  + /*  1050 */   205,  206,  184,  258,  184,   19,  184,   19,  184,  246,
       148785  + /*  1060 */   184,  258,  205,  206,  205,  206,  205,  206,  205,  206,
       148786  + /*  1070 */   205,  206,  184,  205,  206,  205,  206,  205,  206,  205,
       148787  + /*  1080 */   206,  205,  206,  292,  226,  151,  295,  184,  228,  294,
       148788  + /*  1090 */   184,  119,  184,  205,  206,  110,  150,  294,  152,  184,
       148789  + /*  1100 */   240,  184,   22,   23,   23,   19,  184,   26,  205,  206,
       148790  + /*  1110 */   142,  205,  206,  205,  206,  184,  198,  199,  131,  132,
       148791  + /*  1120 */   205,  206,  205,  206,   22,   19,   24,  205,  206,   43,
       148792  + /*  1130 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       148793  + /*  1140 */    54,   55,   56,   57,  184,  109,  184,  109,  184,   43,
       148794  + /*  1150 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       148795  + /*  1160 */    54,   55,   56,   57,   46,  205,  206,  205,  206,  205,
       148796  + /*  1170 */   206,  232,  184,  184,  184,   95,  184,  284,  285,  244,
       148797  + /*  1180 */   245,  242,   96,   97,   98,   99,  100,  101,  102,  103,
       148798  + /*  1190 */   104,  105,  106,  205,  206,  205,  206,  205,  206,  184,
       148799  + /*  1200 */    22,  184,   96,   97,   98,   99,  100,  101,  102,  103,
       148800  + /*  1210 */   104,  105,  106,  184,   24,   23,  184,  184,   26,  184,
       148801  + /*  1220 */   205,  206,  205,  206,  184,   31,  108,  128,   22,  122,
       148802  + /*  1230 */   184,   53,  133,   39,  205,  206,   22,  151,  205,  206,
       148803  + /*  1240 */   205,  206,  113,  114,   23,  205,  206,   26,   59,   23,
       148804  + /*  1250 */    23,  144,   26,   26,  184,   23,   23,   19,   26,   26,
       148805  + /*  1260 */     7,    8,   24,   23,  214,   23,   26,   61,   26,   59,
       148806  + /*  1270 */    23,   23,   23,   26,   26,   26,  145,   19,  147,   59,
       148807  + /*  1280 */   184,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       148808  + /*  1290 */    52,   53,   54,   55,   56,   57,  145,   23,  147,  110,
       148809  + /*  1300 */    26,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       148810  + /*  1310 */    52,   53,   54,   55,   56,   57,   23,  184,  184,   26,
       148811  + /*  1320 */   110,  184,  184,  184,  134,  184,  184,  184,  184,  184,
       148812  + /*  1330 */   110,  184,  184,  184,   96,   97,   98,   99,  100,  101,
       148813  + /*  1340 */   102,  103,  104,  105,  106,  184,  184,  184,  134,  300,
       148814  + /*  1350 */   184,  243,  184,  184,   96,   97,   98,   99,  100,  101,
       148815  + /*  1360 */   102,  103,  104,  105,  106,  184,  184,  184,  184,  184,
       148816  + /*  1370 */   224,  184,  282,  273,   19,  272,  203,  182,  243,  243,
       148817  + /*  1380 */   230,  209,  278,  243,  231,  208,  265,  278,  234,  234,
       148818  + /*  1390 */   234,  217,  213,   60,   19,  243,  208,  237,  233,   44,
       148819  + /*  1400 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
       148820  + /*  1410 */    55,   56,   57,  208,  247,  187,  134,  247,  247,   38,
       148821  + /*  1420 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
       148822  + /*  1430 */    55,   56,   57,  237,  231,  191,  191,  279,  279,  282,
       148823  + /*  1440 */   143,  191,  108,  268,   22,   19,   20,  256,   22,   43,
       148824  + /*  1450 */   257,   96,   97,   98,   99,  100,  101,  102,  103,  104,
       148825  + /*  1460 */   105,  106,   36,  222,  142,  234,   18,  191,  225,   18,
       148826  + /*  1470 */   190,   96,   97,   98,   99,  100,  101,  102,  103,  104,
       148827  + /*  1480 */   105,  106,  225,  191,  225,   59,  225,  257,  234,  234,
       148828  + /*  1490 */   256,  222,  222,  190,  234,  150,   62,   71,  275,  274,
       148829  + /*  1500 */   191,   19,   20,  190,   22,   22,  210,   81,  191,  190,
       148830  + /*  1510 */   210,  191,  108,  190,  207,  207,   64,  207,   36,  215,
       148831  + /*  1520 */    94,  210,  207,  209,  119,  207,  100,  101,  207,  106,
       148832  + /*  1530 */    48,  215,  207,  107,  210,  109,  110,  111,  267,  267,
       148833  + /*  1540 */   114,   59,  210,  249,  137,  108,  248,  191,  249,  248,
       148834  + /*  1550 */    88,  249,  141,   71,  248,  299,  138,  131,  132,   22,
       148835  + /*  1560 */   191,  249,  299,  237,   82,  238,  150,  262,  140,   87,
       148836  + /*  1570 */   139,  145,  146,  147,  148,  149,   94,  248,  238,  236,
       148837  + /*  1580 */    25,  235,  100,  101,  234,  194,   26,  193,   13,  107,
       148838  + /*  1590 */     6,  109,  110,  111,  264,  185,  114,  185,  183,  197,
       148839  + /*  1600 */   183,  203,  183,  203,  203,  197,  203,  211,  211,    4,
       148840  + /*  1610 */   197,    3,  203,   22,  155,   15,  288,  203,   93,  288,
       148841  + /*  1620 */   285,   23,   16,  203,  203,   23,  132,  145,  146,  147,
       148842  + /*  1630 */   148,  149,    0,    1,    2,  143,  123,    5,   24,  135,
       148843  + /*  1640 */    20,   16,   10,   11,   12,   13,   14,  137,    1,   17,
       148844  + /*  1650 */   135,  144,   19,   20,  123,   22,   61,  143,   37,  123,
       148845  + /*  1660 */    53,  109,   30,   53,   32,   53,   53,  134,   34,   36,
       148846  + /*  1670 */     1,    5,   40,   22,  108,  153,   26,   68,   75,   68,
       148847  + /*  1680 */    41,  134,  108,   24,   20,  124,   19,  118,   23,   67,
       148848  + /*  1690 */    22,   67,   59,   22,   22,   22,   22,   67,   28,   37,
       148849  + /*  1700 */    23,  142,   70,   22,   71,   23,  157,   23,   23,   26,
       148850  + /*  1710 */    78,   23,   22,   81,   23,   82,   24,   22,   24,  134,
       148851  + /*  1720 */    87,   23,   19,   20,   92,   22,  109,   94,   23,   22,
       148852  + /*  1730 */    34,   34,  136,  100,  101,   26,   34,   85,   34,   36,
       148853  + /*  1740 */   107,   83,  109,  110,  111,   34,   90,  114,   34,   23,
       148854  + /*  1750 */    75,   75,   44,   22,   24,   26,   34,   23,  126,   26,
       148855  + /*  1760 */    23,   23,   59,  131,  132,   23,   23,   26,   23,   22,
       148856  + /*  1770 */    11,   22,   22,   22,   71,   23,   23,   22,  145,  146,
       148857  + /*  1780 */   147,  148,  149,   26,   23,   82,  154,  134,  128,  134,
       148858  + /*  1790 */    87,  134,   15,    1,  301,  134,  301,   94,  301,  301,
       148859  + /*  1800 */   301,  301,  301,  100,  101,  301,  301,  301,  301,  301,
       148860  + /*  1810 */   107,  301,  109,  110,  111,    1,    2,  114,  301,    5,
       148861  + /*  1820 */   301,  301,  301,  301,   10,   11,   12,   13,   14,  301,
       148862  + /*  1830 */   301,   17,  301,  301,  301,  301,   19,   20,  301,   22,
       148863  + /*  1840 */   301,  301,  301,  301,   30,  301,   32,  301,  145,  146,
       148864  + /*  1850 */   147,  148,  149,   36,   40,  301,  301,  301,  301,  301,
       148865  + /*  1860 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148866  + /*  1870 */   301,  301,  301,  301,  301,  301,   59,  301,  301,  301,
       148867  + /*  1880 */   301,  301,  301,  301,   70,  301,  301,  301,   71,  301,
       148868  + /*  1890 */   301,  301,   78,  301,  301,   81,   19,   20,  301,   22,
       148869  + /*  1900 */   301,  301,  301,  301,  301,  301,   92,  301,  301,  301,
       148870  + /*  1910 */   301,   94,  301,   36,  301,  301,  301,  100,  101,  102,
       148871  + /*  1920 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
       148872  + /*  1930 */   301,  114,  301,  301,  301,  301,   59,  301,  301,  301,
       148873  + /*  1940 */   126,  301,  301,  301,  301,  131,  132,  301,   71,  301,
       148874  + /*  1950 */   301,  301,  301,  301,  301,  301,   19,   20,  301,   22,
       148875  + /*  1960 */   301,  301,  145,  146,  147,  148,  149,  301,  154,  301,
       148876  + /*  1970 */   301,   94,  301,   36,  301,  301,  301,  100,  101,  301,
       148877  + /*  1980 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
       148878  + /*  1990 */   301,  114,    5,  301,  301,  301,   59,   10,   11,   12,
       148879  + /*  2000 */    13,   14,  301,  301,   17,  301,  301,  301,   71,  301,
       148880  + /*  2010 */   301,  301,  301,  301,  301,  301,  301,   30,  301,   32,
       148881  + /*  2020 */   301,  301,  145,  146,  147,  148,  149,   40,  301,  301,
       148882  + /*  2030 */   301,   94,  301,  301,  301,  301,  301,  100,  101,  301,
       148883  + /*  2040 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
       148884  + /*  2050 */   301,  114,  301,  301,  301,  301,  301,   70,  301,  301,
       148885  + /*  2060 */   301,  301,  301,  301,  301,   78,  301,  301,   81,  301,
       148886  + /*  2070 */   301,  301,  301,  301,  301,  301,  301,  301,  301,   92,
       148887  + /*  2080 */   301,  301,  145,  146,  147,  148,  149,  301,  301,  301,
       148888  + /*  2090 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148889  + /*  2100 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148890  + /*  2110 */   301,  301,  301,  126,  301,  301,  301,  301,  131,  132,
       148891  + /*  2120 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148892  + /*  2130 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148893  + /*  2140 */   301,  154,  301,  301,  301,  301,  301,  301,  301,  301,
       148894  + /*  2150 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148895  + /*  2160 */   301,  301,  301,  301,  301,  301,  301,  301,  301,
       148896  +};
       148897  +#define YY_SHIFT_COUNT    (540)
148086 148898   #define YY_SHIFT_MIN      (0)
148087         -#define YY_SHIFT_MAX      (1858)
       148899  +#define YY_SHIFT_MAX      (1987)
148088 148900   static const unsigned short int yy_shift_ofst[] = {
148089         - /*     0 */  1709, 1520, 1858, 1324, 1324,   24, 1374, 1469, 1602, 1712,
148090         - /*    10 */  1712, 1712,  271,    0,    0,  113, 1016, 1712, 1712, 1712,
148091         - /*    20 */  1712, 1712, 1712, 1712, 1712, 1712, 1712,   12,   12,  409,
148092         - /*    30 */   596,   24,   24,   24,   24,   24,   24,   93,  177,  270,
148093         - /*    40 */   363,  456,  549,  642,  735,  828,  848,  996, 1144, 1016,
148094         - /*    50 */  1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016,
148095         - /*    60 */  1016, 1016, 1016, 1016, 1016, 1016, 1016, 1164, 1016, 1257,
148096         - /*    70 */  1277, 1277, 1490, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
148097         - /*    80 */  1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
148098         - /*    90 */  1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
148099         - /*   100 */  1712, 1712, 1712, 1712, 1712, 1742, 1712, 1712, 1712, 1712,
148100         - /*   110 */  1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,  143,
148101         - /*   120 */   162,  162,  162,  162,  162,  204,  151,  186,  650,  690,
148102         - /*   130 */   327,  650,  261,  261,  650,  722,  722,  722,  722,  373,
148103         - /*   140 */    33,    2, 2009, 2009,  330,  330,  330,  346,  289,  278,
148104         - /*   150 */   289,  289,  517,  517,  459,  510,   15,  799,  650,  650,
148105         - /*   160 */   650,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148106         - /*   170 */   650,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148107         - /*   180 */   331,  365,  995,  995,  265,  365,   50, 1038, 2009, 2009,
148108         - /*   190 */  2009,  433,  250,  250,  504,  314,  429,  518,  522,  526,
148109         - /*   200 */   561,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148110         - /*   210 */   192,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148111         - /*   220 */   650,  650,  650,  641,  641,  641,  650,  650,  650,  650,
148112         - /*   230 */   800,  650,  650,  650,  830,  650,  650,  782,  650,  650,
148113         - /*   240 */   650,  650,  650,  650,  650,  650,  739,  902,  689,  895,
148114         - /*   250 */   895,  895,  895,  736,  689,  689,  885,  445,  903, 1124,
148115         - /*   260 */   945,  748,  748, 1066,  945,  945, 1066,  447, 1002,  293,
148116         - /*   270 */  1195, 1195, 1195,  748,  740,  727,  460, 1157, 1348, 1282,
148117         - /*   280 */  1282, 1378, 1378, 1282, 1279, 1315, 1402, 1383, 1294, 1419,
148118         - /*   290 */  1419, 1419, 1419, 1282, 1428, 1294, 1294, 1315, 1402, 1383,
148119         - /*   300 */  1383, 1294, 1282, 1428, 1314, 1400, 1282, 1428, 1453, 1282,
148120         - /*   310 */  1428, 1282, 1428, 1453, 1386, 1386, 1386, 1431, 1453, 1386,
148121         - /*   320 */  1381, 1386, 1431, 1386, 1386, 1453, 1399, 1399, 1453, 1369,
148122         - /*   330 */  1403, 1369, 1403, 1369, 1403, 1369, 1403, 1282, 1404, 1452,
148123         - /*   340 */  1521, 1407, 1404, 1524, 1282, 1416, 1407, 1432, 1434, 1294,
148124         - /*   350 */  1546, 1547, 1569, 1569, 1577, 1577, 1577, 2009, 2009, 2009,
148125         - /*   360 */  2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
148126         - /*   370 */  2009, 2009, 2009,  591,  697, 1059, 1139, 1058,  797,  465,
148127         - /*   380 */  1159, 1182, 1122, 1062, 1180,  936, 1199, 1201, 1205, 1224,
148128         - /*   390 */  1225, 1244, 1061, 1145, 1261, 1161, 1194, 1249, 1251, 1256,
148129         - /*   400 */  1137, 1142, 1263, 1264, 1214, 1207, 1613, 1623, 1605, 1477,
148130         - /*   410 */  1614, 1541, 1620, 1616, 1617, 1509, 1502, 1525, 1619, 1514,
148131         - /*   420 */  1626, 1516, 1634, 1650, 1522, 1512, 1535, 1594, 1621, 1517,
148132         - /*   430 */  1604, 1606, 1607, 1609, 1544, 1559, 1631, 1536, 1666, 1663,
148133         - /*   440 */  1647, 1567, 1523, 1608, 1648, 1610, 1600, 1636, 1549, 1576,
148134         - /*   450 */  1657, 1662, 1664, 1565, 1572, 1665, 1624, 1668, 1671, 1672,
148135         - /*   460 */  1674, 1627, 1660, 1675, 1633, 1667, 1678, 1564, 1681, 1553,
148136         - /*   470 */  1690, 1692, 1691, 1693, 1696, 1700, 1702, 1705, 1704, 1599,
148137         - /*   480 */  1707, 1710, 1630, 1703, 1714, 1618, 1716, 1706, 1716, 1717,
148138         - /*   490 */  1653, 1677, 1670, 1711, 1731, 1732, 1733, 1734, 1723, 1735,
148139         - /*   500 */  1716, 1740, 1743, 1744, 1745, 1739, 1746, 1748, 1761, 1751,
148140         - /*   510 */  1752, 1753, 1754, 1758, 1759, 1749, 1658, 1654, 1655, 1656,
148141         - /*   520 */  1659, 1765, 1776, 1791,
148142         -};
148143         -#define YY_REDUCE_COUNT (372)
148144         -#define YY_REDUCE_MIN   (-235)
148145         -#define YY_REDUCE_MAX   (1441)
       148901  + /*     0 */  1814, 1632, 1987, 1426, 1426,  128, 1482, 1633, 1703, 1877,
       148902  + /*    10 */  1877, 1877,   87,    0,    0,  264, 1106, 1877, 1877, 1877,
       148903  + /*    20 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148904  + /*    30 */   226,  226,  381,  381,  296,  193,  128,  128,  128,  128,
       148905  + /*    40 */   128,  128,   97,  194,  332,  429,  526,  623,  720,  817,
       148906  + /*    50 */   914,  934, 1086, 1238, 1106, 1106, 1106, 1106, 1106, 1106,
       148907  + /*    60 */  1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106,
       148908  + /*    70 */  1106, 1106, 1258, 1106, 1355, 1375, 1375, 1817, 1877, 1877,
       148909  + /*    80 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148910  + /*    90 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148911  + /*   100 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148912  + /*   110 */  1937, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148913  + /*   120 */  1877, 1877, 1877, 1877,   32,  129,  129,  129,  129,  129,
       148914  + /*   130 */    21,  152,  297,  494,  726,   65,  494,  514,  514,  494,
       148915  + /*   140 */   560,  560,  560,  560,  322,  599,   50, 2142, 2142,  155,
       148916  + /*   150 */   155,  155,  313,  392,  386,  392,  392,  481,  481,  200,
       148917  + /*   160 */   480,  684,  758,  494,  494,  494,  494,  494,  494,  494,
       148918  + /*   170 */   494,  494,  494,  494,  494,  494,  494,  494,  494,  494,
       148919  + /*   180 */   494,  494,  494,  494,  768,  768,  494,  166,  377,  377,
       148920  + /*   190 */   635,  835,  835,  635,  748,  987, 2142, 2142, 2142,  448,
       148921  + /*   200 */    45,   45,  403,  484,  502,  106,  525,  508,  528,  538,
       148922  + /*   210 */   494,  494,  494,  494,  494,  494,  494,  494,  494,   84,
       148923  + /*   220 */   494,  494,  494,  494,  494,  494,  494,  494,  494,  494,
       148924  + /*   230 */   494,  494,  267,  267,  267,  494,  494,  494,  494,  769,
       148925  + /*   240 */   494,  494,  494,    4,  477,  494,  494,  788,  494,  494,
       148926  + /*   250 */   494,  494,  494,  494,  494,  494,  727,    5,  135,  985,
       148927  + /*   260 */   985,  985,  985,  522,  135,  135,  797,  326,  875,  986,
       148928  + /*   270 */   968, 1036, 1036, 1038,  968,  968, 1038,  972, 1081, 1118,
       148929  + /*   280 */  1194, 1194, 1194, 1036,  757,  757,  946,  777, 1099, 1102,
       148930  + /*   290 */  1333, 1282, 1282, 1381, 1381, 1282, 1297, 1334, 1422, 1406,
       148931  + /*   300 */  1322, 1448, 1448, 1448, 1448, 1282, 1451, 1322, 1322, 1334,
       148932  + /*   310 */  1422, 1406, 1406, 1322, 1282, 1451, 1345, 1434, 1282, 1451,
       148933  + /*   320 */  1483, 1282, 1451, 1282, 1451, 1483, 1404, 1404, 1404, 1452,
       148934  + /*   330 */  1483, 1404, 1405, 1404, 1452, 1404, 1404, 1483, 1423, 1423,
       148935  + /*   340 */  1483, 1407, 1437, 1407, 1437, 1407, 1437, 1407, 1437, 1282,
       148936  + /*   350 */  1462, 1462, 1411, 1418, 1537, 1282, 1416, 1411, 1428, 1431,
       148937  + /*   360 */  1322, 1555, 1560, 1575, 1575, 1584, 1584, 1584, 2142, 2142,
       148938  + /*   370 */  2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142,
       148939  + /*   380 */  2142, 2142, 2142, 2142,   61,  725,  374, 1080,  198,  771,
       148940  + /*   390 */   283, 1192, 1178, 1190, 1107, 1221, 1206, 1226, 1227, 1232,
       148941  + /*   400 */  1233, 1240, 1242, 1189, 1129, 1253,  216, 1210, 1247, 1248,
       148942  + /*   410 */  1249, 1131, 1151, 1274, 1293, 1220, 1214, 1605, 1608, 1591,
       148943  + /*   420 */  1459, 1600, 1525, 1606, 1598, 1602, 1494, 1492, 1513, 1614,
       148944  + /*   430 */  1504, 1620, 1510, 1625, 1647, 1515, 1507, 1531, 1595, 1621,
       148945  + /*   440 */  1514, 1607, 1610, 1612, 1613, 1536, 1552, 1634, 1533, 1669,
       148946  + /*   450 */  1666, 1651, 1566, 1522, 1609, 1650, 1611, 1603, 1639, 1547,
       148947  + /*   460 */  1574, 1659, 1664, 1667, 1561, 1569, 1668, 1622, 1671, 1672,
       148948  + /*   470 */  1665, 1673, 1624, 1670, 1674, 1630, 1662, 1677, 1559, 1681,
       148949  + /*   480 */  1682, 1549, 1684, 1685, 1683, 1688, 1690, 1692, 1691, 1695,
       148950  + /*   490 */  1694, 1585, 1698, 1705, 1617, 1696, 1707, 1596, 1709, 1697,
       148951  + /*   500 */  1702, 1704, 1711, 1652, 1675, 1658, 1708, 1676, 1656, 1714,
       148952  + /*   510 */  1726, 1731, 1730, 1729, 1733, 1722, 1734, 1709, 1737, 1738,
       148953  + /*   520 */  1742, 1743, 1741, 1745, 1747, 1759, 1749, 1750, 1752, 1753,
       148954  + /*   530 */  1751, 1755, 1757, 1660, 1653, 1655, 1657, 1661, 1761, 1777,
       148955  + /*   540 */  1792,
       148956  +};
       148957  +#define YY_REDUCE_COUNT (383)
       148958  +#define YY_REDUCE_MIN   (-257)
       148959  +#define YY_REDUCE_MAX   (1421)
148146 148960   static const short yy_reduce_ofst[] = {
148147         - /*     0 */  -147,  171,  263,  -96,  169, -144, -162, -149, -102, -156,
148148         - /*    10 */   -98,  216,  354, -170,  -57, -235,  307,  149,  423,  428,
148149         - /*    20 */   471,  313,  451,  519,  489,  496,  499,  545,  547,  555,
148150         - /*    30 */  -116,  540,  558,  592,  594,  597,  599, -206, -206, -206,
148151         - /*    40 */  -206, -206, -206, -206, -206, -206, -206, -206, -206, -206,
148152         - /*    50 */  -206, -206, -206, -206, -206, -206, -206, -206, -206, -206,
148153         - /*    60 */  -206, -206, -206, -206, -206, -206, -206, -206, -206, -206,
148154         - /*    70 */  -206, -206,  196,  309,  494,  537,  612,  656,  675,  679,
148155         - /*    80 */   681,  685,  724,  753,  771,  776,  788,  790,  794,  796,
148156         - /*    90 */   801,  803,  805,  807,  814,  819,  833,  837,  839,  842,
148157         - /*   100 */   845,  847,  849,  853,  873,  891,  893,  917,  921,  937,
148158         - /*   110 */   940,  944,  956,  960,  967,  969,  971,  973,  975, -206,
148159         - /*   120 */  -206, -206, -206, -206, -206, -206, -206, -206,  501, -168,
148160         - /*   130 */    90,  -97,   87,  112,  303,  277,  601,  277,  601,  179,
148161         - /*   140 */  -206, -206, -206, -206, -107, -107, -107,  -43,  -56,  323,
148162         - /*   150 */   500,  512, -187, -177,  317,  609,  353,  353,  120,  144,
148163         - /*   160 */   490,  539,  698,  374,  467,  507,  789,  404, -157,  755,
148164         - /*   170 */   856,  916,  843,  941,  802,  770,  923,  821, 1001, -142,
148165         - /*   180 */   264,  785,  896,  905,  899,  949, -176,  544,  911,  953,
148166         - /*   190 */  1012, -182,  -59,  -30,   16,  -22,  117,  172,  291,  369,
148167         - /*   200 */   407,  415,  566,  586,  647,  699,  754,  813,  850,  892,
148168         - /*   210 */   121, 1023, 1042, 1086, 1121, 1125, 1128, 1129, 1130, 1131,
148169         - /*   220 */  1132, 1134, 1135,  284, 1106, 1123, 1152, 1154, 1155, 1156,
148170         - /*   230 */   397, 1158, 1172, 1173, 1116, 1176, 1177, 1138, 1179,  117,
148171         - /*   240 */  1184, 1185, 1198, 1200, 1202, 1203,  741, 1094, 1153, 1146,
148172         - /*   250 */  1160, 1162, 1163,  397, 1153, 1153, 1170, 1204, 1206, 1103,
148173         - /*   260 */  1168, 1165, 1166, 1133, 1174, 1175, 1140, 1210, 1193, 1208,
148174         - /*   270 */  1212, 1215, 1216, 1178, 1167, 1189, 1196, 1241, 1148, 1243,
148175         - /*   280 */  1245, 1181, 1183, 1247, 1188, 1187, 1190, 1227, 1223, 1234,
148176         - /*   290 */  1236, 1238, 1239, 1274, 1278, 1235, 1237, 1213, 1218, 1253,
148177         - /*   300 */  1254, 1246, 1287, 1289, 1209, 1219, 1303, 1305, 1293, 1306,
148178         - /*   310 */  1309, 1313, 1316, 1297, 1301, 1307, 1308, 1298, 1310, 1311,
148179         - /*   320 */  1312, 1317, 1304, 1318, 1320, 1319, 1265, 1267, 1325, 1295,
148180         - /*   330 */  1300, 1296, 1302, 1326, 1321, 1327, 1330, 1365, 1323, 1269,
148181         - /*   340 */  1272, 1328, 1331, 1322, 1388, 1334, 1336, 1349, 1353, 1357,
148182         - /*   350 */  1405, 1409, 1420, 1421, 1427, 1429, 1430, 1332, 1335, 1339,
148183         - /*   360 */  1418, 1422, 1423, 1424, 1425, 1433, 1426, 1435, 1436, 1437,
148184         - /*   370 */  1438, 1441, 1439,
       148961  + /*     0 */  -168,  -17,  164,  214,  310, -166, -184,  -18,   98, -170,
       148962  + /*    10 */   305,  315, -163, -193, -178, -257,  395,  401,  476,  478,
       148963  + /*    20 */   512,  117,  527,  529,  503,  509,  532,  255,  552,  556,
       148964  + /*    30 */   558,  607,   37,  408,  594,  413,  462,  559,  561,  601,
       148965  + /*    40 */   610,  618, -254, -254, -254, -254, -254, -254, -254, -254,
       148966  + /*    50 */  -254, -254, -254, -254, -254, -254, -254, -254, -254, -254,
       148967  + /*    60 */  -254, -254, -254, -254, -254, -254, -254, -254, -254, -254,
       148968  + /*    70 */  -254, -254, -254, -254, -254, -254, -254, -111,  627,  650,
       148969  + /*    80 */   691,  697,  701,  703,  740,  742,  744,  767,  770,  790,
       148970  + /*    90 */   816,  818,  820,  822,  845,  857,  859,  861,  863,  865,
       148971  + /*   100 */   868,  870,  872,  874,  876,  888,  903,  906,  908,  915,
       148972  + /*   110 */   917,  922,  960,  962,  964,  988,  990,  992, 1015, 1017,
       148973  + /*   120 */  1029, 1033, 1035, 1040, -254, -254, -254, -254, -254, -254,
       148974  + /*   130 */  -254, -254, -254,  190,  270, -196,  160, -160,  450,  647,
       148975  + /*   140 */   260,  458,  260,  458,   78, -254, -254, -254, -254,  206,
       148976  + /*   150 */   206,  206,  320,  598,   -5,  675,  743, -148,  340, -125,
       148977  + /*   160 */   459,  466,  466,  693,  -93,  461,  479,  706,  710,  714,
       148978  + /*   170 */   716,  717,  169, -183,  325,  314,  704,  333,  747,  858,
       148979  + /*   180 */    -8,  819,  565,  755,  646,  660,  517,  265,  713,  791,
       148980  + /*   190 */   712,  795,  803,  918,  695,  860,  893,  935,  939, -181,
       148981  + /*   200 */  -172, -147,  -91,  -46,   -3,  162,  173,  231,  338,  437,
       148982  + /*   210 */   571,  614,  630,  651,  760,  931,  989, 1032, 1046, -218,
       148983  + /*   220 */    38, 1070, 1096, 1133, 1134, 1137, 1138, 1139, 1141, 1142,
       148984  + /*   230 */  1143, 1144,  292,  451, 1050, 1145, 1147, 1148, 1149,  813,
       148985  + /*   240 */  1161, 1162, 1163, 1108, 1049, 1166, 1168, 1146, 1169,  162,
       148986  + /*   250 */  1181, 1182, 1183, 1184, 1185, 1187, 1100, 1103, 1150, 1135,
       148987  + /*   260 */  1136, 1140, 1152,  813, 1150, 1150, 1153, 1173, 1195, 1090,
       148988  + /*   270 */  1154, 1167, 1170, 1104, 1155, 1156, 1109, 1172, 1174, 1179,
       148989  + /*   280 */  1177, 1188, 1205, 1171, 1160, 1196, 1121, 1165, 1203, 1228,
       148990  + /*   290 */  1157, 1244, 1245, 1158, 1159, 1250, 1175, 1193, 1191, 1241,
       148991  + /*   300 */  1231, 1243, 1257, 1259, 1261, 1276, 1280, 1254, 1255, 1230,
       148992  + /*   310 */  1234, 1269, 1270, 1260, 1292, 1303, 1223, 1225, 1309, 1313,
       148993  + /*   320 */  1296, 1317, 1319, 1320, 1323, 1300, 1307, 1308, 1310, 1304,
       148994  + /*   330 */  1311, 1315, 1314, 1318, 1316, 1321, 1325, 1324, 1271, 1272,
       148995  + /*   340 */  1332, 1294, 1298, 1299, 1301, 1302, 1306, 1312, 1329, 1356,
       148996  + /*   350 */  1256, 1263, 1327, 1326, 1305, 1369, 1330, 1340, 1343, 1346,
       148997  + /*   360 */  1350, 1391, 1394, 1410, 1412, 1415, 1417, 1419, 1328, 1331,
       148998  + /*   370 */  1335, 1402, 1398, 1400, 1401, 1403, 1408, 1396, 1397, 1409,
       148999  + /*   380 */  1414, 1420, 1421, 1413,
148185 149000   };
148186 149001   static const YYACTIONTYPE yy_default[] = {
148187         - /*     0 */  1500, 1500, 1500, 1346, 1129, 1235, 1129, 1129, 1129, 1346,
148188         - /*    10 */  1346, 1346, 1129, 1265, 1265, 1399, 1160, 1129, 1129, 1129,
148189         - /*    20 */  1129, 1129, 1129, 1129, 1345, 1129, 1129, 1129, 1129, 1129,
148190         - /*    30 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1271, 1129,
148191         - /*    40 */  1129, 1129, 1129, 1129, 1347, 1348, 1129, 1129, 1129, 1398,
148192         - /*    50 */  1400, 1363, 1281, 1280, 1279, 1278, 1381, 1252, 1276, 1269,
148193         - /*    60 */  1273, 1341, 1342, 1340, 1344, 1348, 1347, 1129, 1272, 1312,
148194         - /*    70 */  1326, 1311, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148195         - /*    80 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148196         - /*    90 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148197         - /*   100 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148198         - /*   110 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1320,
148199         - /*   120 */  1325, 1331, 1324, 1321, 1314, 1313, 1315, 1316, 1129, 1150,
148200         - /*   130 */  1199, 1129, 1129, 1129, 1129, 1417, 1416, 1129, 1129, 1160,
148201         - /*   140 */  1317, 1318, 1328, 1327, 1406, 1456, 1455, 1364, 1129, 1129,
148202         - /*   150 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148203         - /*   160 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148204         - /*   170 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148205         - /*   180 */  1160, 1156, 1306, 1305, 1426, 1156, 1259, 1129, 1412, 1235,
148206         - /*   190 */  1226, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148207         - /*   200 */  1129, 1129, 1129, 1129, 1403, 1401, 1129, 1129, 1129, 1129,
148208         - /*   210 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148209         - /*   220 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148210         - /*   230 */  1129, 1129, 1129, 1129, 1231, 1129, 1129, 1129, 1129, 1129,
148211         - /*   240 */  1129, 1129, 1129, 1129, 1129, 1450, 1129, 1376, 1213, 1231,
148212         - /*   250 */  1231, 1231, 1231, 1233, 1214, 1212, 1225, 1160, 1136, 1492,
148213         - /*   260 */  1275, 1254, 1254, 1489, 1275, 1275, 1489, 1174, 1470, 1171,
148214         - /*   270 */  1265, 1265, 1265, 1254, 1343, 1232, 1225, 1129, 1492, 1240,
148215         - /*   280 */  1240, 1491, 1491, 1240, 1364, 1284, 1290, 1202, 1275, 1208,
148216         - /*   290 */  1208, 1208, 1208, 1240, 1147, 1275, 1275, 1284, 1290, 1202,
148217         - /*   300 */  1202, 1275, 1240, 1147, 1380, 1486, 1240, 1147, 1354, 1240,
148218         - /*   310 */  1147, 1240, 1147, 1354, 1200, 1200, 1200, 1189, 1354, 1200,
148219         - /*   320 */  1174, 1200, 1189, 1200, 1200, 1354, 1358, 1358, 1354, 1258,
148220         - /*   330 */  1253, 1258, 1253, 1258, 1253, 1258, 1253, 1240, 1259, 1425,
148221         - /*   340 */  1129, 1270, 1259, 1349, 1240, 1129, 1270, 1268, 1266, 1275,
148222         - /*   350 */  1153, 1192, 1453, 1453, 1449, 1449, 1449, 1497, 1497, 1412,
148223         - /*   360 */  1465, 1160, 1160, 1160, 1160, 1465, 1176, 1176, 1160, 1160,
148224         - /*   370 */  1160, 1160, 1465, 1129, 1129, 1129, 1129, 1129, 1129, 1460,
148225         - /*   380 */  1129, 1365, 1244, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148226         - /*   390 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148227         - /*   400 */  1129, 1129, 1129, 1129, 1129, 1295, 1129, 1132, 1409, 1129,
148228         - /*   410 */  1129, 1407, 1129, 1129, 1129, 1129, 1129, 1129, 1245, 1129,
148229         - /*   420 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148230         - /*   430 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1488, 1129, 1129,
148231         - /*   440 */  1129, 1129, 1129, 1129, 1379, 1378, 1129, 1129, 1242, 1129,
148232         - /*   450 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148233         - /*   460 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148234         - /*   470 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148235         - /*   480 */  1129, 1129, 1129, 1129, 1129, 1129, 1267, 1129, 1424, 1129,
148236         - /*   490 */  1129, 1129, 1129, 1129, 1129, 1129, 1438, 1260, 1129, 1129,
148237         - /*   500 */  1479, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148238         - /*   510 */  1129, 1129, 1129, 1129, 1129, 1474, 1216, 1297, 1129, 1296,
148239         - /*   520 */  1300, 1129, 1141, 1129,
       149002  + /*     0 */  1536, 1536, 1536, 1376, 1159, 1265, 1159, 1159, 1159, 1376,
       149003  + /*    10 */  1376, 1376, 1159, 1295, 1295, 1429, 1190, 1159, 1159, 1159,
       149004  + /*    20 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1375, 1159, 1159,
       149005  + /*    30 */  1159, 1159, 1459, 1459, 1159, 1159, 1159, 1159, 1159, 1159,
       149006  + /*    40 */  1159, 1159, 1159, 1301, 1159, 1159, 1159, 1159, 1159, 1377,
       149007  + /*    50 */  1378, 1159, 1159, 1159, 1428, 1430, 1393, 1311, 1310, 1309,
       149008  + /*    60 */  1308, 1411, 1282, 1306, 1299, 1303, 1371, 1372, 1370, 1374,
       149009  + /*    70 */  1378, 1377, 1159, 1302, 1342, 1356, 1341, 1159, 1159, 1159,
       149010  + /*    80 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149011  + /*    90 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149012  + /*   100 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149013  + /*   110 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149014  + /*   120 */  1159, 1159, 1159, 1159, 1350, 1355, 1361, 1354, 1351, 1344,
       149015  + /*   130 */  1343, 1345, 1346, 1159, 1180, 1229, 1159, 1159, 1159, 1159,
       149016  + /*   140 */  1447, 1446, 1159, 1159, 1190, 1347, 1348, 1358, 1357, 1436,
       149017  + /*   150 */  1492, 1491, 1394, 1159, 1159, 1159, 1159, 1159, 1159, 1459,
       149018  + /*   160 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149019  + /*   170 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149020  + /*   180 */  1159, 1159, 1159, 1159, 1459, 1459, 1159, 1190, 1459, 1459,
       149021  + /*   190 */  1186, 1336, 1335, 1186, 1289, 1159, 1442, 1265, 1256, 1159,
       149022  + /*   200 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149023  + /*   210 */  1159, 1159, 1159, 1433, 1431, 1159, 1159, 1159, 1159, 1159,
       149024  + /*   220 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149025  + /*   230 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149026  + /*   240 */  1159, 1159, 1159, 1261, 1159, 1159, 1159, 1159, 1159, 1159,
       149027  + /*   250 */  1159, 1159, 1159, 1159, 1159, 1486, 1159, 1406, 1243, 1261,
       149028  + /*   260 */  1261, 1261, 1261, 1263, 1244, 1242, 1255, 1190, 1166, 1528,
       149029  + /*   270 */  1305, 1284, 1284, 1525, 1305, 1305, 1525, 1204, 1506, 1201,
       149030  + /*   280 */  1295, 1295, 1295, 1284, 1289, 1289, 1373, 1262, 1255, 1159,
       149031  + /*   290 */  1528, 1270, 1270, 1527, 1527, 1270, 1394, 1314, 1320, 1232,
       149032  + /*   300 */  1305, 1238, 1238, 1238, 1238, 1270, 1177, 1305, 1305, 1314,
       149033  + /*   310 */  1320, 1232, 1232, 1305, 1270, 1177, 1410, 1522, 1270, 1177,
       149034  + /*   320 */  1384, 1270, 1177, 1270, 1177, 1384, 1230, 1230, 1230, 1219,
       149035  + /*   330 */  1384, 1230, 1204, 1230, 1219, 1230, 1230, 1384, 1388, 1388,
       149036  + /*   340 */  1384, 1288, 1283, 1288, 1283, 1288, 1283, 1288, 1283, 1270,
       149037  + /*   350 */  1469, 1469, 1300, 1289, 1379, 1270, 1159, 1300, 1298, 1296,
       149038  + /*   360 */  1305, 1183, 1222, 1489, 1489, 1485, 1485, 1485, 1533, 1533,
       149039  + /*   370 */  1442, 1501, 1190, 1190, 1190, 1190, 1501, 1206, 1206, 1190,
       149040  + /*   380 */  1190, 1190, 1190, 1501, 1159, 1159, 1159, 1159, 1159, 1159,
       149041  + /*   390 */  1496, 1159, 1395, 1274, 1159, 1159, 1159, 1159, 1159, 1159,
       149042  + /*   400 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149043  + /*   410 */  1159, 1159, 1159, 1159, 1159, 1159, 1325, 1159, 1162, 1439,
       149044  + /*   420 */  1159, 1159, 1437, 1159, 1159, 1159, 1159, 1159, 1159, 1275,
       149045  + /*   430 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149046  + /*   440 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1524, 1159,
       149047  + /*   450 */  1159, 1159, 1159, 1159, 1159, 1409, 1408, 1159, 1159, 1272,
       149048  + /*   460 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149049  + /*   470 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149050  + /*   480 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149051  + /*   490 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1297, 1159,
       149052  + /*   500 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149053  + /*   510 */  1159, 1159, 1159, 1474, 1290, 1159, 1159, 1515, 1159, 1159,
       149054  + /*   520 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149055  + /*   530 */  1159, 1159, 1510, 1246, 1327, 1159, 1326, 1330, 1159, 1171,
       149056  + /*   540 */  1159,
148240 149057   };
148241 149058   /********** End of lemon-generated parsing tables *****************************/
148242 149059   
148243 149060   /* The next table maps tokens (terminal symbols) into fallback tokens.  
148244 149061   ** If a construct like the following:
148245 149062   ** 
148246 149063   **      %fallback ID X Y Z.
................................................................................
148340 149157      59,  /*       WITH => ID */
148341 149158      59,  /*    CURRENT => ID */
148342 149159      59,  /*  FOLLOWING => ID */
148343 149160      59,  /*  PARTITION => ID */
148344 149161      59,  /*  PRECEDING => ID */
148345 149162      59,  /*      RANGE => ID */
148346 149163      59,  /*  UNBOUNDED => ID */
       149164  +   59,  /*    EXCLUDE => ID */
       149165  +   59,  /*     GROUPS => ID */
       149166  +   59,  /*     OTHERS => ID */
       149167  +   59,  /*       TIES => ID */
148347 149168      59,  /*    REINDEX => ID */
148348 149169      59,  /*     RENAME => ID */
148349 149170      59,  /*   CTIME_KW => ID */
148350 149171   };
148351 149172   #endif /* YYFALLBACK */
148352 149173   
148353 149174   /* The following structure represents a single element of the
................................................................................
148518 149339     /*   81 */ "WITH",
148519 149340     /*   82 */ "CURRENT",
148520 149341     /*   83 */ "FOLLOWING",
148521 149342     /*   84 */ "PARTITION",
148522 149343     /*   85 */ "PRECEDING",
148523 149344     /*   86 */ "RANGE",
148524 149345     /*   87 */ "UNBOUNDED",
148525         -  /*   88 */ "REINDEX",
148526         -  /*   89 */ "RENAME",
148527         -  /*   90 */ "CTIME_KW",
148528         -  /*   91 */ "ANY",
148529         -  /*   92 */ "BITAND",
148530         -  /*   93 */ "BITOR",
148531         -  /*   94 */ "LSHIFT",
148532         -  /*   95 */ "RSHIFT",
148533         -  /*   96 */ "PLUS",
148534         -  /*   97 */ "MINUS",
148535         -  /*   98 */ "STAR",
148536         -  /*   99 */ "SLASH",
148537         -  /*  100 */ "REM",
148538         -  /*  101 */ "CONCAT",
148539         -  /*  102 */ "COLLATE",
148540         -  /*  103 */ "BITNOT",
148541         -  /*  104 */ "ON",
148542         -  /*  105 */ "INDEXED",
148543         -  /*  106 */ "STRING",
148544         -  /*  107 */ "JOIN_KW",
148545         -  /*  108 */ "CONSTRAINT",
148546         -  /*  109 */ "DEFAULT",
148547         -  /*  110 */ "NULL",
148548         -  /*  111 */ "PRIMARY",
148549         -  /*  112 */ "UNIQUE",
148550         -  /*  113 */ "CHECK",
148551         -  /*  114 */ "REFERENCES",
148552         -  /*  115 */ "AUTOINCR",
148553         -  /*  116 */ "INSERT",
148554         -  /*  117 */ "DELETE",
148555         -  /*  118 */ "UPDATE",
148556         -  /*  119 */ "SET",
148557         -  /*  120 */ "DEFERRABLE",
148558         -  /*  121 */ "FOREIGN",
148559         -  /*  122 */ "DROP",
148560         -  /*  123 */ "UNION",
148561         -  /*  124 */ "ALL",
148562         -  /*  125 */ "EXCEPT",
148563         -  /*  126 */ "INTERSECT",
148564         -  /*  127 */ "SELECT",
148565         -  /*  128 */ "VALUES",
148566         -  /*  129 */ "DISTINCT",
148567         -  /*  130 */ "DOT",
148568         -  /*  131 */ "FROM",
148569         -  /*  132 */ "JOIN",
148570         -  /*  133 */ "USING",
148571         -  /*  134 */ "ORDER",
148572         -  /*  135 */ "GROUP",
148573         -  /*  136 */ "HAVING",
148574         -  /*  137 */ "LIMIT",
148575         -  /*  138 */ "WHERE",
148576         -  /*  139 */ "INTO",
148577         -  /*  140 */ "NOTHING",
148578         -  /*  141 */ "FLOAT",
148579         -  /*  142 */ "BLOB",
148580         -  /*  143 */ "INTEGER",
148581         -  /*  144 */ "VARIABLE",
148582         -  /*  145 */ "CASE",
148583         -  /*  146 */ "WHEN",
148584         -  /*  147 */ "THEN",
148585         -  /*  148 */ "ELSE",
148586         -  /*  149 */ "INDEX",
148587         -  /*  150 */ "ALTER",
148588         -  /*  151 */ "ADD",
148589         -  /*  152 */ "WINDOW",
148590         -  /*  153 */ "OVER",
148591         -  /*  154 */ "FILTER",
148592         -  /*  155 */ "input",
148593         -  /*  156 */ "cmdlist",
148594         -  /*  157 */ "ecmd",
148595         -  /*  158 */ "cmdx",
148596         -  /*  159 */ "explain",
148597         -  /*  160 */ "cmd",
148598         -  /*  161 */ "transtype",
148599         -  /*  162 */ "trans_opt",
148600         -  /*  163 */ "nm",
148601         -  /*  164 */ "savepoint_opt",
148602         -  /*  165 */ "create_table",
148603         -  /*  166 */ "create_table_args",
148604         -  /*  167 */ "createkw",
148605         -  /*  168 */ "temp",
148606         -  /*  169 */ "ifnotexists",
148607         -  /*  170 */ "dbnm",
148608         -  /*  171 */ "columnlist",
148609         -  /*  172 */ "conslist_opt",
148610         -  /*  173 */ "table_options",
148611         -  /*  174 */ "select",
148612         -  /*  175 */ "columnname",
148613         -  /*  176 */ "carglist",
148614         -  /*  177 */ "typetoken",
148615         -  /*  178 */ "typename",
148616         -  /*  179 */ "signed",
148617         -  /*  180 */ "plus_num",
148618         -  /*  181 */ "minus_num",
148619         -  /*  182 */ "scanpt",
148620         -  /*  183 */ "ccons",
148621         -  /*  184 */ "term",
148622         -  /*  185 */ "expr",
148623         -  /*  186 */ "onconf",
148624         -  /*  187 */ "sortorder",
148625         -  /*  188 */ "autoinc",
148626         -  /*  189 */ "eidlist_opt",
148627         -  /*  190 */ "refargs",
148628         -  /*  191 */ "defer_subclause",
148629         -  /*  192 */ "refarg",
148630         -  /*  193 */ "refact",
148631         -  /*  194 */ "init_deferred_pred_opt",
148632         -  /*  195 */ "conslist",
148633         -  /*  196 */ "tconscomma",
148634         -  /*  197 */ "tcons",
148635         -  /*  198 */ "sortlist",
148636         -  /*  199 */ "eidlist",
148637         -  /*  200 */ "defer_subclause_opt",
148638         -  /*  201 */ "orconf",
148639         -  /*  202 */ "resolvetype",
148640         -  /*  203 */ "raisetype",
148641         -  /*  204 */ "ifexists",
148642         -  /*  205 */ "fullname",
148643         -  /*  206 */ "selectnowith",
148644         -  /*  207 */ "oneselect",
148645         -  /*  208 */ "wqlist",
148646         -  /*  209 */ "multiselect_op",
148647         -  /*  210 */ "distinct",
148648         -  /*  211 */ "selcollist",
148649         -  /*  212 */ "from",
148650         -  /*  213 */ "where_opt",
148651         -  /*  214 */ "groupby_opt",
148652         -  /*  215 */ "having_opt",
148653         -  /*  216 */ "orderby_opt",
148654         -  /*  217 */ "limit_opt",
148655         -  /*  218 */ "window_clause",
148656         -  /*  219 */ "values",
148657         -  /*  220 */ "nexprlist",
148658         -  /*  221 */ "sclp",
148659         -  /*  222 */ "as",
148660         -  /*  223 */ "seltablist",
148661         -  /*  224 */ "stl_prefix",
148662         -  /*  225 */ "joinop",
148663         -  /*  226 */ "indexed_opt",
148664         -  /*  227 */ "on_opt",
148665         -  /*  228 */ "using_opt",
148666         -  /*  229 */ "exprlist",
148667         -  /*  230 */ "xfullname",
148668         -  /*  231 */ "idlist",
148669         -  /*  232 */ "with",
148670         -  /*  233 */ "setlist",
148671         -  /*  234 */ "insert_cmd",
148672         -  /*  235 */ "idlist_opt",
148673         -  /*  236 */ "upsert",
148674         -  /*  237 */ "over_clause",
148675         -  /*  238 */ "likeop",
148676         -  /*  239 */ "between_op",
148677         -  /*  240 */ "in_op",
148678         -  /*  241 */ "paren_exprlist",
148679         -  /*  242 */ "case_operand",
148680         -  /*  243 */ "case_exprlist",
148681         -  /*  244 */ "case_else",
148682         -  /*  245 */ "uniqueflag",
148683         -  /*  246 */ "collate",
148684         -  /*  247 */ "vinto",
148685         -  /*  248 */ "nmnum",
148686         -  /*  249 */ "trigger_decl",
148687         -  /*  250 */ "trigger_cmd_list",
148688         -  /*  251 */ "trigger_time",
148689         -  /*  252 */ "trigger_event",
148690         -  /*  253 */ "foreach_clause",
148691         -  /*  254 */ "when_clause",
148692         -  /*  255 */ "trigger_cmd",
148693         -  /*  256 */ "trnm",
148694         -  /*  257 */ "tridxby",
148695         -  /*  258 */ "database_kw_opt",
148696         -  /*  259 */ "key_opt",
148697         -  /*  260 */ "add_column_fullname",
148698         -  /*  261 */ "kwcolumn_opt",
148699         -  /*  262 */ "create_vtab",
148700         -  /*  263 */ "vtabarglist",
148701         -  /*  264 */ "vtabarg",
148702         -  /*  265 */ "vtabargtoken",
148703         -  /*  266 */ "lp",
148704         -  /*  267 */ "anylist",
148705         -  /*  268 */ "windowdefn_list",
148706         -  /*  269 */ "windowdefn",
148707         -  /*  270 */ "window",
148708         -  /*  271 */ "frame_opt",
148709         -  /*  272 */ "part_opt",
148710         -  /*  273 */ "filter_opt",
148711         -  /*  274 */ "range_or_rows",
148712         -  /*  275 */ "frame_bound",
148713         -  /*  276 */ "frame_bound_s",
148714         -  /*  277 */ "frame_bound_e",
       149346  +  /*   88 */ "EXCLUDE",
       149347  +  /*   89 */ "GROUPS",
       149348  +  /*   90 */ "OTHERS",
       149349  +  /*   91 */ "TIES",
       149350  +  /*   92 */ "REINDEX",
       149351  +  /*   93 */ "RENAME",
       149352  +  /*   94 */ "CTIME_KW",
       149353  +  /*   95 */ "ANY",
       149354  +  /*   96 */ "BITAND",
       149355  +  /*   97 */ "BITOR",
       149356  +  /*   98 */ "LSHIFT",
       149357  +  /*   99 */ "RSHIFT",
       149358  +  /*  100 */ "PLUS",
       149359  +  /*  101 */ "MINUS",
       149360  +  /*  102 */ "STAR",
       149361  +  /*  103 */ "SLASH",
       149362  +  /*  104 */ "REM",
       149363  +  /*  105 */ "CONCAT",
       149364  +  /*  106 */ "COLLATE",
       149365  +  /*  107 */ "BITNOT",
       149366  +  /*  108 */ "ON",
       149367  +  /*  109 */ "INDEXED",
       149368  +  /*  110 */ "STRING",
       149369  +  /*  111 */ "JOIN_KW",
       149370  +  /*  112 */ "CONSTRAINT",
       149371  +  /*  113 */ "DEFAULT",
       149372  +  /*  114 */ "NULL",
       149373  +  /*  115 */ "PRIMARY",
       149374  +  /*  116 */ "UNIQUE",
       149375  +  /*  117 */ "CHECK",
       149376  +  /*  118 */ "REFERENCES",
       149377  +  /*  119 */ "AUTOINCR",
       149378  +  /*  120 */ "INSERT",
       149379  +  /*  121 */ "DELETE",
       149380  +  /*  122 */ "UPDATE",
       149381  +  /*  123 */ "SET",
       149382  +  /*  124 */ "DEFERRABLE",
       149383  +  /*  125 */ "FOREIGN",
       149384  +  /*  126 */ "DROP",
       149385  +  /*  127 */ "UNION",
       149386  +  /*  128 */ "ALL",
       149387  +  /*  129 */ "EXCEPT",
       149388  +  /*  130 */ "INTERSECT",
       149389  +  /*  131 */ "SELECT",
       149390  +  /*  132 */ "VALUES",
       149391  +  /*  133 */ "DISTINCT",
       149392  +  /*  134 */ "DOT",
       149393  +  /*  135 */ "FROM",
       149394  +  /*  136 */ "JOIN",
       149395  +  /*  137 */ "USING",
       149396  +  /*  138 */ "ORDER",
       149397  +  /*  139 */ "GROUP",
       149398  +  /*  140 */ "HAVING",
       149399  +  /*  141 */ "LIMIT",
       149400  +  /*  142 */ "WHERE",
       149401  +  /*  143 */ "INTO",
       149402  +  /*  144 */ "NOTHING",
       149403  +  /*  145 */ "FLOAT",
       149404  +  /*  146 */ "BLOB",
       149405  +  /*  147 */ "INTEGER",
       149406  +  /*  148 */ "VARIABLE",
       149407  +  /*  149 */ "CASE",
       149408  +  /*  150 */ "WHEN",
       149409  +  /*  151 */ "THEN",
       149410  +  /*  152 */ "ELSE",
       149411  +  /*  153 */ "INDEX",
       149412  +  /*  154 */ "ALTER",
       149413  +  /*  155 */ "ADD",
       149414  +  /*  156 */ "WINDOW",
       149415  +  /*  157 */ "OVER",
       149416  +  /*  158 */ "FILTER",
       149417  +  /*  159 */ "TRUEFALSE",
       149418  +  /*  160 */ "ISNOT",
       149419  +  /*  161 */ "FUNCTION",
       149420  +  /*  162 */ "COLUMN",
       149421  +  /*  163 */ "AGG_FUNCTION",
       149422  +  /*  164 */ "AGG_COLUMN",
       149423  +  /*  165 */ "UMINUS",
       149424  +  /*  166 */ "UPLUS",
       149425  +  /*  167 */ "TRUTH",
       149426  +  /*  168 */ "REGISTER",
       149427  +  /*  169 */ "VECTOR",
       149428  +  /*  170 */ "SELECT_COLUMN",
       149429  +  /*  171 */ "IF_NULL_ROW",
       149430  +  /*  172 */ "ASTERISK",
       149431  +  /*  173 */ "SPAN",
       149432  +  /*  174 */ "SPACE",
       149433  +  /*  175 */ "ILLEGAL",
       149434  +  /*  176 */ "input",
       149435  +  /*  177 */ "cmdlist",
       149436  +  /*  178 */ "ecmd",
       149437  +  /*  179 */ "cmdx",
       149438  +  /*  180 */ "explain",
       149439  +  /*  181 */ "cmd",
       149440  +  /*  182 */ "transtype",
       149441  +  /*  183 */ "trans_opt",
       149442  +  /*  184 */ "nm",
       149443  +  /*  185 */ "savepoint_opt",
       149444  +  /*  186 */ "create_table",
       149445  +  /*  187 */ "create_table_args",
       149446  +  /*  188 */ "createkw",
       149447  +  /*  189 */ "temp",
       149448  +  /*  190 */ "ifnotexists",
       149449  +  /*  191 */ "dbnm",
       149450  +  /*  192 */ "columnlist",
       149451  +  /*  193 */ "conslist_opt",
       149452  +  /*  194 */ "table_options",
       149453  +  /*  195 */ "select",
       149454  +  /*  196 */ "columnname",
       149455  +  /*  197 */ "carglist",
       149456  +  /*  198 */ "typetoken",
       149457  +  /*  199 */ "typename",
       149458  +  /*  200 */ "signed",
       149459  +  /*  201 */ "plus_num",
       149460  +  /*  202 */ "minus_num",
       149461  +  /*  203 */ "scanpt",
       149462  +  /*  204 */ "ccons",
       149463  +  /*  205 */ "term",
       149464  +  /*  206 */ "expr",
       149465  +  /*  207 */ "onconf",
       149466  +  /*  208 */ "sortorder",
       149467  +  /*  209 */ "autoinc",
       149468  +  /*  210 */ "eidlist_opt",
       149469  +  /*  211 */ "refargs",
       149470  +  /*  212 */ "defer_subclause",
       149471  +  /*  213 */ "refarg",
       149472  +  /*  214 */ "refact",
       149473  +  /*  215 */ "init_deferred_pred_opt",
       149474  +  /*  216 */ "conslist",
       149475  +  /*  217 */ "tconscomma",
       149476  +  /*  218 */ "tcons",
       149477  +  /*  219 */ "sortlist",
       149478  +  /*  220 */ "eidlist",
       149479  +  /*  221 */ "defer_subclause_opt",
       149480  +  /*  222 */ "orconf",
       149481  +  /*  223 */ "resolvetype",
       149482  +  /*  224 */ "raisetype",
       149483  +  /*  225 */ "ifexists",
       149484  +  /*  226 */ "fullname",
       149485  +  /*  227 */ "selectnowith",
       149486  +  /*  228 */ "oneselect",
       149487  +  /*  229 */ "wqlist",
       149488  +  /*  230 */ "multiselect_op",
       149489  +  /*  231 */ "distinct",
       149490  +  /*  232 */ "selcollist",
       149491  +  /*  233 */ "from",
       149492  +  /*  234 */ "where_opt",
       149493  +  /*  235 */ "groupby_opt",
       149494  +  /*  236 */ "having_opt",
       149495  +  /*  237 */ "orderby_opt",
       149496  +  /*  238 */ "limit_opt",
       149497  +  /*  239 */ "window_clause",
       149498  +  /*  240 */ "values",
       149499  +  /*  241 */ "nexprlist",
       149500  +  /*  242 */ "sclp",
       149501  +  /*  243 */ "as",
       149502  +  /*  244 */ "seltablist",
       149503  +  /*  245 */ "stl_prefix",
       149504  +  /*  246 */ "joinop",
       149505  +  /*  247 */ "indexed_opt",
       149506  +  /*  248 */ "on_opt",
       149507  +  /*  249 */ "using_opt",
       149508  +  /*  250 */ "exprlist",
       149509  +  /*  251 */ "xfullname",
       149510  +  /*  252 */ "idlist",
       149511  +  /*  253 */ "with",
       149512  +  /*  254 */ "setlist",
       149513  +  /*  255 */ "insert_cmd",
       149514  +  /*  256 */ "idlist_opt",
       149515  +  /*  257 */ "upsert",
       149516  +  /*  258 */ "over_clause",
       149517  +  /*  259 */ "likeop",
       149518  +  /*  260 */ "between_op",
       149519  +  /*  261 */ "in_op",
       149520  +  /*  262 */ "paren_exprlist",
       149521  +  /*  263 */ "case_operand",
       149522  +  /*  264 */ "case_exprlist",
       149523  +  /*  265 */ "case_else",
       149524  +  /*  266 */ "uniqueflag",
       149525  +  /*  267 */ "collate",
       149526  +  /*  268 */ "vinto",
       149527  +  /*  269 */ "nmnum",
       149528  +  /*  270 */ "trigger_decl",
       149529  +  /*  271 */ "trigger_cmd_list",
       149530  +  /*  272 */ "trigger_time",
       149531  +  /*  273 */ "trigger_event",
       149532  +  /*  274 */ "foreach_clause",
       149533  +  /*  275 */ "when_clause",
       149534  +  /*  276 */ "trigger_cmd",
       149535  +  /*  277 */ "trnm",
       149536  +  /*  278 */ "tridxby",
       149537  +  /*  279 */ "database_kw_opt",
       149538  +  /*  280 */ "key_opt",
       149539  +  /*  281 */ "add_column_fullname",
       149540  +  /*  282 */ "kwcolumn_opt",
       149541  +  /*  283 */ "create_vtab",
       149542  +  /*  284 */ "vtabarglist",
       149543  +  /*  285 */ "vtabarg",
       149544  +  /*  286 */ "vtabargtoken",
       149545  +  /*  287 */ "lp",
       149546  +  /*  288 */ "anylist",
       149547  +  /*  289 */ "windowdefn_list",
       149548  +  /*  290 */ "windowdefn",
       149549  +  /*  291 */ "window",
       149550  +  /*  292 */ "frame_opt",
       149551  +  /*  293 */ "part_opt",
       149552  +  /*  294 */ "filter_opt",
       149553  +  /*  295 */ "range_or_rows",
       149554  +  /*  296 */ "frame_bound",
       149555  +  /*  297 */ "frame_bound_s",
       149556  +  /*  298 */ "frame_bound_e",
       149557  +  /*  299 */ "frame_exclude_opt",
       149558  +  /*  300 */ "frame_exclude",
148715 149559   };
148716 149560   #endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
148717 149561   
148718 149562   #ifndef NDEBUG
148719 149563   /* For tracing reduce actions, the names of all rules are required.
148720 149564   */
148721 149565   static const char *const yyRuleName[] = {
................................................................................
149005 149849    /* 283 */ "lp ::= LP",
149006 149850    /* 284 */ "with ::= WITH wqlist",
149007 149851    /* 285 */ "with ::= WITH RECURSIVE wqlist",
149008 149852    /* 286 */ "wqlist ::= nm eidlist_opt AS LP select RP",
149009 149853    /* 287 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
149010 149854    /* 288 */ "windowdefn_list ::= windowdefn",
149011 149855    /* 289 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
149012         - /* 290 */ "windowdefn ::= nm AS window",
149013         - /* 291 */ "window ::= LP part_opt orderby_opt frame_opt RP",
149014         - /* 292 */ "part_opt ::= PARTITION BY nexprlist",
149015         - /* 293 */ "part_opt ::=",
149016         - /* 294 */ "frame_opt ::=",
149017         - /* 295 */ "frame_opt ::= range_or_rows frame_bound_s",
149018         - /* 296 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e",
149019         - /* 297 */ "range_or_rows ::= RANGE",
149020         - /* 298 */ "range_or_rows ::= ROWS",
149021         - /* 299 */ "frame_bound_s ::= frame_bound",
149022         - /* 300 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
149023         - /* 301 */ "frame_bound_e ::= frame_bound",
149024         - /* 302 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
149025         - /* 303 */ "frame_bound ::= expr PRECEDING",
149026         - /* 304 */ "frame_bound ::= CURRENT ROW",
149027         - /* 305 */ "frame_bound ::= expr FOLLOWING",
149028         - /* 306 */ "window_clause ::= WINDOW windowdefn_list",
149029         - /* 307 */ "over_clause ::= filter_opt OVER window",
149030         - /* 308 */ "over_clause ::= filter_opt OVER nm",
149031         - /* 309 */ "filter_opt ::=",
149032         - /* 310 */ "filter_opt ::= FILTER LP WHERE expr RP",
149033         - /* 311 */ "input ::= cmdlist",
149034         - /* 312 */ "cmdlist ::= cmdlist ecmd",
149035         - /* 313 */ "cmdlist ::= ecmd",
149036         - /* 314 */ "ecmd ::= SEMI",
149037         - /* 315 */ "ecmd ::= cmdx SEMI",
149038         - /* 316 */ "ecmd ::= explain cmdx",
149039         - /* 317 */ "trans_opt ::=",
149040         - /* 318 */ "trans_opt ::= TRANSACTION",
149041         - /* 319 */ "trans_opt ::= TRANSACTION nm",
149042         - /* 320 */ "savepoint_opt ::= SAVEPOINT",
149043         - /* 321 */ "savepoint_opt ::=",
149044         - /* 322 */ "cmd ::= create_table create_table_args",
149045         - /* 323 */ "columnlist ::= columnlist COMMA columnname carglist",
149046         - /* 324 */ "columnlist ::= columnname carglist",
149047         - /* 325 */ "nm ::= ID|INDEXED",
149048         - /* 326 */ "nm ::= STRING",
149049         - /* 327 */ "nm ::= JOIN_KW",
149050         - /* 328 */ "typetoken ::= typename",
149051         - /* 329 */ "typename ::= ID|STRING",
149052         - /* 330 */ "signed ::= plus_num",
149053         - /* 331 */ "signed ::= minus_num",
149054         - /* 332 */ "carglist ::= carglist ccons",
149055         - /* 333 */ "carglist ::=",
149056         - /* 334 */ "ccons ::= NULL onconf",
149057         - /* 335 */ "conslist_opt ::= COMMA conslist",
149058         - /* 336 */ "conslist ::= conslist tconscomma tcons",
149059         - /* 337 */ "conslist ::= tcons",
149060         - /* 338 */ "tconscomma ::=",
149061         - /* 339 */ "defer_subclause_opt ::= defer_subclause",
149062         - /* 340 */ "resolvetype ::= raisetype",
149063         - /* 341 */ "selectnowith ::= oneselect",
149064         - /* 342 */ "oneselect ::= values",
149065         - /* 343 */ "sclp ::= selcollist COMMA",
149066         - /* 344 */ "as ::= ID|STRING",
149067         - /* 345 */ "expr ::= term",
149068         - /* 346 */ "likeop ::= LIKE_KW|MATCH",
149069         - /* 347 */ "exprlist ::= nexprlist",
149070         - /* 348 */ "nmnum ::= plus_num",
149071         - /* 349 */ "nmnum ::= nm",
149072         - /* 350 */ "nmnum ::= ON",
149073         - /* 351 */ "nmnum ::= DELETE",
149074         - /* 352 */ "nmnum ::= DEFAULT",
149075         - /* 353 */ "plus_num ::= INTEGER|FLOAT",
149076         - /* 354 */ "foreach_clause ::=",
149077         - /* 355 */ "foreach_clause ::= FOR EACH ROW",
149078         - /* 356 */ "trnm ::= nm",
149079         - /* 357 */ "tridxby ::=",
149080         - /* 358 */ "database_kw_opt ::= DATABASE",
149081         - /* 359 */ "database_kw_opt ::=",
149082         - /* 360 */ "kwcolumn_opt ::=",
149083         - /* 361 */ "kwcolumn_opt ::= COLUMNKW",
149084         - /* 362 */ "vtabarglist ::= vtabarg",
149085         - /* 363 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
149086         - /* 364 */ "vtabarg ::= vtabarg vtabargtoken",
149087         - /* 365 */ "anylist ::=",
149088         - /* 366 */ "anylist ::= anylist LP anylist RP",
149089         - /* 367 */ "anylist ::= anylist ANY",
149090         - /* 368 */ "with ::=",
       149856  + /* 290 */ "windowdefn ::= nm AS LP window RP",
       149857  + /* 291 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
       149858  + /* 292 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
       149859  + /* 293 */ "window ::= ORDER BY sortlist frame_opt",
       149860  + /* 294 */ "window ::= nm ORDER BY sortlist frame_opt",
       149861  + /* 295 */ "window ::= frame_opt",
       149862  + /* 296 */ "window ::= nm frame_opt",
       149863  + /* 297 */ "frame_opt ::=",
       149864  + /* 298 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
       149865  + /* 299 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
       149866  + /* 300 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
       149867  + /* 301 */ "frame_bound_s ::= frame_bound",
       149868  + /* 302 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
       149869  + /* 303 */ "frame_bound_e ::= frame_bound",
       149870  + /* 304 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
       149871  + /* 305 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
       149872  + /* 306 */ "frame_bound ::= CURRENT ROW",
       149873  + /* 307 */ "frame_exclude_opt ::=",
       149874  + /* 308 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
       149875  + /* 309 */ "frame_exclude ::= NO OTHERS",
       149876  + /* 310 */ "frame_exclude ::= CURRENT ROW",
       149877  + /* 311 */ "frame_exclude ::= GROUP|TIES",
       149878  + /* 312 */ "window_clause ::= WINDOW windowdefn_list",
       149879  + /* 313 */ "over_clause ::= filter_opt OVER LP window RP",
       149880  + /* 314 */ "over_clause ::= filter_opt OVER nm",
       149881  + /* 315 */ "filter_opt ::=",
       149882  + /* 316 */ "filter_opt ::= FILTER LP WHERE expr RP",
       149883  + /* 317 */ "input ::= cmdlist",
       149884  + /* 318 */ "cmdlist ::= cmdlist ecmd",
       149885  + /* 319 */ "cmdlist ::= ecmd",
       149886  + /* 320 */ "ecmd ::= SEMI",
       149887  + /* 321 */ "ecmd ::= cmdx SEMI",
       149888  + /* 322 */ "ecmd ::= explain cmdx",
       149889  + /* 323 */ "trans_opt ::=",
       149890  + /* 324 */ "trans_opt ::= TRANSACTION",
       149891  + /* 325 */ "trans_opt ::= TRANSACTION nm",
       149892  + /* 326 */ "savepoint_opt ::= SAVEPOINT",
       149893  + /* 327 */ "savepoint_opt ::=",
       149894  + /* 328 */ "cmd ::= create_table create_table_args",
       149895  + /* 329 */ "columnlist ::= columnlist COMMA columnname carglist",
       149896  + /* 330 */ "columnlist ::= columnname carglist",
       149897  + /* 331 */ "nm ::= ID|INDEXED",
       149898  + /* 332 */ "nm ::= STRING",
       149899  + /* 333 */ "nm ::= JOIN_KW",
       149900  + /* 334 */ "typetoken ::= typename",
       149901  + /* 335 */ "typename ::= ID|STRING",
       149902  + /* 336 */ "signed ::= plus_num",
       149903  + /* 337 */ "signed ::= minus_num",
       149904  + /* 338 */ "carglist ::= carglist ccons",
       149905  + /* 339 */ "carglist ::=",
       149906  + /* 340 */ "ccons ::= NULL onconf",
       149907  + /* 341 */ "conslist_opt ::= COMMA conslist",
       149908  + /* 342 */ "conslist ::= conslist tconscomma tcons",
       149909  + /* 343 */ "conslist ::= tcons",
       149910  + /* 344 */ "tconscomma ::=",
       149911  + /* 345 */ "defer_subclause_opt ::= defer_subclause",
       149912  + /* 346 */ "resolvetype ::= raisetype",
       149913  + /* 347 */ "selectnowith ::= oneselect",
       149914  + /* 348 */ "oneselect ::= values",
       149915  + /* 349 */ "sclp ::= selcollist COMMA",
       149916  + /* 350 */ "as ::= ID|STRING",
       149917  + /* 351 */ "expr ::= term",
       149918  + /* 352 */ "likeop ::= LIKE_KW|MATCH",
       149919  + /* 353 */ "exprlist ::= nexprlist",
       149920  + /* 354 */ "nmnum ::= plus_num",
       149921  + /* 355 */ "nmnum ::= nm",
       149922  + /* 356 */ "nmnum ::= ON",
       149923  + /* 357 */ "nmnum ::= DELETE",
       149924  + /* 358 */ "nmnum ::= DEFAULT",
       149925  + /* 359 */ "plus_num ::= INTEGER|FLOAT",
       149926  + /* 360 */ "foreach_clause ::=",
       149927  + /* 361 */ "foreach_clause ::= FOR EACH ROW",
       149928  + /* 362 */ "trnm ::= nm",
       149929  + /* 363 */ "tridxby ::=",
       149930  + /* 364 */ "database_kw_opt ::= DATABASE",
       149931  + /* 365 */ "database_kw_opt ::=",
       149932  + /* 366 */ "kwcolumn_opt ::=",
       149933  + /* 367 */ "kwcolumn_opt ::= COLUMNKW",
       149934  + /* 368 */ "vtabarglist ::= vtabarg",
       149935  + /* 369 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
       149936  + /* 370 */ "vtabarg ::= vtabarg vtabargtoken",
       149937  + /* 371 */ "anylist ::=",
       149938  + /* 372 */ "anylist ::= anylist LP anylist RP",
       149939  + /* 373 */ "anylist ::= anylist ANY",
       149940  + /* 374 */ "with ::=",
149091 149941   };
149092 149942   #endif /* NDEBUG */
149093 149943   
149094 149944   
149095 149945   #if YYSTACKDEPTH<=0
149096 149946   /*
149097 149947   ** Try to increase the size of the parser stack.  Return the number
................................................................................
149209 150059       ** being destroyed before it is finished parsing.
149210 150060       **
149211 150061       ** Note: during a reduce, the only symbols destroyed are those
149212 150062       ** which appear on the RHS of the rule, but which are *not* used
149213 150063       ** inside the C code.
149214 150064       */
149215 150065   /********* Begin destructor definitions ***************************************/
149216         -    case 174: /* select */
149217         -    case 206: /* selectnowith */
149218         -    case 207: /* oneselect */
149219         -    case 219: /* values */
149220         -{
149221         -sqlite3SelectDelete(pParse->db, (yypminor->yy423));
149222         -}
149223         -      break;
149224         -    case 184: /* term */
149225         -    case 185: /* expr */
149226         -    case 213: /* where_opt */
149227         -    case 215: /* having_opt */
149228         -    case 227: /* on_opt */
149229         -    case 242: /* case_operand */
149230         -    case 244: /* case_else */
149231         -    case 247: /* vinto */
149232         -    case 254: /* when_clause */
149233         -    case 259: /* key_opt */
149234         -    case 273: /* filter_opt */
149235         -{
149236         -sqlite3ExprDelete(pParse->db, (yypminor->yy490));
149237         -}
149238         -      break;
149239         -    case 189: /* eidlist_opt */
149240         -    case 198: /* sortlist */
149241         -    case 199: /* eidlist */
149242         -    case 211: /* selcollist */
149243         -    case 214: /* groupby_opt */