sqllogictest
Check-in [865a75877d]
Not logged in

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

Overview
Comment:Update the built-in SQLite to the 3.28.0 beta.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 865a75877dadcad2f5f49441261133d55126d911
User & Date: drh 2019-04-15 14:24:58
Context
2019-07-08
20:51
Update the built-in SQLite to the first 3.29.0 beta. Leaf check-in: bf409a4f2c user: drh tags: trunk
2019-04-15
14:24
Update the built-in SQLite to the 3.28.0 beta. check-in: 865a75877d user: drh tags: trunk
2019-02-06
16:28
Update the built-in SQLite to the latest 3.27.0 beta. check-in: 40b4bc94f8 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.27.0.  By combining all the individual C code files into this
            3  +** version 3.28.0.  By combining all the individual C code files into this
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
  1158   1158   ** been edited in any way since it was last checked in, then the last
  1159   1159   ** four hexadecimal digits of the hash may be modified.
  1160   1160   **
  1161   1161   ** See also: [sqlite3_libversion()],
  1162   1162   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1163   1163   ** [sqlite_version()] and [sqlite_source_id()].
  1164   1164   */
  1165         -#define SQLITE_VERSION        "3.27.0"
  1166         -#define SQLITE_VERSION_NUMBER 3027000
  1167         -#define SQLITE_SOURCE_ID      "2019-02-06 15:23:43 433d6ef637a10ee017b5d8cadd125a162cfedc9895ae862b8dde6dddb7eda59b"
         1165  +#define SQLITE_VERSION        "3.28.0"
         1166  +#define SQLITE_VERSION_NUMBER 3028000
         1167  +#define SQLITE_SOURCE_ID      "2019-04-15 13:59:08 734192d876e0489cb34e454aaa4a9c6858145791a49959b31dee2917a8e41a7d"
  1168   1168   
  1169   1169   /*
  1170   1170   ** CAPI3REF: Run-Time Library Version Numbers
  1171   1171   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1172   1172   **
  1173   1173   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1174   1174   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1224   1224   **
  1225   1225   ** See also: SQL functions [sqlite_compileoption_used()] and
  1226   1226   ** [sqlite_compileoption_get()] and the [compile_options pragma].
  1227   1227   */
  1228   1228   #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  1229   1229   SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
  1230   1230   SQLITE_API const char *sqlite3_compileoption_get(int N);
         1231  +#else
         1232  +# define sqlite3_compileoption_used(X) 0
         1233  +# define sqlite3_compileoption_get(X)  ((void*)0)
  1231   1234   #endif
  1232   1235   
  1233   1236   /*
  1234   1237   ** CAPI3REF: Test To See If The Library Is Threadsafe
  1235   1238   **
  1236   1239   ** ^The sqlite3_threadsafe() function returns zero if and only if
  1237   1240   ** SQLite was compiled with mutexing code omitted due to the
................................................................................
  3121   3124   ** The second parameter is a pointer to an integer into which
  3122   3125   ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  3123   3126   ** following this call.  The second parameter may be a NULL pointer, in
  3124   3127   ** which case the trigger setting is not reported back. </dd>
  3125   3128   **
  3126   3129   ** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
  3127   3130   ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
  3128         -** <dd> ^This option is used to enable or disable the two-argument
  3129         -** version of the [fts3_tokenizer()] function which is part of the
         3131  +** <dd> ^This option is used to enable or disable the
         3132  +** [fts3_tokenizer()] function which is part of the
  3130   3133   ** [FTS3] full-text search engine extension.
  3131   3134   ** There should be two additional arguments.
  3132   3135   ** The first argument is an integer which is 0 to disable fts3_tokenizer() or
  3133   3136   ** positive to enable fts3_tokenizer() or negative to leave the setting
  3134   3137   ** unchanged.
  3135   3138   ** The second parameter is a pointer to an integer into which
  3136   3139   ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
................................................................................
  3234   3237   ** features include but are not limited to the following:
  3235   3238   ** <ul>
  3236   3239   ** <li> The [PRAGMA writable_schema=ON] statement.
  3237   3240   ** <li> Writes to the [sqlite_dbpage] virtual table.
  3238   3241   ** <li> Direct writes to [shadow tables].
  3239   3242   ** </ul>
  3240   3243   ** </dd>
         3244  +**
         3245  +** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
         3246  +** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
         3247  +** "writable_schema" flag. This has the same effect and is logically equivalent
         3248  +** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
         3249  +** The first argument to this setting is an integer which is 0 to disable 
         3250  +** the writable_schema, positive to enable writable_schema, or negative to
         3251  +** leave the setting unchanged. The second parameter is a pointer to an
         3252  +** integer into which is written 0 or 1 to indicate whether the writable_schema
         3253  +** is enabled or disabled following this call.
         3254  +** </dd>
  3241   3255   ** </dl>
  3242   3256   */
  3243   3257   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  3244   3258   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  3245   3259   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  3246   3260   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  3247   3261   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  3248   3262   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  3249   3263   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  3250   3264   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  3251   3265   #define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
  3252   3266   #define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
  3253   3267   #define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
  3254         -#define SQLITE_DBCONFIG_MAX                   1010 /* Largest DBCONFIG */
         3268  +#define SQLITE_DBCONFIG_WRITABLE_SCHEMA       1011 /* int int* */
         3269  +#define SQLITE_DBCONFIG_MAX                   1011 /* Largest DBCONFIG */
  3255   3270   
  3256   3271   /*
  3257   3272   ** CAPI3REF: Enable Or Disable Extended Result Codes
  3258   3273   ** METHOD: sqlite3
  3259   3274   **
  3260   3275   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  3261   3276   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  3404   3419   ** does not affect the value returned by sqlite3_total_changes().
  3405   3420   ** 
  3406   3421   ** ^Changes made as part of [foreign key actions] are included in the
  3407   3422   ** count, but those made as part of REPLACE constraint resolution are
  3408   3423   ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers 
  3409   3424   ** are not counted.
  3410   3425   **
  3411         -** This the [sqlite3_total_changes(D)] interface only reports the number
         3426  +** The [sqlite3_total_changes(D)] interface only reports the number
  3412   3427   ** of rows that changed due to SQL statement run against database
  3413   3428   ** connection D.  Any changes by other database connections are ignored.
  3414   3429   ** To detect changes against a database file from other database
  3415   3430   ** connections use the [PRAGMA data_version] command or the
  3416   3431   ** [SQLITE_FCNTL_DATA_VERSION] [file control].
  3417   3432   ** 
  3418   3433   ** If a separate thread makes changes on the same database connection
................................................................................
  4929   4944   ** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
  4930   4945   ** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
  4931   4946   ** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
  4932   4947   ** sqlite3_stmt_readonly() returns false for those commands.
  4933   4948   */
  4934   4949   SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  4935   4950   
         4951  +/*
         4952  +** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
         4953  +** METHOD: sqlite3_stmt
         4954  +**
         4955  +** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
         4956  +** prepared statement S is an EXPLAIN statement, or 2 if the
         4957  +** statement S is an EXPLAIN QUERY PLAN.
         4958  +** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
         4959  +** an ordinary statement or a NULL pointer.
         4960  +*/
         4961  +SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
         4962  +
  4936   4963   /*
  4937   4964   ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
  4938   4965   ** METHOD: sqlite3_stmt
  4939   4966   **
  4940   4967   ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
  4941   4968   ** [prepared statement] S has been stepped at least once using 
  4942   4969   ** [sqlite3_step(S)] but has neither run to completion (returned
................................................................................
  5068   5095   ** the value of the fourth parameter then the resulting string value will
  5069   5096   ** contain embedded NULs.  The result of expressions involving strings
  5070   5097   ** with embedded NULs is undefined.
  5071   5098   **
  5072   5099   ** ^The fifth argument to the BLOB and string binding interfaces
  5073   5100   ** is a destructor used to dispose of the BLOB or
  5074   5101   ** string after SQLite has finished with it.  ^The destructor is called
  5075         -** to dispose of the BLOB or string even if the call to bind API fails.
         5102  +** to dispose of the BLOB or string even if the call to the bind API fails,
         5103  +** except the destructor is not called if the third parameter is a NULL
         5104  +** pointer or the fourth parameter is negative.
  5076   5105   ** ^If the fifth argument is
  5077   5106   ** the special value [SQLITE_STATIC], then SQLite assumes that the
  5078   5107   ** information is in static, unmanaged space and does not need to be freed.
  5079   5108   ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  5080   5109   ** SQLite makes its own private copy of the data immediately, before
  5081   5110   ** the sqlite3_bind_*() routine returns.
  5082   5111   **
................................................................................
  5985   6014   ** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
  5986   6015   ** datatype of the value
  5987   6016   ** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
  5988   6017   ** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
  5989   6018   ** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
  5990   6019   ** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
  5991   6020   ** against a virtual table.
         6021  +** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
         6022  +** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
  5992   6023   ** </table></blockquote>
  5993   6024   **
  5994   6025   ** <b>Details:</b>
  5995   6026   **
  5996   6027   ** These routines extract type, size, and content information from
  5997   6028   ** [protected sqlite3_value] objects.  Protected sqlite3_value objects
  5998   6029   ** are used to pass parameter information into implementation of
................................................................................
  6045   6076   ** the value for that column returned without setting a result (probably
  6046   6077   ** because it queried [sqlite3_vtab_nochange()] and found that the column
  6047   6078   ** was unchanging).  ^Within an [xUpdate] method, any value for which
  6048   6079   ** sqlite3_value_nochange(X) is true will in all other respects appear
  6049   6080   ** to be a NULL value.  If sqlite3_value_nochange(X) is invoked anywhere other
  6050   6081   ** than within an [xUpdate] method call for an UPDATE statement, then
  6051   6082   ** the return value is arbitrary and meaningless.
         6083  +**
         6084  +** ^The sqlite3_value_frombind(X) interface returns non-zero if the
         6085  +** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
         6086  +** interfaces.  ^If X comes from an SQL literal value, or a table column,
         6087  +** and expression, then sqlite3_value_frombind(X) returns zero.
  6052   6088   **
  6053   6089   ** Please pay particular attention to the fact that the pointer returned
  6054   6090   ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
  6055   6091   ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
  6056   6092   ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
  6057   6093   ** or [sqlite3_value_text16()].
  6058   6094   **
................................................................................
  6091   6127   SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
  6092   6128   SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
  6093   6129   SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
  6094   6130   SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
  6095   6131   SQLITE_API int sqlite3_value_type(sqlite3_value*);
  6096   6132   SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
  6097   6133   SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
         6134  +SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
  6098   6135   
  6099   6136   /*
  6100   6137   ** CAPI3REF: Finding The Subtype Of SQL Values
  6101   6138   ** METHOD: sqlite3_value
  6102   6139   **
  6103   6140   ** The sqlite3_value_subtype(V) function returns the subtype for
  6104   6141   ** an [application-defined SQL function] argument V.  The subtype
................................................................................
  6826   6863   ** CAPI3REF: Return The Filename For A Database Connection
  6827   6864   ** METHOD: sqlite3
  6828   6865   **
  6829   6866   ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  6830   6867   ** associated with database N of connection D.  ^The main database file
  6831   6868   ** has the name "main".  If there is no attached database N on the database
  6832   6869   ** connection D, or if database N is a temporary or in-memory database, then
  6833         -** a NULL pointer is returned.
         6870  +** this function will return either a NULL pointer or an empty string.
  6834   6871   **
  6835   6872   ** ^The filename returned by this function is the output of the
  6836   6873   ** xFullPathname method of the [VFS].  ^In other words, the filename
  6837   6874   ** will be an absolute pathname, even if the filename used
  6838   6875   ** to open the database originally was a URI or relative pathname.
  6839   6876   */
  6840   6877   SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
................................................................................
 11927  11964   ** CAPI3REF: Rebase a changeset
 11928  11965   ** EXPERIMENTAL
 11929  11966   **
 11930  11967   ** Argument pIn must point to a buffer containing a changeset nIn bytes
 11931  11968   ** in size. This function allocates and populates a buffer with a copy
 11932  11969   ** of the changeset rebased rebased according to the configuration of the
 11933  11970   ** rebaser object passed as the first argument. If successful, (*ppOut)
 11934         -** is set to point to the new buffer containing the rebased changset and 
        11971  +** is set to point to the new buffer containing the rebased changeset and 
 11935  11972   ** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
 11936  11973   ** responsibility of the caller to eventually free the new buffer using
 11937  11974   ** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
 11938  11975   ** are set to zero and an SQLite error code returned.
 11939  11976   */
 11940  11977   SQLITE_API int sqlite3rebaser_rebase(
 11941  11978     sqlite3_rebaser*,
................................................................................
 12336  12373   **
 12337  12374   **
 12338  12375   ** xSetAuxdata(pFts5, pAux, xDelete)
 12339  12376   **
 12340  12377   **   Save the pointer passed as the second argument as the extension functions 
 12341  12378   **   "auxiliary data". The pointer may then be retrieved by the current or any
 12342  12379   **   future invocation of the same fts5 extension function made as part of
 12343         -**   of the same MATCH query using the xGetAuxdata() API.
        12380  +**   the same MATCH query using the xGetAuxdata() API.
 12344  12381   **
 12345  12382   **   Each extension function is allocated a single auxiliary data slot for
 12346  12383   **   each FTS query (MATCH expression). If the extension function is invoked 
 12347  12384   **   more than once for a single FTS query, then all invocations share a 
 12348  12385   **   single auxiliary data context.
 12349  12386   **
 12350  12387   **   If there is already an auxiliary data pointer when this function is
................................................................................
 12351  12388   **   invoked, then it is replaced by the new pointer. If an xDelete callback
 12352  12389   **   was specified along with the original pointer, it is invoked at this
 12353  12390   **   point.
 12354  12391   **
 12355  12392   **   The xDelete callback, if one is specified, is also invoked on the
 12356  12393   **   auxiliary data pointer after the FTS5 query has finished.
 12357  12394   **
 12358         -**   If an error (e.g. an OOM condition) occurs within this function, an
        12395  +**   If an error (e.g. an OOM condition) occurs within this function,
 12359  12396   **   the auxiliary data is set to NULL and an error code returned. If the
 12360  12397   **   xDelete parameter was not NULL, it is invoked on the auxiliary data
 12361  12398   **   pointer before returning.
 12362  12399   **
 12363  12400   **
 12364  12401   ** xGetAuxdata(pFts5, bClear)
 12365  12402   **
................................................................................
 13377  13414   ** the hash table.
 13378  13415   */
 13379  13416   struct Hash {
 13380  13417     unsigned int htsize;      /* Number of buckets in the hash table */
 13381  13418     unsigned int count;       /* Number of entries in this table */
 13382  13419     HashElem *first;          /* The first element of the array */
 13383  13420     struct _ht {              /* the hash table */
 13384         -    int count;                 /* Number of entries with this hash */
        13421  +    unsigned int count;        /* Number of entries with this hash */
 13385  13422       HashElem *chain;           /* Pointer to first entry with this hash */
 13386  13423     } *ht;
 13387  13424   };
 13388  13425   
 13389  13426   /* Each element in the hash table is an instance of the following 
 13390  13427   ** structure.  All elements are stored on a single doubly-linked list.
 13391  13428   **
................................................................................
 13518  13555   #define TK_WITH                            81
 13519  13556   #define TK_CURRENT                         82
 13520  13557   #define TK_FOLLOWING                       83
 13521  13558   #define TK_PARTITION                       84
 13522  13559   #define TK_PRECEDING                       85
 13523  13560   #define TK_RANGE                           86
 13524  13561   #define TK_UNBOUNDED                       87
 13525         -#define TK_REINDEX                         88
 13526         -#define TK_RENAME                          89
 13527         -#define TK_CTIME_KW                        90
 13528         -#define TK_ANY                             91
 13529         -#define TK_BITAND                          92
 13530         -#define TK_BITOR                           93
 13531         -#define TK_LSHIFT                          94
 13532         -#define TK_RSHIFT                          95
 13533         -#define TK_PLUS                            96
 13534         -#define TK_MINUS                           97
 13535         -#define TK_STAR                            98
 13536         -#define TK_SLASH                           99
 13537         -#define TK_REM                            100
 13538         -#define TK_CONCAT                         101
 13539         -#define TK_COLLATE                        102
 13540         -#define TK_BITNOT                         103
 13541         -#define TK_ON                             104
 13542         -#define TK_INDEXED                        105
 13543         -#define TK_STRING                         106
 13544         -#define TK_JOIN_KW                        107
 13545         -#define TK_CONSTRAINT                     108
 13546         -#define TK_DEFAULT                        109
 13547         -#define TK_NULL                           110
 13548         -#define TK_PRIMARY                        111
 13549         -#define TK_UNIQUE                         112
 13550         -#define TK_CHECK                          113
 13551         -#define TK_REFERENCES                     114
 13552         -#define TK_AUTOINCR                       115
 13553         -#define TK_INSERT                         116
 13554         -#define TK_DELETE                         117
 13555         -#define TK_UPDATE                         118
 13556         -#define TK_SET                            119
 13557         -#define TK_DEFERRABLE                     120
 13558         -#define TK_FOREIGN                        121
 13559         -#define TK_DROP                           122
 13560         -#define TK_UNION                          123
 13561         -#define TK_ALL                            124
 13562         -#define TK_EXCEPT                         125
 13563         -#define TK_INTERSECT                      126
 13564         -#define TK_SELECT                         127
 13565         -#define TK_VALUES                         128
 13566         -#define TK_DISTINCT                       129
 13567         -#define TK_DOT                            130
 13568         -#define TK_FROM                           131
 13569         -#define TK_JOIN                           132
 13570         -#define TK_USING                          133
 13571         -#define TK_ORDER                          134
 13572         -#define TK_GROUP                          135
 13573         -#define TK_HAVING                         136
 13574         -#define TK_LIMIT                          137
 13575         -#define TK_WHERE                          138
 13576         -#define TK_INTO                           139
 13577         -#define TK_NOTHING                        140
 13578         -#define TK_FLOAT                          141
 13579         -#define TK_BLOB                           142
 13580         -#define TK_INTEGER                        143
 13581         -#define TK_VARIABLE                       144
 13582         -#define TK_CASE                           145
 13583         -#define TK_WHEN                           146
 13584         -#define TK_THEN                           147
 13585         -#define TK_ELSE                           148
 13586         -#define TK_INDEX                          149
 13587         -#define TK_ALTER                          150
 13588         -#define TK_ADD                            151
 13589         -#define TK_WINDOW                         152
 13590         -#define TK_OVER                           153
 13591         -#define TK_FILTER                         154
 13592         -#define TK_TRUEFALSE                      155
 13593         -#define TK_ISNOT                          156
 13594         -#define TK_FUNCTION                       157
 13595         -#define TK_COLUMN                         158
 13596         -#define TK_AGG_FUNCTION                   159
 13597         -#define TK_AGG_COLUMN                     160
 13598         -#define TK_UMINUS                         161
 13599         -#define TK_UPLUS                          162
 13600         -#define TK_TRUTH                          163
 13601         -#define TK_REGISTER                       164
 13602         -#define TK_VECTOR                         165
 13603         -#define TK_SELECT_COLUMN                  166
 13604         -#define TK_IF_NULL_ROW                    167
 13605         -#define TK_ASTERISK                       168
 13606         -#define TK_SPAN                           169
 13607         -#define TK_END_OF_FILE                    170
 13608         -#define TK_UNCLOSED_STRING                171
 13609         -#define TK_SPACE                          172
 13610         -#define TK_ILLEGAL                        173
 13611         -
 13612         -/* The token codes above must all fit in 8 bits */
 13613         -#define TKFLG_MASK           0xff  
 13614         -
 13615         -/* Flags that can be added to a token code when it is not
 13616         -** being stored in a u8: */
 13617         -#define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
        13562  +#define TK_EXCLUDE                         88
        13563  +#define TK_GROUPS                          89
        13564  +#define TK_OTHERS                          90
        13565  +#define TK_TIES                            91
        13566  +#define TK_REINDEX                         92
        13567  +#define TK_RENAME                          93
        13568  +#define TK_CTIME_KW                        94
        13569  +#define TK_ANY                             95
        13570  +#define TK_BITAND                          96
        13571  +#define TK_BITOR                           97
        13572  +#define TK_LSHIFT                          98
        13573  +#define TK_RSHIFT                          99
        13574  +#define TK_PLUS                           100
        13575  +#define TK_MINUS                          101
        13576  +#define TK_STAR                           102
        13577  +#define TK_SLASH                          103
        13578  +#define TK_REM                            104
        13579  +#define TK_CONCAT                         105
        13580  +#define TK_COLLATE                        106
        13581  +#define TK_BITNOT                         107
        13582  +#define TK_ON                             108
        13583  +#define TK_INDEXED                        109
        13584  +#define TK_STRING                         110
        13585  +#define TK_JOIN_KW                        111
        13586  +#define TK_CONSTRAINT                     112
        13587  +#define TK_DEFAULT                        113
        13588  +#define TK_NULL                           114
        13589  +#define TK_PRIMARY                        115
        13590  +#define TK_UNIQUE                         116
        13591  +#define TK_CHECK                          117
        13592  +#define TK_REFERENCES                     118
        13593  +#define TK_AUTOINCR                       119
        13594  +#define TK_INSERT                         120
        13595  +#define TK_DELETE                         121
        13596  +#define TK_UPDATE                         122
        13597  +#define TK_SET                            123
        13598  +#define TK_DEFERRABLE                     124
        13599  +#define TK_FOREIGN                        125
        13600  +#define TK_DROP                           126
        13601  +#define TK_UNION                          127
        13602  +#define TK_ALL                            128
        13603  +#define TK_EXCEPT                         129
        13604  +#define TK_INTERSECT                      130
        13605  +#define TK_SELECT                         131
        13606  +#define TK_VALUES                         132
        13607  +#define TK_DISTINCT                       133
        13608  +#define TK_DOT                            134
        13609  +#define TK_FROM                           135
        13610  +#define TK_JOIN                           136
        13611  +#define TK_USING                          137
        13612  +#define TK_ORDER                          138
        13613  +#define TK_GROUP                          139
        13614  +#define TK_HAVING                         140
        13615  +#define TK_LIMIT                          141
        13616  +#define TK_WHERE                          142
        13617  +#define TK_INTO                           143
        13618  +#define TK_NOTHING                        144
        13619  +#define TK_FLOAT                          145
        13620  +#define TK_BLOB                           146
        13621  +#define TK_INTEGER                        147
        13622  +#define TK_VARIABLE                       148
        13623  +#define TK_CASE                           149
        13624  +#define TK_WHEN                           150
        13625  +#define TK_THEN                           151
        13626  +#define TK_ELSE                           152
        13627  +#define TK_INDEX                          153
        13628  +#define TK_ALTER                          154
        13629  +#define TK_ADD                            155
        13630  +#define TK_WINDOW                         156
        13631  +#define TK_OVER                           157
        13632  +#define TK_FILTER                         158
        13633  +#define TK_TRUEFALSE                      159
        13634  +#define TK_ISNOT                          160
        13635  +#define TK_FUNCTION                       161
        13636  +#define TK_COLUMN                         162
        13637  +#define TK_AGG_FUNCTION                   163
        13638  +#define TK_AGG_COLUMN                     164
        13639  +#define TK_UMINUS                         165
        13640  +#define TK_UPLUS                          166
        13641  +#define TK_TRUTH                          167
        13642  +#define TK_REGISTER                       168
        13643  +#define TK_VECTOR                         169
        13644  +#define TK_SELECT_COLUMN                  170
        13645  +#define TK_IF_NULL_ROW                    171
        13646  +#define TK_ASTERISK                       172
        13647  +#define TK_SPAN                           173
        13648  +#define TK_SPACE                          174
        13649  +#define TK_ILLEGAL                        175
 13618  13650   
 13619  13651   /************** End of parse.h ***********************************************/
 13620  13652   /************** Continuing where we left off in sqliteInt.h ******************/
 13621  13653   #include <stdio.h>
 13622  13654   #include <stdlib.h>
 13623  13655   #include <string.h>
 13624  13656   #include <assert.h>
................................................................................
 14542  14574     int nData;              /* Size of pData.  0 if none. */
 14543  14575     int nZero;              /* Extra zero data appended after pData,nData */
 14544  14576   };
 14545  14577   
 14546  14578   SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
 14547  14579                          int flags, int seekResult);
 14548  14580   SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
 14549         -#ifndef SQLITE_OMIT_WINDOWFUNC
 14550         -SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor*);
 14551         -#endif
 14552  14581   SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
 14553  14582   SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
 14554  14583   SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
 14555  14584   SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
 14556  14585   SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
 14557  14586   #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
 14558  14587   SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
................................................................................
 14902  14931   #define OP_Cast           85 /* synopsis: affinity(r[P1])                  */
 14903  14932   #define OP_Permutation    86
 14904  14933   #define OP_Compare        87 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 14905  14934   #define OP_IsTrue         88 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
 14906  14935   #define OP_Offset         89 /* synopsis: r[P3] = sqlite_offset(P1)        */
 14907  14936   #define OP_Column         90 /* synopsis: r[P3]=PX                         */
 14908  14937   #define OP_Affinity       91 /* synopsis: affinity(r[P1@P2])               */
 14909         -#define OP_BitAnd         92 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
 14910         -#define OP_BitOr          93 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
 14911         -#define OP_ShiftLeft      94 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
 14912         -#define OP_ShiftRight     95 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
 14913         -#define OP_Add            96 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
 14914         -#define OP_Subtract       97 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 14915         -#define OP_Multiply       98 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 14916         -#define OP_Divide         99 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 14917         -#define OP_Remainder     100 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 14918         -#define OP_Concat        101 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 14919         -#define OP_MakeRecord    102 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 14920         -#define OP_BitNot        103 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
 14921         -#define OP_Count         104 /* synopsis: r[P2]=count()                    */
 14922         -#define OP_ReadCookie    105
 14923         -#define OP_String8       106 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 14924         -#define OP_SetCookie     107
 14925         -#define OP_ReopenIdx     108 /* synopsis: root=P2 iDb=P3                   */
 14926         -#define OP_OpenRead      109 /* synopsis: root=P2 iDb=P3                   */
 14927         -#define OP_OpenWrite     110 /* synopsis: root=P2 iDb=P3                   */
        14938  +#define OP_MakeRecord     92 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        14939  +#define OP_Count          93 /* synopsis: r[P2]=count()                    */
        14940  +#define OP_ReadCookie     94
        14941  +#define OP_SetCookie      95
        14942  +#define OP_BitAnd         96 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
        14943  +#define OP_BitOr          97 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
        14944  +#define OP_ShiftLeft      98 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
        14945  +#define OP_ShiftRight     99 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
        14946  +#define OP_Add           100 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
        14947  +#define OP_Subtract      101 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
        14948  +#define OP_Multiply      102 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
        14949  +#define OP_Divide        103 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
        14950  +#define OP_Remainder     104 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
        14951  +#define OP_Concat        105 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
        14952  +#define OP_ReopenIdx     106 /* synopsis: root=P2 iDb=P3                   */
        14953  +#define OP_BitNot        107 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
        14954  +#define OP_OpenRead      108 /* synopsis: root=P2 iDb=P3                   */
        14955  +#define OP_OpenWrite     109 /* synopsis: root=P2 iDb=P3                   */
        14956  +#define OP_String8       110 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 14928  14957   #define OP_OpenDup       111
 14929  14958   #define OP_OpenAutoindex 112 /* synopsis: nColumn=P2                       */
 14930  14959   #define OP_OpenEphemeral 113 /* synopsis: nColumn=P2                       */
 14931  14960   #define OP_SorterOpen    114
 14932  14961   #define OP_SequenceTest  115 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 14933  14962   #define OP_OpenPseudo    116 /* synopsis: P3 columns in r[P2]              */
 14934  14963   #define OP_Close         117
 14935  14964   #define OP_ColumnsUsed   118
 14936  14965   #define OP_SeekHit       119 /* synopsis: seekHit=P2                       */
 14937  14966   #define OP_Sequence      120 /* synopsis: r[P2]=cursor[P1].ctr++           */
 14938  14967   #define OP_NewRowid      121 /* synopsis: r[P2]=rowid                      */
 14939  14968   #define OP_Insert        122 /* synopsis: intkey=r[P3] data=r[P2]          */
 14940         -#define OP_InsertInt     123 /* synopsis: intkey=P3 data=r[P2]             */
 14941         -#define OP_Delete        124
 14942         -#define OP_ResetCount    125
 14943         -#define OP_SorterCompare 126 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 14944         -#define OP_SorterData    127 /* synopsis: r[P2]=data                       */
 14945         -#define OP_RowData       128 /* synopsis: r[P2]=data                       */
 14946         -#define OP_Rowid         129 /* synopsis: r[P2]=rowid                      */
 14947         -#define OP_NullRow       130
 14948         -#define OP_SeekEnd       131
 14949         -#define OP_SorterInsert  132 /* synopsis: key=r[P2]                        */
 14950         -#define OP_IdxInsert     133 /* synopsis: key=r[P2]                        */
 14951         -#define OP_IdxDelete     134 /* synopsis: key=r[P2@P3]                     */
 14952         -#define OP_DeferredSeek  135 /* synopsis: Move P3 to P1.rowid if needed    */
 14953         -#define OP_IdxRowid      136 /* synopsis: r[P2]=rowid                      */
 14954         -#define OP_Destroy       137
 14955         -#define OP_Clear         138
 14956         -#define OP_ResetSorter   139
 14957         -#define OP_CreateBtree   140 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 14958         -#define OP_Real          141 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 14959         -#define OP_SqlExec       142
 14960         -#define OP_ParseSchema   143
 14961         -#define OP_LoadAnalysis  144
 14962         -#define OP_DropTable     145
 14963         -#define OP_DropIndex     146
 14964         -#define OP_DropTrigger   147
 14965         -#define OP_IntegrityCk   148
 14966         -#define OP_RowSetAdd     149 /* synopsis: rowset(P1)=r[P2]                 */
 14967         -#define OP_Param         150
 14968         -#define OP_FkCounter     151 /* synopsis: fkctr[P1]+=P2                    */
 14969         -#define OP_MemMax        152 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 14970         -#define OP_OffsetLimit   153 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 14971         -#define OP_AggInverse    154 /* synopsis: accum=r[P3] inverse(r[P2@P5])    */
 14972         -#define OP_AggStep       155 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 14973         -#define OP_AggStep1      156 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 14974         -#define OP_AggValue      157 /* synopsis: r[P3]=value N=P2                 */
 14975         -#define OP_AggFinal      158 /* synopsis: accum=r[P1] N=P2                 */
 14976         -#define OP_Expire        159
 14977         -#define OP_TableLock     160 /* synopsis: iDb=P1 root=P2 write=P3          */
 14978         -#define OP_VBegin        161
 14979         -#define OP_VCreate       162
 14980         -#define OP_VDestroy      163
 14981         -#define OP_VOpen         164
 14982         -#define OP_VColumn       165 /* synopsis: r[P3]=vcolumn(P2)                */
 14983         -#define OP_VRename       166
 14984         -#define OP_Pagecount     167
 14985         -#define OP_MaxPgcnt      168
 14986         -#define OP_Trace         169
 14987         -#define OP_CursorHint    170
 14988         -#define OP_Noop          171
 14989         -#define OP_Explain       172
 14990         -#define OP_Abortable     173
        14969  +#define OP_Delete        123
        14970  +#define OP_ResetCount    124
        14971  +#define OP_SorterCompare 125 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
        14972  +#define OP_SorterData    126 /* synopsis: r[P2]=data                       */
        14973  +#define OP_RowData       127 /* synopsis: r[P2]=data                       */
        14974  +#define OP_Rowid         128 /* synopsis: r[P2]=rowid                      */
        14975  +#define OP_NullRow       129
        14976  +#define OP_SeekEnd       130
        14977  +#define OP_SorterInsert  131 /* synopsis: key=r[P2]                        */
        14978  +#define OP_IdxInsert     132 /* synopsis: key=r[P2]                        */
        14979  +#define OP_IdxDelete     133 /* synopsis: key=r[P2@P3]                     */
        14980  +#define OP_DeferredSeek  134 /* synopsis: Move P3 to P1.rowid if needed    */
        14981  +#define OP_IdxRowid      135 /* synopsis: r[P2]=rowid                      */
        14982  +#define OP_Destroy       136
        14983  +#define OP_Clear         137
        14984  +#define OP_ResetSorter   138
        14985  +#define OP_CreateBtree   139 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
        14986  +#define OP_SqlExec       140
        14987  +#define OP_ParseSchema   141
        14988  +#define OP_LoadAnalysis  142
        14989  +#define OP_DropTable     143
        14990  +#define OP_DropIndex     144
        14991  +#define OP_Real          145 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
        14992  +#define OP_DropTrigger   146
        14993  +#define OP_IntegrityCk   147
        14994  +#define OP_RowSetAdd     148 /* synopsis: rowset(P1)=r[P2]                 */
        14995  +#define OP_Param         149
        14996  +#define OP_FkCounter     150 /* synopsis: fkctr[P1]+=P2                    */
        14997  +#define OP_MemMax        151 /* synopsis: r[P1]=max(r[P1],r[P2])           */
        14998  +#define OP_OffsetLimit   152 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
        14999  +#define OP_AggInverse    153 /* synopsis: accum=r[P3] inverse(r[P2@P5])    */
        15000  +#define OP_AggStep       154 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        15001  +#define OP_AggStep1      155 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        15002  +#define OP_AggValue      156 /* synopsis: r[P3]=value N=P2                 */
        15003  +#define OP_AggFinal      157 /* synopsis: accum=r[P1] N=P2                 */
        15004  +#define OP_Expire        158
        15005  +#define OP_TableLock     159 /* synopsis: iDb=P1 root=P2 write=P3          */
        15006  +#define OP_VBegin        160
        15007  +#define OP_VCreate       161
        15008  +#define OP_VDestroy      162
        15009  +#define OP_VOpen         163
        15010  +#define OP_VColumn       164 /* synopsis: r[P3]=vcolumn(P2)                */
        15011  +#define OP_VRename       165
        15012  +#define OP_Pagecount     166
        15013  +#define OP_MaxPgcnt      167
        15014  +#define OP_Trace         168
        15015  +#define OP_CursorHint    169
        15016  +#define OP_Noop          170
        15017  +#define OP_Explain       171
        15018  +#define OP_Abortable     172
 14991  15019   
 14992  15020   /* Properties such as "out2" or "jump" that are specified in
 14993  15021   ** comments following the "case" for each opcode in the vdbe.c
 14994  15022   ** are encoded into bitvectors as follows:
 14995  15023   */
 14996  15024   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 14997  15025   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 15007  15035   /*  32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
 15008  15036   /*  40 */ 0x01, 0x23, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03,\
 15009  15037   /*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 15010  15038   /*  56 */ 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,\
 15011  15039   /*  64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\
 15012  15040   /*  72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
 15013  15041   /*  80 */ 0x10, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
 15014         -/*  88 */ 0x12, 0x20, 0x00, 0x00, 0x26, 0x26, 0x26, 0x26,\
 15015         -/*  96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 15016         -/* 104 */ 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
        15042  +/*  88 */ 0x12, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
        15043  +/*  96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
        15044  +/* 104 */ 0x26, 0x26, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00,\
 15017  15045   /* 112 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 15018  15046   /* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 15019         -/* 128 */ 0x00, 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00,\
 15020         -/* 136 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 15021         -/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00,\
 15022         -/* 152 */ 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 15023         -/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
 15024         -/* 168 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,}
        15047  +/* 128 */ 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\
        15048  +/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
        15049  +/* 144 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
        15050  +/* 152 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
        15051  +/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
        15052  +/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00,}
 15025  15053   
 15026  15054   /* The sqlite3P2Values() routine is able to run faster if it knows
 15027  15055   ** the value of the largest JUMP opcode.  The smaller the maximum
 15028  15056   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 15029  15057   ** generated this include file strives to group all JUMP opcodes
 15030  15058   ** together near the beginning of the list.
 15031  15059   */
................................................................................
 16322  16350   #endif
 16323  16351     void *pCommitArg;                 /* Argument to xCommitCallback() */
 16324  16352     int (*xCommitCallback)(void*);    /* Invoked at every commit. */
 16325  16353     void *pRollbackArg;               /* Argument to xRollbackCallback() */
 16326  16354     void (*xRollbackCallback)(void*); /* Invoked at every commit. */
 16327  16355     void *pUpdateArg;
 16328  16356     void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
        16357  +  Parse *pParse;                /* Current parse */
 16329  16358   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 16330  16359     void *pPreUpdateArg;          /* First argument to xPreUpdateCallback */
 16331  16360     void (*xPreUpdateCallback)(   /* Registered using sqlite3_preupdate_hook() */
 16332  16361       void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
 16333  16362     );
 16334  16363     PreUpdate *pPreUpdate;        /* Context for active pre-update callback */
 16335  16364   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
................................................................................
 16455  16484   
 16456  16485   /*
 16457  16486   ** Allowed values for sqlite3.mDbFlags
 16458  16487   */
 16459  16488   #define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
 16460  16489   #define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
 16461  16490   #define DBFLAG_Vacuum         0x0004  /* Currently in a VACUUM */
 16462         -#define DBFLAG_SchemaKnownOk  0x0008  /* Schema is known to be valid */
        16491  +#define DBFLAG_VacuumInto     0x0008  /* Currently running VACUUM INTO */
        16492  +#define DBFLAG_SchemaKnownOk  0x0010  /* Schema is known to be valid */
 16463  16493   
 16464  16494   /*
 16465  16495   ** Bits of the sqlite3.dbOptFlags field that are used by the
 16466  16496   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
 16467  16497   ** selectively disable various optimizations.
 16468  16498   */
 16469  16499   #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
 16470         -                          /*  0x0002   available for reuse */
        16500  +#define SQLITE_WindowFunc     0x0002   /* Use xInverse for window functions */
 16471  16501   #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
 16472  16502   #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
 16473  16503   #define SQLITE_DistinctOpt    0x0010   /* DISTINCT using indexes */
 16474  16504   #define SQLITE_CoverIdxScan   0x0020   /* Covering index scans */
 16475  16505   #define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
 16476  16506   #define SQLITE_Transitive     0x0080   /* Transitive constraints */
 16477  16507   #define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
................................................................................
 16581  16611   #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
 16582  16612   #define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
 16583  16613   #define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
 16584  16614                                       ** single query - might change over time */
 16585  16615   #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
 16586  16616   #define SQLITE_FUNC_OFFSET   0x8000 /* Built-in sqlite_offset() function */
 16587  16617   #define SQLITE_FUNC_WINDOW   0x00010000 /* Built-in window-only function */
 16588         -#define SQLITE_FUNC_WINDOW_SIZE 0x20000 /* Requires partition size as arg. */
 16589  16618   #define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
 16590  16619   
 16591  16620   /*
 16592  16621   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 16593  16622   ** used to create the initializers for the FuncDef structures.
 16594  16623   **
 16595  16624   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
................................................................................
 17387  17416         int regReturn;         /* Register used to hold return address */
 17388  17417       } sub;
 17389  17418     } y;
 17390  17419   };
 17391  17420   
 17392  17421   /*
 17393  17422   ** The following are the meanings of bits in the Expr.flags field.
        17423  +** Value restrictions:
        17424  +**
        17425  +**          EP_Agg == NC_HasAgg == SF_HasAgg
        17426  +**          EP_Win == NC_HasWin
 17394  17427   */
 17395  17428   #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
 17396         -#define EP_Agg       0x000002 /* Contains one or more aggregate functions */
        17429  +#define EP_Distinct  0x000002 /* Aggregate function with DISTINCT keyword */
 17397  17430   #define EP_HasFunc   0x000004 /* Contains one or more functions of any kind */
 17398  17431   #define EP_FixedCol  0x000008 /* TK_Column with a known fixed value */
 17399         -#define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
        17432  +#define EP_Agg       0x000010 /* Contains one or more aggregate functions */
 17400  17433   #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
 17401  17434   #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
 17402  17435   #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
 17403  17436   #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
 17404  17437   #define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
 17405  17438   #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
 17406  17439   #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
 17407  17440   #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
 17408  17441   #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
 17409  17442   #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
 17410         -#define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
        17443  +#define EP_Win       0x008000 /* Contains window functions */
 17411  17444   #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
 17412  17445   #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
 17413  17446   #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
 17414  17447   #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
 17415  17448   #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
 17416  17449   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
 17417  17450   #define EP_Alias     0x400000 /* Is an alias for a result set column */
 17418  17451   #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
 17419  17452   #define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
 17420  17453   #define EP_Subrtn   0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
 17421  17454   #define EP_Quoted   0x4000000 /* TK_ID was originally quoted */
        17455  +#define EP_Static   0x8000000 /* Held in memory not obtained from malloc() */
 17422  17456   
 17423  17457   /*
 17424  17458   ** The EP_Propagate mask is a set of properties that automatically propagate
 17425  17459   ** upwards into parent nodes.
 17426  17460   */
 17427  17461   #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
 17428  17462   
................................................................................
 17654  17688     Select *pWinSelect;  /* SELECT statement for any window functions */
 17655  17689   };
 17656  17690   
 17657  17691   /*
 17658  17692   ** Allowed values for the NameContext, ncFlags field.
 17659  17693   **
 17660  17694   ** Value constraints (all checked via assert()):
 17661         -**    NC_HasAgg    == SF_HasAgg
        17695  +**    NC_HasAgg    == SF_HasAgg    == EP_Agg
 17662  17696   **    NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
        17697  +**    NC_HasWin    == EP_Win
 17663  17698   **
 17664  17699   */
 17665  17700   #define NC_AllowAgg  0x0001  /* Aggregate functions are allowed here */
 17666  17701   #define NC_PartIdx   0x0002  /* True if resolving a partial index WHERE */
 17667  17702   #define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
 17668  17703   #define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
 17669  17704   #define NC_HasAgg    0x0010  /* One or more aggregate functions seen */
................................................................................
 17671  17706   #define NC_VarSelect 0x0040  /* A correlated subquery has been seen */
 17672  17707   #define NC_UEList    0x0080  /* True if uNC.pEList is used */
 17673  17708   #define NC_UAggInfo  0x0100  /* True if uNC.pAggInfo is used */
 17674  17709   #define NC_UUpsert   0x0200  /* True if uNC.pUpsert is used */
 17675  17710   #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
 17676  17711   #define NC_Complex   0x2000  /* True if a function or subquery seen */
 17677  17712   #define NC_AllowWin  0x4000  /* Window functions are allowed here */
        17713  +#define NC_HasWin    0x8000  /* One or more window functions seen */
 17678  17714   
 17679  17715   /*
 17680  17716   ** An instance of the following object describes a single ON CONFLICT
 17681  17717   ** clause in an upsert.
 17682  17718   **
 17683  17719   ** The pUpsertTarget field is only set if the ON CONFLICT clause includes
 17684  17720   ** conflict-target clause.  (In "ON CONFLICT(a,b)" the "(a,b)" is the
................................................................................
 17985  18021   #ifndef SQLITE_OMIT_SHARED_CACHE
 17986  18022     int nTableLock;        /* Number of locks in aTableLock */
 17987  18023     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 17988  18024   #endif
 17989  18025     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 17990  18026     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
 17991  18027     Table *pTriggerTab;  /* Table triggers are being coded for */
        18028  +  Parse *pParentParse; /* Parent parser if this parser is nested */
 17992  18029     int addrCrTab;       /* Address of OP_CreateBtree opcode on CREATE TABLE */
 17993  18030     u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
 17994  18031     u32 oldmask;         /* Mask of old.* columns referenced */
 17995  18032     u32 newmask;         /* Mask of new.* columns referenced */
 17996  18033     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
 17997  18034     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
 17998  18035     u8 disableTriggers;  /* True to disable triggers */
................................................................................
 18425  18462   struct TreeView {
 18426  18463     int iLevel;             /* Which level of the tree we are on */
 18427  18464     u8  bLine[100];         /* Draw vertical in column i if bLine[i] is true */
 18428  18465   };
 18429  18466   #endif /* SQLITE_DEBUG */
 18430  18467   
 18431  18468   /*
 18432         -** This object is used in varioius ways, all related to window functions
        18469  +** This object is used in various ways, all related to window functions
 18433  18470   **
 18434  18471   **   (1) A single instance of this structure is attached to the
 18435  18472   **       the Expr.pWin field for each window function in an expression tree.
 18436  18473   **       This object holds the information contained in the OVER clause,
 18437  18474   **       plus additional fields used during code generation.
 18438  18475   **
 18439  18476   **   (2) All window functions in a single SELECT form a linked-list
................................................................................
 18440  18477   **       attached to Select.pWin.  The Window.pFunc and Window.pExpr
 18441  18478   **       fields point back to the expression that is the window function.
 18442  18479   **
 18443  18480   **   (3) The terms of the WINDOW clause of a SELECT are instances of this
 18444  18481   **       object on a linked list attached to Select.pWinDefn.
 18445  18482   **
 18446  18483   ** The uses (1) and (2) are really the same Window object that just happens
 18447         -** to be accessible in two different ways.  Use (3) is are separate objects.
        18484  +** to be accessible in two different ways.  Use case (3) are separate objects.
 18448  18485   */
 18449  18486   struct Window {
 18450  18487     char *zName;            /* Name of window (may be NULL) */
        18488  +  char *zBase;            /* Name of base window for chaining (may be NULL) */
 18451  18489     ExprList *pPartition;   /* PARTITION BY clause */
 18452  18490     ExprList *pOrderBy;     /* ORDER BY clause */
 18453         -  u8 eType;               /* TK_RANGE or TK_ROWS */
        18491  +  u8 eFrmType;            /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
 18454  18492     u8 eStart;              /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
 18455  18493     u8 eEnd;                /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
        18494  +  u8 bImplicitFrame;      /* True if frame was implicitly specified */
        18495  +  u8 eExclude;            /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
 18456  18496     Expr *pStart;           /* Expression for "<expr> PRECEDING" */
 18457  18497     Expr *pEnd;             /* Expression for "<expr> FOLLOWING" */
 18458  18498     Window *pNextWin;       /* Next window function belonging to this SELECT */
 18459  18499     Expr *pFilter;          /* The FILTER expression */
 18460  18500     FuncDef *pFunc;         /* The function */
 18461  18501     int iEphCsr;            /* Partition buffer or Peer buffer */
 18462  18502     int regAccum;
 18463  18503     int regResult;
 18464  18504     int csrApp;             /* Function cursor (used by min/max) */
 18465  18505     int regApp;             /* Function register (also used by min/max) */
 18466         -  int regPart;            /* First in a set of registers holding PARTITION BY
 18467         -                          ** and ORDER BY values for the window */
        18506  +  int regPart;            /* Array of registers for PARTITION BY values */
 18468  18507     Expr *pOwner;           /* Expression object this window is attached to */
 18469  18508     int nBufferCol;         /* Number of columns in buffer table */
 18470  18509     int iArgCol;            /* Offset of first argument for this function */
        18510  +  int regOne;             /* Register containing constant value 1 */
        18511  +  int regStartRowid;
        18512  +  int regEndRowid;
 18471  18513   };
 18472  18514   
 18473  18515   #ifndef SQLITE_OMIT_WINDOWFUNC
 18474  18516   SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
 18475  18517   SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
 18476         -SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*);
        18518  +SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
 18477  18519   SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
 18478  18520   SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*);
 18479  18521   SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Window*);
 18480  18522   SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
 18481  18523   SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
 18482  18524   SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
 18483  18525   SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
 18484  18526   SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
 18485  18527   SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
 18486  18528   SQLITE_PRIVATE void sqlite3WindowFunctions(void);
        18529  +SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
        18530  +SQLITE_PRIVATE Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
 18487  18531   #else
 18488  18532   # define sqlite3WindowDelete(a,b)
 18489  18533   # define sqlite3WindowFunctions()
 18490  18534   # define sqlite3WindowAttach(a,b,c)
 18491  18535   #endif
 18492  18536   
 18493  18537   /*
................................................................................
 18709  18753   SQLITE_PRIVATE   void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
 18710  18754   #endif
 18711  18755   #endif
 18712  18756   
 18713  18757   
 18714  18758   SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
 18715  18759   SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
        18760  +SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int);
 18716  18761   SQLITE_PRIVATE void sqlite3Dequote(char*);
 18717  18762   SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
 18718  18763   SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
 18719  18764   SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
 18720  18765   SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
 18721  18766   SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
 18722  18767   SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
................................................................................
 19170  19215   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 19171  19216   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 19172  19217   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 19173  19218   SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
 19174  19219   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 19175  19220   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 19176  19221   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
 19177         -SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int, int);
        19222  +SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
 19178  19223   SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
 19179  19224   SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 19180  19225   SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
 19181  19226   SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
 19182  19227   SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
 19183  19228   SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
 19184  19229   SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
................................................................................
 20132  20177   */
 20133  20178   #define MEM_Null      0x0001   /* Value is NULL (or a pointer) */
 20134  20179   #define MEM_Str       0x0002   /* Value is a string */
 20135  20180   #define MEM_Int       0x0004   /* Value is an integer */
 20136  20181   #define MEM_Real      0x0008   /* Value is a real number */
 20137  20182   #define MEM_Blob      0x0010   /* Value is a BLOB */
 20138  20183   #define MEM_AffMask   0x001f   /* Mask of affinity bits */
 20139         -/* Available          0x0020   */
        20184  +#define MEM_FromBind  0x0020   /* Value originates from sqlite3_bind() */
 20140  20185   /* Available          0x0040   */
 20141  20186   #define MEM_Undefined 0x0080   /* Value is undefined */
 20142  20187   #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
 20143         -#define MEM_TypeMask  0xc1ff   /* Mask of type bits */
        20188  +#define MEM_TypeMask  0xc1df   /* Mask of type bits */
 20144  20189   
 20145  20190   
 20146  20191   /* Whenever Mem contains a valid string or blob representation, one of
 20147  20192   ** the following flags must be set to determine the memory management
 20148  20193   ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
 20149  20194   ** string is \000 or \u0000 terminated
 20150  20195   */
................................................................................
 20168  20213   
 20169  20214   /*
 20170  20215   ** Clear any existing type flags from a Mem and replace them with f
 20171  20216   */
 20172  20217   #define MemSetTypeFlag(p, f) \
 20173  20218      ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
 20174  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  +
 20175  20226   /*
 20176  20227   ** Return true if a memory cell is not marked as invalid.  This macro
 20177  20228   ** is for use inside assert() statements only.
 20178  20229   */
 20179  20230   #ifdef SQLITE_DEBUG
 20180  20231   #define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
 20181  20232   #endif
................................................................................
 27117  27168   SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
 27118  27169     if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
 27119  27170       db->mallocFailed = 1;
 27120  27171       if( db->nVdbeExec>0 ){
 27121  27172         db->u1.isInterrupted = 1;
 27122  27173       }
 27123  27174       db->lookaside.bDisable++;
        27175  +    if( db->pParse ){
        27176  +      db->pParse->rc = SQLITE_NOMEM_BKPT;
        27177  +    }
 27124  27178     }
 27125  27179   }
 27126  27180   
 27127  27181   /*
 27128  27182   ** This routine reactivates the memory allocator and clears the
 27129  27183   ** db->mallocFailed flag as necessary.
 27130  27184   **
................................................................................
 27310  27364   
 27311  27365   /*
 27312  27366   ** Set the StrAccum object to an error mode.
 27313  27367   */
 27314  27368   static void setStrAccumError(StrAccum *p, u8 eError){
 27315  27369     assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
 27316  27370     p->accError = eError;
 27317         -  p->nAlloc = 0;
        27371  +  if( p->mxAlloc ) sqlite3_str_reset(p);
        27372  +  if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
 27318  27373   }
 27319  27374   
 27320  27375   /*
 27321  27376   ** Extra argument values from a PrintfArguments object
 27322  27377   */
 27323  27378   static sqlite3_int64 getIntArg(PrintfArguments *p){
 27324  27379     if( p->nArg<=p->nUsed ) return 0;
................................................................................
 27340  27395   ** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
 27341  27396   ** Do the size check before the memory allocation to prevent rogue
 27342  27397   ** SQL from requesting large allocations using the precision or width
 27343  27398   ** field of the printf() function.
 27344  27399   */
 27345  27400   static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
 27346  27401     char *z;
        27402  +  if( pAccum->accError ) return 0;
 27347  27403     if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
 27348  27404       setStrAccumError(pAccum, SQLITE_TOOBIG);
 27349  27405       return 0;
 27350  27406     }
 27351  27407     z = sqlite3DbMallocRaw(pAccum->db, n);
 27352  27408     if( z==0 ){
 27353  27409       setStrAccumError(pAccum, SQLITE_NOMEM);
................................................................................
 28059  28115     assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
 28060  28116     if( p->accError ){
 28061  28117       testcase(p->accError==SQLITE_TOOBIG);
 28062  28118       testcase(p->accError==SQLITE_NOMEM);
 28063  28119       return 0;
 28064  28120     }
 28065  28121     if( p->mxAlloc==0 ){
 28066         -    N = p->nAlloc - p->nChar - 1;
 28067  28122       setStrAccumError(p, SQLITE_TOOBIG);
 28068         -    return N;
        28123  +    return p->nAlloc - p->nChar - 1;
 28069  28124     }else{
 28070  28125       char *zOld = isMalloced(p) ? p->zText : 0;
 28071  28126       i64 szNew = p->nChar;
 28072  28127       szNew += N + 1;
 28073  28128       if( szNew+p->nChar<=p->mxAlloc ){
 28074  28129         /* Force exponential buffer size growth as long as it does not overflow,
 28075  28130         ** to avoid having to call this routine too often */
................................................................................
 28133  28188   ** Append N bytes of text from z to the StrAccum object.  Increase the
 28134  28189   ** size of the memory allocation for StrAccum if necessary.
 28135  28190   */
 28136  28191   SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
 28137  28192     assert( z!=0 || N==0 );
 28138  28193     assert( p->zText!=0 || p->nChar==0 || p->accError );
 28139  28194     assert( N>=0 );
 28140         -  assert( p->accError==0 || p->nAlloc==0 );
        28195  +  assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
 28141  28196     if( p->nChar+N >= p->nAlloc ){
 28142  28197       enlargeAndAppend(p,z,N);
 28143  28198     }else if( N ){
 28144  28199       assert( p->zText );
 28145  28200       p->nChar += N;
 28146  28201       memcpy(&p->zText[p->nChar-N], z, N);
 28147  28202     }
................................................................................
 28766  28821   #endif /* SQLITE_OMIT_WINDOWFUNC */
 28767  28822   
 28768  28823   #ifndef SQLITE_OMIT_WINDOWFUNC
 28769  28824   /*
 28770  28825   ** Generate a human-readable explanation for a Window object
 28771  28826   */
 28772  28827   SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
        28828  +  int nElement = 0;
        28829  +  if( pWin->pFilter ){
        28830  +    sqlite3TreeViewItem(pView, "FILTER", 1);
        28831  +    sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
        28832  +    sqlite3TreeViewPop(pView);
        28833  +  }
 28773  28834     pView = sqlite3TreeViewPush(pView, more);
 28774  28835     if( pWin->zName ){
 28775         -    sqlite3TreeViewLine(pView, "OVER %s", pWin->zName);
        28836  +    sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
 28776  28837     }else{
 28777         -    sqlite3TreeViewLine(pView, "OVER");
        28838  +    sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
        28839  +  }
        28840  +  if( pWin->zBase )    nElement++;
        28841  +  if( pWin->pOrderBy ) nElement++;
        28842  +  if( pWin->eFrmType ) nElement++;
        28843  +  if( pWin->eExclude ) nElement++;
        28844  +  if( pWin->zBase ){
        28845  +    sqlite3TreeViewPush(pView, (--nElement)>0);
        28846  +    sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
        28847  +    sqlite3TreeViewPop(pView);
 28778  28848     }
 28779  28849     if( pWin->pPartition ){
 28780         -    sqlite3TreeViewExprList(pView, pWin->pPartition, 1, "PARTITION-BY");
        28850  +    sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
 28781  28851     }
 28782  28852     if( pWin->pOrderBy ){
 28783         -    sqlite3TreeViewExprList(pView, pWin->pOrderBy, 1, "ORDER-BY");
        28853  +    sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
 28784  28854     }
 28785         -  if( pWin->eType ){
 28786         -    sqlite3TreeViewItem(pView, pWin->eType==TK_RANGE ? "RANGE" : "ROWS", 0);
        28855  +  if( pWin->eFrmType ){
        28856  +    char zBuf[30];
        28857  +    const char *zFrmType = "ROWS";
        28858  +    if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
        28859  +    if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
        28860  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
        28861  +        pWin->bImplicitFrame ? " (implied)" : "");
        28862  +    sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
 28787  28863       sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
 28788  28864       sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
 28789  28865       sqlite3TreeViewPop(pView);
 28790  28866     }
        28867  +  if( pWin->eExclude ){
        28868  +    char zBuf[30];
        28869  +    const char *zExclude;
        28870  +    switch( pWin->eExclude ){
        28871  +      case TK_NO:      zExclude = "NO OTHERS";   break;
        28872  +      case TK_CURRENT: zExclude = "CURRENT ROW"; break;
        28873  +      case TK_GROUP:   zExclude = "GROUP";       break;
        28874  +      case TK_TIES:    zExclude = "TIES";        break;
        28875  +      default:
        28876  +        sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
        28877  +        zExclude = zBuf;
        28878  +        break;
        28879  +    }
        28880  +    sqlite3TreeViewPush(pView, 0);
        28881  +    sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
        28882  +    sqlite3TreeViewPop(pView);
        28883  +  }
 28791  28884     sqlite3TreeViewPop(pView);
 28792  28885   }
 28793  28886   #endif /* SQLITE_OMIT_WINDOWFUNC */
 28794  28887   
 28795  28888   #ifndef SQLITE_OMIT_WINDOWFUNC
 28796  28889   /*
 28797  28890   ** Generate a human-readable explanation for a Window Function object
................................................................................
 29763  29856   #ifndef SQLITE_OMIT_UTF16
 29764  29857   /*
 29765  29858   ** This routine transforms the internal text encoding used by pMem to
 29766  29859   ** desiredEnc. It is an error if the string is already of the desired
 29767  29860   ** encoding, or if *pMem does not contain a string value.
 29768  29861   */
 29769  29862   SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
 29770         -  int len;                    /* Maximum length of output string in bytes */
 29771         -  unsigned char *zOut;                  /* Output buffer */
 29772         -  unsigned char *zIn;                   /* Input iterator */
 29773         -  unsigned char *zTerm;                 /* End of input */
 29774         -  unsigned char *z;                     /* Output iterator */
        29863  +  sqlite3_int64 len;          /* Maximum length of output string in bytes */
        29864  +  unsigned char *zOut;        /* Output buffer */
        29865  +  unsigned char *zIn;         /* Input iterator */
        29866  +  unsigned char *zTerm;       /* End of input */
        29867  +  unsigned char *z;           /* Output iterator */
 29775  29868     unsigned int c;
 29776  29869   
 29777  29870     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 29778  29871     assert( pMem->flags&MEM_Str );
 29779  29872     assert( pMem->enc!=desiredEnc );
 29780  29873     assert( pMem->enc!=0 );
 29781  29874     assert( pMem->n>=0 );
................................................................................
 29816  29909     if( desiredEnc==SQLITE_UTF8 ){
 29817  29910       /* When converting from UTF-16, the maximum growth results from
 29818  29911       ** translating a 2-byte character to a 4-byte UTF-8 character.
 29819  29912       ** A single byte is required for the output string
 29820  29913       ** nul-terminator.
 29821  29914       */
 29822  29915       pMem->n &= ~1;
 29823         -    len = pMem->n * 2 + 1;
        29916  +    len = 2 * (sqlite3_int64)pMem->n + 1;
 29824  29917     }else{
 29825  29918       /* When converting from UTF-8 to UTF-16 the maximum growth is caused
 29826  29919       ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
 29827  29920       ** character. Two bytes are required in the output buffer for the
 29828  29921       ** nul-terminator.
 29829  29922       */
 29830         -    len = pMem->n * 2 + 2;
        29923  +    len = 2 * (sqlite3_int64)pMem->n + 2;
 29831  29924     }
 29832  29925   
 29833  29926     /* Set zIn to point at the start of the input buffer and zTerm to point 1
 29834  29927     ** byte past the end.
 29835  29928     **
 29836  29929     ** Variable zOut is set to point at the output buffer, space obtained
 29837  29930     ** from sqlite3_malloc().
................................................................................
 30130  30223   SQLITE_PRIVATE void sqlite3Coverage(int x){
 30131  30224     static unsigned dummy = 0;
 30132  30225     dummy += (unsigned)x;
 30133  30226   }
 30134  30227   #endif
 30135  30228   
 30136  30229   /*
 30137         -** Give a callback to the test harness that can be used to simulate faults
 30138         -** in places where it is difficult or expensive to do so purely by means
 30139         -** of inputs.
        30230  +** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
        30231  +** or to bypass normal error detection during testing in order to let 
        30232  +** execute proceed futher downstream.
 30140  30233   **
 30141         -** The intent of the integer argument is to let the fault simulator know
 30142         -** which of multiple sqlite3FaultSim() calls has been hit.
        30234  +** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0).  The
        30235  +** sqlite3FaultSim() function only returns non-zero during testing.
 30143  30236   **
 30144         -** Return whatever integer value the test callback returns, or return
 30145         -** SQLITE_OK if no test callback is installed.
        30237  +** During testing, if the test harness has set a fault-sim callback using
        30238  +** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
        30239  +** each call to sqlite3FaultSim() is relayed to that application-supplied
        30240  +** callback and the integer return value form the application-supplied
        30241  +** callback is returned by sqlite3FaultSim().
        30242  +**
        30243  +** The integer argument to sqlite3FaultSim() is a code to identify which
        30244  +** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
        30245  +** should have a unique code.  To prevent legacy testing applications from
        30246  +** breaking, the codes should not be changed or reused.
 30146  30247   */
 30147  30248   #ifndef SQLITE_UNTESTABLE
 30148  30249   SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
 30149  30250     int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
 30150  30251     return xCallback ? xCallback(iTest) : SQLITE_OK;
 30151  30252   }
 30152  30253   #endif
................................................................................
 30322  30423     }else{
 30323  30424       pParse->nErr++;
 30324  30425       sqlite3DbFree(db, pParse->zErrMsg);
 30325  30426       pParse->zErrMsg = zMsg;
 30326  30427       pParse->rc = SQLITE_ERROR;
 30327  30428     }
 30328  30429   }
        30430  +
        30431  +/*
        30432  +** If database connection db is currently parsing SQL, then transfer
        30433  +** error code errCode to that parser if the parser has not already
        30434  +** encountered some other kind of error.
        30435  +*/
        30436  +SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3 *db, int errCode){
        30437  +  Parse *pParse;
        30438  +  if( db==0 || (pParse = db->pParse)==0 ) return errCode;
        30439  +  pParse->rc = errCode;
        30440  +  pParse->nErr++;
        30441  +  return errCode;
        30442  +}
 30329  30443   
 30330  30444   /*
 30331  30445   ** Convert an SQL-style quoted string into a normal string by removing
 30332  30446   ** the quote characters.  The conversion is done in-place.  If the
 30333  30447   ** input does not begin with a quote character, then this routine
 30334  30448   ** is a no-op.
 30335  30449   **
................................................................................
 31674  31788     char *z;               /* Pointer to where zName will be stored */
 31675  31789     int i;                 /* Index in pIn[] where zName is stored */
 31676  31790   
 31677  31791     nInt = nName/4 + 3;
 31678  31792     assert( pIn==0 || pIn[0]>=3 );  /* Verify ok to add new elements */
 31679  31793     if( pIn==0 || pIn[1]+nInt > pIn[0] ){
 31680  31794       /* Enlarge the allocation */
 31681         -    int nAlloc = (pIn ? pIn[0]*2 : 10) + nInt;
        31795  +    sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
 31682  31796       VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
 31683  31797       if( pOut==0 ) return pIn;
 31684  31798       if( pIn==0 ) pOut[1] = 2;
 31685  31799       pIn = pOut;
 31686  31800       pIn[0] = nAlloc;
 31687  31801     }
 31688  31802     i = pIn[1];
................................................................................
 31880  31994   */
 31881  31995   static HashElem *findElementWithHash(
 31882  31996     const Hash *pH,     /* The pH to be searched */
 31883  31997     const char *pKey,   /* The key we are searching for */
 31884  31998     unsigned int *pHash /* Write the hash value here */
 31885  31999   ){
 31886  32000     HashElem *elem;                /* Used to loop thru the element list */
 31887         -  int count;                     /* Number of elements left to test */
        32001  +  unsigned int count;            /* Number of elements left to test */
 31888  32002     unsigned int h;                /* The computed hash */
 31889  32003     static HashElem nullElement = { 0, 0, 0, 0 };
 31890  32004   
 31891  32005     if( pH->ht ){   /*OPTIMIZATION-IF-TRUE*/
 31892  32006       struct _ht *pEntry;
 31893  32007       h = strHash(pKey) % pH->htsize;
 31894  32008       pEntry = &pH->ht[h];
................................................................................
 31928  32042       elem->next->prev = elem->prev;
 31929  32043     }
 31930  32044     if( pH->ht ){
 31931  32045       pEntry = &pH->ht[h];
 31932  32046       if( pEntry->chain==elem ){
 31933  32047         pEntry->chain = elem->next;
 31934  32048       }
        32049  +    assert( pEntry->count>0 );
 31935  32050       pEntry->count--;
 31936         -    assert( pEntry->count>=0 );
 31937  32051     }
 31938  32052     sqlite3_free( elem );
 31939  32053     pH->count--;
 31940  32054     if( pH->count==0 ){
 31941  32055       assert( pH->first==0 );
 31942  32056       assert( pH->count==0 );
 31943  32057       sqlite3HashClear(pH);
................................................................................
 32104  32218       /*  85 */ "Cast"             OpHelp("affinity(r[P1])"),
 32105  32219       /*  86 */ "Permutation"      OpHelp(""),
 32106  32220       /*  87 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 32107  32221       /*  88 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
 32108  32222       /*  89 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
 32109  32223       /*  90 */ "Column"           OpHelp("r[P3]=PX"),
 32110  32224       /*  91 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 32111         -    /*  92 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 32112         -    /*  93 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 32113         -    /*  94 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 32114         -    /*  95 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 32115         -    /*  96 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 32116         -    /*  97 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 32117         -    /*  98 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 32118         -    /*  99 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 32119         -    /* 100 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 32120         -    /* 101 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 32121         -    /* 102 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 32122         -    /* 103 */ "BitNot"           OpHelp("r[P2]= ~r[P1]"),
 32123         -    /* 104 */ "Count"            OpHelp("r[P2]=count()"),
 32124         -    /* 105 */ "ReadCookie"       OpHelp(""),
 32125         -    /* 106 */ "String8"          OpHelp("r[P2]='P4'"),
 32126         -    /* 107 */ "SetCookie"        OpHelp(""),
 32127         -    /* 108 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 32128         -    /* 109 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 32129         -    /* 110 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        32225  +    /*  92 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        32226  +    /*  93 */ "Count"            OpHelp("r[P2]=count()"),
        32227  +    /*  94 */ "ReadCookie"       OpHelp(""),
        32228  +    /*  95 */ "SetCookie"        OpHelp(""),
        32229  +    /*  96 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
        32230  +    /*  97 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
        32231  +    /*  98 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
        32232  +    /*  99 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
        32233  +    /* 100 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
        32234  +    /* 101 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
        32235  +    /* 102 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
        32236  +    /* 103 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
        32237  +    /* 104 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
        32238  +    /* 105 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
        32239  +    /* 106 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        32240  +    /* 107 */ "BitNot"           OpHelp("r[P2]= ~r[P1]"),
        32241  +    /* 108 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        32242  +    /* 109 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        32243  +    /* 110 */ "String8"          OpHelp("r[P2]='P4'"),
 32130  32244       /* 111 */ "OpenDup"          OpHelp(""),
 32131  32245       /* 112 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 32132  32246       /* 113 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 32133  32247       /* 114 */ "SorterOpen"       OpHelp(""),
 32134  32248       /* 115 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 32135  32249       /* 116 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 32136  32250       /* 117 */ "Close"            OpHelp(""),
 32137  32251       /* 118 */ "ColumnsUsed"      OpHelp(""),
 32138  32252       /* 119 */ "SeekHit"          OpHelp("seekHit=P2"),
 32139  32253       /* 120 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 32140  32254       /* 121 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 32141  32255       /* 122 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 32142         -    /* 123 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 32143         -    /* 124 */ "Delete"           OpHelp(""),
 32144         -    /* 125 */ "ResetCount"       OpHelp(""),
 32145         -    /* 126 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 32146         -    /* 127 */ "SorterData"       OpHelp("r[P2]=data"),
 32147         -    /* 128 */ "RowData"          OpHelp("r[P2]=data"),
 32148         -    /* 129 */ "Rowid"            OpHelp("r[P2]=rowid"),
 32149         -    /* 130 */ "NullRow"          OpHelp(""),
 32150         -    /* 131 */ "SeekEnd"          OpHelp(""),
 32151         -    /* 132 */ "SorterInsert"     OpHelp("key=r[P2]"),
 32152         -    /* 133 */ "IdxInsert"        OpHelp("key=r[P2]"),
 32153         -    /* 134 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 32154         -    /* 135 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 32155         -    /* 136 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 32156         -    /* 137 */ "Destroy"          OpHelp(""),
 32157         -    /* 138 */ "Clear"            OpHelp(""),
 32158         -    /* 139 */ "ResetSorter"      OpHelp(""),
 32159         -    /* 140 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 32160         -    /* 141 */ "Real"             OpHelp("r[P2]=P4"),
 32161         -    /* 142 */ "SqlExec"          OpHelp(""),
 32162         -    /* 143 */ "ParseSchema"      OpHelp(""),
 32163         -    /* 144 */ "LoadAnalysis"     OpHelp(""),
 32164         -    /* 145 */ "DropTable"        OpHelp(""),
 32165         -    /* 146 */ "DropIndex"        OpHelp(""),
 32166         -    /* 147 */ "DropTrigger"      OpHelp(""),
 32167         -    /* 148 */ "IntegrityCk"      OpHelp(""),
 32168         -    /* 149 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 32169         -    /* 150 */ "Param"            OpHelp(""),
 32170         -    /* 151 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 32171         -    /* 152 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 32172         -    /* 153 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
 32173         -    /* 154 */ "AggInverse"       OpHelp("accum=r[P3] inverse(r[P2@P5])"),
 32174         -    /* 155 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 32175         -    /* 156 */ "AggStep1"         OpHelp("accum=r[P3] step(r[P2@P5])"),
 32176         -    /* 157 */ "AggValue"         OpHelp("r[P3]=value N=P2"),
 32177         -    /* 158 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 32178         -    /* 159 */ "Expire"           OpHelp(""),
 32179         -    /* 160 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 32180         -    /* 161 */ "VBegin"           OpHelp(""),
 32181         -    /* 162 */ "VCreate"          OpHelp(""),
 32182         -    /* 163 */ "VDestroy"         OpHelp(""),
 32183         -    /* 164 */ "VOpen"            OpHelp(""),
 32184         -    /* 165 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 32185         -    /* 166 */ "VRename"          OpHelp(""),
 32186         -    /* 167 */ "Pagecount"        OpHelp(""),
 32187         -    /* 168 */ "MaxPgcnt"         OpHelp(""),
 32188         -    /* 169 */ "Trace"            OpHelp(""),
 32189         -    /* 170 */ "CursorHint"       OpHelp(""),
 32190         -    /* 171 */ "Noop"             OpHelp(""),
 32191         -    /* 172 */ "Explain"          OpHelp(""),
 32192         -    /* 173 */ "Abortable"        OpHelp(""),
        32256  +    /* 123 */ "Delete"           OpHelp(""),
        32257  +    /* 124 */ "ResetCount"       OpHelp(""),
        32258  +    /* 125 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        32259  +    /* 126 */ "SorterData"       OpHelp("r[P2]=data"),
        32260  +    /* 127 */ "RowData"          OpHelp("r[P2]=data"),
        32261  +    /* 128 */ "Rowid"            OpHelp("r[P2]=rowid"),
        32262  +    /* 129 */ "NullRow"          OpHelp(""),
        32263  +    /* 130 */ "SeekEnd"          OpHelp(""),
        32264  +    /* 131 */ "SorterInsert"     OpHelp("key=r[P2]"),
        32265  +    /* 132 */ "IdxInsert"        OpHelp("key=r[P2]"),
        32266  +    /* 133 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        32267  +    /* 134 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
        32268  +    /* 135 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        32269  +    /* 136 */ "Destroy"          OpHelp(""),
        32270  +    /* 137 */ "Clear"            OpHelp(""),
        32271  +    /* 138 */ "ResetSorter"      OpHelp(""),
        32272  +    /* 139 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
        32273  +    /* 140 */ "SqlExec"          OpHelp(""),
        32274  +    /* 141 */ "ParseSchema"      OpHelp(""),
        32275  +    /* 142 */ "LoadAnalysis"     OpHelp(""),
        32276  +    /* 143 */ "DropTable"        OpHelp(""),
        32277  +    /* 144 */ "DropIndex"        OpHelp(""),
        32278  +    /* 145 */ "Real"             OpHelp("r[P2]=P4"),
        32279  +    /* 146 */ "DropTrigger"      OpHelp(""),
        32280  +    /* 147 */ "IntegrityCk"      OpHelp(""),
        32281  +    /* 148 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        32282  +    /* 149 */ "Param"            OpHelp(""),
        32283  +    /* 150 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        32284  +    /* 151 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        32285  +    /* 152 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
        32286  +    /* 153 */ "AggInverse"       OpHelp("accum=r[P3] inverse(r[P2@P5])"),
        32287  +    /* 154 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
        32288  +    /* 155 */ "AggStep1"         OpHelp("accum=r[P3] step(r[P2@P5])"),
        32289  +    /* 156 */ "AggValue"         OpHelp("r[P3]=value N=P2"),
        32290  +    /* 157 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        32291  +    /* 158 */ "Expire"           OpHelp(""),
        32292  +    /* 159 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        32293  +    /* 160 */ "VBegin"           OpHelp(""),
        32294  +    /* 161 */ "VCreate"          OpHelp(""),
        32295  +    /* 162 */ "VDestroy"         OpHelp(""),
        32296  +    /* 163 */ "VOpen"            OpHelp(""),
        32297  +    /* 164 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        32298  +    /* 165 */ "VRename"          OpHelp(""),
        32299  +    /* 166 */ "Pagecount"        OpHelp(""),
        32300  +    /* 167 */ "MaxPgcnt"         OpHelp(""),
        32301  +    /* 168 */ "Trace"            OpHelp(""),
        32302  +    /* 169 */ "CursorHint"       OpHelp(""),
        32303  +    /* 170 */ "Noop"             OpHelp(""),
        32304  +    /* 171 */ "Explain"          OpHelp(""),
        32305  +    /* 172 */ "Abortable"        OpHelp(""),
 32193  32306     };
 32194  32307     return azName[i];
 32195  32308   }
 32196  32309   #endif
 32197  32310   
 32198  32311   /************** End of opcodes.c *********************************************/
 32199  32312   /************** Begin file os_unix.c *****************************************/
................................................................................
 48617  48730   typedef struct PGroup PGroup;
 48618  48731   
 48619  48732   /*
 48620  48733   ** Each cache entry is represented by an instance of the following 
 48621  48734   ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
 48622  48735   ** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
 48623  48736   ** in memory.
        48737  +**
        48738  +** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
        48739  +** but causes a 2-byte gap in the structure for most architectures (since 
        48740  +** pointers must be either 4 or 8-byte aligned). As this structure is located
        48741  +** in memory directly after the associated page data, if the database is
        48742  +** corrupt, code at the b-tree layer may overread the page buffer and 
        48743  +** read part of this structure before the corruption is detected. This
        48744  +** can cause a valgrind error if the unitialized gap is accessed. Using u16
        48745  +** ensures there is no such gap, and therefore no bytes of unitialized memory
        48746  +** in the structure.
 48624  48747   */
 48625  48748   struct PgHdr1 {
 48626  48749     sqlite3_pcache_page page;      /* Base class. Must be first. pBuf & pExtra */
 48627  48750     unsigned int iKey;             /* Key value (page number) */
 48628         -  u8 isBulkLocal;                /* This page from bulk local storage */
 48629         -  u8 isAnchor;                   /* This is the PGroup.lru element */
        48751  +  u16 isBulkLocal;               /* This page from bulk local storage */
        48752  +  u16 isAnchor;                  /* This is the PGroup.lru element */
 48630  48753     PgHdr1 *pNext;                 /* Next in hash table chain */
 48631  48754     PCache1 *pCache;               /* Cache that currently owns this page */
 48632  48755     PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
 48633  48756     PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
 48634  48757                                    /* NB: pLruPrev is only valid if pLruNext!=0 */
 48635  48758   };
 48636  48759   
................................................................................
 48828  48951       do{
 48829  48952         PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
 48830  48953         pX->page.pBuf = zBulk;
 48831  48954         pX->page.pExtra = &pX[1];
 48832  48955         pX->isBulkLocal = 1;
 48833  48956         pX->isAnchor = 0;
 48834  48957         pX->pNext = pCache->pFree;
        48958  +      pX->pLruPrev = 0;           /* Initializing this saves a valgrind error */
 48835  48959         pCache->pFree = pX;
 48836  48960         zBulk += pCache->szAlloc;
 48837  48961       }while( --nBulk );
 48838  48962     }
 48839  48963     return pCache->pFree!=0;
 48840  48964   }
 48841  48965   
................................................................................
 49003  49127   
 49004  49128   /*
 49005  49129   ** Malloc function used by SQLite to obtain space from the buffer configured
 49006  49130   ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
 49007  49131   ** exists, this function falls back to sqlite3Malloc().
 49008  49132   */
 49009  49133   SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
 49010         -  /* During rebalance operations on a corrupt database file, it is sometimes
 49011         -  ** (rarely) possible to overread the temporary page buffer by a few bytes.
 49012         -  ** Enlarge the allocation slightly so that this does not cause problems. */
        49134  +  assert( sz<=65536+8 ); /* These allocations are never very large */
 49013  49135     return pcache1Alloc(sz);
 49014  49136   }
 49015  49137   
 49016  49138   /*
 49017  49139   ** Free an allocated buffer obtained from sqlite3PageMalloc().
 49018  49140   */
 49019  49141   SQLITE_PRIVATE void sqlite3PageFree(void *p){
................................................................................
 51289  51411   **   * the database file is open,
 51290  51412   **   * there are no dirty pages in the cache, and
 51291  51413   **   * the desired page is not currently in the wal file.
 51292  51414   */
 51293  51415   SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
 51294  51416     if( pPager->fd->pMethods==0 ) return 0;
 51295  51417     if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
        51418  +#ifdef SQLITE_HAS_CODEC
        51419  +  if( pPager->xCodec!=0 ) return 0;
        51420  +#endif
 51296  51421   #ifndef SQLITE_OMIT_WAL
 51297  51422     if( pPager->pWal ){
 51298  51423       u32 iRead = 0;
 51299  51424       int rc;
 51300  51425       rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
 51301  51426       return (rc==SQLITE_OK && iRead==0);
 51302  51427     }
................................................................................
 54238  54363       char *pNew = NULL;             /* New temp space */
 54239  54364       i64 nByte = 0;
 54240  54365   
 54241  54366       if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
 54242  54367         rc = sqlite3OsFileSize(pPager->fd, &nByte);
 54243  54368       }
 54244  54369       if( rc==SQLITE_OK ){
 54245         -      pNew = (char *)sqlite3PageMalloc(pageSize);
 54246         -      if( !pNew ) rc = SQLITE_NOMEM_BKPT;
        54370  +      /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
        54371  +      * cell header parser will never run off the end of the allocation */
        54372  +      pNew = (char *)sqlite3PageMalloc(pageSize+8);
        54373  +      if( !pNew ){
        54374  +        rc = SQLITE_NOMEM_BKPT;
        54375  +      }else{
        54376  +        memset(pNew+pageSize, 0, 8);
        54377  +      }
 54247  54378       }
 54248  54379   
 54249  54380       if( rc==SQLITE_OK ){
 54250  54381         pager_reset(pPager);
 54251  54382         rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
 54252  54383       }
 54253  54384       if( rc==SQLITE_OK ){
................................................................................
 57620  57751     /* If the cache contains a page with page-number pgno, remove it
 57621  57752     ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
 57622  57753     ** page pgno before the 'move' operation, it needs to be retained 
 57623  57754     ** for the page moved there.
 57624  57755     */
 57625  57756     pPg->flags &= ~PGHDR_NEED_SYNC;
 57626  57757     pPgOld = sqlite3PagerLookup(pPager, pgno);
 57627         -  assert( !pPgOld || pPgOld->nRef==1 );
        57758  +  assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
 57628  57759     if( pPgOld ){
        57760  +    if( pPgOld->nRef>1 ){
        57761  +      sqlite3PagerUnrefNotNull(pPgOld);
        57762  +      return SQLITE_CORRUPT_BKPT;
        57763  +    }
 57629  57764       pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
 57630  57765       if( pPager->tempFile ){
 57631  57766         /* Do not discard pages from an in-memory database since we might
 57632  57767         ** need to rollback later.  Just move the page out of the way. */
 57633  57768         sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
 57634  57769       }else{
 57635  57770         sqlite3PcacheDrop(pPgOld);
................................................................................
 58149  58284   
 58150  58285   /*
 58151  58286   ** Release a lock obtained by an earlier successful call to
 58152  58287   ** sqlite3PagerSnapshotCheck().
 58153  58288   */
 58154  58289   SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
 58155  58290     assert( pPager->pWal );
 58156         -  return sqlite3WalSnapshotUnlock(pPager->pWal);
        58291  +  sqlite3WalSnapshotUnlock(pPager->pWal);
 58157  58292   }
 58158  58293   
 58159  58294   #endif /* SQLITE_ENABLE_SNAPSHOT */
 58160  58295   #endif /* !SQLITE_OMIT_WAL */
 58161  58296   
 58162  58297   #ifdef SQLITE_ENABLE_ZIPVFS
 58163  58298   /*
................................................................................
 58750  58885     int iPage,               /* The page we seek */
 58751  58886     volatile u32 **ppPage    /* Write the page pointer here */
 58752  58887   ){
 58753  58888     int rc = SQLITE_OK;
 58754  58889   
 58755  58890     /* Enlarge the pWal->apWiData[] array if required */
 58756  58891     if( pWal->nWiData<=iPage ){
 58757         -    int nByte = sizeof(u32*)*(iPage+1);
        58892  +    sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
 58758  58893       volatile u32 **apNew;
 58759  58894       apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
 58760  58895       if( !apNew ){
 58761  58896         *ppPage = 0;
 58762  58897         return SQLITE_NOMEM_BKPT;
 58763  58898       }
 58764  58899       memset((void*)&apNew[pWal->nWiData], 0,
................................................................................
 58854  58989       s2 = aIn[1];
 58855  58990     }else{
 58856  58991       s1 = s2 = 0;
 58857  58992     }
 58858  58993   
 58859  58994     assert( nByte>=8 );
 58860  58995     assert( (nByte&0x00000007)==0 );
        58996  +  assert( nByte<=65536 );
 58861  58997   
 58862  58998     if( nativeCksum ){
 58863  58999       do {
 58864  59000         s1 += *aData++ + s2;
 58865  59001         s2 += *aData++ + s1;
 58866  59002       }while( aData<aEnd );
 58867  59003     }else{
................................................................................
 59161  59297   ** actually needed.
 59162  59298   */
 59163  59299   static void walCleanupHash(Wal *pWal){
 59164  59300     WalHashLoc sLoc;                /* Hash table location */
 59165  59301     int iLimit = 0;                 /* Zero values greater than this */
 59166  59302     int nByte;                      /* Number of bytes to zero in aPgno[] */
 59167  59303     int i;                          /* Used to iterate through aHash[] */
        59304  +  int rc;                         /* Return code form walHashGet() */
 59168  59305   
 59169  59306     assert( pWal->writeLock );
 59170  59307     testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
 59171  59308     testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
 59172  59309     testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
 59173  59310   
 59174  59311     if( pWal->hdr.mxFrame==0 ) return;
 59175  59312   
 59176  59313     /* Obtain pointers to the hash-table and page-number array containing 
 59177  59314     ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
 59178         -  ** that the page said hash-table and array reside on is already mapped.
        59315  +  ** that the page said hash-table and array reside on is already mapped.(1)
 59179  59316     */
 59180  59317     assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
 59181  59318     assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
 59182         -  walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
        59319  +  rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
        59320  +  if( NEVER(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */
 59183  59321   
 59184  59322     /* Zero all hash-table entries that correspond to frame numbers greater
 59185  59323     ** than pWal->hdr.mxFrame.
 59186  59324     */
 59187  59325     iLimit = pWal->hdr.mxFrame - sLoc.iZero;
 59188  59326     assert( iLimit>0 );
 59189  59327     for(i=0; i<HASHTABLE_NSLOT; i++){
................................................................................
 59789  59927   ** The calling routine should invoke walIteratorFree() to destroy the
 59790  59928   ** WalIterator object when it has finished with it.
 59791  59929   */
 59792  59930   static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
 59793  59931     WalIterator *p;                 /* Return value */
 59794  59932     int nSegment;                   /* Number of segments to merge */
 59795  59933     u32 iLast;                      /* Last frame in log */
 59796         -  int nByte;                      /* Number of bytes to allocate */
        59934  +  sqlite3_int64 nByte;            /* Number of bytes to allocate */
 59797  59935     int i;                          /* Iterator variable */
 59798  59936     ht_slot *aTmp;                  /* Temp space used by merge-sort */
 59799  59937     int rc = SQLITE_OK;             /* Return Code */
 59800  59938   
 59801  59939     /* This routine only runs while holding the checkpoint lock. And
 59802  59940     ** it only runs if there is actually content in the log (mxFrame>0).
 59803  59941     */
................................................................................
 62325  62463     u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
 62326  62464     u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
 62327  62465     u8 max1bytePayload;  /* min(maxLocal,127) */
 62328  62466     u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
 62329  62467     u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
 62330  62468     u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
 62331  62469     u16 cellOffset;      /* Index in aData of first cell pointer */
 62332         -  u16 nFree;           /* Number of free bytes on the page */
        62470  +  int nFree;           /* Number of free bytes on the page. -1 for unknown */
 62333  62471     u16 nCell;           /* Number of cells on this page, local and ovfl */
 62334  62472     u16 maskPage;        /* Mask for page offset */
 62335  62473     u16 aiOvfl[4];       /* Insert the i-th overflow cell before the aiOvfl-th
 62336  62474                          ** non-overflow cell */
 62337  62475     u8 *apOvfl[4];       /* Pointers to the body of overflow cells */
 62338  62476     BtShared *pBt;       /* Pointer to BtShared that this page is part of */
 62339  62477     u8 *aData;           /* Pointer to disk image of the page data */
................................................................................
 63879  64017   ** when saveCursorPosition() was called. Note that this call deletes the 
 63880  64018   ** saved position info stored by saveCursorPosition(), so there can be
 63881  64019   ** at most one effective restoreCursorPosition() call after each 
 63882  64020   ** saveCursorPosition().
 63883  64021   */
 63884  64022   static int btreeRestoreCursorPosition(BtCursor *pCur){
 63885  64023     int rc;
 63886         -  int skipNext;
        64024  +  int skipNext = 0;
 63887  64025     assert( cursorOwnsBtShared(pCur) );
 63888  64026     assert( pCur->eState>=CURSOR_REQUIRESEEK );
 63889  64027     if( pCur->eState==CURSOR_FAULT ){
 63890  64028       return pCur->skipNext;
 63891  64029     }
 63892  64030     pCur->eState = CURSOR_INVALID;
 63893         -  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
        64031  +  if( sqlite3FaultSim(410) ){
        64032  +    rc = SQLITE_IOERR;
        64033  +  }else{
        64034  +    rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
        64035  +  }
 63894  64036     if( rc==SQLITE_OK ){
 63895  64037       sqlite3_free(pCur->pKey);
 63896  64038       pCur->pKey = 0;
 63897  64039       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
 63898  64040       if( skipNext ) pCur->skipNext = skipNext;
 63899  64041       if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
 63900  64042         pCur->eState = CURSOR_SKIPNEXT;
................................................................................
 64467  64609     assert( pPage->nOverflow==0 );
 64468  64610     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 64469  64611     temp = 0;
 64470  64612     src = data = pPage->aData;
 64471  64613     hdr = pPage->hdrOffset;
 64472  64614     cellOffset = pPage->cellOffset;
 64473  64615     nCell = pPage->nCell;
 64474         -  assert( nCell==get2byte(&data[hdr+3]) );
        64616  +  assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
 64475  64617     iCellFirst = cellOffset + 2*nCell;
 64476  64618     usableSize = pPage->pBt->usableSize;
 64477  64619   
 64478  64620     /* This block handles pages with two or fewer free blocks and nMaxFrag
 64479  64621     ** or fewer fragmented bytes. In this case it is faster to move the
 64480  64622     ** two (or one) blocks of cells using memmove() and add the required
 64481  64623     ** offsets to each pointer in the cell-pointer array than it is to 
 64482  64624     ** reconstruct the entire page.  */
 64483  64625     if( (int)data[hdr+7]<=nMaxFrag ){
 64484  64626       int iFree = get2byte(&data[hdr+1]);
 64485         -
 64486         -    /* If the initial freeblock offset were out of bounds, that would
 64487         -    ** have been detected by btreeInitPage() when it was computing the
 64488         -    ** number of free bytes on the page. */
 64489         -    assert( iFree<=usableSize-4 );
        64627  +    if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
 64490  64628       if( iFree ){
 64491  64629         int iFree2 = get2byte(&data[iFree]);
 64492  64630         if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
 64493  64631         if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
 64494  64632           u8 *pEnd = &data[cellOffset + nCell*2];
 64495  64633           u8 *pAddr;
 64496  64634           int sz2 = 0;
................................................................................
 64501  64639           }
 64502  64640           if( iFree2 ){
 64503  64641             if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
 64504  64642             sz2 = get2byte(&data[iFree2+2]);
 64505  64643             if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
 64506  64644             memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
 64507  64645             sz += sz2;
        64646  +        }else if( iFree+sz>usableSize ){
        64647  +          return SQLITE_CORRUPT_PAGE(pPage);
 64508  64648           }
        64649  +
 64509  64650           cbrk = top+sz;
 64510  64651           assert( cbrk+(iFree-top) <= usableSize );
 64511  64652           memmove(&data[cbrk], &data[top], iFree-top);
 64512  64653           for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
 64513  64654             pc = get2byte(pAddr);
 64514  64655             if( pc<iFree ){ put2byte(pAddr, pc+sz); }
 64515  64656             else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
................................................................................
 64552  64693         src = temp;
 64553  64694       }
 64554  64695       memcpy(&data[cbrk], &src[pc], size);
 64555  64696     }
 64556  64697     data[hdr+7] = 0;
 64557  64698   
 64558  64699    defragment_out:
        64700  +  assert( pPage->nFree>=0 );
 64559  64701     if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
 64560  64702       return SQLITE_CORRUPT_PAGE(pPage);
 64561  64703     }
 64562  64704     assert( cbrk>=iCellFirst );
 64563  64705     put2byte(&data[hdr+5], cbrk);
 64564  64706     data[hdr+1] = 0;
 64565  64707     data[hdr+2] = 0;
................................................................................
 64579  64721   ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
 64580  64722   **
 64581  64723   ** Slots on the free list that are between 1 and 3 bytes larger than nByte
 64582  64724   ** will be ignored if adding the extra space to the fragmentation count
 64583  64725   ** causes the fragmentation count to exceed 60.
 64584  64726   */
 64585  64727   static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
 64586         -  const int hdr = pPg->hdrOffset;
 64587         -  u8 * const aData = pPg->aData;
 64588         -  int iAddr = hdr + 1;
 64589         -  int pc = get2byte(&aData[iAddr]);
 64590         -  int x;
 64591         -  int usableSize = pPg->pBt->usableSize;
 64592         -  int size;            /* Size of the free slot */
        64728  +  const int hdr = pPg->hdrOffset;            /* Offset to page header */
        64729  +  u8 * const aData = pPg->aData;             /* Page data */
        64730  +  int iAddr = hdr + 1;                       /* Address of ptr to pc */
        64731  +  int pc = get2byte(&aData[iAddr]);          /* Address of a free slot */
        64732  +  int x;                                     /* Excess size of the slot */
        64733  +  int maxPC = pPg->pBt->usableSize - nByte;  /* Max address for a usable slot */
        64734  +  int size;                                  /* Size of the free slot */
 64593  64735   
 64594  64736     assert( pc>0 );
 64595         -  while( pc<=usableSize-4 ){
        64737  +  while( pc<=maxPC ){
 64596  64738       /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
 64597  64739       ** freeblock form a big-endian integer which is the size of the freeblock
 64598  64740       ** in bytes, including the 4-byte header. */
 64599  64741       size = get2byte(&aData[pc+2]);
 64600  64742       if( (x = size - nByte)>=0 ){
 64601  64743         testcase( x==4 );
 64602  64744         testcase( x==3 );
 64603         -      if( size+pc > usableSize ){
 64604         -        *pRc = SQLITE_CORRUPT_PAGE(pPg);
 64605         -        return 0;
 64606         -      }else if( x<4 ){
        64745  +      if( x<4 ){
 64607  64746           /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
 64608  64747           ** number of bytes in fragments may not exceed 60. */
 64609  64748           if( aData[hdr+7]>57 ) return 0;
 64610  64749   
 64611  64750           /* Remove the slot from the free-list. Update the number of
 64612  64751           ** fragmented bytes within the page. */
 64613  64752           memcpy(&aData[iAddr], &aData[pc], 2);
 64614  64753           aData[hdr+7] += (u8)x;
        64754  +      }else if( x+pc > maxPC ){
        64755  +        /* This slot extends off the end of the usable part of the page */
        64756  +        *pRc = SQLITE_CORRUPT_PAGE(pPg);
        64757  +        return 0;
 64615  64758         }else{
 64616  64759           /* The slot remains on the free-list. Reduce its size to account
 64617         -         ** for the portion used by the new allocation. */
        64760  +        ** for the portion used by the new allocation. */
 64618  64761           put2byte(&aData[pc+2], x);
 64619  64762         }
 64620  64763         return &aData[pc + x];
 64621  64764       }
 64622  64765       iAddr = pc;
 64623  64766       pc = get2byte(&aData[pc]);
 64624         -    if( pc<iAddr+size ) break;
        64767  +    if( pc<=iAddr+size ){
        64768  +      if( pc ){
        64769  +        /* The next slot in the chain is not past the end of the current slot */
        64770  +        *pRc = SQLITE_CORRUPT_PAGE(pPg);
        64771  +      }
        64772  +      return 0;
        64773  +    }
 64625  64774     }
 64626         -  if( pc ){
        64775  +  if( pc>maxPC+nByte-4 ){
        64776  +    /* The free slot chain extends off the end of the page */
 64627  64777       *pRc = SQLITE_CORRUPT_PAGE(pPg);
 64628  64778     }
 64629         -
 64630  64779     return 0;
 64631  64780   }
 64632  64781   
 64633  64782   /*
 64634  64783   ** Allocate nByte bytes of space from within the B-Tree page passed
 64635  64784   ** as the first argument. Write into *pIdx the index into pPage->aData[]
 64636  64785   ** of the first byte of allocated space. Return either SQLITE_OK or
................................................................................
 64672  64821       if( top==0 && pPage->pBt->usableSize==65536 ){
 64673  64822         top = 65536;
 64674  64823       }else{
 64675  64824         return SQLITE_CORRUPT_PAGE(pPage);
 64676  64825       }
 64677  64826     }
 64678  64827   
 64679         -  /* If there is enough space between gap and top for one more cell pointer
 64680         -  ** array entry offset, and if the freelist is not empty, then search the
 64681         -  ** freelist looking for a free slot big enough to satisfy the request.
        64828  +  /* If there is enough space between gap and top for one more cell pointer,
        64829  +  ** and if the freelist is not empty, then search the
        64830  +  ** freelist looking for a slot big enough to satisfy the request.
 64682  64831     */
 64683  64832     testcase( gap+2==top );
 64684  64833     testcase( gap+1==top );
 64685  64834     testcase( gap==top );
 64686  64835     if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
 64687  64836       u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
 64688  64837       if( pSpace ){
................................................................................
 64696  64845   
 64697  64846     /* The request could not be fulfilled using a freelist slot.  Check
 64698  64847     ** to see if defragmentation is necessary.
 64699  64848     */
 64700  64849     testcase( gap+2+nByte==top );
 64701  64850     if( gap+2+nByte>top ){
 64702  64851       assert( pPage->nCell>0 || CORRUPT_DB );
        64852  +    assert( pPage->nFree>=0 );
 64703  64853       rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
 64704  64854       if( rc ) return rc;
 64705  64855       top = get2byteNotZero(&data[hdr+5]);
 64706  64856       assert( gap+2+nByte<=top );
 64707  64857     }
 64708  64858   
 64709  64859   
 64710  64860     /* Allocate memory from the gap in between the cell pointer array
 64711         -  ** and the cell content area.  The btreeInitPage() call has already
        64861  +  ** and the cell content area.  The btreeComputeFreeSpace() call has already
 64712  64862     ** validated the freelist.  Given that the freelist is valid, there
 64713  64863     ** is no way that the allocation can extend off the end of the page.
 64714  64864     ** The assert() below verifies the previous sentence.
 64715  64865     */
 64716  64866     top -= nByte;
 64717  64867     put2byte(&data[hdr+5], top);
 64718  64868     assert( top+nByte <= (int)pPage->pBt->usableSize );
................................................................................
 64723  64873   /*
 64724  64874   ** Return a section of the pPage->aData to the freelist.
 64725  64875   ** The first byte of the new free block is pPage->aData[iStart]
 64726  64876   ** and the size of the block is iSize bytes.
 64727  64877   **
 64728  64878   ** Adjacent freeblocks are coalesced.
 64729  64879   **
 64730         -** Note that even though the freeblock list was checked by btreeInitPage(),
        64880  +** Even though the freeblock list was checked by btreeComputeFreeSpace(),
 64731  64881   ** that routine will not detect overlap between cells or freeblocks.  Nor
 64732  64882   ** does it detect cells or freeblocks that encrouch into the reserved bytes
 64733  64883   ** at the end of the page.  So do additional corruption checks inside this
 64734  64884   ** routine and return SQLITE_CORRUPT if any problems are found.
 64735  64885   */
 64736  64886   static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
 64737  64887     u16 iPtr;                             /* Address of ptr to next freeblock */
................................................................................
 64885  65035       return SQLITE_CORRUPT_PAGE(pPage);
 64886  65036     }
 64887  65037     pPage->max1bytePayload = pBt->max1bytePayload;
 64888  65038     return SQLITE_OK;
 64889  65039   }
 64890  65040   
 64891  65041   /*
 64892         -** Initialize the auxiliary information for a disk block.
 64893         -**
 64894         -** Return SQLITE_OK on success.  If we see that the page does
 64895         -** not contain a well-formed database page, then return 
 64896         -** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
 64897         -** guarantee that the page is well-formed.  It only shows that
 64898         -** we failed to detect any corruption.
        65042  +** Compute the amount of freespace on the page.  In other words, fill
        65043  +** in the pPage->nFree field.
 64899  65044   */
 64900         -static int btreeInitPage(MemPage *pPage){
        65045  +static int btreeComputeFreeSpace(MemPage *pPage){
 64901  65046     int pc;            /* Address of a freeblock within pPage->aData[] */
 64902  65047     u8 hdr;            /* Offset to beginning of page header */
 64903  65048     u8 *data;          /* Equal to pPage->aData */
 64904         -  BtShared *pBt;        /* The main btree structure */
 64905  65049     int usableSize;    /* Amount of usable space on each page */
 64906         -  u16 cellOffset;    /* Offset from start of page to first cell pointer */
 64907  65050     int nFree;         /* Number of unused bytes on the page */
 64908  65051     int top;           /* First byte of the cell content area */
 64909  65052     int iCellFirst;    /* First allowable cell or freeblock offset */
 64910  65053     int iCellLast;     /* Last possible cell or freeblock offset */
 64911  65054   
 64912  65055     assert( pPage->pBt!=0 );
 64913  65056     assert( pPage->pBt->db!=0 );
 64914  65057     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 64915  65058     assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
 64916  65059     assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
 64917  65060     assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
 64918         -  assert( pPage->isInit==0 );
        65061  +  assert( pPage->isInit==1 );
        65062  +  assert( pPage->nFree<0 );
 64919  65063   
 64920         -  pBt = pPage->pBt;
        65064  +  usableSize = pPage->pBt->usableSize;
 64921  65065     hdr = pPage->hdrOffset;
 64922  65066     data = pPage->aData;
 64923         -  /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
 64924         -  ** the b-tree page type. */
 64925         -  if( decodeFlags(pPage, data[hdr]) ){
 64926         -    return SQLITE_CORRUPT_PAGE(pPage);
 64927         -  }
 64928         -  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 64929         -  pPage->maskPage = (u16)(pBt->pageSize - 1);
 64930         -  pPage->nOverflow = 0;
 64931         -  usableSize = pBt->usableSize;
 64932         -  pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
 64933         -  pPage->aDataEnd = &data[usableSize];
 64934         -  pPage->aCellIdx = &data[cellOffset];
 64935         -  pPage->aDataOfst = &data[pPage->childPtrSize];
 64936  65067     /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
 64937  65068     ** the start of the cell content area. A zero value for this integer is
 64938  65069     ** interpreted as 65536. */
 64939  65070     top = get2byteNotZero(&data[hdr+5]);
 64940         -  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
 64941         -  ** number of cells on the page. */
 64942         -  pPage->nCell = get2byte(&data[hdr+3]);
 64943         -  if( pPage->nCell>MX_CELL(pBt) ){
 64944         -    /* To many cells for a single page.  The page must be corrupt */
 64945         -    return SQLITE_CORRUPT_PAGE(pPage);
 64946         -  }
 64947         -  testcase( pPage->nCell==MX_CELL(pBt) );
 64948         -  /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
 64949         -  ** possible for a root page of a table that contains no rows) then the
 64950         -  ** offset to the cell content area will equal the page size minus the
 64951         -  ** bytes of reserved space. */
 64952         -  assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
 64953         -
 64954         -  /* A malformed database page might cause us to read past the end
 64955         -  ** of page when parsing a cell.  
 64956         -  **
 64957         -  ** The following block of code checks early to see if a cell extends
 64958         -  ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
 64959         -  ** returned if it does.
 64960         -  */
 64961         -  iCellFirst = cellOffset + 2*pPage->nCell;
        65071  +  iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
 64962  65072     iCellLast = usableSize - 4;
 64963         -  if( pBt->db->flags & SQLITE_CellSizeCk ){
 64964         -    int i;            /* Index into the cell pointer array */
 64965         -    int sz;           /* Size of a cell */
 64966         -
 64967         -    if( !pPage->leaf ) iCellLast--;
 64968         -    for(i=0; i<pPage->nCell; i++){
 64969         -      pc = get2byteAligned(&data[cellOffset+i*2]);
 64970         -      testcase( pc==iCellFirst );
 64971         -      testcase( pc==iCellLast );
 64972         -      if( pc<iCellFirst || pc>iCellLast ){
 64973         -        return SQLITE_CORRUPT_PAGE(pPage);
 64974         -      }
 64975         -      sz = pPage->xCellSize(pPage, &data[pc]);
 64976         -      testcase( pc+sz==usableSize );
 64977         -      if( pc+sz>usableSize ){
 64978         -        return SQLITE_CORRUPT_PAGE(pPage);
 64979         -      }
 64980         -    }
 64981         -    if( !pPage->leaf ) iCellLast++;
 64982         -  }  
 64983  65073   
 64984  65074     /* Compute the total free space on the page
 64985  65075     ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
 64986  65076     ** start of the first freeblock on the page, or is zero if there are no
 64987  65077     ** freeblocks. */
 64988  65078     pc = get2byte(&data[hdr+1]);
 64989  65079     nFree = data[hdr+7] + top;  /* Init nFree to non-freeblock free space */
................................................................................
 65023  65113     ** serves to verify that the offset to the start of the cell-content
 65024  65114     ** area, according to the page header, lies within the page.
 65025  65115     */
 65026  65116     if( nFree>usableSize ){
 65027  65117       return SQLITE_CORRUPT_PAGE(pPage);
 65028  65118     }
 65029  65119     pPage->nFree = (u16)(nFree - iCellFirst);
        65120  +  return SQLITE_OK;
        65121  +}
        65122  +
        65123  +/*
        65124  +** Do additional sanity check after btreeInitPage() if
        65125  +** PRAGMA cell_size_check=ON 
        65126  +*/
        65127  +static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
        65128  +  int iCellFirst;    /* First allowable cell or freeblock offset */
        65129  +  int iCellLast;     /* Last possible cell or freeblock offset */
        65130  +  int i;             /* Index into the cell pointer array */
        65131  +  int sz;            /* Size of a cell */
        65132  +  int pc;            /* Address of a freeblock within pPage->aData[] */
        65133  +  u8 *data;          /* Equal to pPage->aData */
        65134  +  int usableSize;    /* Maximum usable space on the page */
        65135  +  int cellOffset;    /* Start of cell content area */
        65136  +
        65137  +  iCellFirst = pPage->cellOffset + 2*pPage->nCell;
        65138  +  usableSize = pPage->pBt->usableSize;
        65139  +  iCellLast = usableSize - 4;
        65140  +  data = pPage->aData;
        65141  +  cellOffset = pPage->cellOffset;
        65142  +  if( !pPage->leaf ) iCellLast--;
        65143  +  for(i=0; i<pPage->nCell; i++){
        65144  +    pc = get2byteAligned(&data[cellOffset+i*2]);
        65145  +    testcase( pc==iCellFirst );
        65146  +    testcase( pc==iCellLast );
        65147  +    if( pc<iCellFirst || pc>iCellLast ){
        65148  +      return SQLITE_CORRUPT_PAGE(pPage);
        65149  +    }
        65150  +    sz = pPage->xCellSize(pPage, &data[pc]);
        65151  +    testcase( pc+sz==usableSize );
        65152  +    if( pc+sz>usableSize ){
        65153  +      return SQLITE_CORRUPT_PAGE(pPage);
        65154  +    }
        65155  +  }
        65156  +  return SQLITE_OK;
        65157  +}
        65158  +
        65159  +/*
        65160  +** Initialize the auxiliary information for a disk block.
        65161  +**
        65162  +** Return SQLITE_OK on success.  If we see that the page does
        65163  +** not contain a well-formed database page, then return 
        65164  +** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
        65165  +** guarantee that the page is well-formed.  It only shows that
        65166  +** we failed to detect any corruption.
        65167  +*/
        65168  +static int btreeInitPage(MemPage *pPage){
        65169  +  u8 *data;          /* Equal to pPage->aData */
        65170  +  BtShared *pBt;        /* The main btree structure */
        65171  +
        65172  +  assert( pPage->pBt!=0 );
        65173  +  assert( pPage->pBt->db!=0 );
        65174  +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
        65175  +  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
        65176  +  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
        65177  +  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
        65178  +  assert( pPage->isInit==0 );
        65179  +
        65180  +  pBt = pPage->pBt;
        65181  +  data = pPage->aData + pPage->hdrOffset;
        65182  +  /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
        65183  +  ** the b-tree page type. */
        65184  +  if( decodeFlags(pPage, data[0]) ){
        65185  +    return SQLITE_CORRUPT_PAGE(pPage);
        65186  +  }
        65187  +  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
        65188  +  pPage->maskPage = (u16)(pBt->pageSize - 1);
        65189  +  pPage->nOverflow = 0;
        65190  +  pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
        65191  +  pPage->aCellIdx = data + pPage->childPtrSize + 8;
        65192  +  pPage->aDataEnd = pPage->aData + pBt->usableSize;
        65193  +  pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
        65194  +  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
        65195  +  ** number of cells on the page. */
        65196  +  pPage->nCell = get2byte(&data[3]);
        65197  +  if( pPage->nCell>MX_CELL(pBt) ){
        65198  +    /* To many cells for a single page.  The page must be corrupt */
        65199  +    return SQLITE_CORRUPT_PAGE(pPage);
        65200  +  }
        65201  +  testcase( pPage->nCell==MX_CELL(pBt) );
        65202  +  /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
        65203  +  ** possible for a root page of a table that contains no rows) then the
        65204  +  ** offset to the cell content area will equal the page size minus the
        65205  +  ** bytes of reserved space. */
        65206  +  assert( pPage->nCell>0
        65207  +       || get2byteNotZero(&data[5])==(int)pBt->usableSize
        65208  +       || CORRUPT_DB );
        65209  +  pPage->nFree = -1;  /* Indicate that this value is yet uncomputed */
 65030  65210     pPage->isInit = 1;
        65211  +  if( pBt->db->flags & SQLITE_CellSizeCk ){
        65212  +    return btreeCellSizeCheck(pPage);
        65213  +  }
 65031  65214     return SQLITE_OK;
 65032  65215   }
 65033  65216   
 65034  65217   /*
 65035  65218   ** Set up a raw page so that it looks like a database page holding
 65036  65219   ** no entries.
 65037  65220   */
................................................................................
 65166  65349     assert( sqlite3_mutex_held(pBt->mutex) );
 65167  65350     assert( pCur==0 || ppPage==&pCur->pPage );
 65168  65351     assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
 65169  65352     assert( pCur==0 || pCur->iPage>0 );
 65170  65353   
 65171  65354     if( pgno>btreePagecount(pBt) ){
 65172  65355       rc = SQLITE_CORRUPT_BKPT;
 65173         -    goto getAndInitPage_error;
        65356  +    goto getAndInitPage_error1;
 65174  65357     }
 65175  65358     rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
 65176  65359     if( rc ){
 65177         -    goto getAndInitPage_error;
        65360  +    goto getAndInitPage_error1;
 65178  65361     }
 65179  65362     *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
 65180  65363     if( (*ppPage)->isInit==0 ){
 65181  65364       btreePageFromDbPage(pDbPage, pgno, pBt);
 65182  65365       rc = btreeInitPage(*ppPage);
 65183  65366       if( rc!=SQLITE_OK ){
 65184         -      releasePage(*ppPage);
 65185         -      goto getAndInitPage_error;
        65367  +      goto getAndInitPage_error2;
 65186  65368       }
 65187  65369     }
 65188  65370     assert( (*ppPage)->pgno==pgno );
 65189  65371     assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
 65190  65372   
 65191  65373     /* If obtaining a child page for a cursor, we must verify that the page is
 65192  65374     ** compatible with the root page. */
 65193  65375     if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
 65194  65376       rc = SQLITE_CORRUPT_PGNO(pgno);
 65195         -    releasePage(*ppPage);
 65196         -    goto getAndInitPage_error;
        65377  +    goto getAndInitPage_error2;
 65197  65378     }
 65198  65379     return SQLITE_OK;
 65199  65380   
 65200         -getAndInitPage_error:
        65381  +getAndInitPage_error2:
        65382  +  releasePage(*ppPage);
        65383  +getAndInitPage_error1:
 65201  65384     if( pCur ){
 65202  65385       pCur->iPage--;
 65203  65386       pCur->pPage = pCur->apPage[pCur->iPage];
 65204  65387     }
 65205  65388     testcase( pgno==0 );
 65206  65389     assert( pgno!=0 || rc==SQLITE_CORRUPT );
 65207  65390     return rc;
................................................................................
 68274  68457       assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
 68275  68458       *pRes = 1;
 68276  68459       rc = SQLITE_OK;
 68277  68460     }
 68278  68461     return rc;
 68279  68462   }
 68280  68463   
 68281         -/*
 68282         -** This function is a no-op if cursor pCur does not point to a valid row.
 68283         -** Otherwise, if pCur is valid, configure it so that the next call to
 68284         -** sqlite3BtreeNext() is a no-op.
 68285         -*/
 68286         -#ifndef SQLITE_OMIT_WINDOWFUNC
 68287         -SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor *pCur){
 68288         -  /* We believe that the cursor must always be in the valid state when
 68289         -  ** this routine is called, but the proof is difficult, so we add an
 68290         -  ** ALWaYS() test just in case we are wrong. */
 68291         -  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
 68292         -    pCur->eState = CURSOR_SKIPNEXT;
 68293         -    pCur->skipNext = 1;
 68294         -  }
 68295         -}
 68296         -#endif /* SQLITE_OMIT_WINDOWFUNC */
 68297         -
 68298  68464   /* Move the cursor to the last entry in the table.  Return SQLITE_OK
 68299  68465   ** on success.  Set *pRes to 0 if the cursor actually points to something
 68300  68466   ** or set *pRes to 1 if the table is empty.
 68301  68467   */
 68302  68468   SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
 68303  68469     int rc;
 68304  68470    
................................................................................
 68556  68722             pCur->ix = (u16)idx;
 68557  68723             rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
 68558  68724             pCur->curFlags &= ~BTCF_ValidOvfl;
 68559  68725             if( rc ){
 68560  68726               sqlite3_free(pCellKey);
 68561  68727               goto moveto_finish;
 68562  68728             }
 68563         -          c = xRecordCompare(nCell, pCellKey, pIdxKey);
        68729  +          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
 68564  68730             sqlite3_free(pCellKey);
 68565  68731           }
 68566  68732           assert( 
 68567  68733               (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
 68568  68734            && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
 68569  68735           );
 68570  68736           if( c<0 ){
................................................................................
 69188  69354   */
 69189  69355   static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
 69190  69356     MemPage *pTrunk = 0;                /* Free-list trunk page */
 69191  69357     Pgno iTrunk = 0;                    /* Page number of free-list trunk page */ 
 69192  69358     MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
 69193  69359     MemPage *pPage;                     /* Page being freed. May be NULL. */
 69194  69360     int rc;                             /* Return Code */
 69195         -  int nFree;                          /* Initial number of pages on free-list */
        69361  +  u32 nFree;                          /* Initial number of pages on free-list */
 69196  69362   
 69197  69363     assert( sqlite3_mutex_held(pBt->mutex) );
 69198  69364     assert( CORRUPT_DB || iPage>1 );
 69199  69365     assert( !pMemPage || pMemPage->pgno==iPage );
 69200  69366   
 69201         -  if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
        69367  +  if( iPage<2 || iPage>pBt->nPage ){
        69368  +    return SQLITE_CORRUPT_BKPT;
        69369  +  }
 69202  69370     if( pMemPage ){
 69203  69371       pPage = pMemPage;
 69204  69372       sqlite3PagerRef(pPage->pDbPage);
 69205  69373     }else{
 69206  69374       pPage = btreePageLookup(pBt, iPage);
 69207  69375     }
 69208  69376   
................................................................................
 69605  69773     int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
 69606  69774   
 69607  69775     if( *pRC ) return;
 69608  69776     assert( idx>=0 && idx<pPage->nCell );
 69609  69777     assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
 69610  69778     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 69611  69779     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
        69780  +  assert( pPage->nFree>=0 );
 69612  69781     data = pPage->aData;
 69613  69782     ptr = &pPage->aCellIdx[2*idx];
 69614  69783     pc = get2byte(ptr);
 69615  69784     hdr = pPage->hdrOffset;
 69616  69785     testcase( pc==get2byte(&data[hdr+5]) );
 69617  69786     testcase( pc+sz==pPage->pBt->usableSize );
 69618  69787     if( pc+sz > pPage->pBt->usableSize ){
................................................................................
 69675  69844     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 69676  69845     /* The cell should normally be sized correctly.  However, when moving a
 69677  69846     ** malformed cell from a leaf page to an interior page, if the cell size
 69678  69847     ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
 69679  69848     ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
 69680  69849     ** the term after the || in the following assert(). */
 69681  69850     assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
        69851  +  assert( pPage->nFree>=0 );
 69682  69852     if( pPage->nOverflow || sz+2>pPage->nFree ){
 69683  69853       if( pTemp ){
 69684  69854         memcpy(pTemp, pCell, sz);
 69685  69855         pCell = pTemp;
 69686  69856       }
 69687  69857       if( iChild ){
 69688  69858         put4byte(pCell, iChild);
................................................................................
 69732  69902       }
 69733  69903       pIns = pPage->aCellIdx + i*2;
 69734  69904       memmove(pIns+2, pIns, 2*(pPage->nCell - i));
 69735  69905       put2byte(pIns, idx);
 69736  69906       pPage->nCell++;
 69737  69907       /* increment the cell count */
 69738  69908       if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
 69739         -    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
        69909  +    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
 69740  69910   #ifndef SQLITE_OMIT_AUTOVACUUM
 69741  69911       if( pPage->pBt->autoVacuum ){
 69742  69912         /* The cell may contain a pointer to an overflow page. If so, write
 69743  69913         ** the entry for the overflow page into the pointer map.
 69744  69914         */
 69745  69915         ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
 69746  69916       }
................................................................................
 69819  69989   **    ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
 69820  69990   **    ixNx[4] = Total number of cells.
 69821  69991   **
 69822  69992   ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
 69823  69993   ** are used and they point to the leaf pages only, and the ixNx value are:
 69824  69994   **
 69825  69995   **    ixNx[0] = Number of cells in Child-1.
 69826         -**    ixNx[1] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
 69827         -**    ixNx[2] = Number of cells in Child-1 and Child-2 + both divider cells
        69996  +**    ixNx[1] = Number of cells in Child-1 and Child-2.
        69997  +**    ixNx[2] = Total number of cells.
        69998  +**
        69999  +** Sometimes when deleting, a child page can have zero cells.  In those
        70000  +** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
        70001  +** entries, shift down.  The end result is that each ixNx[] entry should
        70002  +** be larger than the previous
 69828  70003   */
 69829  70004   typedef struct CellArray CellArray;
 69830  70005   struct CellArray {
 69831  70006     int nCell;              /* Number of cells in apCell[] */
 69832  70007     MemPage *pRef;          /* Reference page */
 69833  70008     u8 **apCell;            /* All cells begin balanced */
 69834  70009     u16 *szCell;            /* Local size of all cells in apCell[] */
................................................................................
 70149  70324     }
 70150  70325   
 70151  70326     /* Add any overflow cells */
 70152  70327     for(i=0; i<pPg->nOverflow; i++){
 70153  70328       int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
 70154  70329       if( iCell>=0 && iCell<nNew ){
 70155  70330         pCellptr = &pPg->aCellIdx[iCell * 2];
 70156         -      assert( nCell>=iCell );
 70157         -      memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
        70331  +      if( nCell>iCell ){
        70332  +        memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
        70333  +      }
 70158  70334         nCell++;
 70159  70335         if( pageInsertArray(
 70160  70336               pPg, pBegin, &pData, pCellptr,
 70161  70337               iCell+iNew, 1, pCArray
 70162  70338         ) ) goto editpage_fail;
 70163  70339       }
 70164  70340     }
................................................................................
 70226  70402     MemPage *pNew;                       /* Newly allocated page */
 70227  70403     int rc;                              /* Return Code */
 70228  70404     Pgno pgnoNew;                        /* Page number of pNew */
 70229  70405   
 70230  70406     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 70231  70407     assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 70232  70408     assert( pPage->nOverflow==1 );
 70233         -
        70409  +  
 70234  70410     if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;  /* dbfuzz001.test */
        70411  +  assert( pPage->nFree>=0 );
        70412  +  assert( pParent->nFree>=0 );
 70235  70413   
 70236  70414     /* Allocate a new page. This page will become the right-sibling of 
 70237  70415     ** pPage. Make the parent page writable, so that the new divider cell
 70238  70416     ** may be inserted. If both these operations are successful, proceed.
 70239  70417     */
 70240  70418     rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
 70241  70419   
................................................................................
 70397  70575       /* Reinitialize page pTo so that the contents of the MemPage structure
 70398  70576       ** match the new data. The initialization of pTo can actually fail under
 70399  70577       ** fairly obscure circumstances, even though it is a copy of initialized 
 70400  70578       ** page pFrom.
 70401  70579       */
 70402  70580       pTo->isInit = 0;
 70403  70581       rc = btreeInitPage(pTo);
        70582  +    if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo);
 70404  70583       if( rc!=SQLITE_OK ){
 70405  70584         *pRC = rc;
 70406  70585         return;
 70407  70586       }
 70408  70587     
 70409  70588       /* If this is an auto-vacuum database, update the pointer-map entries
 70410  70589       ** for any b-tree or overflow pages that pTo now contains the pointers to.
................................................................................
 70505  70684     */
 70506  70685     assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
 70507  70686     assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
 70508  70687   
 70509  70688     if( !aOvflSpace ){
 70510  70689       return SQLITE_NOMEM_BKPT;
 70511  70690     }
        70691  +  assert( pParent->nFree>=0 );
 70512  70692   
 70513  70693     /* Find the sibling pages to balance. Also locate the cells in pParent 
 70514  70694     ** that divide the siblings. An attempt is made to find NN siblings on 
 70515  70695     ** either side of pPage. More siblings are taken from one side, however, 
 70516  70696     ** if there are fewer than NN siblings on the other side. If pParent
 70517  70697     ** has NB or fewer children then all children of pParent are taken.  
 70518  70698     **
................................................................................
 70544  70724     pgno = get4byte(pRight);
 70545  70725     while( 1 ){
 70546  70726       rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
 70547  70727       if( rc ){
 70548  70728         memset(apOld, 0, (i+1)*sizeof(MemPage*));
 70549  70729         goto balance_cleanup;
 70550  70730       }
 70551         -    nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
        70731  +    if( apOld[i]->nFree<0 ){
        70732  +      rc = btreeComputeFreeSpace(apOld[i]);
        70733  +      if( rc ){
        70734  +        memset(apOld, 0, (i)*sizeof(MemPage*));
        70735  +        goto balance_cleanup;
        70736  +      }
        70737  +    }
 70552  70738       if( (i--)==0 ) break;
 70553  70739   
 70554  70740       if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
 70555  70741         apDiv[i] = pParent->apOvfl[0];
 70556  70742         pgno = get4byte(apDiv[i]);
 70557  70743         szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
 70558  70744         pParent->nOverflow = 0;
................................................................................
 70588  70774         }
 70589  70775         dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
 70590  70776       }
 70591  70777     }
 70592  70778   
 70593  70779     /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
 70594  70780     ** alignment */
        70781  +  nMaxCells = nOld*(MX_CELL(pBt) + ArraySize(pParent->apOvfl));
 70595  70782     nMaxCells = (nMaxCells + 3)&~3;
 70596  70783   
 70597  70784     /*
 70598  70785     ** Allocate space for memory structures
 70599  70786     */
 70600  70787     szScratch =
 70601  70788          nMaxCells*sizeof(u8*)                       /* b.apCell */
 70602  70789        + nMaxCells*sizeof(u16)                       /* b.szCell */
 70603  70790        + pBt->pageSize;                              /* aSpace1 */
 70604  70791   
 70605         -  assert( szScratch<=6*(int)pBt->pageSize );
        70792  +  assert( szScratch<=7*(int)pBt->pageSize );
 70606  70793     b.apCell = sqlite3StackAllocRaw(0, szScratch );
 70607  70794     if( b.apCell==0 ){
 70608  70795       rc = SQLITE_NOMEM_BKPT;
 70609  70796       goto balance_cleanup;
 70610  70797     }
 70611  70798     b.szCell = (u16*)&b.apCell[nMaxCells];
 70612  70799     aSpace1 = (u8*)&b.szCell[nMaxCells];
................................................................................
 70738  70925     ** 
 70739  70926     */
 70740  70927     usableSpace = pBt->usableSize - 12 + leafCorrection;
 70741  70928     for(i=k=0; i<nOld; i++, k++){
 70742  70929       MemPage *p = apOld[i];
 70743  70930       b.apEnd[k] = p->aDataEnd;
 70744  70931       b.ixNx[k] = cntOld[i];
        70932  +    if( k && b.ixNx[k]==b.ixNx[k-1] ){
        70933  +      k--;  /* Omit b.ixNx[] entry for child pages with no cells */
        70934  +    }
 70745  70935       if( !leafData ){
 70746  70936         k++;
 70747  70937         b.apEnd[k] = pParent->aDataEnd;
 70748  70938         b.ixNx[k] = cntOld[i]+1;
 70749  70939       }
        70940  +    assert( p->nFree>=0 );
 70750  70941       szNew[i] = usableSpace - p->nFree;
 70751  70942       for(j=0; j<p->nOverflow; j++){
 70752  70943         szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
 70753  70944       }
 70754  70945       cntNew[i] = cntOld[i];
 70755  70946     }
 70756  70947     k = nOld;
................................................................................
 70968  71159     ** associated with the right-child of each sibling may also need to be 
 70969  71160     ** updated. This happens below, after the sibling pages have been 
 70970  71161     ** populated, not here.
 70971  71162     */
 70972  71163     if( ISAUTOVACUUM ){
 70973  71164       MemPage *pOld;
 70974  71165       MemPage *pNew = pOld = apNew[0];
 70975         -    u8 *aOld = pNew->aData;
 70976  71166       int cntOldNext = pNew->nCell + pNew->nOverflow;
 70977         -    int usableSize = pBt->usableSize;
 70978  71167       int iNew = 0;
 70979  71168       int iOld = 0;
 70980  71169   
 70981  71170       for(i=0; i<b.nCell; i++){
 70982  71171         u8 *pCell = b.apCell[i];
 70983         -      if( i==cntOldNext ){
 70984         -        pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
        71172  +      while( i==cntOldNext ){
        71173  +        iOld++;
        71174  +        assert( iOld<nNew || iOld<nOld );
        71175  +        pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
 70985  71176           cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
 70986         -        aOld = pOld->aData;
 70987  71177         }
 70988  71178         if( i==cntNew[iNew] ){
 70989  71179           pNew = apNew[++iNew];
 70990  71180           if( !leafData ) continue;
 70991  71181         }
 70992  71182   
 70993  71183         /* Cell pCell is destined for new sibling page pNew. Originally, it
................................................................................
 70994  71184         ** was either part of sibling page iOld (possibly an overflow cell), 
 70995  71185         ** or else the divider cell to the left of sibling page iOld. So,
 70996  71186         ** if sibling page iOld had the same page number as pNew, and if
 70997  71187         ** pCell really was a part of sibling page iOld (not a divider or
 70998  71188         ** overflow cell), we can skip updating the pointer map entries.  */
 70999  71189         if( iOld>=nNew
 71000  71190          || pNew->pgno!=aPgno[iOld]
 71001         -       || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize])
        71191  +       || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
 71002  71192         ){
 71003  71193           if( !leafCorrection ){
 71004  71194             ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
 71005  71195           }
 71006  71196           if( cachedCellSize(&b,i)>pNew->minLocal ){
 71007  71197             ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
 71008  71198           }
................................................................................
 71145  71335       ** by smaller than the child due to the database header, and so all the
 71146  71336       ** free space needs to be up front.
 71147  71337       */
 71148  71338       assert( nNew==1 || CORRUPT_DB );
 71149  71339       rc = defragmentPage(apNew[0], -1);
 71150  71340       testcase( rc!=SQLITE_OK );
 71151  71341       assert( apNew[0]->nFree == 
 71152         -        (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
        71342  +        (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
        71343  +          - apNew[0]->nCell*2)
 71153  71344         || rc!=SQLITE_OK
 71154  71345       );
 71155  71346       copyNodeContent(apNew[0], pParent, &rc);
 71156  71347       freePage(apNew[0], &rc);
 71157  71348     }else if( ISAUTOVACUUM && !leafCorrection ){
 71158  71349       /* Fix the pointer map entries associated with the right-child of each
 71159  71350       ** sibling page. All other pointer map entries have already been taken
................................................................................
 71244  71435     if( rc ){
 71245  71436       *ppChild = 0;
 71246  71437       releasePage(pChild);
 71247  71438       return rc;
 71248  71439     }
 71249  71440     assert( sqlite3PagerIswriteable(pChild->pDbPage) );
 71250  71441     assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
 71251         -  assert( pChild->nCell==pRoot->nCell );
        71442  +  assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
 71252  71443   
 71253  71444     TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
 71254  71445   
 71255  71446     /* Copy the overflow cells from pRoot to pChild */
 71256  71447     memcpy(pChild->aiOvfl, pRoot->aiOvfl,
 71257  71448            pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
 71258  71449     memcpy(pChild->apOvfl, pRoot->apOvfl,
................................................................................
 71286  71477     VVA_ONLY( int balance_quick_called = 0 );
 71287  71478     VVA_ONLY( int balance_deeper_called = 0 );
 71288  71479   
 71289  71480     do {
 71290  71481       int iPage = pCur->iPage;
 71291  71482       MemPage *pPage = pCur->pPage;
 71292  71483   
        71484  +    if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
 71293  71485       if( iPage==0 ){
 71294  71486         if( pPage->nOverflow ){
 71295  71487           /* The root page of the b-tree is overfull. In this case call the
 71296  71488           ** balance_deeper() function to create a new child for the root-page
 71297  71489           ** and copy the current contents of the root-page to it. The
 71298  71490           ** next iteration of the do-loop will balance the child page.
 71299  71491           */ 
................................................................................
 71314  71506       }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
 71315  71507         break;
 71316  71508       }else{
 71317  71509         MemPage * const pParent = pCur->apPage[iPage-1];
 71318  71510         int const iIdx = pCur->aiIdx[iPage-1];
 71319  71511   
 71320  71512         rc = sqlite3PagerWrite(pParent->pDbPage);
        71513  +      if( rc==SQLITE_OK && pParent->nFree<0 ){
        71514  +        rc = btreeComputeFreeSpace(pParent);
        71515  +      }
 71321  71516         if( rc==SQLITE_OK ){
 71322  71517   #ifndef SQLITE_OMIT_QUICKBALANCE
 71323  71518           if( pPage->intKeyLeaf
 71324  71519            && pPage->nOverflow==1
 71325  71520            && pPage->aiOvfl[0]==pPage->nCell
 71326  71521            && pParent->pgno!=1
 71327  71522            && pParent->nCell==iIdx
................................................................................
 71660  71855   
 71661  71856     }
 71662  71857     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 71663  71858   
 71664  71859     pPage = pCur->pPage;
 71665  71860     assert( pPage->intKey || pX->nKey>=0 );
 71666  71861     assert( pPage->leaf || !pPage->intKey );
        71862  +  if( pPage->nFree<0 ){
        71863  +    rc = btreeComputeFreeSpace(pPage);
        71864  +    if( rc ) return rc;
        71865  +  }
 71667  71866   
 71668  71867     TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
 71669  71868             pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
 71670  71869             loc==0 ? "overwrite" : "new entry"));
 71671  71870     assert( pPage->isInit );
 71672  71871     newCell = pBt->pTmpSpace;
 71673  71872     assert( newCell!=0 );
................................................................................
 71802  72001   
 71803  72002     assert( cursorOwnsBtShared(pCur) );
 71804  72003     assert( pBt->inTransaction==TRANS_WRITE );
 71805  72004     assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 71806  72005     assert( pCur->curFlags & BTCF_WriteFlag );
 71807  72006     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 71808  72007     assert( !hasReadConflicts(p, pCur->pgnoRoot) );
 71809         -  assert( pCur->ix<pCur->pPage->nCell );
 71810         -  assert( pCur->eState==CURSOR_VALID );
 71811  72008     assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
        72009  +  if( pCur->eState==CURSOR_REQUIRESEEK ){
        72010  +    rc = btreeRestoreCursorPosition(pCur);
        72011  +    if( rc ) return rc;
        72012  +  }
        72013  +  assert( pCur->eState==CURSOR_VALID );
 71812  72014   
 71813  72015     iCellDepth = pCur->iPage;
 71814  72016     iCellIdx = pCur->ix;
 71815  72017     pPage = pCur->pPage;
 71816  72018     pCell = findCell(pPage, iCellIdx);
        72019  +  if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT;
 71817  72020   
 71818  72021     /* If the bPreserve flag is set to true, then the cursor position must
 71819  72022     ** be preserved following this delete operation. If the current delete
 71820  72023     ** will cause a b-tree rebalance, then this is done by saving the cursor
 71821  72024     ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
 71822  72025     ** returning. 
 71823  72026     **
................................................................................
 71880  72083     ** node to replace the deleted cell.  */
 71881  72084     if( !pPage->leaf ){
 71882  72085       MemPage *pLeaf = pCur->pPage;
 71883  72086       int nCell;
 71884  72087       Pgno n;
 71885  72088       unsigned char *pTmp;
 71886  72089   
        72090  +    if( pLeaf->nFree<0 ){
        72091  +      rc = btreeComputeFreeSpace(pLeaf);
        72092  +      if( rc ) return rc;
        72093  +    }
 71887  72094       if( iCellDepth<pCur->iPage-1 ){
 71888  72095         n = pCur->apPage[iCellDepth+1]->pgno;
 71889  72096       }else{
 71890  72097         n = pCur->pPage->pgno;
 71891  72098       }
 71892  72099       pCell = findCell(pLeaf, pLeaf->nCell-1);
 71893  72100       if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
................................................................................
 72238  72445     int rc;
 72239  72446     MemPage *pPage = 0;
 72240  72447     BtShared *pBt = p->pBt;
 72241  72448   
 72242  72449     assert( sqlite3BtreeHoldsMutex(p) );
 72243  72450     assert( p->inTrans==TRANS_WRITE );
 72244  72451     assert( iTable>=2 );
        72452  +  if( iTable>btreePagecount(pBt) ){
        72453  +    return SQLITE_CORRUPT_BKPT;
        72454  +  }
 72245  72455   
 72246  72456     rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
 72247  72457     if( rc ) return rc;
 72248  72458     rc = sqlite3BtreeClearTable(p, iTable, 0);
 72249  72459     if( rc ){
 72250  72460       releasePage(pPage);
 72251  72461       return rc;
................................................................................
 72586  72796   ** Check the integrity of the freelist or of an overflow page list.
 72587  72797   ** Verify that the number of pages on the list is N.
 72588  72798   */
 72589  72799   static void checkList(
 72590  72800     IntegrityCk *pCheck,  /* Integrity checking context */
 72591  72801     int isFreeList,       /* True for a freelist.  False for overflow page list */
 72592  72802     int iPage,            /* Page number for first page in the list */
 72593         -  int N                 /* Expected number of pages in the list */
        72803  +  u32 N                 /* Expected number of pages in the list */
 72594  72804   ){
 72595  72805     int i;
 72596         -  int expected = N;
        72806  +  u32 expected = N;
 72597  72807     int nErrAtStart = pCheck->nErr;
 72598  72808     while( iPage!=0 && pCheck->mxErr ){
 72599  72809       DbPage *pOvflPage;
 72600  72810       unsigned char *pOvflData;
 72601  72811       if( checkRef(pCheck, iPage) ) break;
 72602  72812       N--;
 72603  72813       if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
................................................................................
 72771  72981     pPage->isInit = 0;
 72772  72982     if( (rc = btreeInitPage(pPage))!=0 ){
 72773  72983       assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
 72774  72984       checkAppendMsg(pCheck,
 72775  72985                      "btreeInitPage() returns error code %d", rc);
 72776  72986       goto end_of_check;
 72777  72987     }
        72988  +  if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
        72989  +    assert( rc==SQLITE_CORRUPT );
        72990  +    checkAppendMsg(pCheck, "free space corruption", rc);
        72991  +    goto end_of_check;
        72992  +  }
 72778  72993     data = pPage->aData;
 72779  72994     hdr = pPage->hdrOffset;
 72780  72995   
 72781  72996     /* Set up for cell analysis */
 72782  72997     pCheck->zPfx = "On tree page %d cell %d: ";
 72783  72998     contentOffset = get2byteNotZero(&data[hdr+5]);
 72784  72999     assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
................................................................................
 72843  73058         }
 72844  73059         maxKey = info.nKey;
 72845  73060         keyCanBeEqual = 0;     /* Only the first key on the page may ==maxKey */
 72846  73061       }
 72847  73062   
 72848  73063       /* Check the content overflow list */
 72849  73064       if( info.nPayload>info.nLocal ){
 72850         -      int nPage;       /* Number of pages on the overflow chain */
        73065  +      u32 nPage;       /* Number of pages on the overflow chain */
 72851  73066         Pgno pgnoOvfl;   /* First page of the overflow chain */
 72852  73067         assert( pc + info.nSize - 4 <= usableSize );
 72853  73068         nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
 72854  73069         pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
 72855  73070   #ifndef SQLITE_OMIT_AUTOVACUUM
 72856  73071         if( pBt->autoVacuum ){
 72857  73072           checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
................................................................................
 72903  73118       ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
 72904  73119       ** is the offset of the first freeblock, or zero if there are no
 72905  73120       ** freeblocks on the page. 
 72906  73121       */
 72907  73122       i = get2byte(&data[hdr+1]);
 72908  73123       while( i>0 ){
 72909  73124         int size, j;
 72910         -      assert( (u32)i<=usableSize-4 );     /* Enforced by btreeInitPage() */
        73125  +      assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
 72911  73126         size = get2byte(&data[i+2]);
 72912         -      assert( (u32)(i+size)<=usableSize );  /* Enforced by btreeInitPage() */
        73127  +      assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
 72913  73128         btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
 72914  73129         /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
 72915  73130         ** big-endian integer which is the offset in the b-tree page of the next
 72916  73131         ** freeblock in the chain, or zero if the freeblock is the last on the
 72917  73132         ** chain. */
 72918  73133         j = get2byte(&data[i]);
 72919  73134         /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
 72920  73135         ** increasing offset. */
 72921         -      assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
 72922         -      assert( (u32)j<=usableSize-4 );   /* Enforced by btreeInitPage() */
        73136  +      assert( j==0 || j>i+size );     /* Enforced by btreeComputeFreeSpace() */
        73137  +      assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
 72923  73138         i = j;
 72924  73139       }
 72925  73140       /* Analyze the min-heap looking for overlap between cells and/or 
 72926  73141       ** freeblocks, and counting the number of untracked bytes in nFrag.
 72927  73142       ** 
 72928  73143       ** Each min-heap entry is of the form:    (start_address<<16)|end_address.
 72929  73144       ** There is an implied first entry the covers the page header, the cell
................................................................................
 74259  74474         /* This is a pointer type.  There may be a flag to indicate what to
 74260  74475         ** do with the pointer. */
 74261  74476         assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
 74262  74477                 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
 74263  74478                 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
 74264  74479   
 74265  74480         /* No other bits set */
 74266         -      assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype
        74481  +      assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
 74267  74482                              |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
 74268  74483       }else{
 74269  74484         /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
 74270  74485         ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
 74271  74486       }
 74272  74487     }else{
 74273  74488       /* The MEM_Cleared bit is only allowed on NULLs */
................................................................................
 74380  74595     assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
 74381  74596     assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
 74382  74597     return rc;
 74383  74598   #endif
 74384  74599   }
 74385  74600   
 74386  74601   /*
 74387         -** Make sure pMem->z points to a writable allocation of at least 
 74388         -** min(n,32) bytes.
        74602  +** Make sure pMem->z points to a writable allocation of at least n bytes.
 74389  74603   **
 74390  74604   ** If the bPreserve argument is true, then copy of the content of
 74391  74605   ** pMem->z into the new allocation.  pMem must be either a string or
 74392  74606   ** blob if bPreserve is true.  If bPreserve is false, any prior content
 74393  74607   ** in pMem->z is discarded.
 74394  74608   */
 74395  74609   SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
................................................................................
 74400  74614     /* If the bPreserve flag is set to true, then the memory cell must already
 74401  74615     ** contain a valid string or blob value.  */
 74402  74616     assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
 74403  74617     testcase( bPreserve && pMem->z==0 );
 74404  74618   
 74405  74619     assert( pMem->szMalloc==0
 74406  74620          || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
 74407         -  if( n<32 ) n = 32;
 74408  74621     if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
 74409  74622       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 74410  74623       bPreserve = 0;
 74411  74624     }else{
 74412  74625       if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
 74413  74626       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
 74414  74627     }
................................................................................
 74502  74715   ** If the given Mem* has a zero-filled tail, turn it into an ordinary
 74503  74716   ** blob stored in dynamically allocated space.
 74504  74717   */
 74505  74718   #ifndef SQLITE_OMIT_INCRBLOB
 74506  74719   SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
 74507  74720     int nByte;
 74508  74721     assert( pMem->flags & MEM_Zero );
 74509         -  assert( pMem->flags&MEM_Blob );
        74722  +  assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
        74723  +  testcase( sqlite3_value_nochange(pMem) );
 74510  74724     assert( !sqlite3VdbeMemIsRowSet(pMem) );
 74511  74725     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 74512  74726   
 74513  74727     /* Set nByte to the number of bytes required to store the expanded blob. */
 74514  74728     nByte = pMem->n + pMem->u.nZero;
 74515  74729     if( nByte<=0 ){
        74730  +    if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
 74516  74731       nByte = 1;
 74517  74732     }
 74518  74733     if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
 74519  74734       return SQLITE_NOMEM_BKPT;
 74520  74735     }
 74521  74736   
 74522  74737     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
................................................................................
 75249  75464       iLimit = SQLITE_MAX_LENGTH;
 75250  75465     }
 75251  75466     flags = (enc==0?MEM_Blob:MEM_Str);
 75252  75467     if( nByte<0 ){
 75253  75468       assert( enc!=0 );
 75254  75469       if( enc==SQLITE_UTF8 ){
 75255  75470         nByte = 0x7fffffff & (int)strlen(z);
 75256         -      if( nByte>iLimit ) nByte = iLimit+1;
 75257  75471       }else{
 75258  75472         for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
 75259  75473       }
 75260  75474       flags |= MEM_Term;
 75261  75475     }
 75262  75476   
 75263  75477     /* The following block sets the new values of Mem.z and Mem.xDel. It
 75264  75478     ** also sets a flag in local variable "flags" to indicate the memory
 75265  75479     ** management (one of MEM_Dyn or MEM_Static).
 75266  75480     */
 75267  75481     if( xDel==SQLITE_TRANSIENT ){
 75268         -    int nAlloc = nByte;
        75482  +    u32 nAlloc = nByte;
 75269  75483       if( flags&MEM_Term ){
 75270  75484         nAlloc += (enc==SQLITE_UTF8?1:2);
 75271  75485       }
 75272  75486       if( nByte>iLimit ){
 75273         -      return SQLITE_TOOBIG;
        75487  +      return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
 75274  75488       }
 75275  75489       testcase( nAlloc==0 );
 75276  75490       testcase( nAlloc==31 );
 75277  75491       testcase( nAlloc==32 );
 75278         -    if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
        75492  +    if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
 75279  75493         return SQLITE_NOMEM_BKPT;
 75280  75494       }
 75281  75495       memcpy(pMem->z, z, nAlloc);
 75282         -  }else if( xDel==SQLITE_DYNAMIC ){
 75283         -    sqlite3VdbeMemRelease(pMem);
 75284         -    pMem->zMalloc = pMem->z = (char *)z;
 75285         -    pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
 75286  75496     }else{
 75287  75497       sqlite3VdbeMemRelease(pMem);
 75288  75498       pMem->z = (char *)z;
 75289         -    pMem->xDel = xDel;
 75290         -    flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
        75499  +    if( xDel==SQLITE_DYNAMIC ){
        75500  +      pMem->zMalloc = pMem->z;
        75501  +      pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
        75502  +    }else{
        75503  +      pMem->xDel = xDel;
        75504  +      flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
        75505  +    }
 75291  75506     }
 75292  75507   
 75293  75508     pMem->n = nByte;
 75294  75509     pMem->flags = flags;
 75295  75510     pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
 75296  75511   
 75297  75512   #ifndef SQLITE_OMIT_UTF16
................................................................................
 76251  76466     ** more frequent reallocs and hence provide more opportunities for 
 76252  76467     ** simulated OOM faults.  SQLITE_TEST_REALLOC_STRESS is generally used
 76253  76468     ** during testing only.  With SQLITE_TEST_REALLOC_STRESS grow the op array
 76254  76469     ** by the minimum* amount required until the size reaches 512.  Normal
 76255  76470     ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
 76256  76471     ** size of the op array or add 1KB of space, whichever is smaller. */
 76257  76472   #ifdef SQLITE_TEST_REALLOC_STRESS
 76258         -  int nNew = (v->nOpAlloc>=512 ? v->nOpAlloc*2 : v->nOpAlloc+nOp);
        76473  +  sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
        76474  +                        : (sqlite3_int64)v->nOpAlloc+nOp);
 76259  76475   #else
 76260         -  int nNew = (v->nOpAlloc ? v->nOpAlloc*2 : (int)(1024/sizeof(Op)));
        76476  +  sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
        76477  +                        : (sqlite3_int64)(1024/sizeof(Op)));
 76261  76478     UNUSED_PARAMETER(nOp);
 76262  76479   #endif
 76263  76480   
 76264  76481     /* Ensure that the size of a VDBE does not grow too large */
 76265  76482     if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
 76266  76483       sqlite3OomFault(p->db);
 76267  76484       return SQLITE_NOMEM;
................................................................................
 76733  76950     memset(&sIter, 0, sizeof(sIter));
 76734  76951     sIter.v = v;
 76735  76952   
 76736  76953     while( (pOp = opIterNext(&sIter))!=0 ){
 76737  76954       int opcode = pOp->opcode;
 76738  76955       if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
 76739  76956        || opcode==OP_VDestroy
        76957  +     || (opcode==OP_Function0 && pOp->p4.pFunc->funcFlags&SQLITE_FUNC_INTERNAL)
 76740  76958        || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
 76741  76959         && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
 76742  76960       ){
 76743  76961         hasAbort = 1;
 76744  76962         break;
 76745  76963       }
 76746  76964       if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
................................................................................
 77040  77258     Vdbe *p,                        /* VM to add scanstatus() to */
 77041  77259     int addrExplain,                /* Address of OP_Explain (or 0) */
 77042  77260     int addrLoop,                   /* Address of loop counter */ 
 77043  77261     int addrVisit,                  /* Address of rows visited counter */
 77044  77262     LogEst nEst,                    /* Estimated number of output rows */
 77045  77263     const char *zName               /* Name of table or index being scanned */
 77046  77264   ){
 77047         -  int nByte = (p->nScan+1) * sizeof(ScanStatus);
        77265  +  sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
 77048  77266     ScanStatus *aNew;
 77049  77267     aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
 77050  77268     if( aNew ){
 77051  77269       ScanStatus *pNew = &aNew[p->nScan++];
 77052  77270       pNew->addrExplain = addrExplain;
 77053  77271       pNew->addrLoop = addrLoop;
 77054  77272       pNew->addrVisit = addrVisit;
................................................................................
 78161  78379   #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
 78162  78380   
 78163  78381   /* An instance of this object describes bulk memory available for use
 78164  78382   ** by subcomponents of a prepared statement.  Space is allocated out
 78165  78383   ** of a ReusableSpace object by the allocSpace() routine below.
 78166  78384   */
 78167  78385   struct ReusableSpace {
 78168         -  u8 *pSpace;          /* Available memory */
 78169         -  int nFree;           /* Bytes of available memory */
 78170         -  int nNeeded;         /* Total bytes that could not be allocated */
        78386  +  u8 *pSpace;            /* Available memory */
        78387  +  sqlite3_int64 nFree;   /* Bytes of available memory */
        78388  +  sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
 78171  78389   };
 78172  78390   
 78173  78391   /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
 78174  78392   ** from the ReusableSpace object.  Return a pointer to the allocated
 78175  78393   ** memory on success.  If insufficient memory is available in the
 78176  78394   ** ReusableSpace object, increase the ReusableSpace.nNeeded
 78177  78395   ** value by the amount needed and return NULL.
................................................................................
 78183  78401   ** This allocator is employed to repurpose unused slots at the end of the
 78184  78402   ** opcode array of prepared state for other memory needs of the prepared
 78185  78403   ** statement.
 78186  78404   */
 78187  78405   static void *allocSpace(
 78188  78406     struct ReusableSpace *p,  /* Bulk memory available for allocation */
 78189  78407     void *pBuf,               /* Pointer to a prior allocation */
 78190         -  int nByte                 /* Bytes of memory needed */
        78408  +  sqlite3_int64 nByte       /* Bytes of memory needed */
 78191  78409   ){
 78192  78410     assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
 78193  78411     if( pBuf==0 ){
 78194  78412       nByte = ROUND8(nByte);
 78195  78413       if( nByte <= p->nFree ){
 78196  78414         p->nFree -= nByte;
 78197  78415         pBuf = &p->pSpace[p->nFree];
................................................................................
 81140  81358     sqlite3_int64 iElapse;
 81141  81359     assert( p->startTime>0 );
 81142  81360     assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
 81143  81361     assert( db->init.busy==0 );
 81144  81362     assert( p->zSql!=0 );
 81145  81363     sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
 81146  81364     iElapse = (iNow - p->startTime)*1000000;
 81147         -#ifndef SQLITE_OMIT_DEPRECATED  	
        81365  +#ifndef SQLITE_OMIT_DEPRECATED
 81148  81366     if( db->xProfile ){
 81149  81367       db->xProfile(db->pProfileArg, p->zSql, iElapse);
 81150  81368     }
 81151  81369   #endif
 81152  81370     if( db->mTrace & SQLITE_TRACE_PROFILE ){
 81153  81371       db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
 81154  81372     }
................................................................................
 81347  81565     return aType[pVal->flags&MEM_AffMask];
 81348  81566   }
 81349  81567   
 81350  81568   /* Return true if a parameter to xUpdate represents an unchanged column */
 81351  81569   SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
 81352  81570     return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
 81353  81571   }
        81572  +
        81573  +/* Return true if a parameter value originated from an sqlite3_bind() */
        81574  +SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
        81575  +  return (pVal->flags&MEM_FromBind)!=0;
        81576  +}
 81354  81577   
 81355  81578   /* Make a copy of an sqlite3_value object
 81356  81579   */
 81357  81580   SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
 81358  81581     sqlite3_value *pNew;
 81359  81582     if( pOrig==0 ) return 0;
 81360  81583     pNew = sqlite3_malloc( sizeof(*pNew) );
................................................................................
 82193  82416   **    3      The name of the table that the column derives from
 82194  82417   **    4      The name of the table column that the result column derives from
 82195  82418   **
 82196  82419   ** If the result is not a simple column reference (if it is an expression
 82197  82420   ** or a constant) then useTypes 2, 3, and 4 return NULL.
 82198  82421   */
 82199  82422   static const void *columnName(
 82200         -  sqlite3_stmt *pStmt,
 82201         -  int N,
 82202         -  const void *(*xFunc)(Mem*),
 82203         -  int useType
        82423  +  sqlite3_stmt *pStmt,     /* The statement */
        82424  +  int N,                   /* Which column to get the name for */
        82425  +  int useUtf16,            /* True to return the name as UTF16 */
        82426  +  int useType              /* What type of name */
 82204  82427   ){
 82205  82428     const void *ret;
 82206  82429     Vdbe *p;
 82207  82430     int n;
 82208  82431     sqlite3 *db;
 82209  82432   #ifdef SQLITE_ENABLE_API_ARMOR
 82210  82433     if( pStmt==0 ){
................................................................................
 82217  82440     db = p->db;
 82218  82441     assert( db!=0 );
 82219  82442     n = sqlite3_column_count(pStmt);
 82220  82443     if( N<n && N>=0 ){
 82221  82444       N += useType*n;
 82222  82445       sqlite3_mutex_enter(db->mutex);
 82223  82446       assert( db->mallocFailed==0 );
 82224         -    ret = xFunc(&p->aColName[N]);
 82225         -     /* A malloc may have failed inside of the xFunc() call. If this
        82447  +#ifndef SQLITE_OMIT_UTF16
        82448  +    if( useUtf16 ){
        82449  +      ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
        82450  +    }else
        82451  +#endif
        82452  +    {
        82453  +      ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
        82454  +    }
        82455  +    /* A malloc may have failed inside of the _text() call. If this
 82226  82456       ** is the case, clear the mallocFailed flag and return NULL.
 82227  82457       */
 82228  82458       if( db->mallocFailed ){
 82229  82459         sqlite3OomClear(db);
 82230  82460         ret = 0;
 82231  82461       }
 82232  82462       sqlite3_mutex_leave(db->mutex);
................................................................................
 82235  82465   }
 82236  82466   
 82237  82467   /*
 82238  82468   ** Return the name of the Nth column of the result set returned by SQL
 82239  82469   ** statement pStmt.
 82240  82470   */
 82241  82471   SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
 82242         -  return columnName(
 82243         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
        82472  +  return columnName(pStmt, N, 0, COLNAME_NAME);
 82244  82473   }
 82245  82474   #ifndef SQLITE_OMIT_UTF16
 82246  82475   SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
 82247         -  return columnName(
 82248         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
        82476  +  return columnName(pStmt, N, 1, COLNAME_NAME);
 82249  82477   }
 82250  82478   #endif
 82251  82479   
 82252  82480   /*
 82253  82481   ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
 82254  82482   ** not define OMIT_DECLTYPE.
 82255  82483   */
................................................................................
 82260  82488   
 82261  82489   #ifndef SQLITE_OMIT_DECLTYPE
 82262  82490   /*
 82263  82491   ** Return the column declaration type (if applicable) of the 'i'th column
 82264  82492   ** of the result set of SQL statement pStmt.
 82265  82493   */
 82266  82494   SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
 82267         -  return columnName(
 82268         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
        82495  +  return columnName(pStmt, N, 0, COLNAME_DECLTYPE);
 82269  82496   }
 82270  82497   #ifndef SQLITE_OMIT_UTF16
 82271  82498   SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
 82272         -  return columnName(
 82273         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
        82499  +  return columnName(pStmt, N, 1, COLNAME_DECLTYPE);
 82274  82500   }
 82275  82501   #endif /* SQLITE_OMIT_UTF16 */
 82276  82502   #endif /* SQLITE_OMIT_DECLTYPE */
 82277  82503   
 82278  82504   #ifdef SQLITE_ENABLE_COLUMN_METADATA
 82279  82505   /*
 82280  82506   ** Return the name of the database from which a result column derives.
 82281  82507   ** NULL is returned if the result column is an expression or constant or
 82282  82508   ** anything else which is not an unambiguous reference to a database column.
 82283  82509   */
 82284  82510   SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
 82285         -  return columnName(
 82286         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
        82511  +  return columnName(pStmt, N, 0, COLNAME_DATABASE);
 82287  82512   }
 82288  82513   #ifndef SQLITE_OMIT_UTF16
 82289  82514   SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
 82290         -  return columnName(
 82291         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
        82515  +  return columnName(pStmt, N, 1, COLNAME_DATABASE);
 82292  82516   }
 82293  82517   #endif /* SQLITE_OMIT_UTF16 */
 82294  82518   
 82295  82519   /*
 82296  82520   ** Return the name of the table from which a result column derives.
 82297  82521   ** NULL is returned if the result column is an expression or constant or
 82298  82522   ** anything else which is not an unambiguous reference to a database column.
 82299  82523   */
 82300  82524   SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
 82301         -  return columnName(
 82302         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
        82525  +  return columnName(pStmt, N, 0, COLNAME_TABLE);
 82303  82526   }
 82304  82527   #ifndef SQLITE_OMIT_UTF16
 82305  82528   SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
 82306         -  return columnName(
 82307         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
        82529  +  return columnName(pStmt, N, 1, COLNAME_TABLE);
 82308  82530   }
 82309  82531   #endif /* SQLITE_OMIT_UTF16 */
 82310  82532   
 82311  82533   /*
 82312  82534   ** Return the name of the table column from which a result column derives.
 82313  82535   ** NULL is returned if the result column is an expression or constant or
 82314  82536   ** anything else which is not an unambiguous reference to a database column.
 82315  82537   */
 82316  82538   SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
 82317         -  return columnName(
 82318         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
        82539  +  return columnName(pStmt, N, 0, COLNAME_COLUMN);
 82319  82540   }
 82320  82541   #ifndef SQLITE_OMIT_UTF16
 82321  82542   SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
 82322         -  return columnName(
 82323         -      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
        82543  +  return columnName(pStmt, N, 1, COLNAME_COLUMN);
 82324  82544   }
 82325  82545   #endif /* SQLITE_OMIT_UTF16 */
 82326  82546   #endif /* SQLITE_ENABLE_COLUMN_METADATA */
 82327  82547   
 82328  82548   
 82329  82549   /******************************* sqlite3_bind_  ***************************
 82330  82550   ** 
................................................................................
 82683  82903   /*
 82684  82904   ** Return true if the prepared statement is guaranteed to not modify the
 82685  82905   ** database.
 82686  82906   */
 82687  82907   SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
 82688  82908     return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
 82689  82909   }
        82910  +
        82911  +/*
        82912  +** Return 1 if the statement is an EXPLAIN and return 2 if the
        82913  +** statement is an EXPLAIN QUERY PLAN
        82914  +*/
        82915  +SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
        82916  +  return pStmt ? ((Vdbe*)pStmt)->explain : 0;
        82917  +}
 82690  82918   
 82691  82919   /*
 82692  82920   ** Return true if the prepared statement is in need of being reset.
 82693  82921   */
 82694  82922   SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
 82695  82923     Vdbe *v = (Vdbe*)pStmt;
 82696  82924     return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
................................................................................
 83373  83601   #endif
 83374  83602   
 83375  83603   /*
 83376  83604   ** Invoke the VDBE coverage callback, if that callback is defined.  This
 83377  83605   ** feature is used for test suite validation only and does not appear an
 83378  83606   ** production builds.
 83379  83607   **
 83380         -** M is an integer between 2 and 4.  2 indicates a ordinary two-way
 83381         -** branch (I=0 means fall through and I=1 means taken).  3 indicates
 83382         -** a 3-way branch where the third way is when one of the operands is
 83383         -** NULL.  4 indicates the OP_Jump instruction which has three destinations
 83384         -** depending on whether the first operand is less than, equal to, or greater
 83385         -** than the second. 
        83608  +** M is the type of branch.  I is the direction taken for this instance of
        83609  +** the branch.
        83610  +**
        83611  +**   M: 2 - two-way branch (I=0: fall-thru   1: jump                )
        83612  +**      3 - two-way + NULL (I=0: fall-thru   1: jump      2: NULL   )
        83613  +**      4 - OP_Jump        (I=0: jump p1     1: jump p2   2: jump p3)
        83614  +**
        83615  +** In other words, if M is 2, then I is either 0 (for fall-through) or
        83616  +** 1 (for when the branch is taken).  If M is 3, the I is 0 for an
        83617  +** ordinary fall-through, I is 1 if the branch was taken, and I is 2 
        83618  +** if the result of comparison is NULL.  For M=3, I=2 the jump may or
        83619  +** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
        83620  +** When M is 4, that means that an OP_Jump is being run.  I is 0, 1, or 2
        83621  +** depending on if the operands are less than, equal, or greater than.
 83386  83622   **
 83387  83623   ** iSrcLine is the source code line (from the __LINE__ macro) that
 83388  83624   ** generated the VDBE instruction combined with flag bits.  The source
 83389  83625   ** code line number is in the lower 24 bits of iSrcLine and the upper
 83390  83626   ** 8 bytes are flags.  The lower three bits of the flags indicate
 83391  83627   ** values for I that should never occur.  For example, if the branch is
 83392  83628   ** always taken, the flags should be 0x05 since the fall-through and
 83393  83629   ** alternate branch are never taken.  If a branch is never taken then
 83394  83630   ** flags should be 0x06 since only the fall-through approach is allowed.
 83395  83631   **
 83396         -** Bit 0x04 of the flags indicates an OP_Jump opcode that is only
        83632  +** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
 83397  83633   ** interested in equal or not-equal.  In other words, I==0 and I==2
 83398         -** should be treated the same.
        83634  +** should be treated as equivalent
 83399  83635   **
 83400  83636   ** Since only a line number is retained, not the filename, this macro
 83401  83637   ** only works for amalgamation builds.  But that is ok, since these macros
 83402  83638   ** should be no-ops except for special builds used to measure test coverage.
 83403  83639   */
 83404  83640   #if !defined(SQLITE_VDBE_COVERAGE)
 83405  83641   # define VdbeBranchTaken(I,M)
................................................................................
 83415  83651       /* The upper 8 bits of iSrcLine are flags.  The lower three bits of
 83416  83652       ** the flags indicate directions that the branch can never go.  If
 83417  83653       ** a branch really does go in one of those directions, assert right
 83418  83654       ** away. */
 83419  83655       mNever = iSrcLine >> 24;
 83420  83656       assert( (I & mNever)==0 );
 83421  83657       if( sqlite3GlobalConfig.xVdbeBranch==0 ) return;  /*NO_TEST*/
        83658  +    /* Invoke the branch coverage callback with three arguments:
        83659  +    **    iSrcLine - the line number of the VdbeCoverage() macro, with
        83660  +    **               flags removed.
        83661  +    **    I        - Mask of bits 0x07 indicating which cases are are
        83662  +    **               fulfilled by this instance of the jump.  0x01 means
        83663  +    **               fall-thru, 0x02 means taken, 0x04 means NULL.  Any
        83664  +    **               impossible cases (ex: if the comparison is never NULL)
        83665  +    **               are filled in automatically so that the coverage
        83666  +    **               measurement logic does not flag those impossible cases
        83667  +    **               as missed coverage.
        83668  +    **    M        - Type of jump.  Same as M argument above
        83669  +    */
 83422  83670       I |= mNever;
 83423  83671       if( M==2 ) I |= 0x04;
 83424  83672       if( M==4 ){
 83425  83673         I |= 0x08;
 83426  83674         if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
 83427  83675       }
 83428  83676       sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
................................................................................
 83962  84210   #ifdef VDBE_PROFILE
 83963  84211     u64 start;                 /* CPU clock count at start of opcode */
 83964  84212   #endif
 83965  84213     /*** INSERT STACK UNION HERE ***/
 83966  84214   
 83967  84215     assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 83968  84216     sqlite3VdbeEnter(p);
        84217  +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
        84218  +  if( db->xProgress ){
        84219  +    u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
        84220  +    assert( 0 < db->nProgressOps );
        84221  +    nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
        84222  +  }else{
        84223  +    nProgressLimit = 0xffffffff;
        84224  +  }
        84225  +#endif
 83969  84226     if( p->rc==SQLITE_NOMEM ){
 83970  84227       /* This happens if a malloc() inside a call to sqlite3_column_text() or
 83971  84228       ** sqlite3_column_text16() failed.  */
 83972  84229       goto no_mem;
 83973  84230     }
 83974  84231     assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
 83975  84232     assert( p->bIsReader || p->readOnly!=0 );
 83976  84233     p->iCurrentTime = 0;
 83977  84234     assert( p->explain==0 );
 83978  84235     p->pResultSet = 0;
 83979  84236     db->busyHandler.nBusy = 0;
 83980  84237     if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 83981  84238     sqlite3VdbeIOTraceSql(p);
 83982         -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 83983         -  if( db->xProgress ){
 83984         -    u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
 83985         -    assert( 0 < db->nProgressOps );
 83986         -    nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
 83987         -  }else{
 83988         -    nProgressLimit = 0xffffffff;
 83989         -  }
 83990         -#endif
 83991  84239   #ifdef SQLITE_DEBUG
 83992  84240     sqlite3BeginBenignMalloc();
 83993  84241     if( p->pc==0
 83994  84242      && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
 83995  84243     ){
 83996  84244       int i;
 83997  84245       int once = 1;
................................................................................
 84159  84407   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 84160  84408     /* Call the progress callback if it is configured and the required number
 84161  84409     ** of VDBE ops have been executed (either since this invocation of
 84162  84410     ** sqlite3VdbeExec() or since last time the progress callback was called).
 84163  84411     ** If the progress callback returns non-zero, exit the virtual machine with
 84164  84412     ** a return code SQLITE_ABORT.
 84165  84413     */
 84166         -  if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
        84414  +  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
 84167  84415       assert( db->nProgressOps!=0 );
 84168         -    nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
        84416  +    nProgressLimit += db->nProgressOps;
 84169  84417       if( db->xProgress(db->pProgressArg) ){
        84418  +      nProgressLimit = 0xffffffff;
 84170  84419         rc = SQLITE_INTERRUPT;
 84171  84420         goto abort_due_to_error;
 84172  84421       }
 84173  84422     }
 84174  84423   #endif
 84175  84424     
 84176  84425     break;
................................................................................
 84441  84690     pOp->opcode = OP_String;
 84442  84691     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
 84443  84692   
 84444  84693   #ifndef SQLITE_OMIT_UTF16
 84445  84694     if( encoding!=SQLITE_UTF8 ){
 84446  84695       rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
 84447  84696       assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
        84697  +    if( rc ) goto too_big;
 84448  84698       if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
 84449  84699       assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
 84450  84700       assert( VdbeMemDynamic(pOut)==0 );
 84451  84701       pOut->szMalloc = 0;
 84452  84702       pOut->flags |= MEM_Static;
 84453  84703       if( pOp->p4type==P4_DYNAMIC ){
 84454  84704         sqlite3DbFree(db, pOp->p4.z);
 84455  84705       }
 84456  84706       pOp->p4type = P4_DYNAMIC;
 84457  84707       pOp->p4.z = pOut->z;
 84458  84708       pOp->p1 = pOut->n;
 84459  84709     }
 84460         -  testcase( rc==SQLITE_TOOBIG );
 84461  84710   #endif
 84462  84711     if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 84463  84712       goto too_big;
 84464  84713     }
 84465  84714     assert( rc==SQLITE_OK );
 84466  84715     /* Fall through to the next case, OP_String */
 84467  84716   }
................................................................................
 84575  84824     assert( pOp->p1>0 && pOp->p1<=p->nVar );
 84576  84825     assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
 84577  84826     pVar = &p->aVar[pOp->p1 - 1];
 84578  84827     if( sqlite3VdbeMemTooBig(pVar) ){
 84579  84828       goto too_big;
 84580  84829     }
 84581  84830     pOut = &aMem[pOp->p2];
 84582         -  sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
        84831  +  if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
        84832  +  memcpy(pOut, pVar, MEMCELLSIZE);
        84833  +  pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
        84834  +  pOut->flags |= MEM_Static|MEM_FromBind;
 84583  84835     UPDATE_MAX_BLOBSIZE(pOut);
 84584  84836     break;
 84585  84837   }
 84586  84838   
 84587  84839   /* Opcode: Move P1 P2 P3 * *
 84588  84840   ** Synopsis: r[P2@P3]=r[P1@P3]
 84589  84841   **
................................................................................
 84708  84960   case OP_ResultRow: {
 84709  84961     Mem *pMem;
 84710  84962     int i;
 84711  84963     assert( p->nResColumn==pOp->p2 );
 84712  84964     assert( pOp->p1>0 );
 84713  84965     assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
 84714  84966   
 84715         -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 84716         -  /* Run the progress counter just before returning.
 84717         -  */
 84718         -  if( db->xProgress!=0
 84719         -   && nVmStep>=nProgressLimit 
 84720         -   && db->xProgress(db->pProgressArg)!=0
 84721         -  ){
 84722         -    rc = SQLITE_INTERRUPT;
 84723         -    goto abort_due_to_error;
 84724         -  }
 84725         -#endif
 84726         -
 84727  84967     /* If this statement has violated immediate foreign key constraints, do
 84728  84968     ** not return the number of rows modified. And do not RELEASE the statement
 84729  84969     ** transaction. It needs to be rolled back.  */
 84730  84970     if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
 84731  84971       assert( db->flags&SQLITE_CountRows );
 84732  84972       assert( p->usesStmtJournal );
 84733  84973       goto abort_due_to_error;
................................................................................
 85085  85325   ** without data loss, then jump immediately to P2, or if P2==0
 85086  85326   ** raise an SQLITE_MISMATCH exception.
 85087  85327   */
 85088  85328   case OP_MustBeInt: {            /* jump, in1 */
 85089  85329     pIn1 = &aMem[pOp->p1];
 85090  85330     if( (pIn1->flags & MEM_Int)==0 ){
 85091  85331       applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
 85092         -    VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
 85093  85332       if( (pIn1->flags & MEM_Int)==0 ){
        85333  +      VdbeBranchTaken(1, 2);
 85094  85334         if( pOp->p2==0 ){
 85095  85335           rc = SQLITE_MISMATCH;
 85096  85336           goto abort_due_to_error;
 85097  85337         }else{
 85098  85338           goto jump_to_p2;
 85099  85339         }
 85100  85340       }
 85101  85341     }
        85342  +  VdbeBranchTaken(0, 2);
 85102  85343     MemSetTypeFlag(pIn1, MEM_Int);
 85103  85344     break;
 85104  85345   }
 85105  85346   
 85106  85347   #ifndef SQLITE_OMIT_FLOATING_POINT
 85107  85348   /* Opcode: RealAffinity P1 * * * *
 85108  85349   **
................................................................................
 85269  85510     if( (flags1 | flags3)&MEM_Null ){
 85270  85511       /* One or both operands are NULL */
 85271  85512       if( pOp->p5 & SQLITE_NULLEQ ){
 85272  85513         /* If SQLITE_NULLEQ is set (which will only happen if the operator is
 85273  85514         ** OP_Eq or OP_Ne) then take the jump or not depending on whether
 85274  85515         ** or not both operands are null.
 85275  85516         */
 85276         -      assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
 85277  85517         assert( (flags1 & MEM_Cleared)==0 );
 85278  85518         assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
 85279  85519         testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
 85280  85520         if( (flags1&flags3&MEM_Null)!=0
 85281  85521          && (flags3&MEM_Cleared)==0
 85282  85522         ){
 85283  85523           res = 0;  /* Operands are equal */
 85284  85524         }else{
 85285         -        res = 1;  /* Operands are not equal */
        85525  +        res = ((flags3 & MEM_Null) ? -1 : +1);  /* Operands are not equal */
 85286  85526         }
 85287  85527       }else{
 85288  85528         /* SQLITE_NULLEQ is clear and at least one operand is NULL,
 85289  85529         ** then the result is always NULL.
 85290  85530         ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
 85291  85531         */
 85292  85532         if( pOp->p5 & SQLITE_STOREP2 ){
................................................................................
 85396  85636         if( (pOp->opcode==OP_Eq)==res2 ) break;
 85397  85637       }
 85398  85638       memAboutToChange(p, pOut);
 85399  85639       MemSetTypeFlag(pOut, MEM_Int);
 85400  85640       pOut->u.i = res2;
 85401  85641       REGISTER_TRACE(pOp->p2, pOut);
 85402  85642     }else{
 85403         -    VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
        85643  +    VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
 85404  85644       if( res2 ){
 85405  85645         goto jump_to_p2;
 85406  85646       }
 85407  85647     }
 85408  85648     break;
 85409  85649   }
 85410  85650   
................................................................................
 85946  86186       op_column_read_header:
 85947  86187         i = pC->nHdrParsed;
 85948  86188         offset64 = aOffset[i];
 85949  86189         zHdr = zData + pC->iHdrOffset;
 85950  86190         zEndHdr = zData + aOffset[0];
 85951  86191         testcase( zHdr>=zEndHdr );
 85952  86192         do{
 85953         -        if( (t = zHdr[0])<0x80 ){
        86193  +        if( (pC->aType[i] = t = zHdr[0])<0x80 ){
 85954  86194             zHdr++;
 85955  86195             offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
 85956  86196           }else{
 85957  86197             zHdr += sqlite3GetVarint32(zHdr, &t);
        86198  +          pC->aType[i] = t;
 85958  86199             offset64 += sqlite3VdbeSerialTypeLen(t);
 85959  86200           }
 85960         -        pC->aType[i++] = t;
 85961         -        aOffset[i] = (u32)(offset64 & 0xffffffff);
        86201  +        aOffset[++i] = (u32)(offset64 & 0xffffffff);
 85962  86202         }while( i<=p2 && zHdr<zEndHdr );
 85963  86203   
 85964  86204         /* The record is corrupt if any of the following are true:
 85965  86205         ** (1) the bytes of the header extend past the declared header size
 85966  86206         ** (2) the entire header was used but not all data was used
 85967  86207         ** (3) the end of the data extends beyond the end of the record.
 85968  86208         */
................................................................................
 86957  87197     pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
 86958  87198     if( pCx==0 ) goto no_mem;
 86959  87199     pCx->nullRow = 1;
 86960  87200     pCx->isEphemeral = 1;
 86961  87201     pCx->pKeyInfo = pOrig->pKeyInfo;
 86962  87202     pCx->isTable = pOrig->isTable;
 86963  87203     pCx->pgnoRoot = pOrig->pgnoRoot;
        87204  +  pCx->isOrdered = pOrig->isOrdered;
 86964  87205     rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
 86965  87206                             pCx->pKeyInfo, pCx->uc.pCursor);
 86966  87207     /* The sqlite3BtreeCursor() routine can only fail for the first cursor
 86967  87208     ** opened for a database.  Since there is already an open cursor when this
 86968  87209     ** opcode is run, the sqlite3BtreeCursor() cannot fail */
 86969  87210     assert( rc==SQLITE_OK );
 86970  87211     break;
................................................................................
 87920  88161   ** and register P2 becomes ephemeral.  If the cursor is changed, the
 87921  88162   ** value of register P2 will then change.  Make sure this does not
 87922  88163   ** cause any problems.)
 87923  88164   **
 87924  88165   ** This instruction only works on tables.  The equivalent instruction
 87925  88166   ** for indices is OP_IdxInsert.
 87926  88167   */
 87927         -/* Opcode: InsertInt P1 P2 P3 P4 P5
 87928         -** Synopsis: intkey=P3 data=r[P2]
 87929         -**
 87930         -** This works exactly like OP_Insert except that the key is the
 87931         -** integer value P3, not the value of the integer stored in register P3.
 87932         -*/
 87933         -case OP_Insert: 
 87934         -case OP_InsertInt: {
        88168  +case OP_Insert: {
 87935  88169     Mem *pData;       /* MEM cell holding data for the record to be inserted */
 87936  88170     Mem *pKey;        /* MEM cell holding key  for the record */
 87937  88171     VdbeCursor *pC;   /* Cursor to table into which insert is written */
 87938  88172     int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 87939  88173     const char *zDb;  /* database name - used by the update hook */
 87940  88174     Table *pTab;      /* Table structure - used by update and pre-update hooks */
 87941  88175     BtreePayload x;   /* Payload to be inserted */
................................................................................
 87948  88182     assert( pC->eCurType==CURTYPE_BTREE );
 87949  88183     assert( pC->uc.pCursor!=0 );
 87950  88184     assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
 87951  88185     assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
 87952  88186     REGISTER_TRACE(pOp->p2, pData);
 87953  88187     sqlite3VdbeIncrWriteCounter(p, pC);
 87954  88188   
 87955         -  if( pOp->opcode==OP_Insert ){
 87956         -    pKey = &aMem[pOp->p3];
 87957         -    assert( pKey->flags & MEM_Int );
 87958         -    assert( memIsValid(pKey) );
 87959         -    REGISTER_TRACE(pOp->p3, pKey);
 87960         -    x.nKey = pKey->u.i;
 87961         -  }else{
 87962         -    assert( pOp->opcode==OP_InsertInt );
 87963         -    x.nKey = pOp->p3;
 87964         -  }
        88189  +  pKey = &aMem[pOp->p3];
        88190  +  assert( pKey->flags & MEM_Int );
        88191  +  assert( memIsValid(pKey) );
        88192  +  REGISTER_TRACE(pOp->p3, pKey);
        88193  +  x.nKey = pKey->u.i;
 87965  88194   
 87966  88195     if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 87967  88196       assert( pC->iDb>=0 );
 87968  88197       zDb = db->aDb[pC->iDb].zDbSName;
 87969  88198       pTab = pOp->p4.pTab;
 87970  88199       assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
 87971  88200     }else{
................................................................................
 88477  88706   #ifdef SQLITE_TEST
 88478  88707     sqlite3_sort_count++;
 88479  88708     sqlite3_search_count--;
 88480  88709   #endif
 88481  88710     p->aCounter[SQLITE_STMTSTATUS_SORT]++;
 88482  88711     /* Fall through into OP_Rewind */
 88483  88712   }
 88484         -/* Opcode: Rewind P1 P2 * * P5
        88713  +/* Opcode: Rewind P1 P2 * * *
 88485  88714   **
 88486  88715   ** The next use of the Rowid or Column or Next instruction for P1 
 88487  88716   ** will refer to the first entry in the database table or index.
 88488  88717   ** If the table or index is empty, jump immediately to P2.
 88489  88718   ** If the table or index is not empty, fall through to the following 
 88490  88719   ** instruction.
 88491  88720   **
 88492         -** If P5 is non-zero and the table is not empty, then the "skip-next"
 88493         -** flag is set on the cursor so that the next OP_Next instruction 
 88494         -** executed on it is a no-op.
 88495         -**
 88496  88721   ** This opcode leaves the cursor configured to move in forward order,
 88497  88722   ** from the beginning toward the end.  In other words, the cursor is
 88498  88723   ** configured to use Next, not Prev.
 88499  88724   */
 88500  88725   case OP_Rewind: {        /* jump */
 88501  88726     VdbeCursor *pC;
 88502  88727     BtCursor *pCrsr;
 88503  88728     int res;
 88504  88729   
 88505  88730     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
        88731  +  assert( pOp->p5==0 );
 88506  88732     pC = p->apCsr[pOp->p1];
 88507  88733     assert( pC!=0 );
 88508  88734     assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
 88509  88735     res = 1;
 88510  88736   #ifdef SQLITE_DEBUG
 88511  88737     pC->seekOp = OP_Rewind;
 88512  88738   #endif
................................................................................
 88513  88739     if( isSorter(pC) ){
 88514  88740       rc = sqlite3VdbeSorterRewind(pC, &res);
 88515  88741     }else{
 88516  88742       assert( pC->eCurType==CURTYPE_BTREE );
 88517  88743       pCrsr = pC->uc.pCursor;
 88518  88744       assert( pCrsr );
 88519  88745       rc = sqlite3BtreeFirst(pCrsr, &res);
 88520         -#ifndef SQLITE_OMIT_WINDOWFUNC
 88521         -    if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr);
 88522         -#endif
 88523  88746       pC->deferredMoveto = 0;
 88524  88747       pC->cacheStatus = CACHE_STALE;
 88525  88748     }
 88526  88749     if( rc ) goto abort_due_to_error;
 88527  88750     pC->nullRow = (u8)res;
 88528  88751     assert( pOp->p2>0 && pOp->p2<p->nOp );
 88529  88752     VdbeBranchTaken(res!=0,2);
................................................................................
 89525  89748       for(i=0; i<p->nMem; i++){
 89526  89749         aMem[i].pScopyFrom = 0;  /* Prevent false-positive AboutToChange() errs */
 89527  89750         aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
 89528  89751       }
 89529  89752     }
 89530  89753   #endif
 89531  89754     pOp = &aOp[-1];
 89532         -
 89533         -  break;
        89755  +  goto check_for_interrupt;
 89534  89756   }
 89535  89757   
 89536  89758   /* Opcode: Param P1 P2 * * *
 89537  89759   **
 89538  89760   ** This opcode is only ever present in sub-programs called via the 
 89539  89761   ** OP_Program instruction. Copy a value currently stored in a memory 
 89540  89762   ** cell of the calling (parent) frame to cell P2 in the current frames 
................................................................................
 89898  90120     Mem *pMem;
 89899  90121     assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
 89900  90122     assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
 89901  90123     pMem = &aMem[pOp->p1];
 89902  90124     assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
 89903  90125   #ifndef SQLITE_OMIT_WINDOWFUNC
 89904  90126     if( pOp->p3 ){
        90127  +    memAboutToChange(p, &aMem[pOp->p3]);
 89905  90128       rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
 89906  90129       pMem = &aMem[pOp->p3];
 89907  90130     }else
 89908  90131   #endif
 89909  90132     {
 89910  90133       rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
 89911  90134     }
................................................................................
 90935  91158       sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
 90936  91159     }
 90937  91160   
 90938  91161     /* This is the only way out of this procedure.  We have to
 90939  91162     ** release the mutexes on btrees that were acquired at the
 90940  91163     ** top. */
 90941  91164   vdbe_return:
 90942         -  testcase( nVmStep>0 );
        91165  +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
        91166  +  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
        91167  +    nProgressLimit += db->nProgressOps;
        91168  +    if( db->xProgress(db->pProgressArg) ){
        91169  +      nProgressLimit = 0xffffffff;
        91170  +      rc = SQLITE_INTERRUPT;
        91171  +      goto abort_due_to_error;
        91172  +    }
        91173  +  }
        91174  +#endif
 90943  91175     p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
 90944  91176     sqlite3VdbeLeave(p);
 90945  91177     assert( rc!=SQLITE_OK || nExtraDelete==0 
 90946  91178          || sqlite3_strlike("DELETE%",p->zSql,0)!=0 
 90947  91179     );
 90948  91180     return rc;
 90949  91181   
................................................................................
 92022  92254       ** In this case, allocate space at p->aAlloc[] to copy the requested
 92023  92255       ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
 92024  92256       int nRem;                     /* Bytes remaining to copy */
 92025  92257   
 92026  92258       /* Extend the p->aAlloc[] allocation if required. */
 92027  92259       if( p->nAlloc<nByte ){
 92028  92260         u8 *aNew;
 92029         -      int nNew = MAX(128, p->nAlloc*2);
        92261  +      sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
 92030  92262         while( nByte>nNew ) nNew = nNew*2;
 92031  92263         aNew = sqlite3Realloc(p->aAlloc, nNew);
 92032  92264         if( !aNew ) return SQLITE_NOMEM_BKPT;
 92033  92265         p->nAlloc = nNew;
 92034  92266         p->aAlloc = aNew;
 92035  92267       }
 92036  92268   
................................................................................
 93314  93546   
 93315  93547     if( pSorter->list.aMemory ){
 93316  93548       int nMin = pSorter->iMemory + nReq;
 93317  93549   
 93318  93550       if( nMin>pSorter->nMemory ){
 93319  93551         u8 *aNew;
 93320  93552         int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
 93321         -      int nNew = pSorter->nMemory * 2;
        93553  +      sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
 93322  93554         while( nNew < nMin ) nNew = nNew*2;
 93323  93555         if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
 93324  93556         if( nNew < nMin ) nNew = nMin;
 93325  93557   
 93326  93558         aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
 93327  93559         if( !aNew ) return SQLITE_NOMEM_BKPT;
 93328  93560         pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
................................................................................
 95307  95539             assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 95308  95540             assert( pExpr->x.pList==0 );
 95309  95541             assert( pExpr->x.pSelect==0 );
 95310  95542             pOrig = pEList->a[j].pExpr;
 95311  95543             if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
 95312  95544               sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
 95313  95545               return WRC_Abort;
        95546  +          }
        95547  +          if( (pNC->ncFlags&NC_AllowWin)==0 && ExprHasProperty(pOrig, EP_Win) ){
        95548  +            sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
        95549  +            return WRC_Abort;
 95314  95550             }
 95315  95551             if( sqlite3ExprVectorSize(pOrig)!=1 ){
 95316  95552               sqlite3ErrorMsg(pParse, "row value misused");
 95317  95553               return WRC_Abort;
 95318  95554             }
 95319  95555             resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
 95320  95556             cnt = 1;
................................................................................
 95598  95834         int no_such_func = 0;       /* True if no such function exists */
 95599  95835         int wrong_num_args = 0;     /* True if wrong number of arguments */
 95600  95836         int is_agg = 0;             /* True if is an aggregate function */
 95601  95837         int nId;                    /* Number of characters in function name */
 95602  95838         const char *zId;            /* The function name. */
 95603  95839         FuncDef *pDef;              /* Information about the function */
 95604  95840         u8 enc = ENC(pParse->db);   /* The database encoding */
        95841  +      int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
 95605  95842   
 95606  95843         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 95607  95844         zId = pExpr->u.zToken;
 95608  95845         nId = sqlite3Strlen30(zId);
 95609  95846         pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
 95610  95847         if( pDef==0 ){
 95611  95848           pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
................................................................................
 95719  95956             pNC->nErr++;
 95720  95957           }else if( wrong_num_args ){
 95721  95958             sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 95722  95959                  nId, zId);
 95723  95960             pNC->nErr++;
 95724  95961           }
 95725  95962           if( is_agg ){
        95963  +          /* Window functions may not be arguments of aggregate functions.
        95964  +          ** Or arguments of other window functions. But aggregate functions
        95965  +          ** may be arguments for window functions.  */
 95726  95966   #ifndef SQLITE_OMIT_WINDOWFUNC
 95727         -          pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg);
        95967  +          pNC->ncFlags &= ~(NC_AllowWin | (!pExpr->y.pWin ? NC_AllowAgg : 0));
 95728  95968   #else
 95729  95969             pNC->ncFlags &= ~NC_AllowAgg;
 95730  95970   #endif
 95731  95971           }
 95732  95972         }
 95733  95973         sqlite3WalkExprList(pWalker, pList);
 95734  95974         if( is_agg ){
................................................................................
 95741  95981             sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
 95742  95982             if( 0==pSel->pWin 
 95743  95983              || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) 
 95744  95984             ){
 95745  95985               pExpr->y.pWin->pNextWin = pSel->pWin;
 95746  95986               pSel->pWin = pExpr->y.pWin;
 95747  95987             }
 95748         -          pNC->ncFlags |= NC_AllowWin;
        95988  +          pNC->ncFlags |= NC_HasWin;
 95749  95989           }else
 95750  95990   #endif /* SQLITE_OMIT_WINDOWFUNC */
 95751  95991           {
 95752  95992             NameContext *pNC2 = pNC;
 95753  95993             pExpr->op = TK_AGG_FUNCTION;
 95754  95994             pExpr->op2 = 0;
 95755  95995             while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
................................................................................
 95759  95999             assert( pDef!=0 );
 95760  96000             if( pNC2 ){
 95761  96001               assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
 95762  96002               testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
 95763  96003               pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
 95764  96004   
 95765  96005             }
 95766         -          pNC->ncFlags |= NC_AllowAgg;
 95767  96006           }
        96007  +        pNC->ncFlags |= savedAllowFlags;
 95768  96008         }
 95769  96009         /* FIX ME:  Compute pExpr->affinity based on the expected return
 95770  96010         ** type of the function 
 95771  96011         */
 95772  96012         return WRC_Prune;
 95773  96013       }
 95774  96014   #ifndef SQLITE_OMIT_SUBQUERY
................................................................................
 96116  96356         resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
 96117  96357                      zType,0);
 96118  96358       }
 96119  96359     }
 96120  96360     return 0;
 96121  96361   }
 96122  96362   
        96363  +#ifndef SQLITE_OMIT_WINDOWFUNC
        96364  +/*
        96365  +** Walker callback for resolveRemoveWindows().
        96366  +*/
        96367  +static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
        96368  +  if( ExprHasProperty(pExpr, EP_WinFunc) ){
        96369  +    Window **pp;
        96370  +    for(pp=&pWalker->u.pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
        96371  +      if( *pp==pExpr->y.pWin ){
        96372  +        *pp = (*pp)->pNextWin;
        96373  +        break;
        96374  +      }    
        96375  +    }
        96376  +  }
        96377  +  return WRC_Continue;
        96378  +}
        96379  +
        96380  +/*
        96381  +** Remove any Window objects owned by the expression pExpr from the
        96382  +** Select.pWin list of Select object pSelect.
        96383  +*/
        96384  +static void resolveRemoveWindows(Select *pSelect, Expr *pExpr){
        96385  +  Walker sWalker;
        96386  +  memset(&sWalker, 0, sizeof(Walker));
        96387  +  sWalker.xExprCallback = resolveRemoveWindowsCb;
        96388  +  sWalker.u.pSelect = pSelect;
        96389  +  sqlite3WalkExpr(&sWalker, pExpr);
        96390  +}
        96391  +#else
        96392  +# define resolveRemoveWindows(x,y)
        96393  +#endif
        96394  +
 96123  96395   /*
 96124  96396   ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
 96125  96397   ** The Name context of the SELECT statement is pNC.  zType is either
 96126  96398   ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
 96127  96399   **
 96128  96400   ** This routine resolves each term of the clause into an expression.
 96129  96401   ** If the order-by term is an integer I between 1 and N (where N is the
................................................................................
 96182  96454       /* Otherwise, treat the ORDER BY term as an ordinary expression */
 96183  96455       pItem->u.x.iOrderByCol = 0;
 96184  96456       if( sqlite3ResolveExprNames(pNC, pE) ){
 96185  96457         return 1;
 96186  96458       }
 96187  96459       for(j=0; j<pSelect->pEList->nExpr; j++){
 96188  96460         if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
 96189         -#ifndef SQLITE_OMIT_WINDOWFUNC
 96190         -        if( ExprHasProperty(pE, EP_WinFunc) ){
 96191         -          /* Since this window function is being changed into a reference
 96192         -          ** to the same window function the result set, remove the instance
 96193         -          ** of this window function from the Select.pWin list. */
 96194         -          Window **pp;
 96195         -          for(pp=&pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
 96196         -            if( *pp==pE->y.pWin ){
 96197         -              *pp = (*pp)->pNextWin;
 96198         -            }    
 96199         -          }
 96200         -        }
 96201         -#endif
        96461  +        /* Since this expresion is being changed into a reference
        96462  +        ** to an identical expression in the result set, remove all Window
        96463  +        ** objects belonging to the expression from the Select.pWin list. */
        96464  +        resolveRemoveWindows(pSelect, pE);
 96202  96465           pItem->u.x.iOrderByCol = j+1;
 96203  96466         }
 96204  96467       }
 96205  96468     }
 96206  96469     return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
 96207  96470   }
 96208  96471   
................................................................................
 96274  96537         p->pOrderBy = 0;
 96275  96538       }
 96276  96539     
 96277  96540       /* Recursively resolve names in all subqueries
 96278  96541       */
 96279  96542       for(i=0; i<p->pSrc->nSrc; i++){
 96280  96543         struct SrcList_item *pItem = &p->pSrc->a[i];
 96281         -      if( pItem->pSelect ){
        96544  +      if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
 96282  96545           NameContext *pNC;         /* Used to iterate name contexts */
 96283  96546           int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
 96284  96547           const char *zSavedContext = pParse->zAuthContext;
 96285  96548   
 96286  96549           /* Count the total number of references to pOuterNC and all of its
 96287  96550           ** parent contexts. After resolving references to expressions in
 96288  96551           ** pItem->pSelect, check if this value has changed. If so, then
................................................................................
 96406  96669             sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
 96407  96670                 "the GROUP BY clause");
 96408  96671             return WRC_Abort;
 96409  96672           }
 96410  96673         }
 96411  96674       }
 96412  96675   
        96676  +#ifndef SQLITE_OMIT_WINDOWFUNC
 96413  96677       if( IN_RENAME_OBJECT ){
 96414  96678         Window *pWin;
 96415  96679         for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
 96416  96680           if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
 96417  96681            || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
 96418  96682           ){
 96419  96683             return WRC_Abort;
 96420  96684           }
 96421  96685         }
 96422  96686       }
        96687  +#endif
 96423  96688   
 96424  96689       /* If this is part of a compound SELECT, check that it has the right
 96425  96690       ** number of expressions in the select list. */
 96426  96691       if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
 96427  96692         sqlite3SelectWrongNumTermsError(pParse, p->pNext);
 96428  96693         return WRC_Abort;
 96429  96694       }
................................................................................
 96496  96761     NameContext *pNC,       /* Namespace to resolve expressions in. */
 96497  96762     Expr *pExpr             /* The expression to be analyzed. */
 96498  96763   ){
 96499  96764     u16 savedHasAgg;
 96500  96765     Walker w;
 96501  96766   
 96502  96767     if( pExpr==0 ) return SQLITE_OK;
 96503         -  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
 96504         -  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
        96768  +  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
        96769  +  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
 96505  96770     w.pParse = pNC->pParse;
 96506  96771     w.xExprCallback = resolveExprStep;
 96507  96772     w.xSelectCallback = resolveSelectStep;
 96508  96773     w.xSelectCallback2 = 0;
 96509  96774     w.u.pNC = pNC;
 96510  96775   #if SQLITE_MAX_EXPR_DEPTH>0
 96511  96776     w.pParse->nHeight += pExpr->nHeight;
................................................................................
 96513  96778       return SQLITE_ERROR;
 96514  96779     }
 96515  96780   #endif
 96516  96781     sqlite3WalkExpr(&w, pExpr);
 96517  96782   #if SQLITE_MAX_EXPR_DEPTH>0
 96518  96783     w.pParse->nHeight -= pExpr->nHeight;
 96519  96784   #endif
 96520         -  if( pNC->ncFlags & NC_HasAgg ){
 96521         -    ExprSetProperty(pExpr, EP_Agg);
 96522         -  }
        96785  +  assert( EP_Agg==NC_HasAgg );
        96786  +  assert( EP_Win==NC_HasWin );
        96787  +  testcase( pNC->ncFlags & NC_HasAgg );
        96788  +  testcase( pNC->ncFlags & NC_HasWin );
        96789  +  ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
 96523  96790     pNC->ncFlags |= savedHasAgg;
 96524  96791     return pNC->nErr>0 || w.pParse->nErr>0;
 96525  96792   }
 96526  96793   
 96527  96794   /*
 96528  96795   ** Resolve all names for all expression in an expression list.  This is
 96529  96796   ** just like sqlite3ResolveExprNames() except that it works for an expression
................................................................................
 97471  97738     if( op==TK_AND && pParse->nErr==0 && !IN_RENAME_OBJECT ){
 97472  97739       /* Take advantage of short-circuit false optimization for AND */
 97473  97740       p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
 97474  97741     }else{
 97475  97742       p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
 97476  97743       if( p ){
 97477  97744         memset(p, 0, sizeof(Expr));
 97478         -      p->op = op & TKFLG_MASK;
        97745  +      p->op = op & 0xff;
 97479  97746         p->iAgg = -1;
 97480  97747       }
 97481  97748       sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
 97482  97749     }
 97483  97750     if( p ) {
 97484  97751       sqlite3ExprCheckHeight(pParse, p->nHeight);
 97485  97752     }
................................................................................
 97936  98203   ** argument. If an OOM condition is encountered, NULL is returned
 97937  98204   ** and the db->mallocFailed flag set.
 97938  98205   */
 97939  98206   #ifndef SQLITE_OMIT_CTE
 97940  98207   static With *withDup(sqlite3 *db, With *p){
 97941  98208     With *pRet = 0;
 97942  98209     if( p ){
 97943         -    int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
        98210  +    sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
 97944  98211       pRet = sqlite3DbMallocZero(db, nByte);
 97945  98212       if( pRet ){
 97946  98213         int i;
 97947  98214         pRet->nCte = p->nCte;
 97948  98215         for(i=0; i<p->nCte; i++){
 97949  98216           pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
 97950  98217           pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
................................................................................
 98201  98468       if( pList==0 ){
 98202  98469         goto no_mem;
 98203  98470       }
 98204  98471       pList->nExpr = 0;
 98205  98472     }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
 98206  98473       ExprList *pNew;
 98207  98474       pNew = sqlite3DbRealloc(db, pList, 
 98208         -             sizeof(*pList)+(2*pList->nExpr - 1)*sizeof(pList->a[0]));
        98475  +         sizeof(*pList)+(2*(sqlite3_int64)pList->nExpr-1)*sizeof(pList->a[0]));
 98209  98476       if( pNew==0 ){
 98210  98477         goto no_mem;
 98211  98478       }
 98212  98479       pList = pNew;
 98213  98480     }
 98214  98481     pItem = &pList->a[pList->nExpr++];
 98215  98482     assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) );
................................................................................
 99166  99433       ** We will have to generate an ephemeral table to do the job.
 99167  99434       */
 99168  99435       u32 savedNQueryLoop = pParse->nQueryLoop;
 99169  99436       int rMayHaveNull = 0;
 99170  99437       eType = IN_INDEX_EPH;
 99171  99438       if( inFlags & IN_INDEX_LOOP ){
 99172  99439         pParse->nQueryLoop = 0;
 99173         -      if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
 99174         -        eType = IN_INDEX_ROWID;
 99175         -      }
 99176  99440       }else if( prRhsHasNull ){
 99177  99441         *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
 99178  99442       }
 99179  99443       assert( pX->op==TK_IN );
 99180         -    sqlite3CodeRhsOfIN(pParse, pX, iTab, eType==IN_INDEX_ROWID);
        99444  +    sqlite3CodeRhsOfIN(pParse, pX, iTab);
 99181  99445       if( rMayHaveNull ){
 99182  99446         sqlite3SetHasNullFlag(v, iTab, rMayHaveNull);
 99183  99447       }
 99184  99448       pParse->nQueryLoop = savedNQueryLoop;
 99185  99449     }
 99186  99450   
 99187  99451     if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
................................................................................
 99274  99538   **
 99275  99539   ** The pExpr parameter is the IN operator.  The cursor number for the
 99276  99540   ** constructed ephermeral table is returned.  The first time the ephemeral
 99277  99541   ** table is computed, the cursor number is also stored in pExpr->iTable,
 99278  99542   ** however the cursor number returned might not be the same, as it might
 99279  99543   ** have been duplicated using OP_OpenDup.
 99280  99544   **
 99281         -** If parameter isRowid is non-zero, then LHS of the IN operator is guaranteed
 99282         -** to be a non-null integer. In this case, the ephemeral table can be an
 99283         -** table B-Tree that keyed by only integers.  The more general cases uses
 99284         -** an index B-Tree which can have arbitrary keys, but is slower to both
 99285         -** read and write.
 99286         -**
 99287  99545   ** If the LHS expression ("x" in the examples) is a column value, or
 99288  99546   ** the SELECT statement returns a column value, then the affinity of that
 99289  99547   ** column is used to build the index keys. If both 'x' and the
 99290  99548   ** SELECT... statement are columns, then numeric affinity is used
 99291  99549   ** if either column has NUMERIC or INTEGER affinity. If neither
 99292  99550   ** 'x' nor the SELECT... statement are columns, then numeric affinity
 99293  99551   ** is used.
 99294  99552   */
 99295  99553   SQLITE_PRIVATE void sqlite3CodeRhsOfIN(
 99296  99554     Parse *pParse,          /* Parsing context */
 99297  99555     Expr *pExpr,            /* The IN operator */
 99298         -  int iTab,               /* Use this cursor number */
 99299         -  int isRowid             /* If true, LHS is a rowid */
        99556  +  int iTab                /* Use this cursor number */
 99300  99557   ){
 99301  99558     int addrOnce = 0;           /* Address of the OP_Once instruction at top */
 99302  99559     int addr;                   /* Address of OP_OpenEphemeral instruction */
 99303  99560     Expr *pLeft;                /* the LHS of the IN operator */
 99304  99561     KeyInfo *pKeyInfo = 0;      /* Key information */
 99305  99562     int nVal;                   /* Size of vector pLeft */
 99306  99563     Vdbe *v;                    /* The prepared statement under construction */
................................................................................
 99345  99602   
 99346  99603       addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
 99347  99604     }
 99348  99605   
 99349  99606     /* Check to see if this is a vector IN operator */
 99350  99607     pLeft = pExpr->pLeft;
 99351  99608     nVal = sqlite3ExprVectorSize(pLeft);
 99352         -  assert( !isRowid || nVal==1 );
 99353  99609   
 99354  99610     /* Construct the ephemeral table that will contain the content of
 99355  99611     ** RHS of the IN operator.
 99356  99612     */
 99357  99613     pExpr->iTable = iTab;
 99358         -  addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, 
 99359         -      pExpr->iTable, (isRowid?0:nVal));
        99614  +  addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
 99360  99615   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 99361  99616     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 99362  99617       VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
 99363  99618     }else{
 99364  99619       VdbeComment((v, "RHS of IN operator"));
 99365  99620     }
 99366  99621   #endif
 99367         -  pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
        99622  +  pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
 99368  99623   
 99369  99624     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 99370  99625       /* Case 1:     expr IN (SELECT ...)
 99371  99626       **
 99372  99627       ** Generate code to write the results of the select into the temporary
 99373  99628       ** table allocated and opened above.
 99374  99629       */
 99375  99630       Select *pSelect = pExpr->x.pSelect;
 99376  99631       ExprList *pEList = pSelect->pEList;
 99377  99632   
 99378  99633       ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",
 99379  99634           addrOnce?"":"CORRELATED ", pSelect->selId
 99380  99635       ));
 99381         -    assert( !isRowid );
 99382  99636       /* If the LHS and RHS of the IN operator do not match, that
 99383  99637       ** error will have been caught long before we reach this point. */
 99384  99638       if( ALWAYS(pEList->nExpr==nVal) ){
 99385  99639         SelectDest dest;
 99386  99640         int i;
 99387  99641         sqlite3SelectDestInit(&dest, SRT_Set, iTab);
 99388  99642         dest.zAffSdst = exprINAffinity(pParse, pExpr);
................................................................................
 99427  99681         assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 99428  99682         pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 99429  99683       }
 99430  99684   
 99431  99685       /* Loop through each expression in <exprlist>. */
 99432  99686       r1 = sqlite3GetTempReg(pParse);
 99433  99687       r2 = sqlite3GetTempReg(pParse);
 99434         -    if( isRowid ) sqlite3VdbeAddOp4(v, OP_Blob, 0, r2, 0, "", P4_STATIC);
 99435  99688       for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
 99436  99689         Expr *pE2 = pItem->pExpr;
 99437         -      int iValToIns;
 99438  99690   
 99439  99691         /* If the expression is not constant then we will need to
 99440  99692         ** disable the test that was generated above that makes sure
 99441  99693         ** this code only executes once.  Because for a non-constant
 99442  99694         ** expression we need to rerun this code each time.
 99443  99695         */
 99444  99696         if( addrOnce && !sqlite3ExprIsConstant(pE2) ){
 99445  99697           sqlite3VdbeChangeToNoop(v, addrOnce);
 99446  99698           addrOnce = 0;
 99447  99699         }
 99448  99700   
 99449  99701         /* Evaluate the expression and insert it into the temp table */
 99450         -      if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
 99451         -        sqlite3VdbeAddOp3(v, OP_InsertInt, iTab, r2, iValToIns);
 99452         -      }else{
 99453         -        r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
 99454         -        if( isRowid ){
 99455         -          sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
 99456         -                            sqlite3VdbeCurrentAddr(v)+2);
 99457         -          VdbeCoverage(v);
 99458         -          sqlite3VdbeAddOp3(v, OP_Insert, iTab, r2, r3);
 99459         -        }else{
 99460         -          sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
 99461         -          sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r3, 1);
 99462         -        }
 99463         -      }
        99702  +      r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
        99703  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
        99704  +      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r3, 1);
 99464  99705       }
 99465  99706       sqlite3ReleaseTempReg(pParse, r1);
 99466  99707       sqlite3ReleaseTempReg(pParse, r2);
 99467  99708     }
 99468  99709     if( pKeyInfo ){
 99469  99710       sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
 99470  99711     }
................................................................................
101672 101913   ** an ordinary JOIN.  The p argument is the WHERE clause.  If the WHERE
101673 101914   ** clause requires that some column of the right table of the LEFT JOIN
101674 101915   ** be non-NULL, then the LEFT JOIN can be safely converted into an
101675 101916   ** ordinary join.
101676 101917   */
101677 101918   SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
101678 101919     Walker w;
       101920  +  p = sqlite3ExprSkipCollate(p);
       101921  +  while( p ){
       101922  +    if( p->op==TK_NOTNULL ){
       101923  +      p = p->pLeft;
       101924  +    }else if( p->op==TK_AND ){
       101925  +      if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
       101926  +      p = p->pRight;
       101927  +    }else{
       101928  +      break;
       101929  +    }
       101930  +  }
101679 101931     w.xExprCallback = impliesNotNullRow;
101680 101932     w.xSelectCallback = 0;
101681 101933     w.xSelectCallback2 = 0;
101682 101934     w.eCode = 0;
101683 101935     w.u.iCur = iTab;
101684 101936     sqlite3WalkExpr(&w, p);
101685 101937     return w.eCode;
................................................................................
102253 102505       pVTab = sqlite3GetVTable(db, pTab);
102254 102506       if( pVTab->pVtab->pModule->xRename==0 ){
102255 102507         pVTab = 0;
102256 102508       }
102257 102509     }
102258 102510   #endif
102259 102511   
102260         -  /* Begin a transaction for database iDb. 
102261         -  ** Then modify the schema cookie (since the ALTER TABLE modifies the
102262         -  ** schema). Open a statement transaction if the table is a virtual
102263         -  ** table.
102264         -  */
       102512  +  /* Begin a transaction for database iDb. Then modify the schema cookie
       102513  +  ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
       102514  +  ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the 
       102515  +  ** nested SQL may raise an exception.  */
102265 102516     v = sqlite3GetVdbe(pParse);
102266 102517     if( v==0 ){
102267 102518       goto exit_rename_table;
102268 102519     }
       102520  +  sqlite3MayAbort(pParse);
102269 102521   
102270 102522     /* figure out how many UTF-8 characters are in zName */
102271 102523     zTabName = pTab->zName;
102272 102524     nTabName = sqlite3Utf8CharLen(zTabName, -1);
102273 102525   
102274 102526     /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
102275 102527     ** the schema to use the new table name.  */
................................................................................
102330 102582     ** SQLite tables) that are identified by the name of the virtual table.
102331 102583     */
102332 102584   #ifndef SQLITE_OMIT_VIRTUALTABLE
102333 102585     if( pVTab ){
102334 102586       int i = ++pParse->nMem;
102335 102587       sqlite3VdbeLoadString(v, i, zName);
102336 102588       sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
102337         -    sqlite3MayAbort(pParse);
102338 102589     }
102339 102590   #endif
102340 102591   
102341 102592     renameReloadSchema(pParse, iDb);
102342 102593     renameTestSchema(pParse, zDb, iDb==1);
102343 102594   
102344 102595   exit_rename_table:
................................................................................
102651 102902       goto exit_rename_column;
102652 102903     }
102653 102904   
102654 102905     /* Do the rename operation using a recursive UPDATE statement that
102655 102906     ** uses the sqlite_rename_column() SQL function to compute the new
102656 102907     ** CREATE statement text for the sqlite_master table.
102657 102908     */
       102909  +  sqlite3MayAbort(pParse);
102658 102910     zNew = sqlite3NameFromToken(db, pNew);
102659 102911     if( !zNew ) goto exit_rename_column;
102660 102912     assert( pNew->n>0 );
102661 102913     bQuote = sqlite3Isquote(pNew->z[0]);
102662 102914     sqlite3NestedParse(pParse, 
102663 102915         "UPDATE \"%w\".%s SET "
102664 102916         "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
................................................................................
105905 106157     ** remove the entry from the db->aDb[] array. i.e. put everything back the
105906 106158     ** way we found it.
105907 106159     */
105908 106160     if( rc==SQLITE_OK ){
105909 106161       sqlite3BtreeEnterAll(db);
105910 106162       db->init.iDb = 0;
105911 106163       db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
105912         -    rc = sqlite3Init(db, &zErrDyn);
       106164  +    if( !REOPEN_AS_MEMDB(db) ){
       106165  +      rc = sqlite3Init(db, &zErrDyn);
       106166  +    }
105913 106167       sqlite3BtreeLeaveAll(db);
105914 106168       assert( zErrDyn==0 || rc!=SQLITE_OK );
105915 106169     }
105916 106170   #ifdef SQLITE_USER_AUTHENTICATION
105917 106171     if( rc==SQLITE_OK ){
105918 106172       u8 newAuth = 0;
105919 106173       rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
................................................................................
106839 107093   
106840 107094     if( pParse->nErr ) return;
106841 107095     assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
106842 107096     va_start(ap, zFormat);
106843 107097     zSql = sqlite3VMPrintf(db, zFormat, ap);
106844 107098     va_end(ap);
106845 107099     if( zSql==0 ){
106846         -    return;   /* A malloc must have failed */
       107100  +    /* This can result either from an OOM or because the formatted string
       107101  +    ** exceeds SQLITE_LIMIT_LENGTH.  In the latter case, we need to set
       107102  +    ** an error */
       107103  +    if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
       107104  +    pParse->nErr++;
       107105  +    return;
106847 107106     }
106848 107107     pParse->nested++;
106849 107108     memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
106850 107109     memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
106851 107110     sqlite3RunParser(pParse, zSql, &zErrMsg);
106852 107111     sqlite3DbFree(db, zErrMsg);
106853 107112     sqlite3DbFree(db, zSql);
................................................................................
107979 108238     }
107980 108239     if( nTerm==1
107981 108240      && pCol
107982 108241      && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
107983 108242      && sortOrder!=SQLITE_SO_DESC
107984 108243     ){
107985 108244       if( IN_RENAME_OBJECT && pList ){
107986         -      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr);
       108245  +      Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
       108246  +      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
107987 108247       }
107988 108248       pTab->iPKey = iCol;
107989 108249       pTab->keyConf = (u8)onError;
107990 108250       assert( autoInc==0 || autoInc==1 );
107991 108251       pTab->tabFlags |= autoInc*TF_Autoincrement;
107992 108252       if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
107993 108253     }else if( autoInc ){
................................................................................
108400 108660       sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
108401 108661                          SQLITE_IDXTYPE_PRIMARYKEY);
108402 108662       if( db->mallocFailed || pParse->nErr ) return;
108403 108663       pPk = sqlite3PrimaryKeyIndex(pTab);
108404 108664       pTab->iPKey = -1;
108405 108665     }else{
108406 108666       pPk = sqlite3PrimaryKeyIndex(pTab);
       108667  +    assert( pPk!=0 );
108407 108668   
108408 108669       /*
108409 108670       ** Remove all redundant columns from the PRIMARY KEY.  For example, change
108410 108671       ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
108411 108672       ** code assumes the PRIMARY KEY contains no repeated columns.
108412 108673       */
108413 108674       for(i=j=1; i<pPk->nKeyCol; i++){
................................................................................
108569 108830         sqlite3ErrorMsg(pParse, "");
108570 108831         return;
108571 108832       }
108572 108833       p->tnum = db->init.newTnum;
108573 108834       if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
108574 108835     }
108575 108836   
       108837  +  assert( (p->tabFlags & TF_HasPrimaryKey)==0
       108838  +       || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
       108839  +  assert( (p->tabFlags & TF_HasPrimaryKey)!=0
       108840  +       || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
       108841  +
108576 108842     /* Special processing for WITHOUT ROWID Tables */
108577 108843     if( tabOpts & TF_WithoutRowid ){
108578 108844       if( (p->tabFlags & TF_Autoincrement) ){
108579 108845         sqlite3ErrorMsg(pParse,
108580 108846             "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
108581 108847         return;
108582 108848       }
................................................................................
109722 109988     }
109723 109989     pDb = &db->aDb[iDb];
109724 109990   
109725 109991     assert( pTab!=0 );
109726 109992     assert( pParse->nErr==0 );
109727 109993     if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
109728 109994          && db->init.busy==0
       109995  +       && pTblName!=0
109729 109996   #if SQLITE_USER_AUTHENTICATION
109730 109997          && sqlite3UserAuthTable(pTab->zName)==0
109731 109998   #endif
109732 109999   #ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
109733 110000          && sqlite3StrICmp(&pTab->zName[7],"master")!=0
109734 110001   #endif
109735         -       && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
109736 110002    ){
109737 110003       sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
109738 110004       goto exit_create_index;
109739 110005     }
109740 110006   #ifndef SQLITE_OMIT_VIEW
109741 110007     if( pTab->pSelect ){
109742 110008       sqlite3ErrorMsg(pParse, "views may not be indexed");
................................................................................
109832 110098       pList = sqlite3ExprListAppend(pParse, 0,
109833 110099                 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
109834 110100       if( pList==0 ) goto exit_create_index;
109835 110101       assert( pList->nExpr==1 );
109836 110102       sqlite3ExprListSetSortOrder(pList, sortOrder);
109837 110103     }else{
109838 110104       sqlite3ExprListCheckLength(pParse, pList, "index");
       110105  +    if( pParse->nErr ) goto exit_create_index;
109839 110106     }
109840 110107   
109841 110108     /* Figure out how many bytes of space are required to store explicitly
109842 110109     ** specified collation sequence names.
109843 110110     */
109844 110111     for(i=0; i<pList->nExpr; i++){
109845 110112       Expr *pExpr = pList->a[i].pExpr;
................................................................................
109850 110117     }
109851 110118   
109852 110119     /* 
109853 110120     ** Allocate the index structure. 
109854 110121     */
109855 110122     nName = sqlite3Strlen30(zName);
109856 110123     nExtraCol = pPk ? pPk->nKeyCol : 1;
       110124  +  assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
109857 110125     pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
109858 110126                                         nName + nExtra + 1, &zExtra);
109859 110127     if( db->mallocFailed ){
109860 110128       goto exit_create_index;
109861 110129     }
109862 110130     assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
109863 110131     assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
................................................................................
110333 110601     sqlite3 *db,      /* Connection to notify of malloc failures */
110334 110602     void *pArray,     /* Array of objects.  Might be reallocated */
110335 110603     int szEntry,      /* Size of each object in the array */
110336 110604     int *pnEntry,     /* Number of objects currently in use */
110337 110605     int *pIdx         /* Write the index of a new slot here */
110338 110606   ){
110339 110607     char *z;
110340         -  int n = *pnEntry;
       110608  +  sqlite3_int64 n = *pIdx = *pnEntry;
110341 110609     if( (n & (n-1))==0 ){
110342         -    int sz = (n==0) ? 1 : 2*n;
       110610  +    sqlite3_int64 sz = (n==0) ? 1 : 2*n;
110343 110611       void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
110344 110612       if( pNew==0 ){
110345 110613         *pIdx = -1;
110346 110614         return pArray;
110347 110615       }
110348 110616       pArray = pNew;
110349 110617     }
110350 110618     z = (char*)pArray;
110351 110619     memset(&z[n * szEntry], 0, szEntry);
110352         -  *pIdx = n;
110353 110620     ++*pnEntry;
110354 110621     return pArray;
110355 110622   }
110356 110623   
110357 110624   /*
110358 110625   ** Append a new element to the given IdList.  Create a new IdList if
110359 110626   ** need be.
................................................................................
110456 110723     assert( nExtra>=1 );
110457 110724     assert( pSrc!=0 );
110458 110725     assert( iStart<=pSrc->nSrc );
110459 110726   
110460 110727     /* Allocate additional space if needed */
110461 110728     if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
110462 110729       SrcList *pNew;
110463         -    int nAlloc = pSrc->nSrc*2+nExtra;
       110730  +    sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
110464 110731       sqlite3 *db = pParse->db;
110465 110732   
110466 110733       if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
110467 110734         sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
110468 110735                         SQLITE_MAX_SRCLIST);
110469 110736         return 0;
110470 110737       }
................................................................................
110963 111230     Index *pIdx       /* The index that triggers the constraint */
110964 111231   ){
110965 111232     char *zErr;
110966 111233     int j;
110967 111234     StrAccum errMsg;
110968 111235     Table *pTab = pIdx->pTable;
110969 111236   
110970         -  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
       111237  +  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 
       111238  +                      pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
110971 111239     if( pIdx->aColExpr ){
110972 111240       sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
110973 111241     }else{
110974 111242       for(j=0; j<pIdx->nKeyCol; j++){
110975 111243         char *zCol;
110976 111244         assert( pIdx->aiColumn[j]>=0 );
110977 111245         zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
................................................................................
111212 111480         if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
111213 111481           sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
111214 111482         }
111215 111483       }
111216 111484     }
111217 111485   
111218 111486     if( pWith ){
111219         -    int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
       111487  +    sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
111220 111488       pNew = sqlite3DbRealloc(db, pWith, nByte);
111221 111489     }else{
111222 111490       pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
111223 111491     }
111224 111492     assert( (pNew!=0 && zName!=0) || db->mallocFailed );
111225 111493   
111226 111494     if( db->mallocFailed ){
................................................................................
114519 114787   ** Set the LIKEOPT flag on the 2-argument function with the given name.
114520 114788   */
114521 114789   static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
114522 114790     FuncDef *pDef;
114523 114791     pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0);
114524 114792     if( ALWAYS(pDef) ){
114525 114793       pDef->funcFlags |= flagVal;
       114794  +  }
       114795  +  pDef = sqlite3FindFunction(db, zName, 3, SQLITE_UTF8, 0);
       114796  +  if( pDef ){
       114797  +    pDef->funcFlags |= flagVal;
114526 114798     }
114527 114799   }
114528 114800   
114529 114801   /*
114530 114802   ** Register the built-in LIKE and GLOB functions.  The caseSensitive
114531 114803   ** parameter determines whether or not the LIKE operator is case
114532 114804   ** sensitive.  GLOB is always case sensitive.
................................................................................
117842 118114           sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
117843 118115           VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
117844 118116         }
117845 118117       }
117846 118118       sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
117847 118119       VdbeComment((v, "for %s", pIdx->zName));
117848 118120   #ifdef SQLITE_ENABLE_NULL_TRIM
117849         -    if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
       118121  +    if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
       118122  +      sqlite3SetMakeRecordP5(v, pIdx->pTable);
       118123  +    }
117850 118124   #endif
117851 118125   
117852 118126       /* In an UPDATE operation, if this index is the PRIMARY KEY index 
117853 118127       ** of a WITHOUT ROWID table and there has been no change the
117854 118128       ** primary key, then no collision is possible.  The collision detection
117855 118129       ** logic below can all be skipped. */
117856 118130       if( isUpdate && pPk==pIdx && pkChng==0 ){
................................................................................
118092 118366       pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
118093 118367       if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
118094 118368         assert( pParse->nested==0 );
118095 118369         pik_flags |= OPFLAG_NCHANGE;
118096 118370         pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
118097 118371   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
118098 118372         if( update_flags==0 ){
118099         -        sqlite3VdbeAddOp4(v, OP_InsertInt, 
118100         -            iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
       118373  +        int r = sqlite3GetTempReg(pParse);
       118374  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, r);
       118375  +        sqlite3VdbeAddOp4(v, OP_Insert, 
       118376  +            iIdxCur+i, aRegIdx[i], r, (char*)pTab, P4_TABLE
118101 118377           );
118102 118378           sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
       118379  +        sqlite3ReleaseTempReg(pParse, r);
118103 118380         }
118104 118381   #endif
118105 118382       }
118106 118383       sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
118107 118384                            aRegIdx[i]+1,
118108 118385                            pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
118109 118386       sqlite3VdbeChangeP5(v, pik_flags);
................................................................................
118443 118720       }
118444 118721       for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
118445 118722         if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
118446 118723       }
118447 118724       if( pSrcIdx==0 ){
118448 118725         return 0;    /* pDestIdx has no corresponding index in pSrc */
118449 118726       }
       118727  +    if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
       118728  +         && sqlite3FaultSim(411)==SQLITE_OK ){
       118729  +      /* The sqlite3FaultSim() call allows this corruption test to be
       118730  +      ** bypassed during testing, in order to exercise other corruption tests
       118731  +      ** further downstream. */
       118732  +      return 0;   /* Corrupt schema - two indexes on the same btree */
       118733  +    }
118450 118734     }
118451 118735   #ifndef SQLITE_OMIT_CHECK
118452 118736     if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
118453 118737       return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
118454 118738     }
118455 118739   #endif
118456 118740   #ifndef SQLITE_OMIT_FOREIGN_KEY
................................................................................
118520 118804         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
118521 118805         sqlite3VdbeVerifyAbortable(v, onError);
118522 118806         addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
118523 118807         VdbeCoverage(v);
118524 118808         sqlite3RowidConstraint(pParse, onError, pDest);
118525 118809         sqlite3VdbeJumpHere(v, addr2);
118526 118810         autoIncStep(pParse, regAutoinc, regRowid);
118527         -    }else if( pDest->pIndex==0 ){
       118811  +    }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
118528 118812         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
118529 118813       }else{
118530 118814         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
118531 118815         assert( (pDest->tabFlags & TF_Autoincrement)==0 );
118532 118816       }
118533 118817       sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
118534 118818       if( db->mDbFlags & DBFLAG_Vacuum ){
................................................................................
118583 118867           if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
118584 118868         }
118585 118869         if( i==pSrcIdx->nColumn ){
118586 118870           idxInsFlags = OPFLAG_USESEEKRESULT;
118587 118871           sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
118588 118872         }
118589 118873       }
118590         -    if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
       118874  +    if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
118591 118875         idxInsFlags |= OPFLAG_NCHANGE;
118592 118876       }
118593 118877       sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
118594 118878       sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
118595 118879       sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
118596 118880       sqlite3VdbeJumpHere(v, addr1);
118597 118881       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
................................................................................
119095 119379                               void (*xStep)(sqlite3_context*,int,sqlite3_value**),
119096 119380                               void (*xFinal)(sqlite3_context*),
119097 119381                               void (*xValue)(sqlite3_context*),
119098 119382                               void (*xInv)(sqlite3_context*,int,sqlite3_value**),
119099 119383                               void(*xDestroy)(void*));
119100 119384     /* Version 3.26.0 and later */
119101 119385     const char *(*normalized_sql)(sqlite3_stmt*);
       119386  +  /* Version 3.28.0 and later */
       119387  +  int (*stmt_isexplain)(sqlite3_stmt*);
       119388  +  int (*value_frombind)(sqlite3_value*);
119102 119389   };
119103 119390   
119104 119391   /*
119105 119392   ** This is the function signature used for all extension entry points.  It
119106 119393   ** is also defined in the file "loadext.c".
119107 119394   */
119108 119395   typedef int (*sqlite3_loadext_entry)(
................................................................................
119384 119671   #define sqlite3_str_errcode            sqlite3_api->str_errcode
119385 119672   #define sqlite3_str_length             sqlite3_api->str_length
119386 119673   #define sqlite3_str_value              sqlite3_api->str_value
119387 119674   /* Version 3.25.0 and later */
119388 119675   #define sqlite3_create_window_function sqlite3_api->create_window_function
119389 119676   /* Version 3.26.0 and later */
119390 119677   #define sqlite3_normalized_sql         sqlite3_api->normalized_sql
       119678  +/* Version 3.28.0 and later */
       119679  +#define sqlite3_stmt_isexplain         sqlite3_api->isexplain
       119680  +#define sqlite3_value_frombind         sqlite3_api->frombind
119391 119681   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
119392 119682   
119393 119683   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
119394 119684     /* This case when the file really is being compiled as a loadable 
119395 119685     ** extension */
119396 119686   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
119397 119687   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
119843 120133     sqlite3_str_errcode,
119844 120134     sqlite3_str_length,
119845 120135     sqlite3_str_value,
119846 120136     /* Version 3.25.0 and later */
119847 120137     sqlite3_create_window_function,
119848 120138     /* Version 3.26.0 and later */
119849 120139   #ifdef SQLITE_ENABLE_NORMALIZE
119850         -  sqlite3_normalized_sql
       120140  +  sqlite3_normalized_sql,
119851 120141   #else
119852         -  0
       120142  +  0,
119853 120143   #endif
       120144  +  /* Version 3.28.0 and later */
       120145  +  sqlite3_stmt_isexplain,
       120146  +  sqlite3_value_frombind
119854 120147   };
119855 120148   
119856 120149   /*
119857 120150   ** Attempt to load an SQLite extension library contained in the file
119858 120151   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
119859 120152   ** default entry point name (sqlite3_extension_init) is used.  Use
119860 120153   ** of the default name is recommended.
................................................................................
129626 129919   
129627 129920   /*
129628 129921   ** Update the accumulator memory cells for an aggregate based on
129629 129922   ** the current cursor position.
129630 129923   **
129631 129924   ** If regAcc is non-zero and there are no min() or max() aggregates
129632 129925   ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
129633         -** registers i register regAcc contains 0. The caller will take care
       129926  +** registers if register regAcc contains 0. The caller will take care
129634 129927   ** of setting and clearing regAcc.
129635 129928   */
129636 129929   static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
129637 129930     Vdbe *v = pParse->pVdbe;
129638 129931     int i;
129639 129932     int regHit = 0;
129640 129933     int addrHitTest = 0;
................................................................................
133718 134011     sqlite3ExprDelete(pParse->db, pInto);
133719 134012     return;
133720 134013   }
133721 134014   
133722 134015   /*
133723 134016   ** This routine implements the OP_Vacuum opcode of the VDBE.
133724 134017   */
133725         -SQLITE_PRIVATE int sqlite3RunVacuum(
       134018  +SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3RunVacuum(
133726 134019     char **pzErrMsg,        /* Write error message here */
133727 134020     sqlite3 *db,            /* Database connection */
133728 134021     int iDb,                /* Which attached DB to vacuum */
133729         -  sqlite3_value *pOut     /* Write results here, if not NULL */
       134022  +  sqlite3_value *pOut     /* Write results here, if not NULL. VACUUM INTO */
133730 134023   ){
133731 134024     int rc = SQLITE_OK;     /* Return code from service routines */
133732 134025     Btree *pMain;           /* The database being vacuumed */
133733 134026     Btree *pTemp;           /* The temporary database we vacuum into */
133734 134027     u32 saved_mDbFlags;     /* Saved value of db->mDbFlags */
133735 134028     u64 saved_flags;        /* Saved value of db->flags */
133736 134029     int saved_nChange;      /* Saved value of db->nChange */
133737 134030     int saved_nTotalChange; /* Saved value of db->nTotalChange */
       134031  +  u32 saved_openFlags;    /* Saved value of db->openFlags */
133738 134032     u8 saved_mTrace;        /* Saved trace settings */
133739 134033     Db *pDb = 0;            /* Database to detach at end of vacuum */
133740 134034     int isMemDb;            /* True if vacuuming a :memory: database */
133741 134035     int nRes;               /* Bytes of reserved space at the end of each page */
133742 134036     int nDb;                /* Number of attached databases */
133743 134037     const char *zDbMain;    /* Schema name of database to vacuum */
133744 134038     const char *zOut;       /* Name of output file */
133745 134039   
133746 134040     if( !db->autoCommit ){
133747 134041       sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
133748         -    return SQLITE_ERROR;
       134042  +    return SQLITE_ERROR; /* IMP: R-12218-18073 */
133749 134043     }
133750 134044     if( db->nVdbeActive>1 ){
133751 134045       sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
133752         -    return SQLITE_ERROR;
       134046  +    return SQLITE_ERROR; /* IMP: R-15610-35227 */
133753 134047     }
       134048  +  saved_openFlags = db->openFlags;
133754 134049     if( pOut ){
133755 134050       if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){
133756 134051         sqlite3SetString(pzErrMsg, db, "non-text filename");
133757 134052         return SQLITE_ERROR;
133758 134053       }
133759 134054       zOut = (const char*)sqlite3_value_text(pOut);
       134055  +    db->openFlags &= ~SQLITE_OPEN_READONLY;
       134056  +    db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
133760 134057     }else{
133761 134058       zOut = "";
133762 134059     }
133763 134060   
133764 134061     /* Save the current value of the database flags so that it can be 
133765 134062     ** restored before returning. Then set the writable-schema flag, and
133766 134063     ** disable CHECK and foreign key constraints.  */
................................................................................
133791 134088     ** actually occurs when doing a vacuum since the vacuum_db is initially
133792 134089     ** empty.  Only the journal header is written.  Apparently it takes more
133793 134090     ** time to parse and run the PRAGMA to turn journalling off than it does
133794 134091     ** to write the journal header file.
133795 134092     */
133796 134093     nDb = db->nDb;
133797 134094     rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
       134095  +  db->openFlags = saved_openFlags;
133798 134096     if( rc!=SQLITE_OK ) goto end_of_vacuum;
133799 134097     assert( (db->nDb-1)==nDb );
133800 134098     pDb = &db->aDb[nDb];
133801 134099     assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
133802 134100     pTemp = pDb->pBt;
133803 134101     if( pOut ){
133804 134102       sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
133805 134103       i64 sz = 0;
133806 134104       if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
133807 134105         rc = SQLITE_ERROR;
133808 134106         sqlite3SetString(pzErrMsg, db, "output file already exists");
133809 134107         goto end_of_vacuum;
133810 134108       }
       134109  +    db->mDbFlags |= DBFLAG_VacuumInto;
133811 134110     }
133812 134111     nRes = sqlite3BtreeGetOptimalReserve(pMain);
133813 134112   
133814 134113     /* A VACUUM cannot change the pagesize of an encrypted database. */
133815 134114   #ifdef SQLITE_HAS_CODEC
133816 134115     if( db->nextPagesize ){
133817 134116       extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
................................................................................
134292 134591   
134293 134592   /*
134294 134593   ** Add a new module argument to pTable->azModuleArg[].
134295 134594   ** The string is not copied - the pointer is stored.  The
134296 134595   ** string will be freed automatically when the table is
134297 134596   ** deleted.
134298 134597   */
134299         -static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
134300         -  int nBytes = sizeof(char *)*(2+pTable->nModuleArg);
       134598  +static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
       134599  +  sqlite3_int64 nBytes = sizeof(char *)*(2+pTable->nModuleArg);
134301 134600     char **azModuleArg;
       134601  +  sqlite3 *db = pParse->db;
       134602  +  if( pTable->nModuleArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
       134603  +    sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
       134604  +  }
134302 134605     azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
134303 134606     if( azModuleArg==0 ){
134304 134607       sqlite3DbFree(db, zArg);
134305 134608     }else{
134306 134609       int i = pTable->nModuleArg++;
134307 134610       azModuleArg[i] = zArg;
134308 134611       azModuleArg[i+1] = 0;
................................................................................
134329 134632     pTable = pParse->pNewTable;
134330 134633     if( pTable==0 ) return;
134331 134634     assert( 0==pTable->pIndex );
134332 134635   
134333 134636     db = pParse->db;
134334 134637   
134335 134638     assert( pTable->nModuleArg==0 );
134336         -  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
134337         -  addModuleArgument(db, pTable, 0);
134338         -  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
       134639  +  addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName));
       134640  +  addModuleArgument(pParse, pTable, 0);
       134641  +  addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
134339 134642     assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
134340 134643          || (pParse->sNameToken.z==pName1->z && pName2->z==0)
134341 134644     );
134342 134645     pParse->sNameToken.n = (int)(
134343 134646         &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
134344 134647     );
134345 134648   
................................................................................
134364 134667   ** virtual table currently under construction in pParse->pTable.
134365 134668   */
134366 134669   static void addArgumentToVtab(Parse *pParse){
134367 134670     if( pParse->sArg.z && pParse->pNewTable ){
134368 134671       const char *z = (const char*)pParse->sArg.z;
134369 134672       int n = pParse->sArg.n;
134370 134673       sqlite3 *db = pParse->db;
134371         -    addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
       134674  +    addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
134372 134675     }
134373 134676   }
134374 134677   
134375 134678   /*
134376 134679   ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
134377 134680   ** has been completely parsed.
134378 134681   */
................................................................................
134653 134956   */
134654 134957   static int growVTrans(sqlite3 *db){
134655 134958     const int ARRAY_INCR = 5;
134656 134959   
134657 134960     /* Grow the sqlite3.aVTrans array if required */
134658 134961     if( (db->nVTrans%ARRAY_INCR)==0 ){
134659 134962       VTable **aVTrans;
134660         -    int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
       134963  +    sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
       134964  +                                 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
134661 134965       aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
134662 134966       if( !aVTrans ){
134663 134967         return SQLITE_NOMEM_BKPT;
134664 134968       }
134665 134969       memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
134666 134970       db->aVTrans = aVTrans;
134667 134971     }
................................................................................
135149 135453       return 0;
135150 135454     }
135151 135455     pMod->pEpoTab = pTab;
135152 135456     pTab->nTabRef = 1;
135153 135457     pTab->pSchema = db->aDb[0].pSchema;
135154 135458     assert( pTab->nModuleArg==0 );
135155 135459     pTab->iPKey = -1;
135156         -  addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
135157         -  addModuleArgument(db, pTab, 0);
135158         -  addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
       135460  +  addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
       135461  +  addModuleArgument(pParse, pTab, 0);
       135462  +  addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
135159 135463     rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
135160 135464     if( rc ){
135161 135465       sqlite3ErrorMsg(pParse, "%s", zErr);
135162 135466       sqlite3DbFree(db, zErr);
135163 135467       sqlite3VtabEponymousTableClear(db, pMod);
135164 135468       return 0;
135165 135469     }
................................................................................
136412 136716       if( pIn ){
136413 136717         int iMap = 0;               /* Index in aiMap[] */
136414 136718         pIn += i;
136415 136719         for(i=iEq;i<pLoop->nLTerm; i++){
136416 136720           if( pLoop->aLTerm[i]->pExpr==pX ){
136417 136721             int iOut = iReg + i - iEq;
136418 136722             if( eType==IN_INDEX_ROWID ){
136419         -            testcase( nEq>1 );  /* Happens with a UNIQUE index on ROWID */
136420 136723               pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
136421 136724             }else{
136422 136725               int iCol = aiMap ? aiMap[iMap++] : 0;
136423 136726               pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
136424 136727             }
136425 136728             sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v);
136426 136729             if( i==iEq ){
................................................................................
136989 137292       x.iIdxCol = iIdxCol;
136990 137293       x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
136991 137294       sqlite3WalkExpr(&w, pWInfo->pWhere);
136992 137295       sqlite3WalkExprList(&w, pWInfo->pOrderBy);
136993 137296       sqlite3WalkExprList(&w, pWInfo->pResultSet);
136994 137297     }
136995 137298   }
       137299  +
       137300  +/*
       137301  +** The pTruth expression is always true because it is the WHERE clause
       137302  +** a partial index that is driving a query loop.  Look through all of the
       137303  +** WHERE clause terms on the query, and if any of those terms must be
       137304  +** true because pTruth is true, then mark those WHERE clause terms as
       137305  +** coded.
       137306  +*/
       137307  +static void whereApplyPartialIndexConstraints(
       137308  +  Expr *pTruth,
       137309  +  int iTabCur,
       137310  +  WhereClause *pWC
       137311  +){
       137312  +  int i;
       137313  +  WhereTerm *pTerm;
       137314  +  while( pTruth->op==TK_AND ){
       137315  +    whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
       137316  +    pTruth = pTruth->pRight;
       137317  +  }
       137318  +  for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
       137319  +    Expr *pExpr;
       137320  +    if( pTerm->wtFlags & TERM_CODED ) continue;
       137321  +    pExpr = pTerm->pExpr;
       137322  +    if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
       137323  +      pTerm->wtFlags |= TERM_CODED;
       137324  +    }
       137325  +  }
       137326  +}
136996 137327   
136997 137328   /*
136998 137329   ** Generate code for the start of the iLevel-th loop in the WHERE clause
136999 137330   ** implementation described by pWInfo.
137000 137331   */
137001 137332   SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
137002 137333     Parse *pParse,       /* Parsing context */
................................................................................
137174 137505       iReleaseReg = ++pParse->nMem;
137175 137506       iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
137176 137507       if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
137177 137508       addrNxt = pLevel->addrNxt;
137178 137509       sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg);
137179 137510       VdbeCoverage(v);
137180 137511       pLevel->op = OP_Noop;
       137512  +    if( (pTerm->prereqAll & pLevel->notReady)==0 ){
       137513  +      pTerm->wtFlags |= TERM_CODED;
       137514  +    }
137181 137515     }else if( (pLoop->wsFlags & WHERE_IPK)!=0
137182 137516            && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
137183 137517     ){
137184 137518       /* Case 3:  We have an inequality comparison against the ROWID field.
137185 137519       */
137186 137520       int testOp = OP_Noop;
137187 137521       int start;
................................................................................
137586 137920       ** into reference to index columns.
137587 137921       **
137588 137922       ** Do not do this for the RHS of a LEFT JOIN. This is because the 
137589 137923       ** expression may be evaluated after OP_NullRow has been executed on
137590 137924       ** the cursor. In this case it is important to do the full evaluation,
137591 137925       ** as the result of the expression may not be NULL, even if all table
137592 137926       ** column values are.  https://www.sqlite.org/src/info/7fa8049685b50b5a
       137927  +    **
       137928  +    ** Also, do not do this when processing one index an a multi-index
       137929  +    ** OR clause, since the transformation will become invalid once we
       137930  +    ** move forward to the next index.
       137931  +    ** https://sqlite.org/src/info/4e8e4857d32d401f
137593 137932       */
137594         -    if( pLevel->iLeftJoin==0 ){
       137933  +    if( pLevel->iLeftJoin==0 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
137595 137934         whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
137596 137935       }
       137936  +
       137937  +    /* If a partial index is driving the loop, try to eliminate WHERE clause
       137938  +    ** terms from the query that must be true due to the WHERE clause of
       137939  +    ** the partial index
       137940  +    */
       137941  +    if( pIdx->pPartIdxWhere ){
       137942  +      whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
       137943  +    }
137597 137944   
137598 137945       /* Record the instruction used to terminate the loop. */
137599 137946       if( pLoop->wsFlags & WHERE_ONEROW ){
137600 137947         pLevel->op = OP_Noop;
137601 137948       }else if( bRev ){
137602 137949         pLevel->op = OP_Prev;
137603 137950       }else{
................................................................................
137754 138101           if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
137755 138102           if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
137756 138103           testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
137757 138104           pExpr = sqlite3ExprDup(db, pExpr, 0);
137758 138105           pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
137759 138106         }
137760 138107         if( pAndExpr ){
137761         -        pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr);
       138108  +        /* The extra 0x10000 bit on the opcode is masked off and does not
       138109  +        ** become part of the new Expr.op.  However, it does make the
       138110  +        ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
       138111  +        ** prevents sqlite3PExpr() from implementing AND short-circuit 
       138112  +        ** optimization, which we do not want here. */
       138113  +        pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, 0, pAndExpr);
137762 138114         }
137763 138115       }
137764 138116   
137765 138117       /* Run a separate WHERE clause for each term of the OR clause.  After
137766 138118       ** eliminating duplicates from other WHERE clauses, the action for each
137767 138119       ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
137768 138120       */
................................................................................
137984 138336           ** that compares BLOBs. */
137985 138337   #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
137986 138338           continue;
137987 138339   #else
137988 138340           u32 x = pLevel->iLikeRepCntr;
137989 138341           if( x>0 ){
137990 138342             skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
       138343  +          VdbeCoverageIf(v, (x&1)==1);
       138344  +          VdbeCoverageIf(v, (x&1)==0);
137991 138345           }
137992         -        VdbeCoverage(v);
137993 138346   #endif
137994 138347         }
137995 138348   #ifdef WHERETRACE_ENABLED /* 0xffff */
137996 138349         if( sqlite3WhereTrace ){
137997 138350           VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
137998 138351                            pWC->nTerm-j, pTerm, iLoop));
137999 138352         }
................................................................................
139587 139940       assert( p->x.pList==0 );
139588 139941     }else if( ExprHasProperty(p, EP_xIsSelect) ){
139589 139942       if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
139590 139943       mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
139591 139944     }else if( p->x.pList ){
139592 139945       mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
139593 139946     }
       139947  +#ifndef SQLITE_OMIT_WINDOWFUNC
       139948  +  if( p->op==TK_FUNCTION && p->y.pWin ){
       139949  +    mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
       139950  +    mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
       139951  +  }
       139952  +#endif
139594 139953     return mask;
139595 139954   }
139596 139955   SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
139597 139956     return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
139598 139957   }
139599 139958   SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
139600 139959     int i;
................................................................................
143003 143362   
143004 143363     /* First call xBestIndex() with all constraints usable. */
143005 143364     WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
143006 143365     WHERETRACE(0x40, ("  VirtualOne: all usable\n"));
143007 143366     rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
143008 143367   
143009 143368     /* If the call to xBestIndex() with all terms enabled produced a plan
143010         -  ** that does not require any source tables (IOW: a plan with mBest==0),
143011         -  ** then there is no point in making any further calls to xBestIndex() 
143012         -  ** since they will all return the same result (if the xBestIndex()
143013         -  ** implementation is sane). */
143014         -  if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){
       143369  +  ** that does not require any source tables (IOW: a plan with mBest==0)
       143370  +  ** and does not use an IN(...) operator, then there is no point in making 
       143371  +  ** any further calls to xBestIndex() since they will all return the same
       143372  +  ** result (if the xBestIndex() implementation is sane). */
       143373  +  if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
143015 143374       int seenZero = 0;             /* True if a plan with no prereqs seen */
143016 143375       int seenZeroNoIN = 0;         /* Plan with no prereqs and no IN(...) seen */
143017 143376       Bitmask mPrev = 0;
143018 143377       Bitmask mBestNoIn = 0;
143019 143378   
143020 143379       /* If the plan produced by the earlier call uses an IN(...) term, call
143021 143380       ** xBestIndex again, this time with IN(...) terms disabled. */
................................................................................
145240 145599         p->nValue++;
145241 145600         p->nStep = 0;
145242 145601       }
145243 145602       sqlite3_result_int64(pCtx, p->nValue);
145244 145603     }
145245 145604   }
145246 145605   
       145606  +/*
       145607  +** Implementation of built-in window function nth_value(). This
       145608  +** implementation is used in "slow mode" only - when the EXCLUDE clause
       145609  +** is not set to the default value "NO OTHERS".
       145610  +*/
       145611  +struct NthValueCtx {
       145612  +  i64 nStep;
       145613  +  sqlite3_value *pValue;
       145614  +};
       145615  +static void nth_valueStepFunc(
       145616  +  sqlite3_context *pCtx, 
       145617  +  int nArg,
       145618  +  sqlite3_value **apArg
       145619  +){
       145620  +  struct NthValueCtx *p;
       145621  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145622  +  if( p ){
       145623  +    i64 iVal;
       145624  +    switch( sqlite3_value_numeric_type(apArg[1]) ){
       145625  +      case SQLITE_INTEGER:
       145626  +        iVal = sqlite3_value_int64(apArg[1]);
       145627  +        break;
       145628  +      case SQLITE_FLOAT: {
       145629  +        double fVal = sqlite3_value_double(apArg[1]);
       145630  +        if( ((i64)fVal)!=fVal ) goto error_out;
       145631  +        iVal = (i64)fVal;
       145632  +        break;
       145633  +      }
       145634  +      default:
       145635  +        goto error_out;
       145636  +    }
       145637  +    if( iVal<=0 ) goto error_out;
       145638  +
       145639  +    p->nStep++;
       145640  +    if( iVal==p->nStep ){
       145641  +      p->pValue = sqlite3_value_dup(apArg[0]);
       145642  +      if( !p->pValue ){
       145643  +        sqlite3_result_error_nomem(pCtx);
       145644  +      }
       145645  +    }
       145646  +  }
       145647  +  UNUSED_PARAMETER(nArg);
       145648  +  UNUSED_PARAMETER(apArg);
       145649  +  return;
       145650  +
       145651  + error_out:
       145652  +  sqlite3_result_error(
       145653  +      pCtx, "second argument to nth_value must be a positive integer", -1
       145654  +  );
       145655  +}
       145656  +static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
       145657  +  struct NthValueCtx *p;
       145658  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
       145659  +  if( p && p->pValue ){
       145660  +    sqlite3_result_value(pCtx, p->pValue);
       145661  +    sqlite3_value_free(p->pValue);
       145662  +    p->pValue = 0;
       145663  +  }
       145664  +}
       145665  +#define nth_valueInvFunc noopStepFunc
       145666  +#define nth_valueValueFunc noopValueFunc
       145667  +
       145668  +static void first_valueStepFunc(
       145669  +  sqlite3_context *pCtx, 
       145670  +  int nArg,
       145671  +  sqlite3_value **apArg
       145672  +){
       145673  +  struct NthValueCtx *p;
       145674  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145675  +  if( p && p->pValue==0 ){
       145676  +    p->pValue = sqlite3_value_dup(apArg[0]);
       145677  +    if( !p->pValue ){
       145678  +      sqlite3_result_error_nomem(pCtx);
       145679  +    }
       145680  +  }
       145681  +  UNUSED_PARAMETER(nArg);
       145682  +  UNUSED_PARAMETER(apArg);
       145683  +}
       145684  +static void first_valueFinalizeFunc(sqlite3_context *pCtx){
       145685  +  struct NthValueCtx *p;
       145686  +  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145687  +  if( p && p->pValue ){
       145688  +    sqlite3_result_value(pCtx, p->pValue);
       145689  +    sqlite3_value_free(p->pValue);
       145690  +    p->pValue = 0;
       145691  +  }
       145692  +}
       145693  +#define first_valueInvFunc noopStepFunc
       145694  +#define first_valueValueFunc noopValueFunc
       145695  +
145247 145696   /*
145248 145697   ** Implementation of built-in window function rank(). Assumes that
145249 145698   ** the window frame has been set to:
145250 145699   **
145251 145700   **   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
145252 145701   */
145253 145702   static void rankStepFunc(
................................................................................
145275 145724     }
145276 145725   }
145277 145726   
145278 145727   /*
145279 145728   ** Implementation of built-in window function percent_rank(). Assumes that
145280 145729   ** the window frame has been set to:
145281 145730   **
145282         -**   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
       145731  +**   GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
145283 145732   */
145284 145733   static void percent_rankStepFunc(
145285 145734     sqlite3_context *pCtx, 
145286 145735     int nArg,
145287 145736     sqlite3_value **apArg
145288 145737   ){
145289 145738     struct CallCount *p;
145290         -  UNUSED_PARAMETER(nArg); assert( nArg==1 );
145291         -
       145739  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145740  +  UNUSED_PARAMETER(apArg);
       145741  +  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145742  +  if( p ){
       145743  +    p->nTotal++;
       145744  +  }
       145745  +}
       145746  +static void percent_rankInvFunc(
       145747  +  sqlite3_context *pCtx, 
       145748  +  int nArg,
       145749  +  sqlite3_value **apArg
       145750  +){
       145751  +  struct CallCount *p;
       145752  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145753  +  UNUSED_PARAMETER(apArg);
145292 145754     p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145293         -  if( p ){
145294         -    if( p->nTotal==0 ){
145295         -      p->nTotal = sqlite3_value_int64(apArg[0]);
145296         -    }
145297         -    p->nStep++;
145298         -    if( p->nValue==0 ){
145299         -      p->nValue = p->nStep;
145300         -    }
145301         -  }
       145755  +  p->nStep++;
145302 145756   }
145303 145757   static void percent_rankValueFunc(sqlite3_context *pCtx){
145304 145758     struct CallCount *p;
145305 145759     p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145306 145760     if( p ){
       145761  +    p->nValue = p->nStep;
145307 145762       if( p->nTotal>1 ){
145308         -      double r = (double)(p->nValue-1) / (double)(p->nTotal-1);
       145763  +      double r = (double)p->nValue / (double)(p->nTotal-1);
145309 145764         sqlite3_result_double(pCtx, r);
145310 145765       }else{
145311 145766         sqlite3_result_double(pCtx, 0.0);
145312 145767       }
145313         -    p->nValue = 0;
145314 145768     }
145315 145769   }
       145770  +#define percent_rankFinalizeFunc percent_rankValueFunc
145316 145771   
145317 145772   /*
145318 145773   ** Implementation of built-in window function cume_dist(). Assumes that
145319 145774   ** the window frame has been set to:
145320 145775   **
145321         -**   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
       145776  +**   GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
145322 145777   */
145323 145778   static void cume_distStepFunc(
145324 145779     sqlite3_context *pCtx, 
145325 145780     int nArg,
145326 145781     sqlite3_value **apArg
145327 145782   ){
145328 145783     struct CallCount *p;
145329         -  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
145330         -
       145784  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145785  +  UNUSED_PARAMETER(apArg);
       145786  +  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145787  +  if( p ){
       145788  +    p->nTotal++;
       145789  +  }
       145790  +}
       145791  +static void cume_distInvFunc(
       145792  +  sqlite3_context *pCtx, 
       145793  +  int nArg,
       145794  +  sqlite3_value **apArg
       145795  +){
       145796  +  struct CallCount *p;
       145797  +  UNUSED_PARAMETER(nArg); assert( nArg==0 );
       145798  +  UNUSED_PARAMETER(apArg);
145331 145799     p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145332         -  if( p ){
145333         -    if( p->nTotal==0 ){
145334         -      p->nTotal = sqlite3_value_int64(apArg[0]);
145335         -    }
145336         -    p->nStep++;
145337         -  }
       145800  +  p->nStep++;
145338 145801   }
145339 145802   static void cume_distValueFunc(sqlite3_context *pCtx){
145340 145803     struct CallCount *p;
145341         -  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145342         -  if( p && p->nTotal ){
       145804  +  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
       145805  +  if( p ){
145343 145806       double r = (double)(p->nStep) / (double)(p->nTotal);
145344 145807       sqlite3_result_double(pCtx, r);
145345 145808     }
145346 145809   }
       145810  +#define cume_distFinalizeFunc cume_distValueFunc
145347 145811   
145348 145812   /*
145349 145813   ** Context object for ntile() window function.
145350 145814   */
145351 145815   struct NtileCtx {
145352 145816     i64 nTotal;                     /* Total rows in partition */
145353 145817     i64 nParam;                     /* Parameter passed to ntile(N) */
................................................................................
145354 145818     i64 iRow;                       /* Current row */
145355 145819   };
145356 145820   
145357 145821   /*
145358 145822   ** Implementation of ntile(). This assumes that the window frame has
145359 145823   ** been coerced to:
145360 145824   **
145361         -**   ROWS UNBOUNDED PRECEDING AND CURRENT ROW
       145825  +**   ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
145362 145826   */
145363 145827   static void ntileStepFunc(
145364 145828     sqlite3_context *pCtx, 
145365 145829     int nArg,
145366 145830     sqlite3_value **apArg
145367 145831   ){
145368 145832     struct NtileCtx *p;
145369         -  assert( nArg==2 ); UNUSED_PARAMETER(nArg);
       145833  +  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
145370 145834     p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145371 145835     if( p ){
145372 145836       if( p->nTotal==0 ){
145373 145837         p->nParam = sqlite3_value_int64(apArg[0]);
145374         -      p->nTotal = sqlite3_value_int64(apArg[1]);
145375 145838         if( p->nParam<=0 ){
145376 145839           sqlite3_result_error(
145377 145840               pCtx, "argument of ntile must be a positive integer", -1
145378 145841           );
145379 145842         }
145380 145843       }
145381         -    p->iRow++;
       145844  +    p->nTotal++;
145382 145845     }
       145846  +}
       145847  +static void ntileInvFunc(
       145848  +  sqlite3_context *pCtx, 
       145849  +  int nArg,
       145850  +  sqlite3_value **apArg
       145851  +){
       145852  +  struct NtileCtx *p;
       145853  +  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
       145854  +  UNUSED_PARAMETER(apArg);
       145855  +  p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145856  +  p->iRow++;
145383 145857   }
145384 145858   static void ntileValueFunc(sqlite3_context *pCtx){
145385 145859     struct NtileCtx *p;
145386 145860     p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
145387 145861     if( p && p->nParam>0 ){
145388 145862       int nSize = (p->nTotal / p->nParam);
145389 145863       if( nSize==0 ){
145390         -      sqlite3_result_int64(pCtx, p->iRow);
       145864  +      sqlite3_result_int64(pCtx, p->iRow+1);
145391 145865       }else{
145392 145866         i64 nLarge = p->nTotal - p->nParam*nSize;
145393 145867         i64 iSmall = nLarge*(nSize+1);
145394         -      i64 iRow = p->iRow-1;
       145868  +      i64 iRow = p->iRow;
145395 145869   
145396 145870         assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
145397 145871   
145398 145872         if( iRow<iSmall ){
145399 145873           sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
145400 145874         }else{
145401 145875           sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
145402 145876         }
145403 145877       }
145404 145878     }
145405 145879   }
       145880  +#define ntileFinalizeFunc ntileValueFunc
145406 145881   
145407 145882   /*
145408 145883   ** Context object for last_value() window function.
145409 145884   */
145410 145885   struct LastValueCtx {
145411 145886     sqlite3_value *pVal;
145412 145887     int nVal;
................................................................................
145448 145923         sqlite3_value_free(p->pVal);
145449 145924         p->pVal = 0;
145450 145925       }
145451 145926     }
145452 145927   }
145453 145928   static void last_valueValueFunc(sqlite3_context *pCtx){
145454 145929     struct LastValueCtx *p;
145455         -  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
       145930  +  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
145456 145931     if( p && p->pVal ){
145457 145932       sqlite3_result_value(pCtx, p->pVal);
145458 145933     }
145459 145934   }
145460 145935   static void last_valueFinalizeFunc(sqlite3_context *pCtx){
145461 145936     struct LastValueCtx *p;
145462 145937     p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
................................................................................
145538 146013   ** Register those built-in window functions that are not also aggregates.
145539 146014   */
145540 146015   SQLITE_PRIVATE void sqlite3WindowFunctions(void){
145541 146016     static FuncDef aWindowFuncs[] = {
145542 146017       WINDOWFUNCX(row_number, 0, 0),
145543 146018       WINDOWFUNCX(dense_rank, 0, 0),
145544 146019       WINDOWFUNCX(rank, 0, 0),
145545         -    WINDOWFUNCX(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE),
145546         -    WINDOWFUNCX(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE),
145547         -    WINDOWFUNCX(ntile, 1, SQLITE_FUNC_WINDOW_SIZE),
       146020  +    WINDOWFUNCALL(percent_rank, 0, 0),
       146021  +    WINDOWFUNCALL(cume_dist, 0, 0),
       146022  +    WINDOWFUNCALL(ntile, 1, 0),
145548 146023       WINDOWFUNCALL(last_value, 1, 0),
145549         -    WINDOWFUNCNOOP(nth_value, 2, 0),
145550         -    WINDOWFUNCNOOP(first_value, 1, 0),
       146024  +    WINDOWFUNCALL(nth_value, 2, 0),
       146025  +    WINDOWFUNCALL(first_value, 1, 0),
145551 146026       WINDOWFUNCNOOP(lead, 1, 0),
145552 146027       WINDOWFUNCNOOP(lead, 2, 0),
145553 146028       WINDOWFUNCNOOP(lead, 3, 0),
145554 146029       WINDOWFUNCNOOP(lag, 1, 0),
145555 146030       WINDOWFUNCNOOP(lag, 2, 0),
145556 146031       WINDOWFUNCNOOP(lag, 3, 0),
145557 146032     };
145558 146033     sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
145559 146034   }
       146035  +
       146036  +static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
       146037  +  Window *p;
       146038  +  for(p=pList; p; p=p->pNextWin){
       146039  +    if( sqlite3StrICmp(p->zName, zName)==0 ) break;
       146040  +  }
       146041  +  if( p==0 ){
       146042  +    sqlite3ErrorMsg(pParse, "no such window: %s", zName);
       146043  +  }
       146044  +  return p;
       146045  +}
145560 146046   
145561 146047   /*
145562 146048   ** This function is called immediately after resolving the function name
145563 146049   ** for a window function within a SELECT statement. Argument pList is a
145564 146050   ** linked list of WINDOW definitions for the current SELECT statement.
145565 146051   ** Argument pFunc is the function definition just resolved and pWin
145566 146052   ** is the Window object representing the associated OVER clause. This
................................................................................
145577 146063   */
145578 146064   SQLITE_PRIVATE void sqlite3WindowUpdate(
145579 146065     Parse *pParse, 
145580 146066     Window *pList,                  /* List of named windows for this SELECT */
145581 146067     Window *pWin,                   /* Window frame to update */
145582 146068     FuncDef *pFunc                  /* Window function definition */
145583 146069   ){
145584         -  if( pWin->zName && pWin->eType==0 ){
145585         -    Window *p;
145586         -    for(p=pList; p; p=p->pNextWin){
145587         -      if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break;
145588         -    }
145589         -    if( p==0 ){
145590         -      sqlite3ErrorMsg(pParse, "no such window: %s", pWin->zName);
145591         -      return;
145592         -    }
       146070  +  if( pWin->zName && pWin->eFrmType==0 ){
       146071  +    Window *p = windowFind(pParse, pList, pWin->zName);
       146072  +    if( p==0 ) return;
145593 146073       pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
145594 146074       pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
145595 146075       pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
145596 146076       pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
145597 146077       pWin->eStart = p->eStart;
145598 146078       pWin->eEnd = p->eEnd;
145599         -    pWin->eType = p->eType;
       146079  +    pWin->eFrmType = p->eFrmType;
       146080  +    pWin->eExclude = p->eExclude;
       146081  +  }else{
       146082  +    sqlite3WindowChain(pParse, pWin, pList);
145600 146083     }
       146084  +  if( (pWin->eFrmType==TK_RANGE)
       146085  +   && (pWin->pStart || pWin->pEnd) 
       146086  +   && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
       146087  +  ){
       146088  +    sqlite3ErrorMsg(pParse, 
       146089  +      "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
       146090  +    );
       146091  +  }else
145601 146092     if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
145602 146093       sqlite3 *db = pParse->db;
145603 146094       if( pWin->pFilter ){
145604 146095         sqlite3ErrorMsg(pParse, 
145605 146096             "FILTER clause may only be used with aggregate window functions"
145606 146097         );
145607         -    }else
145608         -    if( pFunc->zName==row_numberName || pFunc->zName==ntileName ){
145609         -      sqlite3ExprDelete(db, pWin->pStart);
145610         -      sqlite3ExprDelete(db, pWin->pEnd);
145611         -      pWin->pStart = pWin->pEnd = 0;
145612         -      pWin->eType = TK_ROWS;
145613         -      pWin->eStart = TK_UNBOUNDED;
145614         -      pWin->eEnd = TK_CURRENT;
145615         -    }else
145616         -
145617         -    if( pFunc->zName==dense_rankName || pFunc->zName==rankName
145618         -     || pFunc->zName==percent_rankName || pFunc->zName==cume_distName
145619         -    ){
145620         -      sqlite3ExprDelete(db, pWin->pStart);
145621         -      sqlite3ExprDelete(db, pWin->pEnd);
145622         -      pWin->pStart = pWin->pEnd = 0;
145623         -      pWin->eType = TK_RANGE;
145624         -      pWin->eStart = TK_UNBOUNDED;
145625         -      pWin->eEnd = TK_CURRENT;
       146098  +    }else{
       146099  +      struct WindowUpdate {
       146100  +        const char *zFunc;
       146101  +        int eFrmType;
       146102  +        int eStart;
       146103  +        int eEnd;
       146104  +      } aUp[] = {
       146105  +        { row_numberName,   TK_ROWS,   TK_UNBOUNDED, TK_CURRENT }, 
       146106  +        { dense_rankName,   TK_RANGE,  TK_UNBOUNDED, TK_CURRENT }, 
       146107  +        { rankName,         TK_RANGE,  TK_UNBOUNDED, TK_CURRENT }, 
       146108  +        { percent_rankName, TK_GROUPS, TK_CURRENT,   TK_UNBOUNDED }, 
       146109  +        { cume_distName,    TK_GROUPS, TK_FOLLOWING, TK_UNBOUNDED }, 
       146110  +        { ntileName,        TK_ROWS,   TK_CURRENT,   TK_UNBOUNDED }, 
       146111  +        { leadName,         TK_ROWS,   TK_UNBOUNDED, TK_UNBOUNDED }, 
       146112  +        { lagName,          TK_ROWS,   TK_UNBOUNDED, TK_CURRENT }, 
       146113  +      };
       146114  +      int i;
       146115  +      for(i=0; i<ArraySize(aUp); i++){
       146116  +        if( pFunc->zName==aUp[i].zFunc ){
       146117  +          sqlite3ExprDelete(db, pWin->pStart);
       146118  +          sqlite3ExprDelete(db, pWin->pEnd);
       146119  +          pWin->pEnd = pWin->pStart = 0;
       146120  +          pWin->eFrmType = aUp[i].eFrmType;
       146121  +          pWin->eStart = aUp[i].eStart;
       146122  +          pWin->eEnd = aUp[i].eEnd;
       146123  +          pWin->eExclude = 0;
       146124  +          if( pWin->eStart==TK_FOLLOWING ){
       146125  +            pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
       146126  +          }
       146127  +          break;
       146128  +        }
       146129  +      }
145626 146130       }
145627 146131     }
145628 146132     pWin->pFunc = pFunc;
145629 146133   }
145630 146134   
145631 146135   /*
145632 146136   ** Context object passed through sqlite3WalkExprList() to
................................................................................
145823 146327         }
145824 146328       }
145825 146329   
145826 146330       /* Assign a cursor number for the ephemeral table used to buffer rows.
145827 146331       ** The OpenEphemeral instruction is coded later, after it is known how
145828 146332       ** many columns the table will have.  */
145829 146333       pMWin->iEphCsr = pParse->nTab++;
       146334  +    pParse->nTab += 3;
145830 146335   
145831 146336       selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, &pSublist);
145832 146337       selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, &pSublist);
145833 146338       pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
145834 146339   
145835 146340       /* Append the PARTITION BY and ORDER BY expressions to the to the 
145836 146341       ** sub-select expression list. They are required to figure out where 
................................................................................
145878 146383         }else{
145879 146384           pSub->selFlags |= SF_Expanded;
145880 146385           p->selFlags &= ~SF_Aggregate;
145881 146386           sqlite3SelectPrep(pParse, pSub, 0);
145882 146387         }
145883 146388   
145884 146389         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
       146390  +      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
       146391  +      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
       146392  +      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
145885 146393       }else{
145886 146394         sqlite3SelectDelete(db, pSub);
145887 146395       }
145888 146396       if( db->mallocFailed ) rc = SQLITE_NOMEM;
145889 146397     }
145890 146398   
145891 146399     return rc;
................................................................................
145898 146406     if( p ){
145899 146407       sqlite3ExprDelete(db, p->pFilter);
145900 146408       sqlite3ExprListDelete(db, p->pPartition);
145901 146409       sqlite3ExprListDelete(db, p->pOrderBy);
145902 146410       sqlite3ExprDelete(db, p->pEnd);
145903 146411       sqlite3ExprDelete(db, p->pStart);
145904 146412       sqlite3DbFree(db, p->zName);
       146413  +    sqlite3DbFree(db, p->zBase);
145905 146414       sqlite3DbFree(db, p);
145906 146415     }
145907 146416   }
145908 146417   
145909 146418   /*
145910 146419   ** Free the linked list of Window objects starting at the second argument.
145911 146420   */
................................................................................
145934 146443   }
145935 146444   
145936 146445   /*
145937 146446   ** Allocate and return a new Window object describing a Window Definition.
145938 146447   */
145939 146448   SQLITE_PRIVATE Window *sqlite3WindowAlloc(
145940 146449     Parse *pParse,    /* Parsing context */
145941         -  int eType,        /* Frame type. TK_RANGE or TK_ROWS */
       146450  +  int eType,        /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
145942 146451     int eStart,       /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
145943 146452     Expr *pStart,     /* Start window size if TK_PRECEDING or FOLLOWING */
145944 146453     int eEnd,         /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
145945         -  Expr *pEnd        /* End window size if TK_FOLLOWING or PRECEDING */
       146454  +  Expr *pEnd,       /* End window size if TK_FOLLOWING or PRECEDING */
       146455  +  u8 eExclude       /* EXCLUDE clause */
145946 146456   ){
145947 146457     Window *pWin = 0;
       146458  +  int bImplicitFrame = 0;
145948 146459   
145949 146460     /* Parser assures the following: */
145950         -  assert( eType==TK_RANGE || eType==TK_ROWS );
       146461  +  assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS );
145951 146462     assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
145952 146463              || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
145953 146464     assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
145954 146465              || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
145955 146466     assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
145956 146467     assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
145957 146468   
145958         -
145959         -  /* If a frame is declared "RANGE" (not "ROWS"), then it may not use
145960         -  ** either "<expr> PRECEDING" or "<expr> FOLLOWING".
145961         -  */
145962         -  if( eType==TK_RANGE && (pStart!=0 || pEnd!=0) ){
145963         -    sqlite3ErrorMsg(pParse, "RANGE must use only UNBOUNDED or CURRENT ROW");
145964         -    goto windowAllocErr;
       146469  +  if( eType==0 ){
       146470  +    bImplicitFrame = 1;
       146471  +    eType = TK_RANGE;
145965 146472     }
145966 146473   
145967 146474     /* Additionally, the
145968 146475     ** starting boundary type may not occur earlier in the following list than
145969 146476     ** the ending boundary type:
145970 146477     **
145971 146478     **   UNBOUNDED PRECEDING
................................................................................
145977 146484     ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
145978 146485     ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
145979 146486     ** frame boundary.
145980 146487     */
145981 146488     if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
145982 146489      || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
145983 146490     ){
145984         -    sqlite3ErrorMsg(pParse, "unsupported frame delimiter for ROWS");
       146491  +    sqlite3ErrorMsg(pParse, "unsupported frame specification");
145985 146492       goto windowAllocErr;
145986 146493     }
145987 146494   
145988 146495     pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
145989 146496     if( pWin==0 ) goto windowAllocErr;
145990         -  pWin->eType = eType;
       146497  +  pWin->eFrmType = eType;
145991 146498     pWin->eStart = eStart;
145992 146499     pWin->eEnd = eEnd;
       146500  +  if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
       146501  +    eExclude = TK_NO;
       146502  +  }
       146503  +  pWin->eExclude = eExclude;
       146504  +  pWin->bImplicitFrame = bImplicitFrame;
145993 146505     pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
145994 146506     pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
145995 146507     return pWin;
145996 146508   
145997 146509   windowAllocErr:
145998 146510     sqlite3ExprDelete(pParse->db, pEnd);
145999 146511     sqlite3ExprDelete(pParse->db, pStart);
146000 146512     return 0;
146001 146513   }
       146514  +
       146515  +/*
       146516  +** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
       146517  +** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
       146518  +** equivalent nul-terminated string.
       146519  +*/
       146520  +SQLITE_PRIVATE Window *sqlite3WindowAssemble(
       146521  +  Parse *pParse, 
       146522  +  Window *pWin, 
       146523  +  ExprList *pPartition, 
       146524  +  ExprList *pOrderBy, 
       146525  +  Token *pBase
       146526  +){
       146527  +  if( pWin ){
       146528  +    pWin->pPartition = pPartition;
       146529  +    pWin->pOrderBy = pOrderBy;
       146530  +    if( pBase ){
       146531  +      pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
       146532  +    }
       146533  +  }else{
       146534  +    sqlite3ExprListDelete(pParse->db, pPartition);
       146535  +    sqlite3ExprListDelete(pParse->db, pOrderBy);
       146536  +  }
       146537  +  return pWin;
       146538  +}
       146539  +
       146540  +/*
       146541  +** Window *pWin has just been created from a WINDOW clause. Tokne pBase
       146542  +** is the base window. Earlier windows from the same WINDOW clause are
       146543  +** stored in the linked list starting at pWin->pNextWin. This function
       146544  +** either updates *pWin according to the base specification, or else
       146545  +** leaves an error in pParse.
       146546  +*/
       146547  +SQLITE_PRIVATE void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
       146548  +  if( pWin->zBase ){
       146549  +    sqlite3 *db = pParse->db;
       146550  +    Window *pExist = windowFind(pParse, pList, pWin->zBase);
       146551  +    if( pExist ){
       146552  +      const char *zErr = 0;
       146553  +      /* Check for errors */
       146554  +      if( pWin->pPartition ){
       146555  +        zErr = "PARTITION clause";
       146556  +      }else if( pExist->pOrderBy && pWin->pOrderBy ){
       146557  +        zErr = "ORDER BY clause";
       146558  +      }else if( pExist->bImplicitFrame==0 ){
       146559  +        zErr = "frame specification";
       146560  +      }
       146561  +      if( zErr ){
       146562  +        sqlite3ErrorMsg(pParse, 
       146563  +            "cannot override %s of window: %s", zErr, pWin->zBase
       146564  +        );
       146565  +      }else{
       146566  +        pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
       146567  +        if( pExist->pOrderBy ){
       146568  +          assert( pWin->pOrderBy==0 );
       146569  +          pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
       146570  +        }
       146571  +        sqlite3DbFree(db, pWin->zBase);
       146572  +        pWin->zBase = 0;
       146573  +      }
       146574  +    }
       146575  +  }
       146576  +}
146002 146577   
146003 146578   /*
146004 146579   ** Attach window object pWin to expression p.
146005 146580   */
146006 146581   SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
146007 146582     if( p ){
146008 146583       assert( p->op==TK_FUNCTION );
................................................................................
146024 146599   }
146025 146600   
146026 146601   /*
146027 146602   ** Return 0 if the two window objects are identical, or non-zero otherwise.
146028 146603   ** Identical window objects can be processed in a single scan.
146029 146604   */
146030 146605   SQLITE_PRIVATE int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
146031         -  if( p1->eType!=p2->eType ) return 1;
       146606  +  if( p1->eFrmType!=p2->eFrmType ) return 1;
146032 146607     if( p1->eStart!=p2->eStart ) return 1;
146033 146608     if( p1->eEnd!=p2->eEnd ) return 1;
       146609  +  if( p1->eExclude!=p2->eExclude ) return 1;
146034 146610     if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
146035 146611     if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
146036 146612     if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
146037 146613     if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
146038 146614     return 0;
146039 146615   }
146040 146616   
................................................................................
146043 146619   ** This is called by code in select.c before it calls sqlite3WhereBegin()
146044 146620   ** to begin iterating through the sub-query results. It is used to allocate
146045 146621   ** and initialize registers and cursors used by sqlite3WindowCodeStep().
146046 146622   */
146047 146623   SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
146048 146624     Window *pWin;
146049 146625     Vdbe *v = sqlite3GetVdbe(pParse);
146050         -  int nPart = (pMWin->pPartition ? pMWin->pPartition->nExpr : 0);
146051         -  nPart += (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
146052         -  if( nPart ){
       146626  +
       146627  +  /* Allocate registers to use for PARTITION BY values, if any. Initialize
       146628  +  ** said registers to NULL.  */
       146629  +  if( pMWin->pPartition ){
       146630  +    int nExpr = pMWin->pPartition->nExpr;
146053 146631       pMWin->regPart = pParse->nMem+1;
146054         -    pParse->nMem += nPart;
146055         -    sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1);
       146632  +    pParse->nMem += nExpr;
       146633  +    sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
       146634  +  }
       146635  +
       146636  +  pMWin->regOne = ++pParse->nMem;
       146637  +  sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
       146638  +
       146639  +  if( pMWin->eExclude ){
       146640  +    pMWin->regStartRowid = ++pParse->nMem;
       146641  +    pMWin->regEndRowid = ++pParse->nMem;
       146642  +    pMWin->csrApp = pParse->nTab++;
       146643  +    sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
       146644  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
       146645  +    sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
       146646  +    return;
146056 146647     }
146057 146648   
146058 146649     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146059 146650       FuncDef *p = pWin->pFunc;
146060 146651       if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
146061 146652         /* The inline versions of min() and max() require a single ephemeral
146062 146653         ** table and 3 registers. The registers are used as follows:
................................................................................
146077 146668         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
146078 146669         sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
146079 146670         sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
146080 146671       }
146081 146672       else if( p->zName==nth_valueName || p->zName==first_valueName ){
146082 146673         /* Allocate two registers at pWin->regApp. These will be used to
146083 146674         ** store the start and end index of the current frame.  */
146084         -      assert( pMWin->iEphCsr );
146085 146675         pWin->regApp = pParse->nMem+1;
146086 146676         pWin->csrApp = pParse->nTab++;
146087 146677         pParse->nMem += 2;
146088 146678         sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
146089 146679       }
146090 146680       else if( p->zName==leadName || p->zName==lagName ){
146091         -      assert( pMWin->iEphCsr );
146092 146681         pWin->csrApp = pParse->nTab++;
146093 146682         sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
146094 146683       }
146095 146684     }
146096 146685   }
       146686  +
       146687  +#define WINDOW_STARTING_INT  0
       146688  +#define WINDOW_ENDING_INT    1
       146689  +#define WINDOW_NTH_VALUE_INT 2
       146690  +#define WINDOW_STARTING_NUM  3
       146691  +#define WINDOW_ENDING_NUM    4
146097 146692   
146098 146693   /*
146099 146694   ** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
146100 146695   ** value of the second argument to nth_value() (eCond==2) has just been
146101 146696   ** evaluated and the result left in register reg. This function generates VM
146102 146697   ** code to check that the value is a non-negative integer and throws an
146103 146698   ** exception if it is not.
146104 146699   */
146105         -static void windowCheckIntValue(Parse *pParse, int reg, int eCond){
       146700  +static void windowCheckValue(Parse *pParse, int reg, int eCond){
146106 146701     static const char *azErr[] = {
146107 146702       "frame starting offset must be a non-negative integer",
146108 146703       "frame ending offset must be a non-negative integer",
146109         -    "second argument to nth_value must be a positive integer"
       146704  +    "second argument to nth_value must be a positive integer",
       146705  +    "frame starting offset must be a non-negative number",
       146706  +    "frame ending offset must be a non-negative number",
146110 146707     };
146111         -  static int aOp[] = { OP_Ge, OP_Ge, OP_Gt };
       146708  +  static int aOp[] = { OP_Ge, OP_Ge, OP_Gt, OP_Ge, OP_Ge };
146112 146709     Vdbe *v = sqlite3GetVdbe(pParse);
146113 146710     int regZero = sqlite3GetTempReg(pParse);
146114         -  assert( eCond==0 || eCond==1 || eCond==2 );
       146711  +  assert( eCond>=0 && eCond<ArraySize(azErr) );
146115 146712     sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
146116         -  sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
146117         -  VdbeCoverageIf(v, eCond==0);
146118         -  VdbeCoverageIf(v, eCond==1);
146119         -  VdbeCoverageIf(v, eCond==2);
       146713  +  if( eCond>=WINDOW_STARTING_NUM ){
       146714  +    int regString = sqlite3GetTempReg(pParse);
       146715  +    sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
       146716  +    sqlite3VdbeAddOp3(v, OP_Ge, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
       146717  +    sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC|SQLITE_JUMPIFNULL);
       146718  +    VdbeCoverage(v);
       146719  +    assert( eCond==3 || eCond==4 );
       146720  +    VdbeCoverageIf(v, eCond==3);
       146721  +    VdbeCoverageIf(v, eCond==4);
       146722  +  }else{
       146723  +    sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
       146724  +    VdbeCoverage(v);
       146725  +    assert( eCond==0 || eCond==1 || eCond==2 );
       146726  +    VdbeCoverageIf(v, eCond==0);
       146727  +    VdbeCoverageIf(v, eCond==1);
       146728  +    VdbeCoverageIf(v, eCond==2);
       146729  +  }
146120 146730     sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
146121         -  VdbeCoverageNeverNullIf(v, eCond==0);
146122         -  VdbeCoverageNeverNullIf(v, eCond==1);
       146731  +  VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
       146732  +  VdbeCoverageNeverNullIf(v, eCond==1); /*   the OP_MustBeInt */
146123 146733     VdbeCoverageNeverNullIf(v, eCond==2);
       146734  +  VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
       146735  +  VdbeCoverageNeverNullIf(v, eCond==4); /*   the OP_Ge */
146124 146736     sqlite3MayAbort(pParse);
146125 146737     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
146126 146738     sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC);
146127 146739     sqlite3ReleaseTempReg(pParse, regZero);
146128 146740   }
146129 146741   
146130 146742   /*
................................................................................
146156 146768   ** number of rows in the current partition.
146157 146769   */
146158 146770   static void windowAggStep(
146159 146771     Parse *pParse, 
146160 146772     Window *pMWin,                  /* Linked list of window functions */
146161 146773     int csr,                        /* Read arguments from this cursor */
146162 146774     int bInverse,                   /* True to invoke xInverse instead of xStep */
146163         -  int reg,                        /* Array of registers */
146164         -  int regPartSize                 /* Register containing size of partition */
       146775  +  int reg                         /* Array of registers */
146165 146776   ){
146166 146777     Vdbe *v = sqlite3GetVdbe(pParse);
146167 146778     Window *pWin;
146168 146779     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146169         -    int flags = pWin->pFunc->funcFlags;
       146780  +    FuncDef *pFunc = pWin->pFunc;
146170 146781       int regArg;
146171 146782       int nArg = windowArgCount(pWin);
       146783  +    int i;
146172 146784   
146173         -    if( csr>=0 ){
146174         -      int i;
146175         -      for(i=0; i<nArg; i++){
       146785  +    for(i=0; i<nArg; i++){
       146786  +      if( i!=1 || pFunc->zName!=nth_valueName ){
146176 146787           sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
       146788  +      }else{
       146789  +        sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
146177 146790         }
146178         -      regArg = reg;
146179         -      if( flags & SQLITE_FUNC_WINDOW_SIZE ){
146180         -        if( nArg==0 ){
146181         -          regArg = regPartSize;
146182         -        }else{
146183         -          sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg);
146184         -        }
146185         -        nArg++;
146186         -      }
146187         -    }else{
146188         -      assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) );
146189         -      regArg = reg + pWin->iArgCol;
146190 146791       }
       146792  +    regArg = reg;
146191 146793   
146192         -    if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
146193         -      && pWin->eStart!=TK_UNBOUNDED 
       146794  +    if( pMWin->regStartRowid==0
       146795  +     && (pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
       146796  +     && (pWin->eStart!=TK_UNBOUNDED)
146194 146797       ){
146195 146798         int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regArg);
146196 146799         VdbeCoverage(v);
146197 146800         if( bInverse==0 ){
146198 146801           sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
146199 146802           sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
146200 146803           sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
................................................................................
146203 146806           sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
146204 146807           VdbeCoverageNeverTaken(v);
146205 146808           sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
146206 146809           sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
146207 146810         }
146208 146811         sqlite3VdbeJumpHere(v, addrIsNull);
146209 146812       }else if( pWin->regApp ){
146210         -      assert( pWin->pFunc->zName==nth_valueName
146211         -           || pWin->pFunc->zName==first_valueName
       146813  +      assert( pFunc->zName==nth_valueName
       146814  +           || pFunc->zName==first_valueName
146212 146815         );
146213 146816         assert( bInverse==0 || bInverse==1 );
146214 146817         sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
146215         -    }else if( pWin->pFunc->zName==leadName
146216         -           || pWin->pFunc->zName==lagName
146217         -    ){
146218         -      /* no-op */
146219         -    }else{
       146818  +    }else if( pFunc->xSFunc!=noopStepFunc ){
146220 146819         int addrIf = 0;
146221 146820         if( pWin->pFilter ){
146222 146821           int regTmp;
146223 146822           assert( nArg==0 || nArg==pWin->pOwner->x.pList->nExpr );
146224 146823           assert( nArg || pWin->pOwner->x.pList==0 );
146225         -        if( csr>0 ){
146226         -          regTmp = sqlite3GetTempReg(pParse);
146227         -          sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
146228         -        }else{
146229         -          regTmp = regArg + nArg;
146230         -        }
       146824  +        regTmp = sqlite3GetTempReg(pParse);
       146825  +        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
146231 146826           addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
146232 146827           VdbeCoverage(v);
146233         -        if( csr>0 ){
146234         -          sqlite3ReleaseTempReg(pParse, regTmp);
146235         -        }
       146828  +        sqlite3ReleaseTempReg(pParse, regTmp);
146236 146829         }
146237         -      if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
       146830  +      if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
146238 146831           CollSeq *pColl;
146239 146832           assert( nArg>0 );
146240 146833           pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
146241 146834           sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
146242 146835         }
146243 146836         sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep, 
146244 146837                           bInverse, regArg, pWin->regAccum);
146245         -      sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
       146838  +      sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
146246 146839         sqlite3VdbeChangeP5(v, (u8)nArg);
146247 146840         if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
146248 146841       }
146249 146842     }
146250 146843   }
146251 146844   
       146845  +typedef struct WindowCodeArg WindowCodeArg;
       146846  +typedef struct WindowCsrAndReg WindowCsrAndReg;
       146847  +struct WindowCsrAndReg {
       146848  +  int csr;
       146849  +  int reg;
       146850  +};
       146851  +
       146852  +struct WindowCodeArg {
       146853  +  Parse *pParse;
       146854  +  Window *pMWin;
       146855  +  Vdbe *pVdbe;
       146856  +  int regGosub;
       146857  +  int addrGosub;
       146858  +  int regArg;
       146859  +  int eDelete;
       146860  +
       146861  +  WindowCsrAndReg start;
       146862  +  WindowCsrAndReg current;
       146863  +  WindowCsrAndReg end;
       146864  +};
       146865  +
146252 146866   /*
146253         -** Generate VM code to invoke either xValue() (bFinal==0) or xFinalize()
146254         -** (bFinal==1) for each window function in the linked list starting at
       146867  +** Values that may be passed as the second argument to windowCodeOp().
       146868  +*/
       146869  +#define WINDOW_RETURN_ROW 1
       146870  +#define WINDOW_AGGINVERSE 2
       146871  +#define WINDOW_AGGSTEP    3
       146872  +
       146873  +/*
       146874  +** Generate VM code to read the window frames peer values from cursor csr into
       146875  +** an array of registers starting at reg.
       146876  +*/
       146877  +static void windowReadPeerValues(
       146878  +  WindowCodeArg *p,
       146879  +  int csr,
       146880  +  int reg
       146881  +){
       146882  +  Window *pMWin = p->pMWin;
       146883  +  ExprList *pOrderBy = pMWin->pOrderBy;
       146884  +  if( pOrderBy ){
       146885  +    Vdbe *v = sqlite3GetVdbe(p->pParse);
       146886  +    ExprList *pPart = pMWin->pPartition;
       146887  +    int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
       146888  +    int i;
       146889  +    for(i=0; i<pOrderBy->nExpr; i++){
       146890  +      sqlite3VdbeAddOp3(v, OP_Column, csr, iColOff+i, reg+i);
       146891  +    }
       146892  +  }
       146893  +}
       146894  +
       146895  +/*
       146896  +** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
       146897  +** (bFin==1) for each window function in the linked list starting at
146255 146898   ** pMWin. Or, for built-in window-functions that do not use the standard
146256 146899   ** API, generate the equivalent VM code.
146257 146900   */
146258         -static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){
       146901  +static void windowAggFinal(WindowCodeArg *p, int bFin){
       146902  +  Parse *pParse = p->pParse;
       146903  +  Window *pMWin = p->pMWin;
146259 146904     Vdbe *v = sqlite3GetVdbe(pParse);
146260 146905     Window *pWin;
146261 146906   
146262 146907     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146263         -    if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
146264         -     && pWin->eStart!=TK_UNBOUNDED 
       146908  +    if( pMWin->regStartRowid==0
       146909  +     && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
       146910  +     && (pWin->eStart!=TK_UNBOUNDED)
146265 146911       ){
146266 146912         sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146267 146913         sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
146268 146914         VdbeCoverage(v);
146269 146915         sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
146270 146916         sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
146271         -      if( bFinal ){
146272         -        sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
146273         -      }
146274 146917       }else if( pWin->regApp ){
       146918  +      assert( pMWin->regStartRowid==0 );
146275 146919       }else{
146276         -      if( bFinal ){
146277         -        sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin));
       146920  +      int nArg = windowArgCount(pWin);
       146921  +      if( bFin ){
       146922  +        sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
146278 146923           sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
146279 146924           sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
146280 146925           sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
146281 146926         }else{
146282         -        sqlite3VdbeAddOp3(v, OP_AggValue, pWin->regAccum, windowArgCount(pWin),
146283         -                             pWin->regResult);
       146927  +        sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
146284 146928           sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
146285 146929         }
146286 146930       }
146287 146931     }
146288 146932   }
146289 146933   
146290 146934   /*
146291         -** This function generates VM code to invoke the sub-routine at address
146292         -** lblFlushPart once for each partition with the entire partition cached in
146293         -** the Window.iEphCsr temp table.
146294         -*/
146295         -static void windowPartitionCache(
146296         -  Parse *pParse,
146297         -  Select *p,                      /* The rewritten SELECT statement */
146298         -  WhereInfo *pWInfo,              /* WhereInfo to call WhereEnd() on */
146299         -  int regFlushPart,               /* Register to use with Gosub lblFlushPart */
146300         -  int lblFlushPart,               /* Subroutine to Gosub to */
146301         -  int *pRegSize                   /* OUT: Register containing partition size */
146302         -){
146303         -  Window *pMWin = p->pWin;
146304         -  Vdbe *v = sqlite3GetVdbe(pParse);
146305         -  int iSubCsr = p->pSrc->a[0].iCursor;
146306         -  int nSub = p->pSrc->a[0].pTab->nCol;
146307         -  int k;
146308         -
146309         -  int reg = pParse->nMem+1;
146310         -  int regRecord = reg+nSub;
146311         -  int regRowid = regRecord+1;
146312         -
146313         -  *pRegSize = regRowid;
146314         -  pParse->nMem += nSub + 2;
146315         -
146316         -  /* Load the column values for the row returned by the sub-select
146317         -  ** into an array of registers starting at reg. */
146318         -  for(k=0; k<nSub; k++){
146319         -    sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
146320         -  }
146321         -  sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);
146322         -
146323         -  /* Check if this is the start of a new partition. If so, call the
146324         -  ** flush_partition sub-routine.  */
146325         -  if( pMWin->pPartition ){
       146935  +** Generate code to calculate the current values of all window functions in the
       146936  +** p->pMWin list by doing a full scan of the current window frame. Store the
       146937  +** results in the Window.regResult registers, ready to return the upper
       146938  +** layer.
       146939  +*/
       146940  +static void windowFullScan(WindowCodeArg *p){
       146941  +  Window *pWin;
       146942  +  Parse *pParse = p->pParse;
       146943  +  Window *pMWin = p->pMWin;
       146944  +  Vdbe *v = p->pVdbe;
       146945  +
       146946  +  int regCRowid = 0;              /* Current rowid value */
       146947  +  int regCPeer = 0;               /* Current peer values */
       146948  +  int regRowid = 0;               /* AggStep rowid value */
       146949  +  int regPeer = 0;                /* AggStep peer values */
       146950  +
       146951  +  int nPeer;
       146952  +  int lblNext;
       146953  +  int lblBrk;
       146954  +  int addrNext;
       146955  +  int csr = pMWin->csrApp;
       146956  +
       146957  +  nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
       146958  +
       146959  +  lblNext = sqlite3VdbeMakeLabel(pParse);
       146960  +  lblBrk = sqlite3VdbeMakeLabel(pParse);
       146961  +
       146962  +  regCRowid = sqlite3GetTempReg(pParse);
       146963  +  regRowid = sqlite3GetTempReg(pParse);
       146964  +  if( nPeer ){
       146965  +    regCPeer = sqlite3GetTempRange(pParse, nPeer);
       146966  +    regPeer = sqlite3GetTempRange(pParse, nPeer);
       146967  +  }
       146968  +
       146969  +  sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
       146970  +  windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
       146971  +
       146972  +  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
       146973  +    sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
       146974  +  }
       146975  +
       146976  +  sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
       146977  +  VdbeCoverage(v);
       146978  +  addrNext = sqlite3VdbeCurrentAddr(v);
       146979  +  sqlite3VdbeAddOp2(v, OP_Rowid, csr, regRowid);
       146980  +  sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
       146981  +  VdbeCoverageNeverNull(v);
       146982  +
       146983  +  if( pMWin->eExclude==TK_CURRENT ){
       146984  +    sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, lblNext, regRowid);
       146985  +    VdbeCoverageNeverNull(v);
       146986  +  }else if( pMWin->eExclude!=TK_NO ){
146326 146987       int addr;
146327         -    ExprList *pPart = pMWin->pPartition;
146328         -    int nPart = pPart->nExpr;
146329         -    int regNewPart = reg + pMWin->nBufferCol;
146330         -    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
146331         -
146332         -    addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
146333         -    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
146334         -    sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
146335         -    VdbeCoverageEqNe(v);
146336         -    sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
146337         -    sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
146338         -    VdbeComment((v, "call flush_partition"));
146339         -  }
146340         -
146341         -  /* Buffer the current row in the ephemeral table. */
146342         -  sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
146343         -  sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
146344         -
146345         -  /* End of the input loop */
146346         -  sqlite3WhereEnd(pWInfo);
146347         -
146348         -  /* Invoke "flush_partition" to deal with the final (or only) partition */
146349         -  sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
146350         -  VdbeComment((v, "call flush_partition"));
       146988  +    int addrEq = 0;
       146989  +    KeyInfo *pKeyInfo = 0;
       146990  +
       146991  +    if( pMWin->pOrderBy ){
       146992  +      pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
       146993  +    }
       146994  +    if( pMWin->eExclude==TK_TIES ){
       146995  +      addrEq = sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, 0, regRowid);
       146996  +      VdbeCoverageNeverNull(v);
       146997  +    }
       146998  +    if( pKeyInfo ){
       146999  +      windowReadPeerValues(p, csr, regPeer);
       147000  +      sqlite3VdbeAddOp3(v, OP_Compare, regPeer, regCPeer, nPeer);
       147001  +      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
       147002  +      addr = sqlite3VdbeCurrentAddr(v)+1;
       147003  +      sqlite3VdbeAddOp3(v, OP_Jump, addr, lblNext, addr);
       147004  +      VdbeCoverageEqNe(v);
       147005  +    }else{
       147006  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, lblNext);
       147007  +    }
       147008  +    if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
       147009  +  }
       147010  +
       147011  +  windowAggStep(pParse, pMWin, csr, 0, p->regArg);
       147012  +
       147013  +  sqlite3VdbeResolveLabel(v, lblNext);
       147014  +  sqlite3VdbeAddOp2(v, OP_Next, csr, addrNext);
       147015  +  VdbeCoverage(v);
       147016  +  sqlite3VdbeJumpHere(v, addrNext-1);
       147017  +  sqlite3VdbeJumpHere(v, addrNext+1);
       147018  +  sqlite3ReleaseTempReg(pParse, regRowid);
       147019  +  sqlite3ReleaseTempReg(pParse, regCRowid);
       147020  +  if( nPeer ){
       147021  +    sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
       147022  +    sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
       147023  +  }
       147024  +
       147025  +  windowAggFinal(p, 1);
146351 147026   }
146352 147027   
146353 147028   /*
146354 147029   ** Invoke the sub-routine at regGosub (generated by code in select.c) to
146355 147030   ** return the current row of Window.iEphCsr. If all window functions are
146356 147031   ** aggregate window functions that use the standard API, a single
146357 147032   ** OP_Gosub instruction is all that this routine generates. Extra VM code
................................................................................
146359 147034   ** functions:
146360 147035   **
146361 147036   **   nth_value()
146362 147037   **   first_value()
146363 147038   **   lag()
146364 147039   **   lead()
146365 147040   */
146366         -static void windowReturnOneRow(
146367         -  Parse *pParse,
146368         -  Window *pMWin,
146369         -  int regGosub,
146370         -  int addrGosub
146371         -){
146372         -  Vdbe *v = sqlite3GetVdbe(pParse);
146373         -  Window *pWin;
146374         -  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146375         -    FuncDef *pFunc = pWin->pFunc;
146376         -    if( pFunc->zName==nth_valueName
146377         -     || pFunc->zName==first_valueName
146378         -    ){
146379         -      int csr = pWin->csrApp;
146380         -      int lbl = sqlite3VdbeMakeLabel(pParse);
146381         -      int tmpReg = sqlite3GetTempReg(pParse);
146382         -      sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146383         -
146384         -      if( pFunc->zName==nth_valueName ){
146385         -        sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg);
146386         -        windowCheckIntValue(pParse, tmpReg, 2);
146387         -      }else{
146388         -        sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
146389         -      }
146390         -      sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
146391         -      sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
146392         -      VdbeCoverageNeverNull(v);
146393         -      sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
146394         -      VdbeCoverageNeverTaken(v);
146395         -      sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
146396         -      sqlite3VdbeResolveLabel(v, lbl);
146397         -      sqlite3ReleaseTempReg(pParse, tmpReg);
146398         -    }
146399         -    else if( pFunc->zName==leadName || pFunc->zName==lagName ){
146400         -      int nArg = pWin->pOwner->x.pList->nExpr;
146401         -      int iEph = pMWin->iEphCsr;
146402         -      int csr = pWin->csrApp;
146403         -      int lbl = sqlite3VdbeMakeLabel(pParse);
146404         -      int tmpReg = sqlite3GetTempReg(pParse);
146405         -
146406         -      if( nArg<3 ){
       147041  +static void windowReturnOneRow(WindowCodeArg *p){
       147042  +  Window *pMWin = p->pMWin;
       147043  +  Vdbe *v = p->pVdbe;
       147044  +
       147045  +  if( pMWin->regStartRowid ){
       147046  +    windowFullScan(p);
       147047  +  }else{
       147048  +    Parse *pParse = p->pParse;
       147049  +    Window *pWin;
       147050  +
       147051  +    for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
       147052  +      FuncDef *pFunc = pWin->pFunc;
       147053  +      if( pFunc->zName==nth_valueName
       147054  +       || pFunc->zName==first_valueName
       147055  +      ){
       147056  +        int csr = pWin->csrApp;
       147057  +        int lbl = sqlite3VdbeMakeLabel(pParse);
       147058  +        int tmpReg = sqlite3GetTempReg(pParse);
146407 147059           sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
146408         -      }else{
146409         -        sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+2, pWin->regResult);
146410         -      }
146411         -      sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
146412         -      if( nArg<2 ){
146413         -        int val = (pFunc->zName==leadName ? 1 : -1);
146414         -        sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
146415         -      }else{
146416         -        int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
146417         -        int tmpReg2 = sqlite3GetTempReg(pParse);
146418         -        sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
146419         -        sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
146420         -        sqlite3ReleaseTempReg(pParse, tmpReg2);
146421         -      }
146422         -
146423         -      sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
146424         -      VdbeCoverage(v);
146425         -      sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
146426         -      sqlite3VdbeResolveLabel(v, lbl);
146427         -      sqlite3ReleaseTempReg(pParse, tmpReg);
146428         -    }
146429         -  }
146430         -  sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
146431         -}
146432         -
146433         -/*
146434         -** Invoke the code generated by windowReturnOneRow() and, optionally, the
146435         -** xInverse() function for each window function, for one or more rows
146436         -** from the Window.iEphCsr temp table. This routine generates VM code
146437         -** similar to:
146438         -**
146439         -**   while( regCtr>0 ){
146440         -**     regCtr--;
146441         -**     windowReturnOneRow()
146442         -**     if( bInverse ){
146443         -**       AggInverse
146444         -**     }
146445         -**     Next (Window.iEphCsr)
146446         -**   }
146447         -*/
146448         -static void windowReturnRows(
146449         -  Parse *pParse,
146450         -  Window *pMWin,                  /* List of window functions */
146451         -  int regCtr,                     /* Register containing number of rows */
146452         -  int regGosub,                   /* Register for Gosub addrGosub */
146453         -  int addrGosub,                  /* Address of sub-routine for ReturnOneRow */
146454         -  int regInvArg,                  /* Array of registers for xInverse args */
146455         -  int regInvSize                  /* Register containing size of partition */
146456         -){
146457         -  int addr;
146458         -  Vdbe *v = sqlite3GetVdbe(pParse);
146459         -  windowAggFinal(pParse, pMWin, 0);
146460         -  addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1);
146461         -  VdbeCoverage(v);
146462         -  sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
146463         -  windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
146464         -  if( regInvArg ){
146465         -    windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize);
146466         -  }
146467         -  sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr);
146468         -  VdbeCoverage(v);
146469         -  sqlite3VdbeJumpHere(v, addr+1);   /* The OP_Goto */
       147060  +  
       147061  +        if( pFunc->zName==nth_valueName ){
       147062  +          sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
       147063  +          windowCheckValue(pParse, tmpReg, 2);
       147064  +        }else{
       147065  +          sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
       147066  +        }
       147067  +        sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
       147068  +        sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
       147069  +        VdbeCoverageNeverNull(v);
       147070  +        sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
       147071  +        VdbeCoverageNeverTaken(v);
       147072  +        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
       147073  +        sqlite3VdbeResolveLabel(v, lbl);
       147074  +        sqlite3ReleaseTempReg(pParse, tmpReg);
       147075  +      }
       147076  +      else if( pFunc->zName==leadName || pFunc->zName==lagName ){
       147077  +        int nArg = pWin->pOwner->x.pList->nExpr;
       147078  +        int csr = pWin->csrApp;
       147079  +        int lbl = sqlite3VdbeMakeLabel(pParse);
       147080  +        int tmpReg = sqlite3GetTempReg(pParse);
       147081  +        int iEph = pMWin->iEphCsr;
       147082  +  
       147083  +        if( nArg<3 ){
       147084  +          sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
       147085  +        }else{
       147086  +          sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
       147087  +        }
       147088  +        sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
       147089  +        if( nArg<2 ){
       147090  +          int val = (pFunc->zName==leadName ? 1 : -1);
       147091  +          sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
       147092  +        }else{
       147093  +          int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
       147094  +          int tmpReg2 = sqlite3GetTempReg(pParse);
       147095  +          sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
       147096  +          sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
       147097  +          sqlite3ReleaseTempReg(pParse, tmpReg2);
       147098  +        }
       147099  +  
       147100  +        sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
       147101  +        VdbeCoverage(v);
       147102  +        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
       147103  +        sqlite3VdbeResolveLabel(v, lbl);
       147104  +        sqlite3ReleaseTempReg(pParse, tmpReg);
       147105  +      }
       147106  +    }
       147107  +  }
       147108  +  sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
146470 147109   }
146471 147110   
146472 147111   /*
146473 147112   ** Generate code to set the accumulator register for each window function
146474 147113   ** in the linked list passed as the second argument to NULL. And perform
146475 147114   ** any equivalent initialization required by any built-in window functions
146476 147115   ** in the list.
................................................................................
146480 147119     int regArg;
146481 147120     int nArg = 0;
146482 147121     Window *pWin;
146483 147122     for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146484 147123       FuncDef *pFunc = pWin->pFunc;
146485 147124       sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
146486 147125       nArg = MAX(nArg, windowArgCount(pWin));
146487         -    if( pFunc->zName==nth_valueName
146488         -     || pFunc->zName==first_valueName
146489         -    ){
146490         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
146491         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
146492         -    }
       147126  +    if( pMWin->regStartRowid==0 ){
       147127  +      if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
       147128  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
       147129  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
       147130  +      }
146493 147131   
146494         -    if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
146495         -      assert( pWin->eStart!=TK_UNBOUNDED );
146496         -      sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
146497         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
       147132  +      if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
       147133  +        assert( pWin->eStart!=TK_UNBOUNDED );
       147134  +        sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
       147135  +        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
       147136  +      }
146498 147137       }
146499 147138     }
146500 147139     regArg = pParse->nMem+1;
146501 147140     pParse->nMem += nArg;
146502 147141     return regArg;
146503 147142   }
146504 147143   
146505         -
146506         -/*
146507         -** This function does the work of sqlite3WindowCodeStep() for all "ROWS"
146508         -** window frame types except for "BETWEEN UNBOUNDED PRECEDING AND CURRENT
146509         -** ROW". Pseudo-code for each follows.
146510         -**
146511         -** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
146512         -**
146513         -**     ...
146514         -**       if( new partition ){
146515         -**         Gosub flush_partition
146516         -**       }
146517         -**       Insert (record in eph-table)
146518         -**     sqlite3WhereEnd()
146519         -**     Gosub flush_partition
146520         -**  
146521         -**   flush_partition:
146522         -**     Once {
146523         -**       OpenDup (iEphCsr -> csrStart)
146524         -**       OpenDup (iEphCsr -> csrEnd)
146525         -**     }
146526         -**     regStart = <expr1>                // PRECEDING expression
146527         -**     regEnd = <expr2>                  // FOLLOWING expression
146528         -**     if( regStart<0 || regEnd<0 ){ error! }
146529         -**     Rewind (csr,csrStart,csrEnd)      // if EOF goto flush_partition_done
146530         -**       Next(csrEnd)                    // if EOF skip Aggstep
146531         -**       Aggstep (csrEnd)
146532         -**       if( (regEnd--)<=0 ){
146533         -**         AggFinal (xValue)
146534         -**         Gosub addrGosub
146535         -**         Next(csr)                // if EOF goto flush_partition_done
146536         -**         if( (regStart--)<=0 ){
146537         -**           AggInverse (csrStart)
146538         -**           Next(csrStart)
146539         -**         }
146540         -**       }
146541         -**   flush_partition_done:
146542         -**     ResetSorter (csr)
146543         -**     Return
146544         -**
146545         -** ROWS BETWEEN <expr> PRECEDING    AND CURRENT ROW
146546         -** ROWS BETWEEN CURRENT ROW         AND <expr> FOLLOWING
146547         -** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING
146548         -**
146549         -**   These are similar to the above. For "CURRENT ROW", intialize the
146550         -**   register to 0. For "UNBOUNDED PRECEDING" to infinity.
146551         -**
146552         -** ROWS BETWEEN <expr> PRECEDING    AND UNBOUNDED FOLLOWING
146553         -** ROWS BETWEEN CURRENT ROW         AND UNBOUNDED FOLLOWING
146554         -**
146555         -**     Rewind (csr,csrStart,csrEnd)    // if EOF goto flush_partition_done
146556         -**     while( 1 ){
146557         -**       Next(csrEnd)                  // Exit while(1) at EOF
146558         -**       Aggstep (csrEnd)
146559         -**     }
146560         -**     while( 1 ){
146561         -**       AggFinal (xValue)
146562         -**       Gosub addrGosub
146563         -**       Next(csr)                     // if EOF goto flush_partition_done
146564         -**       if( (regStart--)<=0 ){
146565         -**         AggInverse (csrStart)
146566         -**         Next(csrStart)
146567         -**       }
146568         -**     }
146569         -**
146570         -**   For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if() 
146571         -**   condition is always true (as if regStart were initialized to 0).
146572         -**
146573         -** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146574         -** 
146575         -**   This is the only RANGE case handled by this routine. It modifies the
146576         -**   second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to
146577         -**   be:
146578         -**
146579         -**     while( 1 ){
146580         -**       AggFinal (xValue)
146581         -**       while( 1 ){
146582         -**         regPeer++
146583         -**         Gosub addrGosub
146584         -**         Next(csr)                     // if EOF goto flush_partition_done
146585         -**         if( new peer ) break;
146586         -**       }
146587         -**       while( (regPeer--)>0 ){
146588         -**         AggInverse (csrStart)
146589         -**         Next(csrStart)
146590         -**       }
146591         -**     }
146592         -**
146593         -** ROWS BETWEEN <expr> FOLLOWING    AND <expr> FOLLOWING
146594         -**
146595         -**   regEnd = regEnd - regStart
146596         -**   Rewind (csr,csrStart,csrEnd)   // if EOF goto flush_partition_done
146597         -**     Aggstep (csrEnd)
146598         -**     Next(csrEnd)                 // if EOF fall-through
146599         -**     if( (regEnd--)<=0 ){
146600         -**       if( (regStart--)<=0 ){
146601         -**         AggFinal (xValue)
146602         -**         Gosub addrGosub
146603         -**         Next(csr)              // if EOF goto flush_partition_done
146604         -**       }
146605         -**       AggInverse (csrStart)
146606         -**       Next (csrStart)
146607         -**     }
146608         -**
146609         -** ROWS BETWEEN <expr> PRECEDING    AND <expr> PRECEDING
146610         -**
146611         -**   Replace the bit after "Rewind" in the above with:
146612         -**
146613         -**     if( (regEnd--)<=0 ){
146614         -**       AggStep (csrEnd)
146615         -**       Next (csrEnd)
146616         -**     }
146617         -**     AggFinal (xValue)
146618         -**     Gosub addrGosub
146619         -**     Next(csr)                  // if EOF goto flush_partition_done
146620         -**     if( (regStart--)<=0 ){
146621         -**       AggInverse (csr2)
146622         -**       Next (csr2)
146623         -**     }
146624         -**
146625         -*/
146626         -static void windowCodeRowExprStep(
146627         -  Parse *pParse, 
146628         -  Select *p,
146629         -  WhereInfo *pWInfo,
146630         -  int regGosub, 
146631         -  int addrGosub
146632         -){
146633         -  Window *pMWin = p->pWin;
       147144  +/* 
       147145  +** Return true if the current frame should be cached in the ephemeral table,
       147146  +** even if there are no xInverse() calls required.
       147147  +*/
       147148  +static int windowCacheFrame(Window *pMWin){
       147149  +  Window *pWin;
       147150  +  if( pMWin->regStartRowid ) return 1;
       147151  +  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
       147152  +    FuncDef *pFunc = pWin->pFunc;
       147153  +    if( (pFunc->zName==nth_valueName)
       147154  +     || (pFunc->zName==first_valueName)
       147155  +     || (pFunc->zName==leadName)
       147156  +     || (pFunc->zName==lagName)
       147157  +    ){
       147158  +      return 1;
       147159  +    }
       147160  +  }
       147161  +  return 0;
       147162  +}
       147163  +
       147164  +/*
       147165  +** regOld and regNew are each the first register in an array of size
       147166  +** pOrderBy->nExpr. This function generates code to compare the two
       147167  +** arrays of registers using the collation sequences and other comparison
       147168  +** parameters specified by pOrderBy. 
       147169  +**
       147170  +** If the two arrays are not equal, the contents of regNew is copied to 
       147171  +** regOld and control falls through. Otherwise, if the contents of the arrays
       147172  +** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
       147173  +*/
       147174  +static void windowIfNewPeer(
       147175  +  Parse *pParse,
       147176  +  ExprList *pOrderBy,
       147177  +  int regNew,                     /* First in array of new values */
       147178  +  int regOld,                     /* First in array of old values */
       147179  +  int addr                        /* Jump here */
       147180  +){
       147181  +  Vdbe *v = sqlite3GetVdbe(pParse);
       147182  +  if( pOrderBy ){
       147183  +    int nVal = pOrderBy->nExpr;
       147184  +    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
       147185  +    sqlite3VdbeAddOp3(v, OP_Compare, regOld, regNew, nVal);
       147186  +    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
       147187  +    sqlite3VdbeAddOp3(v, OP_Jump, 
       147188  +      sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1
       147189  +    );
       147190  +    VdbeCoverageEqNe(v);
       147191  +    sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
       147192  +  }else{
       147193  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
       147194  +  }
       147195  +}
       147196  +
       147197  +/*
       147198  +** This function is called as part of generating VM programs for RANGE
       147199  +** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
       147200  +** the ORDER BY term in the window, it generates code equivalent to:
       147201  +**
       147202  +**   if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
       147203  +**
       147204  +** A special type of arithmetic is used such that if csr.peerVal is not
       147205  +** a numeric type (real or integer), then the result of the addition is
       147206  +** a copy of csr1.peerVal.
       147207  +*/
       147208  +static void windowCodeRangeTest(
       147209  +  WindowCodeArg *p, 
       147210  +  int op,                          /* OP_Ge or OP_Gt */
       147211  +  int csr1, 
       147212  +  int regVal, 
       147213  +  int csr2,
       147214  +  int lbl
       147215  +){
       147216  +  Parse *pParse = p->pParse;
146634 147217     Vdbe *v = sqlite3GetVdbe(pParse);
146635         -  int regFlushPart;               /* Register for "Gosub flush_partition" */
146636         -  int lblFlushPart;               /* Label for "Gosub flush_partition" */
146637         -  int lblFlushDone;               /* Label for "Gosub flush_partition_done" */
146638         -
146639         -  int regArg;
146640         -  int addr;
146641         -  int csrStart = pParse->nTab++;
146642         -  int csrEnd = pParse->nTab++;
146643         -  int regStart;                    /* Value of <expr> PRECEDING */
146644         -  int regEnd;                      /* Value of <expr> FOLLOWING */
146645         -  int addrGoto;
146646         -  int addrTop;
146647         -  int addrIfPos1 = 0;
146648         -  int addrIfPos2 = 0;
146649         -  int regSize = 0;
146650         -
146651         -  assert( pMWin->eStart==TK_PRECEDING 
146652         -       || pMWin->eStart==TK_CURRENT 
146653         -       || pMWin->eStart==TK_FOLLOWING 
146654         -       || pMWin->eStart==TK_UNBOUNDED 
146655         -  );
146656         -  assert( pMWin->eEnd==TK_FOLLOWING 
146657         -       || pMWin->eEnd==TK_CURRENT 
146658         -       || pMWin->eEnd==TK_UNBOUNDED 
146659         -       || pMWin->eEnd==TK_PRECEDING 
146660         -  );
146661         -
146662         -  /* Allocate register and label for the "flush_partition" sub-routine. */
146663         -  regFlushPart = ++pParse->nMem;
146664         -  lblFlushPart = sqlite3VdbeMakeLabel(pParse);
146665         -  lblFlushDone = sqlite3VdbeMakeLabel(pParse);
146666         -
146667         -  regStart = ++pParse->nMem;
146668         -  regEnd = ++pParse->nMem;
146669         -
146670         -  windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
146671         -
146672         -  addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146673         -
146674         -  /* Start of "flush_partition" */
146675         -  sqlite3VdbeResolveLabel(v, lblFlushPart);
146676         -  sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3);
       147218  +  int reg1 = sqlite3GetTempReg(pParse);
       147219  +  int reg2 = sqlite3GetTempReg(pParse);
       147220  +  int arith = OP_Add;
       147221  +  int addrGe;
       147222  +
       147223  +  int regString = ++pParse->nMem;
       147224  +
       147225  +  assert( op==OP_Ge || op==OP_Gt || op==OP_Le );
       147226  +  assert( p->pMWin->pOrderBy && p->pMWin->pOrderBy->nExpr==1 );
       147227  +  if( p->pMWin->pOrderBy->a[0].sortOrder ){
       147228  +    switch( op ){
       147229  +      case OP_Ge: op = OP_Le; break;
       147230  +      case OP_Gt: op = OP_Lt; break;
       147231  +      default: assert( op==OP_Le ); op = OP_Ge; break;
       147232  +    }
       147233  +    arith = OP_Subtract;
       147234  +  }
       147235  +
       147236  +  windowReadPeerValues(p, csr1, reg1);
       147237  +  windowReadPeerValues(p, csr2, reg2);
       147238  +
       147239  +  /* Check if the peer value for csr1 value is a text or blob by comparing
       147240  +  ** it to the smallest possible string - ''. If it is, jump over the
       147241  +  ** OP_Add or OP_Subtract operation and proceed directly to the comparison. */
       147242  +  sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
       147243  +  addrGe = sqlite3VdbeAddOp3(v, OP_Ge, regString, 0, reg1);
146677 147244     VdbeCoverage(v);
146678         -  VdbeComment((v, "Flush_partition subroutine"));
146679         -  sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr);
146680         -  sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr);
146681         -
146682         -  /* If either regStart or regEnd are not non-negative integers, throw 
146683         -  ** an exception.  */
146684         -  if( pMWin->pStart ){
146685         -    sqlite3ExprCode(pParse, pMWin->pStart, regStart);
146686         -    windowCheckIntValue(pParse, regStart, 0);
146687         -  }
146688         -  if( pMWin->pEnd ){
146689         -    sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
146690         -    windowCheckIntValue(pParse, regEnd, 1);
146691         -  }
146692         -
146693         -  /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
146694         -  **
146695         -  **   if( regEnd<regStart ){
146696         -  **     // The frame always consists of 0 rows
146697         -  **     regStart = regSize;
146698         -  **   }
146699         -  **   regEnd = regEnd - regStart;
146700         -  */
146701         -  if( pMWin->pEnd && pMWin->eStart==TK_FOLLOWING ){
146702         -    assert( pMWin->pStart!=0 );
146703         -    assert( pMWin->eEnd==TK_FOLLOWING );
146704         -    sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd);
146705         -    VdbeCoverageNeverNull(v);
146706         -    sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
146707         -    sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd);
146708         -  }
146709         -
146710         -  if( pMWin->pStart && pMWin->eEnd==TK_PRECEDING ){
146711         -    assert( pMWin->pEnd!=0 );
146712         -    assert( pMWin->eStart==TK_PRECEDING );
146713         -    sqlite3VdbeAddOp3(v, OP_Le, regStart, sqlite3VdbeCurrentAddr(v)+3, regEnd);
146714         -    VdbeCoverageNeverNull(v);
146715         -    sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
146716         -    sqlite3VdbeAddOp2(v, OP_Copy, regSize, regEnd);
146717         -  }
146718         -
146719         -  /* Initialize the accumulator register for each window function to NULL */
146720         -  regArg = windowInitAccum(pParse, pMWin);
146721         -
146722         -  sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone);
146723         -  VdbeCoverage(v);
146724         -  sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone);
146725         -  VdbeCoverageNeverTaken(v);
146726         -  sqlite3VdbeChangeP5(v, 1);
146727         -  sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone);
146728         -  VdbeCoverageNeverTaken(v);
146729         -  sqlite3VdbeChangeP5(v, 1);
146730         -
146731         -  /* Invoke AggStep function for each window function using the row that
146732         -  ** csrEnd currently points to. Or, if csrEnd is already at EOF,
146733         -  ** do nothing.  */
146734         -  addrTop = sqlite3VdbeCurrentAddr(v);
146735         -  if( pMWin->eEnd==TK_PRECEDING ){
146736         -    addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
146737         -    VdbeCoverage(v);
146738         -  }
146739         -  sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
146740         -  VdbeCoverage(v);
146741         -  addr = sqlite3VdbeAddOp0(v, OP_Goto);
146742         -  windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
146743         -  if( pMWin->eEnd==TK_UNBOUNDED ){
146744         -    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
146745         -    sqlite3VdbeJumpHere(v, addr);
146746         -    addrTop = sqlite3VdbeCurrentAddr(v);
146747         -  }else{
146748         -    sqlite3VdbeJumpHere(v, addr);
146749         -    if( pMWin->eEnd==TK_PRECEDING ){
146750         -      sqlite3VdbeJumpHere(v, addrIfPos1);
146751         -    }
146752         -  }
146753         -
146754         -  if( pMWin->eEnd==TK_FOLLOWING ){
146755         -    addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
146756         -    VdbeCoverage(v);
146757         -  }
146758         -  if( pMWin->eStart==TK_FOLLOWING ){
146759         -    addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
146760         -    VdbeCoverage(v);
146761         -  }
146762         -  windowAggFinal(pParse, pMWin, 0);
146763         -  windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
146764         -  sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2);
146765         -  VdbeCoverage(v);
146766         -  sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone);
146767         -  if( pMWin->eStart==TK_FOLLOWING ){
146768         -    sqlite3VdbeJumpHere(v, addrIfPos2);
146769         -  }
146770         -
146771         -  if( pMWin->eStart==TK_CURRENT 
146772         -   || pMWin->eStart==TK_PRECEDING 
146773         -   || pMWin->eStart==TK_FOLLOWING 
146774         -  ){
146775         -    int lblSkipInverse = sqlite3VdbeMakeLabel(pParse);;
146776         -    if( pMWin->eStart==TK_PRECEDING ){
146777         -      sqlite3VdbeAddOp3(v, OP_IfPos, regStart, lblSkipInverse, 1);
       147245  +  sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1);
       147246  +  sqlite3VdbeJumpHere(v, addrGe);
       147247  +  sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
       147248  +  sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
       147249  +  assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le );
       147250  +  testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
       147251  +  testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
       147252  +  testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
       147253  +  testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
       147254  +
       147255  +  sqlite3ReleaseTempReg(pParse, reg1);
       147256  +  sqlite3ReleaseTempReg(pParse, reg2);
       147257  +}
       147258  +
       147259  +/*
       147260  +** Helper function for sqlite3WindowCodeStep(). Each call to this function
       147261  +** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE 
       147262  +** operation. Refer to the header comment for sqlite3WindowCodeStep() for
       147263  +** details.
       147264  +*/
       147265  +static int windowCodeOp(
       147266  + WindowCodeArg *p,                /* Context object */
       147267  + int op,                          /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
       147268  + int regCountdown,                /* Register for OP_IfPos countdown */
       147269  + int jumpOnEof                    /* Jump here if stepped cursor reaches EOF */
       147270  +){
       147271  +  int csr, reg;
       147272  +  Parse *pParse = p->pParse;
       147273  +  Window *pMWin = p->pMWin;
       147274  +  int ret = 0;
       147275  +  Vdbe *v = p->pVdbe;
       147276  +  int addrIf = 0; 
       147277  +  int addrContinue = 0;
       147278  +  int addrGoto = 0;
       147279  +  int bPeer = (pMWin->eFrmType!=TK_ROWS);
       147280  +
       147281  +  int lblDone = sqlite3VdbeMakeLabel(pParse);
       147282  +  int addrNextRange = 0;
       147283  +
       147284  +  /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
       147285  +  ** starts with UNBOUNDED PRECEDING. */
       147286  +  if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
       147287  +    assert( regCountdown==0 && jumpOnEof==0 );
       147288  +    return 0;
       147289  +  }
       147290  +
       147291  +  if( regCountdown>0 ){
       147292  +    if( pMWin->eFrmType==TK_RANGE ){
       147293  +      addrNextRange = sqlite3VdbeCurrentAddr(v);
       147294  +      assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP );
       147295  +      if( op==WINDOW_AGGINVERSE ){
       147296  +        if( pMWin->eStart==TK_FOLLOWING ){
       147297  +          windowCodeRangeTest(
       147298  +              p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
       147299  +          );
       147300  +        }else{
       147301  +          windowCodeRangeTest(
       147302  +              p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
       147303  +          );
       147304  +        }
       147305  +      }else{
       147306  +        windowCodeRangeTest(
       147307  +            p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
       147308  +        );
       147309  +      }
       147310  +    }else{
       147311  +      addrIf = sqlite3VdbeAddOp3(v, OP_IfPos, regCountdown, 0, 1);
146778 147312         VdbeCoverage(v);
146779 147313       }
146780         -    if( pMWin->eStart==TK_FOLLOWING ){
146781         -      sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+2);
146782         -      VdbeCoverage(v);
146783         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, lblSkipInverse);
146784         -    }else{
146785         -      sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
146786         -      VdbeCoverageAlwaysTaken(v);
146787         -    }
146788         -    windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
146789         -    sqlite3VdbeResolveLabel(v, lblSkipInverse);
146790         -  }
146791         -  if( pMWin->eEnd==TK_FOLLOWING ){
146792         -    sqlite3VdbeJumpHere(v, addrIfPos1);
146793         -  }
146794         -  sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
146795         -
146796         -  /* flush_partition_done: */
146797         -  sqlite3VdbeResolveLabel(v, lblFlushDone);
146798         -  sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
146799         -  sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
146800         -  VdbeComment((v, "end flush_partition subroutine"));
146801         -
146802         -  /* Jump to here to skip over flush_partition */
146803         -  sqlite3VdbeJumpHere(v, addrGoto);
146804         -}
146805         -
146806         -/*
146807         -** This function does the work of sqlite3WindowCodeStep() for cases that
146808         -** would normally be handled by windowCodeDefaultStep() when there are
146809         -** one or more built-in window-functions that require the entire partition
146810         -** to be cached in a temp table before any rows can be returned. Additionally.
146811         -** "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is always handled by
146812         -** this function.
146813         -**
146814         -** Pseudo-code corresponding to the VM code generated by this function
146815         -** for each type of window follows.
146816         -**
146817         -** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146818         -**
146819         -**   flush_partition:
146820         -**     Once {
146821         -**       OpenDup (iEphCsr -> csrLead)
146822         -**     }
146823         -**     Integer ctr 0
146824         -**     foreach row (csrLead){
146825         -**       if( new peer ){
146826         -**         AggFinal (xValue)
146827         -**         for(i=0; i<ctr; i++){
146828         -**           Gosub addrGosub
146829         -**           Next iEphCsr
146830         -**         }
146831         -**         Integer ctr 0
146832         -**       }
146833         -**       AggStep (csrLead)
146834         -**       Incr ctr
146835         -**     }
146836         -**
146837         -**     AggFinal (xFinalize)
146838         -**     for(i=0; i<ctr; i++){
146839         -**       Gosub addrGosub
146840         -**       Next iEphCsr
146841         -**     }
146842         -**
146843         -**     ResetSorter (csr)
146844         -**     Return
146845         -**
146846         -** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146847         -**
146848         -**   As above, except that the "if( new peer )" branch is always taken.
146849         -**
146850         -** RANGE BETWEEN CURRENT ROW AND CURRENT ROW 
146851         -**
146852         -**   As above, except that each of the for() loops becomes:
146853         -**
146854         -**         for(i=0; i<ctr; i++){
146855         -**           Gosub addrGosub
146856         -**           AggInverse (iEphCsr)
146857         -**           Next iEphCsr
146858         -**         }
146859         -**
146860         -** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
146861         -**
146862         -**   flush_partition:
146863         -**     Once {
146864         -**       OpenDup (iEphCsr -> csrLead)
146865         -**     }
146866         -**     foreach row (csrLead) {
146867         -**       AggStep (csrLead)
146868         -**     }
146869         -**     foreach row (iEphCsr) {
146870         -**       Gosub addrGosub
146871         -**     }
146872         -** 
146873         -** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146874         -**
146875         -**   flush_partition:
146876         -**     Once {
146877         -**       OpenDup (iEphCsr -> csrLead)
146878         -**     }
146879         -**     foreach row (csrLead){
146880         -**       AggStep (csrLead)
146881         -**     }
146882         -**     Rewind (csrLead)
146883         -**     Integer ctr 0
146884         -**     foreach row (csrLead){
146885         -**       if( new peer ){
146886         -**         AggFinal (xValue)
146887         -**         for(i=0; i<ctr; i++){
146888         -**           Gosub addrGosub
146889         -**           AggInverse (iEphCsr)
146890         -**           Next iEphCsr
146891         -**         }
146892         -**         Integer ctr 0
146893         -**       }
146894         -**       Incr ctr
146895         -**     }
146896         -**
146897         -**     AggFinal (xFinalize)
146898         -**     for(i=0; i<ctr; i++){
146899         -**       Gosub addrGosub
146900         -**       Next iEphCsr
146901         -**     }
146902         -**
146903         -**     ResetSorter (csr)
146904         -**     Return
146905         -*/
146906         -static void windowCodeCacheStep(
146907         -  Parse *pParse, 
146908         -  Select *p,
146909         -  WhereInfo *pWInfo,
146910         -  int regGosub, 
146911         -  int addrGosub
146912         -){
146913         -  Window *pMWin = p->pWin;
146914         -  Vdbe *v = sqlite3GetVdbe(pParse);
146915         -  int k;
146916         -  int addr;
146917         -  ExprList *pPart = pMWin->pPartition;
146918         -  ExprList *pOrderBy = pMWin->pOrderBy;
146919         -  int nPeer = pOrderBy ? pOrderBy->nExpr : 0;
146920         -  int regNewPeer;
146921         -
146922         -  int addrGoto;                   /* Address of Goto used to jump flush_par.. */
146923         -  int addrNext;                   /* Jump here for next iteration of loop */
146924         -  int regFlushPart;
146925         -  int lblFlushPart;
146926         -  int csrLead;
146927         -  int regCtr;
146928         -  int regArg;                     /* Register array to martial function args */
146929         -  int regSize;
146930         -  int lblEmpty;
146931         -  int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT 
146932         -          && pMWin->eEnd==TK_UNBOUNDED;
146933         -
146934         -  assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT) 
146935         -       || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED) 
146936         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT) 
146937         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED) 
146938         -  );
146939         -
146940         -  lblEmpty = sqlite3VdbeMakeLabel(pParse);
146941         -  regNewPeer = pParse->nMem+1;
146942         -  pParse->nMem += nPeer;
146943         -
146944         -  /* Allocate register and label for the "flush_partition" sub-routine. */
146945         -  regFlushPart = ++pParse->nMem;
146946         -  lblFlushPart = sqlite3VdbeMakeLabel(pParse);
146947         -
146948         -  csrLead = pParse->nTab++;
146949         -  regCtr = ++pParse->nMem;
146950         -
146951         -  windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
146952         -  addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
146953         -
146954         -  /* Start of "flush_partition" */
146955         -  sqlite3VdbeResolveLabel(v, lblFlushPart);
146956         -  sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2);
146957         -  VdbeCoverage(v);
146958         -  sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr);
146959         -
146960         -  /* Initialize the accumulator register for each window function to NULL */
146961         -  regArg = windowInitAccum(pParse, pMWin);
146962         -
146963         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr);
146964         -  sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
146965         -  VdbeCoverage(v);
146966         -  sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty);
146967         -  VdbeCoverageNeverTaken(v);
146968         -
146969         -  if( bReverse ){
146970         -    int addr2 = sqlite3VdbeCurrentAddr(v);
146971         -    windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
146972         -    sqlite3VdbeAddOp2(v, OP_Next, csrLead, addr2);
       147314  +  }
       147315  +
       147316  +  if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
       147317  +    windowAggFinal(p, 0);
       147318  +  }
       147319  +  addrContinue = sqlite3VdbeCurrentAddr(v);
       147320  +  switch( op ){
       147321  +    case WINDOW_RETURN_ROW:
       147322  +      csr = p->current.csr;
       147323  +      reg = p->current.reg;
       147324  +      windowReturnOneRow(p);
       147325  +      break;
       147326  +
       147327  +    case WINDOW_AGGINVERSE:
       147328  +      csr = p->start.csr;
       147329  +      reg = p->start.reg;
       147330  +      if( pMWin->regStartRowid ){
       147331  +        assert( pMWin->regEndRowid );
       147332  +        sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
       147333  +      }else{
       147334  +        windowAggStep(pParse, pMWin, csr, 1, p->regArg);
       147335  +      }
       147336  +      break;
       147337  +
       147338  +    default:
       147339  +      assert( op==WINDOW_AGGSTEP );
       147340  +      csr = p->end.csr;
       147341  +      reg = p->end.reg;
       147342  +      if( pMWin->regStartRowid ){
       147343  +        assert( pMWin->regEndRowid );
       147344  +        sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
       147345  +      }else{
       147346  +        windowAggStep(pParse, pMWin, csr, 0, p->regArg);
       147347  +      }
       147348  +      break;
       147349  +  }
       147350  +
       147351  +  if( op==p->eDelete ){
       147352  +    sqlite3VdbeAddOp1(v, OP_Delete, csr);
       147353  +    sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
       147354  +  }
       147355  +
       147356  +  if( jumpOnEof ){
       147357  +    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+2);
       147358  +    VdbeCoverage(v);
       147359  +    ret = sqlite3VdbeAddOp0(v, OP_Goto);
       147360  +  }else{
       147361  +    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer);
146973 147362       VdbeCoverage(v);
146974         -    sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
146975         -    VdbeCoverageNeverTaken(v);
146976         -  }
146977         -  addrNext = sqlite3VdbeCurrentAddr(v);
146978         -
146979         -  if( pOrderBy && (pMWin->eEnd==TK_CURRENT || pMWin->eStart==TK_CURRENT) ){
146980         -    int bCurrent = (pMWin->eStart==TK_CURRENT);
146981         -    int addrJump = 0;             /* Address of OP_Jump below */
146982         -    if( pMWin->eType==TK_RANGE ){
146983         -      int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
146984         -      int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0);
146985         -      KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
146986         -      for(k=0; k<nPeer; k++){
146987         -        sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k);
146988         -      }
146989         -      addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
146990         -      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
146991         -      addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
146992         -      VdbeCoverage(v);
146993         -      sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1);
146994         -    }
146995         -
146996         -    windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 
146997         -        (bCurrent ? regArg : 0), (bCurrent ? regSize : 0)
146998         -    );
146999         -    if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147000         -  }
147001         -
147002         -  if( bReverse==0 ){
147003         -    windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
147004         -  }
147005         -  sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
147006         -  sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext);
147007         -  VdbeCoverage(v);
147008         -
147009         -  windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0);
147010         -
147011         -  sqlite3VdbeResolveLabel(v, lblEmpty);
147012         -  sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
147013         -  sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
147014         -
147015         -  /* Jump to here to skip over flush_partition */
147016         -  sqlite3VdbeJumpHere(v, addrGoto);
147017         -}
147018         -
147019         -
147020         -/*
147021         -** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147022         -**
147023         -**   ...
147024         -**     if( new partition ){
147025         -**       AggFinal (xFinalize)
147026         -**       Gosub addrGosub
147027         -**       ResetSorter eph-table
147028         -**     }
147029         -**     else if( new peer ){
147030         -**       AggFinal (xValue)
147031         -**       Gosub addrGosub
147032         -**       ResetSorter eph-table
147033         -**     }
147034         -**     AggStep
147035         -**     Insert (record into eph-table)
147036         -**   sqlite3WhereEnd()
147037         -**   AggFinal (xFinalize)
147038         -**   Gosub addrGosub
147039         -**
147040         -** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
147041         -**
147042         -**   As above, except take no action for a "new peer". Invoke
147043         -**   the sub-routine once only for each partition.
147044         -**
147045         -** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
147046         -**
147047         -**   As above, except that the "new peer" condition is handled in the
147048         -**   same way as "new partition" (so there is no "else if" block).
147049         -**
147050         -** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147051         -** 
147052         -**   As above, except assume every row is a "new peer".
147053         -*/
147054         -static void windowCodeDefaultStep(
147055         -  Parse *pParse, 
147056         -  Select *p,
147057         -  WhereInfo *pWInfo,
147058         -  int regGosub, 
147059         -  int addrGosub
147060         -){
147061         -  Window *pMWin = p->pWin;
147062         -  Vdbe *v = sqlite3GetVdbe(pParse);
147063         -  int k;
147064         -  int iSubCsr = p->pSrc->a[0].iCursor;
147065         -  int nSub = p->pSrc->a[0].pTab->nCol;
147066         -  int reg = pParse->nMem+1;
147067         -  int regRecord = reg+nSub;
147068         -  int regRowid = regRecord+1;
147069         -  int addr;
147070         -  ExprList *pPart = pMWin->pPartition;
147071         -  ExprList *pOrderBy = pMWin->pOrderBy;
147072         -
147073         -  assert( pMWin->eType==TK_RANGE 
147074         -      || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
147075         -  );
147076         -
147077         -  assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
147078         -       || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
147079         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
147080         -       || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy)
147081         -  );
147082         -
147083         -  if( pMWin->eEnd==TK_UNBOUNDED ){
147084         -    pOrderBy = 0;
147085         -  }
147086         -
147087         -  pParse->nMem += nSub + 2;
147088         -
147089         -  /* Load the individual column values of the row returned by
147090         -  ** the sub-select into an array of registers. */
147091         -  for(k=0; k<nSub; k++){
147092         -    sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
147093         -  }
147094         -
147095         -  /* Check if this is the start of a new partition or peer group. */
147096         -  if( pPart || pOrderBy ){
147097         -    int nPart = (pPart ? pPart->nExpr : 0);
147098         -    int addrGoto = 0;
147099         -    int addrJump = 0;
147100         -    int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
147101         -
147102         -    if( pPart ){
147103         -      int regNewPart = reg + pMWin->nBufferCol;
147104         -      KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
147105         -      addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
147106         -      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
147107         -      addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
147108         -      VdbeCoverageEqNe(v);
147109         -      windowAggFinal(pParse, pMWin, 1);
147110         -      if( pOrderBy ){
147111         -        addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
147112         -      }
147113         -    }
147114         -
147115         -    if( pOrderBy ){
147116         -      int regNewPeer = reg + pMWin->nBufferCol + nPart;
147117         -      int regPeer = pMWin->regPart + nPart;
147118         -
147119         -      if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147120         -      if( pMWin->eType==TK_RANGE ){
147121         -        KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
147122         -        addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
147123         -        sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
147124         -        addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
147125         -        VdbeCoverage(v);
147126         -      }else{
147127         -        addrJump = 0;
147128         -      }
147129         -      windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT);
147130         -      if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
147131         -    }
147132         -
147133         -    sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
147134         -    VdbeCoverage(v);
147135         -    sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
147136         -    sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
147137         -    VdbeCoverage(v);
147138         -
147139         -    sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
147140         -    sqlite3VdbeAddOp3(
147141         -        v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1
147142         -    );
147143         -
147144         -    if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
147145         -  }
147146         -
147147         -  /* Invoke step function for window functions */
147148         -  windowAggStep(pParse, pMWin, -1, 0, reg, 0);
147149         -
147150         -  /* Buffer the current row in the ephemeral table. */
147151         -  if( pMWin->nBufferCol>0 ){
147152         -    sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord);
147153         -  }else{
147154         -    sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord);
147155         -    sqlite3VdbeAppendP4(v, (void*)"", 0);
147156         -  }
147157         -  sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
147158         -  sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
147159         -
147160         -  /* End the database scan loop. */
147161         -  sqlite3WhereEnd(pWInfo);
147162         -
147163         -  windowAggFinal(pParse, pMWin, 1);
147164         -  sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
147165         -  VdbeCoverage(v);
147166         -  sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
147167         -  sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
147168         -  VdbeCoverage(v);
147169         -}
       147363  +    if( bPeer ){
       147364  +      addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
       147365  +    }
       147366  +  }
       147367  +
       147368  +  if( bPeer ){
       147369  +    int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
       147370  +    int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
       147371  +    windowReadPeerValues(p, csr, regTmp);
       147372  +    windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
       147373  +    sqlite3ReleaseTempRange(pParse, regTmp, nReg);
       147374  +  }
       147375  +
       147376  +  if( addrNextRange ){
       147377  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNextRange);
       147378  +  }
       147379  +  sqlite3VdbeResolveLabel(v, lblDone);
       147380  +  if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
       147381  +  if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
       147382  +  return ret;
       147383  +}
       147384  +
147170 147385   
147171 147386   /*
147172 147387   ** Allocate and return a duplicate of the Window object indicated by the
147173 147388   ** third argument. Set the Window.pOwner field of the new object to
147174 147389   ** pOwner.
147175 147390   */
147176 147391   SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
................................................................................
147179 147394       pNew = sqlite3DbMallocZero(db, sizeof(Window));
147180 147395       if( pNew ){
147181 147396         pNew->zName = sqlite3DbStrDup(db, p->zName);
147182 147397         pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
147183 147398         pNew->pFunc = p->pFunc;
147184 147399         pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
147185 147400         pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
147186         -      pNew->eType = p->eType;
       147401  +      pNew->eFrmType = p->eFrmType;
147187 147402         pNew->eEnd = p->eEnd;
147188 147403         pNew->eStart = p->eStart;
       147404  +      pNew->eExclude = p->eExclude;
147189 147405         pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
147190 147406         pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
147191 147407         pNew->pOwner = pOwner;
147192 147408       }
147193 147409     }
147194 147410     return pNew;
147195 147411   }
................................................................................
147207 147423       *pp = sqlite3WindowDup(db, 0, pWin);
147208 147424       if( *pp==0 ) break;
147209 147425       pp = &((*pp)->pNextWin);
147210 147426     }
147211 147427   
147212 147428     return pRet;
147213 147429   }
       147430  +
       147431  +/*
       147432  +** Return true if it can be determined at compile time that expression 
       147433  +** pExpr evaluates to a value that, when cast to an integer, is greater 
       147434  +** than zero. False otherwise.
       147435  +**
       147436  +** If an OOM error occurs, this function sets the Parse.db.mallocFailed 
       147437  +** flag and returns zero.
       147438  +*/
       147439  +static int windowExprGtZero(Parse *pParse, Expr *pExpr){
       147440  +  int ret = 0;
       147441  +  sqlite3 *db = pParse->db;
       147442  +  sqlite3_value *pVal = 0;
       147443  +  sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
       147444  +  if( pVal && sqlite3_value_int(pVal)>0 ){
       147445  +    ret = 1;
       147446  +  }
       147447  +  sqlite3ValueFree(pVal);
       147448  +  return ret;
       147449  +}
147214 147450   
147215 147451   /*
147216 147452   ** sqlite3WhereBegin() has already been called for the SELECT statement 
147217 147453   ** passed as the second argument when this function is invoked. It generates
147218         -** code to populate the Window.regResult register for each window function and
147219         -** invoke the sub-routine at instruction addrGosub once for each row.
147220         -** This function calls sqlite3WhereEnd() before returning. 
       147454  +** code to populate the Window.regResult register for each window function 
       147455  +** and invoke the sub-routine at instruction addrGosub once for each row.
       147456  +** sqlite3WhereEnd() is always called before returning. 
       147457  +**
       147458  +** This function handles several different types of window frames, which
       147459  +** require slightly different processing. The following pseudo code is
       147460  +** used to implement window frames of the form:
       147461  +**
       147462  +**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
       147463  +**
       147464  +** Other window frame types use variants of the following:
       147465  +**
       147466  +**     ... loop started by sqlite3WhereBegin() ...
       147467  +**       if( new partition ){
       147468  +**         Gosub flush
       147469  +**       }
       147470  +**       Insert new row into eph table.
       147471  +**       
       147472  +**       if( first row of partition ){
       147473  +**         // Rewind three cursors, all open on the eph table.
       147474  +**         Rewind(csrEnd);
       147475  +**         Rewind(csrStart);
       147476  +**         Rewind(csrCurrent);
       147477  +**       
       147478  +**         regEnd = <expr2>          // FOLLOWING expression
       147479  +**         regStart = <expr1>        // PRECEDING expression
       147480  +**       }else{
       147481  +**         // First time this branch is taken, the eph table contains two 
       147482  +**         // rows. The first row in the partition, which all three cursors
       147483  +**         // currently point to, and the following row.
       147484  +**         AGGSTEP
       147485  +**         if( (regEnd--)<=0 ){
       147486  +**           RETURN_ROW
       147487  +**           if( (regStart--)<=0 ){
       147488  +**             AGGINVERSE
       147489  +**           }
       147490  +**         }
       147491  +**       }
       147492  +**     }
       147493  +**     flush:
       147494  +**       AGGSTEP
       147495  +**       while( 1 ){
       147496  +**         RETURN ROW
       147497  +**         if( csrCurrent is EOF ) break;
       147498  +**         if( (regStart--)<=0 ){
       147499  +**           AggInverse(csrStart)
       147500  +**           Next(csrStart)
       147501  +**         }
       147502  +**       }
       147503  +**
       147504  +** The pseudo-code above uses the following shorthand:
       147505  +**
       147506  +**   AGGSTEP:    invoke the aggregate xStep() function for each window function
       147507  +**               with arguments read from the current row of cursor csrEnd, then
       147508  +**               step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
       147509  +**
       147510  +**   RETURN_ROW: return a row to the caller based on the contents of the 
       147511  +**               current row of csrCurrent and the current state of all 
       147512  +**               aggregates. Then step cursor csrCurrent forward one row.
       147513  +**
       147514  +**   AGGINVERSE: invoke the aggregate xInverse() function for each window 
       147515  +**               functions with arguments read from the current row of cursor
       147516  +**               csrStart. Then step csrStart forward one row.
       147517  +**
       147518  +** There are two other ROWS window frames that are handled significantly
       147519  +** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
       147520  +** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special 
       147521  +** cases because they change the order in which the three cursors (csrStart,
       147522  +** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
       147523  +** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
       147524  +** three.
       147525  +**
       147526  +**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
       147527  +**
       147528  +**     ... loop started by sqlite3WhereBegin() ...
       147529  +**       if( new partition ){
       147530  +**         Gosub flush
       147531  +**       }
       147532  +**       Insert new row into eph table.
       147533  +**       if( first row of partition ){
       147534  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147535  +**         regEnd = <expr2>
       147536  +**         regStart = <expr1>
       147537  +**       }else{
       147538  +**         if( (regEnd--)<=0 ){
       147539  +**           AGGSTEP
       147540  +**         }
       147541  +**         RETURN_ROW
       147542  +**         if( (regStart--)<=0 ){
       147543  +**           AGGINVERSE
       147544  +**         }
       147545  +**       }
       147546  +**     }
       147547  +**     flush:
       147548  +**       if( (regEnd--)<=0 ){
       147549  +**         AGGSTEP
       147550  +**       }
       147551  +**       RETURN_ROW
       147552  +**
       147553  +**
       147554  +**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
       147555  +**
       147556  +**     ... loop started by sqlite3WhereBegin() ...
       147557  +**     if( new partition ){
       147558  +**       Gosub flush
       147559  +**     }
       147560  +**     Insert new row into eph table.
       147561  +**     if( first row of partition ){
       147562  +**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147563  +**       regEnd = <expr2>
       147564  +**       regStart = regEnd - <expr1>
       147565  +**     }else{
       147566  +**       AGGSTEP
       147567  +**       if( (regEnd--)<=0 ){
       147568  +**         RETURN_ROW
       147569  +**       }
       147570  +**       if( (regStart--)<=0 ){
       147571  +**         AGGINVERSE
       147572  +**       }
       147573  +**     }
       147574  +**   }
       147575  +**   flush:
       147576  +**     AGGSTEP
       147577  +**     while( 1 ){
       147578  +**       if( (regEnd--)<=0 ){
       147579  +**         RETURN_ROW
       147580  +**         if( eof ) break;
       147581  +**       }
       147582  +**       if( (regStart--)<=0 ){
       147583  +**         AGGINVERSE
       147584  +**         if( eof ) break
       147585  +**       }
       147586  +**     }
       147587  +**     while( !eof csrCurrent ){
       147588  +**       RETURN_ROW
       147589  +**     }
       147590  +**
       147591  +** For the most part, the patterns above are adapted to support UNBOUNDED by
       147592  +** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
       147593  +** CURRENT ROW by assuming that it is equivilent to "0 PRECEDING/FOLLOWING".
       147594  +** This is optimized of course - branches that will never be taken and
       147595  +** conditions that are always true are omitted from the VM code. The only
       147596  +** exceptional case is:
       147597  +**
       147598  +**   ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
       147599  +**
       147600  +**     ... loop started by sqlite3WhereBegin() ...
       147601  +**     if( new partition ){
       147602  +**       Gosub flush
       147603  +**     }
       147604  +**     Insert new row into eph table.
       147605  +**     if( first row of partition ){
       147606  +**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147607  +**       regStart = <expr1>
       147608  +**     }else{
       147609  +**       AGGSTEP
       147610  +**     }
       147611  +**   }
       147612  +**   flush:
       147613  +**     AGGSTEP
       147614  +**     while( 1 ){
       147615  +**       if( (regStart--)<=0 ){
       147616  +**         AGGINVERSE
       147617  +**         if( eof ) break
       147618  +**       }
       147619  +**       RETURN_ROW
       147620  +**     }
       147621  +**     while( !eof csrCurrent ){
       147622  +**       RETURN_ROW
       147623  +**     }
       147624  +**
       147625  +** Also requiring special handling are the cases:
       147626  +**
       147627  +**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
       147628  +**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
       147629  +**
       147630  +** when (expr1 < expr2). This is detected at runtime, not by this function.
       147631  +** To handle this case, the pseudo-code programs depicted above are modified
       147632  +** slightly to be:
       147633  +**
       147634  +**     ... loop started by sqlite3WhereBegin() ...
       147635  +**     if( new partition ){
       147636  +**       Gosub flush
       147637  +**     }
       147638  +**     Insert new row into eph table.
       147639  +**     if( first row of partition ){
       147640  +**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147641  +**       regEnd = <expr2>
       147642  +**       regStart = <expr1>
       147643  +**       if( regEnd < regStart ){
       147644  +**         RETURN_ROW
       147645  +**         delete eph table contents
       147646  +**         continue
       147647  +**       }
       147648  +**     ...
       147649  +**
       147650  +** The new "continue" statement in the above jumps to the next iteration
       147651  +** of the outer loop - the one started by sqlite3WhereBegin().
       147652  +**
       147653  +** The various GROUPS cases are implemented using the same patterns as
       147654  +** ROWS. The VM code is modified slightly so that:
       147655  +**
       147656  +**   1. The else branch in the main loop is only taken if the row just
       147657  +**      added to the ephemeral table is the start of a new group. In
       147658  +**      other words, it becomes:
       147659  +**
       147660  +**         ... loop started by sqlite3WhereBegin() ...
       147661  +**         if( new partition ){
       147662  +**           Gosub flush
       147663  +**         }
       147664  +**         Insert new row into eph table.
       147665  +**         if( first row of partition ){
       147666  +**           Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147667  +**           regEnd = <expr2>
       147668  +**           regStart = <expr1>
       147669  +**         }else if( new group ){
       147670  +**           ... 
       147671  +**         }
       147672  +**       }
       147673  +**
       147674  +**   2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or 
       147675  +**      AGGINVERSE step processes the current row of the relevant cursor and
       147676  +**      all subsequent rows belonging to the same group.
       147677  +**
       147678  +** RANGE window frames are a little different again. As for GROUPS, the 
       147679  +** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
       147680  +** deal in groups instead of rows. As for ROWS and GROUPS, there are three
       147681  +** basic cases:
       147682  +**
       147683  +**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
       147684  +**
       147685  +**     ... loop started by sqlite3WhereBegin() ...
       147686  +**       if( new partition ){
       147687  +**         Gosub flush
       147688  +**       }
       147689  +**       Insert new row into eph table.
       147690  +**       if( first row of partition ){
       147691  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147692  +**         regEnd = <expr2>
       147693  +**         regStart = <expr1>
       147694  +**       }else{
       147695  +**         AGGSTEP
       147696  +**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
       147697  +**           RETURN_ROW
       147698  +**           while( csrStart.key + regStart) < csrCurrent.key ){
       147699  +**             AGGINVERSE
       147700  +**           }
       147701  +**         }
       147702  +**       }
       147703  +**     }
       147704  +**     flush:
       147705  +**       AGGSTEP
       147706  +**       while( 1 ){
       147707  +**         RETURN ROW
       147708  +**         if( csrCurrent is EOF ) break;
       147709  +**           while( csrStart.key + regStart) < csrCurrent.key ){
       147710  +**             AGGINVERSE
       147711  +**           }
       147712  +**         }
       147713  +**       }
       147714  +**
       147715  +** In the above notation, "csr.key" means the current value of the ORDER BY 
       147716  +** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
       147717  +** or <expr PRECEDING) read from cursor csr.
       147718  +**
       147719  +**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
       147720  +**
       147721  +**     ... loop started by sqlite3WhereBegin() ...
       147722  +**       if( new partition ){
       147723  +**         Gosub flush
       147724  +**       }
       147725  +**       Insert new row into eph table.
       147726  +**       if( first row of partition ){
       147727  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147728  +**         regEnd = <expr2>
       147729  +**         regStart = <expr1>
       147730  +**       }else{
       147731  +**         if( (csrEnd.key + regEnd) <= csrCurrent.key ){
       147732  +**           AGGSTEP
       147733  +**         }
       147734  +**         while( (csrStart.key + regStart) < csrCurrent.key ){
       147735  +**           AGGINVERSE
       147736  +**         }
       147737  +**         RETURN_ROW
       147738  +**       }
       147739  +**     }
       147740  +**     flush:
       147741  +**       while( (csrEnd.key + regEnd) <= csrCurrent.key ){
       147742  +**         AGGSTEP
       147743  +**       }
       147744  +**       while( (csrStart.key + regStart) < csrCurrent.key ){
       147745  +**         AGGINVERSE
       147746  +**       }
       147747  +**       RETURN_ROW
       147748  +**
       147749  +**   RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
       147750  +**
       147751  +**     ... loop started by sqlite3WhereBegin() ...
       147752  +**       if( new partition ){
       147753  +**         Gosub flush
       147754  +**       }
       147755  +**       Insert new row into eph table.
       147756  +**       if( first row of partition ){
       147757  +**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
       147758  +**         regEnd = <expr2>
       147759  +**         regStart = <expr1>
       147760  +**       }else{
       147761  +**         AGGSTEP
       147762  +**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
       147763  +**           while( (csrCurrent.key + regStart) > csrStart.key ){
       147764  +**             AGGINVERSE
       147765  +**           }
       147766  +**           RETURN_ROW
       147767  +**         }
       147768  +**       }
       147769  +**     }
       147770  +**     flush:
       147771  +**       AGGSTEP
       147772  +**       while( 1 ){
       147773  +**         while( (csrCurrent.key + regStart) > csrStart.key ){
       147774  +**           AGGINVERSE
       147775  +**           if( eof ) break "while( 1 )" loop.
       147776  +**         }
       147777  +**         RETURN_ROW
       147778  +**       }
       147779  +**       while( !eof csrCurrent ){
       147780  +**         RETURN_ROW
       147781  +**       }
       147782  +**
       147783  +** The text above leaves out many details. Refer to the code and comments
       147784  +** below for a more complete picture.
147221 147785   */
147222 147786   SQLITE_PRIVATE void sqlite3WindowCodeStep(
147223 147787     Parse *pParse,                  /* Parse context */
147224 147788     Select *p,                      /* Rewritten SELECT statement */
147225 147789     WhereInfo *pWInfo,              /* Context returned by sqlite3WhereBegin() */
147226 147790     int regGosub,                   /* Register for OP_Gosub */
147227 147791     int addrGosub                   /* OP_Gosub here to return each row */
147228 147792   ){
147229 147793     Window *pMWin = p->pWin;
147230         -
147231         -  /* There are three different functions that may be used to do the work
147232         -  ** of this one, depending on the window frame and the specific built-in
147233         -  ** window functions used (if any).
147234         -  **
147235         -  ** windowCodeRowExprStep() handles all "ROWS" window frames, except for:
147236         -  **
147237         -  **   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147238         -  **
147239         -  ** The exception is because windowCodeRowExprStep() implements all window
147240         -  ** frame types by caching the entire partition in a temp table, and
147241         -  ** "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" is easy enough to
147242         -  ** implement without such a cache.
147243         -  **
147244         -  ** windowCodeCacheStep() is used for:
147245         -  **
147246         -  **   RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
147247         -  **
147248         -  ** It is also used for anything not handled by windowCodeRowExprStep() 
147249         -  ** that invokes a built-in window function that requires the entire 
147250         -  ** partition to be cached in a temp table before any rows are returned
147251         -  ** (e.g. nth_value() or percent_rank()).
147252         -  **
147253         -  ** Finally, assuming there is no built-in window function that requires
147254         -  ** the partition to be cached, windowCodeDefaultStep() is used for:
147255         -  **
147256         -  **   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
147257         -  **   RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
147258         -  **   RANGE BETWEEN CURRENT ROW AND CURRENT ROW 
147259         -  **   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
147260         -  **
147261         -  ** windowCodeDefaultStep() is the only one of the three functions that
147262         -  ** does not cache each partition in a temp table before beginning to
147263         -  ** return rows.
147264         -  */
147265         -  if( pMWin->eType==TK_ROWS 
147266         -   && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy)
147267         -  ){
147268         -    VdbeModuleComment((pParse->pVdbe, "Begin RowExprStep()"));
147269         -    windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub);
147270         -  }else{
147271         -    Window *pWin;
147272         -    int bCache = 0;               /* True to use CacheStep() */
147273         -
147274         -    if( pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED ){
147275         -      bCache = 1;
147276         -    }else{
147277         -      for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147278         -        FuncDef *pFunc = pWin->pFunc;
147279         -        if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
147280         -         || (pFunc->zName==nth_valueName)
147281         -         || (pFunc->zName==first_valueName)
147282         -         || (pFunc->zName==leadName)
147283         -         || (pFunc->zName==lagName)
147284         -        ){
147285         -          bCache = 1;
147286         -          break;
147287         -        }
147288         -      }
147289         -    }
147290         -
147291         -    /* Otherwise, call windowCodeDefaultStep().  */
147292         -    if( bCache ){
147293         -      VdbeModuleComment((pParse->pVdbe, "Begin CacheStep()"));
147294         -      windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
147295         -    }else{
147296         -      VdbeModuleComment((pParse->pVdbe, "Begin DefaultStep()"));
147297         -      windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub);
147298         -    }
       147794  +  ExprList *pOrderBy = pMWin->pOrderBy;
       147795  +  Vdbe *v = sqlite3GetVdbe(pParse);
       147796  +  int csrWrite;                   /* Cursor used to write to eph. table */
       147797  +  int csrInput = p->pSrc->a[0].iCursor;     /* Cursor of sub-select */
       147798  +  int nInput = p->pSrc->a[0].pTab->nCol;    /* Number of cols returned by sub */
       147799  +  int iInput;                               /* To iterate through sub cols */
       147800  +  int addrNe;                     /* Address of OP_Ne */
       147801  +  int addrGosubFlush = 0;         /* Address of OP_Gosub to flush: */
       147802  +  int addrInteger = 0;            /* Address of OP_Integer */
       147803  +  int addrEmpty;                  /* Address of OP_Rewind in flush: */
       147804  +  int regStart = 0;               /* Value of <expr> PRECEDING */
       147805  +  int regEnd = 0;                 /* Value of <expr> FOLLOWING */
       147806  +  int regNew;                     /* Array of registers holding new input row */
       147807  +  int regRecord;                  /* regNew array in record form */
       147808  +  int regRowid;                   /* Rowid for regRecord in eph table */
       147809  +  int regNewPeer = 0;             /* Peer values for new row (part of regNew) */
       147810  +  int regPeer = 0;                /* Peer values for current row */
       147811  +  int regFlushPart = 0;           /* Register for "Gosub flush_partition" */
       147812  +  WindowCodeArg s;                /* Context object for sub-routines */
       147813  +  int lblWhereEnd;                /* Label just before sqlite3WhereEnd() code */
       147814  +
       147815  +  assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT 
       147816  +       || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED 
       147817  +  );
       147818  +  assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT 
       147819  +       || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING 
       147820  +  );
       147821  +  assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
       147822  +       || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
       147823  +       || pMWin->eExclude==TK_NO
       147824  +  );
       147825  +
       147826  +  lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
       147827  +
       147828  +  /* Fill in the context object */
       147829  +  memset(&s, 0, sizeof(WindowCodeArg));
       147830  +  s.pParse = pParse;
       147831  +  s.pMWin = pMWin;
       147832  +  s.pVdbe = v;
       147833  +  s.regGosub = regGosub;
       147834  +  s.addrGosub = addrGosub;
       147835  +  s.current.csr = pMWin->iEphCsr;
       147836  +  csrWrite = s.current.csr+1;
       147837  +  s.start.csr = s.current.csr+2;
       147838  +  s.end.csr = s.current.csr+3;
       147839  +
       147840  +  /* Figure out when rows may be deleted from the ephemeral table. There
       147841  +  ** are four options - they may never be deleted (eDelete==0), they may 
       147842  +  ** be deleted as soon as they are no longer part of the window frame
       147843  +  ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row 
       147844  +  ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
       147845  +  ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
       147846  +  switch( pMWin->eStart ){
       147847  +    case TK_FOLLOWING:
       147848  +      if( pMWin->eFrmType!=TK_RANGE
       147849  +       && windowExprGtZero(pParse, pMWin->pStart)
       147850  +      ){
       147851  +        s.eDelete = WINDOW_RETURN_ROW;
       147852  +      }
       147853  +      break;
       147854  +    case TK_UNBOUNDED:
       147855  +      if( windowCacheFrame(pMWin)==0 ){
       147856  +        if( pMWin->eEnd==TK_PRECEDING ){
       147857  +          if( pMWin->eFrmType!=TK_RANGE
       147858  +           && windowExprGtZero(pParse, pMWin->pEnd)
       147859  +          ){
       147860  +            s.eDelete = WINDOW_AGGSTEP;
       147861  +          }
       147862  +        }else{
       147863  +          s.eDelete = WINDOW_RETURN_ROW;
       147864  +        }
       147865  +      }
       147866  +      break;
       147867  +    default:
       147868  +      s.eDelete = WINDOW_AGGINVERSE;
       147869  +      break;
       147870  +  }
       147871  +
       147872  +  /* Allocate registers for the array of values from the sub-query, the
       147873  +  ** samve values in record form, and the rowid used to insert said record
       147874  +  ** into the ephemeral table.  */
       147875  +  regNew = pParse->nMem+1;
       147876  +  pParse->nMem += nInput;
       147877  +  regRecord = ++pParse->nMem;
       147878  +  regRowid = ++pParse->nMem;
       147879  +
       147880  +  /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
       147881  +  ** clause, allocate registers to store the results of evaluating each
       147882  +  ** <expr>.  */
       147883  +  if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
       147884  +    regStart = ++pParse->nMem;
       147885  +  }
       147886  +  if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
       147887  +    regEnd = ++pParse->nMem;
       147888  +  }
       147889  +
       147890  +  /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
       147891  +  ** registers to store copies of the ORDER BY expressions (peer values) 
       147892  +  ** for the main loop, and for each cursor (start, current and end). */
       147893  +  if( pMWin->eFrmType!=TK_ROWS ){
       147894  +    int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
       147895  +    regNewPeer = regNew + pMWin->nBufferCol;
       147896  +    if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
       147897  +    regPeer = pParse->nMem+1;       pParse->nMem += nPeer;
       147898  +    s.start.reg = pParse->nMem+1;   pParse->nMem += nPeer;
       147899  +    s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
       147900  +    s.end.reg = pParse->nMem+1;     pParse->nMem += nPeer;
       147901  +  }
       147902  +
       147903  +  /* Load the column values for the row returned by the sub-select
       147904  +  ** into an array of registers starting at regNew. Assemble them into
       147905  +  ** a record in register regRecord. */
       147906  +  for(iInput=0; iInput<nInput; iInput++){
       147907  +    sqlite3VdbeAddOp3(v, OP_Column, csrInput, iInput, regNew+iInput);
       147908  +  }
       147909  +  sqlite3VdbeAddOp3(v, OP_MakeRecord, regNew, nInput, regRecord);
       147910  +
       147911  +  /* An input row has just been read into an array of registers starting
       147912  +  ** at regNew. If the window has a PARTITION clause, this block generates 
       147913  +  ** VM code to check if the input row is the start of a new partition.
       147914  +  ** If so, it does an OP_Gosub to an address to be filled in later. The
       147915  +  ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
       147916  +  if( pMWin->pPartition ){
       147917  +    int addr;
       147918  +    ExprList *pPart = pMWin->pPartition;
       147919  +    int nPart = pPart->nExpr;
       147920  +    int regNewPart = regNew + pMWin->nBufferCol;
       147921  +    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
       147922  +
       147923  +    regFlushPart = ++pParse->nMem;
       147924  +    addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
       147925  +    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
       147926  +    sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
       147927  +    VdbeCoverageEqNe(v);
       147928  +    addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub, regFlushPart);
       147929  +    VdbeComment((v, "call flush_partition"));
       147930  +    sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
       147931  +  }
       147932  +
       147933  +  /* Insert the new row into the ephemeral table */
       147934  +  sqlite3VdbeAddOp2(v, OP_NewRowid, csrWrite, regRowid);
       147935  +  sqlite3VdbeAddOp3(v, OP_Insert, csrWrite, regRecord, regRowid);
       147936  +  addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, regRowid);
       147937  +  VdbeCoverageNeverNull(v);
       147938  +
       147939  +  /* This block is run for the first row of each partition */
       147940  +  s.regArg = windowInitAccum(pParse, pMWin);
       147941  +
       147942  +  if( regStart ){
       147943  +    sqlite3ExprCode(pParse, pMWin->pStart, regStart);
       147944  +    windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
       147945  +  }
       147946  +  if( regEnd ){
       147947  +    sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
       147948  +    windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
       147949  +  }
       147950  +
       147951  +  if( pMWin->eStart==pMWin->eEnd && regStart ){
       147952  +    int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
       147953  +    int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
       147954  +    VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
       147955  +    VdbeCoverageNeverNullIf(v, op==OP_Le); /*   values previously checked */
       147956  +    windowAggFinal(&s, 0);
       147957  +    sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
       147958  +    VdbeCoverageNeverTaken(v);
       147959  +    windowReturnOneRow(&s);
       147960  +    sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
       147961  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
       147962  +    sqlite3VdbeJumpHere(v, addrGe);
       147963  +  }
       147964  +  if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
       147965  +    assert( pMWin->eEnd==TK_FOLLOWING );
       147966  +    sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regStart);
       147967  +  }
       147968  +
       147969  +  if( pMWin->eStart!=TK_UNBOUNDED ){
       147970  +    sqlite3VdbeAddOp2(v, OP_Rewind, s.start.csr, 1);
       147971  +    VdbeCoverageNeverTaken(v);
       147972  +  }
       147973  +  sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
       147974  +  VdbeCoverageNeverTaken(v);
       147975  +  sqlite3VdbeAddOp2(v, OP_Rewind, s.end.csr, 1);
       147976  +  VdbeCoverageNeverTaken(v);
       147977  +  if( regPeer && pOrderBy ){
       147978  +    sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
       147979  +    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
       147980  +    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
       147981  +    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
       147982  +  }
       147983  +
       147984  +  sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
       147985  +
       147986  +  sqlite3VdbeJumpHere(v, addrNe);
       147987  +
       147988  +  /* Beginning of the block executed for the second and subsequent rows. */
       147989  +  if( regPeer ){
       147990  +    windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd);
       147991  +  }
       147992  +  if( pMWin->eStart==TK_FOLLOWING ){
       147993  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       147994  +    if( pMWin->eEnd!=TK_UNBOUNDED ){
       147995  +      if( pMWin->eFrmType==TK_RANGE ){
       147996  +        int lbl = sqlite3VdbeMakeLabel(pParse);
       147997  +        int addrNext = sqlite3VdbeCurrentAddr(v);
       147998  +        windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
       147999  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148000  +        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148001  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
       148002  +        sqlite3VdbeResolveLabel(v, lbl);
       148003  +      }else{
       148004  +        windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 0);
       148005  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148006  +      }
       148007  +    }
       148008  +  }else
       148009  +  if( pMWin->eEnd==TK_PRECEDING ){
       148010  +    int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
       148011  +    windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
       148012  +    if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148013  +    windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148014  +    if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148015  +  }else{
       148016  +    int addr = 0;
       148017  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       148018  +    if( pMWin->eEnd!=TK_UNBOUNDED ){
       148019  +      if( pMWin->eFrmType==TK_RANGE ){
       148020  +        int lbl = 0;
       148021  +        addr = sqlite3VdbeCurrentAddr(v);
       148022  +        if( regEnd ){
       148023  +          lbl = sqlite3VdbeMakeLabel(pParse);
       148024  +          windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
       148025  +        }
       148026  +        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148027  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148028  +        if( regEnd ){
       148029  +          sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
       148030  +          sqlite3VdbeResolveLabel(v, lbl);
       148031  +        }
       148032  +      }else{
       148033  +        if( regEnd ){
       148034  +          addr = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
       148035  +          VdbeCoverage(v);
       148036  +        }
       148037  +        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148038  +        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148039  +        if( regEnd ) sqlite3VdbeJumpHere(v, addr);
       148040  +      }
       148041  +    }
       148042  +  }
       148043  +
       148044  +  /* End of the main input loop */
       148045  +  sqlite3VdbeResolveLabel(v, lblWhereEnd);
       148046  +  sqlite3WhereEnd(pWInfo);
       148047  +
       148048  +  /* Fall through */
       148049  +  if( pMWin->pPartition ){
       148050  +    addrInteger = sqlite3VdbeAddOp2(v, OP_Integer, 0, regFlushPart);
       148051  +    sqlite3VdbeJumpHere(v, addrGosubFlush);
       148052  +  }
       148053  +
       148054  +  addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind, csrWrite);
       148055  +  VdbeCoverage(v);
       148056  +  if( pMWin->eEnd==TK_PRECEDING ){
       148057  +    int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
       148058  +    windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
       148059  +    if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148060  +    windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
       148061  +  }else if( pMWin->eStart==TK_FOLLOWING ){
       148062  +    int addrStart;
       148063  +    int addrBreak1;
       148064  +    int addrBreak2;
       148065  +    int addrBreak3;
       148066  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       148067  +    if( pMWin->eFrmType==TK_RANGE ){
       148068  +      addrStart = sqlite3VdbeCurrentAddr(v);
       148069  +      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
       148070  +      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
       148071  +    }else
       148072  +    if( pMWin->eEnd==TK_UNBOUNDED ){
       148073  +      addrStart = sqlite3VdbeCurrentAddr(v);
       148074  +      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regStart, 1);
       148075  +      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, 0, 1);
       148076  +    }else{
       148077  +      assert( pMWin->eEnd==TK_FOLLOWING );
       148078  +      addrStart = sqlite3VdbeCurrentAddr(v);
       148079  +      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 1);
       148080  +      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
       148081  +    }
       148082  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
       148083  +    sqlite3VdbeJumpHere(v, addrBreak2);
       148084  +    addrStart = sqlite3VdbeCurrentAddr(v);
       148085  +    addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
       148086  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
       148087  +    sqlite3VdbeJumpHere(v, addrBreak1);
       148088  +    sqlite3VdbeJumpHere(v, addrBreak3);
       148089  +  }else{
       148090  +    int addrBreak;
       148091  +    int addrStart;
       148092  +    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
       148093  +    addrStart = sqlite3VdbeCurrentAddr(v);
       148094  +    addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
       148095  +    windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
       148096  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
       148097  +    sqlite3VdbeJumpHere(v, addrBreak);
       148098  +  }
       148099  +  sqlite3VdbeJumpHere(v, addrEmpty);
       148100  +
       148101  +  sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
       148102  +  if( pMWin->pPartition ){
       148103  +    if( pMWin->regStartRowid ){
       148104  +      sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
       148105  +      sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
       148106  +    }
       148107  +    sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
       148108  +    sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
147299 148109     }
147300 148110   }
147301 148111   
147302 148112   #endif /* SQLITE_OMIT_WINDOWFUNC */
147303 148113   
147304 148114   /************** End of window.c **********************************************/
147305 148115   /************** Begin file parse.c *******************************************/
................................................................................
147480 148290       ){
147481 148291         sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
147482 148292                            pIdToken->n, pIdToken->z);
147483 148293       }
147484 148294       sqlite3ExprListSetName(pParse, p, pIdToken, 1);
147485 148295       return p;
147486 148296     }
       148297  +
       148298  +#if TK_SPAN>255
       148299  +# error too many tokens in the grammar
       148300  +#endif
147487 148301   /**************** End of %include directives **********************************/
147488 148302   /* These constants specify the various numeric values for terminal symbols
147489 148303   ** in a format understandable to "makeheaders".  This section is blank unless
147490 148304   ** "lemon" is run with the "-m" command-line option.
147491 148305   ***************** Begin makeheaders token definitions *************************/
147492 148306   /**************** End makeheaders token definitions ***************************/
147493 148307   
................................................................................
147543 148357   **    YY_MAX_REDUCE      Maximum value for reduce actions
147544 148358   */
147545 148359   #ifndef INTERFACE
147546 148360   # define INTERFACE 1
147547 148361   #endif
147548 148362   /************* Begin control #defines *****************************************/
147549 148363   #define YYCODETYPE unsigned short int
147550         -#define YYNOCODE 278
       148364  +#define YYNOCODE 301
147551 148365   #define YYACTIONTYPE unsigned short int
147552         -#define YYWILDCARD 91
       148366  +#define YYWILDCARD 95
147553 148367   #define sqlite3ParserTOKENTYPE Token
147554 148368   typedef union {
147555 148369     int yyinit;
147556 148370     sqlite3ParserTOKENTYPE yy0;
147557         -  ExprList* yy42;
147558         -  int yy96;
147559         -  TriggerStep* yy119;
147560         -  Window* yy147;
147561         -  SrcList* yy167;
147562         -  Upsert* yy266;
147563         -  struct FrameBound yy317;
147564         -  IdList* yy336;
147565         -  struct TrigEvent yy350;
147566         -  struct {int value; int mask;} yy367;
147567         -  Select* yy423;
147568         -  const char* yy464;
147569         -  Expr* yy490;
147570         -  With* yy499;
       148371  +  With* yy59;
       148372  +  IdList* yy62;
       148373  +  struct TrigEvent yy90;
       148374  +  Upsert* yy136;
       148375  +  struct FrameBound yy201;
       148376  +  u8 yy238;
       148377  +  const char* yy294;
       148378  +  Window* yy295;
       148379  +  struct {int value; int mask;} yy355;
       148380  +  ExprList* yy434;
       148381  +  TriggerStep* yy455;
       148382  +  Select* yy457;
       148383  +  SrcList* yy483;
       148384  +  int yy494;
       148385  +  Expr* yy524;
147571 148386   } YYMINORTYPE;
147572 148387   #ifndef YYSTACKDEPTH
147573 148388   #define YYSTACKDEPTH 100
147574 148389   #endif
147575 148390   #define sqlite3ParserARG_SDECL
147576 148391   #define sqlite3ParserARG_PDECL
147577 148392   #define sqlite3ParserARG_PARAM
................................................................................
147579 148394   #define sqlite3ParserARG_STORE
147580 148395   #define sqlite3ParserCTX_SDECL Parse *pParse;
147581 148396   #define sqlite3ParserCTX_PDECL ,Parse *pParse
147582 148397   #define sqlite3ParserCTX_PARAM ,pParse
147583 148398   #define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
147584 148399   #define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
147585 148400   #define YYFALLBACK 1
147586         -#define YYNSTATE             524
147587         -#define YYNRULE              369
147588         -#define YYNTOKEN             155
147589         -#define YY_MAX_SHIFT         523
147590         -#define YY_MIN_SHIFTREDUCE   760
147591         -#define YY_MAX_SHIFTREDUCE   1128
147592         -#define YY_ERROR_ACTION      1129
147593         -#define YY_ACCEPT_ACTION     1130
147594         -#define YY_NO_ACTION         1131
147595         -#define YY_MIN_REDUCE        1132
147596         -#define YY_MAX_REDUCE        1500
       148401  +#define YYNSTATE             541
       148402  +#define YYNRULE              375
       148403  +#define YYNTOKEN             176
       148404  +#define YY_MAX_SHIFT         540
       148405  +#define YY_MIN_SHIFTREDUCE   784
       148406  +#define YY_MAX_SHIFTREDUCE   1158
       148407  +#define YY_ERROR_ACTION      1159
       148408  +#define YY_ACCEPT_ACTION     1160
       148409  +#define YY_NO_ACTION         1161
       148410  +#define YY_MIN_REDUCE        1162
       148411  +#define YY_MAX_REDUCE        1536
147597 148412   /************* End control #defines *******************************************/
147598 148413   #define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
147599 148414   
147600 148415   /* Define the yytestcase() macro to be a no-op if is not already defined
147601 148416   ** otherwise.
147602 148417   **
147603 148418   ** Applications can choose to define yytestcase() in the %include section
................................................................................
147656 148471   **  yy_shift_ofst[]    For each state, the offset into yy_action for
147657 148472   **                     shifting terminals.
147658 148473   **  yy_reduce_ofst[]   For each state, the offset into yy_action for
147659 148474   **                     shifting non-terminals after a reduce.
147660 148475   **  yy_default[]       Default action for each state.
147661 148476   **
147662 148477   *********** Begin parsing tables **********************************************/
147663         -#define YY_ACTTAB_COUNT (2009)
       148478  +#define YY_ACTTAB_COUNT (2142)
147664 148479   static const YYACTIONTYPE yy_action[] = {
147665         - /*     0 */   377,  518,  371,  107,  104,  200, 1293,  518, 1130,    1,
147666         - /*    10 */     1,  523,    2, 1134,  518, 1203, 1203, 1262,  277,  373,
147667         - /*    20 */   129,  495,   37,   37, 1397, 1201, 1201, 1211,   65,   65,
147668         - /*    30 */   480,  891,  107,  104,  200,   37,   37, 1043, 1494,  892,
147669         - /*    40 */   346, 1494,  342,  114,  115,  105, 1106, 1106,  957,  960,
147670         - /*    50 */   950,  950,  112,  112,  113,  113,  113,  113,  285,  254,
147671         - /*    60 */   254,  518,  254,  254,  500,  518,  495,  518,  107,  104,
147672         - /*    70 */   200, 1085,  515,  481,  386,  515, 1464,  442,  501,  230,
147673         - /*    80 */   197,  439,   37,   37, 1172,  210,   65,   65,   65,   65,
147674         - /*    90 */   254,  254,  111,  111,  111,  111,  110,  110,  109,  109,
147675         - /*   100 */   109,  108,  404,  515,  404,  155, 1041,  431,  401,  400,
147676         - /*   110 */   254,  254,  373, 1431, 1427,  408, 1110, 1085, 1086, 1087,
147677         - /*   120 */   284, 1112,  500,  515,  500,  368, 1433, 1421, 1428, 1111,
147678         - /*   130 */  1261,  499,  373,  502,  108,  404,  114,  115,  105, 1106,
147679         - /*   140 */  1106,  957,  960,  950,  950,  112,  112,  113,  113,  113,
147680         - /*   150 */   113,  276,  509, 1113,  369, 1113,  114,  115,  105, 1106,
147681         - /*   160 */  1106,  957,  960,  950,  950,  112,  112,  113,  113,  113,
147682         - /*   170 */   113,  496, 1420, 1431,  493, 1468, 1065,  260, 1063,  433,
147683         - /*   180 */    74,  107,  104,  200,  498,  111,  111,  111,  111,  110,
147684         - /*   190 */   110,  109,  109,  109,  108,  404,  373,  113,  113,  113,
147685         - /*   200 */   113,  106,  131,   91, 1361,  111,  111,  111,  111,  110,
147686         - /*   210 */   110,  109,  109,  109,  108,  404,  113,  113,  113,  113,
147687         - /*   220 */   114,  115,  105, 1106, 1106,  957,  960,  950,  950,  112,
147688         - /*   230 */   112,  113,  113,  113,  113,  111,  111,  111,  111,  110,
147689         - /*   240 */   110,  109,  109,  109,  108,  404,  116,  110,  110,  109,
147690         - /*   250 */   109,  109,  108,  404,  111,  111,  111,  111,  110,  110,
147691         - /*   260 */   109,  109,  109,  108,  404,  917,  512,  512,  512,  111,
147692         - /*   270 */   111,  111,  111,  110,  110,  109,  109,  109,  108,  404,
147693         - /*   280 */   517, 1198, 1177,  181,  109,  109,  109,  108,  404,  373,
147694         - /*   290 */  1198,  402,  402,  402,   75,  360,  111,  111,  111,  111,
147695         - /*   300 */   110,  110,  109,  109,  109,  108,  404,  382,  299,  419,
147696         - /*   310 */   287,  170,  518,  114,  115,  105, 1106, 1106,  957,  960,
147697         - /*   320 */   950,  950,  112,  112,  113,  113,  113,  113, 1444,  523,
147698         - /*   330 */     2, 1134,  518,   13,   13,  337,  277, 1085,  129,  226,
147699         - /*   340 */   937, 1058, 1000,  471,  917, 1211,  453,  384, 1085,  395,
147700         - /*   350 */   162, 1057,  155,   45,   45,  416,  928,  401,  400,  479,
147701         - /*   360 */   927,   12,  111,  111,  111,  111,  110,  110,  109,  109,
147702         - /*   370 */   109,  108,  404,  226,  286,  254,  254,  254,  254,  518,
147703         - /*   380 */    16,   16,  373, 1085, 1086, 1087,  314,  299,  515,  472,
147704         - /*   390 */   515,  927,  927,  929, 1085, 1086, 1087,  378,  276,  509,
147705         - /*   400 */    65,   65, 1113,  210, 1113, 1085,  114,  115,  105, 1106,
147706         - /*   410 */  1106,  957,  960,  950,  950,  112,  112,  113,  113,  113,
147707         - /*   420 */   113, 1448,  222, 1134, 1089,  461,  458,  457,  277,  180,
147708         - /*   430 */   129,  378,  392,  408,  423,  456,  500, 1211,  240,  257,
147709         - /*   440 */   324,  464,  319,  463,  227,  470,   12,  317,  424,  300,
147710         - /*   450 */   317, 1085, 1086, 1087,  485,  111,  111,  111,  111,  110,
147711         - /*   460 */   110,  109,  109,  109,  108,  404,  181,  118, 1085,  254,
147712         - /*   470 */   254, 1089,  518,   90,  351,  373,  518, 1181,  365,  798,
147713         - /*   480 */  1440,  339,  515,  248,  248,   77,  325,  133, 1085,  249,
147714         - /*   490 */   424,  300,  794,   49,   49,  210,  515,   65,   65,  114,
147715         - /*   500 */   115,  105, 1106, 1106,  957,  960,  950,  950,  112,  112,
147716         - /*   510 */   113,  113,  113,  113, 1085, 1086, 1087,  222, 1085,  438,
147717         - /*   520 */   461,  458,  457,  937,  787,  408,  171,  857,  362, 1021,
147718         - /*   530 */   456,  136,  198,  486, 1085, 1086, 1087,  448,  794,  928,
147719         - /*   540 */     5,  193,  192,  927, 1022,  107,  104,  200,  111,  111,
147720         - /*   550 */   111,  111,  110,  110,  109,  109,  109,  108,  404, 1023,
147721         - /*   560 */   254,  254,  803, 1085, 1085, 1086, 1087,  437,  373, 1085,
147722         - /*   570 */   344,  787,  791,  515,  927,  927,  929, 1085, 1408, 1396,
147723         - /*   580 */   832, 1085,  176,    3,  852, 1085,  518, 1439,  429,  851,
147724         - /*   590 */   833,  518,  114,  115,  105, 1106, 1106,  957,  960,  950,
147725         - /*   600 */   950,  112,  112,  113,  113,  113,  113,   13,   13, 1085,
147726         - /*   610 */  1086, 1087,   13,   13,  518, 1085, 1086, 1087, 1496,  358,
147727         - /*   620 */  1085,  389, 1234, 1085, 1086, 1087,  391, 1085, 1086, 1087,
147728         - /*   630 */   448, 1085, 1086, 1087,  518,   65,   65,  947,  947,  958,
147729         - /*   640 */   961,  111,  111,  111,  111,  110,  110,  109,  109,  109,
147730         - /*   650 */   108,  404,  518,  382,  878,   13,   13,  518,  877,  518,
147731         - /*   660 */   263,  373,  518,  431,  448, 1070, 1085, 1086, 1087,  267,
147732         - /*   670 */   448,  488, 1360,   64,   64,  431,  812,  155,   50,   50,
147733         - /*   680 */    65,   65,  518,   65,   65,  114,  115,  105, 1106, 1106,
147734         - /*   690 */   957,  960,  950,  950,  112,  112,  113,  113,  113,  113,
147735         - /*   700 */   518,  951,  382,   13,   13,  415,  411,  462,  414, 1085,
147736         - /*   710 */  1366,  777, 1210,  292,  297,  813,  399,  497,  181,  403,
147737         - /*   720 */   261,   15,   15,  276,  509,  414,  413, 1366, 1368,  410,
147738         - /*   730 */   372,  345, 1209,  264,  111,  111,  111,  111,  110,  110,
147739         - /*   740 */   109,  109,  109,  108,  404,  265,  254,  254,  229, 1405,
147740         - /*   750 */   268, 1215,  268, 1103,  373, 1085, 1086, 1087,  938,  515,
147741         - /*   760 */   393,  409,  876,  515,  254,  254, 1152,  482,  473,  262,
147742         - /*   770 */   422,  476,  325,  503,  289,  518,  291,  515,  114,  115,
147743         - /*   780 */   105, 1106, 1106,  957,  960,  950,  950,  112,  112,  113,
147744         - /*   790 */   113,  113,  113,  414, 1021, 1366,   39,   39,  254,  254,
147745         - /*   800 */   254,  254,  980,  254,  254,  254,  254,  255,  255, 1022,
147746         - /*   810 */   279,  515,  516,  515,  846,  846,  515,  138,  515,  518,
147747         - /*   820 */   515, 1043, 1495,  251, 1023, 1495,  876,  111,  111,  111,
147748         - /*   830 */   111,  110,  110,  109,  109,  109,  108,  404,  518, 1353,
147749         - /*   840 */    51,   51,  518,  199,  518,  506,  290,  373,  518,  276,
147750         - /*   850 */   509,  922,    9,  483,  233, 1005, 1005,  445,  189,   52,
147751         - /*   860 */    52,  325,  280,   53,   53,   54,   54,  373,  876,   55,
147752         - /*   870 */    55,  114,  115,  105, 1106, 1106,  957,  960,  950,  950,
147753         - /*   880 */   112,  112,  113,  113,  113,  113,   97,  518,   95, 1104,
147754         - /*   890 */  1041,  114,  115,  105, 1106, 1106,  957,  960,  950,  950,
147755         - /*   900 */   112,  112,  113,  113,  113,  113,  135,  199,   56,   56,
147756         - /*   910 */   765,  766,  767,  225,  224,  223,  518,  283,  437,  233,
147757         - /*   920 */   111,  111,  111,  111,  110,  110,  109,  109,  109,  108,
147758         - /*   930 */   404, 1002,  876,  326,  518, 1002, 1104,   40,   40,  518,
147759         - /*   940 */   111,  111,  111,  111,  110,  110,  109,  109,  109,  108,
147760         - /*   950 */   404,  518,  448,  518, 1104,   41,   41,  518,   17,  518,
147761         - /*   960 */    43,   43, 1155,  379,  518,  448,  518,  443,  518,  390,
147762         - /*   970 */   518,  194,   44,   44,   57,   57, 1247,  518,   58,   58,
147763         - /*   980 */    59,   59,  518,  466,  326,   14,   14,   60,   60,  120,
147764         - /*   990 */   120,   61,   61,  449, 1206,   93,  518,  425,   46,   46,
147765         - /*  1000 */   518, 1104,  518,   62,   62,  518,  437,  305,  518,  852,
147766         - /*  1010 */   518,  298,  518, 1246,  851,  373,  518,   63,   63, 1293,
147767         - /*  1020 */   397,   47,   47,  142,  142, 1467,  143,  143,  821,   70,
147768         - /*  1030 */    70,   48,   48,   66,   66,  373,  518,  121,  121,  114,
147769         - /*  1040 */   115,  105, 1106, 1106,  957,  960,  950,  950,  112,  112,
147770         - /*  1050 */   113,  113,  113,  113,  518,  418,  518,   67,   67,  114,
147771         - /*  1060 */   115,  105, 1106, 1106,  957,  960,  950,  950,  112,  112,
147772         - /*  1070 */   113,  113,  113,  113,  312,  122,  122,  123,  123, 1293,
147773         - /*  1080 */   518,  357, 1126,   88,  518,  435,  325,  387,  111,  111,
147774         - /*  1090 */   111,  111,  110,  110,  109,  109,  109,  108,  404,  266,
147775         - /*  1100 */   518,  119,  119,  518, 1293,  141,  141,  518,  111,  111,
147776         - /*  1110 */   111,  111,  110,  110,  109,  109,  109,  108,  404,  518,
147777         - /*  1120 */   801,  140,  140,  518,  127,  127,  511,  379,  126,  126,
147778         - /*  1130 */   518,  137,  518, 1308,  518,  307,  518,  310,  518,  203,
147779         - /*  1140 */   124,  124, 1307,   96,  125,  125,  207,  388, 1441,  468,
147780         - /*  1150 */  1127,   69,   69,   71,   71,   68,   68,   38,   38,   42,
147781         - /*  1160 */    42,  357, 1042,  373, 1293,  276,  509,  801,  185,  469,
147782         - /*  1170 */   494,  436,  444,    6,  380,  156,  253,  197,  469,  134,
147783         - /*  1180 */   426,   33, 1038,  373, 1121,  359, 1411,  114,  115,  105,
147784         - /*  1190 */  1106, 1106,  957,  960,  950,  950,  112,  112,  113,  113,
147785         - /*  1200 */   113,  113,  914,  296,   27,  293,   90,  114,  103,  105,
147786         - /*  1210 */  1106, 1106,  957,  960,  950,  950,  112,  112,  113,  113,
147787         - /*  1220 */   113,  113,  919,  275,  430,  232,  891,  232,  432,  256,
147788         - /*  1230 */  1127,  232,  398,  370,  892,   28,  111,  111,  111,  111,
147789         - /*  1240 */   110,  110,  109,  109,  109,  108,  404,  301,  454, 1385,
147790         - /*  1250 */    90,  228,  209,  987,  811,  810,  111,  111,  111,  111,
147791         - /*  1260 */   110,  110,  109,  109,  109,  108,  404,  315,  818,  819,
147792         - /*  1270 */    90,  323,  983,  931,  885,  228,  373,  232,  999,  849,
147793         - /*  1280 */   999,  322,  102,  998, 1384,  998,  785,  850,  440,  132,
147794         - /*  1290 */   102,  302, 1243,  306,  309,  311,  373,  313, 1194, 1180,
147795         - /*  1300 */   987,  115,  105, 1106, 1106,  957,  960,  950,  950,  112,
147796         - /*  1310 */   112,  113,  113,  113,  113, 1178, 1179,  318,  327,  328,
147797         - /*  1320 */   931, 1255,  105, 1106, 1106,  957,  960,  950,  950,  112,
147798         - /*  1330 */   112,  113,  113,  113,  113, 1292, 1230, 1457,  273, 1241,
147799         - /*  1340 */   504,  505, 1298,  100,  510,  246,    4, 1161, 1154,  111,
147800         - /*  1350 */   111,  111,  111,  110,  110,  109,  109,  109,  108,  404,
147801         - /*  1360 */   513, 1143,  187, 1142,  202, 1144, 1451,  356, 1227,  111,
147802         - /*  1370 */   111,  111,  111,  110,  110,  109,  109,  109,  108,  404,
147803         - /*  1380 */    11, 1277,  330,  405,  332,  334,  191, 1285,  364,  195,
147804         - /*  1390 */   295,  417,  288,  100,  510,  507,    4,  434,  459,  321,
147805         - /*  1400 */  1177,  349, 1357, 1356,  336,  155,  190, 1454, 1121,  158,
147806         - /*  1410 */   513,  508,  235, 1404,  937, 1402, 1118,  381,   77,  428,
147807         - /*  1420 */    98,   98,    8, 1282,  168,   30,  152,   99,  160,  405,
147808         - /*  1430 */   520,  519,   88,  405,  927, 1362, 1274,  420,  163,   73,
147809         - /*  1440 */   164,   76,  165,  166,  421,  507,  452,  212,  361,  363,
147810         - /*  1450 */   427,  276,  509,   31, 1288,  172,  491,  441,  216, 1351,
147811         - /*  1460 */    82,  490,  447, 1373,  937,  927,  927,  929,  930,   24,
147812         - /*  1470 */    98,   98,  304,  247,  218,  177,  308,   99,  219,  405,
147813         - /*  1480 */   520,  519,  450, 1145,  927,  220,  366, 1197,  100,  510,
147814         - /*  1490 */   465,    4, 1188, 1196, 1195,  394,  803, 1169, 1187,  367,
147815         - /*  1500 */  1168,  396,  484,  320, 1167,  513, 1466,   87,  475,  100,
147816         - /*  1510 */   510,  271,    4,  272,  478,  927,  927,  929,  930,   24,
147817         - /*  1520 */  1443, 1074,  407, 1238, 1239,  258,  513,  329,  405,  331,
147818         - /*  1530 */   355,  355,  354,  243,  352,  234,  489,  774,  498,  184,
147819         - /*  1540 */   507,  338, 1422,  339,  117, 1220,   10,  341,  333,  405,
147820         - /*  1550 */   204,  491,  282, 1219, 1237, 1236,  492,  335,  343,  937,
147821         - /*  1560 */   281,  507,   94, 1337,  186,   98,   98,  347,   89,  487,
147822         - /*  1570 */   348,  241,   99,   29,  405,  520,  519,  274, 1151,  927,
147823         - /*  1580 */   937,  521, 1080,  245,  242,  244,   98,   98,  856,  522,
147824         - /*  1590 */   206, 1140, 1135,   99,  144,  405,  520,  519,  147,  375,
147825         - /*  1600 */   927,  149,  376,  157, 1389, 1390, 1388, 1387,  205,  145,
147826         - /*  1610 */   927,  927,  929,  930,   24,  146,  130,  761, 1165, 1164,
147827         - /*  1620 */    72,  100,  510, 1162,    4,  269,  406,  188,  278,  201,
147828         - /*  1630 */   259,  927,  927,  929,  930,   24,  128,  911,  513,  997,
147829         - /*  1640 */   995,  159,  374,  208,  148,  161,  835,  276,  509,  211,
147830         - /*  1650 */   294, 1011,  915,  167,  150,  383,  169,   78,  385,   79,
147831         - /*  1660 */    80,  405,   81,  151, 1014,  213,  214, 1010,  139,   18,
147832         - /*  1670 */   412,  215,  303,  507,  232, 1115, 1003,  446,  173,  217,
147833         - /*  1680 */   174,   32,  776,  451,  491,  322,  221,  175,  814,  490,
147834         - /*  1690 */    83,  455,  937,   19,  460,  316,   20,   84,   98,   98,
147835         - /*  1700 */   270,  182,   85,  467,  153,   99,  154,  405,  520,  519,
147836         - /*  1710 */  1074,  407,  927,  183,  258,  963, 1046,   86,   34,  355,
147837         - /*  1720 */   355,  354,  243,  352,  474, 1047,  774,   35,  477,  196,
147838         - /*  1730 */   250,  100,  510,  252,    4,  884,  178,  231, 1060,  204,
147839         - /*  1740 */    21,  282,  102,  927,  927,  929,  930,   24,  513,  281,
147840         - /*  1750 */   879,   22, 1064, 1062, 1051,    7,  340,   23,  978,  179,
147841         - /*  1760 */    90,   92,  510,  964,    4,  236,  962,  966, 1020, 1019,
147842         - /*  1770 */   237,  405,  967,   25,   36,  514,  932,  786,  513,  206,
147843         - /*  1780 */   101,   26,  845,  507,  238,  239, 1459,  147,  350, 1458,
147844         - /*  1790 */   149,  353, 1075, 1131, 1131, 1131, 1131,  205, 1131, 1131,
147845         - /*  1800 */  1131,  405,  937, 1131, 1131, 1131, 1131, 1131,   98,   98,
147846         - /*  1810 */  1131, 1131, 1131,  507, 1131,   99, 1131,  405,  520,  519,
147847         - /*  1820 */  1131, 1131,  927, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147848         - /*  1830 */  1131,  374,  937, 1131, 1131, 1131,  276,  509,   98,   98,
147849         - /*  1840 */  1131, 1131, 1131, 1131, 1131,   99, 1131,  405,  520,  519,
147850         - /*  1850 */  1131, 1131,  927,  927,  927,  929,  930,   24, 1131,  412,
147851         - /*  1860 */  1131, 1131, 1131,  258, 1131, 1131, 1131, 1131,  355,  355,
147852         - /*  1870 */   354,  243,  352, 1131, 1131,  774, 1131, 1131, 1131, 1131,
147853         - /*  1880 */  1131, 1131, 1131,  927,  927,  929,  930,   24,  204, 1131,
147854         - /*  1890 */   282, 1131, 1131, 1131, 1131, 1131, 1131, 1131,  281, 1131,
147855         - /*  1900 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147856         - /*  1910 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147857         - /*  1920 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,  206, 1131,
147858         - /*  1930 */  1131, 1131, 1131, 1131, 1131, 1131,  147, 1131, 1131,  149,
147859         - /*  1940 */  1131, 1131, 1131, 1131, 1131, 1131,  205, 1131, 1131, 1131,
147860         - /*  1950 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147861         - /*  1960 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147862         - /*  1970 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147863         - /*  1980 */   374, 1131, 1131, 1131, 1131,  276,  509, 1131, 1131, 1131,
147864         - /*  1990 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,
147865         - /*  2000 */  1131, 1131, 1131, 1131, 1131, 1131, 1131, 1131,  412,
       148480  + /*     0 */   535, 1323,  112,  109,  209,  112,  109,  209, 1160,    1,
       148481  + /*    10 */     1,  540,    2, 1164,  535, 1292, 1228, 1207,  289,  384,
       148482  + /*    20 */   134,   42,   42, 1427,  382, 1228,    9, 1241,  242,  492,
       148483  + /*    30 */  1291,  915,  373,  379, 1026,   70,   70,  427, 1026,  916,
       148484  + /*    40 */   529,  529,  529,  119,  120,  110, 1136, 1136,  981,  984,
       148485  + /*    50 */   974,  974,  117,  117,  118,  118,  118,  118,  380,  264,
       148486  + /*    60 */   264,  264,  264, 1134,  264,  264,  112,  109,  209,  397,
       148487  + /*    70 */   454,  517,  532,  491,  532, 1233, 1233,  532,  239,  206,
       148488  + /*    80 */   493,  112,  109,  209,  464,  219,  118,  118,  118,  118,
       148489  + /*    90 */   111,  393,  440,  444,   16,   16,  116,  116,  116,  116,
       148490  + /*   100 */   115,  115,  114,  114,  114,  113,  415,  971,  971,  982,
       148491  + /*   110 */   985,  235, 1463,  351, 1134,  419,  384,  116,  116,  116,
       148492  + /*   120 */   116,  115,  115,  114,  114,  114,  113,  415,  116,  116,
       148493  + /*   130 */   116,  116,  115,  115,  114,  114,  114,  113,  415,  961,
       148494  + /*   140 */   119,  120,  110, 1136, 1136,  981,  984,  974,  974,  117,
       148495  + /*   150 */   117,  118,  118,  118,  118,  952,  415,  941,  298,  951,
       148496  + /*   160 */   941, 1480,  540,    2, 1164, 1115,  535, 1458,  160,  289,
       148497  + /*   170 */     6,  134, 1504,  389,  406,  975,  338, 1024, 1241,  337,
       148498  + /*   180 */  1089, 1476, 1089,  118,  118,  118,  118,   42,   42,  329,
       148499  + /*   190 */   951,  951,  953,  116,  116,  116,  116,  115,  115,  114,
       148500  + /*   200 */   114,  114,  113,  415,  311,  430,  299,  311,  881,  160,
       148501  + /*   210 */   264,  264,  401,  384,  324, 1115, 1116, 1117,  288,  526,
       148502  + /*   220 */    96,  159, 1441,  532,  141,  116,  116,  116,  116,  115,
       148503  + /*   230 */   115,  114,  114,  114,  113,  415,  219,  119,  120,  110,
       148504  + /*   240 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148505  + /*   250 */   118,  118,  115,  115,  114,  114,  114,  113,  415,  288,
       148506  + /*   260 */   526,  403,  533,  121,  870,  870,  419,  250,  267,  336,
       148507  + /*   270 */   475,  331,  474,  236,  160,  319, 1084,  322, 1465,  329,
       148508  + /*   280 */   350,   12,  535,  384,  502, 1115, 1084,  435,  312, 1084,
       148509  + /*   290 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148510  + /*   300 */   415,  535,  836,   42,   42,  138,  426,  119,  120,  110,
       148511  + /*   310 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148512  + /*   320 */   118,  118,   70,   70,  288,  526,  412,  411,  480, 1457,
       148513  + /*   330 */   335,   79,    6,  473, 1140, 1115, 1116, 1117,  501, 1142,
       148514  + /*   340 */   334,  837,  811, 1484,  512, 1164,  534, 1141,  123,  187,
       148515  + /*   350 */   289,  384,  134,  448,  434, 1115,   80,  349,  498, 1241,
       148516  + /*   360 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148517  + /*   370 */   415, 1143, 1115, 1143,  459,  119,  120,  110, 1136, 1136,
       148518  + /*   380 */   981,  984,  974,  974,  117,  117,  118,  118,  118,  118,
       148519  + /*   390 */   404,  264,  264,  811, 1463,  506,  368, 1156,  535,  114,
       148520  + /*   400 */   114,  114,  113,  415,  532, 1115, 1116, 1117,  231,  518,
       148521  + /*   410 */  1500,  472,  469,  468,  175,  497,  422,  219, 1202,   70,
       148522  + /*   420 */    70,  467, 1115, 1116, 1117,  176,  201,  200,  116,  116,
       148523  + /*   430 */   116,  116,  115,  115,  114,  114,  114,  113,  415,  535,
       148524  + /*   440 */  1115,  264,  264,  435,  312, 1115,  273,  419,  384,  513,
       148525  + /*   450 */  1450, 1115,  326, 1084,  532,  517,   82, 1084,  167,  388,
       148526  + /*   460 */    69,   69, 1115, 1084,  519,  509, 1084, 1084,   12, 1157,
       148527  + /*   470 */  1084,  420,  119,  120,  110, 1136, 1136,  981,  984,  974,
       148528  + /*   480 */   974,  117,  117,  118,  118,  118,  118,  258,  258,  535,
       148529  + /*   490 */  1115, 1116, 1117, 1045,  535, 1115, 1116, 1117, 1323,  535,
       148530  + /*   500 */   532, 1115, 1116, 1117,  296,  483, 1211,  818, 1046,  448,
       148531  + /*   510 */    70,   70, 1115, 1116, 1117,   50,   50,  448,  356,  500,
       148532  + /*   520 */    70,   70,  207, 1047,   32,  116,  116,  116,  116,  115,
       148533  + /*   530 */   115,  114,  114,  114,  113,  415,  453,  264,  264, 1115,
       148534  + /*   540 */   450,  449,  961,  508,  856,  384,  517,    5,  900,  822,
       148535  + /*   550 */   532,  484,  181, 1115,  857,  516,  517,  818,  952,  507,
       148536  + /*   560 */     3, 1115,  951, 1231, 1231,  482,  398, 1115, 1095,  119,
       148537  + /*   570 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
       148538  + /*   580 */   118,  118,  118,  118, 1115,  535,  238, 1115, 1391, 1115,
       148539  + /*   590 */  1116, 1117,  159,  951,  951,  953,  231, 1115,  259,  472,
       148540  + /*   600 */   469,  468,  310, 1115, 1116, 1117,   13,   13,  297,  467,
       148541  + /*   610 */   276, 1115, 1116, 1117,  412,  411, 1095, 1115, 1116, 1117,
       148542  + /*   620 */   395,  355,  116,  116,  116,  116,  115,  115,  114,  114,
       148543  + /*   630 */   114,  113,  415,  208, 1115, 1116, 1117, 1115, 1116, 1117,
       148544  + /*   640 */   264,  264,  384,  337,  902,  393,  815, 1115, 1116, 1117,
       148545  + /*   650 */   413,  413,  413,  532,  112,  109,  209,  309,  900, 1143,
       148546  + /*   660 */   535, 1143,  535,  393,  901, 1210,  119,  120,  110, 1136,
       148547  + /*   670 */  1136,  981,  984,  974,  974,  117,  117,  118,  118,  118,
       148548  + /*   680 */   118,   13,   13,   13,   13,  265,  265,  535,  143,  264,
       148549  + /*   690 */   264,  288,  526,  535, 1119,  400,  535,  402,  532,  510,
       148550  + /*   700 */  1457,  512,  532,    6,  113,  415, 1067, 1530,   70,   70,
       148551  + /*   710 */  1530,  535,  271,  535,   70,   70,  535,   13,   13,  116,
       148552  + /*   720 */   116,  116,  116,  115,  115,  114,  114,  114,  113,  415,
       148553  + /*   730 */   272,  277,   13,   13,   13,   13,  535,   13,   13,  384,
       148554  + /*   740 */   535,  304,  425, 1100,  284, 1119,  184,  801,  185,  338,
       148555  + /*   750 */   285,  514, 1532,  369, 1239, 1438, 1182,   70,   70,  425,
       148556  + /*   760 */   424,   70,   70,  119,  120,  110, 1136, 1136,  981,  984,
       148557  + /*   770 */   974,  974,  117,  117,  118,  118,  118,  118,  190, 1065,
       148558  + /*   780 */  1067, 1531,  442,  107, 1531,  408,  264,  264,  264,  264,
       148559  + /*   790 */   383, 1396,  261,  410,   95,  900,  485,  414,  421,  532,
       148560  + /*   800 */  1045,  532,  301, 1133,  303,  488,  433, 1451, 1396, 1398,
       148561  + /*   810 */   278,  535,  278,  520, 1435, 1046,  116,  116,  116,  116,
       148562  + /*   820 */   115,  115,  114,  114,  114,  113,  415,  425,  264,  264,
       148563  + /*   830 */  1047,  190,   54,   54,  535,  291,  384,  264,  264,  362,
       148564  + /*   840 */   962,  532, 1004,  376, 1084,  264,  264, 1029, 1029,  456,
       148565  + /*   850 */   532,  523,  270, 1065, 1084,   55,   55, 1084,  532,  442,
       148566  + /*   860 */   119,  120,  110, 1136, 1136,  981,  984,  974,  974,  117,
       148567  + /*   870 */   117,  118,  118,  118,  118,  535, 1396,  190,  302, 1383,
       148568  + /*   880 */   208,  535,  789,  790,  791,  535,  515,  535, 1323,  371,
       148569  + /*   890 */   337,  234,  233,  232,  459,  515,   15,   15,  459,  477,
       148570  + /*   900 */   459,  459,   44,   44,  136,  900,   56,   56,   57,   57,
       148571  + /*   910 */  1185,  390,  197,  116,  116,  116,  116,  115,  115,  114,
       148572  + /*   920 */   114,  114,  113,  415,  535,  876,  535,  442,  535,  274,
       148573  + /*   930 */   875, 1323,  357,  384,  353,  140, 1426,  946, 1455, 1323,
       148574  + /*   940 */  1390,    6, 1240, 1236,  292,   58,   58,   59,   59,   60,
       148575  + /*   950 */    60,  535, 1456,  384,  535,    6,  399,  119,  120,  110,
       148576  + /*   960 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148577  + /*   970 */   118,  118,   61,   61,  535,   45,   45,  119,  120,  110,
       148578  + /*   980 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
       148579  + /*   990 */   118,  118, 1477,  479,  202,   46,   46,  275,   95,  455,
       148580  + /*  1000 */   535,  212,  535,  337,  535, 1454,  535,  409,    6,  242,
       148581  + /*  1010 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148582  + /*  1020 */   415,   48,   48,   49,   49,   62,   62,   63,   63,  535,
       148583  + /*  1030 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
       148584  + /*  1040 */   415,  535,  459,  535, 1134,  535, 1151,  535,  142,  535,
       148585  + /*  1050 */    64,   64,  535, 1338,  535,  494,  535,  446,  535, 1264,
       148586  + /*  1060 */   535, 1337,   14,   14,   65,   65,  125,  125,   66,   66,
       148587  + /*  1070 */    51,   51,  535,   67,   67,   68,   68,   52,   52,  147,
       148588  + /*  1080 */   147,  148,  148, 1453,  317,   98,    6,  535, 1245,  481,
       148589  + /*  1090 */   535,  827,  535,   75,   75, 1134,  102,  481,  100,  535,
       148590  + /*  1100 */   532,  535,  368, 1066, 1503,  384,  535,  845,   53,   53,
       148591  + /*  1110 */    93,   71,   71,  126,  126,  295,  528,  390,  288,  526,
       148592  + /*  1120 */    72,   72,  127,  127,  139,  384,   38,  128,  128,  119,
       148593  + /*  1130 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
       148594  + /*  1140 */   118,  118,  118,  118,  535,  495,  535,  447,  535,  119,
       148595  + /*  1150 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
       148596  + /*  1160 */   118,  118,  118,  118,  235,  124,  124,  146,  146,  145,
       148597  + /*  1170 */   145,  287,  535, 1277,  535, 1157,  535,  391,  161,  263,
       148598  + /*  1180 */   206,  381,  116,  116,  116,  116,  115,  115,  114,  114,
       148599  + /*  1190 */   114,  113,  415,  132,  132,  131,  131,  129,  129,  535,
       148600  + /*  1200 */    30,  535,  116,  116,  116,  116,  115,  115,  114,  114,
       148601  + /*  1210 */   114,  113,  415,  535,  216, 1062, 1276,  535,  370,  535,
       148602  + /*  1220 */   130,  130,   74,   74,  535,  915,  389,  876,   17,  437,
       148603  + /*  1230 */   429,   31,  875,  916,   76,   76,  266,  101,   73,   73,
       148604  + /*  1240 */    43,   43,  835,  834,  308,   47,   47,   95,  825,  943,
       148605  + /*  1250 */   441,  938,  241,  241,  305,  443,  313,  384,  241,   95,
       148606  + /*  1260 */   842,  843,  193,  465, 1209,  327,  237,  436,   95, 1011,
       148607  + /*  1270 */  1007,  909,  873,  237,  241,  107, 1023,  384, 1023,  955,
       148608  + /*  1280 */  1415,  119,  120,  110, 1136, 1136,  981,  984,  974,  974,
       148609  + /*  1290 */   117,  117,  118,  118,  118,  118, 1022,  809, 1022,  825,
       148610  + /*  1300 */   137,  119,  108,  110, 1136, 1136,  981,  984,  974,  974,
       148611  + /*  1310 */   117,  117,  118,  118,  118,  118,  874, 1414,  451,  107,
       148612  + /*  1320 */  1011,  314, 1273,  318,  218,  321,  323,  325, 1224, 1208,
       148613  + /*  1330 */   955,  330,  339,  340,  116,  116,  116,  116,  115,  115,
       148614  + /*  1340 */   114,  114,  114,  113,  415, 1285, 1322, 1260, 1493, 1470,
       148615  + /*  1350 */  1271,  283,  521, 1328,  116,  116,  116,  116,  115,  115,
       148616  + /*  1360 */   114,  114,  114,  113,  415, 1191, 1184, 1173, 1172, 1174,
       148617  + /*  1370 */   522, 1487,  211,  460,  384,  256,  199,  367, 1257,  342,
       148618  + /*  1380 */   195,  470,  307,  344,   11,  333,  525,  445, 1307, 1315,
       148619  + /*  1390 */   375,  203, 1207, 1151,  384,  346, 1387,  188,  360,  120,
       148620  + /*  1400 */   110, 1136, 1136,  981,  984,  974,  974,  117,  117,  118,
       148621  + /*  1410 */   118,  118,  118, 1386,  428, 1490,  245,  300,  348, 1148,
       148622  + /*  1420 */   110, 1136, 1136,  981,  984,  974,  974,  117,  117,  118,
       148623  + /*  1430 */   118,  118,  118,  189,  198, 1434, 1432,   78,   81,  163,
       148624  + /*  1440 */    82,  392,  439, 1392,  173,  105,  527,   35,    4,  157,
       148625  + /*  1450 */  1312,  116,  116,  116,  116,  115,  115,  114,  114,  114,
       148626  + /*  1460 */   113,  415,  530,  165,   93, 1304,  431,  432,  168,  463,
       148627  + /*  1470 */   221,  116,  116,  116,  116,  115,  115,  114,  114,  114,
       148628  + /*  1480 */   113,  415,  169,  452,  170,  416,  171,  374,  372,  438,
       148629  + /*  1490 */    36, 1318,  177,  225, 1381,   87,  458,  524, 1403,  316,
       148630  + /*  1500 */   257,  105,  527,  227,    4,  182,  461,  160,  320,  228,
       148631  + /*  1510 */   377, 1175,  476,  229, 1227, 1226,  405, 1225,  530, 1218,
       148632  + /*  1520 */   961,  378, 1199, 1198,  827,  332,  103,  103, 1197,  407,
       148633  + /*  1530 */     8, 1217, 1502,  104,  487,  416,  537,  536,  281,  282,
       148634  + /*  1540 */   951,  416,  490, 1268,  496,   92,  341,  243, 1269,  343,
       148635  + /*  1550 */   244, 1267,  122,  524,  345, 1461,  515,  288,  526,   10,
       148636  + /*  1560 */   354, 1266, 1460,  352,  504, 1250,   99, 1367,   94,  503,
       148637  + /*  1570 */   499,  951,  951,  953,  954,   27,  961,  347, 1249,  194,
       148638  + /*  1580 */   251,  358,  103,  103,  359, 1181,   34,  538, 1110,  104,
       148639  + /*  1590 */   255,  416,  537,  536,  286,  252,  951,  254,  539,  149,
       148640  + /*  1600 */  1170, 1419, 1165, 1420, 1418,  150, 1417,  135,  279,  785,
       148641  + /*  1610 */   151,  417, 1195,  196,  290,  210,  386, 1194,  269,  387,
       148642  + /*  1620 */   162, 1021,  133,   77, 1192, 1019,  935,  951,  951,  953,
       148643  + /*  1630 */   954,   27, 1479, 1104,  418,  164,  153,  268,  217,  166,
       148644  + /*  1640 */   859,  306,  366,  366,  365,  253,  363,  220, 1035,  798,
       148645  + /*  1650 */   172,  939,  105,  527,  155,    4,  394,  174,  396,  156,
       148646  + /*  1660 */    83, 1038,  213,   84,  294,   85,   86,  223,  222,  530,
       148647  + /*  1670 */  1034,  144,  293,   18,  224,  315,  241, 1027, 1145,  178,
       148648  + /*  1680 */   457,  226,  179,   37,  800,  334,  462,  230,  328,  466,
       148649  + /*  1690 */   180,  471,  416,   88,   19,   20,   89,  280,  838,  158,
       148650  + /*  1700 */   191,   90,  215,  478,  524, 1097,  204,  192,  987,   91,
       148651  + /*  1710 */   152, 1070,   39,  154, 1071,  504,  486,   40,  489,  205,
       148652  + /*  1720 */   505,  260,  105,  527,  214,    4,  908,  961,  262,  183,
       148653  + /*  1730 */   240,   21,  903,  103,  103,  107,   22, 1086,   23,  530,
       148654  + /*  1740 */   104, 1088,  416,  537,  536,   24, 1093,  951,   25, 1074,
       148655  + /*  1750 */  1090, 1094,    7,   33,  511,  186,   26, 1002,  385,   95,
       148656  + /*  1760 */   988,  986,  416,  288,  526,  990, 1044,  246, 1043,  247,
       148657  + /*  1770 */   991,   28,   41,  106,  524,  956,  810,   29,  951,  951,
       148658  + /*  1780 */   953,  954,   27,  531,  361,  504,  423,  248,  869,  249,
       148659  + /*  1790 */   503, 1495,  364, 1105, 1161, 1494, 1161,  961, 1161, 1161,
       148660  + /*  1800 */  1161, 1161, 1161,  103,  103, 1161, 1161, 1161, 1161, 1161,
       148661  + /*  1810 */   104, 1161,  416,  537,  536, 1104,  418,  951, 1161,  268,
       148662  + /*  1820 */  1161, 1161, 1161, 1161,  366,  366,  365,  253,  363, 1161,
       148663  + /*  1830 */  1161,  798, 1161, 1161, 1161, 1161,  105,  527, 1161,    4,
       148664  + /*  1840 */  1161, 1161, 1161, 1161,  213, 1161,  294, 1161,  951,  951,
       148665  + /*  1850 */   953,  954,   27,  530,  293, 1161, 1161, 1161, 1161, 1161,
       148666  + /*  1860 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148667  + /*  1870 */  1161, 1161, 1161, 1161, 1161, 1161,  416, 1161, 1161, 1161,
       148668  + /*  1880 */  1161, 1161, 1161, 1161,  215, 1161, 1161, 1161,  524, 1161,
       148669  + /*  1890 */  1161, 1161,  152, 1161, 1161,  154,  105,  527, 1161,    4,
       148670  + /*  1900 */  1161, 1161, 1161, 1161, 1161, 1161,  214, 1161, 1161, 1161,
       148671  + /*  1910 */  1161,  961, 1161,  530, 1161, 1161, 1161,  103,  103,  880,
       148672  + /*  1920 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
       148673  + /*  1930 */  1161,  951, 1161, 1161, 1161, 1161,  416, 1161, 1161, 1161,
       148674  + /*  1940 */   385, 1161, 1161, 1161, 1161,  288,  526, 1161,  524, 1161,
       148675  + /*  1950 */  1161, 1161, 1161, 1161, 1161, 1161,   97,  527, 1161,    4,
       148676  + /*  1960 */  1161, 1161,  951,  951,  953,  954,   27, 1161,  423, 1161,
       148677  + /*  1970 */  1161,  961, 1161,  530, 1161, 1161, 1161,  103,  103, 1161,
       148678  + /*  1980 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
       148679  + /*  1990 */  1161,  951,  268, 1161, 1161, 1161,  416,  366,  366,  365,
       148680  + /*  2000 */   253,  363, 1161, 1161,  798, 1161, 1161, 1161,  524, 1161,
       148681  + /*  2010 */  1161, 1161, 1161, 1161, 1161, 1161, 1161,  213, 1161,  294,
       148682  + /*  2020 */  1161, 1161,  951,  951,  953,  954,   27,  293, 1161, 1161,
       148683  + /*  2030 */  1161,  961, 1161, 1161, 1161, 1161, 1161,  103,  103, 1161,
       148684  + /*  2040 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
       148685  + /*  2050 */  1161,  951, 1161, 1161, 1161, 1161, 1161,  215, 1161, 1161,
       148686  + /*  2060 */  1161, 1161, 1161, 1161, 1161,  152, 1161, 1161,  154, 1161,
       148687  + /*  2070 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,  214,
       148688  + /*  2080 */  1161, 1161,  951,  951,  953,  954,   27, 1161, 1161, 1161,
       148689  + /*  2090 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148690  + /*  2100 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148691  + /*  2110 */  1161, 1161, 1161,  385, 1161, 1161, 1161, 1161,  288,  526,
       148692  + /*  2120 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148693  + /*  2130 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
       148694  + /*  2140 */  1161,  423,
147866 148695   };
147867 148696   static const YYCODETYPE yy_lookahead[] = {
147868         - /*     0 */   168,  163,  184,  238,  239,  240,  163,  163,  155,  156,
147869         - /*    10 */   157,  158,  159,  160,  163,  202,  203,  187,  165,   19,
147870         - /*    20 */   167,  163,  184,  185,  259,  202,  203,  174,  184,  185,
147871         - /*    30 */   174,   31,  238,  239,  240,  184,  185,   22,   23,   39,
147872         - /*    40 */   216,   26,  218,   43,   44,   45,   46,   47,   48,   49,
147873         - /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  174,  206,
147874         - /*    60 */   207,  163,  206,  207,  220,  163,  163,  163,  238,  239,
147875         - /*    70 */   240,   59,  219,  229,  231,  219,  183,  245,  174,  223,
147876         - /*    80 */   224,  249,  184,  185,  191,  232,  184,  185,  184,  185,
147877         - /*    90 */   206,  207,   92,   93,   94,   95,   96,   97,   98,   99,
147878         - /*   100 */   100,  101,  102,  219,  102,   81,   91,  163,   96,   97,
147879         - /*   110 */   206,  207,   19,  275,  276,  262,  104,  105,  106,  107,
147880         - /*   120 */   163,  109,  220,  219,  220,  184,  275,  269,  277,  117,
147881         - /*   130 */   187,  229,   19,  229,  101,  102,   43,   44,   45,   46,
147882         - /*   140 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
147883         - /*   150 */    57,  127,  128,  141,  184,  143,   43,   44,   45,   46,
147884         - /*   160 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
147885         - /*   170 */    57,  268,  269,  275,  276,  197,   83,  233,   85,  163,
147886         - /*   180 */    67,  238,  239,  240,  134,   92,   93,   94,   95,   96,
147887         - /*   190 */    97,   98,   99,  100,  101,  102,   19,   54,   55,   56,
147888         - /*   200 */    57,   58,  152,   26,  247,   92,   93,   94,   95,   96,
147889         - /*   210 */    97,   98,   99,  100,  101,  102,   54,   55,   56,   57,
147890         - /*   220 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
147891         - /*   230 */    53,   54,   55,   56,   57,   92,   93,   94,   95,   96,
147892         - /*   240 */    97,   98,   99,  100,  101,  102,   69,   96,   97,   98,
147893         - /*   250 */    99,  100,  101,  102,   92,   93,   94,   95,   96,   97,
147894         - /*   260 */    98,   99,  100,  101,  102,   73,  179,  180,  181,   92,
147895         - /*   270 */    93,   94,   95,   96,   97,   98,   99,  100,  101,  102,
147896         - /*   280 */   163,  191,  192,  163,   98,   99,  100,  101,  102,   19,
147897         - /*   290 */   200,  179,  180,  181,   24,  175,   92,   93,   94,   95,
147898         - /*   300 */    96,   97,   98,   99,  100,  101,  102,  163,  116,  117,
147899         - /*   310 */   118,   22,  163,   43,   44,   45,   46,   47,   48,   49,
147900         - /*   320 */    50,   51,   52,   53,   54,   55,   56,   57,  157,  158,
147901         - /*   330 */   159,  160,  163,  184,  185,  163,  165,   59,  167,   46,
147902         - /*   340 */    90,   76,   11,  174,   73,  174,   19,  198,   59,   19,
147903         - /*   350 */    72,   86,   81,  184,  185,  234,  106,   96,   97,  163,
147904         - /*   360 */   110,  182,   92,   93,   94,   95,   96,   97,   98,   99,
147905         - /*   370 */   100,  101,  102,   46,  230,  206,  207,  206,  207,  163,
147906         - /*   380 */   184,  185,   19,  105,  106,  107,   23,  116,  219,  220,
147907         - /*   390 */   219,  141,  142,  143,  105,  106,  107,  104,  127,  128,
147908         - /*   400 */   184,  185,  141,  232,  143,   59,   43,   44,   45,   46,
147909         - /*   410 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
147910         - /*   420 */    57,  158,  108,  160,   59,  111,  112,  113,  165,  250,
147911         - /*   430 */   167,  104,  102,  262,  255,  121,  220,  174,  108,  109,
147912         - /*   440 */   110,  111,  112,  113,  114,  229,  182,  120,  117,  118,
147913         - /*   450 */   120,  105,  106,  107,  163,   92,   93,   94,   95,   96,
147914         - /*   460 */    97,   98,   99,  100,  101,  102,  163,   22,   59,  206,
147915         - /*   470 */   207,  106,  163,   26,  171,   19,  163,  193,  175,   23,
147916         - /*   480 */   163,   22,  219,  206,  207,  139,  163,   22,   59,  182,
147917         - /*   490 */   117,  118,   59,  184,  185,  232,  219,  184,  185,   43,
147918         - /*   500 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
147919         - /*   510 */    54,   55,   56,   57,  105,  106,  107,  108,   59,  255,
147920         - /*   520 */   111,  112,  113,   90,   59,  262,   22,   98,  174,   12,
147921         - /*   530 */   121,  208,  163,  220,  105,  106,  107,  163,  105,  106,
147922         - /*   540 */    22,   96,   97,  110,   27,  238,  239,  240,   92,   93,
147923         - /*   550 */    94,   95,   96,   97,   98,   99,  100,  101,  102,   42,
147924         - /*   560 */   206,  207,  115,   59,  105,  106,  107,  163,   19,   59,
147925         - /*   570 */   163,  106,   23,  219,  141,  142,  143,   59,  163,  205,
147926         - /*   580 */    63,   59,   72,   22,  124,   59,  163,  270,  234,  129,
147927         - /*   590 */    73,  163,   43,   44,   45,   46,   47,   48,   49,   50,
147928         - /*   600 */    51,   52,   53,   54,   55,   56,   57,  184,  185,  105,
147929         - /*   610 */   106,  107,  184,  185,  163,  105,  106,  107,  265,  266,
147930         - /*   620 */    59,  198,  225,  105,  106,  107,  198,  105,  106,  107,
147931         - /*   630 */   163,  105,  106,  107,  163,  184,  185,   46,   47,   48,
147932         - /*   640 */    49,   92,   93,   94,   95,   96,   97,   98,   99,  100,
147933         - /*   650 */   101,  102,  163,  163,  132,  184,  185,  163,  132,  163,
147934         - /*   660 */   256,   19,  163,  163,  163,   23,  105,  106,  107,  198,
147935         - /*   670 */   163,  220,  205,  184,  185,  163,   35,   81,  184,  185,
147936         - /*   680 */   184,  185,  163,  184,  185,   43,   44,   45,   46,   47,
147937         - /*   690 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
147938         - /*   700 */   163,  110,  163,  184,  185,  109,  205,   66,  163,   59,
147939         - /*   710 */   163,   21,  205,   16,  174,   74,  220,  198,  163,  220,
147940         - /*   720 */   230,  184,  185,  127,  128,  180,  181,  180,  181,  163,
147941         - /*   730 */   175,  242,  174,  233,   92,   93,   94,   95,   96,   97,
147942         - /*   740 */    98,   99,  100,  101,  102,  233,  206,  207,   26,  163,
147943         - /*   750 */   195,  207,  197,   26,   19,  105,  106,  107,   23,  219,
147944         - /*   760 */   119,  260,   26,  219,  206,  207,  174,   19,  174,  230,
147945         - /*   770 */    80,  174,  163,  174,   77,  163,   79,  219,   43,   44,
147946         - /*   780 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
147947         - /*   790 */    55,   56,   57,  248,   12,  248,  184,  185,  206,  207,
147948         - /*   800 */   206,  207,  112,  206,  207,  206,  207,  206,  207,   27,
147949         - /*   810 */   163,  219,  123,  219,  125,  126,  219,  208,  219,  163,
147950         - /*   820 */   219,   22,   23,   23,   42,   26,   26,   92,   93,   94,
147951         - /*   830 */    95,   96,   97,   98,   99,  100,  101,  102,  163,  149,
147952         - /*   840 */   184,  185,  163,  107,  163,   63,  149,   19,  163,  127,
147953         - /*   850 */   128,   23,   22,  105,   24,  116,  117,  118,  131,  184,
147954         - /*   860 */   185,  163,  163,  184,  185,  184,  185,   19,  132,  184,
147955         - /*   870 */   185,   43,   44,   45,   46,   47,   48,   49,   50,   51,
147956         - /*   880 */    52,   53,   54,   55,   56,   57,  146,  163,  148,   59,
147957         - /*   890 */    91,   43,   44,   45,   46,   47,   48,   49,   50,   51,
147958         - /*   900 */    52,   53,   54,   55,   56,   57,  208,  107,  184,  185,
147959         - /*   910 */     7,    8,    9,  116,  117,  118,  163,  163,  163,   24,
147960         - /*   920 */    92,   93,   94,   95,   96,   97,   98,   99,  100,  101,
147961         - /*   930 */   102,   29,  132,  163,  163,   33,  106,  184,  185,  163,
147962         - /*   940 */    92,   93,   94,   95,   96,   97,   98,   99,  100,  101,
147963         - /*   950 */   102,  163,  163,  163,   59,  184,  185,  163,   22,  163,
147964         - /*   960 */   184,  185,  177,  178,  163,  163,  163,   65,  163,  199,
147965         - /*   970 */   163,   26,  184,  185,  184,  185,  163,  163,  184,  185,
147966         - /*   980 */   184,  185,  163,   98,  163,  184,  185,  184,  185,  184,
147967         - /*   990 */   185,  184,  185,  252,  205,  147,  163,   61,  184,  185,
147968         - /*  1000 */   163,  106,  163,  184,  185,  163,  163,  205,  163,  124,
147969         - /*  1010 */   163,  256,  163,  163,  129,   19,  163,  184,  185,  163,
147970         - /*  1020 */   199,  184,  185,  184,  185,   23,  184,  185,   26,  184,
147971         - /*  1030 */   185,  184,  185,  184,  185,   19,  163,  184,  185,   43,
147972         - /*  1040 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
147973         - /*  1050 */    54,   55,   56,   57,  163,  163,  163,  184,  185,   43,
147974         - /*  1060 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
147975         - /*  1070 */    54,   55,   56,   57,   16,  184,  185,  184,  185,  163,
147976         - /*  1080 */   163,   22,   23,  138,  163,   19,  163,  231,   92,   93,
147977         - /*  1090 */    94,   95,   96,   97,   98,   99,  100,  101,  102,  256,
147978         - /*  1100 */   163,  184,  185,  163,  163,  184,  185,  163,   92,   93,
147979         - /*  1110 */    94,   95,   96,   97,   98,   99,  100,  101,  102,  163,
147980         - /*  1120 */    59,  184,  185,  163,  184,  185,  177,  178,  184,  185,
147981         - /*  1130 */   163,  208,  163,  237,  163,   77,  163,   79,  163,   15,
147982         - /*  1140 */   184,  185,  237,  147,  184,  185,   24,  231,  153,  154,
147983         - /*  1150 */    91,  184,  185,  184,  185,  184,  185,  184,  185,  184,
147984         - /*  1160 */   185,   22,   23,   19,  163,  127,  128,  106,   24,  273,
147985         - /*  1170 */   271,  105,  231,  274,  263,  264,  223,  224,  273,   22,
147986         - /*  1180 */   118,   24,   23,   19,   60,   26,  163,   43,   44,   45,
147987         - /*  1190 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
147988         - /*  1200 */    56,   57,  140,   23,   22,  163,   26,   43,   44,   45,
147989         - /*  1210 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
147990         - /*  1220 */    56,   57,   23,  211,   23,   26,   31,   26,   23,   22,
147991         - /*  1230 */    91,   26,  231,  221,   39,   53,   92,   93,   94,   95,
147992         - /*  1240 */    96,   97,   98,   99,  100,  101,  102,   23,   23,  163,
147993         - /*  1250 */    26,   26,  130,   59,  109,  110,   92,   93,   94,   95,
147994         - /*  1260 */    96,   97,   98,   99,  100,  101,  102,   23,    7,    8,
147995         - /*  1270 */    26,  110,   23,   59,   23,   26,   19,   26,  141,   23,
147996         - /*  1280 */   143,  120,   26,  141,  163,  143,   23,   23,  163,   26,
147997         - /*  1290 */    26,  163,  163,  163,  163,  163,   19,  163,  163,  193,
147998         - /*  1300 */   106,   44,   45,   46,   47,   48,   49,   50,   51,   52,
147999         - /*  1310 */    53,   54,   55,   56,   57,  163,  193,  163,  163,  163,
148000         - /*  1320 */   106,  163,   45,   46,   47,   48,   49,   50,   51,   52,
148001         - /*  1330 */    53,   54,   55,   56,   57,  163,  163,  130,  222,  163,
148002         - /*  1340 */   163,  203,  163,   19,   20,  251,   22,  163,  163,   92,
148003         - /*  1350 */    93,   94,   95,   96,   97,   98,   99,  100,  101,  102,
148004         - /*  1360 */    36,  163,  209,  163,  261,  163,  163,  161,  222,   92,
148005         - /*  1370 */    93,   94,   95,   96,   97,   98,   99,  100,  101,  102,
148006         - /*  1380 */   210,  213,  222,   59,  222,  222,  182,  213,  213,  196,
148007         - /*  1390 */   257,  226,  226,   19,   20,   71,   22,  257,  188,  187,
148008         - /*  1400 */   192,  212,  187,  187,  226,   81,  210,  166,   60,  261,
148009         - /*  1410 */    36,  244,  130,  170,   90,  170,   38,  170,  139,  104,
148010         - /*  1420 */    96,   97,   48,  236,   22,  235,   43,  103,  201,  105,
148011         - /*  1430 */   106,  107,  138,   59,  110,  247,  213,   18,  204,  258,
148012         - /*  1440 */   204,  258,  204,  204,  170,   71,   18,  169,  213,  236,
148013         - /*  1450 */   213,  127,  128,  235,  201,  201,   82,  170,  169,  213,
148014         - /*  1460 */   146,   87,   62,  254,   90,  141,  142,  143,  144,  145,
148015         - /*  1470 */    96,   97,  253,  170,  169,   22,  170,  103,  169,  105,
148016         - /*  1480 */   106,  107,  189,  170,  110,  169,  189,  186,   19,   20,
148017         - /*  1490 */   104,   22,  194,  186,  186,   64,  115,  186,  194,  189,
148018         - /*  1500 */   188,  102,  133,  186,  186,   36,  186,  104,  189,   19,
148019         - /*  1510 */    20,  246,   22,  246,  189,  141,  142,  143,  144,  145,
148020         - /*  1520 */     0,    1,    2,  228,  228,    5,   36,  227,   59,  227,
148021         - /*  1530 */    10,   11,   12,   13,   14,  170,   84,   17,  134,  216,
148022         - /*  1540 */    71,  272,  270,   22,  137,  217,   22,  216,  227,   59,
148023         - /*  1550 */    30,   82,   32,  217,  228,  228,   87,  227,  170,   90,
148024         - /*  1560 */    40,   71,  146,  241,  215,   96,   97,  214,  136,  135,
148025         - /*  1570 */   213,   25,  103,   26,  105,  106,  107,  243,  173,  110,
148026         - /*  1580 */    90,  172,   13,    6,  164,  164,   96,   97,   98,  162,
148027         - /*  1590 */    70,  162,  162,  103,  176,  105,  106,  107,   78,  267,
148028         - /*  1600 */   110,   81,  267,  264,  182,  182,  182,  182,   88,  176,
148029         - /*  1610 */   141,  142,  143,  144,  145,  176,  190,    4,  182,  182,
148030         - /*  1620 */   182,   19,   20,  182,   22,  190,    3,   22,  151,   15,
148031         - /*  1630 */    89,  141,  142,  143,  144,  145,   16,  128,   36,   23,
148032         - /*  1640 */    23,  139,  122,   24,  119,  131,   20,  127,  128,  133,
148033         - /*  1650 */    16,    1,  140,  131,  119,   61,  139,   53,   37,   53,
148034         - /*  1660 */    53,   59,   53,  119,  105,   34,  130,    1,    5,   22,
148035         - /*  1670 */   150,  104,  149,   71,   26,   75,   68,   41,   68,  130,
148036         - /*  1680 */   104,   24,   20,   19,   82,  120,  114,   22,   28,   87,
148037         - /*  1690 */    22,   67,   90,   22,   67,   23,   22,   22,   96,   97,
148038         - /*  1700 */    67,   23,  138,   22,   37,  103,  153,  105,  106,  107,
148039         - /*  1710 */     1,    2,  110,   23,    5,   23,   23,   26,   22,   10,
148040         - /*  1720 */    11,   12,   13,   14,   24,   23,   17,   22,   24,  130,
148041         - /*  1730 */    23,   19,   20,   23,   22,  105,   22,   34,   85,   30,
148042         - /*  1740 */    34,   32,   26,  141,  142,  143,  144,  145,   36,   40,
148043         - /*  1750 */   132,   34,   75,   83,   23,   44,   24,   34,   23,   26,
148044         - /*  1760 */    26,   19,   20,   23,   22,   26,   23,   23,   23,   23,
148045         - /*  1770 */    22,   59,   11,   22,   22,   26,   23,   23,   36,   70,
148046         - /*  1780 */    22,   22,  124,   71,  130,  130,  130,   78,   23,  130,
148047         - /*  1790 */    81,   15,    1,  278,  278,  278,  278,   88,  278,  278,
148048         - /*  1800 */   278,   59,   90,  278,  278,  278,  278,  278,   96,   97,
148049         - /*  1810 */   278,  278,  278,   71,  278,  103,  278,  105,  106,  107,
148050         - /*  1820 */   278,  278,  110,  278,  278,  278,  278,  278,  278,  278,
148051         - /*  1830 */   278,  122,   90,  278,  278,  278,  127,  128,   96,   97,
148052         - /*  1840 */   278,  278,  278,  278,  278,  103,  278,  105,  106,  107,
148053         - /*  1850 */   278,  278,  110,  141,  142,  143,  144,  145,  278,  150,
148054         - /*  1860 */   278,  278,  278,    5,  278,  278,  278,  278,   10,   11,
148055         - /*  1870 */    12,   13,   14,  278,  278,   17,  278,  278,  278,  278,
148056         - /*  1880 */   278,  278,  278,  141,  142,  143,  144,  145,   30,  278,
148057         - /*  1890 */    32,  278,  278,  278,  278,  278,  278,  278,   40,  278,
148058         - /*  1900 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148059         - /*  1910 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148060         - /*  1920 */   278,  278,  278,  278,  278,  278,  278,  278,   70,  278,
148061         - /*  1930 */   278,  278,  278,  278,  278,  278,   78,  278,  278,   81,
148062         - /*  1940 */   278,  278,  278,  278,  278,  278,   88,  278,  278,  278,
148063         - /*  1950 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148064         - /*  1960 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148065         - /*  1970 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148066         - /*  1980 */   122,  278,  278,  278,  278,  127,  128,  278,  278,  278,
148067         - /*  1990 */   278,  278,  278,  278,  278,  278,  278,  278,  278,  278,
148068         - /*  2000 */   278,  278,  278,  278,  278,  278,  278,  278,  150,  278,
148069         - /*  2010 */   278,  278,  278,  278,  278,  278,  278,  278,  278,
148070         -};
148071         -#define YY_SHIFT_COUNT    (523)
       148697  + /*     0 */   184,  184,  259,  260,  261,  259,  260,  261,  176,  177,
       148698  + /*    10 */   178,  179,  180,  181,  184,  208,  212,  213,  186,   19,
       148699  + /*    20 */   188,  205,  206,  280,  205,  221,   22,  195,   24,  195,
       148700  + /*    30 */   208,   31,  195,  205,   29,  205,  206,  255,   33,   39,
       148701  + /*    40 */   200,  201,  202,   43,   44,   45,   46,   47,   48,   49,
       148702  + /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  205,  227,
       148703  + /*    60 */   228,  227,  228,   59,  227,  228,  259,  260,  261,  252,
       148704  + /*    70 */    65,  241,  240,  184,  240,  223,  224,  240,  244,  245,
       148705  + /*    80 */   250,  259,  260,  261,   19,  253,   54,   55,   56,   57,
       148706  + /*    90 */    58,  184,  255,  184,  205,  206,   96,   97,   98,   99,
       148707  + /*   100 */   100,  101,  102,  103,  104,  105,  106,   46,   47,   48,
       148708  + /*   110 */    49,   46,  296,  297,  110,  283,   19,   96,   97,   98,
       148709  + /*   120 */    99,  100,  101,  102,  103,  104,  105,  106,   96,   97,
       148710  + /*   130 */    98,   99,  100,  101,  102,  103,  104,  105,  106,   94,
       148711  + /*   140 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
       148712  + /*   150 */    53,   54,   55,   56,   57,  110,  106,   73,  251,  114,
       148713  + /*   160 */    73,  178,  179,  180,  181,   59,  184,  292,   81,  186,
       148714  + /*   170 */   295,  188,  218,  108,   19,  114,  184,   11,  195,  184,
       148715  + /*   180 */    83,  184,   85,   54,   55,   56,   57,  205,  206,  124,
       148716  + /*   190 */   145,  146,  147,   96,   97,   98,   99,  100,  101,  102,
       148717  + /*   200 */   103,  104,  105,  106,  120,  121,  122,  120,  102,   81,
       148718  + /*   210 */   227,  228,  220,   19,   16,  109,  110,  111,  131,  132,
       148719  + /*   220 */    26,  184,  184,  240,  229,   96,   97,   98,   99,  100,
       148720  + /*   230 */   101,  102,  103,  104,  105,  106,  253,   43,   44,   45,
       148721  + /*   240 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148722  + /*   250 */    56,   57,  100,  101,  102,  103,  104,  105,  106,  131,
       148723  + /*   260 */   132,  106,  127,   69,  129,  130,  283,  112,  113,  114,
       148724  + /*   270 */   115,  116,  117,  118,   81,   77,   76,   79,  296,  124,
       148725  + /*   280 */   298,  203,  184,   19,   84,   59,   86,  121,  122,   89,
       148726  + /*   290 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148727  + /*   300 */   106,  184,   35,  205,  206,   22,  113,   43,   44,   45,
       148728  + /*   310 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148729  + /*   320 */    56,   57,  205,  206,  131,  132,  100,  101,  291,  292,
       148730  + /*   330 */   114,   67,  295,   66,  108,  109,  110,  111,  138,  113,
       148731  + /*   340 */   124,   74,   59,  179,  184,  181,  184,  121,   22,  271,
       148732  + /*   350 */   186,   19,  188,  184,  276,   59,   24,  184,  241,  195,
       148733  + /*   360 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148734  + /*   370 */   106,  145,   59,  147,  184,   43,   44,   45,   46,   47,
       148735  + /*   380 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
       148736  + /*   390 */   123,  227,  228,  110,  296,  297,   22,   23,  184,  102,
       148737  + /*   400 */   103,  104,  105,  106,  240,  109,  110,  111,  112,  195,
       148738  + /*   410 */   204,  115,  116,  117,   22,  184,  226,  253,  212,  205,
       148739  + /*   420 */   206,  125,  109,  110,  111,   22,  100,  101,   96,   97,
       148740  + /*   430 */    98,   99,  100,  101,  102,  103,  104,  105,  106,  184,
       148741  + /*   440 */    59,  227,  228,  121,  122,   59,  277,  283,   19,  289,
       148742  + /*   450 */   290,   59,   23,   76,  240,  241,  143,   76,   72,  189,
       148743  + /*   460 */   205,  206,   59,   86,  250,   84,   89,   86,  203,   95,
       148744  + /*   470 */    89,  281,   43,   44,   45,   46,   47,   48,   49,   50,
       148745  + /*   480 */    51,   52,   53,   54,   55,   56,   57,  227,  228,  184,
       148746  + /*   490 */   109,  110,  111,   12,  184,  109,  110,  111,  184,  184,
       148747  + /*   500 */   240,  109,  110,  111,  184,  195,  214,   59,   27,  184,
       148748  + /*   510 */   205,  206,  109,  110,  111,  205,  206,  184,  263,  138,
       148749  + /*   520 */   205,  206,  184,   42,   22,   96,   97,   98,   99,  100,
       148750  + /*   530 */   101,  102,  103,  104,  105,  106,  266,  227,  228,   59,
       148751  + /*   540 */   270,  276,   94,   66,   63,   19,  241,   22,   26,   23,
       148752  + /*   550 */   240,  241,   72,   59,   73,  250,  241,  109,  110,   82,
       148753  + /*   560 */    22,   59,  114,  223,  224,  250,  252,   59,   91,   43,
       148754  + /*   570 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       148755  + /*   580 */    54,   55,   56,   57,   59,  184,   26,   59,  268,  109,
       148756  + /*   590 */   110,  111,  184,  145,  146,  147,  112,   59,  203,  115,
       148757  + /*   600 */   116,  117,  277,  109,  110,  111,  205,  206,  195,  125,
       148758  + /*   610 */   277,  109,  110,  111,  100,  101,  139,  109,  110,  111,
       148759  + /*   620 */   219,  184,   96,   97,   98,   99,  100,  101,  102,  103,
       148760  + /*   630 */   104,  105,  106,  111,  109,  110,  111,  109,  110,  111,
       148761  + /*   640 */   227,  228,   19,  184,  136,  184,   23,  109,  110,  111,
       148762  + /*   650 */   200,  201,  202,  240,  259,  260,  261,  195,  136,  145,
       148763  + /*   660 */   184,  147,  184,  184,  136,  214,   43,   44,   45,   46,
       148764  + /*   670 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
       148765  + /*   680 */    57,  205,  206,  205,  206,  227,  228,  184,  229,  227,
       148766  + /*   690 */   228,  131,  132,  184,   59,  219,  184,  219,  240,  291,
       148767  + /*   700 */   292,  184,  240,  295,  105,  106,   22,   23,  205,  206,
       148768  + /*   710 */    26,  184,  251,  184,  205,  206,  184,  205,  206,   96,
       148769  + /*   720 */    97,   98,   99,  100,  101,  102,  103,  104,  105,  106,
       148770  + /*   730 */   251,  219,  205,  206,  205,  206,  184,  205,  206,   19,
       148771  + /*   740 */   184,   16,  184,   23,  241,  110,  219,   21,  219,  184,
       148772  + /*   750 */   241,  219,  286,  287,  195,  184,  195,  205,  206,  201,
       148773  + /*   760 */   202,  205,  206,   43,   44,   45,   46,   47,   48,   49,
       148774  + /*   770 */    50,   51,   52,   53,   54,   55,   56,   57,  184,   95,
       148775  + /*   780 */    22,   23,  184,   26,   26,  220,  227,  228,  227,  228,
       148776  + /*   790 */   196,  184,   23,  241,   26,   26,  195,  241,  184,  240,
       148777  + /*   800 */    12,  240,   77,   26,   79,  195,   80,  290,  201,  202,
       148778  + /*   810 */   216,  184,  218,  195,  184,   27,   96,   97,   98,   99,
       148779  + /*   820 */   100,  101,  102,  103,  104,  105,  106,  269,  227,  228,
       148780  + /*   830 */    42,  184,  205,  206,  184,  184,   19,  227,  228,  192,
       148781  + /*   840 */    23,  240,  116,  196,   76,  227,  228,  120,  121,  122,
       148782  + /*   850 */   240,   63,  254,   95,   86,  205,  206,   89,  240,  184,
       148783  + /*   860 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
       148784  + /*   870 */    53,   54,   55,   56,   57,  184,  269,  184,  153,  153,
       148785  + /*   880 */   111,  184,    7,    8,    9,  184,  138,  184,  184,  196,
       148786  + /*   890 */   184,  120,  121,  122,  184,  138,  205,  206,  184,  102,
       148787  + /*   900 */   184,  184,  205,  206,  156,  136,  205,  206,  205,  206,
       148788  + /*   910 */   198,  199,  135,   96,   97,   98,   99,  100,  101,  102,
       148789  + /*   920 */   103,  104,  105,  106,  184,  128,  184,  184,  184,  254,
       148790  + /*   930 */   133,  184,  237,   19,  239,  229,  226,   23,  292,  184,
       148791  + /*   940 */   226,  295,  226,  226,  184,  205,  206,  205,  206,  205,
       148792  + /*   950 */   206,  184,  292,   19,  184,  295,  252,   43,   44,   45,
       148793  + /*   960 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148794  + /*   970 */    56,   57,  205,  206,  184,  205,  206,   43,   44,   45,
       148795  + /*   980 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       148796  + /*   990 */    56,   57,  157,  158,   26,  205,  206,  254,   26,  252,
       148797  + /*  1000 */   184,   15,  184,  184,  184,  292,  184,  252,  295,   24,
       148798  + /*  1010 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148799  + /*  1020 */   106,  205,  206,  205,  206,  205,  206,  205,  206,  184,
       148800  + /*  1030 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
       148801  + /*  1040 */   106,  184,  184,  184,   59,  184,   60,  184,  229,  184,
       148802  + /*  1050 */   205,  206,  184,  258,  184,   19,  184,   19,  184,  246,
       148803  + /*  1060 */   184,  258,  205,  206,  205,  206,  205,  206,  205,  206,
       148804  + /*  1070 */   205,  206,  184,  205,  206,  205,  206,  205,  206,  205,
       148805  + /*  1080 */   206,  205,  206,  292,  226,  151,  295,  184,  228,  294,
       148806  + /*  1090 */   184,  119,  184,  205,  206,  110,  150,  294,  152,  184,
       148807  + /*  1100 */   240,  184,   22,   23,   23,   19,  184,   26,  205,  206,
       148808  + /*  1110 */   142,  205,  206,  205,  206,  184,  198,  199,  131,  132,
       148809  + /*  1120 */   205,  206,  205,  206,   22,   19,   24,  205,  206,   43,
       148810  + /*  1130 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       148811  + /*  1140 */    54,   55,   56,   57,  184,  109,  184,  109,  184,   43,
       148812  + /*  1150 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       148813  + /*  1160 */    54,   55,   56,   57,   46,  205,  206,  205,  206,  205,
       148814  + /*  1170 */   206,  232,  184,  184,  184,   95,  184,  284,  285,  244,
       148815  + /*  1180 */   245,  242,   96,   97,   98,   99,  100,  101,  102,  103,
       148816  + /*  1190 */   104,  105,  106,  205,  206,  205,  206,  205,  206,  184,
       148817  + /*  1200 */    22,  184,   96,   97,   98,   99,  100,  101,  102,  103,
       148818  + /*  1210 */   104,  105,  106,  184,   24,   23,  184,  184,   26,  184,
       148819  + /*  1220 */   205,  206,  205,  206,  184,   31,  108,  128,   22,  122,
       148820  + /*  1230 */   184,   53,  133,   39,  205,  206,   22,  151,  205,  206,
       148821  + /*  1240 */   205,  206,  113,  114,   23,  205,  206,   26,   59,   23,
       148822  + /*  1250 */    23,  144,   26,   26,  184,   23,   23,   19,   26,   26,
       148823  + /*  1260 */     7,    8,   24,   23,  214,   23,   26,   61,   26,   59,
       148824  + /*  1270 */    23,   23,   23,   26,   26,   26,  145,   19,  147,   59,
       148825  + /*  1280 */   184,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       148826  + /*  1290 */    52,   53,   54,   55,   56,   57,  145,   23,  147,  110,
       148827  + /*  1300 */    26,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       148828  + /*  1310 */    52,   53,   54,   55,   56,   57,   23,  184,  184,   26,
       148829  + /*  1320 */   110,  184,  184,  184,  134,  184,  184,  184,  184,  184,
       148830  + /*  1330 */   110,  184,  184,  184,   96,   97,   98,   99,  100,  101,
       148831  + /*  1340 */   102,  103,  104,  105,  106,  184,  184,  184,  134,  300,
       148832  + /*  1350 */   184,  243,  184,  184,   96,   97,   98,   99,  100,  101,
       148833  + /*  1360 */   102,  103,  104,  105,  106,  184,  184,  184,  184,  184,
       148834  + /*  1370 */   224,  184,  282,  273,   19,  272,  203,  182,  243,  243,
       148835  + /*  1380 */   230,  209,  278,  243,  231,  208,  265,  278,  234,  234,
       148836  + /*  1390 */   234,  217,  213,   60,   19,  243,  208,  237,  233,   44,
       148837  + /*  1400 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
       148838  + /*  1410 */    55,   56,   57,  208,  247,  187,  134,  247,  247,   38,
       148839  + /*  1420 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
       148840  + /*  1430 */    55,   56,   57,  237,  231,  191,  191,  279,  279,  282,
       148841  + /*  1440 */   143,  191,  108,  268,   22,   19,   20,  256,   22,   43,
       148842  + /*  1450 */   257,   96,   97,   98,   99,  100,  101,  102,  103,  104,
       148843  + /*  1460 */   105,  106,   36,  222,  142,  234,   18,  191,  225,   18,
       148844  + /*  1470 */   190,   96,   97,   98,   99,  100,  101,  102,  103,  104,
       148845  + /*  1480 */   105,  106,  225,  191,  225,   59,  225,  257,  234,  234,
       148846  + /*  1490 */   256,  222,  222,  190,  234,  150,   62,   71,  275,  274,
       148847  + /*  1500 */   191,   19,   20,  190,   22,   22,  210,   81,  191,  190,
       148848  + /*  1510 */   210,  191,  108,  190,  207,  207,   64,  207,   36,  215,
       148849  + /*  1520 */    94,  210,  207,  209,  119,  207,  100,  101,  207,  106,
       148850  + /*  1530 */    48,  215,  207,  107,  210,  109,  110,  111,  267,  267,
       148851  + /*  1540 */   114,   59,  210,  249,  137,  108,  248,  191,  249,  248,
       148852  + /*  1550 */    88,  249,  141,   71,  248,  299,  138,  131,  132,   22,
       148853  + /*  1560 */   191,  249,  299,  237,   82,  238,  150,  262,  140,   87,
       148854  + /*  1570 */   139,  145,  146,  147,  148,  149,   94,  248,  238,  236,
       148855  + /*  1580 */    25,  235,  100,  101,  234,  194,   26,  193,   13,  107,
       148856  + /*  1590 */     6,  109,  110,  111,  264,  185,  114,  185,  183,  197,
       148857  + /*  1600 */   183,  203,  183,  203,  203,  197,  203,  211,  211,    4,
       148858  + /*  1610 */   197,    3,  203,   22,  155,   15,  288,  203,   93,  288,
       148859  + /*  1620 */   285,   23,   16,  203,  203,   23,  132,  145,  146,  147,
       148860  + /*  1630 */   148,  149,    0,    1,    2,  143,  123,    5,   24,  135,
       148861  + /*  1640 */    20,   16,   10,   11,   12,   13,   14,  137,    1,   17,
       148862  + /*  1650 */   135,  144,   19,   20,  123,   22,   61,  143,   37,  123,
       148863  + /*  1660 */    53,  109,   30,   53,   32,   53,   53,  134,   34,   36,
       148864  + /*  1670 */     1,    5,   40,   22,  108,  153,   26,   68,   75,   68,
       148865  + /*  1680 */    41,  134,  108,   24,   20,  124,   19,  118,   23,   67,
       148866  + /*  1690 */    22,   67,   59,   22,   22,   22,   22,   67,   28,   37,
       148867  + /*  1700 */    23,  142,   70,   22,   71,   23,  157,   23,   23,   26,
       148868  + /*  1710 */    78,   23,   22,   81,   23,   82,   24,   22,   24,  134,
       148869  + /*  1720 */    87,   23,   19,   20,   92,   22,  109,   94,   23,   22,
       148870  + /*  1730 */    34,   34,  136,  100,  101,   26,   34,   85,   34,   36,
       148871  + /*  1740 */   107,   83,  109,  110,  111,   34,   90,  114,   34,   23,
       148872  + /*  1750 */    75,   75,   44,   22,   24,   26,   34,   23,  126,   26,
       148873  + /*  1760 */    23,   23,   59,  131,  132,   23,   23,   26,   23,   22,
       148874  + /*  1770 */    11,   22,   22,   22,   71,   23,   23,   22,  145,  146,
       148875  + /*  1780 */   147,  148,  149,   26,   23,   82,  154,  134,  128,  134,
       148876  + /*  1790 */    87,  134,   15,    1,  301,  134,  301,   94,  301,  301,
       148877  + /*  1800 */   301,  301,  301,  100,  101,  301,  301,  301,  301,  301,
       148878  + /*  1810 */   107,  301,  109,  110,  111,    1,    2,  114,  301,    5,
       148879  + /*  1820 */   301,  301,  301,  301,   10,   11,   12,   13,   14,  301,
       148880  + /*  1830 */   301,   17,  301,  301,  301,  301,   19,   20,  301,   22,
       148881  + /*  1840 */   301,  301,  301,  301,   30,  301,   32,  301,  145,  146,
       148882  + /*  1850 */   147,  148,  149,   36,   40,  301,  301,  301,  301,  301,
       148883  + /*  1860 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148884  + /*  1870 */   301,  301,  301,  301,  301,  301,   59,  301,  301,  301,
       148885  + /*  1880 */   301,  301,  301,  301,   70,  301,  301,  301,   71,  301,
       148886  + /*  1890 */   301,  301,   78,  301,  301,   81,   19,   20,  301,   22,
       148887  + /*  1900 */   301,  301,  301,  301,  301,  301,   92,  301,  301,  301,
       148888  + /*  1910 */   301,   94,  301,   36,  301,  301,  301,  100,  101,  102,
       148889  + /*  1920 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
       148890  + /*  1930 */   301,  114,  301,  301,  301,  301,   59,  301,  301,  301,
       148891  + /*  1940 */   126,  301,  301,  301,  301,  131,  132,  301,   71,  301,
       148892  + /*  1950 */   301,  301,  301,  301,  301,  301,   19,   20,  301,   22,
       148893  + /*  1960 */   301,  301,  145,  146,  147,  148,  149,  301,  154,  301,
       148894  + /*  1970 */   301,   94,  301,   36,  301,  301,  301,  100,  101,  301,
       148895  + /*  1980 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
       148896  + /*  1990 */   301,  114,    5,  301,  301,  301,   59,   10,   11,   12,
       148897  + /*  2000 */    13,   14,  301,  301,   17,  301,  301,  301,   71,  301,
       148898  + /*  2010 */   301,  301,  301,  301,  301,  301,  301,   30,  301,   32,
       148899  + /*  2020 */   301,  301,  145,  146,  147,  148,  149,   40,  301,  301,
       148900  + /*  2030 */   301,   94,  301,  301,  301,  301,  301,  100,  101,  301,
       148901  + /*  2040 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
       148902  + /*  2050 */   301,  114,  301,  301,  301,  301,  301,   70,  301,  301,
       148903  + /*  2060 */   301,  301,  301,  301,  301,   78,  301,  301,   81,  301,
       148904  + /*  2070 */   301,  301,  301,  301,  301,  301,  301,  301,  301,   92,
       148905  + /*  2080 */   301,  301,  145,  146,  147,  148,  149,  301,  301,  301,
       148906  + /*  2090 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148907  + /*  2100 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148908  + /*  2110 */   301,  301,  301,  126,  301,  301,  301,  301,  131,  132,
       148909  + /*  2120 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148910  + /*  2130 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148911  + /*  2140 */   301,  154,  301,  301,  301,  301,  301,  301,  301,  301,
       148912  + /*  2150 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
       148913  + /*  2160 */   301,  301,  301,  301,  301,  301,  301,  301,  301,
       148914  +};
       148915  +#define YY_SHIFT_COUNT    (540)
148072 148916   #define YY_SHIFT_MIN      (0)
148073         -#define YY_SHIFT_MAX      (1858)
       148917  +#define YY_SHIFT_MAX      (1987)
148074 148918   static const unsigned short int yy_shift_ofst[] = {
148075         - /*     0 */  1709, 1520, 1858, 1324, 1324,   24, 1374, 1469, 1602, 1712,
148076         - /*    10 */  1712, 1712,  271,    0,    0,  113, 1016, 1712, 1712, 1712,
148077         - /*    20 */  1712, 1712, 1712, 1712, 1712, 1712, 1712,   12,   12,  409,
148078         - /*    30 */   596,   24,   24,   24,   24,   24,   24,   93,  177,  270,
148079         - /*    40 */   363,  456,  549,  642,  735,  828,  848,  996, 1144, 1016,
148080         - /*    50 */  1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016,
148081         - /*    60 */  1016, 1016, 1016, 1016, 1016, 1016, 1016, 1164, 1016, 1257,
148082         - /*    70 */  1277, 1277, 1490, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
148083         - /*    80 */  1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
148084         - /*    90 */  1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
148085         - /*   100 */  1712, 1712, 1712, 1712, 1712, 1742, 1712, 1712, 1712, 1712,
148086         - /*   110 */  1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,  143,
148087         - /*   120 */   162,  162,  162,  162,  162,  204,  151,  186,  650,  690,
148088         - /*   130 */   327,  650,  261,  261,  650,  722,  722,  722,  722,  373,
148089         - /*   140 */    33,    2, 2009, 2009,  330,  330,  330,  346,  289,  278,
148090         - /*   150 */   289,  289,  517,  517,  459,  510,   15,  799,  650,  650,
148091         - /*   160 */   650,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148092         - /*   170 */   650,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148093         - /*   180 */   331,  365,  995,  995,  265,  365,   50, 1038, 2009, 2009,
148094         - /*   190 */  2009,  433,  250,  250,  504,  314,  429,  518,  522,  526,
148095         - /*   200 */   561,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148096         - /*   210 */   192,  650,  650,  650,  650,  650,  650,  650,  650,  650,
148097         - /*   220 */   650,  650,  650,  641,  641,  641,  650,  650,  650,  650,
148098         - /*   230 */   800,  650,  650,  650,  830,  650,  650,  782,  650,  650,
148099         - /*   240 */   650,  650,  650,  650,  650,  650,  739,  902,  689,  895,
148100         - /*   250 */   895,  895,  895,  736,  689,  689,  885,  445,  903, 1124,
148101         - /*   260 */   945,  748,  748, 1066,  945,  945, 1066,  447, 1002,  293,
148102         - /*   270 */  1195, 1195, 1195,  748,  740,  727,  460, 1157, 1348, 1282,
148103         - /*   280 */  1282, 1378, 1378, 1282, 1279, 1315, 1402, 1383, 1294, 1419,
148104         - /*   290 */  1419, 1419, 1419, 1282, 1428, 1294, 1294, 1315, 1402, 1383,
148105         - /*   300 */  1383, 1294, 1282, 1428, 1314, 1400, 1282, 1428, 1453, 1282,
148106         - /*   310 */  1428, 1282, 1428, 1453, 1386, 1386, 1386, 1431, 1453, 1386,
148107         - /*   320 */  1381, 1386, 1431, 1386, 1386, 1453, 1399, 1399, 1453, 1369,
148108         - /*   330 */  1403, 1369, 1403, 1369, 1403, 1369, 1403, 1282, 1404, 1452,
148109         - /*   340 */  1521, 1407, 1404, 1524, 1282, 1416, 1407, 1432, 1434, 1294,
148110         - /*   350 */  1546, 1547, 1569, 1569, 1577, 1577, 1577, 2009, 2009, 2009,
148111         - /*   360 */  2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
148112         - /*   370 */  2009, 2009, 2009,  591,  697, 1059, 1139, 1058,  797,  465,
148113         - /*   380 */  1159, 1182, 1122, 1062, 1180,  936, 1199, 1201, 1205, 1224,
148114         - /*   390 */  1225, 1244, 1061, 1145, 1261, 1161, 1194, 1249, 1251, 1256,
148115         - /*   400 */  1137, 1142, 1263, 1264, 1214, 1207, 1613, 1623, 1605, 1477,
148116         - /*   410 */  1614, 1541, 1620, 1616, 1617, 1509, 1502, 1525, 1619, 1514,
148117         - /*   420 */  1626, 1516, 1634, 1650, 1522, 1512, 1535, 1594, 1621, 1517,
148118         - /*   430 */  1604, 1606, 1607, 1609, 1544, 1559, 1631, 1536, 1666, 1663,
148119         - /*   440 */  1647, 1567, 1523, 1608, 1648, 1610, 1600, 1636, 1549, 1576,
148120         - /*   450 */  1657, 1662, 1664, 1565, 1572, 1665, 1624, 1668, 1671, 1672,
148121         - /*   460 */  1674, 1627, 1660, 1675, 1633, 1667, 1678, 1564, 1681, 1553,
148122         - /*   470 */  1690, 1692, 1691, 1693, 1696, 1700, 1702, 1705, 1704, 1599,
148123         - /*   480 */  1707, 1710, 1630, 1703, 1714, 1618, 1716, 1706, 1716, 1717,
148124         - /*   490 */  1653, 1677, 1670, 1711, 1731, 1732, 1733, 1734, 1723, 1735,
148125         - /*   500 */  1716, 1740, 1743, 1744, 1745, 1739, 1746, 1748, 1761, 1751,
148126         - /*   510 */  1752, 1753, 1754, 1758, 1759, 1749, 1658, 1654, 1655, 1656,
148127         - /*   520 */  1659, 1765, 1776, 1791,
148128         -};
148129         -#define YY_REDUCE_COUNT (372)
148130         -#define YY_REDUCE_MIN   (-235)
148131         -#define YY_REDUCE_MAX   (1441)
       148919  + /*     0 */  1814, 1632, 1987, 1426, 1426,  128, 1482, 1633, 1703, 1877,
       148920  + /*    10 */  1877, 1877,   87,    0,    0,  264, 1106, 1877, 1877, 1877,
       148921  + /*    20 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148922  + /*    30 */   226,  226,  381,  381,  296,  193,  128,  128,  128,  128,
       148923  + /*    40 */   128,  128,   97,  194,  332,  429,  526,  623,  720,  817,
       148924  + /*    50 */   914,  934, 1086, 1238, 1106, 1106, 1106, 1106, 1106, 1106,
       148925  + /*    60 */  1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106,
       148926  + /*    70 */  1106, 1106, 1258, 1106, 1355, 1375, 1375, 1817, 1877, 1877,
       148927  + /*    80 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148928  + /*    90 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148929  + /*   100 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148930  + /*   110 */  1937, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
       148931  + /*   120 */  1877, 1877, 1877, 1877,   32,  129,  129,  129,  129,  129,
       148932  + /*   130 */    21,  152,  297,  494,  726,   65,  494,  514,  514,  494,
       148933  + /*   140 */   560,  560,  560,  560,  322,  599,   50, 2142, 2142,  155,
       148934  + /*   150 */   155,  155,  313,  392,  386,  392,  392,  481,  481,  200,
       148935  + /*   160 */   480,  684,  758,  494,  494,  494,  494,  494,  494,  494,
       148936  + /*   170 */   494,  494,  494,  494,  494,  494,  494,  494,  494,  494,
       148937  + /*   180 */   494,  494,  494,  494,  768,  768,  494,  166,  377,  377,
       148938  + /*   190 */   635,  835,  835,  635,  748,  987, 2142, 2142, 2142,  448,
       148939  + /*   200 */    45,   45,  403,  484,  502,  106,  525,  508,  528,  538,
       148940  + /*   210 */   494,  494,  494,  494,  494,  494,  494,  494,  494,   84,
       148941  + /*   220 */   494,  494,  494,  494,  494,  494,  494,  494,  494,  494,
       148942  + /*   230 */   494,  494,  267,  267,  267,  494,  494,  494,  494,  769,
       148943  + /*   240 */   494,  494,  494,    4,  477,  494,  494,  788,  494,  494,
       148944  + /*   250 */   494,  494,  494,  494,  494,  494,  727,    5,  135,  985,
       148945  + /*   260 */   985,  985,  985,  522,  135,  135,  797,  326,  875,  986,
       148946  + /*   270 */   968, 1036, 1036, 1038,  968,  968, 1038,  972, 1081, 1118,
       148947  + /*   280 */  1194, 1194, 1194, 1036,  757,  757,  946,  777, 1099, 1102,
       148948  + /*   290 */  1333, 1282, 1282, 1381, 1381, 1282, 1297, 1334, 1422, 1406,
       148949  + /*   300 */  1322, 1448, 1448, 1448, 1448, 1282, 1451, 1322, 1322, 1334,
       148950  + /*   310 */  1422, 1406, 1406, 1322, 1282, 1451, 1345, 1434, 1282, 1451,
       148951  + /*   320 */  1483, 1282, 1451, 1282, 1451, 1483, 1404, 1404, 1404, 1452,
       148952  + /*   330 */  1483, 1404, 1405, 1404, 1452, 1404, 1404, 1483, 1423, 1423,
       148953  + /*   340 */  1483, 1407, 1437, 1407, 1437, 1407, 1437, 1407, 1437, 1282,
       148954  + /*   350 */  1462, 1462, 1411, 1418, 1537, 1282, 1416, 1411, 1428, 1431,
       148955  + /*   360 */  1322, 1555, 1560, 1575, 1575, 1584, 1584, 1584, 2142, 2142,
       148956  + /*   370 */  2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142,
       148957  + /*   380 */  2142, 2142, 2142, 2142,   61,  725,  374, 1080,  198,  771,
       148958  + /*   390 */   283, 1192, 1178, 1190, 1107, 1221, 1206, 1226, 1227, 1232,
       148959  + /*   400 */  1233, 1240, 1242, 1189, 1129, 1253,  216, 1210, 1247, 1248,
       148960  + /*   410 */  1249, 1131, 1151, 1274, 1293, 1220, 1214, 1605, 1608, 1591,
       148961  + /*   420 */  1459, 1600, 1525, 1606, 1598, 1602, 1494, 1492, 1513, 1614,
       148962  + /*   430 */  1504, 1620, 1510, 1625, 1647, 1515, 1507, 1531, 1595, 1621,
       148963  + /*   440 */  1514, 1607, 1610, 1612, 1613, 1536, 1552, 1634, 1533, 1669,
       148964  + /*   450 */  1666, 1651, 1566, 1522, 1609, 1650, 1611, 1603, 1639, 1547,
       148965  + /*   460 */  1574, 1659, 1664, 1667, 1561, 1569, 1668, 1622, 1671, 1672,
       148966  + /*   470 */  1665, 1673, 1624, 1670, 1674, 1630, 1662, 1677, 1559, 1681,
       148967  + /*   480 */  1682, 1549, 1684, 1685, 1683, 1688, 1690, 1692, 1691, 1695,
       148968  + /*   490 */  1694, 1585, 1698, 1705, 1617, 1696, 1707, 1596, 1709, 1697,
       148969  + /*   500 */  1702, 1704, 1711, 1652, 1675, 1658, 1708, 1676, 1656, 1714,
       148970  + /*   510 */  1726, 1731, 1730, 1729, 1733, 1722, 1734, 1709, 1737, 1738,
       148971  + /*   520 */  1742, 1743, 1741, 1745, 1747, 1759, 1749, 1750, 1752, 1753,
       148972  + /*   530 */  1751, 1755, 1757, 1660, 1653, 1655, 1657, 1661, 1761, 1777,
       148973  + /*   540 */  1792,
       148974  +};
       148975  +#define YY_REDUCE_COUNT (383)
       148976  +#define YY_REDUCE_MIN   (-257)
       148977  +#define YY_REDUCE_MAX   (1421)
148132 148978   static const short yy_reduce_ofst[] = {
148133         - /*     0 */  -147,  171,  263,  -96,  169, -144, -162, -149, -102, -156,
148134         - /*    10 */   -98,  216,  354, -170,  -57, -235,  307,  149,  423,  428,
148135         - /*    20 */   471,  313,  451,  519,  489,  496,  499,  545,  547,  555,
148136         - /*    30 */  -116,  540,  558,  592,  594,  597,  599, -206, -206, -206,
148137         - /*    40 */  -206, -206, -206, -206, -206, -206, -206, -206, -206, -206,
148138         - /*    50 */  -206, -206, -206, -206, -206, -206, -206, -206, -206, -206,
148139         - /*    60 */  -206, -206, -206, -206, -206, -206, -206, -206, -206, -206,
148140         - /*    70 */  -206, -206,  196,  309,  494,  537,  612,  656,  675,  679,
148141         - /*    80 */   681,  685,  724,  753,  771,  776,  788,  790,  794,  796,
148142         - /*    90 */   801,  803,  805,  807,  814,  819,  833,  837,  839,  842,
148143         - /*   100 */   845,  847,  849,  853,  873,  891,  893,  917,  921,  937,
148144         - /*   110 */   940,  944,  956,  960,  967,  969,  971,  973,  975, -206,
148145         - /*   120 */  -206, -206, -206, -206, -206, -206, -206, -206,  501, -168,
148146         - /*   130 */    90,  -97,   87,  112,  303,  277,  601,  277,  601,  179,
148147         - /*   140 */  -206, -206, -206, -206, -107, -107, -107,  -43,  -56,  323,
148148         - /*   150 */   500,  512, -187, -177,  317,  609,  353,  353,  120,  144,
148149         - /*   160 */   490,  539,  698,  374,  467,  507,  789,  404, -157,  755,
148150         - /*   170 */   856,  916,  843,  941,  802,  770,  923,  821, 1001, -142,
148151         - /*   180 */   264,  785,  896,  905,  899,  949, -176,  544,  911,  953,
148152         - /*   190 */  1012, -182,  -59,  -30,   16,  -22,  117,  172,  291,  369,
148153         - /*   200 */   407,  415,  566,  586,  647,  699,  754,  813,  850,  892,
148154         - /*   210 */   121, 1023, 1042, 1086, 1121, 1125, 1128, 1129, 1130, 1131,
148155         - /*   220 */  1132, 1134, 1135,  284, 1106, 1123, 1152, 1154, 1155, 1156,
148156         - /*   230 */   397, 1158, 1172, 1173, 1116, 1176, 1177, 1138, 1179,  117,
148157         - /*   240 */  1184, 1185, 1198, 1200, 1202, 1203,  741, 1094, 1153, 1146,
148158         - /*   250 */  1160, 1162, 1163,  397, 1153, 1153, 1170, 1204, 1206, 1103,
148159         - /*   260 */  1168, 1165, 1166, 1133, 1174, 1175, 1140, 1210, 1193, 1208,
148160         - /*   270 */  1212, 1215, 1216, 1178, 1167, 1189, 1196, 1241, 1148, 1243,
148161         - /*   280 */  1245, 1181, 1183, 1247, 1188, 1187, 1190, 1227, 1223, 1234,
148162         - /*   290 */  1236, 1238, 1239, 1274, 1278, 1235, 1237, 1213, 1218, 1253,
148163         - /*   300 */  1254, 1246, 1287, 1289, 1209, 1219, 1303, 1305, 1293, 1306,
148164         - /*   310 */  1309, 1313, 1316, 1297, 1301, 1307, 1308, 1298, 1310, 1311,
148165         - /*   320 */  1312, 1317, 1304, 1318, 1320, 1319, 1265, 1267, 1325, 1295,
148166         - /*   330 */  1300, 1296, 1302, 1326, 1321, 1327, 1330, 1365, 1323, 1269,
148167         - /*   340 */  1272, 1328, 1331, 1322, 1388, 1334, 1336, 1349, 1353, 1357,
148168         - /*   350 */  1405, 1409, 1420, 1421, 1427, 1429, 1430, 1332, 1335, 1339,
148169         - /*   360 */  1418, 1422, 1423, 1424, 1425, 1433, 1426, 1435, 1436, 1437,
148170         - /*   370 */  1438, 1441, 1439,
       148979  + /*     0 */  -168,  -17,  164,  214,  310, -166, -184,  -18,   98, -170,
       148980  + /*    10 */   305,  315, -163, -193, -178, -257,  395,  401,  476,  478,
       148981  + /*    20 */   512,  117,  527,  529,  503,  509,  532,  255,  552,  556,
       148982  + /*    30 */   558,  607,   37,  408,  594,  413,  462,  559,  561,  601,
       148983  + /*    40 */   610,  618, -254, -254, -254, -254, -254, -254, -254, -254,
       148984  + /*    50 */  -254, -254, -254, -254, -254, -254, -254, -254, -254, -254,
       148985  + /*    60 */  -254, -254, -254, -254, -254, -254, -254, -254, -254, -254,
       148986  + /*    70 */  -254, -254, -254, -254, -254, -254, -254, -111,  627,  650,
       148987  + /*    80 */   691,  697,  701,  703,  740,  742,  744,  767,  770,  790,
       148988  + /*    90 */   816,  818,  820,  822,  845,  857,  859,  861,  863,  865,
       148989  + /*   100 */   868,  870,  872,  874,  876,  888,  903,  906,  908,  915,
       148990  + /*   110 */   917,  922,  960,  962,  964,  988,  990,  992, 1015, 1017,
       148991  + /*   120 */  1029, 1033, 1035, 1040, -254, -254, -254, -254, -254, -254,
       148992  + /*   130 */  -254, -254, -254,  190,  270, -196,  160, -160,  450,  647,
       148993  + /*   140 */   260,  458,  260,  458,   78, -254, -254, -254, -254,  206,
       148994  + /*   150 */   206,  206,  320,  598,   -5,  675,  743, -148,  340, -125,
       148995  + /*   160 */   459,  466,  466,  693,  -93,  461,  479,  706,  710,  714,
       148996  + /*   170 */   716,  717,  169, -183,  325,  314,  704,  333,  747,  858,
       148997  + /*   180 */    -8,  819,  565,  755,  646,  660,  517,  265,  713,  791,
       148998  + /*   190 */   712,  795,  803,  918,  695,  860,  893,  935,  939, -181,
       148999  + /*   200 */  -172, -147,  -91,  -46,   -3,  162,  173,  231,  338,  437,
       149000  + /*   210 */   571,  614,  630,  651,  760,  931,  989, 1032, 1046, -218,
       149001  + /*   220 */    38, 1070, 1096, 1133, 1134, 1137, 1138, 1139, 1141, 1142,
       149002  + /*   230 */  1143, 1144,  292,  451, 1050, 1145, 1147, 1148, 1149,  813,
       149003  + /*   240 */  1161, 1162, 1163, 1108, 1049, 1166, 1168, 1146, 1169,  162,
       149004  + /*   250 */  1181, 1182, 1183, 1184, 1185, 1187, 1100, 1103, 1150, 1135,
       149005  + /*   260 */  1136, 1140, 1152,  813, 1150, 1150, 1153, 1173, 1195, 1090,
       149006  + /*   270 */  1154, 1167, 1170, 1104, 1155, 1156, 1109, 1172, 1174, 1179,
       149007  + /*   280 */  1177, 1188, 1205, 1171, 1160, 1196, 1121, 1165, 1203, 1228,
       149008  + /*   290 */  1157, 1244, 1245, 1158, 1159, 1250, 1175, 1193, 1191, 1241,
       149009  + /*   300 */  1231, 1243, 1257, 1259, 1261, 1276, 1280, 1254, 1255, 1230,
       149010  + /*   310 */  1234, 1269, 1270, 1260, 1292, 1303, 1223, 1225, 1309, 1313,
       149011  + /*   320 */  1296, 1317, 1319, 1320, 1323, 1300, 1307, 1308, 1310, 1304,
       149012  + /*   330 */  1311, 1315, 1314, 1318, 1316, 1321, 1325, 1324, 1271, 1272,
       149013  + /*   340 */  1332, 1294, 1298, 1299, 1301, 1302, 1306, 1312, 1329, 1356,
       149014  + /*   350 */  1256, 1263, 1327, 1326, 1305, 1369, 1330, 1340, 1343, 1346,
       149015  + /*   360 */  1350, 1391, 1394, 1410, 1412, 1415, 1417, 1419, 1328, 1331,
       149016  + /*   370 */  1335, 1402, 1398, 1400, 1401, 1403, 1408, 1396, 1397, 1409,
       149017  + /*   380 */  1414, 1420, 1421, 1413,
148171 149018   };
148172 149019   static const YYACTIONTYPE yy_default[] = {
148173         - /*     0 */  1500, 1500, 1500, 1346, 1129, 1235, 1129, 1129, 1129, 1346,
148174         - /*    10 */  1346, 1346, 1129, 1265, 1265, 1399, 1160, 1129, 1129, 1129,
148175         - /*    20 */  1129, 1129, 1129, 1129, 1345, 1129, 1129, 1129, 1129, 1129,
148176         - /*    30 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1271, 1129,
148177         - /*    40 */  1129, 1129, 1129, 1129, 1347, 1348, 1129, 1129, 1129, 1398,
148178         - /*    50 */  1400, 1363, 1281, 1280, 1279, 1278, 1381, 1252, 1276, 1269,
148179         - /*    60 */  1273, 1341, 1342, 1340, 1344, 1348, 1347, 1129, 1272, 1312,
148180         - /*    70 */  1326, 1311, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148181         - /*    80 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148182         - /*    90 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148183         - /*   100 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148184         - /*   110 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1320,
148185         - /*   120 */  1325, 1331, 1324, 1321, 1314, 1313, 1315, 1316, 1129, 1150,
148186         - /*   130 */  1199, 1129, 1129, 1129, 1129, 1417, 1416, 1129, 1129, 1160,
148187         - /*   140 */  1317, 1318, 1328, 1327, 1406, 1456, 1455, 1364, 1129, 1129,
148188         - /*   150 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148189         - /*   160 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148190         - /*   170 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148191         - /*   180 */  1160, 1156, 1306, 1305, 1426, 1156, 1259, 1129, 1412, 1235,
148192         - /*   190 */  1226, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148193         - /*   200 */  1129, 1129, 1129, 1129, 1403, 1401, 1129, 1129, 1129, 1129,
148194         - /*   210 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148195         - /*   220 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148196         - /*   230 */  1129, 1129, 1129, 1129, 1231, 1129, 1129, 1129, 1129, 1129,
148197         - /*   240 */  1129, 1129, 1129, 1129, 1129, 1450, 1129, 1376, 1213, 1231,
148198         - /*   250 */  1231, 1231, 1231, 1233, 1214, 1212, 1225, 1160, 1136, 1492,
148199         - /*   260 */  1275, 1254, 1254, 1489, 1275, 1275, 1489, 1174, 1470, 1171,
148200         - /*   270 */  1265, 1265, 1265, 1254, 1343, 1232, 1225, 1129, 1492, 1240,
148201         - /*   280 */  1240, 1491, 1491, 1240, 1364, 1284, 1290, 1202, 1275, 1208,
148202         - /*   290 */  1208, 1208, 1208, 1240, 1147, 1275, 1275, 1284, 1290, 1202,
148203         - /*   300 */  1202, 1275, 1240, 1147, 1380, 1486, 1240, 1147, 1354, 1240,
148204         - /*   310 */  1147, 1240, 1147, 1354, 1200, 1200, 1200, 1189, 1354, 1200,
148205         - /*   320 */  1174, 1200, 1189, 1200, 1200, 1354, 1358, 1358, 1354, 1258,
148206         - /*   330 */  1253, 1258, 1253, 1258, 1253, 1258, 1253, 1240, 1259, 1425,
148207         - /*   340 */  1129, 1270, 1259, 1349, 1240, 1129, 1270, 1268, 1266, 1275,
148208         - /*   350 */  1153, 1192, 1453, 1453, 1449, 1449, 1449, 1497, 1497, 1412,
148209         - /*   360 */  1465, 1160, 1160, 1160, 1160, 1465, 1176, 1176, 1160, 1160,
148210         - /*   370 */  1160, 1160, 1465, 1129, 1129, 1129, 1129, 1129, 1129, 1460,
148211         - /*   380 */  1129, 1365, 1244, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148212         - /*   390 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148213         - /*   400 */  1129, 1129, 1129, 1129, 1129, 1295, 1129, 1132, 1409, 1129,
148214         - /*   410 */  1129, 1407, 1129, 1129, 1129, 1129, 1129, 1129, 1245, 1129,
148215         - /*   420 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148216         - /*   430 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1488, 1129, 1129,
148217         - /*   440 */  1129, 1129, 1129, 1129, 1379, 1378, 1129, 1129, 1242, 1129,
148218         - /*   450 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148219         - /*   460 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148220         - /*   470 */  1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148221         - /*   480 */  1129, 1129, 1129, 1129, 1129, 1129, 1267, 1129, 1424, 1129,
148222         - /*   490 */  1129, 1129, 1129, 1129, 1129, 1129, 1438, 1260, 1129, 1129,
148223         - /*   500 */  1479, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129, 1129,
148224         - /*   510 */  1129, 1129, 1129, 1129, 1129, 1474, 1216, 1297, 1129, 1296,
148225         - /*   520 */  1300, 1129, 1141, 1129,
       149020  + /*     0 */  1536, 1536, 1536, 1376, 1159, 1265, 1159, 1159, 1159, 1376,
       149021  + /*    10 */  1376, 1376, 1159, 1295, 1295, 1429, 1190, 1159, 1159, 1159,
       149022  + /*    20 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1375, 1159, 1159,
       149023  + /*    30 */  1159, 1159, 1459, 1459, 1159, 1159, 1159, 1159, 1159, 1159,
       149024  + /*    40 */  1159, 1159, 1159, 1301, 1159, 1159, 1159, 1159, 1159, 1377,
       149025  + /*    50 */  1378, 1159, 1159, 1159, 1428, 1430, 1393, 1311, 1310, 1309,
       149026  + /*    60 */  1308, 1411, 1282, 1306, 1299, 1303, 1371, 1372, 1370, 1374,
       149027  + /*    70 */  1378, 1377, 1159, 1302, 1342, 1356, 1341, 1159, 1159, 1159,
       149028  + /*    80 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149029  + /*    90 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149030  + /*   100 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149031  + /*   110 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149032  + /*   120 */  1159, 1159, 1159, 1159, 1350, 1355, 1361, 1354, 1351, 1344,
       149033  + /*   130 */  1343, 1345, 1346, 1159, 1180, 1229, 1159, 1159, 1159, 1159,
       149034  + /*   140 */  1447, 1446, 1159, 1159, 1190, 1347, 1348, 1358, 1357, 1436,
       149035  + /*   150 */  1492, 1491, 1394, 1159, 1159, 1159, 1159, 1159, 1159, 1459,
       149036  + /*   160 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149037  + /*   170 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149038  + /*   180 */  1159, 1159, 1159, 1159, 1459, 1459, 1159, 1190, 1459, 1459,
       149039  + /*   190 */  1186, 1336, 1335, 1186, 1289, 1159, 1442, 1265, 1256, 1159,
       149040  + /*   200 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149041  + /*   210 */  1159, 1159, 1159, 1433, 1431, 1159, 1159, 1159, 1159, 1159,
       149042  + /*   220 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149043  + /*   230 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149044  + /*   240 */  1159, 1159, 1159, 1261, 1159, 1159, 1159, 1159, 1159, 1159,
       149045  + /*   250 */  1159, 1159, 1159, 1159, 1159, 1486, 1159, 1406, 1243, 1261,
       149046  + /*   260 */  1261, 1261, 1261, 1263, 1244, 1242, 1255, 1190, 1166, 1528,
       149047  + /*   270 */  1305, 1284, 1284, 1525, 1305, 1305, 1525, 1204, 1506, 1201,
       149048  + /*   280 */  1295, 1295, 1295, 1284, 1289, 1289, 1373, 1262, 1255, 1159,
       149049  + /*   290 */  1528, 1270, 1270, 1527, 1527, 1270, 1394, 1314, 1320, 1232,
       149050  + /*   300 */  1305, 1238, 1238, 1238, 1238, 1270, 1177, 1305, 1305, 1314,
       149051  + /*   310 */  1320, 1232, 1232, 1305, 1270, 1177, 1410, 1522, 1270, 1177,
       149052  + /*   320 */  1384, 1270, 1177, 1270, 1177, 1384, 1230, 1230, 1230, 1219,
       149053  + /*   330 */  1384, 1230, 1204, 1230, 1219, 1230, 1230, 1384, 1388, 1388,
       149054  + /*   340 */  1384, 1288, 1283, 1288, 1283, 1288, 1283, 1288, 1283, 1270,
       149055  + /*   350 */  1469, 1469, 1300, 1289, 1379, 1270, 1159, 1300, 1298, 1296,
       149056  + /*   360 */  1305, 1183, 1222, 1489, 1489, 1485, 1485, 1485, 1533, 1533,
       149057  + /*   370 */  1442, 1501, 1190, 1190, 1190, 1190, 1501, 1206, 1206, 1190,
       149058  + /*   380 */  1190, 1190, 1190, 1501, 1159, 1159, 1159, 1159, 1159, 1159,
       149059  + /*   390 */  1496, 1159, 1395, 1274, 1159, 1159, 1159, 1159, 1159, 1159,
       149060  + /*   400 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149061  + /*   410 */  1159, 1159, 1159, 1159, 1159, 1159, 1325, 1159, 1162, 1439,
       149062  + /*   420 */  1159, 1159, 1437, 1159, 1159, 1159, 1159, 1159, 1159, 1275,
       149063  + /*   430 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149064  + /*   440 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1524, 1159,
       149065  + /*   450 */  1159, 1159, 1159, 1159, 1159, 1409, 1408, 1159, 1159, 1272,
       149066  + /*   460 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149067  + /*   470 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149068  + /*   480 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149069  + /*   490 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1297, 1159,
       149070  + /*   500 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149071  + /*   510 */  1159, 1159, 1159, 1474, 1290, 1159, 1159, 1515, 1159, 1159,
       149072  + /*   520 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
       149073  + /*   530 */  1159, 1159, 1510, 1246, 1327, 1159, 1326, 1330, 1159, 1171,
       149074  + /*   540 */  1159,
148226 149075   };
148227 149076   /********** End of lemon-generated parsing tables *****************************/
148228 149077   
148229 149078   /* The next table maps tokens (terminal symbols) into fallback tokens.  
148230 149079   ** If a construct like the following:
148231 149080   ** 
148232 149081   **      %fallback ID X Y Z.
................................................................................
148326 149175      59,  /*       WITH => ID */
148327 149176      59,  /*    CURRENT => ID */
148328 149177      59,  /*  FOLLOWING => ID */
148329 149178      59,  /*  PARTITION => ID */
148330 149179      59,  /*  PRECEDING => ID */
148331 149180      59,  /*      RANGE => ID */
148332 149181      59,  /*  UNBOUNDED => ID */
       149182  +   59,  /*    EXCLUDE => ID */
       149183  +   59,  /*     GROUPS => ID */
       149184  +   59,  /*     OTHERS => ID */
       149185  +   59,  /*       TIES => ID */
148333 149186      59,  /*    REINDEX => ID */
148334 149187      59,  /*     RENAME => ID */
148335 149188      59,  /*   CTIME_KW => ID */
148336 149189   };
148337 149190   #endif /* YYFALLBACK */
148338 149191   
148339 149192   /* The following structure represents a single element of the
................................................................................
148504 149357     /*   81 */ "WITH",
148505 149358     /*   82 */ "CURRENT",
148506 149359     /*   83 */ "FOLLOWING",
148507 149360     /*   84 */ "PARTITION",
148508 149361     /*   85 */ "PRECEDING",
148509 149362     /*   86 */ "RANGE",
148510 149363     /*   87 */ "UNBOUNDED",
148511         -  /*   88 */ "REINDEX",
148512         -  /*   89 */ "RENAME",
148513         -  /*   90 */ "CTIME_KW",
148514         -  /*   91 */ "ANY",
148515         -  /*   92 */ "BITAND",
148516         -  /*   93 */ "BITOR",
148517         -  /*   94 */ "LSHIFT",
148518         -  /*   95 */ "RSHIFT",
148519         -  /*   96 */ "PLUS",
148520         -  /*   97 */ "MINUS",
148521         -  /*   98 */ "STAR",
148522         -  /*   99 */ "SLASH",
148523         -  /*  100 */ "REM",
148524         -  /*  101 */ "CONCAT",
148525         -  /*  102 */ "COLLATE",
148526         -  /*  103 */ "BITNOT",
148527         -  /*  104 */ "ON",
148528         -  /*  105 */ "INDEXED",
148529         -  /*  106 */ "STRING",
148530         -  /*  107 */ "JOIN_KW",
148531         -  /*  108 */ "CONSTRAINT",
148532         -  /*  109 */ "DEFAULT",
148533         -  /*  110 */ "NULL",
148534         -  /*  111 */ "PRIMARY",
148535         -  /*  112 */ "UNIQUE",
148536         -  /*  113 */ "CHECK",
148537         -  /*  114 */ "REFERENCES",
148538         -  /*  115 */ "AUTOINCR",
148539         -  /*  116 */ "INSERT",
148540         -  /*  117 */ "DELETE",
148541         -  /*  118 */ "UPDATE",
148542         -  /*  119 */ "SET",
148543         -  /*  120 */ "DEFERRABLE",
148544         -  /*  121 */ "FOREIGN",
148545         -  /*  122 */ "DROP",
148546         -  /*  123 */ "UNION",
148547         -  /*  124 */ "ALL",
148548         -  /*  125 */ "EXCEPT",
148549         -  /*  126 */ "INTERSECT",
148550         -  /*  127 */ "SELECT",
148551         -  /*  128 */ "VALUES",
148552         -  /*  129 */ "DISTINCT",
148553         -  /*  130 */ "DOT",
148554         -  /*  131 */ "FROM",
148555         -  /*  132 */ "JOIN",
148556         -  /*  133 */ "USING",
148557         -  /*  134 */ "ORDER",
148558         -  /*  135 */ "GROUP",
148559         -  /*  136 */ "HAVING",
148560         -  /*  137 */ "LIMIT",
148561         -  /*  138 */ "WHERE",
148562         -  /*  139 */ "INTO",
148563         -  /*  140 */ "NOTHING",
148564         -  /*  141 */ "FLOAT",
148565         -  /*  142 */ "BLOB",
148566         -  /*  143 */ "INTEGER",
148567         -  /*  144 */ "VARIABLE",
148568         -  /*  145 */ "CASE",
148569         -  /*  146 */ "WHEN",
148570         -  /*  147 */ "THEN",
148571         -  /*  148 */ "ELSE",
148572         -  /*  149 */ "INDEX",
148573         -  /*  150 */ "ALTER",
148574         -  /*  151 */ "ADD",
148575         -  /*  152 */ "WINDOW",
148576         -  /*  153 */ "OVER",
148577         -  /*  154 */ "FILTER",
148578         -  /*  155 */ "input",
148579         -  /*  156 */ "cmdlist",
148580         -  /*  157 */ "ecmd",
148581         -  /*  158 */ "cmdx",
148582         -  /*  159 */ "explain",
148583         -  /*  160 */ "cmd",
148584         -  /*  161 */ "transtype",
148585         -  /*  162 */ "trans_opt",
148586         -  /*  163 */ "nm",
148587         -  /*  164 */ "savepoint_opt",
148588         -  /*  165 */ "create_table",
148589         -  /*  166 */ "create_table_args",
148590         -  /*  167 */ "createkw",
148591         -  /*  168 */ "temp",
148592         -  /*  169 */ "ifnotexists",
148593         -  /*  170 */ "dbnm",
148594         -  /*  171 */ "columnlist",
148595         -  /*  172 */ "conslist_opt",
148596         -  /*  173 */ "table_options",
148597         -  /*  174 */ "select",
148598         -  /*  175 */ "columnname",
148599         -  /*  176 */ "carglist",
148600         -  /*  177 */ "typetoken",
148601         -  /*  178 */ "typename",
148602         -  /*  179 */ "signed",
148603         -  /*  180 */ "plus_num",
148604         -  /*  181 */ "minus_num",
148605         -  /*  182 */ "scanpt",
148606         -  /*  183 */ "ccons",
148607         -  /*  184 */ "term",
148608         -  /*  185 */ "expr",
148609         -  /*  186 */ "onconf",
148610         -  /*  187 */ "sortorder",
148611         -  /*  188 */ "autoinc",
148612         -  /*  189 */ "eidlist_opt",
148613         -  /*  190 */ "refargs",
148614         -  /*  191 */ "defer_subclause",
148615         -  /*  192 */ "refarg",
148616         -  /*  193 */ "refact",
148617         -  /*  194 */ "init_deferred_pred_opt",
148618         -  /*  195 */ "conslist",
148619         -  /*  196 */ "tconscomma",
148620         -  /*  197 */ "tcons",
148621         -  /*  198 */ "sortlist",
148622         -  /*  199 */ "eidlist",
148623         -  /*  200 */ "defer_subclause_opt",
148624         -  /*  201 */ "orconf",
148625         -  /*  202 */ "resolvetype",
148626         -  /*  203 */ "raisetype",
148627         -  /*  204 */ "ifexists",
148628         -  /*  205 */ "fullname",
148629         -  /*  206 */ "selectnowith",
148630         -  /*  207 */ "oneselect",
148631         -  /*  208 */ "wqlist",
148632         -  /*  209 */ "multiselect_op",
148633         -  /*  210 */ "distinct",
148634         -  /*  211 */ "selcollist",
148635         -  /*  212 */ "from",
148636         -  /*  213 */ "where_opt",
148637         -  /*  214 */ "groupby_opt",
148638         -  /*  215 */ "having_opt",
148639         -  /*  216 */ "orderby_opt",
148640         -  /*  217 */ "limit_opt",
148641         -  /*  218 */ "window_clause",
148642         -  /*  219 */ "values",
148643         -  /*  220 */ "nexprlist",
148644         -  /*  221 */ "sclp",
148645         -  /*  222 */ "as",
148646         -  /*  223 */ "seltablist",
148647         -  /*  224 */ "stl_prefix",
148648         -  /*  225 */ "joinop",
148649         -  /*  226 */ "indexed_opt",
148650         -  /*  227 */ "on_opt",
148651         -  /*  228 */ "using_opt",
148652         -  /*  229 */ "exprlist",
148653         -  /*  230 */ "xfullname",
148654         -  /*  231 */ "idlist",
148655         -  /*  232 */ "with",
148656         -  /*  233 */ "setlist",
148657         -  /*  234 */ "insert_cmd",
148658         -  /*  235 */ "idlist_opt",
148659         -  /*  236 */ "upsert",
148660         -  /*  237 */ "over_clause",
148661         -  /*  238 */ "likeop",
148662         -  /*  239 */ "between_op",
148663         -  /*  240 */ "in_op",
148664         -  /*  241 */ "paren_exprlist",
148665         -  /*  242 */ "case_operand",
148666         -  /*  243 */ "case_exprlist",
148667         -  /*  244 */ "case_else",
148668         -  /*  245 */ "uniqueflag",
148669         -  /*  246 */ "collate",
148670         -  /*  247 */ "vinto",
148671         -  /*  248 */ "nmnum",
148672         -  /*  249 */ "trigger_decl",
148673         -  /*  250 */ "trigger_cmd_list",
148674         -  /*  251 */ "trigger_time",
148675         -  /*  252 */ "trigger_event",
148676         -  /*  253 */ "foreach_clause",
148677         -  /*  254 */ "when_clause",
148678         -  /*  255 */ "trigger_cmd",
148679         -  /*  256 */ "trnm",
148680         -  /*  257 */ "tridxby",
148681         -  /*  258 */ "database_kw_opt",
148682         -  /*  259 */ "key_opt",
148683         -  /*  260 */ "add_column_fullname",
148684         -  /*  261 */ "kwcolumn_opt",
148685         -  /*  262 */ "create_vtab",
148686         -  /*  263 */ "vtabarglist",
148687         -  /*  264 */ "vtabarg",
148688         -  /*  265 */ "vtabargtoken",
148689         -  /*  266 */ "lp",
148690         -  /*  267 */ "anylist",
148691         -  /*  268 */ "windowdefn_list",
148692         -  /*  269 */ "windowdefn",
148693         -  /*  270 */ "window",
148694         -  /*  271 */ "frame_opt",
148695         -  /*  272 */ "part_opt",
148696         -  /*  273 */ "filter_opt",
148697         -  /*  274 */ "range_or_rows",
148698         -  /*  275 */ "frame_bound",
148699         -  /*  276 */ "frame_bound_s",
148700         -  /*  277 */ "frame_bound_e",
       149364  +  /*   88 */ "EXCLUDE",
       149365  +  /*   89 */ "GROUPS",
       149366  +  /*   90 */ "OTHERS",
       149367  +  /*   91 */ "TIES",
       149368  +  /*   92 */ "REINDEX",
       149369  +  /*   93 */ "RENAME",
       149370  +  /*   94 */ "CTIME_KW",
       149371  +  /*   95 */ "ANY",
       149372  +  /*   96 */ "BITAND",
       149373  +  /*   97 */ "BITOR",
       149374  +  /*   98 */ "LSHIFT",
       149375  +  /*   99 */ "RSHIFT",
       149376  +  /*  100 */ "PLUS",
       149377  +  /*  101 */ "MINUS",
       149378  +  /*  102 */ "STAR",
       149379  +  /*  103 */ "SLASH",
       149380  +  /*  104 */ "REM",
       149381  +  /*  105 */ "CONCAT",
       149382  +  /*  106 */ "COLLATE",
       149383  +  /*  107 */ "BITNOT",
       149384  +  /*  108 */ "ON",
       149385  +  /*  109 */ "INDEXED",
       149386  +  /*  110 */ "STRING",
       149387  +  /*  111 */ "JOIN_KW",
       149388  +  /*  112 */ "CONSTRAINT",
       149389  +  /*  113 */ "DEFAULT",
       149390  +  /*  114 */ "NULL",
       149391  +  /*  115 */ "PRIMARY",
       149392  +  /*  116 */ "UNIQUE",
       149393  +  /*  117 */ "CHECK",
       149394  +  /*  118 */ "REFERENCES",
       149395  +  /*  119 */ "AUTOINCR",
       149396  +  /*  120 */ "INSERT",
       149397  +  /*  121 */ "DELETE",
       149398  +  /*  122 */ "UPDATE",
       149399  +  /*  123 */ "SET",
       149400  +  /*  124 */ "DEFERRABLE",
       149401  +  /*  125 */ "FOREIGN",
       149402  +  /*  126 */ "DROP",
       149403  +  /*  127 */ "UNION",
       149404  +  /*  128 */ "ALL",
       149405  +  /*  129 */ "EXCEPT",
       149406  +  /*  130 */ "INTERSECT",
       149407  +  /*  131 */ "SELECT",
       149408  +  /*  132 */ "VALUES",
       149409  +  /*  133 */ "DISTINCT",
       149410  +  /*  134 */ "DOT",
       149411  +  /*  135 */ "FROM",
       149412  +  /*  136 */ "JOIN",
       149413  +  /*  137 */ "USING",
       149414  +  /*  138 */ "ORDER",
       149415  +  /*  139 */ "GROUP",
       149416  +  /*  140 */ "HAVING",
       149417  +  /*  141 */ "LIMIT",
       149418  +  /*  142 */ "WHERE",
       149419  +  /*  143 */ "INTO",
       149420  +  /*  144 */ "NOTHING",
       149421  +  /*  145 */ "FLOAT",
       149422  +  /*  146 */ "BLOB",
       149423  +  /*  147 */ "INTEGER",
       149424  +  /*  148 */ "VARIABLE",
       149425  +  /*  149 */ "CASE",
       149426  +  /*  150 */ "WHEN",
       149427  +  /*  151 */ "THEN",
       149428  +  /*  152 */ "ELSE",
       149429  +  /*  153 */ "INDEX",
       149430  +  /*  154 */ "ALTER",
       149431  +  /*  155 */ "ADD",
       149432  +  /*  156 */ "WINDOW",
       149433  +  /*  157 */ "OVER",
       149434  +  /*  158 */ "FILTER",
       149435  +  /*  159 */ "TRUEFALSE",
       149436  +  /*  160 */ "ISNOT",
       149437  +  /*  161 */ "FUNCTION",
       149438  +  /*  162 */ "COLUMN",
       149439  +  /*  163 */ "AGG_FUNCTION",
       149440  +  /*  164 */ "AGG_COLUMN",
       149441  +  /*  165 */ "UMINUS",
       149442  +  /*  166 */ "UPLUS",
       149443  +  /*  167 */ "TRUTH",
       149444  +  /*  168 */ "REGISTER",
       149445  +  /*  169 */ "VECTOR",
       149446  +  /*  170 */ "SELECT_COLUMN",
       149447  +  /*  171 */ "IF_NULL_ROW",
       149448  +  /*  172 */ "ASTERISK",
       149449  +  /*  173 */ "SPAN",
       149450  +  /*  174 */ "SPACE",
       149451  +  /*  175 */ "ILLEGAL",
       149452  +  /*  176 */ &q