SQLite Android Bindings
Check-in [1dfc539feb]
Not logged in

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

Overview
Comment:Upgrade this project to version 3.22.0.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 1dfc539febc5597abe32342579961f4410326547
User & Date: dan 2018-01-22 18:59:56
Context
2018-01-22
19:26
Upgrade build.gradle to include new google() repository. check-in: 9e0de54b71 user: dan tags: trunk
18:59
Upgrade this project to version 3.22.0. check-in: 1dfc539feb user: dan tags: trunk
2017-11-28
18:16
Fix a broken test case in DatabaseUtilsTest.java. check-in: e4878173ae user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.21.0.  By combining all the individual C code files into this
            3  +** version 3.22.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
................................................................................
  1143   1143   ** been edited in any way since it was last checked in, then the last
  1144   1144   ** four hexadecimal digits of the hash may be modified.
  1145   1145   **
  1146   1146   ** See also: [sqlite3_libversion()],
  1147   1147   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1148   1148   ** [sqlite_version()] and [sqlite_source_id()].
  1149   1149   */
  1150         -#define SQLITE_VERSION        "3.21.0"
  1151         -#define SQLITE_VERSION_NUMBER 3021000
  1152         -#define SQLITE_SOURCE_ID      "2017-10-24 18:55:49 1a584e499906b5c87ec7d43d4abce641fdf017c42125b083109bc77c4de48827"
         1150  +#define SQLITE_VERSION        "3.22.0"
         1151  +#define SQLITE_VERSION_NUMBER 3022000
         1152  +#define SQLITE_SOURCE_ID      "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
  1153   1153   
  1154   1154   /*
  1155   1155   ** CAPI3REF: Run-Time Library Version Numbers
  1156   1156   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1157   1157   **
  1158   1158   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1159   1159   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1490   1490   ** support for additional result codes that provide more detailed information
  1491   1491   ** about errors. These [extended result codes] are enabled or disabled
  1492   1492   ** on a per database connection basis using the
  1493   1493   ** [sqlite3_extended_result_codes()] API.  Or, the extended code for
  1494   1494   ** the most recent error can be obtained using
  1495   1495   ** [sqlite3_extended_errcode()].
  1496   1496   */
         1497  +#define SQLITE_ERROR_MISSING_COLLSEQ   (SQLITE_ERROR | (1<<8))
         1498  +#define SQLITE_ERROR_RETRY             (SQLITE_ERROR | (2<<8))
  1497   1499   #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
  1498   1500   #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
  1499   1501   #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
  1500   1502   #define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
  1501   1503   #define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
  1502   1504   #define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
  1503   1505   #define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
................................................................................
  1533   1535   #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
  1534   1536   #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
  1535   1537   #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
  1536   1538   #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
  1537   1539   #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
  1538   1540   #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
  1539   1541   #define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
         1542  +#define SQLITE_READONLY_CANTINIT       (SQLITE_READONLY | (5<<8))
         1543  +#define SQLITE_READONLY_DIRECTORY      (SQLITE_READONLY | (6<<8))
  1540   1544   #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
  1541   1545   #define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
  1542   1546   #define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
  1543   1547   #define SQLITE_CONSTRAINT_FOREIGNKEY   (SQLITE_CONSTRAINT | (3<<8))
  1544   1548   #define SQLITE_CONSTRAINT_FUNCTION     (SQLITE_CONSTRAINT | (4<<8))
  1545   1549   #define SQLITE_CONSTRAINT_NOTNULL      (SQLITE_CONSTRAINT | (5<<8))
  1546   1550   #define SQLITE_CONSTRAINT_PRIMARYKEY   (SQLITE_CONSTRAINT | (6<<8))
................................................................................
  2151   2155   ** CAPI3REF: OS Interface Object
  2152   2156   **
  2153   2157   ** An instance of the sqlite3_vfs object defines the interface between
  2154   2158   ** the SQLite core and the underlying operating system.  The "vfs"
  2155   2159   ** in the name of the object stands for "virtual file system".  See
  2156   2160   ** the [VFS | VFS documentation] for further information.
  2157   2161   **
  2158         -** The value of the iVersion field is initially 1 but may be larger in
  2159         -** future versions of SQLite.  Additional fields may be appended to this
  2160         -** object when the iVersion value is increased.  Note that the structure
  2161         -** of the sqlite3_vfs object changes in the transaction between
  2162         -** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
  2163         -** modified.
         2162  +** The VFS interface is sometimes extended by adding new methods onto
         2163  +** the end.  Each time such an extension occurs, the iVersion field
         2164  +** is incremented.  The iVersion value started out as 1 in
         2165  +** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
         2166  +** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
         2167  +** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6].  Additional fields
         2168  +** may be appended to the sqlite3_vfs object and the iVersion value
         2169  +** may increase again in future versions of SQLite.
         2170  +** Note that the structure
         2171  +** of the sqlite3_vfs object changes in the transition from
         2172  +** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
         2173  +** and yet the iVersion field was not modified.
  2164   2174   **
  2165   2175   ** The szOsFile field is the size of the subclassed [sqlite3_file]
  2166   2176   ** structure used by this VFS.  mxPathname is the maximum length of
  2167   2177   ** a pathname in this VFS.
  2168   2178   **
  2169   2179   ** Registered sqlite3_vfs objects are kept on a linked list formed by
  2170   2180   ** the pNext pointer.  The [sqlite3_vfs_register()]
................................................................................
  3069   3079   ** operation before closing the connection. This option may be used to
  3070   3080   ** override this behaviour. The first parameter passed to this operation
  3071   3081   ** is an integer - non-zero to disable checkpoints-on-close, or zero (the
  3072   3082   ** default) to enable them. The second parameter is a pointer to an integer
  3073   3083   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  3074   3084   ** have been disabled - 0 if they are not disabled, 1 if they are.
  3075   3085   ** </dd>
  3076         -**
  3077   3086   ** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  3078   3087   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  3079   3088   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  3080   3089   ** a single SQL query statement will always use the same algorithm regardless
  3081   3090   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  3082   3091   ** that look at the values of bound parameters, which can make some queries
  3083   3092   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  3084   3093   ** the QPSG active, SQLite will always use the same query plan in the field as
  3085   3094   ** was used during testing in the lab.
  3086   3095   ** </dd>
  3087         -**
         3096  +** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
         3097  +** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
         3098  +** include output for any operations performed by trigger programs. This
         3099  +** option is used to set or clear (the default) a flag that governs this
         3100  +** behavior. The first parameter passed to this operation is an integer -
         3101  +** non-zero to enable output for trigger programs, or zero to disable it.
         3102  +** The second parameter is a pointer to an integer into which is written 
         3103  +** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
         3104  +** it is not disabled, 1 if it is.  
         3105  +** </dd>
  3088   3106   ** </dl>
  3089   3107   */
  3090   3108   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  3091   3109   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  3092   3110   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  3093   3111   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  3094   3112   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  3095   3113   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  3096   3114   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  3097   3115   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  3098         -
         3116  +#define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
         3117  +#define SQLITE_DBCONFIG_MAX                   1008 /* Largest DBCONFIG */
  3099   3118   
  3100   3119   /*
  3101   3120   ** CAPI3REF: Enable Or Disable Extended Result Codes
  3102   3121   ** METHOD: sqlite3
  3103   3122   **
  3104   3123   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  3105   3124   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  3952   3971      void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  3953   3972   
  3954   3973   /*
  3955   3974   ** CAPI3REF: SQL Trace Event Codes
  3956   3975   ** KEYWORDS: SQLITE_TRACE
  3957   3976   **
  3958   3977   ** These constants identify classes of events that can be monitored
  3959         -** using the [sqlite3_trace_v2()] tracing logic.  The third argument
  3960         -** to [sqlite3_trace_v2()] is an OR-ed combination of one or more of
         3978  +** using the [sqlite3_trace_v2()] tracing logic.  The M argument
         3979  +** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
  3961   3980   ** the following constants.  ^The first argument to the trace callback
  3962   3981   ** is one of the following constants.
  3963   3982   **
  3964   3983   ** New tracing constants may be added in future releases.
  3965   3984   **
  3966   3985   ** ^A trace callback has four arguments: xCallback(T,C,P,X).
  3967   3986   ** ^The T argument is one of the integer type codes above.
................................................................................
  5799   5818   ** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
  5800   5819   ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
  5801   5820   ** TEXT in bytes
  5802   5821   ** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
  5803   5822   ** datatype of the value
  5804   5823   ** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
  5805   5824   ** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
         5825  +** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
         5826  +** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
         5827  +** against a virtual table.
  5806   5828   ** </table></blockquote>
  5807   5829   **
  5808   5830   ** <b>Details:</b>
  5809   5831   **
  5810   5832   ** These routines extract type, size, and content information from
  5811   5833   ** [protected sqlite3_value] objects.  Protected sqlite3_value objects
  5812   5834   ** are used to pass parameter information into implementation of
................................................................................
  5846   5868   ** ^(The sqlite3_value_numeric_type() interface attempts to apply
  5847   5869   ** numeric affinity to the value.  This means that an attempt is
  5848   5870   ** made to convert the value to an integer or floating point.  If
  5849   5871   ** such a conversion is possible without loss of information (in other
  5850   5872   ** words, if the value is a string that looks like a number)
  5851   5873   ** then the conversion is performed.  Otherwise no conversion occurs.
  5852   5874   ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
         5875  +**
         5876  +** ^Within the [xUpdate] method of a [virtual table], the
         5877  +** sqlite3_value_nochange(X) interface returns true if and only if
         5878  +** the column corresponding to X is unchanged by the UPDATE operation
         5879  +** that the xUpdate method call was invoked to implement and if
         5880  +** and the prior [xColumn] method call that was invoked to extracted
         5881  +** the value for that column returned without setting a result (probably
         5882  +** because it queried [sqlite3_vtab_nochange()] and found that the column
         5883  +** was unchanging).  ^Within an [xUpdate] method, any value for which
         5884  +** sqlite3_value_nochange(X) is true will in all other respects appear
         5885  +** to be a NULL value.  If sqlite3_value_nochange(X) is invoked anywhere other
         5886  +** than within an [xUpdate] method call for an UPDATE statement, then
         5887  +** the return value is arbitrary and meaningless.
  5853   5888   **
  5854   5889   ** Please pay particular attention to the fact that the pointer returned
  5855   5890   ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
  5856   5891   ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
  5857   5892   ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
  5858   5893   ** or [sqlite3_value_text16()].
  5859   5894   **
................................................................................
  5869   5904   SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
  5870   5905   SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
  5871   5906   SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
  5872   5907   SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
  5873   5908   SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
  5874   5909   SQLITE_API int sqlite3_value_type(sqlite3_value*);
  5875   5910   SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
         5911  +SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
  5876   5912   
  5877   5913   /*
  5878   5914   ** CAPI3REF: Finding The Subtype Of SQL Values
  5879   5915   ** METHOD: sqlite3_value
  5880   5916   **
  5881   5917   ** The sqlite3_value_subtype(V) function returns the subtype for
  5882   5918   ** an [application-defined SQL function] argument V.  The subtype
................................................................................
  7971   8007   ** ^A NULL pointer can be used in place of "main" to refer to the
  7972   8008   ** main database file.
  7973   8009   ** ^The third and fourth parameters to this routine
  7974   8010   ** are passed directly through to the second and third parameters of
  7975   8011   ** the xFileControl method.  ^The return value of the xFileControl
  7976   8012   ** method becomes the return value of this routine.
  7977   8013   **
  7978         -** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
         8014  +** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
  7979   8015   ** a pointer to the underlying [sqlite3_file] object to be written into
  7980         -** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
         8016  +** the space pointed to by the 4th parameter.  ^The [SQLITE_FCNTL_FILE_POINTER]
  7981   8017   ** case is a short-circuit path which does not actually invoke the
  7982   8018   ** underlying sqlite3_io_methods.xFileControl method.
  7983   8019   **
  7984   8020   ** ^If the second parameter (zDbName) does not match the name of any
  7985   8021   ** open database file, then SQLITE_ERROR is returned.  ^This error
  7986   8022   ** code is not remembered and will not be recalled by [sqlite3_errcode()]
  7987   8023   ** or [sqlite3_errmsg()].  The underlying xFileControl method might
  7988   8024   ** also return SQLITE_ERROR.  There is no way to distinguish between
  7989   8025   ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
  7990   8026   ** xFileControl method.
  7991   8027   **
  7992         -** See also: [SQLITE_FCNTL_LOCKSTATE]
         8028  +** See also: [file control opcodes]
  7993   8029   */
  7994   8030   SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
  7995   8031   
  7996   8032   /*
  7997   8033   ** CAPI3REF: Testing Interface
  7998   8034   **
  7999   8035   ** ^The sqlite3_test_control() interface is used to read out internal
................................................................................
  8042   8078   #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  8043   8079   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  8044   8080   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  8045   8081   #define SQLITE_TESTCTRL_BYTEORDER               22
  8046   8082   #define SQLITE_TESTCTRL_ISINIT                  23
  8047   8083   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  8048   8084   #define SQLITE_TESTCTRL_IMPOSTER                25
  8049         -#define SQLITE_TESTCTRL_LAST                    25
         8085  +#define SQLITE_TESTCTRL_PARSER_COVERAGE         26
         8086  +#define SQLITE_TESTCTRL_LAST                    26  /* Largest TESTCTRL */
  8050   8087   
  8051   8088   /*
  8052   8089   ** CAPI3REF: SQLite Runtime Status
  8053   8090   **
  8054   8091   ** ^These interfaces are used to retrieve runtime status information
  8055   8092   ** about the performance of SQLite, and optionally to reset various
  8056   8093   ** highwater marks.  ^The first argument is an integer code for
................................................................................
  9296   9333   ** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
  9297   9334   ** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
  9298   9335   ** of the SQL statement that triggered the call to the [xUpdate] method of the
  9299   9336   ** [virtual table].
  9300   9337   */
  9301   9338   SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
  9302   9339   
         9340  +/*
         9341  +** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
         9342  +**
         9343  +** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
         9344  +** method of a [virtual table], then it returns true if and only if the
         9345  +** column is being fetched as part of an UPDATE operation during which the
         9346  +** column value will not change.  Applications might use this to substitute
         9347  +** a lighter-weight value to return that the corresponding [xUpdate] method
         9348  +** understands as a "no-change" value.
         9349  +**
         9350  +** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
         9351  +** the column is not changed by the UPDATE statement, they the xColumn
         9352  +** method can optionally return without setting a result, without calling
         9353  +** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
         9354  +** In that case, [sqlite3_value_nochange(X)] will return true for the
         9355  +** same column in the [xUpdate] method.
         9356  +*/
         9357  +SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
         9358  +
         9359  +/*
         9360  +** CAPI3REF: Determine The Collation For a Virtual Table Constraint
         9361  +**
         9362  +** This function may only be called from within a call to the [xBestIndex]
         9363  +** method of a [virtual table]. 
         9364  +**
         9365  +** The first argument must be the sqlite3_index_info object that is the
         9366  +** first parameter to the xBestIndex() method. The second argument must be
         9367  +** an index into the aConstraint[] array belonging to the sqlite3_index_info
         9368  +** structure passed to xBestIndex. This function returns a pointer to a buffer 
         9369  +** containing the name of the collation sequence for the corresponding
         9370  +** constraint.
         9371  +*/
         9372  +SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
         9373  +
  9303   9374   /*
  9304   9375   ** CAPI3REF: Conflict resolution modes
  9305   9376   ** KEYWORDS: {conflict resolution mode}
  9306   9377   **
  9307   9378   ** These constants are returned by [sqlite3_vtab_on_conflict()] to
  9308   9379   ** inform a [virtual table] implementation what the [ON CONFLICT] mode
  9309   9380   ** is for the SQL statement being evaluated.
................................................................................
 10023  10094   ** no changes will be recorded in either of these scenarios.
 10024  10095   **
 10025  10096   ** Changes are not recorded for individual rows that have NULL values stored
 10026  10097   ** in one or more of their PRIMARY KEY columns.
 10027  10098   **
 10028  10099   ** SQLITE_OK is returned if the call completes without error. Or, if an error 
 10029  10100   ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
        10101  +**
        10102  +** <h3>Special sqlite_stat1 Handling</h3>
        10103  +**
        10104  +** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to 
        10105  +** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
        10106  +**  <pre>
        10107  +**  &nbsp;     CREATE TABLE sqlite_stat1(tbl,idx,stat)  
        10108  +**  </pre>
        10109  +**
        10110  +** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are 
        10111  +** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes 
        10112  +** are recorded for rows for which (idx IS NULL) is true. However, for such
        10113  +** rows a zero-length blob (SQL value X'') is stored in the changeset or
        10114  +** patchset instead of a NULL value. This allows such changesets to be
        10115  +** manipulated by legacy implementations of sqlite3changeset_invert(),
        10116  +** concat() and similar.
        10117  +**
        10118  +** The sqlite3changeset_apply() function automatically converts the 
        10119  +** zero-length blob back to a NULL value when updating the sqlite_stat1
        10120  +** table. However, if the application calls sqlite3changeset_new(),
        10121  +** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset 
        10122  +** iterator directly (including on a changeset iterator passed to a
        10123  +** conflict-handler callback) then the X'' value is returned. The application
        10124  +** must translate X'' to NULL itself if required.
        10125  +**
        10126  +** Legacy (older than 3.22.0) versions of the sessions module cannot capture
        10127  +** changes made to the sqlite_stat1 table. Legacy versions of the
        10128  +** sqlite3changeset_apply() function silently ignore any modifications to the
        10129  +** sqlite_stat1 table that are part of a changeset or patchset.
 10030  10130   */
 10031  10131   SQLITE_API int sqlite3session_attach(
 10032  10132     sqlite3_session *pSession,      /* Session object */
 10033  10133     const char *zTab                /* Table name */
 10034  10134   );
 10035  10135   
 10036  10136   /*
................................................................................
 13131  13231   typedef struct Bitvec Bitvec;
 13132  13232   typedef struct CollSeq CollSeq;
 13133  13233   typedef struct Column Column;
 13134  13234   typedef struct Db Db;
 13135  13235   typedef struct Schema Schema;
 13136  13236   typedef struct Expr Expr;
 13137  13237   typedef struct ExprList ExprList;
 13138         -typedef struct ExprSpan ExprSpan;
 13139  13238   typedef struct FKey FKey;
 13140  13239   typedef struct FuncDestructor FuncDestructor;
 13141  13240   typedef struct FuncDef FuncDef;
 13142  13241   typedef struct FuncDefHash FuncDefHash;
 13143  13242   typedef struct IdList IdList;
 13144  13243   typedef struct Index Index;
 13145  13244   typedef struct IndexSample IndexSample;
................................................................................
 13477  13576                          int flags, int seekResult);
 13478  13577   SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
 13479  13578   SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
 13480  13579   SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
 13481  13580   SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
 13482  13581   SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
 13483  13582   SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
        13583  +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
        13584  +SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
        13585  +#endif
 13484  13586   SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
 13485  13587   SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
 13486  13588   SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
 13487  13589   
 13488  13590   SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
 13489  13591   SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
 13490  13592   SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*);
................................................................................
 13690  13792   #define P4_EXPR       (-10) /* P4 is a pointer to an Expr tree */
 13691  13793   #define P4_MEM        (-11) /* P4 is a pointer to a Mem*    structure */
 13692  13794   #define P4_VTAB       (-12) /* P4 is a pointer to an sqlite3_vtab structure */
 13693  13795   #define P4_REAL       (-13) /* P4 is a 64-bit floating point value */
 13694  13796   #define P4_INT64      (-14) /* P4 is a 64-bit signed integer */
 13695  13797   #define P4_INTARRAY   (-15) /* P4 is a vector of 32-bit integers */
 13696  13798   #define P4_FUNCCTX    (-16) /* P4 is a pointer to an sqlite3_context object */
        13799  +#define P4_DYNBLOB    (-17) /* Pointer to memory from sqliteMalloc() */
 13697  13800   
 13698  13801   /* Error message codes for OP_Halt */
 13699  13802   #define P5_ConstraintNotNull 1
 13700  13803   #define P5_ConstraintUnique  2
 13701  13804   #define P5_ConstraintCheck   3
 13702  13805   #define P5_ConstraintFK      4
 13703  13806   
................................................................................
 13828  13931   #define OP_Subtract       89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 13829  13932   #define OP_Multiply       90 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 13830  13933   #define OP_Divide         91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 13831  13934   #define OP_Remainder      92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 13832  13935   #define OP_Concat         93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 13833  13936   #define OP_Compare        94 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 13834  13937   #define OP_BitNot         95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 13835         -#define OP_Column         96 /* synopsis: r[P3]=PX                         */
        13938  +#define OP_Offset         96 /* synopsis: r[P3] = sqlite_offset(P1)        */
 13836  13939   #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 13837         -#define OP_Affinity       98 /* synopsis: affinity(r[P1@P2])               */
 13838         -#define OP_MakeRecord     99 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 13839         -#define OP_Count         100 /* synopsis: r[P2]=count()                    */
 13840         -#define OP_ReadCookie    101
 13841         -#define OP_SetCookie     102
 13842         -#define OP_ReopenIdx     103 /* synopsis: root=P2 iDb=P3                   */
 13843         -#define OP_OpenRead      104 /* synopsis: root=P2 iDb=P3                   */
 13844         -#define OP_OpenWrite     105 /* synopsis: root=P2 iDb=P3                   */
 13845         -#define OP_OpenDup       106
 13846         -#define OP_OpenAutoindex 107 /* synopsis: nColumn=P2                       */
 13847         -#define OP_OpenEphemeral 108 /* synopsis: nColumn=P2                       */
 13848         -#define OP_SorterOpen    109
 13849         -#define OP_SequenceTest  110 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 13850         -#define OP_OpenPseudo    111 /* synopsis: P3 columns in r[P2]              */
 13851         -#define OP_Close         112
 13852         -#define OP_ColumnsUsed   113
 13853         -#define OP_Sequence      114 /* synopsis: r[P2]=cursor[P1].ctr++           */
 13854         -#define OP_NewRowid      115 /* synopsis: r[P2]=rowid                      */
 13855         -#define OP_Insert        116 /* synopsis: intkey=r[P3] data=r[P2]          */
 13856         -#define OP_InsertInt     117 /* synopsis: intkey=P3 data=r[P2]             */
 13857         -#define OP_Delete        118
 13858         -#define OP_ResetCount    119
 13859         -#define OP_SorterCompare 120 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 13860         -#define OP_SorterData    121 /* synopsis: r[P2]=data                       */
 13861         -#define OP_RowData       122 /* synopsis: r[P2]=data                       */
 13862         -#define OP_Rowid         123 /* synopsis: r[P2]=rowid                      */
 13863         -#define OP_NullRow       124
 13864         -#define OP_SeekEnd       125
 13865         -#define OP_SorterInsert  126 /* synopsis: key=r[P2]                        */
 13866         -#define OP_IdxInsert     127 /* synopsis: key=r[P2]                        */
 13867         -#define OP_IdxDelete     128 /* synopsis: key=r[P2@P3]                     */
 13868         -#define OP_DeferredSeek  129 /* synopsis: Move P3 to P1.rowid if needed    */
 13869         -#define OP_IdxRowid      130 /* synopsis: r[P2]=rowid                      */
 13870         -#define OP_Destroy       131
        13940  +#define OP_Column         98 /* synopsis: r[P3]=PX                         */
        13941  +#define OP_Affinity       99 /* synopsis: affinity(r[P1@P2])               */
        13942  +#define OP_MakeRecord    100 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        13943  +#define OP_Count         101 /* synopsis: r[P2]=count()                    */
        13944  +#define OP_ReadCookie    102
        13945  +#define OP_SetCookie     103
        13946  +#define OP_ReopenIdx     104 /* synopsis: root=P2 iDb=P3                   */
        13947  +#define OP_OpenRead      105 /* synopsis: root=P2 iDb=P3                   */
        13948  +#define OP_OpenWrite     106 /* synopsis: root=P2 iDb=P3                   */
        13949  +#define OP_OpenDup       107
        13950  +#define OP_OpenAutoindex 108 /* synopsis: nColumn=P2                       */
        13951  +#define OP_OpenEphemeral 109 /* synopsis: nColumn=P2                       */
        13952  +#define OP_SorterOpen    110
        13953  +#define OP_SequenceTest  111 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
        13954  +#define OP_OpenPseudo    112 /* synopsis: P3 columns in r[P2]              */
        13955  +#define OP_Close         113
        13956  +#define OP_ColumnsUsed   114
        13957  +#define OP_Sequence      115 /* synopsis: r[P2]=cursor[P1].ctr++           */
        13958  +#define OP_NewRowid      116 /* synopsis: r[P2]=rowid                      */
        13959  +#define OP_Insert        117 /* synopsis: intkey=r[P3] data=r[P2]          */
        13960  +#define OP_InsertInt     118 /* synopsis: intkey=P3 data=r[P2]             */
        13961  +#define OP_Delete        119
        13962  +#define OP_ResetCount    120
        13963  +#define OP_SorterCompare 121 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
        13964  +#define OP_SorterData    122 /* synopsis: r[P2]=data                       */
        13965  +#define OP_RowData       123 /* synopsis: r[P2]=data                       */
        13966  +#define OP_Rowid         124 /* synopsis: r[P2]=rowid                      */
        13967  +#define OP_NullRow       125
        13968  +#define OP_SeekEnd       126
        13969  +#define OP_SorterInsert  127 /* synopsis: key=r[P2]                        */
        13970  +#define OP_IdxInsert     128 /* synopsis: key=r[P2]                        */
        13971  +#define OP_IdxDelete     129 /* synopsis: key=r[P2@P3]                     */
        13972  +#define OP_DeferredSeek  130 /* synopsis: Move P3 to P1.rowid if needed    */
        13973  +#define OP_IdxRowid      131 /* synopsis: r[P2]=rowid                      */
 13871  13974   #define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 13872         -#define OP_Clear         133
 13873         -#define OP_ResetSorter   134
 13874         -#define OP_CreateBtree   135 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 13875         -#define OP_SqlExec       136
 13876         -#define OP_ParseSchema   137
 13877         -#define OP_LoadAnalysis  138
 13878         -#define OP_DropTable     139
 13879         -#define OP_DropIndex     140
 13880         -#define OP_DropTrigger   141
 13881         -#define OP_IntegrityCk   142
 13882         -#define OP_RowSetAdd     143 /* synopsis: rowset(P1)=r[P2]                 */
 13883         -#define OP_Param         144
 13884         -#define OP_FkCounter     145 /* synopsis: fkctr[P1]+=P2                    */
 13885         -#define OP_MemMax        146 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 13886         -#define OP_OffsetLimit   147 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 13887         -#define OP_AggStep0      148 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 13888         -#define OP_AggStep       149 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 13889         -#define OP_AggFinal      150 /* synopsis: accum=r[P1] N=P2                 */
 13890         -#define OP_Expire        151
 13891         -#define OP_TableLock     152 /* synopsis: iDb=P1 root=P2 write=P3          */
 13892         -#define OP_VBegin        153
 13893         -#define OP_VCreate       154
 13894         -#define OP_VDestroy      155
 13895         -#define OP_VOpen         156
 13896         -#define OP_VColumn       157 /* synopsis: r[P3]=vcolumn(P2)                */
 13897         -#define OP_VRename       158
 13898         -#define OP_Pagecount     159
 13899         -#define OP_MaxPgcnt      160
 13900         -#define OP_PureFunc0     161
 13901         -#define OP_Function0     162 /* synopsis: r[P3]=func(r[P2@P5])             */
 13902         -#define OP_PureFunc      163
 13903         -#define OP_Function      164 /* synopsis: r[P3]=func(r[P2@P5])             */
 13904         -#define OP_CursorHint    165
 13905         -#define OP_Noop          166
 13906         -#define OP_Explain       167
        13975  +#define OP_Destroy       133
        13976  +#define OP_Clear         134
        13977  +#define OP_ResetSorter   135
        13978  +#define OP_CreateBtree   136 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
        13979  +#define OP_SqlExec       137
        13980  +#define OP_ParseSchema   138
        13981  +#define OP_LoadAnalysis  139
        13982  +#define OP_DropTable     140
        13983  +#define OP_DropIndex     141
        13984  +#define OP_DropTrigger   142
        13985  +#define OP_IntegrityCk   143
        13986  +#define OP_RowSetAdd     144 /* synopsis: rowset(P1)=r[P2]                 */
        13987  +#define OP_Param         145
        13988  +#define OP_FkCounter     146 /* synopsis: fkctr[P1]+=P2                    */
        13989  +#define OP_MemMax        147 /* synopsis: r[P1]=max(r[P1],r[P2])           */
        13990  +#define OP_OffsetLimit   148 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
        13991  +#define OP_AggStep0      149 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        13992  +#define OP_AggStep       150 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        13993  +#define OP_AggFinal      151 /* synopsis: accum=r[P1] N=P2                 */
        13994  +#define OP_Expire        152
        13995  +#define OP_TableLock     153 /* synopsis: iDb=P1 root=P2 write=P3          */
        13996  +#define OP_VBegin        154
        13997  +#define OP_VCreate       155
        13998  +#define OP_VDestroy      156
        13999  +#define OP_VOpen         157
        14000  +#define OP_VColumn       158 /* synopsis: r[P3]=vcolumn(P2)                */
        14001  +#define OP_VRename       159
        14002  +#define OP_Pagecount     160
        14003  +#define OP_MaxPgcnt      161
        14004  +#define OP_PureFunc0     162
        14005  +#define OP_Function0     163 /* synopsis: r[P3]=func(r[P2@P5])             */
        14006  +#define OP_PureFunc      164
        14007  +#define OP_Function      165 /* synopsis: r[P3]=func(r[P2@P5])             */
        14008  +#define OP_Trace         166
        14009  +#define OP_CursorHint    167
        14010  +#define OP_Noop          168
        14011  +#define OP_Explain       169
 13907  14012   
 13908  14013   /* Properties such as "out2" or "jump" that are specified in
 13909  14014   ** comments following the "case" for each opcode in the vdbe.c
 13910  14015   ** are encoded into bitvectors as follows:
 13911  14016   */
 13912  14017   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 13913  14018   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 13924  14029   /*  40 */ 0x01, 0x01, 0x23, 0x26, 0x26, 0x0b, 0x01, 0x01,\
 13925  14030   /*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 13926  14031   /*  56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
 13927  14032   /*  64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
 13928  14033   /*  72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 13929  14034   /*  80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
 13930  14035   /*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 13931         -/*  96 */ 0x00, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
        14036  +/*  96 */ 0x20, 0x10, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
 13932  14037   /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 13933         -/* 112 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
 13934         -/* 120 */ 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04, 0x04,\
 13935         -/* 128 */ 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00, 0x10,\
 13936         -/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,\
 13937         -/* 144 */ 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00,\
 13938         -/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
 13939         -/* 160 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 13940         -}
        14038  +/* 112 */ 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
        14039  +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04,\
        14040  +/* 128 */ 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
        14041  +/* 136 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
        14042  +/* 144 */ 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00,\
        14043  +/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
        14044  +/* 160 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
        14045  +/* 168 */ 0x00, 0x00,}
 13941  14046   
 13942  14047   /* The sqlite3P2Values() routine is able to run faster if it knows
 13943  14048   ** the value of the largest JUMP opcode.  The smaller the maximum
 13944  14049   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 13945  14050   ** generated this include file strives to group all JUMP opcodes
 13946  14051   ** together near the beginning of the list.
 13947  14052   */
................................................................................
 15193  15298     unsigned nProgressOps;        /* Number of opcodes for progress callback */
 15194  15299   #endif
 15195  15300   #ifndef SQLITE_OMIT_VIRTUALTABLE
 15196  15301     int nVTrans;                  /* Allocated size of aVTrans */
 15197  15302     Hash aModule;                 /* populated by sqlite3_create_module() */
 15198  15303     VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
 15199  15304     VTable **aVTrans;             /* Virtual tables with open transactions */
 15200         -  VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
        15305  +  VTable *pDisconnect;          /* Disconnect these in next sqlite3_prepare() */
 15201  15306   #endif
 15202  15307     Hash aFunc;                   /* Hash table of connection functions */
 15203  15308     Hash aCollSeq;                /* All collating sequences */
 15204  15309     BusyHandler busyHandler;      /* Busy callback */
 15205  15310     Db aDbStatic[2];              /* Static space for the 2 default backends */
 15206  15311     Savepoint *pSavepoint;        /* List of active savepoints */
 15207  15312     int busyTimeout;              /* Busy handler timeout, in msec */
................................................................................
 15268  15373   #define SQLITE_LoadExtension  0x00010000  /* Enable load_extension */
 15269  15374   #define SQLITE_LoadExtFunc    0x00020000  /* Enable load_extension() SQL func */
 15270  15375   #define SQLITE_EnableTrigger  0x00040000  /* True to enable triggers */
 15271  15376   #define SQLITE_DeferFKs       0x00080000  /* Defer all FK constraints */
 15272  15377   #define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
 15273  15378   #define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
 15274  15379   #define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
 15275         -#define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee */
        15380  +#define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee*/
        15381  +#define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
        15382  +
 15276  15383   /* Flags used only if debugging */
 15277  15384   #ifdef SQLITE_DEBUG
 15278  15385   #define SQLITE_SqlTrace       0x08000000  /* Debug print SQL as it executes */
 15279  15386   #define SQLITE_VdbeListing    0x10000000  /* Debug listings of VDBE programs */
 15280  15387   #define SQLITE_VdbeTrace      0x20000000  /* True to trace VDBE execution */
 15281  15388   #define SQLITE_VdbeAddopTrace 0x40000000  /* Trace sqlite3VdbeAddOp() calls */
 15282  15389   #define SQLITE_VdbeEQP        0x80000000  /* Debug EXPLAIN QUERY PLAN */
................................................................................
 15401  15508   #define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */
 15402  15509   #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
 15403  15510   #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
 15404  15511   #define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
 15405  15512   #define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
 15406  15513                                       ** single query - might change over time */
 15407  15514   #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
        15515  +#define SQLITE_FUNC_OFFSET   0x8000 /* Built-in sqlite_offset() function */
 15408  15516   
 15409  15517   /*
 15410  15518   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 15411  15519   ** used to create the initializers for the FuncDef structures.
 15412  15520   **
 15413  15521   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
 15414  15522   **     Used to create a scalar function definition of a function zName
................................................................................
 15945  16053     unsigned idxType:2;      /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
 15946  16054     unsigned bUnordered:1;   /* Use this index for == or IN queries only */
 15947  16055     unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
 15948  16056     unsigned isResized:1;    /* True if resizeIndexObject() has been called */
 15949  16057     unsigned isCovering:1;   /* True if this is a covering index */
 15950  16058     unsigned noSkipScan:1;   /* Do not try to use skip-scan if true */
 15951  16059     unsigned hasStat1:1;     /* aiRowLogEst values come from sqlite_stat1 */
        16060  +  unsigned bNoQuery:1;     /* Do not use this index to optimize queries */
 15952  16061   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 15953  16062     int nSample;             /* Number of elements in aSample[] */
 15954  16063     int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
 15955  16064     tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
 15956  16065     IndexSample *aSample;    /* Samples of the left-most key */
 15957  16066     tRowcnt *aiRowEst;       /* Non-logarithmic stat1 data for this index */
 15958  16067     tRowcnt nRowEst0;        /* Non-logarithmic number of rows in the index */
................................................................................
 16175  16284   };
 16176  16285   
 16177  16286   /*
 16178  16287   ** The following are the meanings of bits in the Expr.flags field.
 16179  16288   */
 16180  16289   #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
 16181  16290   #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
 16182         -                  /* 0x000004 // available for use */
        16291  +#define EP_HasFunc   0x000004 /* Contains one or more functions of any kind */
 16183  16292                     /* 0x000008 // available for use */
 16184  16293   #define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
 16185  16294   #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
 16186  16295   #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
 16187  16296   #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
 16188  16297   #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
 16189  16298   #define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
................................................................................
 16199  16308   #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
 16200  16309   #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
 16201  16310   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
 16202  16311   #define EP_Alias     0x400000 /* Is an alias for a result set column */
 16203  16312   #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
 16204  16313   
 16205  16314   /*
 16206         -** Combinations of two or more EP_* flags
        16315  +** The EP_Propagate mask is a set of properties that automatically propagate
        16316  +** upwards into parent nodes.
 16207  16317   */
 16208         -#define EP_Propagate (EP_Collate|EP_Subquery) /* Propagate these bits up tree */
        16318  +#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
 16209  16319   
 16210  16320   /*
 16211  16321   ** These macros can be used to test, set, or clear bits in the
 16212  16322   ** Expr.flags field.
 16213  16323   */
 16214  16324   #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
 16215  16325   #define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))
................................................................................
 16273  16383           u16 iAlias;           /* Index into Parse.aAlias[] for zName */
 16274  16384         } x;
 16275  16385         int iConstExprReg;      /* Register in which Expr value is cached */
 16276  16386       } u;
 16277  16387     } a[1];                  /* One slot for each expression in the list */
 16278  16388   };
 16279  16389   
 16280         -/*
 16281         -** An instance of this structure is used by the parser to record both
 16282         -** the parse tree for an expression and the span of input text for an
 16283         -** expression.
 16284         -*/
 16285         -struct ExprSpan {
 16286         -  Expr *pExpr;          /* The expression parse tree */
 16287         -  const char *zStart;   /* First character of input text */
 16288         -  const char *zEnd;     /* One character past the end of input text */
 16289         -};
 16290         -
 16291  16390   /*
 16292  16391   ** An instance of this structure can hold a simple list of identifiers,
 16293  16392   ** such as the list "a,b,c" in the following statements:
 16294  16393   **
 16295  16394   **      INSERT INTO t(a,b,c) VALUES ...;
 16296  16395   **      CREATE INDEX idx ON t(a,b,c);
 16297  16396   **      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
................................................................................
 16481  16580   #define NC_PartIdx   0x0002  /* True if resolving a partial index WHERE */
 16482  16581   #define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
 16483  16582   #define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
 16484  16583   #define NC_HasAgg    0x0010  /* One or more aggregate functions seen */
 16485  16584   #define NC_IdxExpr   0x0020  /* True if resolving columns of CREATE INDEX */
 16486  16585   #define NC_VarSelect 0x0040  /* A correlated subquery has been seen */
 16487  16586   #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
        16587  +#define NC_Complex   0x2000  /* True if a function or subquery seen */
 16488  16588   
 16489  16589   /*
 16490  16590   ** An instance of the following structure contains all information
 16491  16591   ** needed to generate code for a single SELECT statement.
 16492  16592   **
 16493  16593   ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
 16494  16594   ** If there is a LIMIT clause, the parser sets nLimit to the value of the
................................................................................
 16520  16620     Expr *pWhere;          /* The WHERE clause */
 16521  16621     ExprList *pGroupBy;    /* The GROUP BY clause */
 16522  16622     Expr *pHaving;         /* The HAVING clause */
 16523  16623     ExprList *pOrderBy;    /* The ORDER BY clause */
 16524  16624     Select *pPrior;        /* Prior select in a compound select statement */
 16525  16625     Select *pNext;         /* Next select to the left in a compound */
 16526  16626     Expr *pLimit;          /* LIMIT expression. NULL means not used. */
 16527         -  Expr *pOffset;         /* OFFSET expression. NULL means not used. */
 16528  16627     With *pWith;           /* WITH clause attached to this select. Or NULL. */
 16529  16628   };
 16530  16629   
 16531  16630   /*
 16532  16631   ** Allowed values for Select.selFlags.  The "SF" prefix stands for
 16533  16632   ** "Select Flag".
 16534  16633   **
................................................................................
 16551  16650   #define SF_NestedFrom     0x00800  /* Part of a parenthesized FROM clause */
 16552  16651   #define SF_MinMaxAgg      0x01000  /* Aggregate containing min() or max() */
 16553  16652   #define SF_Recursive      0x02000  /* The recursive part of a recursive CTE */
 16554  16653   #define SF_FixedLimit     0x04000  /* nSelectRow set by a constant LIMIT */
 16555  16654   #define SF_MaybeConvert   0x08000  /* Need convertCompoundSelectToSubquery() */
 16556  16655   #define SF_Converted      0x10000  /* By convertCompoundSelectToSubquery() */
 16557  16656   #define SF_IncludeHidden  0x20000  /* Include hidden columns in output */
        16657  +#define SF_ComplexResult  0x40000  /* Result set contains subquery or function */
 16558  16658   
 16559  16659   
 16560  16660   /*
 16561  16661   ** The results of a SELECT can be distributed in several ways, as defined
 16562  16662   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
 16563  16663   ** Type".
 16564  16664   **
................................................................................
 16755  16855     int nRangeReg;       /* Size of the temporary register block */
 16756  16856     int iRangeReg;       /* First register in temporary register block */
 16757  16857     int nErr;            /* Number of errors seen */
 16758  16858     int nTab;            /* Number of previously allocated VDBE cursors */
 16759  16859     int nMem;            /* Number of memory cells used so far */
 16760  16860     int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
 16761  16861     int szOpAlloc;       /* Bytes of memory space allocated for Vdbe.aOp[] */
 16762         -  int iSelfTab;        /* Table for associated with an index on expr, or negative
        16862  +  int iSelfTab;        /* Table associated with an index on expr, or negative
 16763  16863                          ** of the base register during check-constraint eval */
 16764  16864     int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
 16765  16865     int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
 16766  16866     int nLabel;          /* Number of labels used */
 16767  16867     int *aLabel;         /* Space to hold the labels */
 16768  16868     ExprList *pConstExpr;/* Constant expressions */
 16769  16869     Token constraintName;/* Name of the constraint currently being parsed */
................................................................................
 16896  16996   #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
 16897  16997   #define OPFLAG_SEEKEQ        0x02    /* OP_Open** cursor uses EQ seek only */
 16898  16998   #define OPFLAG_FORDELETE     0x08    /* OP_Open should use BTREE_FORDELETE */
 16899  16999   #define OPFLAG_P2ISREG       0x10    /* P2 to OP_Open** is a register number */
 16900  17000   #define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
 16901  17001   #define OPFLAG_SAVEPOSITION  0x02    /* OP_Delete/Insert: save cursor pos */
 16902  17002   #define OPFLAG_AUXDELETE     0x04    /* OP_Delete: index in a DELETE op */
        17003  +#define OPFLAG_NOCHNG_MAGIC  0x6d    /* OP_MakeRecord: serialtype 10 is ok */
 16903  17004   
 16904  17005   /*
 16905  17006    * Each trigger present in the database schema is stored as an instance of
 16906  17007    * struct Trigger.
 16907  17008    *
 16908  17009    * Pointers to instances of struct Trigger are stored in two ways.
 16909  17010    * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
................................................................................
 16983  17084     u8 orconf;           /* OE_Rollback etc. */
 16984  17085     Trigger *pTrig;      /* The trigger that this step is a part of */
 16985  17086     Select *pSelect;     /* SELECT statement or RHS of INSERT INTO SELECT ... */
 16986  17087     char *zTarget;       /* Target table for DELETE, UPDATE, INSERT */
 16987  17088     Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
 16988  17089     ExprList *pExprList; /* SET clause for UPDATE. */
 16989  17090     IdList *pIdList;     /* Column names for INSERT */
        17091  +  char *zSpan;         /* Original SQL text of this command */
 16990  17092     TriggerStep *pNext;  /* Next in the link-list */
 16991  17093     TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
 16992  17094   };
 16993  17095   
 16994  17096   /*
 16995  17097   ** The following structure contains information used by the sqliteFix...
 16996  17098   ** routines as they walk the parse tree to make database references
................................................................................
 17202  17304   /*
 17203  17305   ** The SQLITE_*_BKPT macros are substitutes for the error codes with
 17204  17306   ** the same name but without the _BKPT suffix.  These macros invoke
 17205  17307   ** routines that report the line-number on which the error originated
 17206  17308   ** using sqlite3_log().  The routines also provide a convenient place
 17207  17309   ** to set a debugger breakpoint.
 17208  17310   */
        17311  +SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType);
 17209  17312   SQLITE_PRIVATE int sqlite3CorruptError(int);
 17210  17313   SQLITE_PRIVATE int sqlite3MisuseError(int);
 17211  17314   SQLITE_PRIVATE int sqlite3CantopenError(int);
 17212  17315   #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
 17213  17316   #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
 17214  17317   #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
 17215  17318   #ifdef SQLITE_DEBUG
................................................................................
 17292  17395   SQLITE_PRIVATE void *sqlite3Malloc(u64);
 17293  17396   SQLITE_PRIVATE void *sqlite3MallocZero(u64);
 17294  17397   SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
 17295  17398   SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
 17296  17399   SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64);
 17297  17400   SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
 17298  17401   SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
        17402  +SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
 17299  17403   SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
 17300  17404   SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
 17301  17405   SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
 17302  17406   SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
 17303  17407   SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
 17304  17408   SQLITE_PRIVATE int sqlite3MallocSize(void*);
 17305  17409   SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
................................................................................
 17359  17463   SQLITE_PRIVATE void sqlite3StatusDown(int, int);
 17360  17464   SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
 17361  17465   SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
 17362  17466   
 17363  17467   /* Access to mutexes used by sqlite3_status() */
 17364  17468   SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
 17365  17469   SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
        17470  +
        17471  +#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
        17472  +SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*);
        17473  +#else
        17474  +# define sqlite3MutexWarnOnContention(x)
        17475  +#endif
 17366  17476   
 17367  17477   #ifndef SQLITE_OMIT_FLOATING_POINT
 17368  17478   SQLITE_PRIVATE   int sqlite3IsNaN(double);
 17369  17479   #else
 17370  17480   # define sqlite3IsNaN(X)  0
 17371  17481   #endif
 17372  17482   
................................................................................
 17424  17534   SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
 17425  17535   SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
 17426  17536   SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
 17427  17537   SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
 17428  17538   SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
 17429  17539   SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int);
 17430  17540   SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
 17431         -SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
        17541  +SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
 17432  17542   SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
 17433  17543   SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
 17434  17544   SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
 17435  17545   SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
 17436  17546   SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
 17437  17547   #ifndef SQLITE_OMIT_VIRTUALTABLE
 17438  17548   SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
................................................................................
 17454  17564   #else
 17455  17565   # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
 17456  17566   #endif
 17457  17567   SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*,Token*);
 17458  17568   SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
 17459  17569   SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
 17460  17570   SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
 17461         -SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
        17571  +SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
 17462  17572   SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
 17463  17573   SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
 17464  17574   SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
 17465  17575                       sqlite3_vfs**,char**,char **);
 17466  17576   SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
 17467  17577   
 17468  17578   #ifdef SQLITE_UNTESTABLE
................................................................................
 17526  17636   SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
 17527  17637   SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
 17528  17638   SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
 17529  17639                             Expr*, int, int, u8);
 17530  17640   SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
 17531  17641   SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
 17532  17642   SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
 17533         -                         Expr*,ExprList*,u32,Expr*,Expr*);
        17643  +                         Expr*,ExprList*,u32,Expr*);
 17534  17644   SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
 17535  17645   SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
 17536  17646   SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
 17537  17647   SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
 17538  17648   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 17539         -SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*);
        17649  +SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
 17540  17650   #endif
 17541         -SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
 17542         -SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
        17651  +SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
        17652  +SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*);
 17543  17653   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 17544  17654   SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
 17545  17655   SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
 17546  17656   SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
 17547  17657   SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
 17548  17658   SQLITE_PRIVATE int sqlite3WhereOrderedInnerLoop(WhereInfo*);
 17549  17659   SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
................................................................................
 17659  17769   SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
 17660  17770   SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
 17661  17771   SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
 17662  17772   SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
 17663  17773   SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
 17664  17774   
 17665  17775   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 17666         -SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
        17776  +SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
 17667  17777   #endif
 17668  17778   
 17669  17779   #ifndef SQLITE_OMIT_TRIGGER
 17670  17780   SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
 17671  17781                              Expr*,int, int);
 17672  17782   SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
 17673  17783   SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
................................................................................
 17675  17785   SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
 17676  17786   SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
 17677  17787   SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
 17678  17788                               int, int, int);
 17679  17789   SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
 17680  17790     void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
 17681  17791   SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
 17682         -SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
        17792  +SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
        17793  +                                        const char*,const char*);
 17683  17794   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
 17684         -                                        Select*,u8);
 17685         -SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
 17686         -SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
        17795  +                                        Select*,u8,const char*,const char*);
        17796  +SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8,
        17797  +                                        const char*,const char*);
        17798  +SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*,
        17799  +                                        const char*,const char*);
 17687  17800   SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
 17688  17801   SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
 17689  17802   SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
 17690  17803   # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
 17691  17804   # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
 17692  17805   #else
 17693  17806   # define sqlite3TriggersExist(B,C,D,E,F) 0
................................................................................
 18109  18222     #define sqlite3ConnectionUnlocked(x)
 18110  18223     #define sqlite3ConnectionClosed(x)
 18111  18224   #endif
 18112  18225   
 18113  18226   #ifdef SQLITE_DEBUG
 18114  18227   SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
 18115  18228   #endif
        18229  +#if defined(YYCOVERAGE)
        18230  +SQLITE_PRIVATE   int sqlite3ParserCoverage(FILE*);
        18231  +#endif
 18116  18232   
 18117  18233   /*
 18118  18234   ** If the SQLITE_ENABLE IOTRACE exists then the global variable
 18119  18235   ** sqlite3IoTrace is a pointer to a printf-like routine used to
 18120  18236   ** print I/O tracing messages.
 18121  18237   */
 18122  18238   #ifdef SQLITE_ENABLE_IOTRACE
................................................................................
 18737  18853   
 18738  18854   /* One or more of the following flags are set to indicate the validOK
 18739  18855   ** representations of the value stored in the Mem struct.
 18740  18856   **
 18741  18857   ** If the MEM_Null flag is set, then the value is an SQL NULL value.
 18742  18858   ** For a pointer type created using sqlite3_bind_pointer() or
 18743  18859   ** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
        18860  +** If both MEM_Null and MEM_Zero are set, that means that the value is
        18861  +** an unchanging column value from VColumn.
 18744  18862   **
 18745  18863   ** If the MEM_Str flag is set then Mem.z points at a string representation.
 18746  18864   ** Usually this is encoded in the same unicode encoding as the main
 18747  18865   ** database (see below for exceptions). If the MEM_Term flag is also
 18748  18866   ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
 18749  18867   ** flags may coexist with the MEM_Str flag.
 18750  18868   */
................................................................................
 19475  19593   ** dates afterwards, depending on locale.  Beware of this difference.
 19476  19594   **
 19477  19595   ** The conversion algorithms are implemented based on descriptions
 19478  19596   ** in the following text:
 19479  19597   **
 19480  19598   **      Jean Meeus
 19481  19599   **      Astronomical Algorithms, 2nd Edition, 1998
 19482         -**      ISBM 0-943396-61-1
        19600  +**      ISBN 0-943396-61-1
 19483  19601   **      Willmann-Bell, Inc
 19484  19602   **      Richmond, Virginia (USA)
 19485  19603   */
 19486  19604   /* #include "sqliteInt.h" */
 19487  19605   /* #include <stdlib.h> */
 19488  19606   /* #include <assert.h> */
 19489  19607   #include <time.h>
................................................................................
 23380  23498   ** allocate a mutex while the system is uninitialized.
 23381  23499   */
 23382  23500   static SQLITE_WSD int mutexIsInit = 0;
 23383  23501   #endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
 23384  23502   
 23385  23503   
 23386  23504   #ifndef SQLITE_MUTEX_OMIT
        23505  +
        23506  +#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
        23507  +/*
        23508  +** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
        23509  +** the implementation of a wrapper around the system default mutex
        23510  +** implementation (sqlite3DefaultMutex()). 
        23511  +**
        23512  +** Most calls are passed directly through to the underlying default
        23513  +** mutex implementation. Except, if a mutex is configured by calling
        23514  +** sqlite3MutexWarnOnContention() on it, then if contention is ever
        23515  +** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
        23516  +**
        23517  +** This type of mutex is used as the database handle mutex when testing
        23518  +** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
        23519  +*/
        23520  +
        23521  +/* 
        23522  +** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
        23523  +** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
        23524  +** allocated by the system mutex implementation. Variable iType is usually set
        23525  +** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
        23526  +** or one of the static mutex identifiers. Or, if this is a recursive mutex
        23527  +** that has been configured using sqlite3MutexWarnOnContention(), it is
        23528  +** set to SQLITE_MUTEX_WARNONCONTENTION.
        23529  +*/
        23530  +typedef struct CheckMutex CheckMutex;
        23531  +struct CheckMutex {
        23532  +  int iType;
        23533  +  sqlite3_mutex *mutex;
        23534  +};
        23535  +
        23536  +#define SQLITE_MUTEX_WARNONCONTENTION  (-1)
        23537  +
        23538  +/* 
        23539  +** Pointer to real mutex methods object used by the CheckMutex
        23540  +** implementation. Set by checkMutexInit(). 
        23541  +*/
        23542  +static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
        23543  +
        23544  +#ifdef SQLITE_DEBUG
        23545  +static int checkMutexHeld(sqlite3_mutex *p){
        23546  +  return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
        23547  +}
        23548  +static int checkMutexNotheld(sqlite3_mutex *p){
        23549  +  return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
        23550  +}
        23551  +#endif
        23552  +
        23553  +/*
        23554  +** Initialize and deinitialize the mutex subsystem.
        23555  +*/
        23556  +static int checkMutexInit(void){ 
        23557  +  pGlobalMutexMethods = sqlite3DefaultMutex();
        23558  +  return SQLITE_OK; 
        23559  +}
        23560  +static int checkMutexEnd(void){ 
        23561  +  pGlobalMutexMethods = 0;
        23562  +  return SQLITE_OK; 
        23563  +}
        23564  +
        23565  +/*
        23566  +** Allocate a mutex.
        23567  +*/
        23568  +static sqlite3_mutex *checkMutexAlloc(int iType){
        23569  +  static CheckMutex staticMutexes[] = {
        23570  +    {2, 0}, {3, 0}, {4, 0}, {5, 0},
        23571  +    {6, 0}, {7, 0}, {8, 0}, {9, 0},
        23572  +    {10, 0}, {11, 0}, {12, 0}, {13, 0}
        23573  +  };
        23574  +  CheckMutex *p = 0;
        23575  +
        23576  +  assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 );
        23577  +  if( iType<2 ){
        23578  +    p = sqlite3MallocZero(sizeof(CheckMutex));
        23579  +    if( p==0 ) return 0;
        23580  +    p->iType = iType;
        23581  +  }else{
        23582  +#ifdef SQLITE_ENABLE_API_ARMOR
        23583  +    if( iType-2>=ArraySize(staticMutexes) ){
        23584  +      (void)SQLITE_MISUSE_BKPT;
        23585  +      return 0;
        23586  +    }
        23587  +#endif
        23588  +    p = &staticMutexes[iType-2];
        23589  +  }
        23590  +
        23591  +  if( p->mutex==0 ){
        23592  +    p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
        23593  +    if( p->mutex==0 ){
        23594  +      if( iType<2 ){
        23595  +        sqlite3_free(p);
        23596  +      }
        23597  +      p = 0;
        23598  +    }
        23599  +  }
        23600  +
        23601  +  return (sqlite3_mutex*)p;
        23602  +}
        23603  +
        23604  +/*
        23605  +** Free a mutex.
        23606  +*/
        23607  +static void checkMutexFree(sqlite3_mutex *p){
        23608  +  assert( SQLITE_MUTEX_RECURSIVE<2 );
        23609  +  assert( SQLITE_MUTEX_FAST<2 );
        23610  +  assert( SQLITE_MUTEX_WARNONCONTENTION<2 );
        23611  +
        23612  +#if SQLITE_ENABLE_API_ARMOR
        23613  +  if( ((CheckMutex*)p)->iType<2 )
        23614  +#endif
        23615  +  {
        23616  +    CheckMutex *pCheck = (CheckMutex*)p;
        23617  +    pGlobalMutexMethods->xMutexFree(pCheck->mutex);
        23618  +    sqlite3_free(pCheck);
        23619  +  }
        23620  +#ifdef SQLITE_ENABLE_API_ARMOR
        23621  +  else{
        23622  +    (void)SQLITE_MISUSE_BKPT;
        23623  +  }
        23624  +#endif
        23625  +}
        23626  +
        23627  +/*
        23628  +** Enter the mutex.
        23629  +*/
        23630  +static void checkMutexEnter(sqlite3_mutex *p){
        23631  +  CheckMutex *pCheck = (CheckMutex*)p;
        23632  +  if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
        23633  +    if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
        23634  +      return;
        23635  +    }
        23636  +    sqlite3_log(SQLITE_MISUSE, 
        23637  +        "illegal multi-threaded access to database connection"
        23638  +    );
        23639  +  }
        23640  +  pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
        23641  +}
        23642  +
        23643  +/*
        23644  +** Enter the mutex (do not block).
        23645  +*/
        23646  +static int checkMutexTry(sqlite3_mutex *p){
        23647  +  CheckMutex *pCheck = (CheckMutex*)p;
        23648  +  return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
        23649  +}
        23650  +
        23651  +/*
        23652  +** Leave the mutex.
        23653  +*/
        23654  +static void checkMutexLeave(sqlite3_mutex *p){
        23655  +  CheckMutex *pCheck = (CheckMutex*)p;
        23656  +  pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
        23657  +}
        23658  +
        23659  +sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
        23660  +  static const sqlite3_mutex_methods sMutex = {
        23661  +    checkMutexInit,
        23662  +    checkMutexEnd,
        23663  +    checkMutexAlloc,
        23664  +    checkMutexFree,
        23665  +    checkMutexEnter,
        23666  +    checkMutexTry,
        23667  +    checkMutexLeave,
        23668  +#ifdef SQLITE_DEBUG
        23669  +    checkMutexHeld,
        23670  +    checkMutexNotheld
        23671  +#else
        23672  +    0,
        23673  +    0
        23674  +#endif
        23675  +  };
        23676  +  return &sMutex;
        23677  +}
        23678  +
        23679  +/*
        23680  +** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
        23681  +** one on which there should be no contention.
        23682  +*/
        23683  +SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
        23684  +  if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){
        23685  +    CheckMutex *pCheck = (CheckMutex*)p;
        23686  +    assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
        23687  +    pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
        23688  +  }
        23689  +}
        23690  +#endif   /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
        23691  +
 23387  23692   /*
 23388  23693   ** Initialize the mutex system.
 23389  23694   */
 23390  23695   SQLITE_PRIVATE int sqlite3MutexInit(void){ 
 23391  23696     int rc = SQLITE_OK;
 23392  23697     if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
 23393  23698       /* If the xMutexAlloc method has not been set, then the user did not
................................................................................
 23395  23700       ** sqlite3_initialize() being called. This block copies pointers to
 23396  23701       ** the default implementation into the sqlite3GlobalConfig structure.
 23397  23702       */
 23398  23703       sqlite3_mutex_methods const *pFrom;
 23399  23704       sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
 23400  23705   
 23401  23706       if( sqlite3GlobalConfig.bCoreMutex ){
        23707  +#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
        23708  +      pFrom = multiThreadedCheckMutex();
        23709  +#else
 23402  23710         pFrom = sqlite3DefaultMutex();
        23711  +#endif
 23403  23712       }else{
 23404  23713         pFrom = sqlite3NoopMutex();
 23405  23714       }
 23406  23715       pTo->xMutexInit = pFrom->xMutexInit;
 23407  23716       pTo->xMutexEnd = pFrom->xMutexEnd;
 23408  23717       pTo->xMutexFree = pFrom->xMutexFree;
 23409  23718       pTo->xMutexEnter = pFrom->xMutexEnter;
................................................................................
 23521  23830   SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
 23522  23831     assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
 23523  23832     return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
 23524  23833   }
 23525  23834   #endif
 23526  23835   
 23527  23836   #endif /* !defined(SQLITE_MUTEX_OMIT) */
        23837  +
 23528  23838   
 23529  23839   /************** End of mutex.c ***********************************************/
 23530  23840   /************** Begin file mutex_noop.c **************************************/
 23531  23841   /*
 23532  23842   ** 2008 October 07
 23533  23843   **
 23534  23844   ** The author disclaims copyright to this source code.  In place of
................................................................................
 25452  25762     zNew = sqlite3DbMallocRawNN(db, n+1);
 25453  25763     if( zNew ){
 25454  25764       memcpy(zNew, z, (size_t)n);
 25455  25765       zNew[n] = 0;
 25456  25766     }
 25457  25767     return zNew;
 25458  25768   }
        25769  +
        25770  +/*
        25771  +** The text between zStart and zEnd represents a phrase within a larger
        25772  +** SQL statement.  Make a copy of this phrase in space obtained form
        25773  +** sqlite3DbMalloc().  Omit leading and trailing whitespace.
        25774  +*/
        25775  +SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
        25776  +  int n;
        25777  +  while( sqlite3Isspace(zStart[0]) ) zStart++;
        25778  +  n = (int)(zEnd - zStart);
        25779  +  while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
        25780  +  return sqlite3DbStrNDup(db, zStart, n);
        25781  +}
 25459  25782   
 25460  25783   /*
 25461  25784   ** Free any prior content in *pz and replace it with a copy of zNew.
 25462  25785   */
 25463  25786   SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
 25464  25787     sqlite3DbFree(db, *pz);
 25465  25788     *pz = sqlite3DbStrDup(db, zNew);
................................................................................
 26623  26946     StrAccum acc;
 26624  26947     char zBuf[500];
 26625  26948     sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
 26626  26949     va_start(ap,zFormat);
 26627  26950     sqlite3VXPrintf(&acc, zFormat, ap);
 26628  26951     va_end(ap);
 26629  26952     sqlite3StrAccumFinish(&acc);
        26953  +#ifdef SQLITE_OS_TRACE_PROC
        26954  +  {
        26955  +    extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
        26956  +    SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
        26957  +  }
        26958  +#else
 26630  26959     fprintf(stdout,"%s", zBuf);
 26631  26960     fflush(stdout);
        26961  +#endif
 26632  26962   }
 26633  26963   #endif
 26634  26964   
 26635  26965   
 26636  26966   /*
 26637  26967   ** variable-argument wrapper around sqlite3VXPrintf().  The bFlags argument
 26638  26968   ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
................................................................................
 26797  27127         n = 0;
 26798  27128         if( p->pSrc && p->pSrc->nSrc ) n++;
 26799  27129         if( p->pWhere ) n++;
 26800  27130         if( p->pGroupBy ) n++;
 26801  27131         if( p->pHaving ) n++;
 26802  27132         if( p->pOrderBy ) n++;
 26803  27133         if( p->pLimit ) n++;
 26804         -      if( p->pOffset ) n++;
 26805  27134       }
 26806  27135       sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
 26807  27136       if( p->pSrc && p->pSrc->nSrc ){
 26808  27137         int i;
 26809  27138         pView = sqlite3TreeViewPush(pView, (n--)>0);
 26810  27139         sqlite3TreeViewLine(pView, "FROM");
 26811  27140         for(i=0; i<p->pSrc->nSrc; i++){
................................................................................
 26854  27183         sqlite3TreeViewPop(pView);
 26855  27184       }
 26856  27185       if( p->pOrderBy ){
 26857  27186         sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
 26858  27187       }
 26859  27188       if( p->pLimit ){
 26860  27189         sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
 26861         -      sqlite3TreeViewExpr(pView, p->pLimit, 0);
 26862         -      sqlite3TreeViewPop(pView);
 26863         -    }
 26864         -    if( p->pOffset ){
 26865         -      sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
 26866         -      sqlite3TreeViewExpr(pView, p->pOffset, 0);
        27190  +      sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
        27191  +      if( p->pLimit->pRight ){
        27192  +        sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
        27193  +        sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
        27194  +        sqlite3TreeViewPop(pView);
        27195  +      }
 26867  27196         sqlite3TreeViewPop(pView);
 26868  27197       }
 26869  27198       if( p->pPrior ){
 26870  27199         const char *zOp = "UNION";
 26871  27200         switch( p->op ){
 26872  27201           case TK_ALL:         zOp = "UNION ALL";  break;
 26873  27202           case TK_INTERSECT:   zOp = "INTERSECT";  break;
................................................................................
 27152  27481     if( pList==0 ){
 27153  27482       sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
 27154  27483     }else{
 27155  27484       int i;
 27156  27485       sqlite3TreeViewLine(pView, "%s", zLabel);
 27157  27486       for(i=0; i<pList->nExpr; i++){
 27158  27487         int j = pList->a[i].u.x.iOrderByCol;
 27159         -      if( j ){
        27488  +      char *zName = pList->a[i].zName;
        27489  +      if( j || zName ){
 27160  27490           sqlite3TreeViewPush(pView, 0);
        27491  +      }
        27492  +      if( zName ){
        27493  +        sqlite3TreeViewLine(pView, "AS %s", zName);
        27494  +      }
        27495  +      if( j ){
 27161  27496           sqlite3TreeViewLine(pView, "iOrderByCol=%d", j);
 27162  27497         }
 27163  27498         sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
 27164         -      if( j ) sqlite3TreeViewPop(pView);
        27499  +      if( j || zName ){
        27500  +        sqlite3TreeViewPop(pView);
        27501  +      }
 27165  27502       }
 27166  27503     }
 27167  27504   }
 27168  27505   SQLITE_PRIVATE void sqlite3TreeViewExprList(
 27169  27506     TreeView *pView,
 27170  27507     const ExprList *pList,
 27171  27508     u8 moreToFollow,
................................................................................
 28446  28783       return 1;
 28447  28784     }
 28448  28785     a = (unsigned char *)zLeft;
 28449  28786     b = (unsigned char *)zRight;
 28450  28787     while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
 28451  28788     return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
 28452  28789   }
        28790  +
        28791  +/*
        28792  +** Compute 10 to the E-th power.  Examples:  E==1 results in 10.
        28793  +** E==2 results in 100.  E==50 results in 1.0e50.
        28794  +**
        28795  +** This routine only works for values of E between 1 and 341.
        28796  +*/
        28797  +static LONGDOUBLE_TYPE sqlite3Pow10(int E){
        28798  +#if defined(_MSC_VER)
        28799  +  static const LONGDOUBLE_TYPE x[] = {
        28800  +    1.0e+001,
        28801  +    1.0e+002,
        28802  +    1.0e+004,
        28803  +    1.0e+008,
        28804  +    1.0e+016,
        28805  +    1.0e+032,
        28806  +    1.0e+064,
        28807  +    1.0e+128,
        28808  +    1.0e+256
        28809  +  };
        28810  +  LONGDOUBLE_TYPE r = 1.0;
        28811  +  int i;
        28812  +  assert( E>=0 && E<=307 );
        28813  +  for(i=0; E!=0; i++, E >>=1){
        28814  +    if( E & 1 ) r *= x[i];
        28815  +  }
        28816  +  return r;
        28817  +#else
        28818  +  LONGDOUBLE_TYPE x = 10.0;
        28819  +  LONGDOUBLE_TYPE r = 1.0;
        28820  +  while(1){
        28821  +    if( E & 1 ) r *= x;
        28822  +    E >>= 1;
        28823  +    if( E==0 ) break;
        28824  +    x *= x;
        28825  +  }
        28826  +  return r; 
        28827  +#endif
        28828  +}
 28453  28829   
 28454  28830   /*
 28455  28831   ** The string z[] is an text representation of a real number.
 28456  28832   ** Convert this string to a double and write it into *pResult.
 28457  28833   **
 28458  28834   ** The string z[] is length bytes in length (bytes, not characters) and
 28459  28835   ** uses the encoding enc.  The string is not necessarily zero-terminated.
................................................................................
 28514  28890     }else if( *z=='+' ){
 28515  28891       z+=incr;
 28516  28892     }
 28517  28893   
 28518  28894     /* copy max significant digits to significand */
 28519  28895     while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
 28520  28896       s = s*10 + (*z - '0');
 28521         -    z+=incr, nDigits++;
        28897  +    z+=incr; nDigits++;
 28522  28898     }
 28523  28899   
 28524  28900     /* skip non-significant significand digits
 28525  28901     ** (increase exponent by d to shift decimal left) */
 28526         -  while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
        28902  +  while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; nDigits++; d++; }
 28527  28903     if( z>=zEnd ) goto do_atof_calc;
 28528  28904   
 28529  28905     /* if decimal point is present */
 28530  28906     if( *z=='.' ){
 28531  28907       z+=incr;
 28532  28908       /* copy digits from after decimal to significand
 28533  28909       ** (decrease exponent by d to shift decimal right) */
 28534  28910       while( z<zEnd && sqlite3Isdigit(*z) ){
 28535  28911         if( s<((LARGEST_INT64-9)/10) ){
 28536  28912           s = s*10 + (*z - '0');
 28537  28913           d--;
 28538  28914         }
 28539         -      z+=incr, nDigits++;
        28915  +      z+=incr; nDigits++;
 28540  28916       }
 28541  28917     }
 28542  28918     if( z>=zEnd ) goto do_atof_calc;
 28543  28919   
 28544  28920     /* if exponent is present */
 28545  28921     if( *z=='e' || *z=='E' ){
 28546  28922       z+=incr;
................................................................................
 28602  28978   
 28603  28979       /* adjust the sign of significand */
 28604  28980       s = sign<0 ? -s : s;
 28605  28981   
 28606  28982       if( e==0 ){                                         /*OPTIMIZATION-IF-TRUE*/
 28607  28983         result = (double)s;
 28608  28984       }else{
 28609         -      LONGDOUBLE_TYPE scale = 1.0;
 28610  28985         /* attempt to handle extremely small/large numbers better */
 28611  28986         if( e>307 ){                                      /*OPTIMIZATION-IF-TRUE*/
 28612  28987           if( e<342 ){                                    /*OPTIMIZATION-IF-TRUE*/
 28613         -          while( e%308 ) { scale *= 1.0e+1; e -= 1; }
        28988  +          LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
 28614  28989             if( esign<0 ){
 28615  28990               result = s / scale;
 28616  28991               result /= 1.0e+308;
 28617  28992             }else{
 28618  28993               result = s * scale;
 28619  28994               result *= 1.0e+308;
 28620  28995             }
................................................................................
 28626  29001               result = INFINITY*s;
 28627  29002   #else
 28628  29003               result = 1e308*1e308*s;  /* Infinity */
 28629  29004   #endif
 28630  29005             }
 28631  29006           }
 28632  29007         }else{
 28633         -        /* 1.0e+22 is the largest power of 10 than can be 
 28634         -        ** represented exactly. */
 28635         -        while( e%22 ) { scale *= 1.0e+1; e -= 1; }
 28636         -        while( e>0 ) { scale *= 1.0e+22; e -= 22; }
        29008  +        LONGDOUBLE_TYPE scale = sqlite3Pow10(e);
 28637  29009           if( esign<0 ){
 28638  29010             result = s / scale;
 28639  29011           }else{
 28640  29012             result = s * scale;
 28641  29013           }
 28642  29014         }
 28643  29015       }
................................................................................
 30087  30459       /*  89 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 30088  30460       /*  90 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 30089  30461       /*  91 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 30090  30462       /*  92 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 30091  30463       /*  93 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 30092  30464       /*  94 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 30093  30465       /*  95 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 30094         -    /*  96 */ "Column"           OpHelp("r[P3]=PX"),
        30466  +    /*  96 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
 30095  30467       /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 30096         -    /*  98 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 30097         -    /*  99 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 30098         -    /* 100 */ "Count"            OpHelp("r[P2]=count()"),
 30099         -    /* 101 */ "ReadCookie"       OpHelp(""),
 30100         -    /* 102 */ "SetCookie"        OpHelp(""),
 30101         -    /* 103 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 30102         -    /* 104 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 30103         -    /* 105 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 30104         -    /* 106 */ "OpenDup"          OpHelp(""),
 30105         -    /* 107 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 30106         -    /* 108 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 30107         -    /* 109 */ "SorterOpen"       OpHelp(""),
 30108         -    /* 110 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 30109         -    /* 111 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 30110         -    /* 112 */ "Close"            OpHelp(""),
 30111         -    /* 113 */ "ColumnsUsed"      OpHelp(""),
 30112         -    /* 114 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 30113         -    /* 115 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 30114         -    /* 116 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 30115         -    /* 117 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 30116         -    /* 118 */ "Delete"           OpHelp(""),
 30117         -    /* 119 */ "ResetCount"       OpHelp(""),
 30118         -    /* 120 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 30119         -    /* 121 */ "SorterData"       OpHelp("r[P2]=data"),
 30120         -    /* 122 */ "RowData"          OpHelp("r[P2]=data"),
 30121         -    /* 123 */ "Rowid"            OpHelp("r[P2]=rowid"),
 30122         -    /* 124 */ "NullRow"          OpHelp(""),
 30123         -    /* 125 */ "SeekEnd"          OpHelp(""),
 30124         -    /* 126 */ "SorterInsert"     OpHelp("key=r[P2]"),
 30125         -    /* 127 */ "IdxInsert"        OpHelp("key=r[P2]"),
 30126         -    /* 128 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 30127         -    /* 129 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 30128         -    /* 130 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 30129         -    /* 131 */ "Destroy"          OpHelp(""),
        30468  +    /*  98 */ "Column"           OpHelp("r[P3]=PX"),
        30469  +    /*  99 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
        30470  +    /* 100 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        30471  +    /* 101 */ "Count"            OpHelp("r[P2]=count()"),
        30472  +    /* 102 */ "ReadCookie"       OpHelp(""),
        30473  +    /* 103 */ "SetCookie"        OpHelp(""),
        30474  +    /* 104 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        30475  +    /* 105 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        30476  +    /* 106 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        30477  +    /* 107 */ "OpenDup"          OpHelp(""),
        30478  +    /* 108 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
        30479  +    /* 109 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
        30480  +    /* 110 */ "SorterOpen"       OpHelp(""),
        30481  +    /* 111 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
        30482  +    /* 112 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
        30483  +    /* 113 */ "Close"            OpHelp(""),
        30484  +    /* 114 */ "ColumnsUsed"      OpHelp(""),
        30485  +    /* 115 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
        30486  +    /* 116 */ "NewRowid"         OpHelp("r[P2]=rowid"),
        30487  +    /* 117 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
        30488  +    /* 118 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
        30489  +    /* 119 */ "Delete"           OpHelp(""),
        30490  +    /* 120 */ "ResetCount"       OpHelp(""),
        30491  +    /* 121 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        30492  +    /* 122 */ "SorterData"       OpHelp("r[P2]=data"),
        30493  +    /* 123 */ "RowData"          OpHelp("r[P2]=data"),
        30494  +    /* 124 */ "Rowid"            OpHelp("r[P2]=rowid"),
        30495  +    /* 125 */ "NullRow"          OpHelp(""),
        30496  +    /* 126 */ "SeekEnd"          OpHelp(""),
        30497  +    /* 127 */ "SorterInsert"     OpHelp("key=r[P2]"),
        30498  +    /* 128 */ "IdxInsert"        OpHelp("key=r[P2]"),
        30499  +    /* 129 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        30500  +    /* 130 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
        30501  +    /* 131 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 30130  30502       /* 132 */ "Real"             OpHelp("r[P2]=P4"),
 30131         -    /* 133 */ "Clear"            OpHelp(""),
 30132         -    /* 134 */ "ResetSorter"      OpHelp(""),
 30133         -    /* 135 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 30134         -    /* 136 */ "SqlExec"          OpHelp(""),
 30135         -    /* 137 */ "ParseSchema"      OpHelp(""),
 30136         -    /* 138 */ "LoadAnalysis"     OpHelp(""),
 30137         -    /* 139 */ "DropTable"        OpHelp(""),
 30138         -    /* 140 */ "DropIndex"        OpHelp(""),
 30139         -    /* 141 */ "DropTrigger"      OpHelp(""),
 30140         -    /* 142 */ "IntegrityCk"      OpHelp(""),
 30141         -    /* 143 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 30142         -    /* 144 */ "Param"            OpHelp(""),
 30143         -    /* 145 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 30144         -    /* 146 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 30145         -    /* 147 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
 30146         -    /* 148 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
 30147         -    /* 149 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 30148         -    /* 150 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 30149         -    /* 151 */ "Expire"           OpHelp(""),
 30150         -    /* 152 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 30151         -    /* 153 */ "VBegin"           OpHelp(""),
 30152         -    /* 154 */ "VCreate"          OpHelp(""),
 30153         -    /* 155 */ "VDestroy"         OpHelp(""),
 30154         -    /* 156 */ "VOpen"            OpHelp(""),
 30155         -    /* 157 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 30156         -    /* 158 */ "VRename"          OpHelp(""),
 30157         -    /* 159 */ "Pagecount"        OpHelp(""),
 30158         -    /* 160 */ "MaxPgcnt"         OpHelp(""),
 30159         -    /* 161 */ "PureFunc0"        OpHelp(""),
 30160         -    /* 162 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
 30161         -    /* 163 */ "PureFunc"         OpHelp(""),
 30162         -    /* 164 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 30163         -    /* 165 */ "CursorHint"       OpHelp(""),
 30164         -    /* 166 */ "Noop"             OpHelp(""),
 30165         -    /* 167 */ "Explain"          OpHelp(""),
        30503  +    /* 133 */ "Destroy"          OpHelp(""),
        30504  +    /* 134 */ "Clear"            OpHelp(""),
        30505  +    /* 135 */ "ResetSorter"      OpHelp(""),
        30506  +    /* 136 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
        30507  +    /* 137 */ "SqlExec"          OpHelp(""),
        30508  +    /* 138 */ "ParseSchema"      OpHelp(""),
        30509  +    /* 139 */ "LoadAnalysis"     OpHelp(""),
        30510  +    /* 140 */ "DropTable"        OpHelp(""),
        30511  +    /* 141 */ "DropIndex"        OpHelp(""),
        30512  +    /* 142 */ "DropTrigger"      OpHelp(""),
        30513  +    /* 143 */ "IntegrityCk"      OpHelp(""),
        30514  +    /* 144 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        30515  +    /* 145 */ "Param"            OpHelp(""),
        30516  +    /* 146 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        30517  +    /* 147 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        30518  +    /* 148 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
        30519  +    /* 149 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
        30520  +    /* 150 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
        30521  +    /* 151 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        30522  +    /* 152 */ "Expire"           OpHelp(""),
        30523  +    /* 153 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        30524  +    /* 154 */ "VBegin"           OpHelp(""),
        30525  +    /* 155 */ "VCreate"          OpHelp(""),
        30526  +    /* 156 */ "VDestroy"         OpHelp(""),
        30527  +    /* 157 */ "VOpen"            OpHelp(""),
        30528  +    /* 158 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        30529  +    /* 159 */ "VRename"          OpHelp(""),
        30530  +    /* 160 */ "Pagecount"        OpHelp(""),
        30531  +    /* 161 */ "MaxPgcnt"         OpHelp(""),
        30532  +    /* 162 */ "PureFunc0"        OpHelp(""),
        30533  +    /* 163 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
        30534  +    /* 164 */ "PureFunc"         OpHelp(""),
        30535  +    /* 165 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
        30536  +    /* 166 */ "Trace"            OpHelp(""),
        30537  +    /* 167 */ "CursorHint"       OpHelp(""),
        30538  +    /* 168 */ "Noop"             OpHelp(""),
        30539  +    /* 169 */ "Explain"          OpHelp(""),
 30166  30540     };
 30167  30541     return azName[i];
 30168  30542   }
 30169  30543   #endif
 30170  30544   
 30171  30545   /************** End of opcodes.c *********************************************/
 30172  30546   /************** Begin file os_unix.c *****************************************/
................................................................................
 30849  31223   #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
 30850  31224   
 30851  31225   #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
 30852  31226     { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
 30853  31227   #else
 30854  31228     { "munmap",       (sqlite3_syscall_ptr)0,               0 },
 30855  31229   #endif
 30856         -#define osMunmap ((void*(*)(void*,size_t))aSyscall[23].pCurrent)
        31230  +#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent)
 30857  31231   
 30858  31232   #if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
 30859  31233     { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
 30860  31234   #else
 30861  31235     { "mremap",       (sqlite3_syscall_ptr)0,               0 },
 30862  31236   #endif
 30863  31237   #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
................................................................................
 30879  31253   #if defined(HAVE_LSTAT)
 30880  31254     { "lstat",         (sqlite3_syscall_ptr)lstat,          0 },
 30881  31255   #else
 30882  31256     { "lstat",         (sqlite3_syscall_ptr)0,              0 },
 30883  31257   #endif
 30884  31258   #define osLstat      ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
 30885  31259   
        31260  +#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
 30886  31261     { "ioctl",         (sqlite3_syscall_ptr)ioctl,          0 },
        31262  +#else
        31263  +  { "ioctl",         (sqlite3_syscall_ptr)0,              0 },
        31264  +#endif
 30887  31265   #define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
 30888  31266   
 30889  31267   }; /* End of the overrideable system calls */
 30890  31268   
 30891  31269   
 30892  31270   /*
 30893  31271   ** On some systems, calls to fchown() will trigger a message in a security
................................................................................
 34312  34690     if( pFile->sectorSize == 0 ){
 34313  34691       struct statvfs fsInfo;
 34314  34692          
 34315  34693       /* Set defaults for non-supported filesystems */
 34316  34694       pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
 34317  34695       pFile->deviceCharacteristics = 0;
 34318  34696       if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
 34319         -      return pFile->sectorSize;
        34697  +      return;
 34320  34698       }
 34321  34699   
 34322  34700       if( !strcmp(fsInfo.f_basetype, "tmp") ) {
 34323  34701         pFile->sectorSize = fsInfo.f_bsize;
 34324  34702         pFile->deviceCharacteristics =
 34325  34703           SQLITE_IOCAP_ATOMIC4K |       /* All ram filesystem writes are atomic */
 34326  34704           SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
................................................................................
 34470  34848     unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
 34471  34849     sqlite3_mutex *mutex;      /* Mutex to access this object */
 34472  34850     char *zFilename;           /* Name of the mmapped file */
 34473  34851     int h;                     /* Open file descriptor */
 34474  34852     int szRegion;              /* Size of shared-memory regions */
 34475  34853     u16 nRegion;               /* Size of array apRegion */
 34476  34854     u8 isReadonly;             /* True if read-only */
        34855  +  u8 isUnlocked;             /* True if no DMS lock held */
 34477  34856     char **apRegion;           /* Array of mapped shared-memory regions */
 34478  34857     int nRef;                  /* Number of unixShm objects pointing to this */
 34479  34858     unixShm *pFirst;           /* All unixShm objects pointing to this */
 34480  34859   #ifdef SQLITE_DEBUG
 34481  34860     u8 exclMask;               /* Mask of exclusive locks held */
 34482  34861     u8 sharedMask;             /* Mask of shared locks held */
 34483  34862     u8 nextShmId;              /* Next available unixShm.id value */
................................................................................
 34526  34905   ){
 34527  34906     unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
 34528  34907     struct flock f;        /* The posix advisory locking structure */
 34529  34908     int rc = SQLITE_OK;    /* Result code form fcntl() */
 34530  34909   
 34531  34910     /* Access to the unixShmNode object is serialized by the caller */
 34532  34911     pShmNode = pFile->pInode->pShmNode;
 34533         -  assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
        34912  +  assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->mutex) );
 34534  34913   
 34535  34914     /* Shared locks never span more than one byte */
 34536  34915     assert( n==1 || lockType!=F_RDLCK );
 34537  34916   
 34538  34917     /* Locks are within range */
 34539  34918     assert( n>=1 && n<=SQLITE_SHM_NLOCK );
 34540  34919   
................................................................................
 34631  35010         robust_close(pFd, p->h, __LINE__);
 34632  35011         p->h = -1;
 34633  35012       }
 34634  35013       p->pInode->pShmNode = 0;
 34635  35014       sqlite3_free(p);
 34636  35015     }
 34637  35016   }
        35017  +
        35018  +/*
        35019  +** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
        35020  +** take it now. Return SQLITE_OK if successful, or an SQLite error
        35021  +** code otherwise.
        35022  +**
        35023  +** If the DMS cannot be locked because this is a readonly_shm=1 
        35024  +** connection and no other process already holds a lock, return
        35025  +** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
        35026  +*/
        35027  +static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
        35028  +  struct flock lock;
        35029  +  int rc = SQLITE_OK;
        35030  +
        35031  +  /* Use F_GETLK to determine the locks other processes are holding
        35032  +  ** on the DMS byte. If it indicates that another process is holding
        35033  +  ** a SHARED lock, then this process may also take a SHARED lock
        35034  +  ** and proceed with opening the *-shm file. 
        35035  +  **
        35036  +  ** Or, if no other process is holding any lock, then this process
        35037  +  ** is the first to open it. In this case take an EXCLUSIVE lock on the
        35038  +  ** DMS byte and truncate the *-shm file to zero bytes in size. Then
        35039  +  ** downgrade to a SHARED lock on the DMS byte.
        35040  +  **
        35041  +  ** If another process is holding an EXCLUSIVE lock on the DMS byte,
        35042  +  ** return SQLITE_BUSY to the caller (it will try again). An earlier
        35043  +  ** version of this code attempted the SHARED lock at this point. But
        35044  +  ** this introduced a subtle race condition: if the process holding
        35045  +  ** EXCLUSIVE failed just before truncating the *-shm file, then this
        35046  +  ** process might open and use the *-shm file without truncating it.
        35047  +  ** And if the *-shm file has been corrupted by a power failure or
        35048  +  ** system crash, the database itself may also become corrupt.  */
        35049  +  lock.l_whence = SEEK_SET;
        35050  +  lock.l_start = UNIX_SHM_DMS;
        35051  +  lock.l_len = 1;
        35052  +  lock.l_type = F_WRLCK;
        35053  +  if( osFcntl(pShmNode->h, F_GETLK, &lock)!=0 ) {
        35054  +    rc = SQLITE_IOERR_LOCK;
        35055  +  }else if( lock.l_type==F_UNLCK ){
        35056  +    if( pShmNode->isReadonly ){
        35057  +      pShmNode->isUnlocked = 1;
        35058  +      rc = SQLITE_READONLY_CANTINIT;
        35059  +    }else{
        35060  +      rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
        35061  +      if( rc==SQLITE_OK && robust_ftruncate(pShmNode->h, 0) ){
        35062  +        rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
        35063  +      }
        35064  +    }
        35065  +  }else if( lock.l_type==F_WRLCK ){
        35066  +    rc = SQLITE_BUSY;
        35067  +  }
        35068  +
        35069  +  if( rc==SQLITE_OK ){
        35070  +    assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK );
        35071  +    rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
        35072  +  }
        35073  +  return rc;
        35074  +}
 34638  35075   
 34639  35076   /*
 34640  35077   ** Open a shared-memory area associated with open database file pDbFd.  
 34641  35078   ** This particular implementation uses mmapped files.
 34642  35079   **
 34643  35080   ** The file used to implement shared-memory is in the same directory
 34644  35081   ** as the open database file and has the same name as the open database
................................................................................
 34670  35107   ** that no other processes are able to read or write the database.  In
 34671  35108   ** that case, we do not really need shared memory.  No shared memory
 34672  35109   ** file is created.  The shared memory will be simulated with heap memory.
 34673  35110   */
 34674  35111   static int unixOpenSharedMemory(unixFile *pDbFd){
 34675  35112     struct unixShm *p = 0;          /* The connection to be opened */
 34676  35113     struct unixShmNode *pShmNode;   /* The underlying mmapped file */
 34677         -  int rc;                         /* Result code */
        35114  +  int rc = SQLITE_OK;             /* Result code */
 34678  35115     unixInodeInfo *pInode;          /* The inode of fd */
 34679         -  char *zShmFilename;             /* Name of the file used for SHM */
        35116  +  char *zShm;             /* Name of the file used for SHM */
 34680  35117     int nShmFilename;               /* Size of the SHM filename in bytes */
 34681  35118   
 34682  35119     /* Allocate space for the new unixShm object. */
 34683  35120     p = sqlite3_malloc64( sizeof(*p) );
 34684  35121     if( p==0 ) return SQLITE_NOMEM_BKPT;
 34685  35122     memset(p, 0, sizeof(*p));
 34686  35123     assert( pDbFd->pShm==0 );
................................................................................
 34713  35150   #endif
 34714  35151       pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
 34715  35152       if( pShmNode==0 ){
 34716  35153         rc = SQLITE_NOMEM_BKPT;
 34717  35154         goto shm_open_err;
 34718  35155       }
 34719  35156       memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
 34720         -    zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
        35157  +    zShm = pShmNode->zFilename = (char*)&pShmNode[1];
 34721  35158   #ifdef SQLITE_SHM_DIRECTORY
 34722         -    sqlite3_snprintf(nShmFilename, zShmFilename, 
        35159  +    sqlite3_snprintf(nShmFilename, zShm, 
 34723  35160                        SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
 34724  35161                        (u32)sStat.st_ino, (u32)sStat.st_dev);
 34725  35162   #else
 34726         -    sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath);
 34727         -    sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
        35163  +    sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
        35164  +    sqlite3FileSuffix3(pDbFd->zPath, zShm);
 34728  35165   #endif
 34729  35166       pShmNode->h = -1;
 34730  35167       pDbFd->pInode->pShmNode = pShmNode;
 34731  35168       pShmNode->pInode = pDbFd->pInode;
 34732  35169       if( sqlite3GlobalConfig.bCoreMutex ){
 34733  35170         pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 34734  35171         if( pShmNode->mutex==0 ){
 34735  35172           rc = SQLITE_NOMEM_BKPT;
 34736  35173           goto shm_open_err;
 34737  35174         }
 34738  35175       }
 34739  35176   
 34740  35177       if( pInode->bProcessLock==0 ){
 34741         -      int openFlags = O_RDWR | O_CREAT;
 34742         -      if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
 34743         -        openFlags = O_RDONLY;
 34744         -        pShmNode->isReadonly = 1;
        35178  +      if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
        35179  +        pShmNode->h = robust_open(zShm, O_RDWR|O_CREAT, (sStat.st_mode&0777));
 34745  35180         }
 34746         -      pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
 34747  35181         if( pShmNode->h<0 ){
 34748         -        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
 34749         -        goto shm_open_err;
        35182  +        pShmNode->h = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777));
        35183  +        if( pShmNode->h<0 ){
        35184  +          rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
        35185  +          goto shm_open_err;
        35186  +        }
        35187  +        pShmNode->isReadonly = 1;
 34750  35188         }
 34751  35189   
 34752  35190         /* If this process is running as root, make sure that the SHM file
 34753  35191         ** is owned by the same user that owns the original database.  Otherwise,
 34754  35192         ** the original owner will not be able to connect.
 34755  35193         */
 34756  35194         robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
 34757         -  
 34758         -      /* Check to see if another process is holding the dead-man switch.
 34759         -      ** If not, truncate the file to zero length. 
 34760         -      */
 34761         -      rc = SQLITE_OK;
 34762         -      if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
 34763         -        if( robust_ftruncate(pShmNode->h, 0) ){
 34764         -          rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
 34765         -        }
 34766         -      }
 34767         -      if( rc==SQLITE_OK ){
 34768         -        rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
 34769         -      }
 34770         -      if( rc ) goto shm_open_err;
        35195  +
        35196  +      rc = unixLockSharedMemory(pDbFd, pShmNode);
        35197  +      if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
 34771  35198       }
 34772  35199     }
 34773  35200   
 34774  35201     /* Make the new connection a child of the unixShmNode */
 34775  35202     p->pShmNode = pShmNode;
 34776  35203   #ifdef SQLITE_DEBUG
 34777  35204     p->id = pShmNode->nextShmId++;
................................................................................
 34787  35214     ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
 34788  35215     ** mutex.
 34789  35216     */
 34790  35217     sqlite3_mutex_enter(pShmNode->mutex);
 34791  35218     p->pNext = pShmNode->pFirst;
 34792  35219     pShmNode->pFirst = p;
 34793  35220     sqlite3_mutex_leave(pShmNode->mutex);
 34794         -  return SQLITE_OK;
        35221  +  return rc;
 34795  35222   
 34796  35223     /* Jump here on any error */
 34797  35224   shm_open_err:
 34798  35225     unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
 34799  35226     sqlite3_free(p);
 34800  35227     unixLeaveMutex();
 34801  35228     return rc;
................................................................................
 34839  35266       rc = unixOpenSharedMemory(pDbFd);
 34840  35267       if( rc!=SQLITE_OK ) return rc;
 34841  35268     }
 34842  35269   
 34843  35270     p = pDbFd->pShm;
 34844  35271     pShmNode = p->pShmNode;
 34845  35272     sqlite3_mutex_enter(pShmNode->mutex);
        35273  +  if( pShmNode->isUnlocked ){
        35274  +    rc = unixLockSharedMemory(pDbFd, pShmNode);
        35275  +    if( rc!=SQLITE_OK ) goto shmpage_out;
        35276  +    pShmNode->isUnlocked = 0;
        35277  +  }
 34846  35278     assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 34847  35279     assert( pShmNode->pInode==pDbFd->pInode );
 34848  35280     assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 34849  35281     assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
 34850  35282   
 34851  35283     /* Minimum number of regions required to be mapped. */
 34852  35284     nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
................................................................................
 36107  36539     struct statfs fsInfo;
 36108  36540   #endif
 36109  36541   
 36110  36542     /* If creating a master or main-file journal, this function will open
 36111  36543     ** a file-descriptor on the directory too. The first time unixSync()
 36112  36544     ** is called the directory file descriptor will be fsync()ed and close()d.
 36113  36545     */
 36114         -  int syncDir = (isCreate && (
        36546  +  int isNewJrnl = (isCreate && (
 36115  36547           eType==SQLITE_OPEN_MASTER_JOURNAL 
 36116  36548        || eType==SQLITE_OPEN_MAIN_JOURNAL 
 36117  36549        || eType==SQLITE_OPEN_WAL
 36118  36550     ));
 36119  36551   
 36120  36552     /* If argument zPath is a NULL pointer, this function is required to open
 36121  36553     ** a temporary file. Use this buffer to store the file name in.
................................................................................
 36177  36609       /* Database filenames are double-zero terminated if they are not
 36178  36610       ** URIs with parameters.  Hence, they can always be passed into
 36179  36611       ** sqlite3_uri_parameter(). */
 36180  36612       assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
 36181  36613   
 36182  36614     }else if( !zName ){
 36183  36615       /* If zName is NULL, the upper layer is requesting a temp file. */
 36184         -    assert(isDelete && !syncDir);
        36616  +    assert(isDelete && !isNewJrnl);
 36185  36617       rc = unixGetTempname(pVfs->mxPathname, zTmpname);
 36186  36618       if( rc!=SQLITE_OK ){
 36187  36619         return rc;
 36188  36620       }
 36189  36621       zName = zTmpname;
 36190  36622   
 36191  36623       /* Generated temporary filenames are always double-zero terminated
................................................................................
 36212  36644         assert( !p->pPreallocatedUnused );
 36213  36645         assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
 36214  36646         return rc;
 36215  36647       }
 36216  36648       fd = robust_open(zName, openFlags, openMode);
 36217  36649       OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
 36218  36650       assert( !isExclusive || (openFlags & O_CREAT)!=0 );
 36219         -    if( fd<0 && errno!=EISDIR && isReadWrite ){
 36220         -      /* Failed to open the file for read/write access. Try read-only. */
 36221         -      flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
 36222         -      openFlags &= ~(O_RDWR|O_CREAT);
 36223         -      flags |= SQLITE_OPEN_READONLY;
 36224         -      openFlags |= O_RDONLY;
 36225         -      isReadonly = 1;
 36226         -      fd = robust_open(zName, openFlags, openMode);
        36651  +    if( fd<0 ){
        36652  +      if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
        36653  +        /* If unable to create a journal because the directory is not
        36654  +        ** writable, change the error code to indicate that. */
        36655  +        rc = SQLITE_READONLY_DIRECTORY;
        36656  +      }else if( errno!=EISDIR && isReadWrite ){
        36657  +        /* Failed to open the file for read/write access. Try read-only. */
        36658  +        flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
        36659  +        openFlags &= ~(O_RDWR|O_CREAT);
        36660  +        flags |= SQLITE_OPEN_READONLY;
        36661  +        openFlags |= O_RDONLY;
        36662  +        isReadonly = 1;
        36663  +        fd = robust_open(zName, openFlags, openMode);
        36664  +      }
 36227  36665       }
 36228  36666       if( fd<0 ){
 36229         -      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
        36667  +      int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
        36668  +      if( rc==SQLITE_OK ) rc = rc2;
 36230  36669         goto open_finished;
 36231  36670       }
 36232  36671   
 36233  36672       /* If this process is running as root and if creating a new rollback
 36234  36673       ** journal or WAL file, set the ownership of the journal or WAL to be
 36235  36674       ** the same as the original database.
 36236  36675       */
................................................................................
 36282  36721   #endif
 36283  36722   
 36284  36723     /* Set up appropriate ctrlFlags */
 36285  36724     if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
 36286  36725     if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
 36287  36726     noLock = eType!=SQLITE_OPEN_MAIN_DB;
 36288  36727     if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
 36289         -  if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
        36728  +  if( isNewJrnl )               ctrlFlags |= UNIXFILE_DIRSYNC;
 36290  36729     if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
 36291  36730   
 36292  36731   #if SQLITE_ENABLE_LOCKING_STYLE
 36293  36732   #if SQLITE_PREFER_PROXY_LOCKING
 36294  36733     isAutoProxy = 1;
 36295  36734   #endif
 36296  36735     if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
................................................................................
 41913  42352   struct winShmNode {
 41914  42353     sqlite3_mutex *mutex;      /* Mutex to access this object */
 41915  42354     char *zFilename;           /* Name of the file */
 41916  42355     winFile hFile;             /* File handle from winOpen */
 41917  42356   
 41918  42357     int szRegion;              /* Size of shared-memory regions */
 41919  42358     int nRegion;               /* Size of array apRegion */
        42359  +  u8 isReadonly;             /* True if read-only */
        42360  +  u8 isUnlocked;             /* True if no DMS lock held */
        42361  +
 41920  42362     struct ShmRegion {
 41921  42363       HANDLE hMap;             /* File handle from CreateFileMapping */
 41922  42364       void *pMap;
 41923  42365     } *aRegion;
 41924  42366     DWORD lastErrno;           /* The Windows errno from the last I/O error */
 41925  42367   
 41926  42368     int nRef;                  /* Number of winShm objects pointing to this */
................................................................................
 41979  42421     int lockType,         /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
 41980  42422     int ofst,             /* Offset to first byte to be locked/unlocked */
 41981  42423     int nByte             /* Number of bytes to lock or unlock */
 41982  42424   ){
 41983  42425     int rc = 0;           /* Result code form Lock/UnlockFileEx() */
 41984  42426   
 41985  42427     /* Access to the winShmNode object is serialized by the caller */
 41986         -  assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
        42428  +  assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
 41987  42429   
 41988  42430     OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
 41989  42431              pFile->hFile.h, lockType, ofst, nByte));
 41990  42432   
 41991  42433     /* Release/Acquire the system-level lock */
 41992  42434     if( lockType==WINSHM_UNLCK ){
 41993  42435       rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
................................................................................
 42059  42501         sqlite3_free(p->aRegion);
 42060  42502         sqlite3_free(p);
 42061  42503       }else{
 42062  42504         pp = &p->pNext;
 42063  42505       }
 42064  42506     }
 42065  42507   }
        42508  +
        42509  +/*
        42510  +** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
        42511  +** take it now. Return SQLITE_OK if successful, or an SQLite error
        42512  +** code otherwise.
        42513  +**
        42514  +** If the DMS cannot be locked because this is a readonly_shm=1
        42515  +** connection and no other process already holds a lock, return
        42516  +** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
        42517  +*/
        42518  +static int winLockSharedMemory(winShmNode *pShmNode){
        42519  +  int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
        42520  +
        42521  +  if( rc==SQLITE_OK ){
        42522  +    if( pShmNode->isReadonly ){
        42523  +      pShmNode->isUnlocked = 1;
        42524  +      winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
        42525  +      return SQLITE_READONLY_CANTINIT;
        42526  +    }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
        42527  +      winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
        42528  +      return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
        42529  +                         "winLockSharedMemory", pShmNode->zFilename);
        42530  +    }
        42531  +  }
        42532  +
        42533  +  if( rc==SQLITE_OK ){
        42534  +    winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
        42535  +  }
        42536  +
        42537  +  return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
        42538  +}
 42066  42539   
 42067  42540   /*
 42068  42541   ** Open the shared-memory area associated with database file pDbFd.
 42069  42542   **
 42070  42543   ** When opening a new shared-memory file, if no other instances of that
 42071  42544   ** file are currently open, in this process or in other processes, then
 42072  42545   ** the file must be truncated to zero length or have its header cleared.
 42073  42546   */
 42074  42547   static int winOpenSharedMemory(winFile *pDbFd){
 42075  42548     struct winShm *p;                  /* The connection to be opened */
 42076         -  struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
 42077         -  int rc;                            /* Result code */
 42078         -  struct winShmNode *pNew;           /* Newly allocated winShmNode */
        42549  +  winShmNode *pShmNode = 0;          /* The underlying mmapped file */
        42550  +  int rc = SQLITE_OK;                /* Result code */
        42551  +  winShmNode *pNew;                  /* Newly allocated winShmNode */
 42079  42552     int nName;                         /* Size of zName in bytes */
 42080  42553   
 42081  42554     assert( pDbFd->pShm==0 );    /* Not previously opened */
 42082  42555   
 42083  42556     /* Allocate space for the new sqlite3_shm object.  Also speculatively
 42084  42557     ** allocate space for a new winShmNode and filename.
 42085  42558     */
................................................................................
 42104  42577       ** use FILE_ID_BOTH_DIR_INFO Structure.
 42105  42578       */
 42106  42579       if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
 42107  42580     }
 42108  42581     if( pShmNode ){
 42109  42582       sqlite3_free(pNew);
 42110  42583     }else{
        42584  +    int inFlags = SQLITE_OPEN_WAL;
        42585  +    int outFlags = 0;
        42586  +
 42111  42587       pShmNode = pNew;
 42112  42588       pNew = 0;
 42113  42589       ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
 42114  42590       pShmNode->pNext = winShmNodeList;
 42115  42591       winShmNodeList = pShmNode;
 42116  42592   
 42117  42593       if( sqlite3GlobalConfig.bCoreMutex ){
................................................................................
 42118  42594         pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 42119  42595         if( pShmNode->mutex==0 ){
 42120  42596           rc = SQLITE_IOERR_NOMEM_BKPT;
 42121  42597           goto shm_open_err;
 42122  42598         }
 42123  42599       }
 42124  42600   
 42125         -    rc = winOpen(pDbFd->pVfs,
 42126         -                 pShmNode->zFilename,             /* Name of the file (UTF-8) */
 42127         -                 (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
 42128         -                 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
 42129         -                 0);
 42130         -    if( SQLITE_OK!=rc ){
        42601  +    if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
        42602  +      inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
        42603  +    }else{
        42604  +      inFlags |= SQLITE_OPEN_READONLY;
        42605  +    }
        42606  +    rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
        42607  +                 (sqlite3_file*)&pShmNode->hFile,
        42608  +                 inFlags, &outFlags);
        42609  +    if( rc!=SQLITE_OK ){
        42610  +      rc = winLogError(rc, osGetLastError(), "winOpenShm",
        42611  +                       pShmNode->zFilename);
 42131  42612         goto shm_open_err;
 42132  42613       }
        42614  +    if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
 42133  42615   
 42134         -    /* Check to see if another process is holding the dead-man switch.
 42135         -    ** If not, truncate the file to zero length.
 42136         -    */
 42137         -    if( winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
 42138         -      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
 42139         -      if( rc!=SQLITE_OK ){
 42140         -        rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
 42141         -                         "winOpenShm", pDbFd->zPath);
 42142         -      }
 42143         -    }
 42144         -    if( rc==SQLITE_OK ){
 42145         -      winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
 42146         -      rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
 42147         -    }
 42148         -    if( rc ) goto shm_open_err;
        42616  +    rc = winLockSharedMemory(pShmNode);
        42617  +    if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
 42149  42618     }
 42150  42619   
 42151  42620     /* Make the new connection a child of the winShmNode */
 42152  42621     p->pShmNode = pShmNode;
 42153  42622   #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
 42154  42623     p->id = pShmNode->nextShmId++;
 42155  42624   #endif
................................................................................
 42164  42633     ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
 42165  42634     ** mutex.
 42166  42635     */
 42167  42636     sqlite3_mutex_enter(pShmNode->mutex);
 42168  42637     p->pNext = pShmNode->pFirst;
 42169  42638     pShmNode->pFirst = p;
 42170  42639     sqlite3_mutex_leave(pShmNode->mutex);
 42171         -  return SQLITE_OK;
        42640  +  return rc;
 42172  42641   
 42173  42642     /* Jump here on any error */
 42174  42643   shm_open_err:
 42175  42644     winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
 42176  42645     winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
 42177  42646     sqlite3_free(p);
 42178  42647     sqlite3_free(pNew);
................................................................................
 42368  42837     int szRegion,                   /* Size of regions */
 42369  42838     int isWrite,                    /* True to extend file if necessary */
 42370  42839     void volatile **pp              /* OUT: Mapped memory */
 42371  42840   ){
 42372  42841     winFile *pDbFd = (winFile*)fd;
 42373  42842     winShm *pShm = pDbFd->pShm;
 42374  42843     winShmNode *pShmNode;
        42844  +  DWORD protect = PAGE_READWRITE;
        42845  +  DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
 42375  42846     int rc = SQLITE_OK;
 42376  42847   
 42377  42848     if( !pShm ){
 42378  42849       rc = winOpenSharedMemory(pDbFd);
 42379  42850       if( rc!=SQLITE_OK ) return rc;
 42380  42851       pShm = pDbFd->pShm;
 42381  42852     }
 42382  42853     pShmNode = pShm->pShmNode;
 42383  42854   
 42384  42855     sqlite3_mutex_enter(pShmNode->mutex);
        42856  +  if( pShmNode->isUnlocked ){
        42857  +    rc = winLockSharedMemory(pShmNode);
        42858  +    if( rc!=SQLITE_OK ) goto shmpage_out;
        42859  +    pShmNode->isUnlocked = 0;
        42860  +  }
 42385  42861     assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 42386  42862   
 42387  42863     if( pShmNode->nRegion<=iRegion ){
 42388  42864       struct ShmRegion *apNew;           /* New aRegion[] array */
 42389  42865       int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
 42390  42866       sqlite3_int64 sz;                  /* Current size of wal-index file */
 42391  42867   
................................................................................
 42423  42899           pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
 42424  42900       );
 42425  42901       if( !apNew ){
 42426  42902         rc = SQLITE_IOERR_NOMEM_BKPT;
 42427  42903         goto shmpage_out;
 42428  42904       }
 42429  42905       pShmNode->aRegion = apNew;
        42906  +
        42907  +    if( pShmNode->isReadonly ){
        42908  +      protect = PAGE_READONLY;
        42909  +      flags = FILE_MAP_READ;
        42910  +    }
 42430  42911   
 42431  42912       while( pShmNode->nRegion<=iRegion ){
 42432  42913         HANDLE hMap = NULL;         /* file-mapping handle */
 42433  42914         void *pMap = 0;             /* Mapped memory region */
 42434  42915   
 42435  42916   #if SQLITE_OS_WINRT
 42436  42917         hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
 42437         -          NULL, PAGE_READWRITE, nByte, NULL
        42918  +          NULL, protect, nByte, NULL
 42438  42919         );
 42439  42920   #elif defined(SQLITE_WIN32_HAS_WIDE)
 42440  42921         hMap = osCreateFileMappingW(pShmNode->hFile.h,
 42441         -          NULL, PAGE_READWRITE, 0, nByte, NULL
        42922  +          NULL, protect, 0, nByte, NULL
 42442  42923         );
 42443  42924   #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
 42444  42925         hMap = osCreateFileMappingA(pShmNode->hFile.h,
 42445         -          NULL, PAGE_READWRITE, 0, nByte, NULL
        42926  +          NULL, protect, 0, nByte, NULL
 42446  42927         );
 42447  42928   #endif
 42448  42929         OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
 42449  42930                  osGetCurrentProcessId(), pShmNode->nRegion, nByte,
 42450  42931                  hMap ? "ok" : "failed"));
 42451  42932         if( hMap ){
 42452  42933           int iOffset = pShmNode->nRegion*szRegion;
 42453  42934           int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
 42454  42935   #if SQLITE_OS_WINRT
 42455         -        pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
        42936  +        pMap = osMapViewOfFileFromApp(hMap, flags,
 42456  42937               iOffset - iOffsetShift, szRegion + iOffsetShift
 42457  42938           );
 42458  42939   #else
 42459         -        pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
        42940  +        pMap = osMapViewOfFile(hMap, flags,
 42460  42941               0, iOffset - iOffsetShift, szRegion + iOffsetShift
 42461  42942           );
 42462  42943   #endif
 42463  42944           OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
 42464  42945                    osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
 42465  42946                    szRegion, pMap ? "ok" : "failed"));
 42466  42947         }
................................................................................
 42483  42964       int iOffset = iRegion*szRegion;
 42484  42965       int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
 42485  42966       char *p = (char *)pShmNode->aRegion[iRegion].pMap;
 42486  42967       *pp = (void *)&p[iOffsetShift];
 42487  42968     }else{
 42488  42969       *pp = 0;
 42489  42970     }
        42971  +  if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
 42490  42972     sqlite3_mutex_leave(pShmNode->mutex);
 42491  42973     return rc;
 42492  42974   }
 42493  42975   
 42494  42976   #else
 42495  42977   # define winShmMap     0
 42496  42978   # define winShmLock    0
................................................................................
 43311  43793         h = osCreateFile2((LPCWSTR)zConverted,
 43312  43794                           dwDesiredAccess,
 43313  43795                           dwShareMode,
 43314  43796                           dwCreationDisposition,
 43315  43797                           &extendedParameters);
 43316  43798         if( h!=INVALID_HANDLE_VALUE ) break;
 43317  43799         if( isReadWrite ){
 43318         -        int isRO = 0;
 43319         -        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        43800  +        int rc2, isRO = 0;
        43801  +        sqlite3BeginBenignMalloc();
        43802  +        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        43803  +        sqlite3EndBenignMalloc();
 43320  43804           if( rc2==SQLITE_OK && isRO ) break;
 43321  43805         }
 43322  43806       }while( winRetryIoerr(&cnt, &lastErrno) );
 43323  43807   #else
 43324  43808       do{
 43325  43809         h = osCreateFileW((LPCWSTR)zConverted,
 43326  43810                           dwDesiredAccess,
 43327  43811                           dwShareMode, NULL,
 43328  43812                           dwCreationDisposition,
 43329  43813                           dwFlagsAndAttributes,
 43330  43814                           NULL);
 43331  43815         if( h!=INVALID_HANDLE_VALUE ) break;
 43332  43816         if( isReadWrite ){
 43333         -        int isRO = 0;
 43334         -        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        43817  +        int rc2, isRO = 0;
        43818  +        sqlite3BeginBenignMalloc();
        43819  +        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        43820  +        sqlite3EndBenignMalloc();
 43335  43821           if( rc2==SQLITE_OK && isRO ) break;
 43336  43822         }
 43337  43823       }while( winRetryIoerr(&cnt, &lastErrno) );
 43338  43824   #endif
 43339  43825     }
 43340  43826   #ifdef SQLITE_WIN32_HAS_ANSI
 43341  43827     else{
................................................................................
 43344  43830                           dwDesiredAccess,
 43345  43831                           dwShareMode, NULL,
 43346  43832                           dwCreationDisposition,
 43347  43833                           dwFlagsAndAttributes,
 43348  43834                           NULL);
 43349  43835         if( h!=INVALID_HANDLE_VALUE ) break;
 43350  43836         if( isReadWrite ){
 43351         -        int isRO = 0;
 43352         -        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        43837  +        int rc2, isRO = 0;
        43838  +        sqlite3BeginBenignMalloc();
        43839  +        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
        43840  +        sqlite3EndBenignMalloc();
 43353  43841           if( rc2==SQLITE_OK && isRO ) break;
 43354  43842         }
 43355  43843       }while( winRetryIoerr(&cnt, &lastErrno) );
 43356  43844     }
 43357  43845   #endif
 43358  43846     winLogIoerr(cnt, __LINE__);
 43359  43847   
................................................................................
 45252  45740   
 45253  45741   /*
 45254  45742   ** Make sure the page is marked as clean. If it isn't clean already,
 45255  45743   ** make it so.
 45256  45744   */
 45257  45745   SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
 45258  45746     assert( sqlite3PcachePageSanity(p) );
 45259         -  if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
 45260         -    assert( (p->flags & PGHDR_CLEAN)==0 );
 45261         -    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
 45262         -    p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
 45263         -    p->flags |= PGHDR_CLEAN;
 45264         -    pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
 45265         -    assert( sqlite3PcachePageSanity(p) );
 45266         -    if( p->nRef==0 ){
 45267         -      pcacheUnpin(p);
 45268         -    }
        45747  +  assert( (p->flags & PGHDR_DIRTY)!=0 );
        45748  +  assert( (p->flags & PGHDR_CLEAN)==0 );
        45749  +  pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
        45750  +  p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
        45751  +  p->flags |= PGHDR_CLEAN;
        45752  +  pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
        45753  +  assert( sqlite3PcachePageSanity(p) );
        45754  +  if( p->nRef==0 ){
        45755  +    pcacheUnpin(p);
 45269  45756     }
 45270  45757   }
 45271  45758   
 45272  45759   /*
 45273  45760   ** Make every page in the cache clean.
 45274  45761   */
 45275  45762   SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
................................................................................
 53054  53541       );
 53055  53542       if( rc==SQLITE_OK && pData ){
 53056  53543         if( pPager->eState>PAGER_READER || pPager->tempFile ){
 53057  53544           pPg = sqlite3PagerLookup(pPager, pgno);
 53058  53545         }
 53059  53546         if( pPg==0 ){
 53060  53547           rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
 53061         -     }else{
        53548  +      }else{
 53062  53549           sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
 53063  53550         }
 53064  53551         if( pPg ){
 53065  53552           assert( rc==SQLITE_OK );
 53066  53553           *ppPage = pPg;
 53067  53554           return SQLITE_OK;
 53068  53555         }
................................................................................
 55241  55728   ** WAL-INDEX FORMAT
 55242  55729   **
 55243  55730   ** Conceptually, the wal-index is shared memory, though VFS implementations
 55244  55731   ** might choose to implement the wal-index using a mmapped file.  Because
 55245  55732   ** the wal-index is shared memory, SQLite does not support journal_mode=WAL 
 55246  55733   ** on a network filesystem.  All users of the database must be able to
 55247  55734   ** share memory.
        55735  +**
        55736  +** In the default unix and windows implementation, the wal-index is a mmapped
        55737  +** file whose name is the database name with a "-shm" suffix added.  For that
        55738  +** reason, the wal-index is sometimes called the "shm" file.
 55248  55739   **
 55249  55740   ** The wal-index is transient.  After a crash, the wal-index can (and should
 55250  55741   ** be) reconstructed from the original WAL file.  In fact, the VFS is required
 55251  55742   ** to either truncate or zero the header of the wal-index when the last
 55252  55743   ** connection to it closes.  Because the wal-index is transient, it can
 55253  55744   ** use an architecture-specific format; it does not have to be cross-platform.
 55254  55745   ** Hence, unlike the database and WAL file formats which store all values
................................................................................
 55381  55872   ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
 55382  55873   ** returns SQLITE_CANTOPEN.
 55383  55874   */
 55384  55875   #define WAL_MAX_VERSION      3007000
 55385  55876   #define WALINDEX_MAX_VERSION 3007000
 55386  55877   
 55387  55878   /*
 55388         -** Indices of various locking bytes.   WAL_NREADER is the number
        55879  +** Index numbers for various locking bytes.   WAL_NREADER is the number
 55389  55880   ** of available reader locks and should be at least 3.  The default
 55390  55881   ** is SQLITE_SHM_NLOCK==8 and  WAL_NREADER==5.
        55882  +**
        55883  +** Technically, the various VFSes are free to implement these locks however
        55884  +** they see fit.  However, compatibility is encouraged so that VFSes can
        55885  +** interoperate.  The standard implemention used on both unix and windows
        55886  +** is for the index number to indicate a byte offset into the
        55887  +** WalCkptInfo.aLock[] array in the wal-index header.  In other words, all
        55888  +** locks are on the shm file.  The WALINDEX_LOCK_OFFSET constant (which
        55889  +** should be 120) is the location in the shm file for the first locking
        55890  +** byte.
 55391  55891   */
 55392  55892   #define WAL_WRITE_LOCK         0
 55393  55893   #define WAL_ALL_BUT_WRITE      1
 55394  55894   #define WAL_CKPT_LOCK          1
 55395  55895   #define WAL_RECOVER_LOCK       2
 55396  55896   #define WAL_READ_LOCK(I)       (3+(I))
 55397  55897   #define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
................................................................................
 55507  56007   #define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
 55508  56008   #define WALINDEX_HDR_SIZE    (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
 55509  56009   
 55510  56010   /* Size of header before each frame in wal */
 55511  56011   #define WAL_FRAME_HDRSIZE 24
 55512  56012   
 55513  56013   /* Size of write ahead log header, including checksum. */
 55514         -/* #define WAL_HDRSIZE 24 */
 55515  56014   #define WAL_HDRSIZE 32
 55516  56015   
 55517  56016   /* WAL magic value. Either this value, or the same value with the least
 55518  56017   ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
 55519  56018   ** big-endian format in the first 4 bytes of a WAL file.
 55520  56019   **
 55521  56020   ** If the LSB is set, then the checksums for each frame within the WAL
................................................................................
 55553  56052     u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
 55554  56053     u8 writeLock;              /* True if in a write transaction */
 55555  56054     u8 ckptLock;               /* True if holding a checkpoint lock */
 55556  56055     u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
 55557  56056     u8 truncateOnCommit;       /* True to truncate WAL file on commit */
 55558  56057     u8 syncHeader;             /* Fsync the WAL header if true */
 55559  56058     u8 padToSectorBoundary;    /* Pad transactions out to the next sector */
        56059  +  u8 bShmUnreliable;         /* SHM content is read-only and unreliable */
 55560  56060     WalIndexHdr hdr;           /* Wal-index header for current transaction */
 55561  56061     u32 minFrame;              /* Ignore wal frames before this one */
 55562  56062     u32 iReCksum;              /* On commit, recalculate checksums from here */
 55563  56063     const char *zWalName;      /* Name of WAL file */
 55564  56064     u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
 55565  56065   #ifdef SQLITE_DEBUG
 55566  56066     u8 lockError;              /* True if a locking error has occurred */
................................................................................
 55641  56141       sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
 55642  56142   )
 55643  56143   
 55644  56144   /*
 55645  56145   ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
 55646  56146   ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
 55647  56147   ** numbered from zero.
        56148  +**
        56149  +** If the wal-index is currently smaller the iPage pages then the size
        56150  +** of the wal-index might be increased, but only if it is safe to do
        56151  +** so.  It is safe to enlarge the wal-index if pWal->writeLock is true
        56152  +** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
 55648  56153   **
 55649  56154   ** If this call is successful, *ppPage is set to point to the wal-index
 55650  56155   ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
 55651  56156   ** then an SQLite error code is returned and *ppPage is set to 0.
 55652  56157   */
 55653  56158   static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
 55654  56159     int rc = SQLITE_OK;
................................................................................
 55673  56178       if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 55674  56179         pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
 55675  56180         if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
 55676  56181       }else{
 55677  56182         rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
 55678  56183             pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
 55679  56184         );
 55680         -      if( rc==SQLITE_READONLY ){
        56185  +      assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
        56186  +      testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
        56187  +      if( (rc&0xff)==SQLITE_READONLY ){
 55681  56188           pWal->readOnly |= WAL_SHM_RDONLY;
 55682         -        rc = SQLITE_OK;
        56189  +        if( rc==SQLITE_READONLY ){
        56190  +          rc = SQLITE_OK;
        56191  +        }
 55683  56192         }
 55684  56193       }
 55685  56194     }
 55686  56195   
 55687  56196     *ppPage = pWal->apWiData[iPage];
 55688  56197     assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
 55689  56198     return rc;
................................................................................
 56197  56706   ** the necessary locks, this routine returns SQLITE_BUSY.
 56198  56707   */
 56199  56708   static int walIndexRecover(Wal *pWal){
 56200  56709     int rc;                         /* Return Code */
 56201  56710     i64 nSize;                      /* Size of log file */
 56202  56711     u32 aFrameCksum[2] = {0, 0};
 56203  56712     int iLock;                      /* Lock offset to lock for checkpoint */
 56204         -  int nLock;                      /* Number of locks to hold */
 56205  56713   
 56206  56714     /* Obtain an exclusive lock on all byte in the locking range not already
 56207  56715     ** locked by the caller. The caller is guaranteed to have locked the
 56208  56716     ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
 56209  56717     ** If successful, the same bytes that are locked here are unlocked before
 56210  56718     ** this function returns.
 56211  56719     */
 56212  56720     assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
 56213  56721     assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
 56214  56722     assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
 56215  56723     assert( pWal->writeLock );
 56216  56724     iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
 56217         -  nLock = SQLITE_SHM_NLOCK - iLock;
 56218         -  rc = walLockExclusive(pWal, iLock, nLock);
        56725  +  rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
        56726  +  if( rc==SQLITE_OK ){
        56727  +    rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
        56728  +    if( rc!=SQLITE_OK ){
        56729  +      walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
        56730  +    }
        56731  +  }
 56219  56732     if( rc ){
 56220  56733       return rc;
 56221  56734     }
        56735  +
 56222  56736     WALTRACE(("WAL%p: recovery begin...\n", pWal));
 56223  56737   
 56224  56738     memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
 56225  56739   
 56226  56740     rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
 56227  56741     if( rc!=SQLITE_OK ){
 56228  56742       goto recovery_error;
................................................................................
 56352  56866             pWal->hdr.mxFrame, pWal->zWalName
 56353  56867         );
 56354  56868       }
 56355  56869     }
 56356  56870   
 56357  56871   recovery_error:
 56358  56872     WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
 56359         -  walUnlockExclusive(pWal, iLock, nLock);
        56873  +  walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
        56874  +  walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 56360  56875     return rc;
 56361  56876   }
 56362  56877   
 56363  56878   /*
 56364  56879   ** Close an open wal-index.
 56365  56880   */
 56366  56881   static void walIndexClose(Wal *pWal, int isDelete){
 56367         -  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
        56882  +  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
 56368  56883       int i;
 56369  56884       for(i=0; i<pWal->nWiData; i++){
 56370  56885         sqlite3_free((void *)pWal->apWiData[i]);
 56371  56886         pWal->apWiData[i] = 0;
 56372  56887       }
 56373         -  }else{
        56888  +  }
        56889  +  if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
 56374  56890       sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
 56375  56891     }
 56376  56892   }
 56377  56893   
 56378  56894   /* 
 56379  56895   ** Open a connection to the WAL file zWalName. The database file must 
 56380  56896   ** already be opened on connection pDbFd. The buffer that zWalName points
................................................................................
 57159  57675       testcase( pWal->szPage>=65536 );
 57160  57676     }
 57161  57677   
 57162  57678     /* The header was successfully read. Return zero. */
 57163  57679     return 0;
 57164  57680   }
 57165  57681   
        57682  +/*
        57683  +** This is the value that walTryBeginRead returns when it needs to
        57684  +** be retried.
        57685  +*/
        57686  +#define WAL_RETRY  (-1)
        57687  +
 57166  57688   /*
 57167  57689   ** Read the wal-index header from the wal-index and into pWal->hdr.
 57168  57690   ** If the wal-header appears to be corrupt, try to reconstruct the
 57169  57691   ** wal-index from the WAL before returning.
 57170  57692   **
 57171  57693   ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
 57172  57694   ** changed by this operation.  If pWal->hdr is unchanged, set *pChanged
................................................................................
 57182  57704   
 57183  57705     /* Ensure that page 0 of the wal-index (the page that contains the 
 57184  57706     ** wal-index header) is mapped. Return early if an error occurs here.
 57185  57707     */
 57186  57708     assert( pChanged );
 57187  57709     rc = walIndexPage(pWal, 0, &page0);
 57188  57710     if( rc!=SQLITE_OK ){
 57189         -    return rc;
 57190         -  };
 57191         -  assert( page0 || pWal->writeLock==0 );
        57711  +    assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */
        57712  +    if( rc==SQLITE_READONLY_CANTINIT ){
        57713  +      /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
        57714  +      ** was openable but is not writable, and this thread is unable to
        57715  +      ** confirm that another write-capable connection has the shared-memory
        57716  +      ** open, and hence the content of the shared-memory is unreliable,
        57717  +      ** since the shared-memory might be inconsistent with the WAL file
        57718  +      ** and there is no writer on hand to fix it. */
        57719  +      assert( page0==0 );
        57720  +      assert( pWal->writeLock==0 );
        57721  +      assert( pWal->readOnly & WAL_SHM_RDONLY );
        57722  +      pWal->bShmUnreliable = 1;
        57723  +      pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
        57724  +      *pChanged = 1;
        57725  +    }else{
        57726  +      return rc; /* Any other non-OK return is just an error */
        57727  +    }
        57728  +  }else{
        57729  +    /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
        57730  +    ** is zero, which prevents the SHM from growing */
        57731  +    testcase( page0!=0 );
        57732  +  }
        57733  +  assert( page0!=0 || pWal->writeLock==0 );
 57192  57734   
 57193  57735     /* If the first page of the wal-index has been mapped, try to read the
 57194  57736     ** wal-index header immediately, without holding any lock. This usually
 57195  57737     ** works, but may fail if the wal-index header is corrupt or currently 
 57196  57738     ** being modified by another thread or process.
 57197  57739     */
 57198  57740     badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
 57199  57741   
 57200  57742     /* If the first attempt failed, it might have been due to a race
 57201  57743     ** with a writer.  So get a WRITE lock and try again.
 57202  57744     */
 57203  57745     assert( badHdr==0 || pWal->writeLock==0 );
 57204  57746     if( badHdr ){
 57205         -    if( pWal->readOnly & WAL_SHM_RDONLY ){
        57747  +    if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
 57206  57748         if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
 57207  57749           walUnlockShared(pWal, WAL_WRITE_LOCK);
 57208  57750           rc = SQLITE_READONLY_RECOVERY;
 57209  57751         }
 57210  57752       }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
 57211  57753         pWal->writeLock = 1;
 57212  57754         if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
................................................................................
 57228  57770     /* If the header is read successfully, check the version number to make
 57229  57771     ** sure the wal-index was not constructed with some future format that
 57230  57772     ** this version of SQLite cannot understand.
 57231  57773     */
 57232  57774     if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
 57233  57775       rc = SQLITE_CANTOPEN_BKPT;
 57234  57776     }
 57235         -
 57236         -  return rc;
 57237         -}
 57238         -
 57239         -/*
 57240         -** This is the value that walTryBeginRead returns when it needs to
 57241         -** be retried.
 57242         -*/
 57243         -#define WAL_RETRY  (-1)
        57777  +  if( pWal->bShmUnreliable ){
        57778  +    if( rc!=SQLITE_OK ){
        57779  +      walIndexClose(pWal, 0);
        57780  +      pWal->bShmUnreliable = 0;
        57781  +      assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
        57782  +      /* walIndexRecover() might have returned SHORT_READ if a concurrent
        57783  +      ** writer truncated the WAL out from under it.  If that happens, it
        57784  +      ** indicates that a writer has fixed the SHM file for us, so retry */
        57785  +      if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY;
        57786  +    }
        57787  +    pWal->exclusiveMode = WAL_NORMAL_MODE;
        57788  +  }
        57789  +
        57790  +  return rc;
        57791  +}
        57792  +
        57793  +/*
        57794  +** Open a transaction in a connection where the shared-memory is read-only
        57795  +** and where we cannot verify that there is a separate write-capable connection
        57796  +** on hand to keep the shared-memory up-to-date with the WAL file.
        57797  +**
        57798  +** This can happen, for example, when the shared-memory is implemented by
        57799  +** memory-mapping a *-shm file, where a prior writer has shut down and
        57800  +** left the *-shm file on disk, and now the present connection is trying
        57801  +** to use that database but lacks write permission on the *-shm file.
        57802  +** Other scenarios are also possible, depending on the VFS implementation.
        57803  +**
        57804  +** Precondition:
        57805  +**
        57806  +**    The *-wal file has been read and an appropriate wal-index has been
        57807  +**    constructed in pWal->apWiData[] using heap memory instead of shared
        57808  +**    memory. 
        57809  +**
        57810  +** If this function returns SQLITE_OK, then the read transaction has
        57811  +** been successfully opened. In this case output variable (*pChanged) 
        57812  +** is set to true before returning if the caller should discard the
        57813  +** contents of the page cache before proceeding. Or, if it returns 
        57814  +** WAL_RETRY, then the heap memory wal-index has been discarded and 
        57815  +** the caller should retry opening the read transaction from the 
        57816  +** beginning (including attempting to map the *-shm file). 
        57817  +**
        57818  +** If an error occurs, an SQLite error code is returned.
        57819  +*/
        57820  +static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
        57821  +  i64 szWal;                      /* Size of wal file on disk in bytes */
        57822  +  i64 iOffset;                    /* Current offset when reading wal file */
        57823  +  u8 aBuf[WAL_HDRSIZE];           /* Buffer to load WAL header into */
        57824  +  u8 *aFrame = 0;                 /* Malloc'd buffer to load entire frame */
        57825  +  int szFrame;                    /* Number of bytes in buffer aFrame[] */
        57826  +  u8 *aData;                      /* Pointer to data part of aFrame buffer */
        57827  +  volatile void *pDummy;          /* Dummy argument for xShmMap */
        57828  +  int rc;                         /* Return code */
        57829  +  u32 aSaveCksum[2];              /* Saved copy of pWal->hdr.aFrameCksum */
        57830  +
        57831  +  assert( pWal->bShmUnreliable );
        57832  +  assert( pWal->readOnly & WAL_SHM_RDONLY );
        57833  +  assert( pWal->nWiData>0 && pWal->apWiData[0] );
        57834  +
        57835  +  /* Take WAL_READ_LOCK(0). This has the effect of preventing any
        57836  +  ** writers from running a checkpoint, but does not stop them
        57837  +  ** from running recovery.  */
        57838  +  rc = walLockShared(pWal, WAL_READ_LOCK(0));
        57839  +  if( rc!=SQLITE_OK ){
        57840  +    if( rc==SQLITE_BUSY ) rc = WAL_RETRY;
        57841  +    goto begin_unreliable_shm_out;
        57842  +  }
        57843  +  pWal->readLock = 0;
        57844  +
        57845  +  /* Check to see if a separate writer has attached to the shared-memory area,
        57846  +  ** thus making the shared-memory "reliable" again.  Do this by invoking
        57847  +  ** the xShmMap() routine of the VFS and looking to see if the return
        57848  +  ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
        57849  +  **
        57850  +  ** If the shared-memory is now "reliable" return WAL_RETRY, which will
        57851  +  ** cause the heap-memory WAL-index to be discarded and the actual
        57852  +  ** shared memory to be used in its place.
        57853  +  **
        57854  +  ** This step is important because, even though this connection is holding
        57855  +  ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
        57856  +  ** have already checkpointed the WAL file and, while the current
        57857  +  ** is active, wrap the WAL and start overwriting frames that this
        57858  +  ** process wants to use.
        57859  +  **
        57860  +  ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
        57861  +  ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
        57862  +  ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
        57863  +  ** even if some external agent does a "chmod" to make the shared-memory
        57864  +  ** writable by us, until sqlite3OsShmUnmap() has been called.
        57865  +  ** This is a requirement on the VFS implementation.
        57866  +   */
        57867  +  rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
        57868  +  assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
        57869  +  if( rc!=SQLITE_READONLY_CANTINIT ){
        57870  +    rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc);
        57871  +    goto begin_unreliable_shm_out;
        57872  +  }
        57873  +
        57874  +  /* We reach this point only if the real shared-memory is still unreliable.
        57875  +  ** Assume the in-memory WAL-index substitute is correct and load it
        57876  +  ** into pWal->hdr.
        57877  +  */
        57878  +  memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
        57879  +
        57880  +  /* Make sure some writer hasn't come in and changed the WAL file out
        57881  +  ** from under us, then disconnected, while we were not looking.
        57882  +  */
        57883  +  rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
        57884  +  if( rc!=SQLITE_OK ){
        57885  +    goto begin_unreliable_shm_out;
        57886  +  }
        57887  +  if( szWal<WAL_HDRSIZE ){
        57888  +    /* If the wal file is too small to contain a wal-header and the
        57889  +    ** wal-index header has mxFrame==0, then it must be safe to proceed
        57890  +    ** reading the database file only. However, the page cache cannot
        57891  +    ** be trusted, as a read/write connection may have connected, written
        57892  +    ** the db, run a checkpoint, truncated the wal file and disconnected
        57893  +    ** since this client's last read transaction.  */
        57894  +    *pChanged = 1;
        57895  +    rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
        57896  +    goto begin_unreliable_shm_out;
        57897  +  }
        57898  +
        57899  +  /* Check the salt keys at the start of the wal file still match. */
        57900  +  rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
        57901  +  if( rc!=SQLITE_OK ){
        57902  +    goto begin_unreliable_shm_out;
        57903  +  }
        57904  +  if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
        57905  +    /* Some writer has wrapped the WAL file while we were not looking.
        57906  +    ** Return WAL_RETRY which will cause the in-memory WAL-index to be
        57907  +    ** rebuilt. */
        57908  +    rc = WAL_RETRY;
        57909  +    goto begin_unreliable_shm_out;
        57910  +  }
        57911  +
        57912  +  /* Allocate a buffer to read frames into */
        57913  +  szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE;
        57914  +  aFrame = (u8 *)sqlite3_malloc64(szFrame);
        57915  +  if( aFrame==0 ){
        57916  +    rc = SQLITE_NOMEM_BKPT;
        57917  +    goto begin_unreliable_shm_out;
        57918  +  }
        57919  +  aData = &aFrame[WAL_FRAME_HDRSIZE];
        57920  +
        57921  +  /* Check to see if a complete transaction has been appended to the
        57922  +  ** wal file since the heap-memory wal-index was created. If so, the
        57923  +  ** heap-memory wal-index is discarded and WAL_RETRY returned to
        57924  +  ** the caller.  */
        57925  +  aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
        57926  +  aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
        57927  +  for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage); 
        57928  +      iOffset+szFrame<=szWal; 
        57929  +      iOffset+=szFrame
        57930  +  ){
        57931  +    u32 pgno;                   /* Database page number for frame */
        57932  +    u32 nTruncate;              /* dbsize field from frame header */
        57933  +
        57934  +    /* Read and decode the next log frame. */
        57935  +    rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
        57936  +    if( rc!=SQLITE_OK ) break;
        57937  +    if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
        57938  +
        57939  +    /* If nTruncate is non-zero, then a complete transaction has been
        57940  +    ** appended to this wal file. Set rc to WAL_RETRY and break out of
        57941  +    ** the loop.  */
        57942  +    if( nTruncate ){
        57943  +      rc = WAL_RETRY;
        57944  +      break;
        57945  +    }
        57946  +  }
        57947  +  pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
        57948  +  pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
        57949  +
        57950  + begin_unreliable_shm_out:
        57951  +  sqlite3_free(aFrame);
        57952  +  if( rc!=SQLITE_OK ){
        57953  +    int i;
        57954  +    for(i=0; i<pWal->nWiData; i++){
        57955  +      sqlite3_free((void*)pWal->apWiData[i]);
        57956  +      pWal->apWiData[i] = 0;
        57957  +    }
        57958  +    pWal->bShmUnreliable = 0;
        57959  +    sqlite3WalEndReadTransaction(pWal);
        57960  +    *pChanged = 1;
        57961  +  }
        57962  +  return rc;
        57963  +}
 57244  57964   
 57245  57965   /*
 57246  57966   ** Attempt to start a read transaction.  This might fail due to a race or
 57247  57967   ** other transient condition.  When that happens, it returns WAL_RETRY to
 57248  57968   ** indicate to the caller that it is safe to retry immediately.
 57249  57969   **
 57250  57970   ** On success return SQLITE_OK.  On a permanent failure (such an
................................................................................
 57252  57972   ** recovery) return a positive error code.
 57253  57973   **
 57254  57974   ** The useWal parameter is true to force the use of the WAL and disable
 57255  57975   ** the case where the WAL is bypassed because it has been completely
 57256  57976   ** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr() 
 57257  57977   ** to make a copy of the wal-index header into pWal->hdr.  If the 
 57258  57978   ** wal-index header has changed, *pChanged is set to 1 (as an indication 
 57259         -** to the caller that the local paget cache is obsolete and needs to be 
        57979  +** to the caller that the local page cache is obsolete and needs to be 
 57260  57980   ** flushed.)  When useWal==1, the wal-index header is assumed to already
 57261  57981   ** be loaded and the pChanged parameter is unused.
 57262  57982   **
 57263  57983   ** The caller must set the cnt parameter to the number of prior calls to
 57264  57984   ** this routine during the current read attempt that returned WAL_RETRY.
 57265  57985   ** This routine will start taking more aggressive measures to clear the
 57266  57986   ** race conditions after multiple WAL_RETRY returns, and after an excessive
................................................................................
 57298  58018     int mxI;                        /* Index of largest aReadMark[] value */
 57299  58019     int i;                          /* Loop counter */
 57300  58020     int rc = SQLITE_OK;             /* Return code  */
 57301  58021     u32 mxFrame;                    /* Wal frame to lock to */
 57302  58022   
 57303  58023     assert( pWal->readLock<0 );     /* Not currently locked */
 57304  58024   
        58025  +  /* useWal may only be set for read/write connections */
        58026  +  assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
        58027  +
 57305  58028     /* Take steps to avoid spinning forever if there is a protocol error.
 57306  58029     **
 57307  58030     ** Circumstances that cause a RETRY should only last for the briefest
 57308  58031     ** instances of time.  No I/O or other system calls are done while the
 57309  58032     ** locks are held, so the locks should not be held for very long. But 
 57310  58033     ** if we are unlucky, another process that is holding a lock might get
 57311  58034     ** paged out or take a page-fault that is time-consuming to resolve, 
................................................................................
 57326  58049         return SQLITE_PROTOCOL;
 57327  58050       }
 57328  58051       if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
 57329  58052       sqlite3OsSleep(pWal->pVfs, nDelay);
 57330  58053     }
 57331  58054   
 57332  58055     if( !useWal ){
 57333         -    rc = walIndexReadHdr(pWal, pChanged);
        58056  +    assert( rc==SQLITE_OK );
        58057  +    if( pWal->bShmUnreliable==0 ){
        58058  +      rc = walIndexReadHdr(pWal, pChanged);
        58059  +    }
 57334  58060       if( rc==SQLITE_BUSY ){
 57335  58061         /* If there is not a recovery running in another thread or process
 57336  58062         ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
 57337  58063         ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
 57338  58064         ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
 57339  58065         ** would be technically correct.  But the race is benign since with
 57340  58066         ** WAL_RETRY this routine will be called again and will probably be
................................................................................
 57355  58081         }else if( rc==SQLITE_BUSY ){
 57356  58082           rc = SQLITE_BUSY_RECOVERY;
 57357  58083         }
 57358  58084       }
 57359  58085       if( rc!=SQLITE_OK ){
 57360  58086         return rc;
 57361  58087       }
        58088  +    else if( pWal->bShmUnreliable ){
        58089  +      return walBeginShmUnreliable(pWal, pChanged);
        58090  +    }
 57362  58091     }
 57363  58092   
        58093  +  assert( pWal->nWiData>0 );
        58094  +  assert( pWal->apWiData[0]!=0 );
 57364  58095     pInfo = walCkptInfo(pWal);
 57365         -  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame 
        58096  +  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame
 57366  58097   #ifdef SQLITE_ENABLE_SNAPSHOT
 57367         -   && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0
 57368         -     || 0==memcmp(&pWal->hdr, pWal->pSnapshot, sizeof(WalIndexHdr)))
        58098  +   && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
 57369  58099   #endif
 57370  58100     ){
 57371  58101       /* The WAL has been completely backfilled (or it is empty).
 57372  58102       ** and can be safely ignored.
 57373  58103       */
 57374  58104       rc = walLockShared(pWal, WAL_READ_LOCK(0));
 57375  58105       walShmBarrier(pWal);
................................................................................
 57432  58162         }else if( rc!=SQLITE_BUSY ){
 57433  58163           return rc;
 57434  58164         }
 57435  58165       }
 57436  58166     }
 57437  58167     if( mxI==0 ){
 57438  58168       assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
 57439         -    return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
        58169  +    return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
 57440  58170     }
 57441  58171   
 57442  58172     rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
 57443  58173     if( rc ){
 57444  58174       return rc==SQLITE_BUSY ? WAL_RETRY : rc;
 57445  58175     }
 57446  58176     /* Now that the read-lock has been obtained, check that neither the
................................................................................
 57704  58434   
 57705  58435     /* If the "last page" field of the wal-index header snapshot is 0, then
 57706  58436     ** no data will be read from the wal under any circumstances. Return early
 57707  58437     ** in this case as an optimization.  Likewise, if pWal->readLock==0, 
 57708  58438     ** then the WAL is ignored by the reader so return early, as if the 
 57709  58439     ** WAL were empty.
 57710  58440     */
 57711         -  if( iLast==0 || pWal->readLock==0 ){
        58441  +  if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
 57712  58442       *piRead = 0;
 57713  58443       return SQLITE_OK;
 57714  58444     }
 57715  58445   
 57716  58446     /* Search the hash table or tables for an entry matching page number
 57717  58447     ** pgno. Each iteration of the following for() loop searches one
 57718  58448     ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
................................................................................
 57767  58497   #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 57768  58498     /* If expensive assert() statements are available, do a linear search
 57769  58499     ** of the wal-index file content. Make sure the results agree with the
 57770  58500     ** result obtained using the hash indexes above.  */
 57771  58501     {
 57772  58502       u32 iRead2 = 0;
 57773  58503       u32 iTest;
 57774         -    assert( pWal->minFrame>0 );
 57775         -    for(iTest=iLast; iTest>=pWal->minFrame; iTest--){
        58504  +    assert( pWal->bShmUnreliable || pWal->minFrame>0 );
        58505  +    for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
 57776  58506         if( walFramePgno(pWal, iTest)==pgno ){
 57777  58507           iRead2 = iTest;
 57778  58508           break;
 57779  58509         }
 57780  58510       }
 57781  58511       assert( iRead==iRead2 );
 57782  58512     }
................................................................................
 58544  59274     ** locks are taken in this case). Nor should the pager attempt to
 58545  59275     ** upgrade to exclusive-mode following such an error.
 58546  59276     */
 58547  59277     assert( pWal->readLock>=0 || pWal->lockError );
 58548  59278     assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
 58549  59279   
 58550  59280     if( op==0 ){
 58551         -    if( pWal->exclusiveMode ){
 58552         -      pWal->exclusiveMode = 0;
        59281  +    if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
        59282  +      pWal->exclusiveMode = WAL_NORMAL_MODE;
 58553  59283         if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
 58554         -        pWal->exclusiveMode = 1;
        59284  +        pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
 58555  59285         }
 58556         -      rc = pWal->exclusiveMode==0;
        59286  +      rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
 58557  59287       }else{
 58558  59288         /* Already in locking_mode=NORMAL */
 58559  59289         rc = 0;
 58560  59290       }
 58561  59291     }else if( op>0 ){
 58562         -    assert( pWal->exclusiveMode==0 );
        59292  +    assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
 58563  59293       assert( pWal->readLock>=0 );
 58564  59294       walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
 58565         -    pWal->exclusiveMode = 1;
        59295  +    pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
 58566  59296       rc = 1;
 58567  59297     }else{
 58568         -    rc = pWal->exclusiveMode==0;
        59298  +    rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
 58569  59299     }
 58570  59300     return rc;
 58571  59301   }
 58572  59302   
 58573  59303   /* 
 58574  59304   ** Return true if the argument is non-NULL and the WAL module is using
 58575  59305   ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
................................................................................
 59781  60511     #define setSharedCacheTableLock(a,b,c) SQLITE_OK
 59782  60512     #define clearAllSharedCacheTableLocks(a)
 59783  60513     #define downgradeAllSharedCacheTableLocks(a)
 59784  60514     #define hasSharedCacheTableLock(a,b,c,d) 1
 59785  60515     #define hasReadConflicts(a, b) 0
 59786  60516   #endif
 59787  60517   
        60518  +/*
        60519  +** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
        60520  +** (MemPage*) as an argument. The (MemPage*) must not be NULL.
        60521  +**
        60522  +** If SQLITE_DEBUG is not defined, then this macro is equivalent to
        60523  +** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
        60524  +** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
        60525  +** with the page number and filename associated with the (MemPage*).
        60526  +*/
        60527  +#ifdef SQLITE_DEBUG
        60528  +int corruptPageError(int lineno, MemPage *p){
        60529  +  char *zMsg;
        60530  +  sqlite3BeginBenignMalloc();
        60531  +  zMsg = sqlite3_mprintf("database corruption page %d of %s",
        60532  +      (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
        60533  +  );
        60534  +  sqlite3EndBenignMalloc();
        60535  +  if( zMsg ){
        60536  +    sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
        60537  +  }
        60538  +  sqlite3_free(zMsg);
        60539  +  return SQLITE_CORRUPT_BKPT;
        60540  +}
        60541  +# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
        60542  +#else
        60543  +# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
        60544  +#endif
        60545  +
 59788  60546   #ifndef SQLITE_OMIT_SHARED_CACHE
 59789  60547   
 59790  60548   #ifdef SQLITE_DEBUG
 59791  60549   /*
 59792  60550   **** This function is only used as part of an assert() statement. ***
 59793  60551   **
 59794  60552   ** Check to see if pBtree holds the required locks to read or write to the 
................................................................................
 61069  61827         if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
 61070  61828           u8 *pEnd = &data[cellOffset + nCell*2];
 61071  61829           u8 *pAddr;
 61072  61830           int sz2 = 0;
 61073  61831           int sz = get2byte(&data[iFree+2]);
 61074  61832           int top = get2byte(&data[hdr+5]);
 61075  61833           if( top>=iFree ){
 61076         -          return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61834  +          return SQLITE_CORRUPT_PAGE(pPage);
 61077  61835           }
 61078  61836           if( iFree2 ){
 61079  61837             assert( iFree+sz<=iFree2 ); /* Verified by pageFindSlot() */
 61080  61838             sz2 = get2byte(&data[iFree2+2]);
 61081  61839             assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
 61082  61840             memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
 61083  61841             sz += sz2;
................................................................................
 61103  61861       pc = get2byte(pAddr);
 61104  61862       testcase( pc==iCellFirst );
 61105  61863       testcase( pc==iCellLast );
 61106  61864       /* These conditions have already been verified in btreeInitPage()
 61107  61865       ** if PRAGMA cell_size_check=ON.
 61108  61866       */
 61109  61867       if( pc<iCellFirst || pc>iCellLast ){
 61110         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61868  +      return SQLITE_CORRUPT_PAGE(pPage);
 61111  61869       }
 61112  61870       assert( pc>=iCellFirst && pc<=iCellLast );
 61113  61871       size = pPage->xCellSize(pPage, &src[pc]);
 61114  61872       cbrk -= size;
 61115  61873       if( cbrk<iCellFirst || pc+size>usableSize ){
 61116         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61874  +      return SQLITE_CORRUPT_PAGE(pPage);
 61117  61875       }
 61118  61876       assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
 61119  61877       testcase( cbrk+size==usableSize );
 61120  61878       testcase( pc+size==usableSize );
 61121  61879       put2byte(pAddr, cbrk);
 61122  61880       if( temp==0 ){
 61123  61881         int x;
................................................................................
 61129  61887       }
 61130  61888       memcpy(&data[cbrk], &src[pc], size);
 61131  61889     }
 61132  61890     data[hdr+7] = 0;
 61133  61891   
 61134  61892    defragment_out:
 61135  61893     if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
 61136         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61894  +    return SQLITE_CORRUPT_PAGE(pPage);
 61137  61895     }
 61138  61896     assert( cbrk>=iCellFirst );
 61139  61897     put2byte(&data[hdr+5], cbrk);
 61140  61898     data[hdr+1] = 0;
 61141  61899     data[hdr+2] = 0;
 61142  61900     memset(&data[iCellFirst], 0, cbrk-iCellFirst);
 61143  61901     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
................................................................................
 61173  61931       ** freeblock form a big-endian integer which is the size of the freeblock
 61174  61932       ** in bytes, including the 4-byte header. */
 61175  61933       size = get2byte(&aData[pc+2]);
 61176  61934       if( (x = size - nByte)>=0 ){
 61177  61935         testcase( x==4 );
 61178  61936         testcase( x==3 );
 61179  61937         if( size+pc > usableSize ){
 61180         -        *pRc = SQLITE_CORRUPT_PGNO(pPg->pgno);
        61938  +        *pRc = SQLITE_CORRUPT_PAGE(pPg);
 61181  61939           return 0;
 61182  61940         }else if( x<4 ){
 61183  61941           /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
 61184  61942           ** number of bytes in fragments may not exceed 60. */
 61185  61943           if( aData[hdr+7]>57 ) return 0;
 61186  61944   
 61187  61945           /* Remove the slot from the free-list. Update the number of
................................................................................
 61196  61954         return &aData[pc + x];
 61197  61955       }
 61198  61956       iAddr = pc;
 61199  61957       pc = get2byte(&aData[pc]);
 61200  61958       if( pc<iAddr+size ) break;
 61201  61959     }
 61202  61960     if( pc ){
 61203         -    *pRc = SQLITE_CORRUPT_PGNO(pPg->pgno);
        61961  +    *pRc = SQLITE_CORRUPT_PAGE(pPg);
 61204  61962     }
 61205  61963   
 61206  61964     return 0;
 61207  61965   }
 61208  61966   
 61209  61967   /*
 61210  61968   ** Allocate nByte bytes of space from within the B-Tree page passed
................................................................................
 61244  62002     ** integer, so a value of 0 is used in its place. */
 61245  62003     top = get2byte(&data[hdr+5]);
 61246  62004     assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
 61247  62005     if( gap>top ){
 61248  62006       if( top==0 && pPage->pBt->usableSize==65536 ){
 61249  62007         top = 65536;
 61250  62008       }else{
 61251         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62009  +      return SQLITE_CORRUPT_PAGE(pPage);
 61252  62010       }
 61253  62011     }
 61254  62012   
 61255  62013     /* If there is enough space between gap and top for one more cell pointer
 61256  62014     ** array entry offset, and if the freelist is not empty, then search the
 61257  62015     ** freelist looking for a free slot big enough to satisfy the request.
 61258  62016     */
................................................................................
 61334  62092     iPtr = hdr + 1;
 61335  62093     if( data[iPtr+1]==0 && data[iPtr]==0 ){
 61336  62094       iFreeBlk = 0;  /* Shortcut for the case when the freelist is empty */
 61337  62095     }else{
 61338  62096       while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
 61339  62097         if( iFreeBlk<iPtr+4 ){
 61340  62098           if( iFreeBlk==0 ) break;
 61341         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62099  +        return SQLITE_CORRUPT_PAGE(pPage);
 61342  62100         }
 61343  62101         iPtr = iFreeBlk;
 61344  62102       }
 61345  62103       if( iFreeBlk>pPage->pBt->usableSize-4 ){
 61346         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62104  +      return SQLITE_CORRUPT_PAGE(pPage);
 61347  62105       }
 61348  62106       assert( iFreeBlk>iPtr || iFreeBlk==0 );
 61349  62107     
 61350  62108       /* At this point:
 61351  62109       **    iFreeBlk:   First freeblock after iStart, or zero if none
 61352  62110       **    iPtr:       The address of a pointer to iFreeBlk
 61353  62111       **
 61354  62112       ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
 61355  62113       */
 61356  62114       if( iFreeBlk && iEnd+3>=iFreeBlk ){
 61357  62115         nFrag = iFreeBlk - iEnd;
 61358         -      if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62116  +      if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
 61359  62117         iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
 61360  62118         if( iEnd > pPage->pBt->usableSize ){
 61361         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62119  +        return SQLITE_CORRUPT_PAGE(pPage);
 61362  62120         }
 61363  62121         iSize = iEnd - iStart;
 61364  62122         iFreeBlk = get2byte(&data[iFreeBlk]);
 61365  62123       }
 61366  62124     
 61367  62125       /* If iPtr is another freeblock (that is, if iPtr is not the freelist
 61368  62126       ** pointer in the page header) then check to see if iStart should be
 61369  62127       ** coalesced onto the end of iPtr.
 61370  62128       */
 61371  62129       if( iPtr>hdr+1 ){
 61372  62130         int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
 61373  62131         if( iPtrEnd+3>=iStart ){
 61374         -        if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62132  +        if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
 61375  62133           nFrag += iStart - iPtrEnd;
 61376  62134           iSize = iEnd - iPtr;
 61377  62135           iStart = iPtr;
 61378  62136         }
 61379  62137       }
 61380         -    if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62138  +    if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
 61381  62139       data[hdr+7] -= nFrag;
 61382  62140     }
 61383  62141     x = get2byte(&data[hdr+5]);
 61384  62142     if( iStart<=x ){
 61385  62143       /* The new freeblock is at the beginning of the cell content area,
 61386  62144       ** so just extend the cell content area rather than create another
 61387  62145       ** freelist entry */
 61388         -    if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62146  +    if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
 61389  62147       put2byte(&data[hdr+1], iFreeBlk);
 61390  62148       put2byte(&data[hdr+5], iEnd);
 61391  62149     }else{
 61392  62150       /* Insert the new freeblock into the freelist */
 61393  62151       put2byte(&data[iPtr], iStart);
 61394  62152     }
 61395  62153     if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
................................................................................
 61454  62212       pPage->intKeyLeaf = 0;
 61455  62213       pPage->xParseCell = btreeParseCellPtrIndex;
 61456  62214       pPage->maxLocal = pBt->maxLocal;
 61457  62215       pPage->minLocal = pBt->minLocal;
 61458  62216     }else{
 61459  62217       /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
 61460  62218       ** an error. */
 61461         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62219  +    return SQLITE_CORRUPT_PAGE(pPage);
 61462  62220     }
 61463  62221     pPage->max1bytePayload = pBt->max1bytePayload;
 61464  62222     return SQLITE_OK;
 61465  62223   }
 61466  62224   
 61467  62225   /*
 61468  62226   ** Initialize the auxiliary information for a disk block.
................................................................................
 61495  62253   
 61496  62254     pBt = pPage->pBt;
 61497  62255     hdr = pPage->hdrOffset;
 61498  62256     data = pPage->aData;
 61499  62257     /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
 61500  62258     ** the b-tree page type. */
 61501  62259     if( decodeFlags(pPage, data[hdr]) ){
 61502         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62260  +    return SQLITE_CORRUPT_PAGE(pPage);
 61503  62261     }
 61504  62262     assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 61505  62263     pPage->maskPage = (u16)(pBt->pageSize - 1);
 61506  62264     pPage->nOverflow = 0;
 61507  62265     usableSize = pBt->usableSize;
 61508  62266     pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
 61509  62267     pPage->aDataEnd = &data[usableSize];
................................................................................
 61514  62272     ** interpreted as 65536. */
 61515  62273     top = get2byteNotZero(&data[hdr+5]);
 61516  62274     /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
 61517  62275     ** number of cells on the page. */
 61518  62276     pPage->nCell = get2byte(&data[hdr+3]);
 61519  62277     if( pPage->nCell>MX_CELL(pBt) ){
 61520  62278       /* To many cells for a single page.  The page must be corrupt */
 61521         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62279  +    return SQLITE_CORRUPT_PAGE(pPage);
 61522  62280     }
 61523  62281     testcase( pPage->nCell==MX_CELL(pBt) );
 61524  62282     /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
 61525  62283     ** possible for a root page of a table that contains no rows) then the
 61526  62284     ** offset to the cell content area will equal the page size minus the
 61527  62285     ** bytes of reserved space. */
 61528  62286     assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
................................................................................
 61542  62300   
 61543  62301       if( !pPage->leaf ) iCellLast--;
 61544  62302       for(i=0; i<pPage->nCell; i++){
 61545  62303         pc = get2byteAligned(&data[cellOffset+i*2]);
 61546  62304         testcase( pc==iCellFirst );
 61547  62305         testcase( pc==iCellLast );
 61548  62306         if( pc<iCellFirst || pc>iCellLast ){
 61549         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62307  +        return SQLITE_CORRUPT_PAGE(pPage);
 61550  62308         }
 61551  62309         sz = pPage->xCellSize(pPage, &data[pc]);
 61552  62310         testcase( pc+sz==usableSize );
 61553  62311         if( pc+sz>usableSize ){
 61554         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62312  +        return SQLITE_CORRUPT_PAGE(pPage);
 61555  62313         }
 61556  62314       }
 61557  62315       if( !pPage->leaf ) iCellLast++;
 61558  62316     }  
 61559  62317   
 61560  62318     /* Compute the total free space on the page
 61561  62319     ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
................................................................................
 61565  62323     nFree = data[hdr+7] + top;  /* Init nFree to non-freeblock free space */
 61566  62324     if( pc>0 ){
 61567  62325       u32 next, size;
 61568  62326       if( pc<iCellFirst ){
 61569  62327         /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
 61570  62328         ** always be at least one cell before the first freeblock.
 61571  62329         */
 61572         -      return SQLITE_CORRUPT_PGNO(pPage->pgno); 
        62330  +      return SQLITE_CORRUPT_PAGE(pPage); 
 61573  62331       }
 61574  62332       while( 1 ){
 61575  62333         if( pc>iCellLast ){
 61576  62334           /* Freeblock off the end of the page */
 61577         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62335  +        return SQLITE_CORRUPT_PAGE(pPage);
 61578  62336         }
 61579  62337         next = get2byte(&data[pc]);
 61580  62338         size = get2byte(&data[pc+2]);
 61581  62339         nFree = nFree + size;
 61582  62340         if( next<=pc+size+3 ) break;
 61583  62341         pc = next;
 61584  62342       }
 61585  62343       if( next>0 ){
 61586  62344         /* Freeblock not in ascending order */
 61587         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62345  +      return SQLITE_CORRUPT_PAGE(pPage);
 61588  62346       }
 61589  62347       if( pc+size>(unsigned int)usableSize ){
 61590  62348         /* Last freeblock extends past page end */
 61591         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62349  +      return SQLITE_CORRUPT_PAGE(pPage);
 61592  62350       }
 61593  62351     }
 61594  62352   
 61595  62353     /* At this point, nFree contains the sum of the offset to the start
 61596  62354     ** of the cell-content area plus the number of free bytes within
 61597  62355     ** the cell-content area. If this is greater than the usable-size
 61598  62356     ** of the page, then the page must be corrupted. This check also
 61599  62357     ** serves to verify that the offset to the start of the cell-content
 61600  62358     ** area, according to the page header, lies within the page.
 61601  62359     */
 61602  62360     if( nFree>usableSize ){
 61603         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62361  +    return SQLITE_CORRUPT_PAGE(pPage);
 61604  62362     }
 61605  62363     pPage->nFree = (u16)(nFree - iCellFirst);
 61606  62364     pPage->isInit = 1;
 61607  62365     return SQLITE_OK;
 61608  62366   }
 61609  62367   
 61610  62368   /*
................................................................................
 63127  63885   */
 63128  63886   static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
 63129  63887     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 63130  63888     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 63131  63889     if( eType==PTRMAP_OVERFLOW2 ){
 63132  63890       /* The pointer is always the first 4 bytes of the page in this case.  */
 63133  63891       if( get4byte(pPage->aData)!=iFrom ){
 63134         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        63892  +      return SQLITE_CORRUPT_PAGE(pPage);
 63135  63893       }
 63136  63894       put4byte(pPage->aData, iTo);
 63137  63895     }else{
 63138  63896       int i;
 63139  63897       int nCell;
 63140  63898       int rc;
 63141  63899   
................................................................................
 63146  63904       for(i=0; i<nCell; i++){
 63147  63905         u8 *pCell = findCell(pPage, i);
 63148  63906         if( eType==PTRMAP_OVERFLOW1 ){
 63149  63907           CellInfo info;
 63150  63908           pPage->xParseCell(pPage, pCell, &info);
 63151  63909           if( info.nLocal<info.nPayload ){
 63152  63910             if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
 63153         -            return SQLITE_CORRUPT_PGNO(pPage->pgno);
        63911  +            return SQLITE_CORRUPT_PAGE(pPage);
 63154  63912             }
 63155  63913             if( iFrom==get4byte(pCell+info.nSize-4) ){
 63156  63914               put4byte(pCell+info.nSize-4, iTo);
 63157  63915               break;
 63158  63916             }
 63159  63917           }
 63160  63918         }else{
................................................................................
 63164  63922           }
 63165  63923         }
 63166  63924       }
 63167  63925     
 63168  63926       if( i==nCell ){
 63169  63927         if( eType!=PTRMAP_BTREE || 
 63170  63928             get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
 63171         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        63929  +        return SQLITE_CORRUPT_PAGE(pPage);
 63172  63930         }
 63173  63931         put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
 63174  63932       }
 63175  63933     }
 63176  63934     return SQLITE_OK;
 63177  63935   }
 63178  63936   
................................................................................
 64076  64834     assert( cursorHoldsMutex(pCur) );
 64077  64835     assert( pCur->eState==CURSOR_VALID );
 64078  64836     assert( pCur->curIntKey );
 64079  64837     getCellInfo(pCur);
 64080  64838     return pCur->info.nKey;
 64081  64839   }
 64082  64840   
        64841  +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
        64842  +/*
        64843  +** Return the offset into the database file for the start of the
        64844  +** payload to which the cursor is pointing.
        64845  +*/
        64846  +SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor *pCur){
        64847  +  assert( cursorHoldsMutex(pCur) );
        64848  +  assert( pCur->eState==CURSOR_VALID );
        64849  +  getCellInfo(pCur);
        64850  +  return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
        64851  +         (i64)(pCur->info.pPayload - pCur->pPage->aData);
        64852  +}
        64853  +#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
        64854  +
 64083  64855   /*
 64084  64856   ** Return the number of bytes of payload for the entry that pCur is
 64085  64857   ** currently pointing to.  For table btrees, this will be the amount
 64086  64858   ** of data.  For index btrees, this will be the size of the key.
 64087  64859   **
 64088  64860   ** The caller must guarantee that the cursor is pointing to a non-NULL
 64089  64861   ** valid entry.  In other words, the calling procedure must guarantee
................................................................................
 64262  65034     assert( aPayload > pPage->aData );
 64263  65035     if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
 64264  65036       /* Trying to read or write past the end of the data is an error.  The
 64265  65037       ** conditional above is really:
 64266  65038       **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
 64267  65039       ** but is recast into its current form to avoid integer overflow problems
 64268  65040       */
 64269         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        65041  +    return SQLITE_CORRUPT_PAGE(pPage);
 64270  65042     }
 64271  65043   
 64272  65044     /* Check if data must be read/written to/from the btree page itself. */
 64273  65045     if( offset<pCur->info.nLocal ){
 64274  65046       int a = amt;
 64275  65047       if( a+offset>pCur->info.nLocal ){
 64276  65048         a = pCur->info.nLocal - offset;
................................................................................
 64410  65182         if( rc ) break;
 64411  65183         iIdx++;
 64412  65184       }
 64413  65185     }
 64414  65186   
 64415  65187     if( rc==SQLITE_OK && amt>0 ){
 64416  65188       /* Overflow chain ends prematurely */
 64417         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        65189  +    return SQLITE_CORRUPT_PAGE(pPage);
 64418  65190     }
 64419  65191     return rc;
 64420  65192   }
 64421  65193   
 64422  65194   /*
 64423  65195   ** Read part of the payload for the row at which that cursor pCur is currently
 64424  65196   ** pointing.  "amt" bytes will be transferred into pBuf[].  The transfer
................................................................................
 64688  65460     ** Earlier versions of SQLite assumed that this test could not fail
 64689  65461     ** if the root page was already loaded when this function was called (i.e.
 64690  65462     ** if pCur->iPage>=0). But this is not so if the database is corrupted 
 64691  65463     ** in such a way that page pRoot is linked into a second b-tree table 
 64692  65464     ** (or the freelist).  */
 64693  65465     assert( pRoot->intKey==1 || pRoot->intKey==0 );
 64694  65466     if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
 64695         -    return SQLITE_CORRUPT_PGNO(pCur->pPage->pgno);
        65467  +    return SQLITE_CORRUPT_PAGE(pCur->pPage);
 64696  65468     }
 64697  65469   
 64698  65470   skip_init:  
 64699  65471     pCur->ix = 0;
 64700  65472     pCur->info.nSize = 0;
 64701  65473     pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
 64702  65474   
................................................................................
 64961  65733       if( xRecordCompare==0 ){
 64962  65734         for(;;){
 64963  65735           i64 nCellKey;
 64964  65736           pCell = findCellPastPtr(pPage, idx);
 64965  65737           if( pPage->intKeyLeaf ){
 64966  65738             while( 0x80 <= *(pCell++) ){
 64967  65739               if( pCell>=pPage->aDataEnd ){
 64968         -              return SQLITE_CORRUPT_PGNO(pPage->pgno);
        65740  +              return SQLITE_CORRUPT_PAGE(pPage);
 64969  65741               }
 64970  65742             }
 64971  65743           }
 64972  65744           getVarint(pCell, (u64*)&nCellKey);
 64973  65745           if( nCellKey<intKey ){
 64974  65746             lwr = idx+1;
 64975  65747             if( lwr>upr ){ c = -1; break; }
................................................................................
 65035  65807             pPage->xParseCell(pPage, pCellBody, &pCur->info);
 65036  65808             nCell = (int)pCur->info.nKey;
 65037  65809             testcase( nCell<0 );   /* True if key size is 2^32 or more */
 65038  65810             testcase( nCell==0 );  /* Invalid key size:  0x80 0x80 0x00 */
 65039  65811             testcase( nCell==1 );  /* Invalid key size:  0x80 0x80 0x01 */
 65040  65812             testcase( nCell==2 );  /* Minimum legal index key size */
 65041  65813             if( nCell<2 ){
 65042         -            rc = SQLITE_CORRUPT_PGNO(pPage->pgno);
        65814  +            rc = SQLITE_CORRUPT_PAGE(pPage);
 65043  65815               goto moveto_finish;
 65044  65816             }
 65045  65817             pCellKey = sqlite3Malloc( nCell+18 );
 65046  65818             if( pCellKey==0 ){
 65047  65819               rc = SQLITE_NOMEM_BKPT;
 65048  65820               goto moveto_finish;
 65049  65821             }
................................................................................
 65838  66610     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 65839  66611     pPage->xParseCell(pPage, pCell, pInfo);
 65840  66612     if( pInfo->nLocal==pInfo->nPayload ){
 65841  66613       return SQLITE_OK;  /* No overflow pages. Return without doing anything */
 65842  66614     }
 65843  66615     if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
 65844  66616       /* Cell extends past end of page */
 65845         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        66617  +    return SQLITE_CORRUPT_PAGE(pPage);
 65846  66618     }
 65847  66619     ovflPgno = get4byte(pCell + pInfo->nSize - 4);
 65848  66620     pBt = pPage->pBt;
 65849  66621     assert( pBt->usableSize > 4 );
 65850  66622     ovflPageSize = pBt->usableSize - 4;
 65851  66623     nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
 65852  66624     assert( nOvfl>0 || 
................................................................................
 70457  71229   
 70458  71230     /* Cannot be both MEM_Int and MEM_Real at the same time */
 70459  71231     assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
 70460  71232   
 70461  71233     if( p->flags & MEM_Null ){
 70462  71234       /* Cannot be both MEM_Null and some other type */
 70463  71235       assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob
 70464         -                         |MEM_RowSet|MEM_Frame|MEM_Agg|MEM_Zero))==0 );
        71236  +                         |MEM_RowSet|MEM_Frame|MEM_Agg))==0 );
 70465  71237   
 70466  71238       /* If MEM_Null is set, then either the value is a pure NULL (the usual
 70467  71239       ** case) or it is a pointer set using sqlite3_bind_pointer() or
 70468  71240       ** sqlite3_result_pointer().  If a pointer, then MEM_Term must also be
 70469  71241       ** set.
 70470  71242       */
 70471  71243       if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
................................................................................
 70764  71536   ** This routine calls the finalize method for that function.  The
 70765  71537   ** result of the aggregate is stored back into pMem.
 70766  71538   **
 70767  71539   ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
 70768  71540   ** otherwise.
 70769  71541   */
 70770  71542   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
 70771         -  int rc = SQLITE_OK;
 70772         -  if( ALWAYS(pFunc && pFunc->xFinalize) ){
 70773         -    sqlite3_context ctx;
 70774         -    Mem t;
 70775         -    assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
 70776         -    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 70777         -    memset(&ctx, 0, sizeof(ctx));
 70778         -    memset(&t, 0, sizeof(t));
 70779         -    t.flags = MEM_Null;
 70780         -    t.db = pMem->db;
 70781         -    ctx.pOut = &t;
 70782         -    ctx.pMem = pMem;
 70783         -    ctx.pFunc = pFunc;
 70784         -    pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
 70785         -    assert( (pMem->flags & MEM_Dyn)==0 );
 70786         -    if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
 70787         -    memcpy(pMem, &t, sizeof(t));
 70788         -    rc = ctx.isError;
 70789         -  }
 70790         -  return rc;
        71543  +  sqlite3_context ctx;
        71544  +  Mem t;
        71545  +  assert( pFunc!=0 );
        71546  +  assert( pFunc->xFinalize!=0 );
        71547  +  assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
        71548  +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
        71549  +  memset(&ctx, 0, sizeof(ctx));
        71550  +  memset(&t, 0, sizeof(t));
        71551  +  t.flags = MEM_Null;
        71552  +  t.db = pMem->db;
        71553  +  ctx.pOut = &t;
        71554  +  ctx.pMem = pMem;
        71555  +  ctx.pFunc = pFunc;
        71556  +  pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
        71557  +  assert( (pMem->flags & MEM_Dyn)==0 );
        71558  +  if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
        71559  +  memcpy(pMem, &t, sizeof(t));
        71560  +  return ctx.isError;
 70791  71561   }
 70792  71562   
 70793  71563   /*
 70794  71564   ** If the memory cell contains a value that must be freed by
 70795  71565   ** invoking the external callback in Mem.xDel, then this routine
 70796  71566   ** will free that value.  It also sets Mem.flags to MEM_Null.
 70797  71567   **
................................................................................
 71737  72507     sqlite3_value *pVal = 0;
 71738  72508     int negInt = 1;
 71739  72509     const char *zNeg = "";
 71740  72510     int rc = SQLITE_OK;
 71741  72511   
 71742  72512     assert( pExpr!=0 );
 71743  72513     while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
        72514  +#if defined(SQLITE_ENABLE_STAT3_OR_STAT4)
        72515  +  if( op==TK_REGISTER ) op = pExpr->op2;
        72516  +#else
 71744  72517     if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
        72518  +#endif
 71745  72519   
 71746  72520     /* Compressed expressions only appear when parsing the DEFAULT clause
 71747  72521     ** on a table column definition, and hence only when pCtx==0.  This
 71748  72522     ** check ensures that an EP_TokenOnly expression is never passed down
 71749  72523     ** into valueFromFunction(). */
 71750  72524     assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
 71751  72525   
................................................................................
 71832  72606     }
 71833  72607   #endif
 71834  72608   
 71835  72609     *ppVal = pVal;
 71836  72610     return rc;
 71837  72611   
 71838  72612   no_mem:
 71839         -  sqlite3OomFault(db);
        72613  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
        72614  +  if( pCtx==0 || pCtx->pParse->nErr==0 )
        72615  +#endif
        72616  +    sqlite3OomFault(db);
 71840  72617     sqlite3DbFree(db, zVal);
 71841  72618     assert( *ppVal==0 );
 71842  72619   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 71843  72620     if( pCtx==0 ) sqlite3ValueFree(pVal);
 71844  72621   #else
 71845  72622     assert( pCtx==0 ); sqlite3ValueFree(pVal);
 71846  72623   #endif
................................................................................
 73045  73822       case P4_FUNCCTX: {
 73046  73823         freeP4FuncCtx(db, (sqlite3_context*)p4);
 73047  73824         break;
 73048  73825       }
 73049  73826       case P4_REAL:
 73050  73827       case P4_INT64:
 73051  73828       case P4_DYNAMIC:
        73829  +    case P4_DYNBLOB:
 73052  73830       case P4_INTARRAY: {
 73053  73831         sqlite3DbFree(db, p4);
 73054  73832         break;
 73055  73833       }
 73056  73834       case P4_KEYINFO: {
 73057  73835         if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
 73058  73836         break;
................................................................................
 73586  74364         sqlite3StrAccumAppend(&x, "]", 1);
 73587  74365         break;
 73588  74366       }
 73589  74367       case P4_SUBPROGRAM: {
 73590  74368         sqlite3XPrintf(&x, "program");
 73591  74369         break;
 73592  74370       }
        74371  +    case P4_DYNBLOB:
 73593  74372       case P4_ADVANCE: {
 73594  74373         zTemp[0] = 0;
 73595  74374         break;
 73596  74375       }
 73597  74376       case P4_TABLE: {
 73598  74377         sqlite3XPrintf(&x, "%s", pOp->p4.pTab->zName);
 73599  74378         break;
................................................................................
 73818  74597     int nSub = 0;                        /* Number of sub-vdbes seen so far */
 73819  74598     SubProgram **apSub = 0;              /* Array of sub-vdbes */
 73820  74599     Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
 73821  74600     sqlite3 *db = p->db;                 /* The database connection */
 73822  74601     int i;                               /* Loop counter */
 73823  74602     int rc = SQLITE_OK;                  /* Return code */
 73824  74603     Mem *pMem = &p->aMem[1];             /* First Mem of result set */
        74604  +  int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
        74605  +  Op *pOp = 0;
 73825  74606   
 73826  74607     assert( p->explain );
 73827  74608     assert( p->magic==VDBE_MAGIC_RUN );
 73828  74609     assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
 73829  74610   
 73830  74611     /* Even though this opcode does not use dynamic strings for
 73831  74612     ** the result, result columns may become dynamic if the user calls
 73832  74613     ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
 73833  74614     */
 73834  74615     releaseMemArray(pMem, 8);
 73835  74616     p->pResultSet = 0;
 73836  74617   
 73837         -  if( p->rc==SQLITE_NOMEM_BKPT ){
        74618  +  if( p->rc==SQLITE_NOMEM ){
 73838  74619       /* This happens if a malloc() inside a call to sqlite3_column_text() or
 73839  74620       ** sqlite3_column_text16() failed.  */
 73840  74621       sqlite3OomFault(db);
 73841  74622       return SQLITE_ERROR;
 73842  74623     }
 73843  74624   
 73844  74625     /* When the number of output rows reaches nRow, that means the
................................................................................
 73845  74626     ** listing has finished and sqlite3_step() should return SQLITE_DONE.
 73846  74627     ** nRow is the sum of the number of rows in the main program, plus
 73847  74628     ** the sum of the number of rows in all trigger subprograms encountered
 73848  74629     ** so far.  The nRow value will increase as new trigger subprograms are
 73849  74630     ** encountered, but p->pc will eventually catch up to nRow.
 73850  74631     */
 73851  74632     nRow = p->nOp;
 73852         -  if( p->explain==1 ){
        74633  +  if( bListSubprogs ){
 73853  74634       /* The first 8 memory cells are used for the result set.  So we will
 73854  74635       ** commandeer the 9th cell to use as storage for an array of pointers
 73855  74636       ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
 73856  74637       ** cells.  */
 73857  74638       assert( p->nMem>9 );
 73858  74639       pSub = &p->aMem[9];
 73859  74640       if( pSub->flags&MEM_Blob ){
................................................................................
 73865  74646       for(i=0; i<nSub; i++){
 73866  74647         nRow += apSub[i]->nOp;
 73867  74648       }
 73868  74649     }
 73869  74650   
 73870  74651     do{
 73871  74652       i = p->pc++;
 73872         -  }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
 73873         -  if( i>=nRow ){
 73874         -    p->rc = SQLITE_OK;
 73875         -    rc = SQLITE_DONE;
 73876         -  }else if( db->u1.isInterrupted ){
 73877         -    p->rc = SQLITE_INTERRUPT;
 73878         -    rc = SQLITE_ERROR;
 73879         -    sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
 73880         -  }else{
 73881         -    char *zP4;
 73882         -    Op *pOp;
        74653  +    if( i>=nRow ){
        74654  +      p->rc = SQLITE_OK;
        74655  +      rc = SQLITE_DONE;
        74656  +      break;
        74657  +    }
 73883  74658       if( i<p->nOp ){
 73884  74659         /* The output line number is small enough that we are still in the
 73885  74660         ** main program. */
 73886  74661         pOp = &p->aOp[i];
 73887  74662       }else{
 73888  74663         /* We are currently listing subprograms.  Figure out which one and
 73889  74664         ** pick up the appropriate opcode. */
................................................................................
 73890  74665         int j;
 73891  74666         i -= p->nOp;
 73892  74667         for(j=0; i>=apSub[j]->nOp; j++){
 73893  74668           i -= apSub[j]->nOp;
 73894  74669         }
 73895  74670         pOp = &apSub[j]->aOp[i];
 73896  74671       }
 73897         -    if( p->explain==1 ){
        74672  +
        74673  +    /* When an OP_Program opcode is encounter (the only opcode that has
        74674  +    ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
        74675  +    ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
        74676  +    ** has not already been seen.
        74677  +    */
        74678  +    if( bListSubprogs && pOp->p4type==P4_SUBPROGRAM ){
        74679  +      int nByte = (nSub+1)*sizeof(SubProgram*);
        74680  +      int j;
        74681  +      for(j=0; j<nSub; j++){
        74682  +        if( apSub[j]==pOp->p4.pProgram ) break;
        74683  +      }
        74684  +      if( j==nSub ){
        74685  +        p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
        74686  +        if( p->rc!=SQLITE_OK ){
        74687  +          rc = SQLITE_ERROR;
        74688  +          break;
        74689  +        }
        74690  +        apSub = (SubProgram **)pSub->z;
        74691  +        apSub[nSub++] = pOp->p4.pProgram;
        74692  +        pSub->flags |= MEM_Blob;
        74693  +        pSub->n = nSub*sizeof(SubProgram*);
        74694  +        nRow += pOp->p4.pProgram->nOp;
        74695  +      }
        74696  +    }
        74697  +  }while( p->explain==2 && pOp->opcode!=OP_Explain );
        74698  +
        74699  +  if( rc==SQLITE_OK ){
        74700  +    if( db->u1.isInterrupted ){
        74701  +      p->rc = SQLITE_INTERRUPT;
        74702  +      rc = SQLITE_ERROR;
        74703  +      sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
        74704  +    }else{
        74705  +      char *zP4;
        74706  +      if( p->explain==1 ){
        74707  +        pMem->flags = MEM_Int;
        74708  +        pMem->u.i = i;                                /* Program counter */
        74709  +        pMem++;
        74710  +    
        74711  +        pMem->flags = MEM_Static|MEM_Str|MEM_Term;
        74712  +        pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
        74713  +        assert( pMem->z!=0 );
        74714  +        pMem->n = sqlite3Strlen30(pMem->z);
        74715  +        pMem->enc = SQLITE_UTF8;
        74716  +        pMem++;
        74717  +      }
        74718  +
        74719  +      pMem->flags = MEM_Int;
        74720  +      pMem->u.i = pOp->p1;                          /* P1 */
        74721  +      pMem++;
        74722  +
 73898  74723         pMem->flags = MEM_Int;
 73899         -      pMem->u.i = i;                                /* Program counter */
        74724  +      pMem->u.i = pOp->p2;                          /* P2 */
 73900  74725         pMem++;
 73901         -  
 73902         -      pMem->flags = MEM_Static|MEM_Str|MEM_Term;
 73903         -      pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
 73904         -      assert( pMem->z!=0 );
 73905         -      pMem->n = sqlite3Strlen30(pMem->z);
 73906         -      pMem->enc = SQLITE_UTF8;
        74726  +
        74727  +      pMem->flags = MEM_Int;
        74728  +      pMem->u.i = pOp->p3;                          /* P3 */
 73907  74729         pMem++;
 73908  74730   
 73909         -      /* When an OP_Program opcode is encounter (the only opcode that has
 73910         -      ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
 73911         -      ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
 73912         -      ** has not already been seen.
 73913         -      */
 73914         -      if( pOp->p4type==P4_SUBPROGRAM ){
 73915         -        int nByte = (nSub+1)*sizeof(SubProgram*);
 73916         -        int j;
 73917         -        for(j=0; j<nSub; j++){
 73918         -          if( apSub[j]==pOp->p4.pProgram ) break;
 73919         -        }
 73920         -        if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
 73921         -          apSub = (SubProgram **)pSub->z;
 73922         -          apSub[nSub++] = pOp->p4.pProgram;
 73923         -          pSub->flags |= MEM_Blob;
 73924         -          pSub->n = nSub*sizeof(SubProgram*);
 73925         -        }
 73926         -      }
 73927         -    }
 73928         -
 73929         -    pMem->flags = MEM_Int;
 73930         -    pMem->u.i = pOp->p1;                          /* P1 */
 73931         -    pMem++;
 73932         -
 73933         -    pMem->flags = MEM_Int;
 73934         -    pMem->u.i = pOp->p2;                          /* P2 */
 73935         -    pMem++;
 73936         -
 73937         -    pMem->flags = MEM_Int;
 73938         -    pMem->u.i = pOp->p3;                          /* P3 */
 73939         -    pMem++;
 73940         -
 73941         -    if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
 73942         -      assert( p->db->mallocFailed );
 73943         -      return SQLITE_ERROR;
 73944         -    }
 73945         -    pMem->flags = MEM_Str|MEM_Term;
 73946         -    zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
 73947         -    if( zP4!=pMem->z ){
 73948         -      pMem->n = 0;
 73949         -      sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
 73950         -    }else{
 73951         -      assert( pMem->z!=0 );
 73952         -      pMem->n = sqlite3Strlen30(pMem->z);
 73953         -      pMem->enc = SQLITE_UTF8;
 73954         -    }
 73955         -    pMem++;
 73956         -
 73957         -    if( p->explain==1 ){
 73958         -      if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
        74731  +      if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
 73959  74732           assert( p->db->mallocFailed );
 73960  74733           return SQLITE_ERROR;
 73961  74734         }
 73962  74735         pMem->flags = MEM_Str|MEM_Term;
 73963         -      pMem->n = 2;
 73964         -      sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
 73965         -      pMem->enc = SQLITE_UTF8;
        74736  +      zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
        74737  +      if( zP4!=pMem->z ){
        74738  +        pMem->n = 0;
        74739  +        sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
        74740  +      }else{
        74741  +        assert( pMem->z!=0 );
        74742  +        pMem->n = sqlite3Strlen30(pMem->z);
        74743  +        pMem->enc = SQLITE_UTF8;
        74744  +      }
 73966  74745         pMem++;
 73967         -  
        74746  +
        74747  +      if( p->explain==1 ){
        74748  +        if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
        74749  +          assert( p->db->mallocFailed );
        74750  +          return SQLITE_ERROR;
        74751  +        }
        74752  +        pMem->flags = MEM_Str|MEM_Term;
        74753  +        pMem->n = 2;
        74754  +        sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
        74755  +        pMem->enc = SQLITE_UTF8;
        74756  +        pMem++;
        74757  +    
 73968  74758   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 73969         -      if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
 73970         -        assert( p->db->mallocFailed );
 73971         -        return SQLITE_ERROR;
        74759  +        if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
        74760  +          assert( p->db->mallocFailed );
        74761  +          return SQLITE_ERROR;
        74762  +        }
        74763  +        pMem->flags = MEM_Str|MEM_Term;
        74764  +        pMem->n = displayComment(pOp, zP4, pMem->z, 500);
        74765  +        pMem->enc = SQLITE_UTF8;
        74766  +#else
        74767  +        pMem->flags = MEM_Null;                       /* Comment */
        74768  +#endif
 73972  74769         }
 73973         -      pMem->flags = MEM_Str|MEM_Term;
 73974         -      pMem->n = displayComment(pOp, zP4, pMem->z, 500);
 73975         -      pMem->enc = SQLITE_UTF8;
 73976         -#else
 73977         -      pMem->flags = MEM_Null;                       /* Comment */
 73978         -#endif
        74770  +
        74771  +      p->nResColumn = 8 - 4*(p->explain-1);
        74772  +      p->pResultSet = &p->aMem[1];
        74773  +      p->rc = SQLITE_OK;
        74774  +      rc = SQLITE_ROW;
 73979  74775       }
 73980         -
 73981         -    p->nResColumn = 8 - 4*(p->explain-1);
 73982         -    p->pResultSet = &p->aMem[1];
 73983         -    p->rc = SQLITE_OK;
 73984         -    rc = SQLITE_ROW;
 73985  74776     }
 73986  74777     return rc;
 73987  74778   }
 73988  74779   #endif /* SQLITE_OMIT_EXPLAIN */
 73989  74780   
 73990  74781   #ifdef SQLITE_DEBUG
 73991  74782   /*
................................................................................
 74447  75238         };
 74448  75239         Pager *pPager;   /* Pager associated with pBt */
 74449  75240         needXcommit = 1;
 74450  75241         sqlite3BtreeEnter(pBt);
 74451  75242         pPager = sqlite3BtreePager(pBt);
 74452  75243         if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
 74453  75244          && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
        75245  +       && sqlite3PagerIsMemdb(pPager)==0
 74454  75246         ){ 
 74455  75247           assert( i!=1 );
 74456  75248           nTrans++;
 74457  75249         }
 74458  75250         rc = sqlite3PagerExclusiveLock(pPager);
 74459  75251         sqlite3BtreeLeave(pBt);
 74460  75252       }
................................................................................
 75222  76014   
 75223  76015   /*
 75224  76016   ** Delete an entire VDBE.
 75225  76017   */
 75226  76018   SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
 75227  76019     sqlite3 *db;
 75228  76020   
 75229         -  if( NEVER(p==0) ) return;
        76021  +  assert( p!=0 );
 75230  76022     db = p->db;
 75231  76023     assert( sqlite3_mutex_held(db->mutex) );
 75232  76024     sqlite3VdbeClearObject(db, p);
 75233  76025     if( p->pPrev ){
 75234  76026       p->pPrev->pNext = p->pNext;
 75235  76027     }else{
 75236  76028       assert( db->pVdbe==p );
................................................................................
 75618  76410   }
 75619  76411   SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
 75620  76412     const unsigned char *buf,     /* Buffer to deserialize from */
 75621  76413     u32 serial_type,              /* Serial type to deserialize */
 75622  76414     Mem *pMem                     /* Memory cell to write value into */
 75623  76415   ){
 75624  76416     switch( serial_type ){
 75625         -    case 10:   /* Reserved for future use */
        76417  +    case 10: { /* Internal use only: NULL with virtual table
        76418  +               ** UPDATE no-change flag set */
        76419  +      pMem->flags = MEM_Null|MEM_Zero;
        76420  +      pMem->n = 0;
        76421  +      pMem->u.nZero = 0;
        76422  +      break;
        76423  +    }
 75626  76424       case 11:   /* Reserved for future use */
 75627  76425       case 0: {  /* Null */
 75628  76426         /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
 75629  76427         pMem->flags = MEM_Null;
 75630  76428         break;
 75631  76429       }
 75632  76430       case 1: {
................................................................................
 77170  77968        SQLITE_INTEGER,  /* 0x1c */
 77171  77969        SQLITE_NULL,     /* 0x1d */
 77172  77970        SQLITE_INTEGER,  /* 0x1e */
 77173  77971        SQLITE_NULL,     /* 0x1f */
 77174  77972     };
 77175  77973     return aType[pVal->flags&MEM_AffMask];
 77176  77974   }
        77975  +
        77976  +/* Return true if a parameter to xUpdate represents an unchanged column */
        77977  +SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
        77978  +  return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
        77979  +}
 77177  77980   
 77178  77981   /* Make a copy of an sqlite3_value object
 77179  77982   */
 77180  77983   SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
 77181  77984     sqlite3_value *pNew;
 77182  77985     if( pOrig==0 ) return 0;
 77183  77986     pNew = sqlite3_malloc( sizeof(*pNew) );
................................................................................
 77647  78450   ** sqlite3_create_function16() routines that originally registered the
 77648  78451   ** application defined function.
 77649  78452   */
 77650  78453   SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
 77651  78454     assert( p && p->pOut );
 77652  78455     return p->pOut->db;
 77653  78456   }
        78457  +
        78458  +/*
        78459  +** If this routine is invoked from within an xColumn method of a virtual
        78460  +** table, then it returns true if and only if the the call is during an
        78461  +** UPDATE operation and the value of the column will not be modified
        78462  +** by the UPDATE.
        78463  +**
        78464  +** If this routine is called from any context other than within the
        78465  +** xColumn method of a virtual table, then the return value is meaningless
        78466  +** and arbitrary.
        78467  +**
        78468  +** Virtual table implements might use this routine to optimize their
        78469  +** performance by substituting a NULL result, or some other light-weight
        78470  +** value, as a signal to the xUpdate routine that the column is unchanged.
        78471  +*/
        78472  +SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
        78473  +  assert( p );
        78474  +  return sqlite3_value_nochange(p->pOut);
        78475  +}
 77654  78476   
 77655  78477   /*
 77656  78478   ** Return the current time for a statement.  If the current time
 77657  78479   ** is requested more than once within the same run of a single prepared
 77658  78480   ** statement, the exact same time is returned for each invocation regardless
 77659  78481   ** of the amount of time that elapses between invocations.  In other words,
 77660  78482   ** the time returned is always the time of the first call.
................................................................................
 79529  80351   /*
 79530  80352   ** Print the value of a register for tracing purposes:
 79531  80353   */
 79532  80354   static void memTracePrint(Mem *p){
 79533  80355     if( p->flags & MEM_Undefined ){
 79534  80356       printf(" undefined");
 79535  80357     }else if( p->flags & MEM_Null ){
 79536         -    printf(" NULL");
        80358  +    printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
 79537  80359     }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
 79538  80360       printf(" si:%lld", p->u.i);
 79539  80361     }else if( p->flags & MEM_Int ){
 79540  80362       printf(" i:%lld", p->u.i);
 79541  80363   #ifndef SQLITE_OMIT_FLOATING_POINT
 79542  80364     }else if( p->flags & MEM_Real ){
 79543  80365       printf(" r:%g", p->u.r);
................................................................................
 81503  82325     if( p->apCsr[pOp->p1]->nullRow ){
 81504  82326       sqlite3VdbeMemSetNull(aMem + pOp->p3);
 81505  82327       goto jump_to_p2;
 81506  82328     }
 81507  82329     break;
 81508  82330   }
 81509  82331   
        82332  +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
        82333  +/* Opcode: Offset P1 P2 P3 * *
        82334  +** Synopsis: r[P3] = sqlite_offset(P1)
        82335  +**
        82336  +** Store in register r[P3] the byte offset into the database file that is the
        82337  +** start of the payload for the record at which that cursor P1 is currently
        82338  +** pointing.
        82339  +**
        82340  +** P2 is the column number for the argument to the sqlite_offset() function.
        82341  +** This opcode does not use P2 itself, but the P2 value is used by the
        82342  +** code generator.  The P1, P2, and P3 operands to this opcode are the
        82343  +** as as for OP_Column.
        82344  +**
        82345  +** This opcode is only available if SQLite is compiled with the
        82346  +** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
        82347  +*/
        82348  +case OP_Offset: {          /* out3 */
        82349  +  VdbeCursor *pC;    /* The VDBE cursor */
        82350  +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
        82351  +  pC = p->apCsr[pOp->p1];
        82352  +  pOut = &p->aMem[pOp->p3];
        82353  +  if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
        82354  +    sqlite3VdbeMemSetNull(pOut);
        82355  +  }else{
        82356  +    sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
        82357  +  }
        82358  +  break;
        82359  +}
        82360  +#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
        82361  +
 81510  82362   /* Opcode: Column P1 P2 P3 P4 P5
 81511  82363   ** Synopsis: r[P3]=PX
 81512  82364   **
 81513  82365   ** Interpret the data that cursor P1 points to as a structure built using
 81514  82366   ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
 81515  82367   ** information about the format of the data.)  Extract the P2-th column
 81516  82368   ** from this record.  If there are less that (P2+1) 
................................................................................
 81916  82768   
 81917  82769     /* Loop through the elements that will make up the record to figure
 81918  82770     ** out how much space is required for the new record.
 81919  82771     */
 81920  82772     pRec = pLast;
 81921  82773     do{
 81922  82774       assert( memIsValid(pRec) );
 81923         -    pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
        82775  +    serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
 81924  82776       if( pRec->flags & MEM_Zero ){
 81925         -      if( nData ){
        82777  +      if( serial_type==0 ){
        82778  +        /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
        82779  +        ** table methods that never invoke sqlite3_result_xxxxx() while
        82780  +        ** computing an unchanging column value in an UPDATE statement.
        82781  +        ** Give such values a special internal-use-only serial-type of 10
        82782  +        ** so that they can be passed through to xUpdate and have
        82783  +        ** a true sqlite3_value_nochange(). */
        82784  +        assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
        82785  +        serial_type = 10;
        82786  +      }else if( nData ){
 81926  82787           if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
 81927  82788         }else{
 81928  82789           nZero += pRec->u.nZero;
 81929  82790           len -= pRec->u.nZero;
 81930  82791         }
 81931  82792       }
 81932  82793       nData += len;
 81933  82794       testcase( serial_type==127 );
 81934  82795       testcase( serial_type==128 );
 81935  82796       nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
        82797  +    pRec->uTemp = serial_type;
 81936  82798       if( pRec==pData0 ) break;
 81937  82799       pRec--;
 81938  82800     }while(1);
 81939  82801   
 81940  82802     /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
 81941  82803     ** which determines the total number of bytes in the header. The varint
 81942  82804     ** value is the size of the header in bytes including the size varint
................................................................................
 83539  84401   case OP_InsertInt: {
 83540  84402     Mem *pData;       /* MEM cell holding data for the record to be inserted */
 83541  84403     Mem *pKey;        /* MEM cell holding key  for the record */
 83542  84404     VdbeCursor *pC;   /* Cursor to table into which insert is written */
 83543  84405     int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 83544  84406     const char *zDb;  /* database name - used by the update hook */
 83545  84407     Table *pTab;      /* Table structure - used by update and pre-update hooks */
 83546         -  int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
 83547  84408     BtreePayload x;   /* Payload to be inserted */
 83548  84409   
 83549         -  op = 0;
 83550  84410     pData = &aMem[pOp->p2];
 83551  84411     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 83552  84412     assert( memIsValid(pData) );
 83553  84413     pC = p->apCsr[pOp->p1];
 83554  84414     assert( pC!=0 );
 83555  84415     assert( pC->eCurType==CURTYPE_BTREE );
 83556  84416     assert( pC->uc.pCursor!=0 );
................................................................................
 83570  84430     }
 83571  84431   
 83572  84432     if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 83573  84433       assert( pC->iDb>=0 );
 83574  84434       zDb = db->aDb[pC->iDb].zDbSName;
 83575  84435       pTab = pOp->p4.pTab;
 83576  84436       assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
 83577         -    op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
 83578  84437     }else{
 83579         -    pTab = 0; /* Not needed.  Silence a compiler warning. */
        84438  +    pTab = 0;
 83580  84439       zDb = 0;  /* Not needed.  Silence a compiler warning. */
 83581  84440     }
 83582  84441   
 83583  84442   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 83584  84443     /* Invoke the pre-update hook, if any */
 83585         -  if( db->xPreUpdateCallback 
 83586         -   && pOp->p4type==P4_TABLE
 83587         -   && !(pOp->p5 & OPFLAG_ISUPDATE)
 83588         -  ){
 83589         -    sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
        84444  +  if( pTab ){
        84445  +    if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
        84446  +      sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
        84447  +    }
        84448  +    if( db->xUpdateCallback==0 || pTab->aCol==0 ){
        84449  +      /* Prevent post-update hook from running in cases when it should not */
        84450  +      pTab = 0;
        84451  +    }
 83590  84452     }
 83591  84453     if( pOp->p5 & OPFLAG_ISNOOP ) break;
 83592  84454   #endif
 83593  84455   
 83594  84456     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 83595  84457     if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
 83596  84458     assert( pData->flags & (MEM_Blob|MEM_Str) );
................................................................................
 83607  84469         (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
 83608  84470     );
 83609  84471     pC->deferredMoveto = 0;
 83610  84472     pC->cacheStatus = CACHE_STALE;
 83611  84473   
 83612  84474     /* Invoke the update-hook if required. */
 83613  84475     if( rc ) goto abort_due_to_error;
 83614         -  if( db->xUpdateCallback && op ){
 83615         -    db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey);
        84476  +  if( pTab ){
        84477  +    assert( db->xUpdateCallback!=0 );
        84478  +    assert( pTab->aCol!=0 );
        84479  +    db->xUpdateCallback(db->pUpdateArg,
        84480  +           (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
        84481  +           zDb, pTab->zName, x.nKey);
 83616  84482     }
 83617  84483     break;
 83618  84484   }
 83619  84485   
 83620  84486   /* Opcode: Delete P1 P2 P3 P4 P5
 83621  84487   **
 83622  84488   ** Delete the record at which the P1 cursor is currently pointing.
................................................................................
 85817  86683     VdbeBranchTaken(res!=0,2);
 85818  86684     if( res ) goto jump_to_p2;
 85819  86685     break;
 85820  86686   }
 85821  86687   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 85822  86688   
 85823  86689   #ifndef SQLITE_OMIT_VIRTUALTABLE
 85824         -/* Opcode: VColumn P1 P2 P3 * *
        86690  +/* Opcode: VColumn P1 P2 P3 * P5
 85825  86691   ** Synopsis: r[P3]=vcolumn(P2)
 85826  86692   **
 85827         -** Store the value of the P2-th column of
 85828         -** the row of the virtual-table that the 
 85829         -** P1 cursor is pointing to into register P3.
        86693  +** Store in register P3 the value of the P2-th column of
        86694  +** the current row of the virtual-table of cursor P1.
        86695  +**
        86696  +** If the VColumn opcode is being used to fetch the value of
        86697  +** an unchanging column during an UPDATE operation, then the P5
        86698  +** value is 1.  Otherwise, P5 is 0.  The P5 value is returned
        86699  +** by sqlite3_vtab_nochange() routine can can be used
        86700  +** by virtual table implementations to return special "no-change"
        86701  +** marks which can be more efficient, depending on the virtual table.
 85830  86702   */
 85831  86703   case OP_VColumn: {
 85832  86704     sqlite3_vtab *pVtab;
 85833  86705     const sqlite3_module *pModule;
 85834  86706     Mem *pDest;
 85835  86707     sqlite3_context sContext;
 85836  86708   
................................................................................
 85844  86716       break;
 85845  86717     }
 85846  86718     pVtab = pCur->uc.pVCur->pVtab;
 85847  86719     pModule = pVtab->pModule;
 85848  86720     assert( pModule->xColumn );
 85849  86721     memset(&sContext, 0, sizeof(sContext));
 85850  86722     sContext.pOut = pDest;
 85851         -  MemSetTypeFlag(pDest, MEM_Null);
        86723  +  if( pOp->p5 ){
        86724  +    sqlite3VdbeMemSetNull(pDest);
        86725  +    pDest->flags = MEM_Null|MEM_Zero;
        86726  +    pDest->u.nZero = 0;
        86727  +  }else{
        86728  +    MemSetTypeFlag(pDest, MEM_Null);
        86729  +  }
 85852  86730     rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
 85853  86731     sqlite3VtabImportErrmsg(p, pVtab);
 85854  86732     if( sContext.isError ){
 85855  86733       rc = sContext.isError;
 85856  86734     }
 85857  86735     sqlite3VdbeChangeEncoding(pDest, encoding);
 85858  86736     REGISTER_TRACE(pOp->p3, pDest);
................................................................................
 86170  87048     }
 86171  87049   
 86172  87050     REGISTER_TRACE(pOp->p3, pOut);
 86173  87051     UPDATE_MAX_BLOBSIZE(pOut);
 86174  87052     break;
 86175  87053   }
 86176  87054   
 86177         -
        87055  +/* Opcode: Trace P1 P2 * P4 *
        87056  +**
        87057  +** Write P4 on the statement trace output if statement tracing is
        87058  +** enabled.
        87059  +**
        87060  +** Operand P1 must be 0x7fffffff and P2 must positive.
        87061  +*/
 86178  87062   /* Opcode: Init P1 P2 P3 P4 *
 86179  87063   ** Synopsis: Start at P2
 86180  87064   **
 86181  87065   ** Programs contain a single instance of this opcode as the very first
 86182  87066   ** opcode.
 86183  87067   **
 86184  87068   ** If tracing is enabled (by the sqlite3_trace()) interface, then
................................................................................
 86189  87073   **
 86190  87074   ** Increment the value of P1 so that OP_Once opcodes will jump the
 86191  87075   ** first time they are evaluated for this run.
 86192  87076   **
 86193  87077   ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
 86194  87078   ** error is encountered.
 86195  87079   */
        87080  +case OP_Trace:
 86196  87081   case OP_Init: {          /* jump */
 86197  87082     char *zTrace;
 86198  87083     int i;
 86199  87084   
 86200  87085     /* If the P4 argument is not NULL, then it must be an SQL comment string.
 86201  87086     ** The "--" string is broken up to prevent false-positives with srcck1.c.
 86202  87087     **
................................................................................
 86203  87088     ** This assert() provides evidence for:
 86204  87089     ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
 86205  87090     ** would have been returned by the legacy sqlite3_trace() interface by
 86206  87091     ** using the X argument when X begins with "--" and invoking
 86207  87092     ** sqlite3_expanded_sql(P) otherwise.
 86208  87093     */
 86209  87094     assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
 86210         -  assert( pOp==p->aOp );  /* Always instruction 0 */
        87095  +
        87096  +  /* OP_Init is always instruction 0 */
        87097  +  assert( pOp==p->aOp || pOp->opcode==OP_Trace );
 86211  87098   
 86212  87099   #ifndef SQLITE_OMIT_TRACE
 86213  87100     if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
 86214  87101      && !p->doingRerun
 86215  87102      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 86216  87103     ){
 86217  87104   #ifndef SQLITE_OMIT_DEPRECATED
................................................................................
 86246  87133     ){
 86247  87134       sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
 86248  87135     }
 86249  87136   #endif /* SQLITE_DEBUG */
 86250  87137   #endif /* SQLITE_OMIT_TRACE */
 86251  87138     assert( pOp->p2>0 );
 86252  87139     if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
        87140  +    if( pOp->opcode==OP_Trace ) break;
 86253  87141       for(i=1; i<p->nOp; i++){
 86254  87142         if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
 86255  87143       }
 86256  87144       pOp->p1 = 0;
 86257  87145     }
 86258  87146     pOp->p1++;
 86259  87147     p->aCounter[SQLITE_STMTSTATUS_RUN]++;
................................................................................
 90190  91078   SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
 90191  91079     if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
 90192  91080     if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
 90193  91081     if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
 90194  91082     if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
 90195  91083     if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
 90196  91084     if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
 90197         -  if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
 90198  91085     return WRC_Continue;
 90199  91086   }
 90200  91087   
 90201  91088   /*
 90202  91089   ** Walk the parse trees associated with all subqueries in the
 90203  91090   ** FROM clause of SELECT statement p.  Do not invoke the select
 90204  91091   ** callback on p, but do invoke it on each FROM clause subquery
................................................................................
 90207  91094   */
 90208  91095   SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
 90209  91096     SrcList *pSrc;
 90210  91097     int i;
 90211  91098     struct SrcList_item *pItem;
 90212  91099   
 90213  91100     pSrc = p->pSrc;
 90214         -  if( ALWAYS(pSrc) ){
 90215         -    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
 90216         -      if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
 90217         -        return WRC_Abort;
 90218         -      }
 90219         -      if( pItem->fg.isTabFunc
 90220         -       && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
 90221         -      ){
 90222         -        return WRC_Abort;
 90223         -      }
        91101  +  assert( pSrc!=0 );
        91102  +  for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
        91103  +    if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
        91104  +      return WRC_Abort;
        91105  +    }
        91106  +    if( pItem->fg.isTabFunc
        91107  +     && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
        91108  +    ){
        91109  +      return WRC_Abort;
 90224  91110       }
 90225  91111     }
 90226  91112     return WRC_Continue;
 90227  91113   } 
 90228  91114   
 90229  91115   /*
 90230  91116   ** Call sqlite3WalkExpr() for every expression in Select statement p.
................................................................................
 90859  91745       ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
 90860  91746       ** clause processing on UPDATE and DELETE statements.
 90861  91747       */
 90862  91748       case TK_ROW: {
 90863  91749         SrcList *pSrcList = pNC->pSrcList;
 90864  91750         struct SrcList_item *pItem;
 90865  91751         assert( pSrcList && pSrcList->nSrc==1 );
 90866         -      pItem = pSrcList->a; 
        91752  +      pItem = pSrcList->a;
        91753  +      assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
 90867  91754         pExpr->op = TK_COLUMN;
 90868  91755         pExpr->pTab = pItem->pTab;
 90869  91756         pExpr->iTable = pItem->iCursor;
 90870  91757         pExpr->iColumn = -1;
 90871  91758         pExpr->affinity = SQLITE_AFF_INTEGER;
 90872  91759         break;
 90873  91760       }
................................................................................
 91458  92345       p->selFlags |= SF_Resolved;
 91459  92346   
 91460  92347       /* Resolve the expressions in the LIMIT and OFFSET clauses. These
 91461  92348       ** are not allowed to refer to any names, so pass an empty NameContext.
 91462  92349       */
 91463  92350       memset(&sNC, 0, sizeof(sNC));
 91464  92351       sNC.pParse = pParse;
 91465         -    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
 91466         -        sqlite3ResolveExprNames(&sNC, p->pOffset) ){
        92352  +    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
 91467  92353         return WRC_Abort;
 91468  92354       }
 91469  92355   
 91470  92356       /* If the SF_Converted flags is set, then this Select object was
 91471  92357       ** was created by the convertCompoundSelectToSubquery() function.
 91472  92358       ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
 91473  92359       ** as if it were part of the sub-query, not the parent. This block
................................................................................
 92455  93341     if( p ){
 92456  93342       int i;
 92457  93343       for(i=0; i<p->nExpr; i++){
 92458  93344         heightOfExpr(p->a[i].pExpr, pnHeight);
 92459  93345       }
 92460  93346     }
 92461  93347   }
 92462         -static void heightOfSelect(Select *p, int *pnHeight){
 92463         -  if( p ){
        93348  +static void heightOfSelect(Select *pSelect, int *pnHeight){
        93349  +  Select *p;
        93350  +  for(p=pSelect; p; p=p->pPrior){
 92464  93351       heightOfExpr(p->pWhere, pnHeight);
 92465  93352       heightOfExpr(p->pHaving, pnHeight);
 92466  93353       heightOfExpr(p->pLimit, pnHeight);
 92467         -    heightOfExpr(p->pOffset, pnHeight);
 92468  93354       heightOfExprList(p->pEList, pnHeight);
 92469  93355       heightOfExprList(p->pGroupBy, pnHeight);
 92470  93356       heightOfExprList(p->pOrderBy, pnHeight);
 92471         -    heightOfSelect(p->pPrior, pnHeight);
 92472  93357     }
 92473  93358   }
 92474  93359   
 92475  93360   /*
 92476  93361   ** Set the Expr.nHeight variable in the structure passed as an 
 92477  93362   ** argument. An expression with no children, Expr.pList or 
 92478  93363   ** Expr.pSelect member has a height of 1. Any other expression
................................................................................
 92749  93634     assert( pToken );
 92750  93635     pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
 92751  93636     if( pNew==0 ){
 92752  93637       sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
 92753  93638       return 0;
 92754  93639     }
 92755  93640     pNew->x.pList = pList;
        93641  +  ExprSetProperty(pNew, EP_HasFunc);
 92756  93642     assert( !ExprHasProperty(pNew, EP_xIsSelect) );
 92757  93643     sqlite3ExprSetHeightAndFlags(pParse, pNew);
 92758  93644     return pNew;
 92759  93645   }
 92760  93646   
 92761  93647   /*
 92762  93648   ** Assign a variable number to an expression that encodes a wildcard
................................................................................
 93258  94144       pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
 93259  94145       pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
 93260  94146       pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
 93261  94147       pNew->op = p->op;
 93262  94148       pNew->pNext = pNext;
 93263  94149       pNew->pPrior = 0;
 93264  94150       pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
 93265         -    pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
 93266  94151       pNew->iLimit = 0;
 93267  94152       pNew->iOffset = 0;
 93268  94153       pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
 93269  94154       pNew->addrOpenEphm[0] = -1;
 93270  94155       pNew->addrOpenEphm[1] = -1;
 93271  94156       pNew->nSelectRow = p->nSelectRow;
 93272  94157       pNew->pWith = withDup(db, p->pWith);
................................................................................
 93452  94337   ** pList might be NULL following an OOM error.  But pSpan should never be
 93453  94338   ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
 93454  94339   ** is set.
 93455  94340   */
 93456  94341   SQLITE_PRIVATE void sqlite3ExprListSetSpan(
 93457  94342     Parse *pParse,          /* Parsing context */
 93458  94343     ExprList *pList,        /* List to which to add the span. */
 93459         -  ExprSpan *pSpan         /* The span to be added */
        94344  +  const char *zStart,     /* Start of the span */
        94345  +  const char *zEnd        /* End of the span */
 93460  94346   ){
 93461  94347     sqlite3 *db = pParse->db;
 93462  94348     assert( pList!=0 || db->mallocFailed!=0 );
 93463  94349     if( pList ){
 93464  94350       struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
 93465  94351       assert( pList->nExpr>0 );
 93466         -    assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
 93467  94352       sqlite3DbFree(db, pItem->zSpan);
 93468         -    pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
 93469         -                                    (int)(pSpan->zEnd - pSpan->zStart));
        94353  +    pItem->zSpan = sqlite3DbSpanDup(db, zStart, zEnd);
 93470  94354     }
 93471  94355   }
 93472  94356   
 93473  94357   /*
 93474  94358   ** If the expression list pEList contains more than iLimit elements,
 93475  94359   ** leave an error message in pParse.
 93476  94360   */
................................................................................
 93895  94779     if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
 93896  94780       testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
 93897  94781       testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
 93898  94782       return 0; /* No DISTINCT keyword and no aggregate functions */
 93899  94783     }
 93900  94784     assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
 93901  94785     if( p->pLimit ) return 0;              /* Has no LIMIT clause */
 93902         -  assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
 93903  94786     if( p->pWhere ) return 0;              /* Has no WHERE clause */
 93904  94787     pSrc = p->pSrc;
 93905  94788     assert( pSrc!=0 );
 93906  94789     if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
 93907  94790     if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
 93908  94791     pTab = pSrc->a[0].pTab;
 93909  94792     assert( pTab!=0 );
................................................................................
 93985  94868   **     SELECT <column1>, <column2>... FROM <table>
 93986  94869   **
 93987  94870   ** If the RHS of the IN operator is a list or a more complex subquery, then
 93988  94871   ** an ephemeral table might need to be generated from the RHS and then
 93989  94872   ** pX->iTable made to point to the ephemeral table instead of an
 93990  94873   ** existing table.
 93991  94874   **
 93992         -** The inFlags parameter must contain exactly one of the bits
 93993         -** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP.  If inFlags contains
 93994         -** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
 93995         -** fast membership test.  When the IN_INDEX_LOOP bit is set, the
 93996         -** IN index will be used to loop over all values of the RHS of the
 93997         -** IN operator.
        94875  +** The inFlags parameter must contain, at a minimum, one of the bits
        94876  +** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both.  If inFlags contains
        94877  +** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
        94878  +** membership test.  When the IN_INDEX_LOOP bit is set, the IN index will
        94879  +** be used to loop over all values of the RHS of the IN operator.
 93998  94880   **
 93999  94881   ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
 94000  94882   ** through the set members) then the b-tree must not contain duplicates.
 94001         -** An epheremal table must be used unless the selected columns are guaranteed
        94883  +** An epheremal table will be created unless the selected columns are guaranteed
 94002  94884   ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
 94003  94885   ** a UNIQUE constraint or index.
 94004  94886   **
 94005  94887   ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 
 94006  94888   ** for fast set membership tests) then an epheremal table must 
 94007  94889   ** be used unless <columns> is a single INTEGER PRIMARY KEY column or an 
 94008  94890   ** index can be found with the specified <columns> as its left-most.
................................................................................
 94535  95417         **
 94536  95418         ** In both cases, the query is augmented with "LIMIT 1".  Any 
 94537  95419         ** preexisting limit is discarded in place of the new LIMIT 1.
 94538  95420         */
 94539  95421         Select *pSel;                         /* SELECT statement to encode */
 94540  95422         SelectDest dest;                      /* How to deal with SELECT result */
 94541  95423         int nReg;                             /* Registers to allocate */
        95424  +      Expr *pLimit;                         /* New limit expression */
 94542  95425   
 94543  95426         testcase( pExpr->op==TK_EXISTS );
 94544  95427         testcase( pExpr->op==TK_SELECT );
 94545  95428         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
 94546  95429         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 94547  95430   
 94548  95431         pSel = pExpr->x.pSelect;
................................................................................
 94556  95439           sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
 94557  95440           VdbeComment((v, "Init subquery result"));
 94558  95441         }else{
 94559  95442           dest.eDest = SRT_Exists;
 94560  95443           sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
 94561  95444           VdbeComment((v, "Init EXISTS result"));
 94562  95445         }
 94563         -      sqlite3ExprDelete(pParse->db, pSel->pLimit);
 94564         -      pSel->pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,
 94565         -                                  &sqlite3IntTokens[1], 0);
        95446  +      pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[1], 0);
        95447  +      if( pSel->pLimit ){
        95448  +        sqlite3ExprDelete(pParse->db, pSel->pLimit->pLeft);
        95449  +        pSel->pLimit->pLeft = pLimit;
        95450  +      }else{
        95451  +        pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
        95452  +      }
 94566  95453         pSel->iLimit = 0;
 94567         -      pSel->selFlags &= ~SF_MultiValue;
 94568  95454         if( sqlite3Select(pParse, pSel, &dest) ){
 94569  95455           return 0;
 94570  95456         }
 94571  95457         rReg = dest.iSDParm;
 94572  95458         ExprSetVVAProperty(pExpr, EP_NoReduce);
 94573  95459         break;
 94574  95460       }
................................................................................
 95666  96552           pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
 95667  96553         }
 95668  96554   #endif
 95669  96555         if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
 95670  96556           if( !pColl ) pColl = db->pDfltColl; 
 95671  96557           sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
 95672  96558         }
 95673         -      sqlite3VdbeAddOp4(v, pParse->iSelfTab ? OP_PureFunc0 : OP_Function0,
 95674         -                        constMask, r1, target, (char*)pDef, P4_FUNCDEF);
 95675         -      sqlite3VdbeChangeP5(v, (u8)nFarg);
        96559  +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
        96560  +      if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){
        96561  +        Expr *pArg = pFarg->a[0].pExpr;
        96562  +        if( pArg->op==TK_COLUMN ){
        96563  +          sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
        96564  +        }else{
        96565  +          sqlite3VdbeAddOp2(v, OP_Null, 0, target);
        96566  +        }
        96567  +      }else
        96568  +#endif
        96569  +      {
        96570  +        sqlite3VdbeAddOp4(v, pParse->iSelfTab ? OP_PureFunc0 : OP_Function0,
        96571  +                          constMask, r1, target, (char*)pDef, P4_FUNCDEF);
        96572  +        sqlite3VdbeChangeP5(v, (u8)nFarg);
        96573  +      }
 95676  96574         if( nFarg && constMask==0 ){
 95677  96575           sqlite3ReleaseTempRange(pParse, r1, nFarg);
 95678  96576         }
 95679  96577         return target;
 95680  96578       }
 95681  96579   #ifndef SQLITE_OMIT_SUBQUERY
 95682  96580       case TK_EXISTS:
................................................................................
 98162  99060         aCreateTbl[i] = 0;
 98163  99061         sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
 98164  99062         if( zWhere ){
 98165  99063           sqlite3NestedParse(pParse,
 98166  99064              "DELETE FROM %Q.%s WHERE %s=%Q",
 98167  99065              pDb->zDbSName, zTab, zWhereType, zWhere
 98168  99066           );
        99067  +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
        99068  +      }else if( db->xPreUpdateCallback ){
        99069  +        sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
        99070  +#endif
 98169  99071         }else{
 98170  99072           /* The sqlite_stat[134] table already exists.  Delete all rows. */
 98171  99073           sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
 98172  99074         }
 98173  99075       }
 98174  99076     }
 98175  99077   
................................................................................
 98926  99828     int regRowid = iMem++;       /* Rowid argument passed to stat_push() */
 98927  99829   #endif
 98928  99830     int regTemp = iMem++;        /* Temporary use register */
 98929  99831     int regTabname = iMem++;     /* Register containing table name */
 98930  99832     int regIdxname = iMem++;     /* Register containing index name */
 98931  99833     int regStat1 = iMem++;       /* Value for the stat column of sqlite_stat1 */
 98932  99834     int regPrev = iMem;          /* MUST BE LAST (see below) */
        99835  +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
        99836  +  Table *pStat1 = 0; 
        99837  +#endif
 98933  99838   
 98934  99839     pParse->nMem = MAX(pParse->nMem, iMem);
 98935  99840     v = sqlite3GetVdbe(pParse);
 98936  99841     if( v==0 || NEVER(pTab==0) ){
 98937  99842       return;
 98938  99843     }
 98939  99844     if( pTab->tnum==0 ){
................................................................................
 98950  99855     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 98951  99856   #ifndef SQLITE_OMIT_AUTHORIZATION
 98952  99857     if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
 98953  99858         db->aDb[iDb].zDbSName ) ){
 98954  99859       return;
 98955  99860     }
 98956  99861   #endif
        99862  +
        99863  +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
        99864  +  if( db->xPreUpdateCallback ){
        99865  +    pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
        99866  +    if( pStat1==0 ) return;
        99867  +    pStat1->zName = (char*)&pStat1[1];
        99868  +    memcpy(pStat1->zName, "sqlite_stat1", 13);
        99869  +    pStat1->nCol = 3;
        99870  +    pStat1->iPKey = -1;
        99871  +    sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNBLOB);
        99872  +  }
        99873  +#endif
 98957  99874   
 98958  99875     /* Establish a read-lock on the table at the shared-cache level. 
 98959  99876     ** Open a read-only cursor on the table. Also allocate a cursor number
 98960  99877     ** to use for scanning indexes (iIdxCur). No index cursor is opened at
 98961  99878     ** this time though.  */
 98962  99879     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 98963  99880     iTabCur = iTab++;
................................................................................
 99152 100069   
 99153 100070       /* Add the entry to the stat1 table. */
 99154 100071       callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
 99155 100072       assert( "BBB"[0]==SQLITE_AFF_TEXT );
 99156 100073       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
 99157 100074       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
 99158 100075       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
       100076  +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
       100077  +    sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
       100078  +#endif
 99159 100079       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 99160 100080   
 99161 100081       /* Add the entries to the stat3 or stat4 table. */
 99162 100082   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 99163 100083       {
 99164 100084         int regEq = regStat1;
 99165 100085         int regLt = regStat1+1;
................................................................................
 99215 100135       jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
 99216 100136       sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
 99217 100137       assert( "BBB"[0]==SQLITE_AFF_TEXT );
 99218 100138       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
 99219 100139       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
 99220 100140       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
 99221 100141       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
       100142  +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
       100143  +    sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
       100144  +#endif
 99222 100145       sqlite3VdbeJumpHere(v, jZeroRows);
 99223 100146     }
 99224 100147   }
 99225 100148   
 99226 100149   
 99227 100150   /*
 99228 100151   ** Generate code that will cause the most recent index analysis to
................................................................................
100346 101269       }
100347 101270       if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
100348 101271         return 1;
100349 101272       }
100350 101273       if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
100351 101274         return 1;
100352 101275       }
100353         -    if( sqlite3FixExpr(pFix, pSelect->pOffset) ){
100354         -      return 1;
100355         -    }
100356 101276       pSelect = pSelect->pPrior;
100357 101277     }
100358 101278     return 0;
100359 101279   }
100360 101280   SQLITE_PRIVATE int sqlite3FixExpr(
100361 101281     DbFixer *pFix,     /* Context of the fixation */
100362 101282     Expr *pExpr        /* The expression to be fixed to one database */
................................................................................
101915 102835   **
101916 102836   ** Default value expressions must be constant.  Raise an exception if this
101917 102837   ** is not the case.
101918 102838   **
101919 102839   ** This routine is called by the parser while in the middle of
101920 102840   ** parsing a CREATE TABLE statement.
101921 102841   */
101922         -SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
       102842  +SQLITE_PRIVATE void sqlite3AddDefaultValue(
       102843  +  Parse *pParse,           /* Parsing context */
       102844  +  Expr *pExpr,             /* The parsed expression of the default value */
       102845  +  const char *zStart,      /* Start of the default value text */
       102846  +  const char *zEnd         /* First character past end of defaut value text */
       102847  +){
101923 102848     Table *p;
101924 102849     Column *pCol;
101925 102850     sqlite3 *db = pParse->db;
101926 102851     p = pParse->pNewTable;
101927 102852     if( p!=0 ){
101928 102853       pCol = &(p->aCol[p->nCol-1]);
101929         -    if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){
       102854  +    if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
101930 102855         sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
101931 102856             pCol->zName);
101932 102857       }else{
101933 102858         /* A copy of pExpr is used instead of the original, as pExpr contains
101934         -      ** tokens that point to volatile memory. The 'span' of the expression
101935         -      ** is required by pragma table_info.
       102859  +      ** tokens that point to volatile memory.	
101936 102860         */
101937 102861         Expr x;
101938 102862         sqlite3ExprDelete(db, pCol->pDflt);
101939 102863         memset(&x, 0, sizeof(x));
101940 102864         x.op = TK_SPAN;
101941         -      x.u.zToken = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
101942         -                                    (int)(pSpan->zEnd - pSpan->zStart));
101943         -      x.pLeft = pSpan->pExpr;
       102865  +      x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
       102866  +      x.pLeft = pExpr;
101944 102867         x.flags = EP_Skip;
101945 102868         pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
101946 102869         sqlite3DbFree(db, x.u.zToken);
101947 102870       }
101948 102871     }
101949         -  sqlite3ExprDelete(db, pSpan->pExpr);
       102872  +  sqlite3ExprDelete(db, pExpr);
101950 102873   }
101951 102874   
101952 102875   /*
101953 102876   ** Backwards Compatibility Hack:
101954 102877   ** 
101955 102878   ** Historical versions of SQLite accepted strings as column names in
101956 102879   ** indexes and PRIMARY KEY constraints and in UNIQUE constraints.  Example:
................................................................................
102659 103582         assert(pParse->nTab==1);
102660 103583         sqlite3MayAbort(pParse);
102661 103584         sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
102662 103585         sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
102663 103586         pParse->nTab = 2;
102664 103587         addrTop = sqlite3VdbeCurrentAddr(v) + 1;
102665 103588         sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
102666         -      sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
102667         -      sqlite3Select(pParse, pSelect, &dest);
102668         -      sqlite3VdbeEndCoroutine(v, regYield);
102669         -      sqlite3VdbeJumpHere(v, addrTop - 1);
102670 103589         if( pParse->nErr ) return;
102671 103590         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
102672 103591         if( pSelTab==0 ) return;
102673 103592         assert( p->aCol==0 );
102674 103593         p->nCol = pSelTab->nCol;
102675 103594         p->aCol = pSelTab->aCol;
102676 103595         pSelTab->nCol = 0;
102677 103596         pSelTab->aCol = 0;
102678 103597         sqlite3DeleteTable(db, pSelTab);
       103598  +      sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
       103599  +      sqlite3Select(pParse, pSelect, &dest);
       103600  +      if( pParse->nErr ) return;
       103601  +      sqlite3VdbeEndCoroutine(v, regYield);
       103602  +      sqlite3VdbeJumpHere(v, addrTop - 1);
102679 103603         addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
102680 103604         VdbeCoverage(v);
102681 103605         sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
102682 103606         sqlite3TableAffinity(v, p, 0);
102683 103607         sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
102684 103608         sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
102685 103609         sqlite3VdbeGoto(v, addrInsLoop);
................................................................................
102814 103738     p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
102815 103739     if( db->mallocFailed ) goto create_view_fail;
102816 103740   
102817 103741     /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
102818 103742     ** the end.
102819 103743     */
102820 103744     sEnd = pParse->sLastToken;
102821         -  assert( sEnd.z[0]!=0 );
       103745  +  assert( sEnd.z[0]!=0 || sEnd.n==0 );
102822 103746     if( sEnd.z[0]!=';' ){
102823 103747       sEnd.z += sEnd.n;
102824 103748     }
102825 103749     sEnd.n = 0;
102826 103750     n = (int)(sEnd.z - pBegin->z);
102827 103751     assert( n>0 );
102828 103752     z = pBegin->z;
................................................................................
104546 105470     if( !p && (pOn || pUsing) ){
104547 105471       sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
104548 105472         (pOn ? "ON" : "USING")
104549 105473       );
104550 105474       goto append_from_error;
104551 105475     }
104552 105476     p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
104553         -  if( p==0 || NEVER(p->nSrc==0) ){
       105477  +  if( p==0 ){
104554 105478       goto append_from_error;
104555 105479     }
       105480  +  assert( p->nSrc>0 );
104556 105481     pItem = &p->a[p->nSrc-1];
104557 105482     assert( pAlias!=0 );
104558 105483     if( pAlias->n ){
104559 105484       pItem->zAlias = sqlite3NameFromToken(db, pAlias);
104560 105485     }
104561 105486     pItem->pSelect = pSubquery;
104562 105487     pItem->pOn = pOn;
................................................................................
105057 105982       for(i=0; i<nCol; i++){
105058 105983         const char *zColl = pIdx->azColl[i];
105059 105984         pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
105060 105985                           sqlite3LocateCollSeq(pParse, zColl);
105061 105986         pKey->aSortOrder[i] = pIdx->aSortOrder[i];
105062 105987       }
105063 105988       if( pParse->nErr ){
       105989  +      assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
       105990  +      if( pIdx->bNoQuery==0 ){
       105991  +        /* Deactivate the index because it contains an unknown collating
       105992  +        ** sequence.  The only way to reactive the index is to reload the
       105993  +        ** schema.  Adding the missing collating sequence later does not
       105994  +        ** reactive the index.  The application had the chance to register
       105995  +        ** the missing index using the collation-needed callback.  For
       105996  +        ** simplicity, SQLite will not give the application a second chance.
       105997  +        */
       105998  +        pIdx->bNoQuery = 1;
       105999  +        pParse->rc = SQLITE_ERROR_RETRY;
       106000  +      }
105064 106001         sqlite3KeyInfoUnref(pKey);
105065 106002         pKey = 0;
105066 106003       }
105067 106004     }
105068 106005     return pKey;
105069 106006   }
105070 106007   
................................................................................
105242 106179     }
105243 106180     if( p && !p->xCmp && synthCollSeq(db, p) ){
105244 106181       p = 0;
105245 106182     }
105246 106183     assert( !p || p->xCmp );
105247 106184     if( p==0 ){
105248 106185       sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
       106186  +    pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
105249 106187     }
105250 106188     return p;
105251 106189   }
105252 106190   
105253 106191   /*
105254 106192   ** This routine is called on a collation sequence before it is used to
105255 106193   ** check that it is defined. An undefined collation sequence exists when
................................................................................
105715 106653   ** pWhere argument is an optional WHERE clause that restricts the
105716 106654   ** set of rows in the view that are to be added to the ephemeral table.
105717 106655   */
105718 106656   SQLITE_PRIVATE void sqlite3MaterializeView(
105719 106657     Parse *pParse,       /* Parsing context */
105720 106658     Table *pView,        /* View definition */
105721 106659     Expr *pWhere,        /* Optional WHERE clause to be added */
       106660  +  ExprList *pOrderBy,  /* Optional ORDER BY clause */
       106661  +  Expr *pLimit,        /* Optional LIMIT clause */
105722 106662     int iCur             /* Cursor number for ephemeral table */
105723 106663   ){
105724 106664     SelectDest dest;
105725 106665     Select *pSel;
105726 106666     SrcList *pFrom;
105727 106667     sqlite3 *db = pParse->db;
105728 106668     int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
................................................................................
105731 106671     if( pFrom ){
105732 106672       assert( pFrom->nSrc==1 );
105733 106673       pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
105734 106674       pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
105735 106675       assert( pFrom->a[0].pOn==0 );
105736 106676       assert( pFrom->a[0].pUsing==0 );
105737 106677     }
105738         -  pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 
105739         -                          SF_IncludeHidden, 0, 0);
       106678  +  pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy, 
       106679  +                          SF_IncludeHidden, pLimit);
105740 106680     sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
105741 106681     sqlite3Select(pParse, pSel, &dest);
105742 106682     sqlite3SelectDelete(db, pSel);
105743 106683   }
105744 106684   #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
105745 106685   
105746 106686   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
................................................................................
105754 106694   */
105755 106695   SQLITE_PRIVATE Expr *sqlite3LimitWhere(
105756 106696     Parse *pParse,               /* The parser context */
105757 106697     SrcList *pSrc,               /* the FROM clause -- which tables to scan */
105758 106698     Expr *pWhere,                /* The WHERE clause.  May be null */
105759 106699     ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
105760 106700     Expr *pLimit,                /* The LIMIT clause.  May be null */
105761         -  Expr *pOffset,               /* The OFFSET clause.  May be null */
105762 106701     char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
105763 106702   ){
105764         -  Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
       106703  +  sqlite3 *db = pParse->db;
       106704  +  Expr *pLhs = NULL;           /* LHS of IN(SELECT...) operator */
105765 106705     Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
105766         -  Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
105767 106706     ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
105768 106707     SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
105769 106708     Select *pSelect = NULL;      /* Complete SELECT tree */
       106709  +  Table *pTab;
105770 106710   
105771 106711     /* Check that there isn't an ORDER BY without a LIMIT clause.
105772 106712     */
105773         -  if( pOrderBy && (pLimit == 0) ) {
       106713  +  if( pOrderBy && pLimit==0 ) {
105774 106714       sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
105775         -    goto limit_where_cleanup;
       106715  +    sqlite3ExprDelete(pParse->db, pWhere);
       106716  +    sqlite3ExprListDelete(pParse->db, pOrderBy);
       106717  +    return 0;
105776 106718     }
105777 106719   
105778 106720     /* We only need to generate a select expression if there
105779 106721     ** is a limit/offset term to enforce.
105780 106722     */
105781 106723     if( pLimit == 0 ) {
105782         -    /* if pLimit is null, pOffset will always be null as well. */
105783         -    assert( pOffset == 0 );
105784 106724       return pWhere;
105785 106725     }
105786 106726   
105787 106727     /* Generate a select expression tree to enforce the limit/offset 
105788 106728     ** term for the DELETE or UPDATE statement.  For example:
105789 106729     **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
105790 106730     ** becomes:
105791 106731     **   DELETE FROM table_a WHERE rowid IN ( 
105792 106732     **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
105793 106733     **   );
105794 106734     */
105795 106735   
105796         -  pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0);
105797         -  if( pSelectRowid == 0 ) goto limit_where_cleanup;
105798         -  pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
105799         -  if( pEList == 0 ) goto limit_where_cleanup;
       106736  +  pTab = pSrc->a[0].pTab;
       106737  +  if( HasRowid(pTab) ){
       106738  +    pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0);
       106739  +    pEList = sqlite3ExprListAppend(
       106740  +        pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0)
       106741  +    );
       106742  +  }else{
       106743  +    Index *pPk = sqlite3PrimaryKeyIndex(pTab);
       106744  +    if( pPk->nKeyCol==1 ){
       106745  +      const char *zName = pTab->aCol[pPk->aiColumn[0]].zName;
       106746  +      pLhs = sqlite3Expr(db, TK_ID, zName);
       106747  +      pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName));
       106748  +    }else{
       106749  +      int i;
       106750  +      for(i=0; i<pPk->nKeyCol; i++){
       106751  +        Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zName);
       106752  +        pEList = sqlite3ExprListAppend(pParse, pEList, p);
       106753  +      }
       106754  +      pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
       106755  +      if( pLhs ){
       106756  +        pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
       106757  +      }
       106758  +    }
       106759  +  }
105800 106760   
105801 106761     /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
105802 106762     ** and the SELECT subtree. */
       106763  +  pSrc->a[0].pTab = 0;
105803 106764     pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
105804         -  if( pSelectSrc == 0 ) {
105805         -    sqlite3ExprListDelete(pParse->db, pEList);
105806         -    goto limit_where_cleanup;
105807         -  }
       106765  +  pSrc->a[0].pTab = pTab;
       106766  +  pSrc->a[0].pIBIndex = 0;
105808 106767   
105809 106768     /* generate the SELECT expression tree. */
105810         -  pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
105811         -                             pOrderBy,0,pLimit,pOffset);
105812         -  if( pSelect == 0 ) return 0;
       106769  +  pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0, 
       106770  +      pOrderBy,0,pLimit
       106771  +  );
105813 106772   
105814 106773     /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
105815         -  pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0);
105816         -  pInClause = pWhereRowid ? sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0) : 0;
       106774  +  pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
105817 106775     sqlite3PExprAddSelect(pParse, pInClause, pSelect);
105818 106776     return pInClause;
105819         -
105820         -limit_where_cleanup:
105821         -  sqlite3ExprDelete(pParse->db, pWhere);
105822         -  sqlite3ExprListDelete(pParse->db, pOrderBy);
105823         -  sqlite3ExprDelete(pParse->db, pLimit);
105824         -  sqlite3ExprDelete(pParse->db, pOffset);
105825         -  return 0;
105826 106777   }
105827 106778   #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
105828 106779          /*      && !defined(SQLITE_OMIT_SUBQUERY) */
105829 106780   
105830 106781   /*
105831 106782   ** Generate code for a DELETE FROM statement.
105832 106783   **
................................................................................
105833 106784   **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
105834 106785   **                 \________/       \________________/
105835 106786   **                  pTabList              pWhere
105836 106787   */
105837 106788   SQLITE_PRIVATE void sqlite3DeleteFrom(
105838 106789     Parse *pParse,         /* The parser context */
105839 106790     SrcList *pTabList,     /* The table from which we should delete things */
105840         -  Expr *pWhere           /* The WHERE clause.  May be null */
       106791  +  Expr *pWhere,          /* The WHERE clause.  May be null */
       106792  +  ExprList *pOrderBy,    /* ORDER BY clause. May be null */
       106793  +  Expr *pLimit           /* LIMIT clause. May be null */
105841 106794   ){
105842 106795     Vdbe *v;               /* The virtual database engine */
105843 106796     Table *pTab;           /* The table from which records will be deleted */
105844 106797     int i;                 /* Loop counter */
105845 106798     WhereInfo *pWInfo;     /* Information about the WHERE clause */
105846 106799     Index *pIdx;           /* For looping over indices of the table */
105847 106800     int iTabCur;           /* Cursor number for the table */
................................................................................
105877 106830   
105878 106831     memset(&sContext, 0, sizeof(sContext));
105879 106832     db = pParse->db;
105880 106833     if( pParse->nErr || db->mallocFailed ){
105881 106834       goto delete_from_cleanup;
105882 106835     }
105883 106836     assert( pTabList->nSrc==1 );
       106837  +
105884 106838   
105885 106839     /* Locate the table which we want to delete.  This table has to be
105886 106840     ** put in an SrcList structure because some of the subroutines we
105887 106841     ** will be calling are designed to work with multiple tables and expect
105888 106842     ** an SrcList* parameter instead of just a Table* parameter.
105889 106843     */
105890 106844     pTab = sqlite3SrcListLookup(pParse, pTabList);
................................................................................
105892 106846   
105893 106847     /* Figure out if we have any triggers and if the table being
105894 106848     ** deleted from is a view
105895 106849     */
105896 106850   #ifndef SQLITE_OMIT_TRIGGER
105897 106851     pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
105898 106852     isView = pTab->pSelect!=0;
105899         -  bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
105900 106853   #else
105901 106854   # define pTrigger 0
105902 106855   # define isView 0
105903 106856   #endif
       106857  +  bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
105904 106858   #ifdef SQLITE_OMIT_VIEW
105905 106859   # undef isView
105906 106860   # define isView 0
105907 106861   #endif
       106862  +
       106863  +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
       106864  +  if( !isView ){
       106865  +    pWhere = sqlite3LimitWhere(
       106866  +        pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
       106867  +    );
       106868  +    pOrderBy = 0;
       106869  +    pLimit = 0;
       106870  +  }
       106871  +#endif
105908 106872   
105909 106873     /* If pTab is really a view, make sure it has been initialized.
105910 106874     */
105911 106875     if( sqlite3ViewGetColumnNames(pParse, pTab) ){
105912 106876       goto delete_from_cleanup;
105913 106877     }
105914 106878   
................................................................................
105949 106913     sqlite3BeginWriteOperation(pParse, 1, iDb);
105950 106914   
105951 106915     /* If we are trying to delete from a view, realize that view into
105952 106916     ** an ephemeral table.
105953 106917     */
105954 106918   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
105955 106919     if( isView ){
105956         -    sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur);
       106920  +    sqlite3MaterializeView(pParse, pTab, 
       106921  +        pWhere, pOrderBy, pLimit, iTabCur
       106922  +    );
105957 106923       iDataCur = iIdxCur = iTabCur;
       106924  +    pOrderBy = 0;
       106925  +    pLimit = 0;
105958 106926     }
105959 106927   #endif
105960 106928   
105961 106929     /* Resolve the column names in the WHERE clause.
105962 106930     */
105963 106931     memset(&sNC, 0, sizeof(sNC));
105964 106932     sNC.pParse = pParse;
................................................................................
106194 107162       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
106195 107163     }
106196 107164   
106197 107165   delete_from_cleanup:
106198 107166     sqlite3AuthContextPop(&sContext);
106199 107167     sqlite3SrcListDelete(db, pTabList);
106200 107168     sqlite3ExprDelete(db, pWhere);
       107169  +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) 
       107170  +  sqlite3ExprListDelete(db, pOrderBy);
       107171  +  sqlite3ExprDelete(db, pLimit);
       107172  +#endif
106201 107173     sqlite3DbFree(db, aToOpen);
106202 107174     return;
106203 107175   }
106204 107176   /* Make sure "isView" and other macros defined above are undefined. Otherwise
106205 107177   ** they may interfere with compilation of other functions in this file
106206 107178   ** (or in another file, if this file becomes part of the amalgamation).  */
106207 107179   #ifdef isView
................................................................................
106351 107323     ** the update-hook is not invoked for rows removed by REPLACE, but the 
106352 107324     ** pre-update-hook is.
106353 107325     */ 
106354 107326     if( pTab->pSelect==0 ){
106355 107327       u8 p5 = 0;
106356 107328       sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
106357 107329       sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
106358         -    if( pParse->nested==0 ){
       107330  +    if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
106359 107331         sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
106360 107332       }
106361 107333       if( eMode!=ONEPASS_OFF ){
106362 107334         sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
106363 107335       }
106364 107336       if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
106365 107337         sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
................................................................................
107235 108207         ** that point.
107236 108208         **
107237 108209         ** For a case-insensitive search, set variable cx to be the same as
107238 108210         ** c but in the other case and search the input string for either
107239 108211         ** c or cx.
107240 108212         */
107241 108213         if( c<=0x80 ){
107242         -        u32 cx;
       108214  +        char zStop[3];
107243 108215           int bMatch;
107244 108216           if( noCase ){
107245         -          cx = sqlite3Toupper(c);
107246         -          c = sqlite3Tolower(c);
       108217  +          zStop[0] = sqlite3Toupper(c);
       108218  +          zStop[1] = sqlite3Tolower(c);
       108219  +          zStop[2] = 0;
107247 108220           }else{
107248         -          cx = c;
       108221  +          zStop[0] = c;
       108222  +          zStop[1] = 0;
107249 108223           }
107250         -        while( (c2 = *(zString++))!=0 ){
107251         -          if( c2!=c && c2!=cx ) continue;
       108224  +        while(1){
       108225  +          zString += strcspn((const char*)zString, zStop);
       108226  +          if( zString[0]==0 ) break;
       108227  +          zString++;
107252 108228             bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
107253 108229             if( bMatch!=SQLITE_NOMATCH ) return bMatch;
107254 108230           }
107255 108231         }else{
107256 108232           int bMatch;
107257 108233           while( (c2 = Utf8Read(zString))!=0 ){
107258 108234             if( c2!=c ) continue;
................................................................................
108332 109308   #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
108333 109309       FUNCTION2(unlikely,          1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
108334 109310       FUNCTION2(likelihood,        2, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
108335 109311       FUNCTION2(likely,            1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
108336 109312   #ifdef SQLITE_DEBUG
108337 109313       FUNCTION2(affinity,          1, 0, 0, noopFunc,  SQLITE_FUNC_AFFINITY),
108338 109314   #endif
       109315  +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
       109316  +    FUNCTION2(sqlite_offset,     1, 0, 0, noopFunc,  SQLITE_FUNC_OFFSET|
       109317  +                                                     SQLITE_FUNC_TYPEOF),
       109318  +#endif
108339 109319       FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
108340 109320       FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
108341 109321       FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
108342 109322       FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
108343 109323       FUNCTION(trim,               1, 3, 0, trimFunc         ),
108344 109324       FUNCTION(trim,               2, 3, 0, trimFunc         ),
108345 109325       FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
................................................................................
109154 110134         }
109155 110135         if( !p ) return;
109156 110136         iSkip = sqlite3VdbeMakeLabel(v);
109157 110137         sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
109158 110138       }
109159 110139   
109160 110140       pParse->disableTriggers = 1;
109161         -    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
       110141  +    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
109162 110142       pParse->disableTriggers = 0;
109163 110143   
109164 110144       /* If the DELETE has generated immediate foreign key constraint 
109165 110145       ** violations, halt the VDBE and return an error at this point, before
109166 110146       ** any modifications to the schema are made. This is because statement
109167 110147       ** transactions are not able to rollback schema changes.  
109168 110148       **
................................................................................
109712 110692         if( pRaise ){
109713 110693           pRaise->affinity = OE_Abort;
109714 110694         }
109715 110695         pSelect = sqlite3SelectNew(pParse, 
109716 110696             sqlite3ExprListAppend(pParse, 0, pRaise),
109717 110697             sqlite3SrcListAppend(db, 0, &tFrom, 0),
109718 110698             pWhere,
109719         -          0, 0, 0, 0, 0, 0
       110699  +          0, 0, 0, 0, 0
109720 110700         );
109721 110701         pWhere = 0;
109722 110702       }
109723 110703   
109724 110704       /* Disable lookaside memory allocation */
109725 110705       db->lookaside.bDisable++;
109726 110706   
................................................................................
110762 111742           sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
110763 111743         }else if( pSelect ){
110764 111744           sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
110765 111745         }else{
110766 111746           VdbeOp *pOp;
110767 111747           sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
110768 111748           pOp = sqlite3VdbeGetOp(v, -1);
110769         -        if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
       111749  +        assert( pOp!=0 );
       111750  +        if( pOp->opcode==OP_Null && !IsVirtual(pTab) ){
110770 111751             appendFlag = 1;
110771 111752             pOp->opcode = OP_NewRowid;
110772 111753             pOp->p1 = iDataCur;
110773 111754             pOp->p2 = regRowid;
110774 111755             pOp->p3 = regAutoinc;
110775 111756           }
110776 111757         }
................................................................................
111423 112404            (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
111424 112405       ){
111425 112406         sqlite3VdbeResolveLabel(v, addrUniqueOk);
111426 112407         continue;
111427 112408       }
111428 112409   
111429 112410       /* Check to see if the new index entry will be unique */
       112411  +    sqlite3ExprCachePush(pParse);
111430 112412       sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
111431 112413                            regIdx, pIdx->nKeyCol); VdbeCoverage(v);
111432 112414   
111433 112415       /* Generate code to handle collisions */
111434 112416       regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
111435 112417       if( isUpdate || onError==OE_Replace ){
111436 112418         if( HasRowid(pTab) ){
................................................................................
111511 112493               regR, nPkField, 0, OE_Replace,
111512 112494               (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
111513 112495           seenReplace = 1;
111514 112496           break;
111515 112497         }
111516 112498       }
111517 112499       sqlite3VdbeResolveLabel(v, addrUniqueOk);
       112500  +    sqlite3ExprCachePop(pParse);
111518 112501       if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
111519 112502     }
111520 112503     if( ipkTop ){
111521 112504       sqlite3VdbeGoto(v, ipkTop+1);
111522 112505       sqlite3VdbeJumpHere(v, ipkBottom);
111523 112506     }
111524 112507     
................................................................................
111859 112842     ** there is no ORDER BY, we will get an error. */
111860 112843     if( pSelect->pGroupBy ){
111861 112844       return 0;   /* SELECT may not have a GROUP BY clause */
111862 112845     }
111863 112846     if( pSelect->pLimit ){
111864 112847       return 0;   /* SELECT may not have a LIMIT clause */
111865 112848     }
111866         -  assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
111867 112849     if( pSelect->pPrior ){
111868 112850       return 0;   /* SELECT may not be a compound query */
111869 112851     }
111870 112852     if( pSelect->selFlags & SF_Distinct ){
111871 112853       return 0;   /* SELECT may not be DISTINCT */
111872 112854     }
111873 112855     pEList = pSelect->pEList;
................................................................................
112571 113553     int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
112572 113554                       sqlite3_stmt**,const char**);
112573 113555     int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
112574 113556                         sqlite3_stmt**,const void**);
112575 113557     int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
112576 113558     void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
112577 113559     void *(*value_pointer)(sqlite3_value*,const char*);
       113560  +  int (*vtab_nochange)(sqlite3_context*);
       113561  +  int (*value_nochange)(sqlite3_value*);
       113562  +  const char *(*vtab_collation)(sqlite3_index_info*,int);
112578 113563   };
112579 113564   
112580 113565   /*
112581 113566   ** This is the function signature used for all extension entry points.  It
112582 113567   ** is also defined in the file "loadext.c".
112583 113568   */
112584 113569   typedef int (*sqlite3_loadext_entry)(
................................................................................
112837 113822   #define sqlite3_set_last_insert_rowid  sqlite3_api->set_last_insert_rowid
112838 113823   /* Version 3.20.0 and later */
112839 113824   #define sqlite3_prepare_v3             sqlite3_api->prepare_v3
112840 113825   #define sqlite3_prepare16_v3           sqlite3_api->prepare16_v3
112841 113826   #define sqlite3_bind_pointer           sqlite3_api->bind_pointer
112842 113827   #define sqlite3_result_pointer         sqlite3_api->result_pointer
112843 113828   #define sqlite3_value_pointer          sqlite3_api->value_pointer
       113829  +/* Version 3.22.0 and later */
       113830  +#define sqlite3_vtab_nochange          sqlite3_api->vtab_nochange
       113831  +#define sqlite3_value_nochange         sqltie3_api->value_nochange
       113832  +#define sqlite3_vtab_collation         sqltie3_api->vtab_collation
112844 113833   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
112845 113834   
112846 113835   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
112847 113836     /* This case when the file really is being compiled as a loadable 
112848 113837     ** extension */
112849 113838   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
112850 113839   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
113271 114260     /* Version 3.18.0 and later */
113272 114261     sqlite3_set_last_insert_rowid,
113273 114262     /* Version 3.20.0 and later */
113274 114263     sqlite3_prepare_v3,
113275 114264     sqlite3_prepare16_v3,
113276 114265     sqlite3_bind_pointer,
113277 114266     sqlite3_result_pointer,
113278         -  sqlite3_value_pointer
       114267  +  sqlite3_value_pointer,
       114268  +  /* Version 3.22.0 and later */
       114269  +  sqlite3_vtab_nochange,
       114270  +  sqlite3_value_nochange,
       114271  +  sqlite3_vtab_collation
113279 114272   };
113280 114273   
113281 114274   /*
113282 114275   ** Attempt to load an SQLite extension library contained in the file
113283 114276   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
113284 114277   ** default entry point name (sqlite3_extension_init) is used.  Use
113285 114278   ** of the default name is recommended.
................................................................................
113337 114330   
113338 114331     zEntry = zProc ? zProc : "sqlite3_extension_init";
113339 114332   
113340 114333     handle = sqlite3OsDlOpen(pVfs, zFile);
113341 114334   #if SQLITE_OS_UNIX || SQLITE_OS_WIN
113342 114335     for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
113343 114336       char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
       114337  +    int bExists = 0;
113344 114338       if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
113345         -    handle = sqlite3OsDlOpen(pVfs, zAltFile);
       114339  +    sqlite3OsAccess(pVfs, zAltFile, SQLITE_ACCESS_EXISTS, &bExists);
       114340  +    if( bExists )  handle = sqlite3OsDlOpen(pVfs, zAltFile);
113346 114341       sqlite3_free(zAltFile);
113347 114342     }
113348 114343   #endif
113349 114344     if( handle==0 ){
113350 114345       if( pzErrMsg ){
113351 114346         *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
113352 114347         if( zErrmsg ){
................................................................................
115374 116369     ** the returned data set are:
115375 116370     **
115376 116371     ** cid:        Column id (numbered from left to right, starting at 0)
115377 116372     ** name:       Column name
115378 116373     ** type:       Column declaration type.
115379 116374     ** notnull:    True if 'NOT NULL' is part of column declaration
115380 116375     ** dflt_value: The default value for the column, if any.
       116376  +  ** pk:         Non-zero for PK fields.
115381 116377     */
115382 116378     case PragTyp_TABLE_INFO: if( zRight ){
115383 116379       Table *pTab;
115384 116380       pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
115385 116381       if( pTab ){
115386 116382         int i, k;
115387 116383         int nHidden = 0;
................................................................................
117257 118253     ** We return -1000000 instead of the more usual -1 simply because using
117258 118254     ** -1000000 as the incorrect index into db->aDb[] is much 
117259 118255     ** more likely to cause a segfault than -1 (of course there are assert()
117260 118256     ** statements too, but it never hurts to play the odds).
117261 118257     */
117262 118258     assert( sqlite3_mutex_held(db->mutex) );
117263 118259     if( pSchema ){
117264         -    for(i=0; ALWAYS(i<db->nDb); i++){
       118260  +    for(i=0; 1; i++){
       118261  +      assert( i<db->nDb );
117265 118262         if( db->aDb[i].pSchema==pSchema ){
117266 118263           break;
117267 118264         }
117268 118265       }
117269 118266       assert( i>=0 && i<db->nDb );
117270 118267     }
117271 118268     return i;
................................................................................
117438 118435       sParse.pTriggerPrg = pT->pNext;
117439 118436       sqlite3DbFree(db, pT);
117440 118437     }
117441 118438   
117442 118439   end_prepare:
117443 118440   
117444 118441     sqlite3ParserReset(&sParse);
117445         -  rc = sqlite3ApiExit(db, rc);
117446         -  assert( (rc&db->errMask)==rc );
117447 118442     return rc;
117448 118443   }
117449 118444   static int sqlite3LockAndPrepare(
117450 118445     sqlite3 *db,              /* Database handle. */
117451 118446     const char *zSql,         /* UTF-8 encoded SQL statement. */
117452 118447     int nBytes,               /* Length of zSql in bytes. */
117453 118448     u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
117454 118449     Vdbe *pOld,               /* VM being reprepared */
117455 118450     sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
117456 118451     const char **pzTail       /* OUT: End of parsed string */
117457 118452   ){
117458 118453     int rc;
       118454  +  int cnt = 0;
117459 118455   
117460 118456   #ifdef SQLITE_ENABLE_API_ARMOR
117461 118457     if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
117462 118458   #endif
117463 118459     *ppStmt = 0;
117464 118460     if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
117465 118461       return SQLITE_MISUSE_BKPT;
117466 118462     }
117467 118463     sqlite3_mutex_enter(db->mutex);
117468 118464     sqlite3BtreeEnterAll(db);
117469         -  rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
117470         -  if( rc==SQLITE_SCHEMA ){
117471         -    sqlite3ResetOneSchema(db, -1);
117472         -    sqlite3_finalize(*ppStmt);
       118465  +  do{
       118466  +    /* Make multiple attempts to compile the SQL, until it either succeeds
       118467  +    ** or encounters a permanent error.  A schema problem after one schema
       118468  +    ** reset is considered a permanent error. */
117473 118469       rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
117474         -  }
       118470  +    assert( rc==SQLITE_OK || *ppStmt==0 );
       118471  +  }while( rc==SQLITE_ERROR_RETRY
       118472  +       || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
117475 118473     sqlite3BtreeLeaveAll(db);
       118474  +  rc = sqlite3ApiExit(db, rc);
       118475  +  assert( (rc&db->errMask)==rc );
117476 118476     sqlite3_mutex_leave(db->mutex);
117477         -  assert( rc==SQLITE_OK || *ppStmt==0 );
117478 118477     return rc;
117479 118478   }
117480 118479   
117481 118480   /*
117482 118481   ** Rerun the compilation of a statement after a schema change.
117483 118482   **
117484 118483   ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
................................................................................
117757 118756       sqlite3ExprListDelete(db, p->pEList);
117758 118757       sqlite3SrcListDelete(db, p->pSrc);
117759 118758       sqlite3ExprDelete(db, p->pWhere);
117760 118759       sqlite3ExprListDelete(db, p->pGroupBy);
117761 118760       sqlite3ExprDelete(db, p->pHaving);
117762 118761       sqlite3ExprListDelete(db, p->pOrderBy);
117763 118762       sqlite3ExprDelete(db, p->pLimit);
117764         -    sqlite3ExprDelete(db, p->pOffset);
117765 118763       if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
117766 118764       if( bFree ) sqlite3DbFreeNN(db, p);
117767 118765       p = pPrior;
117768 118766       bFree = 1;
117769 118767     }
117770 118768   }
117771 118769   
................................................................................
117790 118788     ExprList *pEList,     /* which columns to include in the result */
117791 118789     SrcList *pSrc,        /* the FROM clause -- which tables to scan */
117792 118790     Expr *pWhere,         /* the WHERE clause */
117793 118791     ExprList *pGroupBy,   /* the GROUP BY clause */
117794 118792     Expr *pHaving,        /* the HAVING clause */
117795 118793     ExprList *pOrderBy,   /* the ORDER BY clause */
117796 118794     u32 selFlags,         /* Flag parameters, such as SF_Distinct */
117797         -  Expr *pLimit,         /* LIMIT value.  NULL means not used */
117798         -  Expr *pOffset         /* OFFSET value.  NULL means no offset */
       118795  +  Expr *pLimit          /* LIMIT value.  NULL means not used */
117799 118796   ){
117800 118797     Select *pNew;
117801 118798     Select standin;
117802 118799     pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
117803 118800     if( pNew==0 ){
117804 118801       assert( pParse->db->mallocFailed );
117805 118802       pNew = &standin;
................................................................................
117824 118821     pNew->pWhere = pWhere;
117825 118822     pNew->pGroupBy = pGroupBy;
117826 118823     pNew->pHaving = pHaving;
117827 118824     pNew->pOrderBy = pOrderBy;
117828 118825     pNew->pPrior = 0;
117829 118826     pNew->pNext = 0;
117830 118827     pNew->pLimit = pLimit;
117831         -  pNew->pOffset = pOffset;
117832 118828     pNew->pWith = 0;
117833         -  assert( pOffset==0 || pLimit!=0 || pParse->nErr>0
117834         -                     || pParse->db->mallocFailed!=0 );
117835 118829     if( pParse->db->mallocFailed ) {
117836 118830       clearSelect(pParse->db, pNew, pNew!=&standin);
117837 118831       pNew = 0;
117838 118832     }else{
117839 118833       assert( pNew->pSrc!=0 || pParse->nErr>0 );
117840 118834     }
117841 118835     assert( pNew!=&standin );
................................................................................
119069 120063     char const *zOrigDb = 0;
119070 120064     char const *zOrigTab = 0;
119071 120065     char const *zOrigCol = 0;
119072 120066   #endif
119073 120067   
119074 120068     assert( pExpr!=0 );
119075 120069     assert( pNC->pSrcList!=0 );
       120070  +  assert( pExpr->op!=TK_AGG_COLUMN );  /* This routine runes before aggregates
       120071  +                                       ** are processed */
119076 120072     switch( pExpr->op ){
119077         -    case TK_AGG_COLUMN:
119078 120073       case TK_COLUMN: {
119079 120074         /* The expression is a column. Locate the table the column is being
119080 120075         ** extracted from in NameContext.pSrcList. This table may be real
119081 120076         ** database table or a subquery.
119082 120077         */
119083 120078         Table *pTab = 0;            /* Table structure column is extracted from */
119084 120079         Select *pS = 0;             /* Select the column is extracted from */
119085 120080         int iCol = pExpr->iColumn;  /* Index of column in pTab */
119086         -      testcase( pExpr->op==TK_AGG_COLUMN );
119087         -      testcase( pExpr->op==TK_COLUMN );
119088 120081         while( pNC && !pTab ){
119089 120082           SrcList *pTabList = pNC->pSrcList;
119090 120083           for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
119091 120084           if( j<pTabList->nSrc ){
119092 120085             pTab = pTabList->a[j].pTab;
119093 120086             pS = pTabList->a[j].pSelect;
119094 120087           }else{
................................................................................
119284 120277       return;
119285 120278     }
119286 120279   #endif
119287 120280   
119288 120281     if( pParse->colNamesSet || db->mallocFailed ) return;
119289 120282     /* Column names are determined by the left-most term of a compound select */
119290 120283     while( pSelect->pPrior ) pSelect = pSelect->pPrior;
       120284  +  SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
119291 120285     pTabList = pSelect->pSrc;
119292 120286     pEList = pSelect->pEList;
119293 120287     assert( v!=0 );
119294 120288     assert( pTabList!=0 );
119295 120289     pParse->colNamesSet = 1;
119296 120290     fullName = (db->flags & SQLITE_FullColNames)!=0;
119297 120291     srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
................................................................................
119392 120386         /* If the column contains an "AS <name>" phrase, use <name> as the name */
119393 120387       }else{
119394 120388         Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
119395 120389         while( pColExpr->op==TK_DOT ){
119396 120390           pColExpr = pColExpr->pRight;
119397 120391           assert( pColExpr!=0 );
119398 120392         }
119399         -      if( (pColExpr->op==TK_COLUMN || pColExpr->op==TK_AGG_COLUMN)
119400         -       && pColExpr->pTab!=0 
119401         -      ){
       120393  +      assert( pColExpr->op!=TK_AGG_COLUMN );
       120394  +      if( pColExpr->op==TK_COLUMN ){
119402 120395           /* For columns use the column name name */
119403 120396           int iCol = pColExpr->iColumn;
119404 120397           Table *pTab = pColExpr->pTab;
       120398  +        assert( pTab!=0 );
119405 120399           if( iCol<0 ) iCol = pTab->iPKey;
119406 120400           zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
119407 120401         }else if( pColExpr->op==TK_ID ){
119408 120402           assert( !ExprHasProperty(pColExpr, EP_IntValue) );
119409 120403           zName = pColExpr->u.zToken;
119410 120404         }else{
119411 120405           /* Use the original text of the column expression as its name */
................................................................................
119557 120551     }
119558 120552     return sqlite3VdbeCreate(pParse);
119559 120553   }
119560 120554   
119561 120555   
119562 120556   /*
119563 120557   ** Compute the iLimit and iOffset fields of the SELECT based on the
119564         -** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
       120558  +** pLimit expressions.  pLimit->pLeft and pLimit->pRight hold the expressions
119565 120559   ** that appear in the original SQL statement after the LIMIT and OFFSET
119566 120560   ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
119567 120561   ** are the integer memory register numbers for counters used to compute 
119568 120562   ** the limit and offset.  If there is no limit and/or offset, then 
119569 120563   ** iLimit and iOffset are negative.
119570 120564   **
119571 120565   ** This routine changes the values of iLimit and iOffset only if
119572         -** a limit or offset is defined by pLimit and pOffset.  iLimit and
119573         -** iOffset should have been preset to appropriate default values (zero)
       120566  +** a limit or offset is defined by pLimit->pLeft and pLimit->pRight.  iLimit
       120567  +** and iOffset should have been preset to appropriate default values (zero)
119574 120568   ** prior to calling this routine.
119575 120569   **
119576 120570   ** The iOffset register (if it exists) is initialized to the value
119577 120571   ** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
119578 120572   ** iOffset+1 is initialized to LIMIT+OFFSET.
119579 120573   **
119580         -** Only if pLimit!=0 or pOffset!=0 do the limit registers get
       120574  +** Only if pLimit->pLeft!=0 do the limit registers get
119581 120575   ** redefined.  The UNION ALL operator uses this property to force
119582 120576   ** the reuse of the same limit and offset registers across multiple
119583 120577   ** SELECT statements.
119584 120578   */
119585 120579   static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
119586 120580     Vdbe *v = 0;
119587 120581     int iLimit = 0;
119588 120582     int iOffset;
119589 120583     int n;
       120584  +  Expr *pLimit = p->pLimit;
       120585  +
119590 120586     if( p->iLimit ) return;
119591 120587   
119592 120588     /* 
119593 120589     ** "LIMIT -1" always shows all rows.  There is some
119594 120590     ** controversy about what the correct behavior should be.
119595 120591     ** The current implementation interprets "LIMIT 0" to mean
119596 120592     ** no rows.
119597 120593     */
119598 120594     sqlite3ExprCacheClear(pParse);
119599         -  assert( p->pOffset==0 || p->pLimit!=0 );
119600         -  if( p->pLimit ){
       120595  +  if( pLimit ){
       120596  +    assert( pLimit->op==TK_LIMIT );
       120597  +    assert( pLimit->pLeft!=0 );
119601 120598       p->iLimit = iLimit = ++pParse->nMem;
119602 120599       v = sqlite3GetVdbe(pParse);
119603 120600       assert( v!=0 );
119604         -    if( sqlite3ExprIsInteger(p->pLimit, &n) ){
       120601  +    if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
119605 120602         sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
119606 120603         VdbeComment((v, "LIMIT counter"));
119607 120604         if( n==0 ){
119608 120605           sqlite3VdbeGoto(v, iBreak);
119609 120606         }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
119610 120607           p->nSelectRow = sqlite3LogEst((u64)n);
119611 120608           p->selFlags |= SF_FixedLimit;
119612 120609         }
119613 120610       }else{
119614         -      sqlite3ExprCode(pParse, p->pLimit, iLimit);
       120611  +      sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
119615 120612         sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
119616 120613         VdbeComment((v, "LIMIT counter"));
119617 120614         sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
119618 120615       }
119619         -    if( p->pOffset ){
       120616  +    if( pLimit->pRight ){
119620 120617         p->iOffset = iOffset = ++pParse->nMem;
119621 120618         pParse->nMem++;   /* Allocate an extra register for limit+offset */
119622         -      sqlite3ExprCode(pParse, p->pOffset, iOffset);
       120619  +      sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
119623 120620         sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
119624 120621         VdbeComment((v, "OFFSET counter"));
119625 120622         sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
119626 120623         VdbeComment((v, "LIMIT+OFFSET"));
119627 120624       }
119628 120625     }
119629 120626   }
................................................................................
119745 120742     int iQueue;                   /* The Queue table */
119746 120743     int iDistinct = 0;            /* To ensure unique results if UNION */
119747 120744     int eDest = SRT_Fifo;         /* How to write to Queue */
119748 120745     SelectDest destQueue;         /* SelectDest targetting the Queue table */
119749 120746     int i;                        /* Loop counter */
119750 120747     int rc;                       /* Result code */
119751 120748     ExprList *pOrderBy;           /* The ORDER BY clause */
119752         -  Expr *pLimit, *pOffset;       /* Saved LIMIT and OFFSET */
       120749  +  Expr *pLimit;                 /* Saved LIMIT and OFFSET */
119753 120750     int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
119754 120751   
119755 120752     /* Obtain authorization to do a recursive query */
119756 120753     if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
119757 120754   
119758 120755     /* Process the LIMIT and OFFSET clauses, if they exist */
119759 120756     addrBreak = sqlite3VdbeMakeLabel(v);
119760 120757     p->nSelectRow = 320;  /* 4 billion rows */
119761 120758     computeLimitRegisters(pParse, p, addrBreak);
119762 120759     pLimit = p->pLimit;
119763         -  pOffset = p->pOffset;
119764 120760     regLimit = p->iLimit;
119765 120761     regOffset = p->iOffset;
119766         -  p->pLimit = p->pOffset = 0;
       120762  +  p->pLimit = 0;
119767 120763     p->iLimit = p->iOffset = 0;
119768 120764     pOrderBy = p->pOrderBy;
119769 120765   
119770 120766     /* Locate the cursor number of the Current table */
119771 120767     for(i=0; ALWAYS(i<pSrc->nSrc); i++){
119772 120768       if( pSrc->a[i].fg.isRecursive ){
119773 120769         iCurrent = pSrc->a[i].iCursor;
................................................................................
119852 120848     sqlite3VdbeGoto(v, addrTop);
119853 120849     sqlite3VdbeResolveLabel(v, addrBreak);
119854 120850   
119855 120851   end_of_recursive_query:
119856 120852     sqlite3ExprListDelete(pParse->db, p->pOrderBy);
119857 120853     p->pOrderBy = pOrderBy;
119858 120854     p->pLimit = pLimit;
119859         -  p->pOffset = pOffset;
119860 120855     return;
119861 120856   }
119862 120857   #endif /* SQLITE_OMIT_CTE */
119863 120858   
119864 120859   /* Forward references */
119865 120860   static int multiSelectOrderBy(
119866 120861     Parse *pParse,        /* Parsing context */
................................................................................
119871 120866   /*
119872 120867   ** Handle the special case of a compound-select that originates from a
119873 120868   ** VALUES clause.  By handling this as a special case, we avoid deep
119874 120869   ** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
119875 120870   ** on a VALUES clause.
119876 120871   **
119877 120872   ** Because the Select object originates from a VALUES clause:
119878         -**   (1) It has no LIMIT or OFFSET
       120873  +**   (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
119879 120874   **   (2) All terms are UNION ALL
119880 120875   **   (3) There is no ORDER BY clause
       120876  +**
       120877  +** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
       120878  +** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
       120879  +** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
       120880  +** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
119881 120881   */
119882 120882   static int multiSelectValues(
119883 120883     Parse *pParse,        /* Parsing context */
119884 120884     Select *p,            /* The right-most of SELECTs to be coded */
119885 120885     SelectDest *pDest     /* What to do with query results */
119886 120886   ){
119887 120887     Select *pPrior;
       120888  +  Select *pRightmost = p;
119888 120889     int nRow = 1;
119889 120890     int rc = 0;
119890 120891     assert( p->selFlags & SF_MultiValue );
119891 120892     do{
119892 120893       assert( p->selFlags & SF_Values );
119893 120894       assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
119894         -    assert( p->pLimit==0 );
119895         -    assert( p->pOffset==0 );
119896 120895       assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
119897 120896       if( p->pPrior==0 ) break;
119898 120897       assert( p->pPrior->pNext==p );
119899 120898       p = p->pPrior;
119900 120899       nRow++;
119901 120900     }while(1);
119902 120901     while( p ){
119903 120902       pPrior = p->pPrior;
119904 120903       p->pPrior = 0;
119905 120904       rc = sqlite3Select(pParse, p, pDest);
119906 120905       p->pPrior = pPrior;
119907         -    if( rc ) break;
       120906  +    if( rc || pRightmost->pLimit ) break;
119908 120907       p->nSelectRow = nRow;
119909 120908       p = p->pNext;
119910 120909     }
119911 120910     return rc;
119912 120911   }
119913 120912   
119914 120913   /*
................................................................................
120015 121014       case TK_ALL: {
120016 121015         int addr = 0;
120017 121016         int nLimit;
120018 121017         assert( !pPrior->pLimit );
120019 121018         pPrior->iLimit = p->iLimit;
120020 121019         pPrior->iOffset = p->iOffset;
120021 121020         pPrior->pLimit = p->pLimit;
120022         -      pPrior->pOffset = p->pOffset;
120023 121021         explainSetInteger(iSub1, pParse->iNextSelectId);
120024 121022         rc = sqlite3Select(pParse, pPrior, &dest);
120025 121023         p->pLimit = 0;
120026         -      p->pOffset = 0;
120027 121024         if( rc ){
120028 121025           goto multi_select_end;
120029 121026         }
120030 121027         p->pPrior = 0;
120031 121028         p->iLimit = pPrior->iLimit;
120032 121029         p->iOffset = pPrior->iOffset;
120033 121030         if( p->iLimit ){
................................................................................
120041 121038         explainSetInteger(iSub2, pParse->iNextSelectId);
120042 121039         rc = sqlite3Select(pParse, p, &dest);
120043 121040         testcase( rc!=SQLITE_OK );
120044 121041         pDelete = p->pPrior;
120045 121042         p->pPrior = pPrior;
120046 121043         p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
120047 121044         if( pPrior->pLimit
120048         -       && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
       121045  +       && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
120049 121046          && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) 
120050 121047         ){
120051 121048           p->nSelectRow = sqlite3LogEst((u64)nLimit);
120052 121049         }
120053 121050         if( addr ){
120054 121051           sqlite3VdbeJumpHere(v, addr);
120055 121052         }
................................................................................
120056 121053         break;
120057 121054       }
120058 121055       case TK_EXCEPT:
120059 121056       case TK_UNION: {
120060 121057         int unionTab;    /* Cursor number of the temporary table holding result */
120061 121058         u8 op = 0;       /* One of the SRT_ operations to apply to self */
120062 121059         int priorOp;     /* The SRT_ operation to apply to prior selects */
120063         -      Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
       121060  +      Expr *pLimit;    /* Saved values of p->nLimit  */
120064 121061         int addr;
120065 121062         SelectDest uniondest;
120066 121063   
120067 121064         testcase( p->op==TK_EXCEPT );
120068 121065         testcase( p->op==TK_UNION );
120069 121066         priorOp = SRT_Union;
120070 121067         if( dest.eDest==priorOp ){
120071 121068           /* We can reuse a temporary table generated by a SELECT to our
120072 121069           ** right.
120073 121070           */
120074 121071           assert( p->pLimit==0 );      /* Not allowed on leftward elements */
120075         -        assert( p->pOffset==0 );     /* Not allowed on leftward elements */
120076 121072           unionTab = dest.iSDParm;
120077 121073         }else{
120078 121074           /* We will need to create our own temporary table to hold the
120079 121075           ** intermediate results.
120080 121076           */
120081 121077           unionTab = pParse->nTab++;
120082 121078           assert( p->pOrderBy==0 );
................................................................................
120104 121100         }else{
120105 121101           assert( p->op==TK_UNION );
120106 121102           op = SRT_Union;
120107 121103         }
120108 121104         p->pPrior = 0;
120109 121105         pLimit = p->pLimit;
120110 121106         p->pLimit = 0;
120111         -      pOffset = p->pOffset;
120112         -      p->pOffset = 0;
120113 121107         uniondest.eDest = op;
120114 121108         explainSetInteger(iSub2, pParse->iNextSelectId);
120115 121109         rc = sqlite3Select(pParse, p, &uniondest);
120116 121110         testcase( rc!=SQLITE_OK );
120117 121111         /* Query flattening in sqlite3Select() might refill p->pOrderBy.
120118 121112         ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
120119 121113         sqlite3ExprListDelete(db, p->pOrderBy);
................................................................................
120121 121115         p->pPrior = pPrior;
120122 121116         p->pOrderBy = 0;
120123 121117         if( p->op==TK_UNION ){
120124 121118           p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
120125 121119         }
120126 121120         sqlite3ExprDelete(db, p->pLimit);
120127 121121         p->pLimit = pLimit;
120128         -      p->pOffset = pOffset;
120129 121122         p->iLimit = 0;
120130 121123         p->iOffset = 0;
120131 121124   
120132 121125         /* Convert the data in the temporary table into whatever form
120133 121126         ** it is that we currently need.
120134 121127         */
120135 121128         assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
................................................................................
120149 121142           sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
120150 121143         }
120151 121144         break;
120152 121145       }
120153 121146       default: assert( p->op==TK_INTERSECT ); {
120154 121147         int tab1, tab2;
120155 121148         int iCont, iBreak, iStart;
120156         -      Expr *pLimit, *pOffset;
       121149  +      Expr *pLimit;
120157 121150         int addr;
120158 121151         SelectDest intersectdest;
120159 121152         int r1;
120160 121153   
120161 121154         /* INTERSECT is different from the others since it requires
120162 121155         ** two temporary tables.  Hence it has its own case.  Begin
120163 121156         ** by allocating the tables we will need.
................................................................................
120185 121178         */
120186 121179         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
120187 121180         assert( p->addrOpenEphm[1] == -1 );
120188 121181         p->addrOpenEphm[1] = addr;
120189 121182         p->pPrior = 0;
120190 121183         pLimit = p->pLimit;
120191 121184         p->pLimit = 0;
120192         -      pOffset = p->pOffset;
120193         -      p->pOffset = 0;
120194 121185         intersectdest.iSDParm = tab2;
120195 121186         explainSetInteger(iSub2, pParse->iNextSelectId);
120196 121187         rc = sqlite3Select(pParse, p, &intersectdest);
120197 121188         testcase( rc!=SQLITE_OK );
120198 121189         pDelete = p->pPrior;
120199 121190         p->pPrior = pPrior;
120200 121191         if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
120201 121192         sqlite3ExprDelete(db, p->pLimit);
120202 121193         p->pLimit = pLimit;
120203         -      p->pOffset = pOffset;
120204 121194   
120205 121195         /* Generate code to take the intersection of the two temporary
120206 121196         ** tables.
120207 121197         */
120208 121198         assert( p->pEList );
120209 121199         iBreak = sqlite3VdbeMakeLabel(v);
120210 121200         iCont = sqlite3VdbeMakeLabel(v);
................................................................................
120675 121665                                     regLimitA);
120676 121666       sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
120677 121667     }else{
120678 121668       regLimitA = regLimitB = 0;
120679 121669     }
120680 121670     sqlite3ExprDelete(db, p->pLimit);
120681 121671     p->pLimit = 0;
120682         -  sqlite3ExprDelete(db, p->pOffset);
120683         -  p->pOffset = 0;
120684 121672   
120685 121673     regAddrA = ++pParse->nMem;
120686 121674     regAddrB = ++pParse->nMem;
120687 121675     regOutA = ++pParse->nMem;
120688 121676     regOutB = ++pParse->nMem;
120689 121677     sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
120690 121678     sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
................................................................................
121066 122054   **  (18)  If the sub-query is a compound select, then all terms of the
121067 122055   **        ORDER BY clause of the parent must be simple references to 
121068 122056   **        columns of the sub-query.
121069 122057   **
121070 122058   **  (19)  If the subquery uses LIMIT then the outer query may not
121071 122059   **        have a WHERE clause.
121072 122060   **
121073         -**  (**)  Subsumed into (17d3).  Was: If the sub-query is a compound select,
121074         -**        then it must not use an ORDER BY clause - Ticket #3773.  Because
121075         -**        of (17d3), then only way to have a compound subquery is if it is
121076         -**        the only term in the FROM clause of the outer query.  But if the
121077         -**        only term in the FROM clause has an ORDER BY, then it will be
121078         -**        implemented as a co-routine and the flattener will never be called.
       122061  +**  (20)  If the sub-query is a compound select, then it must not use
       122062  +**        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
       122063  +**        somewhat by saying that the terms of the ORDER BY clause must
       122064  +**        appear as unmodified result columns in the outer query.  But we
       122065  +**        have other optimizations in mind to deal with that case.
121079 122066   **
121080 122067   **  (21)  If the subquery uses LIMIT then the outer query may not be
121081 122068   **        DISTINCT.  (See ticket [752e1646fc]).
121082 122069   **
121083 122070   **  (22)  The subquery may not be a recursive CTE.
121084 122071   **
121085 122072   **  (**)  Subsumed into restriction (17d3).  Was: If the outer query is
................................................................................
121141 122128     assert( pSubSrc );
121142 122129     /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
121143 122130     ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
121144 122131     ** because they could be computed at compile-time.  But when LIMIT and OFFSET
121145 122132     ** became arbitrary expressions, we were forced to add restrictions (13)
121146 122133     ** and (14). */
121147 122134     if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
121148         -  if( pSub->pOffset ) return 0;                          /* Restriction (14) */
       122135  +  if( pSub->pLimit && pSub->pLimit->pRight ) return 0;   /* Restriction (14) */
121149 122136     if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
121150 122137       return 0;                                            /* Restriction (15) */
121151 122138     }
121152 122139     if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
121153 122140     if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (4)  */
121154 122141     if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
121155 122142        return 0;         /* Restrictions (8)(9) */
................................................................................
121205 122192   
121206 122193     /* Restriction (17): If the sub-query is a compound SELECT, then it must
121207 122194     ** use only the UNION ALL operator. And none of the simple select queries
121208 122195     ** that make up the compound SELECT are allowed to be aggregate or distinct
121209 122196     ** queries.
121210 122197     */
121211 122198     if( pSub->pPrior ){
       122199  +    if( pSub->pOrderBy ){
       122200  +      return 0;  /* Restriction (20) */
       122201  +    }
121212 122202       if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
121213 122203         return 0; /* (17d1), (17d2), or (17d3) */
121214 122204       }
121215 122205       for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
121216 122206         testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
121217 122207         testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
121218 122208         assert( pSub->pSrc!=0 );
................................................................................
121239 122229     ** The only way that the recursive part of a CTE can contain a compound
121240 122230     ** subquery is for the subquery to be one term of a join.  But if the
121241 122231     ** subquery is a join, then the flattening has already been stopped by
121242 122232     ** restriction (17d3)
121243 122233     */
121244 122234     assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
121245 122235   
121246         -  /* Ex-restriction (20):
121247         -  ** A compound subquery must be the only term in the FROM clause of the
121248         -  ** outer query by restriction (17d3).  But if that term also has an
121249         -  ** ORDER BY clause, then the subquery will be implemented by co-routine
121250         -  ** and so the flattener will never be invoked.  Hence, it is not possible
121251         -  ** for the subquery to be a compound and have an ORDER BY clause.
121252         -  */
121253         -  assert( pSub->pPrior==0 || pSub->pOrderBy==0 );
121254         -
121255 122236     /***** If we reach this point, flattening is permitted. *****/
121256 122237     SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n",
121257 122238                      pSub->zSelName, pSub, iFrom));
121258 122239   
121259 122240     /* Authorize the subquery */
121260 122241     pParse->zAuthContext = pSubitem->zName;
121261 122242     TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
................................................................................
121295 122276     **
121296 122277     ** We call this the "compound-subquery flattening".
121297 122278     */
121298 122279     for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
121299 122280       Select *pNew;
121300 122281       ExprList *pOrderBy = p->pOrderBy;
121301 122282       Expr *pLimit = p->pLimit;
121302         -    Expr *pOffset = p->pOffset;
121303 122283       Select *pPrior = p->pPrior;
121304 122284       p->pOrderBy = 0;
121305 122285       p->pSrc = 0;
121306 122286       p->pPrior = 0;
121307 122287       p->pLimit = 0;
121308         -    p->pOffset = 0;
121309 122288       pNew = sqlite3SelectDup(db, p, 0);
121310 122289       sqlite3SelectSetName(pNew, pSub->zSelName);
121311         -    p->pOffset = pOffset;
121312 122290       p->pLimit = pLimit;
121313 122291       p->pOrderBy = pOrderBy;
121314 122292       p->pSrc = pSrc;
121315 122293       p->op = TK_ALL;
121316 122294       if( pNew==0 ){
121317 122295         p->pPrior = pPrior;
121318 122296       }else{
................................................................................
121602 122580       }
121603 122581     }
121604 122582     return nChng;
121605 122583   }
121606 122584   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
121607 122585   
121608 122586   /*
121609         -** Based on the contents of the AggInfo structure indicated by the first
121610         -** argument, this function checks if the following are true:
121611         -**
121612         -**    * the query contains just a single aggregate function,
121613         -**    * the aggregate function is either min() or max(), and
121614         -**    * the argument to the aggregate function is a column value.
121615         -**
121616         -** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
121617         -** is returned as appropriate. Also, *ppMinMax is set to point to the 
121618         -** list of arguments passed to the aggregate before returning.
121619         -**
121620         -** Or, if the conditions above are not met, *ppMinMax is set to 0 and
121621         -** WHERE_ORDERBY_NORMAL is returned.
121622         -*/
121623         -static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
121624         -  int eRet = WHERE_ORDERBY_NORMAL;          /* Return value */
121625         -
121626         -  *ppMinMax = 0;
121627         -  if( pAggInfo->nFunc==1 ){
121628         -    Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
121629         -    ExprList *pEList = pExpr->x.pList;      /* Arguments to agg function */
121630         -
121631         -    assert( pExpr->op==TK_AGG_FUNCTION );
121632         -    if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
121633         -      const char *zFunc = pExpr->u.zToken;
121634         -      if( sqlite3StrICmp(zFunc, "min")==0 ){
121635         -        eRet = WHERE_ORDERBY_MIN;
121636         -        *ppMinMax = pEList;
121637         -      }else if( sqlite3StrICmp(zFunc, "max")==0 ){
121638         -        eRet = WHERE_ORDERBY_MAX;
121639         -        *ppMinMax = pEList;
121640         -      }
121641         -    }
121642         -  }
121643         -
121644         -  assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
       122587  +** The pFunc is the only aggregate function in the query.  Check to see
       122588  +** if the query is a candidate for the min/max optimization. 
       122589  +**
       122590  +** If the query is a candidate for the min/max optimization, then set
       122591  +** *ppMinMax to be an ORDER BY clause to be used for the optimization
       122592  +** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
       122593  +** whether pFunc is a min() or max() function.
       122594  +**
       122595  +** If the query is not a candidate for the min/max optimization, return
       122596  +** WHERE_ORDERBY_NORMAL (which must be zero).
       122597  +**
       122598  +** This routine must be called after aggregate functions have been
       122599  +** located but before their arguments have been subjected to aggregate
       122600  +** analysis.
       122601  +*/
       122602  +static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
       122603  +  int eRet = WHERE_ORDERBY_NORMAL;      /* Return value */
       122604  +  ExprList *pEList = pFunc->x.pList;    /* Arguments to agg function */
       122605  +  const char *zFunc;                    /* Name of aggregate function pFunc */
       122606  +  ExprList *pOrderBy;
       122607  +  u8 sortOrder;
       122608  +
       122609  +  assert( *ppMinMax==0 );
       122610  +  assert( pFunc->op==TK_AGG_FUNCTION );
       122611  +  if( pEList==0 || pEList->nExpr!=1 ) return eRet;
       122612  +  zFunc = pFunc->u.zToken;
       122613  +  if( sqlite3StrICmp(zFunc, "min")==0 ){
       122614  +    eRet = WHERE_ORDERBY_MIN;
       122615  +    sortOrder = SQLITE_SO_ASC;
       122616  +  }else if( sqlite3StrICmp(zFunc, "max")==0 ){
       122617  +    eRet = WHERE_ORDERBY_MAX;
       122618  +    sortOrder = SQLITE_SO_DESC;
       122619  +  }else{
       122620  +    return eRet;
       122621  +  }
       122622  +  *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
       122623  +  assert( pOrderBy!=0 || db->mallocFailed );
       122624  +  if( pOrderBy ) pOrderBy->a[0].sortOrder = sortOrder;
121645 122625     return eRet;
121646 122626   }
121647 122627   
121648 122628   /*
121649 122629   ** The select statement passed as the first argument is an aggregate query.
121650 122630   ** The second argument is the associated aggregate-info object. This 
121651 122631   ** function tests if the SELECT is of the form:
................................................................................
121768 122748     p->pWith = 0;
121769 122749     p->selFlags &= ~SF_Compound;
121770 122750     assert( (p->selFlags & SF_Converted)==0 );
121771 122751     p->selFlags |= SF_Converted;
121772 122752     assert( pNew->pPrior!=0 );
121773 122753     pNew->pPrior->pNext = pNew;
121774 122754     pNew->pLimit = 0;
121775         -  pNew->pOffset = 0;
121776 122755     return WRC_Continue;
121777 122756   }
121778 122757   
121779 122758   /*
121780 122759   ** Check to see if the FROM clause term pFrom has table-valued function
121781 122760   ** arguments.  If it does, leave an error message in pParse and return
121782 122761   ** non-zero, since pFrom is not allowed to be a table-valued function.
................................................................................
122024 123003     int i, j, k;
122025 123004     SrcList *pTabList;
122026 123005     ExprList *pEList;
122027 123006     struct SrcList_item *pFrom;
122028 123007     sqlite3 *db = pParse->db;
122029 123008     Expr *pE, *pRight, *pExpr;
122030 123009     u16 selFlags = p->selFlags;
       123010  +  u32 elistFlags = 0;
122031 123011   
122032 123012     p->selFlags |= SF_Expanded;
122033 123013     if( db->mallocFailed  ){
122034 123014       return WRC_Abort;
122035 123015     }
122036         -  if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
       123016  +  assert( p->pSrc!=0 );
       123017  +  if( (selFlags & SF_Expanded)!=0 ){
122037 123018       return WRC_Prune;
122038 123019     }
122039 123020     pTabList = p->pSrc;
122040 123021     pEList = p->pEList;
122041 123022     if( OK_IF_ALWAYS_TRUE(p->pWith) ){
122042 123023       sqlite3WithPush(pParse, p->pWith, 0);
122043 123024     }
................................................................................
122136 123117     */
122137 123118     for(k=0; k<pEList->nExpr; k++){
122138 123119       pE = pEList->a[k].pExpr;
122139 123120       if( pE->op==TK_ASTERISK ) break;
122140 123121       assert( pE->op!=TK_DOT || pE->pRight!=0 );
122141 123122       assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
122142 123123       if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
       123124  +    elistFlags |= pE->flags;
122143 123125     }
122144 123126     if( k<pEList->nExpr ){
122145 123127       /*
122146 123128       ** If we get here it means the result set contains one or more "*"
122147 123129       ** operators that need to be expanded.  Loop through each expression
122148 123130       ** in the result set and expand them one by one.
122149 123131       */
................................................................................
122151 123133       ExprList *pNew = 0;
122152 123134       int flags = pParse->db->flags;
122153 123135       int longNames = (flags & SQLITE_FullColNames)!=0
122154 123136                         && (flags & SQLITE_ShortColNames)==0;
122155 123137   
122156 123138       for(k=0; k<pEList->nExpr; k++){
122157 123139         pE = a[k].pExpr;
       123140  +      elistFlags |= pE->flags;
122158 123141         pRight = pE->pRight;
122159 123142         assert( pE->op!=TK_DOT || pRight!=0 );
122160 123143         if( pE->op!=TK_ASTERISK
122161 123144          && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
122162 123145         ){
122163 123146           /* This particular expression does not need to be expanded.
122164 123147           */
................................................................................
122280 123263             }
122281 123264           }
122282 123265         }
122283 123266       }
122284 123267       sqlite3ExprListDelete(db, pEList);
122285 123268       p->pEList = pNew;
122286 123269     }
122287         -  if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
122288         -    sqlite3ErrorMsg(pParse, "too many columns in result set");
122289         -    return WRC_Abort;
       123270  +  if( p->pEList ){
       123271  +    if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
       123272  +      sqlite3ErrorMsg(pParse, "too many columns in result set");
       123273  +      return WRC_Abort;
       123274  +    }
       123275  +    if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
       123276  +      p->selFlags |= SF_ComplexResult;
       123277  +    }
122290 123278     }
122291 123279     return WRC_Continue;
122292 123280   }
122293 123281   
122294 123282   /*
122295 123283   ** No-op routine for the parse-tree walker.
122296 123284   **
................................................................................
122818 123806     Expr *pHaving;         /* The HAVING clause.  May be NULL */
122819 123807     int rc = 1;            /* Value to return from this function */
122820 123808     DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
122821 123809     SortCtx sSort;         /* Info on how to code the ORDER BY clause */
122822 123810     AggInfo sAggInfo;      /* Information used by aggregate queries */
122823 123811     int iEnd;              /* Address of the end of the query */
122824 123812     sqlite3 *db;           /* The database connection */
       123813  +  ExprList *pMinMaxOrderBy = 0;  /* Added ORDER BY for min/max queries */
       123814  +  u8 minMaxFlag;                 /* Flag for min/max queries */
122825 123815   
122826 123816   #ifndef SQLITE_OMIT_EXPLAIN
122827 123817     int iRestoreSelectId = pParse->iSelectId;
122828 123818     pParse->iSelectId = pParse->iNextSelectId++;
122829 123819   #endif
122830 123820   
122831 123821     db = pParse->db;
................................................................................
122904 123894       ** is not a join.  But if the outer query is not a join, then the subquery
122905 123895       ** will be implemented as a co-routine and there is no advantage to
122906 123896       ** flattening in that case.
122907 123897       */
122908 123898       if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
122909 123899       assert( pSub->pGroupBy==0 );
122910 123900   
122911         -    /* If the subquery contains an ORDER BY clause and if
       123901  +    /* If the outer query contains a "complex" result set (that is,
       123902  +    ** if the result set of the outer query uses functions or subqueries)
       123903  +    ** and if the subquery contains an ORDER BY clause and if
122912 123904       ** it will be implemented as a co-routine, then do not flatten.  This
122913 123905       ** restriction allows SQL constructs like this:
122914 123906       **
122915 123907       **  SELECT expensive_function(x)
122916 123908       **    FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
122917 123909       **
122918 123910       ** The expensive_function() is only computed on the 10 rows that
122919 123911       ** are output, rather than every row of the table.
       123912  +    **
       123913  +    ** The requirement that the outer query have a complex result set
       123914  +    ** means that flattening does occur on simpler SQL constraints without
       123915  +    ** the expensive_function() like:
       123916  +    **
       123917  +    **  SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
122920 123918       */
122921 123919       if( pSub->pOrderBy!=0
122922 123920        && i==0
       123921  +     && (p->selFlags & SF_ComplexResult)!=0
122923 123922        && (pTabList->nSrc==1
122924 123923            || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
122925 123924       ){
122926 123925         continue;
122927 123926       }
122928 123927   
122929 123928       if( flattenSubquery(pParse, p, i, isAgg) ){
................................................................................
123334 124333           assert( pWhere==p->pWhere );
123335 124334           havingToWhere(pParse, pGroupBy, pHaving, &p->pWhere);
123336 124335           pWhere = p->pWhere;
123337 124336         }
123338 124337         sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
123339 124338       }
123340 124339       sAggInfo.nAccumulator = sAggInfo.nColumn;
       124340  +    if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
       124341  +      minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
       124342  +    }else{
       124343  +      minMaxFlag = WHERE_ORDERBY_NORMAL;
       124344  +    }
123341 124345       for(i=0; i<sAggInfo.nFunc; i++){
123342 124346         assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
123343 124347         sNC.ncFlags |= NC_InAggFunc;
123344 124348         sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
123345 124349         sNC.ncFlags &= ~NC_InAggFunc;
123346 124350       }
123347 124351       sAggInfo.mxReg = pParse->nMem;
123348 124352       if( db->mallocFailed ) goto select_end;
       124353  +#if SELECTTRACE_ENABLED
       124354  +    if( sqlite3SelectTrace & 0x400 ){
       124355  +      int ii;
       124356  +      SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n"));
       124357  +      sqlite3TreeViewSelect(0, p, 0);
       124358  +      for(ii=0; ii<sAggInfo.nColumn; ii++){
       124359  +        sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
       124360  +            ii, sAggInfo.aCol[ii].iMem);
       124361  +        sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0);
       124362  +      }
       124363  +      for(ii=0; ii<sAggInfo.nFunc; ii++){
       124364  +        sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
       124365  +            ii, sAggInfo.aFunc[ii].iMem);
       124366  +        sqlite3TreeViewExpr(0, sAggInfo.aFunc[ii].pExpr, 0);
       124367  +      }
       124368  +    }
       124369  +#endif
       124370  +
123349 124371   
123350 124372       /* Processing for aggregates with GROUP BY is very different and
123351 124373       ** much more complex than aggregates without a GROUP BY.
123352 124374       */
123353 124375       if( pGroupBy ){
123354 124376         KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
123355 124377         int addr1;          /* A-vs-B comparision jump */
................................................................................
123571 124593         */
123572 124594         sqlite3VdbeResolveLabel(v, addrReset);
123573 124595         resetAccumulator(pParse, &sAggInfo);
123574 124596         sqlite3VdbeAddOp1(v, OP_Return, regReset);
123575 124597        
123576 124598       } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
123577 124599       else {
123578         -      ExprList *pDel = 0;
123579 124600   #ifndef SQLITE_OMIT_BTREECOUNT
123580 124601         Table *pTab;
123581 124602         if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
123582 124603           /* If isSimpleCount() returns a pointer to a Table structure, then
123583 124604           ** the SQL statement is of the form:
123584 124605           **
123585 124606           **   SELECT count(*) FROM <tbl>
................................................................................
123633 124654           }
123634 124655           sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
123635 124656           sqlite3VdbeAddOp1(v, OP_Close, iCsr);
123636 124657           explainSimpleCount(pParse, pTab, pBest);
123637 124658         }else
123638 124659   #endif /* SQLITE_OMIT_BTREECOUNT */
123639 124660         {
123640         -        /* Check if the query is of one of the following forms:
123641         -        **
123642         -        **   SELECT min(x) FROM ...
123643         -        **   SELECT max(x) FROM ...
123644         -        **
123645         -        ** If it is, then ask the code in where.c to attempt to sort results
123646         -        ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. 
123647         -        ** If where.c is able to produce results sorted in this order, then
123648         -        ** add vdbe code to break out of the processing loop after the 
123649         -        ** first iteration (since the first iteration of the loop is 
123650         -        ** guaranteed to operate on the row with the minimum or maximum 
123651         -        ** value of x, the only row required).
123652         -        **
123653         -        ** A special flag must be passed to sqlite3WhereBegin() to slightly
123654         -        ** modify behavior as follows:
123655         -        **
123656         -        **   + If the query is a "SELECT min(x)", then the loop coded by
123657         -        **     where.c should not iterate over any values with a NULL value
123658         -        **     for x.
123659         -        **
123660         -        **   + The optimizer code in where.c (the thing that decides which
123661         -        **     index or indices to use) should place a different priority on 
123662         -        **     satisfying the 'ORDER BY' clause than it does in other cases.
123663         -        **     Refer to code and comments in where.c for details.
123664         -        */
123665         -        ExprList *pMinMax = 0;
123666         -        u8 flag = WHERE_ORDERBY_NORMAL;
123667         -        
123668         -        assert( p->pGroupBy==0 );
123669         -        assert( flag==0 );
123670         -        if( p->pHaving==0 ){
123671         -          flag = minMaxQuery(&sAggInfo, &pMinMax);
123672         -        }
123673         -        assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
123674         -
123675         -        if( flag ){
123676         -          pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
123677         -          pDel = pMinMax;
123678         -          assert( db->mallocFailed || pMinMax!=0 );
123679         -          if( !db->mallocFailed ){
123680         -            pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
123681         -            pMinMax->a[0].pExpr->op = TK_COLUMN;
123682         -          }
123683         -        }
123684         -  
123685 124661           /* This case runs if the aggregate has no GROUP BY clause.  The
123686 124662           ** processing is much simpler since there is only a single row
123687 124663           ** of output.
123688 124664           */
       124665  +        assert( p->pGroupBy==0 );
123689 124666           resetAccumulator(pParse, &sAggInfo);
123690         -        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0);
       124667  +
       124668  +        /* If this query is a candidate for the min/max optimization, then
       124669  +        ** minMaxFlag will have been previously set to either
       124670  +        ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
       124671  +        ** be an appropriate ORDER BY expression for the optimization.
       124672  +        */
       124673  +        assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
       124674  +        assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
       124675  +
       124676  +        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
       124677  +                                   0, minMaxFlag, 0);
123691 124678           if( pWInfo==0 ){
123692         -          sqlite3ExprListDelete(db, pDel);
123693 124679             goto select_end;
123694 124680           }
123695 124681           updateAccumulator(pParse, &sAggInfo);
123696         -        assert( pMinMax==0 || pMinMax->nExpr==1 );
123697 124682           if( sqlite3WhereIsOrdered(pWInfo)>0 ){
123698 124683             sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
123699 124684             VdbeComment((v, "%s() by index",
123700         -                (flag==WHERE_ORDERBY_MIN?"min":"max")));
       124685  +                (minMaxFlag==WHERE_ORDERBY_MIN?"min":"max")));
123701 124686           }
123702 124687           sqlite3WhereEnd(pWInfo);
123703 124688           finalizeAggFunctions(pParse, &sAggInfo);
123704 124689         }
123705 124690   
123706 124691         sSort.pOrderBy = 0;
123707 124692         sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
123708 124693         selectInnerLoop(pParse, p, -1, 0, 0, 
123709 124694                         pDest, addrEnd, addrEnd);
123710         -      sqlite3ExprListDelete(db, pDel);
123711 124695       }
123712 124696       sqlite3VdbeResolveLabel(v, addrEnd);
123713 124697       
123714 124698     } /* endif aggregate query */
123715 124699   
123716 124700     if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
123717 124701       explainTempTable(pParse, "DISTINCT");
................................................................................
123735 124719     rc = (pParse->nErr>0);
123736 124720   
123737 124721     /* Control jumps to here if an error is encountered above, or upon
123738 124722     ** successful coding of the SELECT.
123739 124723     */
123740 124724   select_end:
123741 124725     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
123742         -
       124726  +  sqlite3ExprListDelete(db, pMinMaxOrderBy);
123743 124727     sqlite3DbFree(db, sAggInfo.aCol);
123744 124728     sqlite3DbFree(db, sAggInfo.aFunc);
123745 124729   #if SELECTTRACE_ENABLED
123746 124730     SELECTTRACE(1,pParse,p,("end processing\n"));
123747 124731     pParse->nSelectIndent--;
123748 124732   #endif
123749 124733     return rc;
................................................................................
123975 124959       TriggerStep * pTmp = pTriggerStep;
123976 124960       pTriggerStep = pTriggerStep->pNext;
123977 124961   
123978 124962       sqlite3ExprDelete(db, pTmp->pWhere);
123979 124963       sqlite3ExprListDelete(db, pTmp->pExprList);
123980 124964       sqlite3SelectDelete(db, pTmp->pSelect);
123981 124965       sqlite3IdListDelete(db, pTmp->pIdList);
       124966  +    sqlite3DbFree(db, pTmp->zSpan);
123982 124967   
123983 124968       sqlite3DbFree(db, pTmp);
123984 124969     }
123985 124970   }
123986 124971   
123987 124972   /*
123988 124973   ** Given table pTab, return a list of all the triggers attached to 
................................................................................
124289 125274   
124290 125275   triggerfinish_cleanup:
124291 125276     sqlite3DeleteTrigger(db, pTrig);
124292 125277     assert( !pParse->pNewTrigger );
124293 125278     sqlite3DeleteTriggerStep(db, pStepList);
124294 125279   }
124295 125280   
       125281  +/*
       125282  +** Duplicate a range of text from an SQL statement, then convert all
       125283  +** whitespace characters into ordinary space characters.
       125284  +*/
       125285  +static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
       125286  +  char *z = sqlite3DbSpanDup(db, zStart, zEnd);
       125287  +  int i;
       125288  +  if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
       125289  +  return z;
       125290  +}    
       125291  +
124296 125292   /*
124297 125293   ** Turn a SELECT statement (that the pSelect parameter points to) into
124298 125294   ** a trigger step.  Return a pointer to a TriggerStep structure.
124299 125295   **
124300 125296   ** The parser calls this routine when it finds a SELECT statement in
124301 125297   ** body of a TRIGGER.  
124302 125298   */
124303         -SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
       125299  +SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(
       125300  +  sqlite3 *db,                /* Database connection */
       125301  +  Select *pSelect,            /* The SELECT statement */
       125302  +  const char *zStart,         /* Start of SQL text */
       125303  +  const char *zEnd            /* End of SQL text */
       125304  +){
124304 125305     TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
124305 125306     if( pTriggerStep==0 ) {
124306 125307       sqlite3SelectDelete(db, pSelect);
124307 125308       return 0;
124308 125309     }
124309 125310     pTriggerStep->op = TK_SELECT;
124310 125311     pTriggerStep->pSelect = pSelect;
124311 125312     pTriggerStep->orconf = OE_Default;
       125313  +  pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
124312 125314     return pTriggerStep;
124313 125315   }
124314 125316   
124315 125317   /*
124316 125318   ** Allocate space to hold a new trigger step.  The allocated space
124317 125319   ** holds both the TriggerStep object and the TriggerStep.target.z string.
124318 125320   **
124319 125321   ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
124320 125322   */
124321 125323   static TriggerStep *triggerStepAllocate(
124322 125324     sqlite3 *db,                /* Database connection */
124323 125325     u8 op,                      /* Trigger opcode */
124324         -  Token *pName                /* The target name */
       125326  +  Token *pName,               /* The target name */
       125327  +  const char *zStart,         /* Start of SQL text */
       125328  +  const char *zEnd            /* End of SQL text */
124325 125329   ){
124326 125330     TriggerStep *pTriggerStep;
124327 125331   
124328 125332     pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
124329 125333     if( pTriggerStep ){
124330 125334       char *z = (char*)&pTriggerStep[1];
124331 125335       memcpy(z, pName->z, pName->n);
124332 125336       sqlite3Dequote(z);
124333 125337       pTriggerStep->zTarget = z;
124334 125338       pTriggerStep->op = op;
       125339  +    pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
124335 125340     }
124336 125341     return pTriggerStep;
124337 125342   }
124338 125343   
124339 125344   /*
124340 125345   ** Build a trigger step out of an INSERT statement.  Return a pointer
124341 125346   ** to the new trigger step.
................................................................................
124344 125349   ** body of a trigger.
124345 125350   */
124346 125351   SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
124347 125352     sqlite3 *db,        /* The database connection */
124348 125353     Token *pTableName,  /* Name of the table into which we insert */
124349 125354     IdList *pColumn,    /* List of columns in pTableName to insert into */
124350 125355     Select *pSelect,    /* A SELECT statement that supplies values */
124351         -  u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
       125356  +  u8 orconf,          /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
       125357  +  const char *zStart, /* Start of SQL text */
       125358  +  const char *zEnd    /* End of SQL text */
124352 125359   ){
124353 125360     TriggerStep *pTriggerStep;
124354 125361   
124355 125362     assert(pSelect != 0 || db->mallocFailed);
124356 125363   
124357         -  pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
       125364  +  pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName, zStart, zEnd);
124358 125365     if( pTriggerStep ){
124359 125366       pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
124360 125367       pTriggerStep->pIdList = pColumn;
124361 125368       pTriggerStep->orconf = orconf;
124362 125369     }else{
124363 125370       sqlite3IdListDelete(db, pColumn);
124364 125371     }
................................................................................
124373 125380   ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
124374 125381   */
124375 125382   SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
124376 125383     sqlite3 *db,         /* The database connection */
124377 125384     Token *pTableName,   /* Name of the table to be updated */
124378 125385     ExprList *pEList,    /* The SET clause: list of column and new values */
124379 125386     Expr *pWhere,        /* The WHERE clause */
124380         -  u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
       125387  +  u8 orconf,           /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
       125388  +  const char *zStart,  /* Start of SQL text */
       125389  +  const char *zEnd     /* End of SQL text */
124381 125390   ){
124382 125391     TriggerStep *pTriggerStep;
124383 125392   
124384         -  pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
       125393  +  pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName, zStart, zEnd);
124385 125394     if( pTriggerStep ){
124386 125395       pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
124387 125396       pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
124388 125397       pTriggerStep->orconf = orconf;
124389 125398     }
124390 125399     sqlite3ExprListDelete(db, pEList);
124391 125400     sqlite3ExprDelete(db, pWhere);
................................................................................
124396 125405   ** Construct a trigger step that implements a DELETE statement and return
124397 125406   ** a pointer to that trigger step.  The parser calls this routine when it
124398 125407   ** sees a DELETE statement inside the body of a CREATE TRIGGER.
124399 125408   */
124400 125409   SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
124401 125410     sqlite3 *db,            /* Database connection */
124402 125411     Token *pTableName,      /* The table from which rows are deleted */
124403         -  Expr *pWhere            /* The WHERE clause */
       125412  +  Expr *pWhere,           /* The WHERE clause */
       125413  +  const char *zStart,     /* Start of SQL text */
       125414  +  const char *zEnd        /* End of SQL text */
124404 125415   ){
124405 125416     TriggerStep *pTriggerStep;
124406 125417   
124407         -  pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
       125418  +  pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName, zStart, zEnd);
124408 125419     if( pTriggerStep ){
124409 125420       pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
124410 125421       pTriggerStep->orconf = OE_Default;
124411 125422     }
124412 125423     sqlite3ExprDelete(db, pWhere);
124413 125424     return pTriggerStep;
124414 125425   }
................................................................................
124654 125665       **   END;
124655 125666       **
124656 125667       **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
124657 125668       **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
124658 125669       */
124659 125670       pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
124660 125671       assert( pParse->okConstFactor==0 );
       125672  +
       125673  +#ifndef SQLITE_OMIT_TRACE
       125674  +    if( pStep->zSpan ){
       125675  +      sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
       125676  +                        sqlite3MPrintf(db, "-- %s", pStep->zSpan),
       125677  +                        P4_DYNAMIC);
       125678  +    }
       125679  +#endif
124661 125680   
124662 125681       switch( pStep->op ){
124663 125682         case TK_UPDATE: {
124664 125683           sqlite3Update(pParse, 
124665 125684             targetSrcList(pParse, pStep),
124666 125685             sqlite3ExprListDup(db, pStep->pExprList, 0), 
124667 125686             sqlite3ExprDup(db, pStep->pWhere, 0), 
124668         -          pParse->eOrconf
       125687  +          pParse->eOrconf, 0, 0
124669 125688           );
124670 125689           break;
124671 125690         }
124672 125691         case TK_INSERT: {
124673 125692           sqlite3Insert(pParse, 
124674 125693             targetSrcList(pParse, pStep),
124675 125694             sqlite3SelectDup(db, pStep->pSelect, 0), 
................................................................................
124677 125696             pParse->eOrconf
124678 125697           );
124679 125698           break;
124680 125699         }
124681 125700         case TK_DELETE: {
124682 125701           sqlite3DeleteFrom(pParse, 
124683 125702             targetSrcList(pParse, pStep),
124684         -          sqlite3ExprDup(db, pStep->pWhere, 0)
       125703  +          sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
124685 125704           );
124686 125705           break;
124687 125706         }
124688 125707         default: assert( pStep->op==TK_SELECT ); {
124689 125708           SelectDest sDest;
124690 125709           Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
124691 125710           sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
................................................................................
124795 125814         (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
124796 125815           (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
124797 125816           (pTrigger->op==TK_INSERT ? "INSERT" : ""),
124798 125817           (pTrigger->op==TK_DELETE ? "DELETE" : ""),
124799 125818         pTab->zName
124800 125819       ));
124801 125820   #ifndef SQLITE_OMIT_TRACE
124802         -    sqlite3VdbeChangeP4(v, -1, 
124803         -      sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
124804         -    );
       125821  +    if( pTrigger->zName ){
       125822  +      sqlite3VdbeChangeP4(v, -1, 
       125823  +        sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
       125824  +      );
       125825  +    }
124805 125826   #endif
124806 125827   
124807 125828       /* If one was specified, code the WHEN clause. If it evaluates to false
124808 125829       ** (or NULL) the sub-vdbe is immediately halted by jumping to the 
124809 125830       ** OP_Halt inserted at the end of the program.  */
124810 125831       if( pTrigger->pWhen ){
124811 125832         pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
................................................................................
124825 125846       if( iEndTrigger ){
124826 125847         sqlite3VdbeResolveLabel(v, iEndTrigger);
124827 125848       }
124828 125849       sqlite3VdbeAddOp0(v, OP_Halt);
124829 125850       VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
124830 125851   
124831 125852       transferParseError(pParse, pSubParse);
124832         -    if( db->mallocFailed==0 ){
       125853  +    if( db->mallocFailed==0 && pParse->nErr==0 ){
124833 125854         pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
124834 125855       }
124835 125856       pProgram->nMem = pSubParse->nMem;
124836 125857       pProgram->nCsr = pSubParse->nTab;
124837 125858       pProgram->token = (void *)pTrigger;
124838 125859       pPrg->aColmask[0] = pSubParse->oldmask;
124839 125860       pPrg->aColmask[1] = pSubParse->newmask;
................................................................................
125144 126165   *            onError   pTabList      pChanges             pWhere
125145 126166   */
125146 126167   SQLITE_PRIVATE void sqlite3Update(
125147 126168     Parse *pParse,         /* The parser context */
125148 126169     SrcList *pTabList,     /* The table in which we should change things */
125149 126170     ExprList *pChanges,    /* Things to be changed */
125150 126171     Expr *pWhere,          /* The WHERE clause.  May be null */
125151         -  int onError            /* How to handle constraint errors */
       126172  +  int onError,           /* How to handle constraint errors */
       126173  +  ExprList *pOrderBy,    /* ORDER BY clause. May be null */
       126174  +  Expr *pLimit           /* LIMIT clause. May be null */
125152 126175   ){
125153 126176     int i, j;              /* Loop counters */
125154 126177     Table *pTab;           /* The table to be updated */
125155 126178     int addrTop = 0;       /* VDBE instruction address of the start of the loop */
125156 126179     WhereInfo *pWInfo;     /* Information about the WHERE clause */
125157 126180     Vdbe *v;               /* The virtual database engine */
125158 126181     Index *pIdx;           /* For looping over indices */
................................................................................
125228 126251   # define isView 0
125229 126252   # define tmask 0
125230 126253   #endif
125231 126254   #ifdef SQLITE_OMIT_VIEW
125232 126255   # undef isView
125233 126256   # define isView 0
125234 126257   #endif
       126258  +
       126259  +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
       126260  +  if( !isView ){
       126261  +    pWhere = sqlite3LimitWhere(
       126262  +        pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
       126263  +    );
       126264  +    pOrderBy = 0;
       126265  +    pLimit = 0;
       126266  +  }
       126267  +#endif
125235 126268   
125236 126269     if( sqlite3ViewGetColumnNames(pParse, pTab) ){
125237 126270       goto update_cleanup;
125238 126271     }
125239 126272     if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
125240 126273       goto update_cleanup;
125241 126274     }
................................................................................
125397 126430     }
125398 126431   
125399 126432     /* If we are trying to update a view, realize that view into
125400 126433     ** an ephemeral table.
125401 126434     */
125402 126435   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
125403 126436     if( isView ){
125404         -    sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur);
       126437  +    sqlite3MaterializeView(pParse, pTab, 
       126438  +        pWhere, pOrderBy, pLimit, iDataCur
       126439  +    );
       126440  +    pOrderBy = 0;
       126441  +    pLimit = 0;
125405 126442     }
125406 126443   #endif
125407 126444   
125408 126445     /* Resolve the column names in all the expressions in the
125409 126446     ** WHERE clause.
125410 126447     */
125411 126448     if( sqlite3ResolveExprNames(&sNC, pWhere) ){
................................................................................
125781 126818   
125782 126819   update_cleanup:
125783 126820     sqlite3AuthContextPop(&sContext);
125784 126821     sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
125785 126822     sqlite3SrcListDelete(db, pTabList);
125786 126823     sqlite3ExprListDelete(db, pChanges);
125787 126824     sqlite3ExprDelete(db, pWhere);
       126825  +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) 
       126826  +  sqlite3ExprListDelete(db, pOrderBy);
       126827  +  sqlite3ExprDelete(db, pLimit);
       126828  +#endif
125788 126829     return;
125789 126830   }
125790 126831   /* Make sure "isView" and other macros defined above are undefined. Otherwise
125791 126832   ** they may interfere with compilation of other functions in this file
125792 126833   ** (or in another file, if this file becomes part of the amalgamation).  */
125793 126834   #ifdef isView
125794 126835    #undef isView
................................................................................
125840 126881     int regRec;                     /* Register in which to assemble record */
125841 126882     int regRowid;                   /* Register for ephem table rowid */
125842 126883     int iCsr = pSrc->a[0].iCursor;  /* Cursor used for virtual table scan */
125843 126884     int aDummy[2];                  /* Unused arg for sqlite3WhereOkOnePass() */
125844 126885     int bOnePass;                   /* True to use onepass strategy */
125845 126886     int addr;                       /* Address of OP_OpenEphemeral */
125846 126887   
125847         -  /* Allocate nArg registers to martial the arguments to VUpdate. Then
       126888  +  /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
125848 126889     ** create and open the ephemeral table in which the records created from
125849 126890     ** these arguments will be temporarily stored. */
125850 126891     assert( v );
125851 126892     ephemTab = pParse->nTab++;
125852 126893     addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg);
125853 126894     regArg = pParse->nMem + 1;
125854 126895     pParse->nMem += nArg;
................................................................................
125861 126902   
125862 126903     /* Populate the argument registers. */
125863 126904     for(i=0; i<pTab->nCol; i++){
125864 126905       if( aXRef[i]>=0 ){
125865 126906         sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
125866 126907       }else{
125867 126908         sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
       126909  +      sqlite3VdbeChangeP5(v, 1); /* Enable sqlite3_vtab_nochange() */
125868 126910       }
125869 126911     }
125870 126912     if( HasRowid(pTab) ){
125871 126913       sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
125872 126914       if( pRowid ){
125873 126915         sqlite3ExprCode(pParse, pRowid, regArg+1);
125874 126916       }else{
................................................................................
125895 126937       if( sqlite3IsToplevel(pParse) ){
125896 126938         pParse->isMultiWrite = 0;
125897 126939       }
125898 126940     }else{
125899 126941       /* Create a record from the argument register contents and insert it into
125900 126942       ** the ephemeral table. */
125901 126943       sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
       126944  +#ifdef SQLITE_DEBUG
       126945  +    /* Signal an assert() within OP_MakeRecord that it is allowed to
       126946  +    ** accept no-change records with serial_type 10 */
       126947  +    sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
       126948  +#endif
125902 126949       sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
125903 126950       sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
125904 126951     }
125905 126952   
125906 126953   
125907 126954     if( bOnePass==0 ){
125908 126955       /* End the virtual table scan */
................................................................................
128245 129292     WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
128246 129293     int iLevel,                     /* Value for "level" column of output */
128247 129294     int iFrom,                      /* Value for "from" column of output */
128248 129295     u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
128249 129296   ){
128250 129297     int ret = 0;
128251 129298   #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
128252         -  if( pParse->explain==2 )
       129299  +  if( sqlite3ParseToplevel(pParse)->explain==2 )
128253 129300   #endif
128254 129301     {
128255 129302       struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
128256 129303       Vdbe *v = pParse->pVdbe;      /* VM being constructed */
128257 129304       sqlite3 *db = pParse->db;     /* Database handle */
128258 129305       int iId = pParse->iSelectId;  /* Select id (left-most output column) */
128259 129306       int isSearch;                 /* True for a SEARCH. False for SCAN. */
................................................................................
128411 129458   ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
128412 129459   ** The TERM_LIKECOND marking indicates that the term should be coded inside
128413 129460   ** a conditional such that is only evaluated on the second pass of a
128414 129461   ** LIKE-optimization loop, when scanning BLOBs instead of strings.
128415 129462   */
128416 129463   static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
128417 129464     int nLoop = 0;
128418         -  while( ALWAYS(pTerm!=0)
128419         -      && (pTerm->wtFlags & TERM_CODED)==0
       129465  +  assert( pTerm!=0 );
       129466  +  while( (pTerm->wtFlags & TERM_CODED)==0
128420 129467         && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
128421 129468         && (pLevel->notReady & pTerm->prereqAll)==0
128422 129469     ){
128423 129470       if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
128424 129471         pTerm->wtFlags |= TERM_LIKECOND;
128425 129472       }else{
128426 129473         pTerm->wtFlags |= TERM_CODED;
128427 129474       }
128428 129475       if( pTerm->iParent<0 ) break;
128429 129476       pTerm = &pTerm->pWC->a[pTerm->iParent];
       129477  +    assert( pTerm!=0 );
128430 129478       pTerm->nChild--;
128431 129479       if( pTerm->nChild!=0 ) break;
128432 129480       nLoop++;
128433 129481     }
128434 129482   }
128435 129483   
128436 129484   /*
................................................................................
128492 129540       if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB
128493 129541        || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
128494 129542       ){
128495 129543         zAff[i] = SQLITE_AFF_BLOB;
128496 129544       }
128497 129545     }
128498 129546   }
       129547  +
       129548  +
       129549  +/*
       129550  +** pX is an expression of the form:  (vector) IN (SELECT ...)
       129551  +** In other words, it is a vector IN operator with a SELECT clause on the
       129552  +** LHS.  But not all terms in the vector are indexable and the terms might
       129553  +** not be in the correct order for indexing.
       129554  +**
       129555  +** This routine makes a copy of the input pX expression and then adjusts
       129556  +** the vector on the LHS with corresponding changes to the SELECT so that
       129557  +** the vector contains only index terms and those terms are in the correct
       129558  +** order.  The modified IN expression is returned.  The caller is responsible
       129559  +** for deleting the returned expression.
       129560  +**
       129561  +** Example:
       129562  +**
       129563  +**    CREATE TABLE t1(a,b,c,d,e,f);
       129564  +**    CREATE INDEX t1x1 ON t1(e,c);
       129565  +**    SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
       129566  +**                           \_______________________________________/
       129567  +**                                     The pX expression
       129568  +**
       129569  +** Since only columns e and c can be used with the index, in that order,
       129570  +** the modified IN expression that is returned will be:
       129571  +**
       129572  +**        (e,c) IN (SELECT z,x FROM t2)
       129573  +**
       129574  +** The reduced pX is different from the original (obviously) and thus is
       129575  +** only used for indexing, to improve performance.  The original unaltered
       129576  +** IN expression must also be run on each output row for correctness.
       129577  +*/
       129578  +static Expr *removeUnindexableInClauseTerms(
       129579  +  Parse *pParse,        /* The parsing context */
       129580  +  int iEq,              /* Look at loop terms starting here */
       129581  +  WhereLoop *pLoop,     /* The current loop */
       129582  +  Expr *pX              /* The IN expression to be reduced */
       129583  +){
       129584  +  sqlite3 *db = pParse->db;
       129585  +  Expr *pNew = sqlite3ExprDup(db, pX, 0);
       129586  +  if( db->mallocFailed==0 ){
       129587  +    ExprList *pOrigRhs = pNew->x.pSelect->pEList;  /* Original unmodified RHS */
       129588  +    ExprList *pOrigLhs = pNew->pLeft->x.pList;     /* Original unmodified LHS */
       129589  +    ExprList *pRhs = 0;         /* New RHS after modifications */
       129590  +    ExprList *pLhs = 0;         /* New LHS after mods */
       129591  +    int i;                      /* Loop counter */
       129592  +    Select *pSelect;            /* Pointer to the SELECT on the RHS */
       129593  +
       129594  +    for(i=iEq; i<pLoop->nLTerm; i++){
       129595  +      if( pLoop->aLTerm[i]->pExpr==pX ){
       129596  +        int iField = pLoop->aLTerm[i]->iField - 1;
       129597  +        assert( pOrigRhs->a[iField].pExpr!=0 );
       129598  +        pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
       129599  +        pOrigRhs->a[iField].pExpr = 0;
       129600  +        assert( pOrigLhs->a[iField].pExpr!=0 );
       129601  +        pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
       129602  +        pOrigLhs->a[iField].pExpr = 0;
       129603  +      }
       129604  +    }
       129605  +    sqlite3ExprListDelete(db, pOrigRhs);
       129606  +    sqlite3ExprListDelete(db, pOrigLhs);
       129607  +    pNew->pLeft->x.pList = pLhs;
       129608  +    pNew->x.pSelect->pEList = pRhs;
       129609  +    if( pLhs && pLhs->nExpr==1 ){
       129610  +      /* Take care here not to generate a TK_VECTOR containing only a
       129611  +      ** single value. Since the parser never creates such a vector, some
       129612  +      ** of the subroutines do not handle this case.  */
       129613  +      Expr *p = pLhs->a[0].pExpr;
       129614  +      pLhs->a[0].pExpr = 0;
       129615  +      sqlite3ExprDelete(db, pNew->pLeft);
       129616  +      pNew->pLeft = p;
       129617  +    }
       129618  +    pSelect = pNew->x.pSelect;
       129619  +    if( pSelect->pOrderBy ){
       129620  +      /* If the SELECT statement has an ORDER BY clause, zero the 
       129621  +      ** iOrderByCol variables. These are set to non-zero when an 
       129622  +      ** ORDER BY term exactly matches one of the terms of the 
       129623  +      ** result-set. Since the result-set of the SELECT statement may
       129624  +      ** have been modified or reordered, these variables are no longer 
       129625  +      ** set correctly.  Since setting them is just an optimization, 
       129626  +      ** it's easiest just to zero them here.  */
       129627  +      ExprList *pOrderBy = pSelect->pOrderBy;
       129628  +      for(i=0; i<pOrderBy->nExpr; i++){
       129629  +        pOrderBy->a[i].u.x.iOrderByCol = 0;
       129630  +      }
       129631  +    }
       129632  +
       129633  +#if 0
       129634  +    printf("For indexing, change the IN expr:\n");
       129635  +    sqlite3TreeViewExpr(0, pX, 0);
       129636  +    printf("Into:\n");
       129637  +    sqlite3TreeViewExpr(0, pNew, 0);
       129638  +#endif
       129639  +  }
       129640  +  return pNew;
       129641  +}
       129642  +
128499 129643   
128500 129644   /*
128501 129645   ** Generate code for a single equality term of the WHERE clause.  An equality
128502 129646   ** term can be either X=expr or X IN (...).   pTerm is the term to be 
128503 129647   ** coded.
128504 129648   **
128505 129649   ** The current value for the constraint is left in a register, the index
................................................................................
128555 129699       for(i=0; i<iEq; i++){
128556 129700         if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
128557 129701           disableTerm(pLevel, pTerm);
128558 129702           return iTarget;
128559 129703         }
128560 129704       }
128561 129705       for(i=iEq;i<pLoop->nLTerm; i++){
128562         -      if( ALWAYS(pLoop->aLTerm[i]) && pLoop->aLTerm[i]->pExpr==pX ) nEq++;
       129706  +      assert( pLoop->aLTerm[i]!=0 );
       129707  +      if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
128563 129708       }
128564 129709   
128565 129710       if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
128566 129711         eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0);
128567 129712       }else{
128568         -      Select *pSelect = pX->x.pSelect;
128569 129713         sqlite3 *db = pParse->db;
128570         -      u16 savedDbOptFlags = db->dbOptFlags;
128571         -      ExprList *pOrigRhs = pSelect->pEList;
128572         -      ExprList *pOrigLhs = pX->pLeft->x.pList;
128573         -      ExprList *pRhs = 0;         /* New Select.pEList for RHS */
128574         -      ExprList *pLhs = 0;         /* New pX->pLeft vector */
       129714  +      pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
128575 129715   
128576         -      for(i=iEq;i<pLoop->nLTerm; i++){
128577         -        if( pLoop->aLTerm[i]->pExpr==pX ){
128578         -          int iField = pLoop->aLTerm[i]->iField - 1;
128579         -          Expr *pNewRhs = sqlite3ExprDup(db, pOrigRhs->a[iField].pExpr, 0);
128580         -          Expr *pNewLhs = sqlite3ExprDup(db, pOrigLhs->a[iField].pExpr, 0);
128581         -
128582         -          pRhs = sqlite3ExprListAppend(pParse, pRhs, pNewRhs);
128583         -          pLhs = sqlite3ExprListAppend(pParse, pLhs, pNewLhs);
128584         -        }
128585         -      }
128586 129716         if( !db->mallocFailed ){
128587         -        Expr *pLeft = pX->pLeft;
128588         -
128589         -        if( pSelect->pOrderBy ){
128590         -          /* If the SELECT statement has an ORDER BY clause, zero the 
128591         -          ** iOrderByCol variables. These are set to non-zero when an 
128592         -          ** ORDER BY term exactly matches one of the terms of the 
128593         -          ** result-set. Since the result-set of the SELECT statement may
128594         -          ** have been modified or reordered, these variables are no longer 
128595         -          ** set correctly.  Since setting them is just an optimization, 
128596         -          ** it's easiest just to zero them here.  */
128597         -          ExprList *pOrderBy = pSelect->pOrderBy;
128598         -          for(i=0; i<pOrderBy->nExpr; i++){
128599         -            pOrderBy->a[i].u.x.iOrderByCol = 0;
128600         -          }
128601         -        }
128602         -
128603         -        /* Take care here not to generate a TK_VECTOR containing only a
128604         -        ** single value. Since the parser never creates such a vector, some
128605         -        ** of the subroutines do not handle this case.  */
128606         -        if( pLhs->nExpr==1 ){
128607         -          pX->pLeft = pLhs->a[0].pExpr;
128608         -        }else{
128609         -          pLeft->x.pList = pLhs;
128610         -          aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int) * nEq);
128611         -          testcase( aiMap==0 );
128612         -        }
128613         -        pSelect->pEList = pRhs;
128614         -        db->dbOptFlags |= SQLITE_QueryFlattener;
       129717  +        aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
128615 129718           eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap);
128616         -        db->dbOptFlags = savedDbOptFlags;
128617         -        testcase( aiMap!=0 && aiMap[0]!=0 );
128618         -        pSelect->pEList = pOrigRhs;
128619         -        pLeft->x.pList = pOrigLhs;
128620         -        pX->pLeft = pLeft;
       129719  +        pTerm->pExpr->iTable = pX->iTable;
128621 129720         }
128622         -      sqlite3ExprListDelete(pParse->db, pLhs);
128623         -      sqlite3ExprListDelete(pParse->db, pRhs);
       129721  +      sqlite3ExprDelete(db, pX);
       129722  +      pX = pTerm->pExpr;
128624 129723       }
128625 129724   
128626 129725       if( eType==IN_INDEX_INDEX_DESC ){
128627 129726         testcase( bRev );
128628 129727         bRev = !bRev;
128629 129728       }
128630 129729       iTab = pX->iTable;
................................................................................
129755 130854         if( sqlite3ExprIsVector(pRight)==0 ){
129756 130855           disableTerm(pLevel, pRangeEnd);
129757 130856         }else{
129758 130857           endEq = 1;
129759 130858         }
129760 130859       }else if( bStopAtNull ){
129761 130860         sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
       130861  +      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
129762 130862         endEq = 0;
129763 130863         nConstraint++;
129764 130864       }
129765 130865       sqlite3DbFree(db, zStartAff);
129766 130866       sqlite3DbFree(db, zEndAff);
129767 130867   
129768 130868       /* Top of the loop body */
................................................................................
131255 132355     Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
131256 132356     Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
131257 132357     int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
131258 132358     int noCase = 0;                  /* uppercase equivalent to lowercase */
131259 132359     int op;                          /* Top-level operator.  pExpr->op */
131260 132360     Parse *pParse = pWInfo->pParse;  /* Parsing context */
131261 132361     sqlite3 *db = pParse->db;        /* Database connection */
131262         -  unsigned char eOp2;              /* op2 value for LIKE/REGEXP/GLOB */
       132362  +  unsigned char eOp2 = 0;          /* op2 value for LIKE/REGEXP/GLOB */
131263 132363     int nLeft;                       /* Number of elements on left side vector */
131264 132364   
131265 132365     if( db->mallocFailed ){
131266 132366       return;
131267 132367     }
131268 132368     pTerm = &pWC->a[idxTerm];
131269 132369     pMaskSet = &pWInfo->sMaskSet;
................................................................................
131499 132599     ** not normally optimized for ordinary tables.  In other words, OP
131500 132600     ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
131501 132601     ** This information is used by the xBestIndex methods of
131502 132602     ** virtual tables.  The native query optimizer does not attempt
131503 132603     ** to do anything with MATCH functions.
131504 132604     */
131505 132605     if( pWC->op==TK_AND ){
131506         -    Expr *pRight, *pLeft;
       132606  +    Expr *pRight = 0, *pLeft = 0;
131507 132607       int res = isAuxiliaryVtabOperator(pExpr, &eOp2, &pLeft, &pRight);
131508 132608       while( res-- > 0 ){
131509 132609         int idxNew;
131510 132610         WhereTerm *pNewTerm;
131511 132611         Bitmask prereqColumn, prereqExpr;
131512 132612   
131513 132613         prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
................................................................................
131822 132922   ** generating the code that loops through a table looking for applicable
131823 132923   ** rows.  Indices are selected and used to speed the search when doing
131824 132924   ** so is applicable.  Because this module is responsible for selecting
131825 132925   ** indices, you might also think of this module as the "query optimizer".
131826 132926   */
131827 132927   /* #include "sqliteInt.h" */
131828 132928   /* #include "whereInt.h" */
       132929  +
       132930  +/*
       132931  +** Extra information appended to the end of sqlite3_index_info but not
       132932  +** visible to the xBestIndex function, at least not directly.  The
       132933  +** sqlite3_vtab_collation() interface knows how to reach it, however.
       132934  +**
       132935  +** This object is not an API and can be changed from one release to the
       132936  +** next.  As long as allocateIndexInfo() and sqlite3_vtab_collation()
       132937  +** agree on the structure, all will be well.
       132938  +*/
       132939  +typedef struct HiddenIndexInfo HiddenIndexInfo;
       132940  +struct HiddenIndexInfo {
       132941  +  WhereClause *pWC;   /* The Where clause being analyzed */
       132942  +  Parse *pParse;      /* The parsing context */
       132943  +};
131829 132944   
131830 132945   /* Forward declaration of methods */
131831 132946   static int whereLoopResize(sqlite3*, WhereLoop*, int);
131832 132947   
131833 132948   /* Test variable that can be set to enable WHERE tracing */
131834 132949   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
131835 132950   /***/ int sqlite3WhereTrace = 0;
................................................................................
132645 133760   #ifndef SQLITE_OMIT_VIRTUALTABLE
132646 133761   /*
132647 133762   ** Allocate and populate an sqlite3_index_info structure. It is the 
132648 133763   ** responsibility of the caller to eventually release the structure
132649 133764   ** by passing the pointer returned by this function to sqlite3_free().
132650 133765   */
132651 133766   static sqlite3_index_info *allocateIndexInfo(
132652         -  Parse *pParse,
132653         -  WhereClause *pWC,
       133767  +  Parse *pParse,                  /* The parsing context */
       133768  +  WhereClause *pWC,               /* The WHERE clause being analyzed */
132654 133769     Bitmask mUnusable,              /* Ignore terms with these prereqs */
132655         -  struct SrcList_item *pSrc,
132656         -  ExprList *pOrderBy,
       133770  +  struct SrcList_item *pSrc,      /* The FROM clause term that is the vtab */
       133771  +  ExprList *pOrderBy,             /* The ORDER BY clause */
132657 133772     u16 *pmNoOmit                   /* Mask of terms not to omit */
132658 133773   ){
132659 133774     int i, j;
132660 133775     int nTerm;
132661 133776     struct sqlite3_index_constraint *pIdxCons;
132662 133777     struct sqlite3_index_orderby *pIdxOrderBy;
132663 133778     struct sqlite3_index_constraint_usage *pUsage;
       133779  +  struct HiddenIndexInfo *pHidden;
132664 133780     WhereTerm *pTerm;
132665 133781     int nOrderBy;
132666 133782     sqlite3_index_info *pIdxInfo;
132667 133783     u16 mNoOmit = 0;
132668 133784   
132669 133785     /* Count the number of possible WHERE clause constraints referring
132670 133786     ** to this virtual table */
................................................................................
132698 133814       }
132699 133815     }
132700 133816   
132701 133817     /* Allocate the sqlite3_index_info structure
132702 133818     */
132703 133819     pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
132704 133820                              + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
132705         -                           + sizeof(*pIdxOrderBy)*nOrderBy );
       133821  +                           + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) );
132706 133822     if( pIdxInfo==0 ){
132707 133823       sqlite3ErrorMsg(pParse, "out of memory");
132708 133824       return 0;
132709 133825     }
132710 133826   
132711 133827     /* Initialize the structure.  The sqlite3_index_info structure contains
132712 133828     ** many fields that are declared "const" to prevent xBestIndex from
132713 133829     ** changing them.  We have to do some funky casting in order to
132714 133830     ** initialize those fields.
132715 133831     */
132716         -  pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
       133832  +  pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
       133833  +  pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
132717 133834     pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
132718 133835     pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
132719 133836     *(int*)&pIdxInfo->nConstraint = nTerm;
132720 133837     *(int*)&pIdxInfo->nOrderBy = nOrderBy;
132721 133838     *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
132722 133839     *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
132723 133840     *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
132724 133841                                                                      pUsage;
132725 133842   
       133843  +  pHidden->pWC = pWC;
       133844  +  pHidden->pParse = pParse;
132726 133845     for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
132727 133846       u16 op;
132728 133847       if( pTerm->leftCursor != pSrc->iCursor ) continue;
132729 133848       if( pTerm->prereqRight & mUnusable ) continue;
132730 133849       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
132731 133850       testcase( pTerm->eOperator & WO_IN );
132732 133851       testcase( pTerm->eOperator & WO_IS );
................................................................................
133667 134786     sqlite3DbFreeNN(db, p);
133668 134787   }
133669 134788   
133670 134789   /*
133671 134790   ** Free a WhereInfo structure
133672 134791   */
133673 134792   static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
133674         -  if( ALWAYS(pWInfo) ){
133675         -    int i;
133676         -    for(i=0; i<pWInfo->nLevel; i++){
133677         -      WhereLevel *pLevel = &pWInfo->a[i];
133678         -      if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
133679         -        sqlite3DbFree(db, pLevel->u.in.aInLoop);
133680         -      }
       134793  +  int i;
       134794  +  assert( pWInfo!=0 );
       134795  +  for(i=0; i<pWInfo->nLevel; i++){
       134796  +    WhereLevel *pLevel = &pWInfo->a[i];
       134797  +    if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
       134798  +      sqlite3DbFree(db, pLevel->u.in.aInLoop);
133681 134799       }
133682         -    sqlite3WhereClauseClear(&pWInfo->sWC);
133683         -    while( pWInfo->pLoops ){
133684         -      WhereLoop *p = pWInfo->pLoops;
133685         -      pWInfo->pLoops = p->pNextLoop;
133686         -      whereLoopDelete(db, p);
133687         -    }
133688         -    sqlite3DbFreeNN(db, pWInfo);
133689 134800     }
       134801  +  sqlite3WhereClauseClear(&pWInfo->sWC);
       134802  +  while( pWInfo->pLoops ){
       134803  +    WhereLoop *p = pWInfo->pLoops;
       134804  +    pWInfo->pLoops = p->pNextLoop;
       134805  +    whereLoopDelete(db, p);
       134806  +  }
       134807  +  sqlite3DbFreeNN(db, pWInfo);
133690 134808   }
133691 134809   
133692 134810   /*
133693 134811   ** Return TRUE if all of the following are true:
133694 134812   **
133695 134813   **   (1)  X has the same or lower cost that Y
133696 134814   **   (2)  X uses fewer WHERE clause terms than Y
................................................................................
134265 135383                             ** changes "x IN (?)" into "x=?". */
134266 135384         }
134267 135385       }else if( eOp & (WO_EQ|WO_IS) ){
134268 135386         int iCol = pProbe->aiColumn[saved_nEq];
134269 135387         pNew->wsFlags |= WHERE_COLUMN_EQ;
134270 135388         assert( saved_nEq==pNew->u.btree.nEq );
134271 135389         if( iCol==XN_ROWID 
134272         -       || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
       135390  +       || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
134273 135391         ){
134274 135392           if( iCol>=0 && pProbe->uniqNotNull==0 ){
134275 135393             pNew->wsFlags |= WHERE_UNQ_WANTED;
134276 135394           }else{
134277 135395             pNew->wsFlags |= WHERE_ONEROW;
134278 135396           }
134279 135397         }
................................................................................
134674 135792           pNew->prereq = mPrereq | pTerm->prereqRight;
134675 135793           rc = whereLoopInsert(pBuilder, pNew);
134676 135794         }
134677 135795       }
134678 135796     }
134679 135797   #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
134680 135798   
134681         -  /* Loop over all indices
134682         -  */
134683         -  for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
       135799  +  /* Loop over all indices. If there was an INDEXED BY clause, then only 
       135800  +  ** consider index pProbe.  */
       135801  +  for(; rc==SQLITE_OK && pProbe; 
       135802  +      pProbe=(pSrc->pIBIndex ? 0 : pProbe->pNext), iSortIdx++
       135803  +  ){
134684 135804       if( pProbe->pPartIdxWhere!=0
134685 135805        && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
134686 135806         testcase( pNew->iTab!=pSrc->iCursor );  /* See ticket [98d973b8f5] */
134687 135807         continue;  /* Partial index inappropriate for this query */
134688 135808       }
       135809  +    if( pProbe->bNoQuery ) continue;
134689 135810       rSize = pProbe->aiRowLogEst[0];
134690 135811       pNew->u.btree.nEq = 0;
134691 135812       pNew->u.btree.nBtm = 0;
134692 135813       pNew->u.btree.nTop = 0;
134693 135814       pNew->nSkip = 0;
134694 135815       pNew->nLTerm = 0;
134695 135816       pNew->iSortIdx = 0;
................................................................................
134786 135907         pTab->tabFlags |= TF_StatsUsed;
134787 135908       }
134788 135909   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
134789 135910       sqlite3Stat4ProbeFree(pBuilder->pRec);
134790 135911       pBuilder->nRecValid = 0;
134791 135912       pBuilder->pRec = 0;
134792 135913   #endif
134793         -
134794         -    /* If there was an INDEXED BY clause, then only that one index is
134795         -    ** considered. */
134796         -    if( pSrc->pIBIndex ) break;
134797 135914     }
134798 135915     return rc;
134799 135916   }
134800 135917   
134801 135918   #ifndef SQLITE_OMIT_VIRTUALTABLE
134802 135919   
134803 135920   /*
................................................................................
134944 136061     WHERETRACE(0xffff, ("  bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
134945 136062                         *pbIn, (sqlite3_uint64)mPrereq,
134946 136063                         (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
134947 136064   
134948 136065     return rc;
134949 136066   }
134950 136067   
       136068  +/*
       136069  +** If this function is invoked from within an xBestIndex() callback, it
       136070  +** returns a pointer to a buffer containing the name of the collation
       136071  +** sequence associated with element iCons of the sqlite3_index_info.aConstraint
       136072  +** array. Or, if iCons is out of range or there is no active xBestIndex
       136073  +** call, return NULL.
       136074  +*/
       136075  +SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
       136076  +  HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
       136077  +  const char *zRet = 0;
       136078  +  if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
       136079  +    CollSeq *pC = 0;
       136080  +    int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
       136081  +    Expr *pX = pHidden->pWC->a[iTerm].pExpr;
       136082  +    if( pX->pLeft ){
       136083  +      pC = sqlite3BinaryCompareCollSeq(pHidden->pParse, pX->pLeft, pX->pRight);
       136084  +    }
       136085  +    zRet = (pC ? pC->zName : "BINARY");
       136086  +  }
       136087  +  return zRet;
       136088  +}
134951 136089   
134952 136090   /*
134953 136091   ** Add all WhereLoop objects for a table of the join identified by
134954 136092   ** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
134955 136093   **
134956 136094   ** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
134957 136095   ** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
................................................................................
136484 137622       }
136485 137623       sqlite3DebugPrintf("\n");
136486 137624       for(ii=0; ii<pWInfo->nLevel; ii++){
136487 137625         whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
136488 137626       }
136489 137627     }
136490 137628   #endif
136491         -  /* Attempt to omit tables from the join that do not effect the result */
       137629  +
       137630  +  /* Attempt to omit tables from the join that do not affect the result.
       137631  +  ** For a table to not affect the result, the following must be true:
       137632  +  **
       137633  +  **   1) The query must not be an aggregate.
       137634  +  **   2) The table must be the RHS of a LEFT JOIN.
       137635  +  **   3) Either the query must be DISTINCT, or else the ON or USING clause
       137636  +  **      must contain a constraint that limits the scan of the table to 
       137637  +  **      at most a single row.
       137638  +  **   4) The table must not be referenced by any part of the query apart
       137639  +  **      from its own USING or ON clause.
       137640  +  **
       137641  +  ** For example, given:
       137642  +  **
       137643  +  **     CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
       137644  +  **     CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
       137645  +  **     CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
       137646  +  **
       137647  +  ** then table t2 can be omitted from the following:
       137648  +  **
       137649  +  **     SELECT v1, v3 FROM t1 
       137650  +  **       LEFT JOIN t2 USING (t1.ipk=t2.ipk)
       137651  +  **       LEFT JOIN t3 USING (t1.ipk=t3.ipk)
       137652  +  **
       137653  +  ** or from:
       137654  +  **
       137655  +  **     SELECT DISTINCT v1, v3 FROM t1 
       137656  +  **       LEFT JOIN t2
       137657  +  **       LEFT JOIN t3 USING (t1.ipk=t3.ipk)
       137658  +  */
       137659  +  notReady = ~(Bitmask)0;
136492 137660     if( pWInfo->nLevel>=2
136493         -   && pResultSet!=0
       137661  +   && pResultSet!=0               /* guarantees condition (1) above */
136494 137662      && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
136495 137663     ){
       137664  +    int i;
136496 137665       Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
136497 137666       if( sWLB.pOrderBy ){
136498 137667         tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
136499 137668       }
136500         -    while( pWInfo->nLevel>=2 ){
       137669  +    for(i=pWInfo->nLevel-1; i>=1; i--){
136501 137670         WhereTerm *pTerm, *pEnd;
136502         -      pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
136503         -      if( (pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 ) break;
       137671  +      struct SrcList_item *pItem;
       137672  +      pLoop = pWInfo->a[i].pWLoop;
       137673  +      pItem = &pWInfo->pTabList->a[pLoop->iTab];
       137674  +      if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
136504 137675         if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
136505 137676          && (pLoop->wsFlags & WHERE_ONEROW)==0
136506 137677         ){
136507         -        break;
       137678  +        continue;
136508 137679         }
136509         -      if( (tabUsed & pLoop->maskSelf)!=0 ) break;
       137680  +      if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
136510 137681         pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
136511 137682         for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
136512         -        if( (pTerm->prereqAll & pLoop->maskSelf)!=0
136513         -         && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
136514         -        ){
136515         -          break;
       137683  +        if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
       137684  +          if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
       137685  +           || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
       137686  +          ){
       137687  +            break;
       137688  +          }
136516 137689           }
136517 137690         }
136518         -      if( pTerm<pEnd ) break;
       137691  +      if( pTerm<pEnd ) continue;
136519 137692         WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
       137693  +      notReady &= ~pLoop->maskSelf;
       137694  +      for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
       137695  +        if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
       137696  +          pTerm->wtFlags |= TERM_CODED;
       137697  +        }
       137698  +      }
       137699  +      if( i!=pWInfo->nLevel-1 ){
       137700  +        int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
       137701  +        memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
       137702  +      }
136520 137703         pWInfo->nLevel--;
136521 137704         nTabList--;
136522 137705       }
136523 137706     }
136524 137707     WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
136525 137708     pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
136526 137709   
136527 137710     /* If the caller is an UPDATE or DELETE statement that is requesting
136528 137711     ** to use a one-pass algorithm, determine if this is appropriate.
       137712  +  **
       137713  +  ** A one-pass approach can be used if the caller has requested one
       137714  +  ** and either (a) the scan visits at most one row or (b) each
       137715  +  ** of the following are true:
       137716  +  **
       137717  +  **   * the caller has indicated that a one-pass approach can be used
       137718  +  **     with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
       137719  +  **   * the table is not a virtual table, and
       137720  +  **   * either the scan does not use the OR optimization or the caller
       137721  +  **     is a DELETE operation (WHERE_DUPLICATES_OK is only specified
       137722  +  **     for DELETE).
       137723  +  **
       137724  +  ** The last qualification is because an UPDATE statement uses
       137725  +  ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
       137726  +  ** use a one-pass approach, and this is not set accurately for scans
       137727  +  ** that use the OR optimization.
136529 137728     */
136530 137729     assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
136531 137730     if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
136532 137731       int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
136533 137732       int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
136534         -    if( bOnerow
136535         -     || ((wctrlFlags & WHERE_ONEPASS_MULTIROW)!=0
136536         -           && 0==(wsFlags & WHERE_VIRTUALTABLE))
136537         -    ){
       137733  +    if( bOnerow || (
       137734  +        0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW)
       137735  +     && 0==(wsFlags & WHERE_VIRTUALTABLE)
       137736  +     && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK))
       137737  +    )){
136538 137738         pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
136539 137739         if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
136540 137740           if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
136541 137741             bFordelete = OPFLAG_FORDELETE;
136542 137742           }
136543 137743           pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
136544 137744         }
................................................................................
136667 137867     pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
136668 137868     if( db->mallocFailed ) goto whereBeginError;
136669 137869   
136670 137870     /* Generate the code to do the search.  Each iteration of the for
136671 137871     ** loop below generates code for a single nested loop of the VM
136672 137872     ** program.
136673 137873     */
136674         -  notReady = ~(Bitmask)0;
136675 137874     for(ii=0; ii<nTabList; ii++){
136676 137875       int addrExplain;
136677 137876       int wsFlags;
136678 137877       pLevel = &pWInfo->a[ii];
136679 137878       wsFlags = pLevel->pWLoop->wsFlags;
136680 137879   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
136681 137880       if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
................................................................................
136731 137930       pLoop = pLevel->pWLoop;
136732 137931       if( pLevel->op!=OP_Noop ){
136733 137932   #ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
136734 137933         int addrSeek = 0;
136735 137934         Index *pIdx;
136736 137935         int n;
136737 137936         if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
       137937  +       && i==pWInfo->nLevel-1  /* Ticket [ef9318757b152e3] 2017-10-21 */
136738 137938          && (pLoop->wsFlags & WHERE_INDEXED)!=0
136739 137939          && (pIdx = pLoop->u.btree.pIndex)->hasStat1
136740 137940          && (n = pLoop->u.btree.nIdxCol)>0
136741 137941          && pIdx->aiRowLogEst[n]>=36
136742 137942         ){
136743 137943           int r1 = pParse->nMem+1;
136744 137944           int j, op;
................................................................................
136797 137997       }
136798 137998   #endif
136799 137999       if( pLevel->iLeftJoin ){
136800 138000         int ws = pLoop->wsFlags;
136801 138001         addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
136802 138002         assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 );
136803 138003         if( (ws & WHERE_IDX_ONLY)==0 ){
136804         -        sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
       138004  +        assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
       138005  +        sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
136805 138006         }
136806 138007         if( (ws & WHERE_INDEXED) 
136807 138008          || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx) 
136808 138009         ){
136809 138010           sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
136810 138011         }
136811 138012         if( pLevel->op==OP_Return ){
................................................................................
136866 138067        && !db->mallocFailed
136867 138068       ){
136868 138069         last = sqlite3VdbeCurrentAddr(v);
136869 138070         k = pLevel->addrBody;
136870 138071         pOp = sqlite3VdbeGetOp(v, k);
136871 138072         for(; k<last; k++, pOp++){
136872 138073           if( pOp->p1!=pLevel->iTabCur ) continue;
136873         -        if( pOp->opcode==OP_Column ){
       138074  +        if( pOp->opcode==OP_Column
       138075  +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
       138076  +         || pOp->opcode==OP_Offset
       138077  +#endif
       138078  +        ){
136874 138079             int x = pOp->p2;
136875 138080             assert( pIdx->pTable==pTab );
136876 138081             if( !HasRowid(pTab) ){
136877 138082               Index *pPk = sqlite3PrimaryKeyIndex(pTab);
136878 138083               x = pPk->aiColumn[x];
136879 138084               assert( x>=0 );
136880 138085             }
................................................................................
136965 138170   
136966 138171   /*
136967 138172   ** Alternative datatype for the argument to the malloc() routine passed
136968 138173   ** into sqlite3ParserAlloc().  The default is size_t.
136969 138174   */
136970 138175   #define YYMALLOCARGTYPE  u64
136971 138176   
136972         -/*
136973         -** An instance of this structure holds information about the
136974         -** LIMIT clause of a SELECT statement.
136975         -*/
136976         -struct LimitVal {
136977         -  Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
136978         -  Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
136979         -};
136980         -
136981 138177   /*
136982 138178   ** An instance of the following structure describes the event of a
136983 138179   ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
136984 138180   ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
136985 138181   **
136986 138182   **      UPDATE ON (a,b,c)
136987 138183   **
................................................................................
137017 138213           cnt>mxSelect
137018 138214         ){
137019 138215           sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
137020 138216         }
137021 138217       }
137022 138218     }
137023 138219   
137024         -  /* This is a utility routine used to set the ExprSpan.zStart and
137025         -  ** ExprSpan.zEnd values of pOut so that the span covers the complete
137026         -  ** range of text beginning with pStart and going to the end of pEnd.
137027         -  */
137028         -  static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
137029         -    pOut->zStart = pStart->z;
137030         -    pOut->zEnd = &pEnd->z[pEnd->n];
137031         -  }
137032 138220   
137033 138221     /* Construct a new Expr object from a single identifier.  Use the
137034 138222     ** new Expr to populate pOut.  Set the span of pOut to be the identifier
137035 138223     ** that created the expression.
137036 138224     */
137037         -  static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token t){
       138225  +  static Expr *tokenExpr(Parse *pParse, int op, Token t){
137038 138226       Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
137039 138227       if( p ){
137040 138228         memset(p, 0, sizeof(Expr));
137041 138229         p->op = (u8)op;
137042 138230         p->flags = EP_Leaf;
137043 138231         p->iAgg = -1;
137044 138232         p->u.zToken = (char*)&p[1];
................................................................................
137048 138236           if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
137049 138237           sqlite3Dequote(p->u.zToken);
137050 138238         }
137051 138239   #if SQLITE_MAX_EXPR_DEPTH>0
137052 138240         p->nHeight = 1;
137053 138241   #endif  
137054 138242       }
137055         -    pOut->pExpr = p;
137056         -    pOut->zStart = t.z;
137057         -    pOut->zEnd = &t.z[t.n];
       138243  +    return p;
137058 138244     }
137059 138245   
137060         -  /* This routine constructs a binary expression node out of two ExprSpan
137061         -  ** objects and uses the result to populate a new ExprSpan object.
137062         -  */
137063         -  static void spanBinaryExpr(
137064         -    Parse *pParse,      /* The parsing context.  Errors accumulate here */
137065         -    int op,             /* The binary operation */
137066         -    ExprSpan *pLeft,    /* The left operand, and output */
137067         -    ExprSpan *pRight    /* The right operand */
137068         -  ){
137069         -    pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr);
137070         -    pLeft->zEnd = pRight->zEnd;
137071         -  }
137072         -
137073         -  /* If doNot is true, then add a TK_NOT Expr-node wrapper around the
137074         -  ** outside of *ppExpr.
137075         -  */
137076         -  static void exprNot(Parse *pParse, int doNot, ExprSpan *pSpan){
137077         -    if( doNot ){
137078         -      pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0);
137079         -    }
137080         -  }
137081         -
137082         -  /* Construct an expression node for a unary postfix operator
137083         -  */
137084         -  static void spanUnaryPostfix(
137085         -    Parse *pParse,         /* Parsing context to record errors */
137086         -    int op,                /* The operator */
137087         -    ExprSpan *pOperand,    /* The operand, and output */
137088         -    Token *pPostOp         /* The operand token for setting the span */
137089         -  ){
137090         -    pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
137091         -    pOperand->zEnd = &pPostOp->z[pPostOp->n];
137092         -  }                           
137093         -
137094 138246     /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
137095 138247     ** unary TK_ISNULL or TK_NOTNULL expression. */
137096 138248     static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
137097 138249       sqlite3 *db = pParse->db;
137098 138250       if( pA && pY && pY->op==TK_NULL ){
137099 138251         pA->op = (u8)op;
137100 138252         sqlite3ExprDelete(db, pA->pRight);
137101 138253         pA->pRight = 0;
137102 138254       }
137103 138255     }
137104 138256   
137105         -  /* Construct an expression node for a unary prefix operator
137106         -  */
137107         -  static void spanUnaryPrefix(
137108         -    ExprSpan *pOut,        /* Write the new expression node here */
137109         -    Parse *pParse,         /* Parsing context to record errors */
137110         -    int op,                /* The operator */
137111         -    ExprSpan *pOperand,    /* The operand */
137112         -    Token *pPreOp         /* The operand token for setting the span */
137113         -  ){
137114         -    pOut->zStart = pPreOp->z;
137115         -    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
137116         -    pOut->zEnd = pOperand->zEnd;
137117         -  }
137118         -
137119 138257     /* Add a single new term to an ExprList that is used to store a
137120 138258     ** list of identifiers.  Report an error if the ID list contains
137121 138259     ** a COLLATE clause or an ASC or DESC keyword, except ignore the
137122 138260     ** error while parsing a legacy schema.
137123 138261     */
137124 138262     static ExprList *parserAddExprIdListTerm(
137125 138263       Parse *pParse,
................................................................................
137180 138318   **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
137181 138319   **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
137182 138320   **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
137183 138321   **    YYERRORSYMBOL      is the code number of the error symbol.  If not
137184 138322   **                       defined, then do no error processing.
137185 138323   **    YYNSTATE           the combined number of states.
137186 138324   **    YYNRULE            the number of rules in the grammar
       138325  +**    YYNTOKEN           Number of terminal symbols
137187 138326   **    YY_MAX_SHIFT       Maximum value for shift actions
137188 138327   **    YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
137189 138328   **    YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
137190         -**    YY_MIN_REDUCE      Minimum value for reduce actions
137191         -**    YY_MAX_REDUCE      Maximum value for reduce actions
137192 138329   **    YY_ERROR_ACTION    The yy_action[] code for syntax error
137193 138330   **    YY_ACCEPT_ACTION   The yy_action[] code for accept
137194 138331   **    YY_NO_ACTION       The yy_action[] code for no-op
       138332  +**    YY_MIN_REDUCE      Minimum value for reduce actions
       138333  +**    YY_MAX_REDUCE      Maximum value for reduce actions
137195 138334   */
137196 138335   #ifndef INTERFACE
137197 138336   # define INTERFACE 1
137198 138337   #endif
137199 138338   /************* Begin control #defines *****************************************/
137200 138339   #define YYCODETYPE unsigned char
137201         -#define YYNOCODE 252
       138340  +#define YYNOCODE 253
137202 138341   #define YYACTIONTYPE unsigned short int
137203 138342   #define YYWILDCARD 83
137204 138343   #define sqlite3ParserTOKENTYPE Token
137205 138344   typedef union {
137206 138345     int yyinit;
137207 138346     sqlite3ParserTOKENTYPE yy0;
137208         -  Expr* yy72;
137209         -  TriggerStep* yy145;
137210         -  ExprList* yy148;
137211         -  SrcList* yy185;
137212         -  ExprSpan yy190;
137213         -  int yy194;
137214         -  Select* yy243;
137215         -  IdList* yy254;
137216         -  With* yy285;
137217         -  struct TrigEvent yy332;
137218         -  struct LimitVal yy354;
137219         -  struct {int value; int mask;} yy497;
       138347  +  int yy4;
       138348  +  struct TrigEvent yy90;
       138349  +  TriggerStep* yy203;
       138350  +  struct {int value; int mask;} yy215;
       138351  +  SrcList* yy259;
       138352  +  Expr* yy314;
       138353  +  ExprList* yy322;
       138354  +  const char* yy336;
       138355  +  IdList* yy384;
       138356  +  Select* yy387;
       138357  +  With* yy451;
137220 138358   } YYMINORTYPE;
137221 138359   #ifndef YYSTACKDEPTH
137222 138360   #define YYSTACKDEPTH 100
137223 138361   #endif
137224 138362   #define sqlite3ParserARG_SDECL Parse *pParse;
137225 138363   #define sqlite3ParserARG_PDECL ,Parse *pParse
137226 138364   #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
137227 138365   #define sqlite3ParserARG_STORE yypParser->pParse = pParse
137228 138366   #define YYFALLBACK 1
137229         -#define YYNSTATE             455
137230         -#define YYNRULE              329
137231         -#define YY_MAX_SHIFT         454
137232         -#define YY_MIN_SHIFTREDUCE   664
137233         -#define YY_MAX_SHIFTREDUCE   992
137234         -#define YY_MIN_REDUCE        993
137235         -#define YY_MAX_REDUCE        1321
137236         -#define YY_ERROR_ACTION      1322
137237         -#define YY_ACCEPT_ACTION     1323
137238         -#define YY_NO_ACTION         1324
       138367  +#define YYNSTATE             466
       138368  +#define YYNRULE              330
       138369  +#define YYNTOKEN             143
       138370  +#define YY_MAX_SHIFT         465
       138371  +#define YY_MIN_SHIFTREDUCE   675
       138372  +#define YY_MAX_SHIFTREDUCE   1004
       138373  +#define YY_ERROR_ACTION      1005
       138374  +#define YY_ACCEPT_ACTION     1006
       138375  +#define YY_NO_ACTION         1007
       138376  +#define YY_MIN_REDUCE        1008
       138377  +#define YY_MAX_REDUCE        1337
137239 138378   /************* End control #defines *******************************************/
137240 138379   
137241 138380   /* Define the yytestcase() macro to be a no-op if is not already defined
137242 138381   ** otherwise.
137243 138382   **
137244 138383   ** Applications can choose to define yytestcase() in the %include section
137245 138384   ** to a macro that can assist in verifying code coverage.  For production
................................................................................
137261 138400   **
137262 138401   **   0 <= N <= YY_MAX_SHIFT             Shift N.  That is, push the lookahead
137263 138402   **                                      token onto the stack and goto state N.
137264 138403   **
137265 138404   **   N between YY_MIN_SHIFTREDUCE       Shift to an arbitrary state then
137266 138405   **     and YY_MAX_SHIFTREDUCE           reduce by rule N-YY_MIN_SHIFTREDUCE.
137267 138406   **
137268         -**   N between YY_MIN_REDUCE            Reduce by rule N-YY_MIN_REDUCE
137269         -**     and YY_MAX_REDUCE
137270         -**
137271 138407   **   N == YY_ERROR_ACTION               A syntax error has occurred.
137272 138408   **
137273 138409   **   N == YY_ACCEPT_ACTION              The parser accepts its input.
137274 138410   **
137275 138411   **   N == YY_NO_ACTION                  No such action.  Denotes unused
137276 138412   **                                      slots in the yy_action[] table.
       138413  +**
       138414  +**   N between YY_MIN_REDUCE            Reduce by rule N-YY_MIN_REDUCE
       138415  +**     and YY_MAX_REDUCE
137277 138416   **
137278 138417   ** The action table is constructed as a single large table named yy_action[].
137279 138418   ** Given state S and lookahead X, the action is computed as either:
137280 138419   **
137281 138420   **    (A)   N = yy_action[ yy_shift_ofst[S] + X ]
137282 138421   **    (B)   N = yy_default[S]
137283 138422   **
137284         -** The (A) formula is preferred.  The B formula is used instead if:
137285         -**    (1)  The yy_shift_ofst[S]+X value is out of range, or
137286         -**    (2)  yy_lookahead[yy_shift_ofst[S]+X] is not equal to X, or
137287         -**    (3)  yy_shift_ofst[S] equal YY_SHIFT_USE_DFLT.
137288         -** (Implementation note: YY_SHIFT_USE_DFLT is chosen so that
137289         -** YY_SHIFT_USE_DFLT+X will be out of range for all possible lookaheads X.
137290         -** Hence only tests (1) and (2) need to be evaluated.)
       138423  +** The (A) formula is preferred.  The B formula is used instead if
       138424  +** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
137291 138425   **
137292 138426   ** The formulas above are for computing the action when the lookahead is
137293 138427   ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
137294 138428   ** a reduce action) then the yy_reduce_ofst[] array is used in place of
137295         -** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
137296         -** YY_SHIFT_USE_DFLT.
       138429  +** the yy_shift_ofst[] array.
137297 138430   **
137298 138431   ** The following are the tables generated in this section:
137299 138432   **
137300 138433   **  yy_action[]        A single table containing all actions.
137301 138434   **  yy_lookahead[]     A table containing the lookahead for each entry in
137302 138435   **                     yy_action.  Used to detect hash collisions.
137303 138436   **  yy_shift_ofst[]    For each state, the offset into yy_action for
137304 138437   **                     shifting terminals.
137305 138438   **  yy_reduce_ofst[]   For each state, the offset into yy_action for
137306 138439   **                     shifting non-terminals after a reduce.
137307 138440   **  yy_default[]       Default action for each state.
137308 138441   **
137309 138442   *********** Begin parsing tables **********************************************/
137310         -#define YY_ACTTAB_COUNT (1566)
       138443  +#define YY_ACTTAB_COUNT (1541)
137311 138444   static const YYACTIONTYPE yy_action[] = {
137312         - /*     0 */   324, 1323,  155,  155,    2,  203,   94,   94,   94,   93,
137313         - /*    10 */   350,   98,   98,   98,   98,   91,   95,   95,   94,   94,
137314         - /*    20 */    94,   93,  350,  268,   99,  100,   90,  971,  971,  847,
137315         - /*    30 */   850,  839,  839,   97,   97,   98,   98,   98,   98,  350,
137316         - /*    40 */   969,   96,   96,   96,   96,   95,   95,   94,   94,   94,
137317         - /*    50 */    93,  350,  950,   96,   96,   96,   96,   95,   95,   94,
137318         - /*    60 */    94,   94,   93,  350,  250,   96,   96,   96,   96,   95,
137319         - /*    70 */    95,   94,   94,   94,   93,  350,  224,  224,  969,  132,
137320         - /*    80 */   888,  348,  347,  415,  172,  324, 1286,  449,  414,  950,
137321         - /*    90 */   951,  952,  808,  977, 1032,  950,  300,  786,  428,  132,
137322         - /*   100 */   975,  362,  976,    9,    9,  787,  132,   52,   52,   99,
137323         - /*   110 */   100,   90,  971,  971,  847,  850,  839,  839,   97,   97,
137324         - /*   120 */    98,   98,   98,   98,  372,  978,  241,  978,  262,  369,
137325         - /*   130 */   261,  120,  950,  951,  952,  194,   58,  324,  401,  398,
137326         - /*   140 */   397,  808,  427,  429,   75,  808, 1260, 1260,  132,  396,
137327         - /*   150 */    96,   96,   96,   96,   95,   95,   94,   94,   94,   93,
137328         - /*   160 */   350,   99,  100,   90,  971,  971,  847,  850,  839,  839,
137329         - /*   170 */    97,   97,   98,   98,   98,   98,  786,  262,  369,  261,
137330         - /*   180 */   826,  262,  364,  251,  787, 1084,  101, 1114,   72,  324,
137331         - /*   190 */   227, 1113,  242,  411,  442,  819,   92,   89,  178,  818,
137332         - /*   200 */  1022,  268,   96,   96,   96,   96,   95,   95,   94,   94,
137333         - /*   210 */    94,   93,  350,   99,  100,   90,  971,  971,  847,  850,
137334         - /*   220 */   839,  839,   97,   97,   98,   98,   98,   98,  449,  372,
137335         - /*   230 */   818,  818,  820,   92,   89,  178,   60,   92,   89,  178,
137336         - /*   240 */  1025,  324,  357,  930, 1316,  300,   61, 1316,   52,   52,
137337         - /*   250 */   836,  836,  848,  851,   96,   96,   96,   96,   95,   95,
137338         - /*   260 */    94,   94,   94,   93,  350,   99,  100,   90,  971,  971,
137339         - /*   270 */   847,  850,  839,  839,   97,   97,   98,   98,   98,   98,
137340         - /*   280 */    92,   89,  178,  427,  412,  198,  930, 1317,  454,  995,
137341         - /*   290 */  1317,  355, 1024,  324,  243,  231,  114,  277,  348,  347,
137342         - /*   300 */  1242,  950,  416, 1071,  928,  840,   96,   96,   96,   96,
137343         - /*   310 */    95,   95,   94,   94,   94,   93,  350,   99,  100,   90,
137344         - /*   320 */   971,  971,  847,  850,  839,  839,   97,   97,   98,   98,
137345         - /*   330 */    98,   98,  449,  328,  449,  120,   23,  256,  950,  951,
137346         - /*   340 */   952,  968,  978,  438,  978,  324,  329,  928,  954,  701,
137347         - /*   350 */   200,  175,   52,   52,   52,   52,  939,  353,   96,   96,
137348         - /*   360 */    96,   96,   95,   95,   94,   94,   94,   93,  350,   99,
137349         - /*   370 */   100,   90,  971,  971,  847,  850,  839,  839,   97,   97,
137350         - /*   380 */    98,   98,   98,   98,  354,  449,  954,  427,  417,  427,
137351         - /*   390 */   426, 1290,   92,   89,  178,  268,  253,  324,  255, 1058,
137352         - /*   400 */  1037,  694,   93,  350,  383,   52,   52,  380, 1058,  374,
137353         - /*   410 */    96,   96,   96,   96,   95,   95,   94,   94,   94,   93,
137354         - /*   420 */   350,   99,  100,   90,  971,  971,  847,  850,  839,  839,
137355         - /*   430 */    97,   97,   98,   98,   98,   98,  228,  449,  167,  449,
137356         - /*   440 */   427,  407,  157,  446,  446,  446,  349,  349,  349,  324,
137357         - /*   450 */   310,  316,  991,  827,  320,  242,  411,   51,   51,   36,
137358         - /*   460 */    36,  254,   96,   96,   96,   96,   95,   95,   94,   94,
137359         - /*   470 */    94,   93,  350,   99,  100,   90,  971,  971,  847,  850,
137360         - /*   480 */   839,  839,   97,   97,   98,   98,   98,   98,  194,  316,
137361         - /*   490 */   929,  401,  398,  397,  224,  224, 1265,  939,  353, 1318,
137362         - /*   500 */   317,  324,  396, 1063, 1063,  813,  414, 1061, 1061,  950,
137363         - /*   510 */   299,  448,  992,  268,   96,   96,   96,   96,   95,   95,
137364         - /*   520 */    94,   94,   94,   93,  350,   99,  100,   90,  971,  971,
137365         - /*   530 */   847,  850,  839,  839,   97,   97,   98,   98,   98,   98,
137366         - /*   540 */   757, 1041,  449,  893,  893,  386,  950,  951,  952,  410,
137367         - /*   550 */   992,  747,  747,  324,  229,  268,  221,  296,  268,  771,
137368         - /*   560 */   890,  378,   52,   52,  890,  421,   96,   96,   96,   96,
137369         - /*   570 */    95,   95,   94,   94,   94,   93,  350,   99,  100,   90,
137370         - /*   580 */   971,  971,  847,  850,  839,  839,   97,   97,   98,   98,
137371         - /*   590 */    98,   98,  103,  449,  275,  384, 1241,  343,  157, 1207,
137372         - /*   600 */   909,  669,  670,  671,  176,  197,  196,  195,  324,  298,
137373         - /*   610 */   319, 1266,    2,   37,   37,  910, 1134, 1040,   96,   96,
137374         - /*   620 */    96,   96,   95,   95,   94,   94,   94,   93,  350,  697,
137375         - /*   630 */   911,  177,   99,  100,   90,  971,  971,  847,  850,  839,
137376         - /*   640 */   839,   97,   97,   98,   98,   98,   98,  230,  146,  120,
137377         - /*   650 */   735, 1235,  826,  270, 1141,  273, 1141,  771,  171,  170,
137378         - /*   660 */   736, 1141,   82,  324,   80,  268,  697,  819,  158,  268,
137379         - /*   670 */   378,  818,   78,   96,   96,   96,   96,   95,   95,   94,
137380         - /*   680 */    94,   94,   93,  350,  120,  950,  393,   99,  100,   90,
137381         - /*   690 */   971,  971,  847,  850,  839,  839,   97,   97,   98,   98,
137382         - /*   700 */    98,   98,  818,  818,  820, 1141, 1070,  370,  331,  133,
137383         - /*   710 */  1066, 1141, 1250,  198,  268,  324, 1016,  330,  245,  333,
137384         - /*   720 */    24,  334,  950,  951,  952,  368,  335,   81,   96,   96,
137385         - /*   730 */    96,   96,   95,   95,   94,   94,   94,   93,  350,   99,
137386         - /*   740 */   100,   90,  971,  971,  847,  850,  839,  839,   97,   97,
137387         - /*   750 */    98,   98,   98,   98,  132,  267,  260,  445,  330,  223,
137388         - /*   760 */   175, 1289,  925,  752,  724,  318, 1073,  324,  751,  246,
137389         - /*   770 */   385,  301,  301,  378,  329,  361,  344,  414, 1233,  280,
137390         - /*   780 */    96,   96,   96,   96,   95,   95,   94,   94,   94,   93,
137391         - /*   790 */   350,   99,   88,   90,  971,  971,  847,  850,  839,  839,
137392         - /*   800 */    97,   97,   98,   98,   98,   98,  337,  346,  721,  722,
137393         - /*   810 */   449,  120,  118,  887,  162,  887,  810,  371,  324,  202,
137394         - /*   820 */   202,  373,  249,  263,  202,  394,   74,  704,  208, 1069,
137395         - /*   830 */    12,   12,   96,   96,   96,   96,   95,   95,   94,   94,
137396         - /*   840 */    94,   93,  350,  100,   90,  971,  971,  847,  850,  839,
137397         - /*   850 */   839,   97,   97,   98,   98,   98,   98,  449,  771,  232,
137398         - /*   860 */   449,  278,  120,  286,   74,  704,  714,  713,  324,  342,
137399         - /*   870 */   749,  877, 1209,   77,  285, 1255,  780,   52,   52,  202,
137400         - /*   880 */    27,   27,  418,   96,   96,   96,   96,   95,   95,   94,
137401         - /*   890 */    94,   94,   93,  350,   90,  971,  971,  847,  850,  839,
137402         - /*   900 */   839,   97,   97,   98,   98,   98,   98,   86,  444,  877,
137403         - /*   910 */     3, 1193,  422, 1013,  873,  435,  886,  208,  886,  689,
137404         - /*   920 */  1091,  257,  116,  822,  447, 1230,  117, 1229,   86,  444,
137405         - /*   930 */   177,    3,  381,   96,   96,   96,   96,   95,   95,   94,
137406         - /*   940 */    94,   94,   93,  350,  339,  447,  120,  351,  120,  212,
137407         - /*   950 */   169,  287,  404,  282,  403,  199,  771,  950,  433,  419,
137408         - /*   960 */   439,  822,  280,  691, 1039,  264,  269,  132,  351,  153,
137409         - /*   970 */   826,  376,   74,  272,  274,  276,   83,   84, 1054,  433,
137410         - /*   980 */   147, 1038,  443,   85,  351,  451,  450,  281,  132,  818,
137411         - /*   990 */    25,  826,  449,  120,  950,  951,  952,   83,   84,   86,
137412         - /*  1000 */   444,  691,    3,  408,   85,  351,  451,  450,  449,    5,
137413         - /*  1010 */   818,  203,   32,   32, 1107,  120,  447,  950,  225, 1140,
137414         - /*  1020 */   818,  818,  820,  821,   19,  203,  226,  950,   38,   38,
137415         - /*  1030 */  1087,  314,  314,  313,  215,  311,  120,  449,  678,  351,
137416         - /*  1040 */   237,  818,  818,  820,  821,   19,  969,  409,  377,    1,
137417         - /*  1050 */   433,  180,  706,  248,  950,  951,  952,   10,   10,  449,
137418         - /*  1060 */   969,  247,  826, 1098,  950,  951,  952,  430,   83,   84,
137419         - /*  1070 */   756,  336,  950,   20,  431,   85,  351,  451,  450,   10,
137420         - /*  1080 */    10,  818,   86,  444,  969,    3,  950,  449,  302,  303,
137421         - /*  1090 */   182,  950, 1146,  338, 1021, 1015, 1004,  183,  969,  447,
137422         - /*  1100 */   132,  181,   76,  444,   21,    3,  449,   10,   10,  950,
137423         - /*  1110 */   951,  952,  818,  818,  820,  821,   19,  715, 1279,  447,
137424         - /*  1120 */   389,  233,  351,  950,  951,  952,   10,   10,  950,  951,
137425         - /*  1130 */   952, 1003,  218,  433, 1005,  325, 1273,  773,  289,  291,
137426         - /*  1140 */   424,  293,  351,    7,  159,  826,  363,  402,  315,  360,
137427         - /*  1150 */  1129,   83,   84,  433, 1232,  716,  772,  259,   85,  351,
137428         - /*  1160 */   451,  450,  358,  375,  818,  826,  360,  359,  399, 1211,
137429         - /*  1170 */   157,   83,   84,  681,   98,   98,   98,   98,   85,  351,
137430         - /*  1180 */   451,  450,  323,  252,  818,  295, 1211, 1213, 1235,  173,
137431         - /*  1190 */  1037,  284,  434,  340, 1204,  818,  818,  820,  821,   19,
137432         - /*  1200 */   308,  234,  449,  234,   96,   96,   96,   96,   95,   95,
137433         - /*  1210 */    94,   94,   94,   93,  350,  818,  818,  820,  821,   19,
137434         - /*  1220 */   909,  120,   39,   39, 1203,  449,  168,  360,  449, 1276,
137435         - /*  1230 */   367,  449,  135,  449,  986,  910,  449, 1249,  449, 1247,
137436         - /*  1240 */   449,  205,  983,  449,  370,   40,   40, 1211,   41,   41,
137437         - /*  1250 */   911,   42,   42,   28,   28,  870,   29,   29,   31,   31,
137438         - /*  1260 */    43,   43,  379,   44,   44,  449,   59,  449,  332,  449,
137439         - /*  1270 */   432,   62,  144,  156,  449,  130,  449,   72,  449,  137,
137440         - /*  1280 */   449,  365,  449,  392,  139,   45,   45,   11,   11,   46,
137441         - /*  1290 */    46,  140, 1200,  449,  105,  105,   47,   47,   48,   48,
137442         - /*  1300 */    33,   33,   49,   49, 1126,  449,  141,  366,  449,  185,
137443         - /*  1310 */   142,  449, 1234,   50,   50,  449,  160,  449,  148,  449,
137444         - /*  1320 */  1136,  382,  449,   67,  449,   34,   34,  449,  122,  122,
137445         - /*  1330 */   449,  123,  123,  449, 1198,  124,  124,   56,   56,   35,
137446         - /*  1340 */    35,  449,  106,  106,   53,   53,  449,  107,  107,  449,
137447         - /*  1350 */   108,  108,  449,  104,  104,  449,  406,  449,  388,  449,
137448         - /*  1360 */   189,  121,  121,  449,  190,  449,  119,  119,  449,  112,
137449         - /*  1370 */   112,  449,  111,  111, 1218,  109,  109,  110,  110,   55,
137450         - /*  1380 */    55,  266,  752,   57,   57,   54,   54,  751,   26,   26,
137451         - /*  1390 */  1099,   30,   30,  219,  154,  390,  271,  191,  321, 1006,
137452         - /*  1400 */   192,  405, 1057, 1056, 1055,  341, 1048,  706, 1047, 1029,
137453         - /*  1410 */   322,  420, 1028,   71, 1095,  283,  288, 1027, 1288,  204,
137454         - /*  1420 */     6,  297,   79, 1184,  437, 1096, 1094,  290,  345,  292,
137455         - /*  1430 */   441, 1093,  294,  102,  425,   73,  423,  213, 1012,   22,
137456         - /*  1440 */   452,  945,  214, 1077,  216,  217,  238,  453,  306,  304,
137457         - /*  1450 */   307,  239,  240, 1001,  305,  125,  996,  126,  115,  235,
137458         - /*  1460 */   127,  665,  352,  166,  244,  179,  356,  113,  885,  883,
137459         - /*  1470 */   806,  136,  128,  738,  326,  138,  327,  258,  184,  899,
137460         - /*  1480 */   143,  129,  145,   63,   64,   65,   66,  902,  186,  187,
137461         - /*  1490 */   898,    8,   13,  188,  134,  265,  891,  202,  980,  387,
137462         - /*  1500 */   150,  149,  680,  161,  391,  193,  285,  279,  395,  151,
137463         - /*  1510 */    68,  717,   14,   15,  400,   69,   16,  131,  236,  825,
137464         - /*  1520 */   824,  853,  746,  750,    4,   70,  174,  413,  220,  222,
137465         - /*  1530 */   152,  779,  774,   77,  868,   74,  854,  201,   17,  852,
137466         - /*  1540 */   908,  206,  907,  207,   18,  857,  934,  163,  436,  210,
137467         - /*  1550 */   935,  164,  209,  165,  440,  856,  823,  312,  690,   87,
137468         - /*  1560 */   211,  309, 1281,  940,  995, 1280,
       138445  + /*     0 */  1006,  156,  156,    2, 1302,   90,   87,  179,   90,   87,
       138446  + /*    10 */   179,  460, 1048,  460,  465, 1010,  460,  333, 1130,  335,
       138447  + /*    20 */   246,  330,  112,  303,  439, 1258,  304,  419, 1129, 1087,
       138448  + /*    30 */    72,  798,   50,   50,   50,   50,  331,   30,   30,  799,
       138449  + /*    40 */   951,  364,  371,   97,   98,   88,  983,  983,  859,  862,
       138450  + /*    50 */   851,  851,   95,   95,   96,   96,   96,   96,  120,  371,
       138451  + /*    60 */   370,  120,  348,   22,   90,   87,  179,  438,  423,  438,
       138452  + /*    70 */   440,  335,  420,  385,   90,   87,  179,  116,   73,  163,
       138453  + /*    80 */   848,  848,  860,  863,   94,   94,   94,   94,   93,   93,
       138454  + /*    90 */    92,   92,   92,   91,  361,   97,   98,   88,  983,  983,
       138455  + /*   100 */   859,  862,  851,  851,   95,   95,   96,   96,   96,   96,
       138456  + /*   110 */   718,  365,  339,   93,   93,   92,   92,   92,   91,  361,
       138457  + /*   120 */    99,  371,  453,  335,   94,   94,   94,   94,   93,   93,
       138458  + /*   130 */    92,   92,   92,   91,  361,  852,   94,   94,   94,   94,
       138459  + /*   140 */    93,   93,   92,   92,   92,   91,  361,   97,   98,   88,
       138460  + /*   150 */   983,  983,  859,  862,  851,  851,   95,   95,   96,   96,
       138461  + /*   160 */    96,   96,   92,   92,   92,   91,  361,  838,  132,  195,
       138462  + /*   170 */    58,  244,  412,  409,  408,  335,  457,  457,  457,  304,
       138463  + /*   180 */    59,  332,  831,  407,  394,  962,  830,  391,   94,   94,
       138464  + /*   190 */    94,   94,   93,   93,   92,   92,   92,   91,  361,   97,
       138465  + /*   200 */    98,   88,  983,  983,  859,  862,  851,  851,   95,   95,
       138466  + /*   210 */    96,   96,   96,   96,  426,  357,  460,  830,  830,  832,
       138467  + /*   220 */    91,  361,  962,  963,  964,  195,  459,  335,  412,  409,
       138468  + /*   230 */   408,  280,  361,  820,  132,   11,   11,   50,   50,  407,
       138469  + /*   240 */    94,   94,   94,   94,   93,   93,   92,   92,   92,   91,
       138470  + /*   250 */   361,   97,   98,   88,  983,  983,  859,  862,  851,  851,
       138471  + /*   260 */    95,   95,   96,   96,   96,   96,  460,  221,  460,  264,
       138472  + /*   270 */   375,  254,  438,  428, 1276, 1276,  383, 1074, 1053,  335,
       138473  + /*   280 */   245,  422,  299,  713,  271,  271, 1074,   50,   50,   50,
       138474  + /*   290 */    50,  962,   94,   94,   94,   94,   93,   93,   92,   92,
       138475  + /*   300 */    92,   91,  361,   97,   98,   88,  983,  983,  859,  862,
       138476  + /*   310 */   851,  851,   95,   95,   96,   96,   96,   96,   90,   87,
       138477  + /*   320 */   179, 1306,  438,  437,  438,  418,  368,  253,  962,  963,
       138478  + /*   330 */   964,  335,  360,  360,  360,  706,  359,  358,  324,  962,
       138479  + /*   340 */  1281,  951,  364,  230,   94,   94,   94,   94,   93,   93,
       138480  + /*   350 */    92,   92,   92,   91,  361,   97,   98,   88,  983,  983,
       138481  + /*   360 */   859,  862,  851,  851,   95,   95,   96,   96,   96,   96,
       138482  + /*   370 */   769,  460,  120,  226,  226,  366,  962,  963,  964, 1089,
       138483  + /*   380 */   990,  900,  990,  335, 1057,  425,  421,  839,  759,  759,
       138484  + /*   390 */   425,  427,   50,   50,  432,  381,   94,   94,   94,   94,
       138485  + /*   400 */    93,   93,   92,   92,   92,   91,  361,   97,   98,   88,
       138486  + /*   410 */   983,  983,  859,  862,  851,  851,   95,   95,   96,   96,
       138487  + /*   420 */    96,   96,  460,  259,  460,  120,  117,  354,  942, 1332,
       138488  + /*   430 */   942, 1333, 1332,  278, 1333,  335,  680,  681,  682,  825,
       138489  + /*   440 */   201,  176,  303,   50,   50,   49,   49,  404,   94,   94,
       138490  + /*   450 */    94,   94,   93,   93,   92,   92,   92,   91,  361,   97,
       138491  + /*   460 */    98,   88,  983,  983,  859,  862,  851,  851,   95,   95,
       138492  + /*   470 */    96,   96,   96,   96,  199,  460,  380,  265,  433,  380,
       138493  + /*   480 */   265,  383,  256,  158,  258,  319, 1003,  335,  155,  940,
       138494  + /*   490 */   177,  940,  273,  379,  276,  322,   34,   34,  302,  962,
       138495  + /*   500 */    94,   94,   94,   94,   93,   93,   92,   92,   92,   91,
       138496  + /*   510 */   361,   97,   98,   88,  983,  983,  859,  862,  851,  851,
       138497  + /*   520 */    95,   95,   96,   96,   96,   96,  905,  905,  397,  460,
       138498  + /*   530 */   301,  158,  101,  319,  941,  340,  962,  963,  964,  313,
       138499  + /*   540 */   283,  449,  335,  327,  146, 1266, 1004,  257,  234,  248,
       138500  + /*   550 */    35,   35,   94,   94,   94,   94,   93,   93,   92,   92,
       138501  + /*   560 */    92,   91,  361,  709,  785, 1227,   97,   98,   88,  983,
       138502  + /*   570 */   983,  859,  862,  851,  851,   95,   95,   96,   96,   96,
       138503  + /*   580 */    96,  962, 1227, 1229,  245,  422,  838,  198,  197,  196,
       138504  + /*   590 */  1079, 1079, 1077, 1077, 1004, 1334,  320,  335,  172,  171,
       138505  + /*   600 */   709,  831,  159,  271,  271,  830,   76,   94,   94,   94,
       138506  + /*   610 */    94,   93,   93,   92,   92,   92,   91,  361,  962,  963,
       138507  + /*   620 */   964,   97,   98,   88,  983,  983,  859,  862,  851,  851,
       138508  + /*   630 */    95,   95,   96,   96,   96,   96,  830,  830,  832, 1157,
       138509  + /*   640 */  1157,  199, 1157,  173, 1227,  231,  232, 1282,    2,  335,
       138510  + /*   650 */   271,  764,  271,  820,  271,  271,  763,  389,  389,  389,
       138511  + /*   660 */   132,   79,   94,   94,   94,   94,   93,   93,   92,   92,
       138512  + /*   670 */    92,   91,  361,   97,   98,   88,  983,  983,  859,  862,
       138513  + /*   680 */   851,  851,   95,   95,   96,   96,   96,   96,  460,  264,
       138514  + /*   690 */   223,  460, 1257,  783, 1223, 1157, 1086, 1082,   80,  271,
       138515  + /*   700 */    78,  335,  340, 1031,  341,  344,  345,  902,  346,   10,
       138516  + /*   710 */    10,  902,   25,   25,   94,   94,   94,   94,   93,   93,
       138517  + /*   720 */    92,   92,   92,   91,  361,   97,   86,   88,  983,  983,
       138518  + /*   730 */   859,  862,  851,  851,   95,   95,   96,   96,   96,   96,
       138519  + /*   740 */  1157,  270,  395,  117,  233,  263,  235,   70,  456,  341,
       138520  + /*   750 */   225,  176,  335, 1305,  342,  133,  736,  966,  980,  249,
       138521  + /*   760 */  1150,  396,  325, 1085, 1028,  178,   94,   94,   94,   94,
       138522  + /*   770 */    93,   93,   92,   92,   92,   91,  361,   98,   88,  983,
       138523  + /*   780 */   983,  859,  862,  851,  851,   95,   95,   96,   96,   96,
       138524  + /*   790 */    96,  783,  783,  132,  120,  966,  120,  120,  120,  798,
       138525  + /*   800 */   252,  937,  335,  353,  321,  429,  355,  799,  822,  692,
       138526  + /*   810 */   390,  203,  446,  450,  372,  716,  454,   94,   94,   94,
       138527  + /*   820 */    94,   93,   93,   92,   92,   92,   91,  361,   88,  983,
       138528  + /*   830 */   983,  859,  862,  851,  851,   95,   95,   96,   96,   96,
       138529  + /*   840 */    96,   84,  455, 1225,    3, 1209,  120,  120,  382,  387,
       138530  + /*   850 */   120,  203, 1271,  716,  384,  168,  266,  203,  458,   72,
       138531  + /*   860 */   260, 1246,   84,  455,  178,    3,  378,   94,   94,   94,
       138532  + /*   870 */    94,   93,   93,   92,   92,   92,   91,  361,  350,  458,
       138533  + /*   880 */  1245,  362,  430,  213,  228,  290,  415,  285,  414,  200,
       138534  + /*   890 */   783,  882,  444,  726,  725,  405,  283,  921,  209,  921,
       138535  + /*   900 */   281,  132,  362,   72,  838,  289,  147,  733,  734,  392,
       138536  + /*   910 */    81,   82,  922,  444,  922,  267,  288,   83,  362,  462,
       138537  + /*   920 */   461,  272,  132,  830,   23,  838,  388,  923, 1216,  923,
       138538  + /*   930 */  1056,   81,   82,   84,  455,  899,    3,  899,   83,  362,
       138539  + /*   940 */   462,  461,  761,  962,  830,   75,    1,  443,  275,  747,
       138540  + /*   950 */   458,    5,  962,  204,  830,  830,  832,  833,   18,  748,
       138541  + /*   960 */   229,  962,  277,   19,  153,  317,  317,  316,  216,  314,
       138542  + /*   970 */   279,  460,  689,  362, 1055,  830,  830,  832,  833,   18,
       138543  + /*   980 */   962,  963,  964,  962,  444,  181,  460,  251,  981,  962,
       138544  + /*   990 */   963,  964,    8,    8,   20,  250,  838, 1070,  962,  963,
       138545  + /*  1000 */   964,  417,   81,   82,  768,  204,  347,   36,   36,   83,
       138546  + /*  1010 */   362,  462,  461, 1054,  284,  830,   84,  455, 1123,    3,
       138547  + /*  1020 */   962,  963,  964,  460,  183,  962,  981,  764,  889, 1107,
       138548  + /*  1030 */   460,  184,  763,  458,  132,  182,   74,  455,  460,    3,
       138549  + /*  1040 */   981,  898,  834,  898,    8,    8,  830,  830,  832,  833,
       138550  + /*  1050 */    18,    8,    8,  458,  219, 1156,  362, 1103,  349,    8,
       138551  + /*  1060 */     8,  240,  962,  963,  964,  236,  889,  444,  792,  336,
       138552  + /*  1070 */   158,  203,  885,  435,  700,  209,  362,  114,  981,  838,
       138553  + /*  1080 */   834,  227,  334, 1114,  441,   81,   82,  444,  442,  305,
       138554  + /*  1090 */   784,  306,   83,  362,  462,  461,  369, 1162,  830,  838,
       138555  + /*  1100 */   460, 1037,  237, 1030,  237,   81,   82,    7,   96,   96,
       138556  + /*  1110 */    96,   96,   83,  362,  462,  461, 1019, 1018,  830, 1020,
       138557  + /*  1120 */  1289,   37,   37,  400,   96,   96,   96,   96,   89,  830,
       138558  + /*  1130 */   830,  832,  833,   18, 1100,  318,  962,  292,   94,   94,
       138559  + /*  1140 */    94,   94,   93,   93,   92,   92,   92,   91,  361,  830,
       138560  + /*  1150 */   830,  832,  833,   18,   94,   94,   94,   94,   93,   93,
       138561  + /*  1160 */    92,   92,   92,   91,  361,  359,  358,  226,  226,  727,
       138562  + /*  1170 */   294,  296,  460,  962,  963,  964,  460,  989,  160,  425,
       138563  + /*  1180 */   170, 1295,  262,  460,  987,  374,  988,  386, 1145,  255,
       138564  + /*  1190 */   326,  460,  373,   38,   38,  410,  174,   39,   39,  413,
       138565  + /*  1200 */   460,  287,  460, 1053,   40,   40,  298,  728, 1220,  990,
       138566  + /*  1210 */   445,  990,   26,   26, 1219,  460,  311,  460,  169, 1292,
       138567  + /*  1220 */   460,   27,   27,   29,   29,  998,  460,  206,  135,  995,
       138568  + /*  1230 */  1265, 1263,  460,   57,   60,  460,   41,   41,   42,   42,
       138569  + /*  1240 */   460,   43,   43,  460,  343,  351,  460,    9,    9,  460,
       138570  + /*  1250 */   144,  460,  130,   44,   44,  460,  103,  103,  460,  137,
       138571  + /*  1260 */    70,   45,   45,  460,   46,   46,  460,   31,   31, 1142,
       138572  + /*  1270 */    47,   47,   48,   48,  460,  376,   32,   32,  460,  122,
       138573  + /*  1280 */   122,  460,  157,  460,  123,  123,  139,  124,  124,  460,
       138574  + /*  1290 */   186,  460,  377,  460,  115,   54,   54,  460,  403,   33,
       138575  + /*  1300 */    33,  460,  104,  104,   51,   51,  460,  161,  460,  140,
       138576  + /*  1310 */   105,  105,  106,  106,  102,  102,  460,  141,  121,  121,
       138577  + /*  1320 */   460,  142,  119,  119,  190,  460, 1152,  110,  110,  109,
       138578  + /*  1330 */   109,  702,  460,  148,  393,   65,  460,  107,  107,  460,
       138579  + /*  1340 */   323,  108,  108,  399,  460, 1234,   53,   53, 1214,  269,
       138580  + /*  1350 */   154,  416, 1115,   55,   55,  220,  401,   52,   52,  191,
       138581  + /*  1360 */    24,   24,  274,  192,  193,   28,   28, 1021,  328,  702,
       138582  + /*  1370 */  1073,  352, 1072,  718, 1071,  431, 1111, 1064,  329, 1045,
       138583  + /*  1380 */    69,  205,    6,  291, 1044,  286, 1112, 1043, 1304, 1110,
       138584  + /*  1390 */   293,  300,  295,  297, 1063, 1200, 1109,   77,  241,  448,
       138585  + /*  1400 */   356,  452,  436,  100,  214,   71,  434, 1027, 1093,   21,
       138586  + /*  1410 */   463,  242,  243,  957,  215,  217,  218,  464,  309,  307,
       138587  + /*  1420 */   308,  310, 1016,  125, 1250, 1251, 1011, 1249,  126,  127,
       138588  + /*  1430 */  1248,  113,  676,  337,  238,  338,  134,  363,  167, 1041,
       138589  + /*  1440 */  1040,   56,  247,  367,  180,  897,  111,  895,  136, 1038,
       138590  + /*  1450 */   818,  128,  138,  750,  261,  911,  185,  143,  145,   61,
       138591  + /*  1460 */    62,   63,   64,  129,  914,  187,  188,  910,  118,   12,
       138592  + /*  1470 */   189,  903,  268,  992,  203,  162,  398,  150,  149,  691,
       138593  + /*  1480 */   402,  288,  194,  406,  151,  411,   66,   13,  729,  239,
       138594  + /*  1490 */   282,   14,   67,  131,  837,  836,  865,  758,   15,    4,
       138595  + /*  1500 */    68,  762,  175,  222,  224,  424,  152,  869,  791,  202,
       138596  + /*  1510 */   786,   75,   72,  880,  866,  864,   16,   17,  920,  207,
       138597  + /*  1520 */   919,  208,  447,  946,  164,  211,  947,  210,  165,  451,
       138598  + /*  1530 */   868,  166,  315,  835,  701,   85,  212, 1297,  312,  952,
       138599  + /*  1540 */  1296,
137469 138600   };
137470 138601   static const YYCODETYPE yy_lookahead[] = {
137471         - /*     0 */    19,  144,  145,  146,  147,   24,   90,   91,   92,   93,
137472         - /*    10 */    94,   54,   55,   56,   57,   58,   88,   89,   90,   91,
137473         - /*    20 */    92,   93,   94,  152,   43,   44,   45,   46,   47,   48,
137474         - /*    30 */    49,   50,   51,   52,   53,   54,   55,   56,   57,   94,
137475         - /*    40 */    59,   84,   85,   86,   87,   88,   89,   90,   91,   92,
137476         - /*    50 */    93,   94,   59,   84,   85,   86,   87,   88,   89,   90,
137477         - /*    60 */    91,   92,   93,   94,  193,   84,   85,   86,   87,   88,
137478         - /*    70 */    89,   90,   91,   92,   93,   94,  194,  195,   97,   79,
137479         - /*    80 */    11,   88,   89,  152,   26,   19,  171,  152,  206,   96,
137480         - /*    90 */    97,   98,   72,  100,  179,   59,  152,   31,  163,   79,
137481         - /*   100 */   107,  219,  109,  172,  173,   39,   79,  172,  173,   43,
137482         - /*   110 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
137483         - /*   120 */    54,   55,   56,   57,  152,  132,  199,  134,  108,  109,
137484         - /*   130 */   110,  196,   96,   97,   98,   99,  209,   19,  102,  103,
137485         - /*   140 */   104,   72,  207,  208,   26,   72,  119,  120,   79,  113,
137486         - /*   150 */    84,   85,   86,   87,   88,   89,   90,   91,   92,   93,
137487         - /*   160 */    94,   43,   44,   45,   46,   47,   48,   49,   50,   51,
137488         - /*   170 */    52,   53,   54,   55,   56,   57,   31,  108,  109,  110,
137489         - /*   180 */    82,  108,  109,  110,   39,  210,   68,  175,  130,   19,
137490         - /*   190 */   218,  175,  119,  120,  250,   97,  221,  222,  223,  101,
137491         - /*   200 */   172,  152,   84,   85,   86,   87,   88,   89,   90,   91,
137492         - /*   210 */    92,   93,   94,   43,   44,   45,   46,   47,   48,   49,
137493         - /*   220 */    50,   51,   52,   53,   54,   55,   56,   57,  152,  152,
137494         - /*   230 */   132,  133,  134,  221,  222,  223,   66,  221,  222,  223,
137495         - /*   240 */   172,   19,  193,   22,   23,  152,   24,   26,  172,  173,
137496         - /*   250 */    46,   47,   48,   49,   84,   85,   86,   87,   88,   89,
137497         - /*   260 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
137498         - /*   270 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
137499         - /*   280 */   221,  222,  223,  207,  208,   46,   22,   23,  148,  149,
137500         - /*   290 */    26,  242,  172,   19,  154,  218,  156,   23,   88,   89,
137501         - /*   300 */   241,   59,  163,  163,   83,  101,   84,   85,   86,   87,
137502         - /*   310 */    88,   89,   90,   91,   92,   93,   94,   43,   44,   45,
137503         - /*   320 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
137504         - /*   330 */    56,   57,  152,  157,  152,  196,  196,   16,   96,   97,
137505         - /*   340 */    98,   26,  132,  250,  134,   19,  107,   83,   59,   23,
137506         - /*   350 */   211,  212,  172,  173,  172,  173,    1,    2,   84,   85,
137507         - /*   360 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   43,
137508         - /*   370 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
137509         - /*   380 */    54,   55,   56,   57,  244,  152,   97,  207,  208,  207,
137510         - /*   390 */   208,  185,  221,  222,  223,  152,   75,   19,   77,  179,
137511         - /*   400 */   180,   23,   93,   94,  228,  172,  173,  231,  188,  152,
137512         - /*   410 */    84,   85,   86,   87,   88,   89,   90,   91,   92,   93,
137513         - /*   420 */    94,   43,   44,   45,   46,   47,   48,   49,   50,   51,
137514         - /*   430 */    52,   53,   54,   55,   56,   57,  193,  152,  123,  152,
137515         - /*   440 */   207,  208,  152,  168,  169,  170,  168,  169,  170,   19,
137516         - /*   450 */   160,   22,   23,   23,  164,  119,  120,  172,  173,  172,
137517         - /*   460 */   173,  140,   84,   85,   86,   87,   88,   89,   90,   91,
137518         - /*   470 */    92,   93,   94,   43,   44,   45,   46,   47,   48,   49,
137519         - /*   480 */    50,   51,   52,   53,   54,   55,   56,   57,   99,   22,
137520         - /*   490 */    23,  102,  103,  104,  194,  195,    0,    1,    2,  247,
137521         - /*   500 */   248,   19,  113,  190,  191,   23,  206,  190,  191,   59,
137522         - /*   510 */   225,  152,   83,  152,   84,   85,   86,   87,   88,   89,
137523         - /*   520 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
137524         - /*   530 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
137525         - /*   540 */    90,  181,  152,  108,  109,  110,   96,   97,   98,  115,
137526         - /*   550 */    83,  117,  118,   19,  193,  152,   23,  152,  152,   26,
137527         - /*   560 */    29,  152,  172,  173,   33,  152,   84,   85,   86,   87,
137528         - /*   570 */    88,   89,   90,   91,   92,   93,   94,   43,   44,   45,
137529         - /*   580 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
137530         - /*   590 */    56,   57,   22,  152,   16,   64,  193,  207,  152,  193,
137531         - /*   600 */    12,    7,    8,    9,  152,  108,  109,  110,   19,  152,
137532         - /*   610 */   164,  146,  147,  172,  173,   27,  163,  181,   84,   85,
137533         - /*   620 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   59,
137534         - /*   630 */    42,   98,   43,   44,   45,   46,   47,   48,   49,   50,
137535         - /*   640 */    51,   52,   53,   54,   55,   56,   57,  238,   22,  196,
137536         - /*   650 */    62,  163,   82,   75,  152,   77,  152,  124,   88,   89,
137537         - /*   660 */    72,  152,  137,   19,  139,  152,   96,   97,   24,  152,
137538         - /*   670 */   152,  101,  138,   84,   85,   86,   87,   88,   89,   90,
137539         - /*   680 */    91,   92,   93,   94,  196,   59,   19,   43,   44,   45,
137540         - /*   690 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
137541         - /*   700 */    56,   57,  132,  133,  134,  152,  193,  219,  245,  246,
137542         - /*   710 */   193,  152,  152,   46,  152,   19,  166,  167,  152,  217,
137543         - /*   720 */   232,  217,   96,   97,   98,  237,  217,  138,   84,   85,
137544         - /*   730 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   43,
137545         - /*   740 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
137546         - /*   750 */    54,   55,   56,   57,   79,  193,  238,  166,  167,  211,
137547         - /*   760 */   212,   23,   23,  116,   26,   26,  195,   19,  121,  152,
137548         - /*   770 */   217,  152,  152,  152,  107,  100,  217,  206,  163,  112,
137549         - /*   780 */    84,   85,   86,   87,   88,   89,   90,   91,   92,   93,
137550         - /*   790 */    94,   43,   44,   45,   46,   47,   48,   49,   50,   51,
137551         - /*   800 */    52,   53,   54,   55,   56,   57,  187,  187,    7,    8,
137552         - /*   810 */   152,  196,   22,  132,   24,  134,   23,   23,   19,   26,
137553         - /*   820 */    26,   23,  152,   23,   26,   23,   26,   59,   26,  163,
137554         - /*   830 */   172,  173,   84,   85,   86,   87,   88,   89,   90,   91,
137555         - /*   840 */    92,   93,   94,   44,   45,   46,   47,   48,   49,   50,
137556         - /*   850 */    51,   52,   53,   54,   55,   56,   57,  152,   26,  238,
137557         - /*   860 */   152,   23,  196,  101,   26,   97,  100,  101,   19,   19,
137558         - /*   870 */    23,   59,  152,   26,  112,  152,   23,  172,  173,   26,
137559         - /*   880 */   172,  173,   19,   84,   85,   86,   87,   88,   89,   90,
137560         - /*   890 */    91,   92,   93,   94,   45,   46,   47,   48,   49,   50,
137561         - /*   900 */    51,   52,   53,   54,   55,   56,   57,   19,   20,   97,
137562         - /*   910 */    22,   23,  207,  163,   23,  163,  132,   26,  134,   23,
137563         - /*   920 */   213,  152,   26,   59,   36,  152,   22,  152,   19,   20,
137564         - /*   930 */    98,   22,  152,   84,   85,   86,   87,   88,   89,   90,
137565         - /*   940 */    91,   92,   93,   94,   94,   36,  196,   59,  196,   99,
137566         - /*   950 */   100,  101,  102,  103,  104,  105,  124,   59,   70,   96,
137567         - /*   960 */   163,   97,  112,   59,  181,  152,  152,   79,   59,   71,
137568         - /*   970 */    82,   19,   26,  152,  152,  152,   88,   89,  152,   70,
137569         - /*   980 */    22,  152,  163,   95,   96,   97,   98,  152,   79,  101,
137570         - /*   990 */    22,   82,  152,  196,   96,   97,   98,   88,   89,   19,
137571         - /*  1000 */    20,   97,   22,  163,   95,   96,   97,   98,  152,   22,
137572         - /*  1010 */   101,   24,  172,  173,  152,  196,   36,   59,   22,  152,
137573         - /*  1020 */   132,  133,  134,  135,  136,   24,    5,   59,  172,  173,
137574         - /*  1030 */   152,   10,   11,   12,   13,   14,  196,  152,   17,   59,
137575         - /*  1040 */   210,  132,  133,  134,  135,  136,   59,  207,   96,   22,
137576         - /*  1050 */    70,   30,  106,   32,   96,   97,   98,  172,  173,  152,
137577         - /*  1060 */    59,   40,   82,  152,   96,   97,   98,  152,   88,   89,
137578         - /*  1070 */    90,  186,   59,   22,  191,   95,   96,   97,   98,  172,
137579         - /*  1080 */   173,  101,   19,   20,   97,   22,   59,  152,  152,  152,
137580         - /*  1090 */    69,   59,  152,  186,  152,  152,  152,   76,   97,   36,
137581         - /*  1100 */    79,   80,   19,   20,   53,   22,  152,  172,  173,   96,
137582         - /*  1110 */    97,   98,  132,  133,  134,  135,  136,   35,  122,   36,
137583         - /*  1120 */   234,  186,   59,   96,   97,   98,  172,  173,   96,   97,
137584         - /*  1130 */    98,  152,  233,   70,  152,  114,  152,  124,  210,  210,
137585         - /*  1140 */   186,  210,   59,  198,  197,   82,  214,   65,  150,  152,
137586         - /*  1150 */   201,   88,   89,   70,  201,   73,  124,  239,   95,   96,
137587         - /*  1160 */    97,   98,  141,  239,  101,   82,  169,  170,  176,  152,
137588         - /*  1170 */   152,   88,   89,   21,   54,   55,   56,   57,   95,   96,
137589         - /*  1180 */    97,   98,  164,  214,  101,  214,  169,  170,  163,  184,
137590         - /*  1190 */   180,  175,  227,  111,  175,  132,  133,  134,  135,  136,
137591         - /*  1200 */   200,  183,  152,  185,   84,   85,   86,   87,   88,   89,
137592         - /*  1210 */    90,   91,   92,   93,   94,  132,  133,  134,  135,  136,
137593         - /*  1220 */    12,  196,  172,  173,  175,  152,  198,  230,  152,  155,
137594         - /*  1230 */    78,  152,  243,  152,   60,   27,  152,  159,  152,  159,
137595         - /*  1240 */   152,  122,   38,  152,  219,  172,  173,  230,  172,  173,
137596         - /*  1250 */    42,  172,  173,  172,  173,  103,  172,  173,  172,  173,
137597         - /*  1260 */   172,  173,  237,  172,  173,  152,  240,  152,  159,  152,
137598         - /*  1270 */    62,  240,   22,  220,  152,   43,  152,  130,  152,  189,
137599         - /*  1280 */   152,   18,  152,   18,  192,  172,  173,  172,  173,  172,
137600         - /*  1290 */   173,  192,  140,  152,  172,  173,  172,  173,  172,  173,
137601         - /*  1300 */   172,  173,  172,  173,  201,  152,  192,  159,  152,  158,
137602         - /*  1310 */   192,  152,  201,  172,  173,  152,  220,  152,  189,  152,
137603         - /*  1320 */   189,  159,  152,  137,  152,  172,  173,  152,  172,  173,
137604         - /*  1330 */   152,  172,  173,  152,  201,  172,  173,  172,  173,  172,
137605         - /*  1340 */   173,  152,  172,  173,  172,  173,  152,  172,  173,  152,
137606         - /*  1350 */   172,  173,  152,  172,  173,  152,   90,  152,   61,  152,
137607         - /*  1360 */   158,  172,  173,  152,  158,  152,  172,  173,  152,  172,
137608         - /*  1370 */   173,  152,  172,  173,  236,  172,  173,  172,  173,  172,
137609         - /*  1380 */   173,  235,  116,  172,  173,  172,  173,  121,  172,  173,
137610         - /*  1390 */   159,  172,  173,  159,   22,  177,  159,  158,  177,  159,
137611         - /*  1400 */   158,  107,  174,  174,  174,   63,  182,  106,  182,  174,
137612         - /*  1410 */   177,  125,  176,  107,  216,  174,  215,  174,  174,  159,
137613         - /*  1420 */    22,  159,  137,  224,  177,  216,  216,  215,   94,  215,
137614         - /*  1430 */   177,  216,  215,  129,  126,  128,  127,   25,  162,   26,
137615         - /*  1440 */   161,   13,  153,  205,  153,    6,  226,  151,  202,  204,
137616         - /*  1450 */   201,  229,  229,  151,  203,  165,  151,  165,  178,  178,
137617         - /*  1460 */   165,    4,    3,   22,  142,   15,   81,   16,   23,   23,
137618         - /*  1470 */   120,  131,  111,   20,  249,  123,  249,   16,  125,    1,
137619         - /*  1480 */   123,  111,  131,   53,   53,   53,   53,   96,   34,  122,
137620         - /*  1490 */     1,    5,   22,  107,  246,  140,   67,   26,   74,   41,
137621         - /*  1500 */   107,   67,   20,   24,   19,  105,  112,   23,   66,   22,
137622         - /*  1510 */    22,   28,   22,   22,   66,   22,   22,   37,   66,   23,
137623         - /*  1520 */    23,   23,  116,   23,   22,   26,  122,   26,   23,   23,
137624         - /*  1530 */    22,   96,  124,   26,   23,   26,   23,   34,   34,   23,
137625         - /*  1540 */    23,   26,   23,   22,   34,   11,   23,   22,   24,  122,
137626         - /*  1550 */    23,   22,   26,   22,   24,   23,   23,   15,   23,   22,
137627         - /*  1560 */   122,   23,  122,    1,  251,  122,
137628         -};
137629         -#define YY_SHIFT_USE_DFLT (1566)
137630         -#define YY_SHIFT_COUNT    (454)
137631         -#define YY_SHIFT_MIN      (-84)
137632         -#define YY_SHIFT_MAX      (1562)
137633         -static const short yy_shift_ofst[] = {
137634         - /*     0 */   355,  888, 1021,  909, 1063, 1063, 1063, 1063,   20,  -19,
137635         - /*    10 */    66,   66,  170, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
137636         - /*    20 */    -7,   -7,   36,   73,   69,   27,  118,  222,  274,  326,
137637         - /*    30 */   378,  430,  482,  534,  589,  644,  696,  696,  696,  696,
137638         - /*    40 */   696,  696,  696,  696,  696,  696,  696,  696,  696,  696,
137639         - /*    50 */   696,  696,  696,  748,  696,  799,  849,  849,  980, 1063,
137640         - /*    60 */  1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
137641         - /*    70 */  1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
137642         - /*    80 */  1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
137643         - /*    90 */  1083, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
137644         - /*   100 */  1063, 1063, 1063, 1063,  -43, 1120, 1120, 1120, 1120, 1120,
137645         - /*   110 */   -31,  -72,  -84,  242, 1152,  667,  210,  210,  242,  309,
137646         - /*   120 */   336,  -55, 1566, 1566, 1566,  850,  850,  850,  626,  626,
137647         - /*   130 */   588,  588,  898,  221,  264,  242,  242,  242,  242,  242,
137648         - /*   140 */   242,  242,  242,  242,  242,  242,  242,  242,  242,  242,
137649         - /*   150 */   242,  242,  242,  242,  242,  496,  675,  289,  289,  336,
137650         - /*   160 */     0,    0,    0,    0,    0,    0, 1566, 1566, 1566,  570,
137651         - /*   170 */    98,   98,  958,  389,  450,  968, 1013, 1032, 1027,  242,
137652         - /*   180 */   242,  242,  242,  242,  242,  242,  242,  242,  242,  242,
137653         - /*   190 */   242,  242,  242,  242,  242, 1082, 1082, 1082,  242,  242,
137654         - /*   200 */   533,  242,  242,  242,  987,  242,  242, 1208,  242,  242,
137655         - /*   210 */   242,  242,  242,  242,  242,  242,  242,  242,  435,  531,
137656         - /*   220 */  1001, 1001, 1001,  832,  434, 1266,  594,   58,  863,  863,
137657         - /*   230 */   952,   58,  952,  946,  738,  239,  145,  863,  525,  145,
137658         - /*   240 */   145,  315,  647,  790, 1174, 1119, 1119, 1204, 1204, 1119,
137659         - /*   250 */  1250, 1232, 1147, 1263, 1263, 1263, 1263, 1119, 1265, 1147,
137660         - /*   260 */  1250, 1232, 1232, 1147, 1119, 1265, 1186, 1297, 1119, 1119,
137661         - /*   270 */  1265, 1372, 1119, 1265, 1119, 1265, 1372, 1294, 1294, 1294,
137662         - /*   280 */  1342, 1372, 1294, 1301, 1294, 1342, 1294, 1294, 1286, 1306,
137663         - /*   290 */  1286, 1306, 1286, 1306, 1286, 1306, 1119, 1398, 1119, 1285,
137664         - /*   300 */  1372, 1334, 1334, 1372, 1304, 1308, 1307, 1309, 1147, 1412,
137665         - /*   310 */  1413, 1428, 1428, 1439, 1439, 1439, 1566, 1566, 1566, 1566,
137666         - /*   320 */  1566, 1566, 1566, 1566,  204,  321,  429,  467,  578,  497,
137667         - /*   330 */   904,  739, 1051,  793,  794,  798,  800,  802,  838,  768,
137668         - /*   340 */   766,  801,  762,  847,  853,  812,  891,  681,  784,  896,
137669         - /*   350 */   864,  996, 1457, 1459, 1441, 1322, 1450, 1385, 1451, 1445,
137670         - /*   360 */  1446, 1350, 1340, 1361, 1352, 1453, 1353, 1461, 1478, 1357,
137671         - /*   370 */  1351, 1430, 1431, 1432, 1433, 1370, 1391, 1454, 1367, 1489,
137672         - /*   380 */  1486, 1470, 1386, 1355, 1429, 1471, 1434, 1424, 1458, 1393,
137673         - /*   390 */  1479, 1482, 1485, 1394, 1400, 1487, 1442, 1488, 1490, 1484,
137674         - /*   400 */  1491, 1448, 1483, 1493, 1452, 1480, 1496, 1497, 1498, 1499,
137675         - /*   410 */  1406, 1494, 1500, 1502, 1501, 1404, 1505, 1506, 1435, 1503,
137676         - /*   420 */  1508, 1408, 1507, 1504, 1509, 1510, 1511, 1507, 1513, 1516,
137677         - /*   430 */  1517, 1515, 1519, 1521, 1534, 1523, 1525, 1524, 1526, 1527,
137678         - /*   440 */  1529, 1530, 1526, 1532, 1531, 1533, 1535, 1537, 1427, 1438,
137679         - /*   450 */  1440, 1443, 1538, 1542, 1562,
137680         -};
137681         -#define YY_REDUCE_USE_DFLT (-144)
137682         -#define YY_REDUCE_COUNT (323)
137683         -#define YY_REDUCE_MIN   (-143)
137684         -#define YY_REDUCE_MAX   (1305)
       138602  + /*     0 */   144,  145,  146,  147,  172,  222,  223,  224,  222,  223,
       138603  + /*    10 */   224,  152,  180,  152,  148,  149,  152,  173,  176,   19,
       138604  + /*    20 */   154,  173,  156,  152,  163,  242,  152,  163,  176,  163,
       138605  + /*    30 */    26,   31,  173,  174,  173,  174,  173,  173,  174,   39,
       138606  + /*    40 */     1,    2,  152,   43,   44,   45,   46,   47,   48,   49,
       138607  + /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  197,  169,
       138608  + /*    60 */   170,  197,  188,  197,  222,  223,  224,  208,  209,  208,
       138609  + /*    70 */   209,   19,  208,  152,  222,  223,  224,   22,   26,   24,
       138610  + /*    80 */    46,   47,   48,   49,   84,   85,   86,   87,   88,   89,
       138611  + /*    90 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
       138612  + /*   100 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
       138613  + /*   110 */   106,  245,  157,   88,   89,   90,   91,   92,   93,   94,
       138614  + /*   120 */    68,  231,  251,   19,   84,   85,   86,   87,   88,   89,
       138615  + /*   130 */    90,   91,   92,   93,   94,  101,   84,   85,   86,   87,
       138616  + /*   140 */    88,   89,   90,   91,   92,   93,   94,   43,   44,   45,
       138617  + /*   150 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       138618  + /*   160 */    56,   57,   90,   91,   92,   93,   94,   82,   79,   99,
       138619  + /*   170 */    66,  200,  102,  103,  104,   19,  168,  169,  170,  152,
       138620  + /*   180 */    24,  210,   97,  113,  229,   59,  101,  232,   84,   85,
       138621  + /*   190 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   43,
       138622  + /*   200 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       138623  + /*   210 */    54,   55,   56,   57,  152,  188,  152,  132,  133,  134,
       138624  + /*   220 */    93,   94,   96,   97,   98,   99,  152,   19,  102,  103,
       138625  + /*   230 */   104,   23,   94,   72,   79,  173,  174,  173,  174,  113,
       138626  + /*   240 */    84,   85,   86,   87,   88,   89,   90,   91,   92,   93,
       138627  + /*   250 */    94,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       138628  + /*   260 */    52,   53,   54,   55,   56,   57,  152,  171,  152,  108,
       138629  + /*   270 */   109,  110,  208,  209,  119,  120,  152,  180,  181,   19,
       138630  + /*   280 */   119,  120,  152,   23,  152,  152,  189,  173,  174,  173,
       138631  + /*   290 */   174,   59,   84,   85,   86,   87,   88,   89,   90,   91,
       138632  + /*   300 */    92,   93,   94,   43,   44,   45,   46,   47,   48,   49,
       138633  + /*   310 */    50,   51,   52,   53,   54,   55,   56,   57,  222,  223,
       138634  + /*   320 */   224,  186,  208,  209,  208,  209,  194,  194,   96,   97,
       138635  + /*   330 */    98,   19,  168,  169,  170,   23,   88,   89,  163,   59,
       138636  + /*   340 */     0,    1,    2,  219,   84,   85,   86,   87,   88,   89,
       138637  + /*   350 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
       138638  + /*   360 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
       138639  + /*   370 */    90,  152,  197,  195,  196,  243,   96,   97,   98,  196,
       138640  + /*   380 */   132,   11,  134,   19,  182,  207,  115,   23,  117,  118,
       138641  + /*   390 */   207,  163,  173,  174,  152,  220,   84,   85,   86,   87,
       138642  + /*   400 */    88,   89,   90,   91,   92,   93,   94,   43,   44,   45,
       138643  + /*   410 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       138644  + /*   420 */    56,   57,  152,   16,  152,  197,  171,  208,   22,   23,
       138645  + /*   430 */    22,   23,   26,   16,   26,   19,    7,    8,    9,   23,
       138646  + /*   440 */   212,  213,  152,  173,  174,  173,  174,   19,   84,   85,
       138647  + /*   450 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   43,
       138648  + /*   460 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       138649  + /*   470 */    54,   55,   56,   57,   46,  152,  109,  110,  208,  109,
       138650  + /*   480 */   110,  152,   75,  152,   77,   22,   23,   19,  233,   83,
       138651  + /*   490 */   152,   83,   75,  238,   77,  164,  173,  174,  226,   59,
       138652  + /*   500 */    84,   85,   86,   87,   88,   89,   90,   91,   92,   93,
       138653  + /*   510 */    94,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       138654  + /*   520 */    52,   53,   54,   55,   56,   57,  108,  109,  110,  152,
       138655  + /*   530 */   152,  152,   22,   22,   23,  107,   96,   97,   98,  160,
       138656  + /*   540 */   112,  251,   19,  164,   22,  152,   83,  140,  219,  152,
       138657  + /*   550 */   173,  174,   84,   85,   86,   87,   88,   89,   90,   91,
       138658  + /*   560 */    92,   93,   94,   59,  124,  152,   43,   44,   45,   46,
       138659  + /*   570 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
       138660  + /*   580 */    57,   59,  169,  170,  119,  120,   82,  108,  109,  110,
       138661  + /*   590 */   191,  192,  191,  192,   83,  248,  249,   19,   88,   89,
       138662  + /*   600 */    96,   97,   24,  152,  152,  101,  138,   84,   85,   86,
       138663  + /*   610 */    87,   88,   89,   90,   91,   92,   93,   94,   96,   97,
       138664  + /*   620 */    98,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       138665  + /*   630 */    52,   53,   54,   55,   56,   57,  132,  133,  134,  152,
       138666  + /*   640 */   152,   46,  152,   26,  231,  194,  194,  146,  147,   19,
       138667  + /*   650 */   152,  116,  152,   72,  152,  152,  121,  152,  152,  152,
       138668  + /*   660 */    79,  138,   84,   85,   86,   87,   88,   89,   90,   91,
       138669  + /*   670 */    92,   93,   94,   43,   44,   45,   46,   47,   48,   49,
       138670  + /*   680 */    50,   51,   52,   53,   54,   55,   56,   57,  152,  108,
       138671  + /*   690 */    23,  152,  194,   26,  194,  152,  194,  194,  137,  152,
       138672  + /*   700 */   139,   19,  107,  166,  167,  218,  218,   29,  218,  173,
       138673  + /*   710 */   174,   33,  173,  174,   84,   85,   86,   87,   88,   89,
       138674  + /*   720 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
       138675  + /*   730 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
       138676  + /*   740 */   152,  194,   64,  171,  239,  239,  239,  130,  166,  167,
       138677  + /*   750 */   212,  213,   19,   23,  246,  247,   26,   59,   26,  152,
       138678  + /*   760 */   163,  218,  163,  163,  163,   98,   84,   85,   86,   87,
       138679  + /*   770 */    88,   89,   90,   91,   92,   93,   94,   44,   45,   46,
       138680  + /*   780 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
       138681  + /*   790 */    57,  124,   26,   79,  197,   97,  197,  197,  197,   31,
       138682  + /*   800 */   152,   23,   19,   19,   26,   19,  218,   39,   23,   21,
       138683  + /*   810 */   238,   26,  163,  163,  100,   59,  163,   84,   85,   86,
       138684  + /*   820 */    87,   88,   89,   90,   91,   92,   93,   94,   45,   46,
       138685  + /*   830 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
       138686  + /*   840 */    57,   19,   20,  152,   22,   23,  197,  197,   23,   19,
       138687  + /*   850 */   197,   26,  152,   97,   23,  123,   23,   26,   36,   26,
       138688  + /*   860 */   152,  152,   19,   20,   98,   22,   78,   84,   85,   86,
       138689  + /*   870 */    87,   88,   89,   90,   91,   92,   93,   94,   94,   36,
       138690  + /*   880 */   152,   59,   96,   99,  100,  101,  102,  103,  104,  105,
       138691  + /*   890 */   124,  103,   70,  100,  101,   23,  112,   12,   26,   12,
       138692  + /*   900 */    23,   79,   59,   26,   82,  101,   22,    7,    8,  152,
       138693  + /*   910 */    88,   89,   27,   70,   27,  152,  112,   95,   96,   97,
       138694  + /*   920 */    98,  152,   79,  101,   22,   82,   96,   42,  140,   42,
       138695  + /*   930 */   182,   88,   89,   19,   20,  132,   22,  134,   95,   96,
       138696  + /*   940 */    97,   98,   23,   59,  101,   26,   22,   62,  152,   62,
       138697  + /*   950 */    36,   22,   59,   24,  132,  133,  134,  135,  136,   72,
       138698  + /*   960 */     5,   59,  152,   22,   71,   10,   11,   12,   13,   14,
       138699  + /*   970 */   152,  152,   17,   59,  182,  132,  133,  134,  135,  136,
       138700  + /*   980 */    96,   97,   98,   59,   70,   30,  152,   32,   59,   96,
       138701  + /*   990 */    97,   98,  173,  174,   53,   40,   82,  152,   96,   97,
       138702  + /*  1000 */    98,   90,   88,   89,   90,   24,  187,  173,  174,   95,
       138703  + /*  1010 */    96,   97,   98,  152,  152,  101,   19,   20,  152,   22,
       138704  + /*  1020 */    96,   97,   98,  152,   69,   59,   97,  116,   59,  214,
       138705  + /*  1030 */   152,   76,  121,   36,   79,   80,   19,   20,  152,   22,
       138706  + /*  1040 */    59,  132,   59,  134,  173,  174,  132,  133,  134,  135,
       138707  + /*  1050 */   136,  173,  174,   36,  234,  152,   59,  152,  187,  173,
       138708  + /*  1060 */   174,  211,   96,   97,   98,  187,   97,   70,   23,  114,
       138709  + /*  1070 */   152,   26,   23,  187,   23,   26,   59,   26,   97,   82,
       138710  + /*  1080 */    97,   22,  164,  152,  152,   88,   89,   70,  192,  152,
       138711  + /*  1090 */   124,  152,   95,   96,   97,   98,  141,  152,  101,   82,
       138712  + /*  1100 */   152,  152,  184,  152,  186,   88,   89,  199,   54,   55,
       138713  + /*  1110 */    56,   57,   95,   96,   97,   98,  152,  152,  101,  152,
       138714  + /*  1120 */   152,  173,  174,  235,   54,   55,   56,   57,   58,  132,
       138715  + /*  1130 */   133,  134,  135,  136,  211,  150,   59,  211,   84,   85,
       138716  + /*  1140 */    86,   87,   88,   89,   90,   91,   92,   93,   94,  132,
       138717  + /*  1150 */   133,  134,  135,  136,   84,   85,   86,   87,   88,   89,
       138718  + /*  1160 */    90,   91,   92,   93,   94,   88,   89,  195,  196,   35,
       138719  + /*  1170 */   211,  211,  152,   96,   97,   98,  152,  100,  198,  207,
       138720  + /*  1180 */   171,  122,  240,  152,  107,  215,  109,  240,  202,  215,
       138721  + /*  1190 */   202,  152,  220,  173,  174,  177,  185,  173,  174,   65,
       138722  + /*  1200 */   152,  176,  152,  181,  173,  174,  215,   73,  176,  132,
       138723  + /*  1210 */   228,  134,  173,  174,  176,  152,  201,  152,  199,  155,
       138724  + /*  1220 */   152,  173,  174,  173,  174,   60,  152,  122,  244,   38,
       138725  + /*  1230 */   159,  159,  152,  241,  241,  152,  173,  174,  173,  174,
       138726  + /*  1240 */   152,  173,  174,  152,  159,  111,  152,  173,  174,  152,
       138727  + /*  1250 */    22,  152,   43,  173,  174,  152,  173,  174,  152,  190,
       138728  + /*  1260 */   130,  173,  174,  152,  173,  174,  152,  173,  174,  202,
       138729  + /*  1270 */   173,  174,  173,  174,  152,   18,  173,  174,  152,  173,
       138730  + /*  1280 */   174,  152,  221,  152,  173,  174,  193,  173,  174,  152,
       138731  + /*  1290 */   158,  152,  159,  152,   22,  173,  174,  152,   18,  173,
       138732  + /*  1300 */   174,  152,  173,  174,  173,  174,  152,  221,  152,  193,
       138733  + /*  1310 */   173,  174,  173,  174,  173,  174,  152,  193,  173,  174,
       138734  + /*  1320 */   152,  193,  173,  174,  158,  152,  190,  173,  174,  173,
       138735  + /*  1330 */   174,   59,  152,  190,  159,  137,  152,  173,  174,  152,
       138736  + /*  1340 */   202,  173,  174,   61,  152,  237,  173,  174,  202,  236,
       138737  + /*  1350 */    22,  107,  159,  173,  174,  159,  178,  173,  174,  158,
       138738  + /*  1360 */   173,  174,  159,  158,  158,  173,  174,  159,  178,   97,
       138739  + /*  1370 */   175,   63,  175,  106,  175,  125,  217,  183,  178,  175,
       138740  + /*  1380 */   107,  159,   22,  216,  177,  175,  217,  175,  175,  217,
       138741  + /*  1390 */   216,  159,  216,  216,  183,  225,  217,  137,  227,  178,
       138742  + /*  1400 */    94,  178,  126,  129,   25,  128,  127,  162,  206,   26,
       138743  + /*  1410 */   161,  230,  230,   13,  153,  153,    6,  151,  203,  205,
       138744  + /*  1420 */   204,  202,  151,  165,  171,  171,  151,  171,  165,  165,
       138745  + /*  1430 */   171,  179,    4,  250,  179,  250,  247,    3,   22,  171,
       138746  + /*  1440 */   171,  171,  142,   81,   15,   23,   16,   23,  131,  171,
       138747  + /*  1450 */   120,  111,  123,   20,   16,    1,  125,  123,  131,   53,
       138748  + /*  1460 */    53,   53,   53,  111,   96,   34,  122,    1,    5,   22,
       138749  + /*  1470 */   107,   67,  140,   74,   26,   24,   41,  107,   67,   20,
       138750  + /*  1480 */    19,  112,  105,   66,   22,   66,   22,   22,   28,   66,
       138751  + /*  1490 */    23,   22,   22,   37,   23,   23,   23,  116,   22,   22,
       138752  + /*  1500 */    26,   23,  122,   23,   23,   26,   22,   11,   96,   34,
       138753  + /*  1510 */   124,   26,   26,   23,   23,   23,   34,   34,   23,   26,
       138754  + /*  1520 */    23,   22,   24,   23,   22,  122,   23,   26,   22,   24,
       138755  + /*  1530 */    23,   22,   15,   23,   23,   22,  122,  122,   23,    1,
       138756  + /*  1540 */   122,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138757  + /*  1550 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138758  + /*  1560 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138759  + /*  1570 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138760  + /*  1580 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138761  + /*  1590 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138762  + /*  1600 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138763  + /*  1610 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138764  + /*  1620 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138765  + /*  1630 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138766  + /*  1640 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138767  + /*  1650 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138768  + /*  1660 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138769  + /*  1670 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
       138770  + /*  1680 */   252,  252,  252,  252,
       138771  +};
       138772  +#define YY_SHIFT_COUNT    (465)
       138773  +#define YY_SHIFT_MIN      (0)
       138774  +#define YY_SHIFT_MAX      (1538)
       138775  +static const unsigned short int yy_shift_ofst[] = {
       138776  + /*     0 */    39,  822,  955,  843,  997,  997,  997,  997,    0,    0,
       138777  + /*    10 */   104,  630,  997,  997,  997,  997,  997,  997,  997, 1077,
       138778  + /*    20 */  1077,  126,  161,  155,   52,  156,  208,  260,  312,  364,
       138779  + /*    30 */   416,  468,  523,  578,  630,  630,  630,  630,  630,  630,
       138780  + /*    40 */   630,  630,  630,  630,  630,  630,  630,  630,  630,  630,
       138781  + /*    50 */   630,  682,  630,  733,  783,  783,  914,  997,  997,  997,
       138782  + /*    60 */   997,  997,  997,  997,  997,  997,  997,  997,  997,  997,
       138783  + /*    70 */   997,  997,  997,  997,  997,  997,  997,  997,  997,  997,
       138784  + /*    80 */   997,  997,  997,  997,  997,  997,  997,  997, 1017,  997,
       138785  + /*    90 */   997,  997,  997,  997,  997,  997,  997,  997,  997,  997,
       138786  + /*   100 */   997,  997, 1070, 1054, 1054, 1054, 1054, 1054,   40,   25,
       138787  + /*   110 */    72,  232,  788,  428,  248,  248,  232,  581,  367,  127,
       138788  + /*   120 */   465,  138, 1541, 1541, 1541,  784,  784,  784,  522,  522,
       138789  + /*   130 */   887,  887,  893,  406,  408,  232,  232,  232,  232,  232,
       138790  + /*   140 */   232,  232,  232,  232,  232,  232,  232,  232,  232,  232,
       138791  + /*   150 */   232,  232,  232,  232,  232,  370,  340,  714,  698,  698,
       138792  + /*   160 */   465,   89,   89,   89,   89,   89,   89, 1541, 1541, 1541,
       138793  + /*   170 */   504,   85,   85,  884,   70,  280,  902,  440,  966,  924,
       138794  + /*   180 */   232,  232,  232,  232,  232,  232,  232,  232,  232,  232,
       138795  + /*   190 */   232,  232,  232,  232,  232,  232, 1134, 1134, 1134,  232,
       138796  + /*   200 */   232,  667,  232,  232,  232,  929,  232,  232,  885,  232,
       138797  + /*   210 */   232,  232,  232,  232,  232,  232,  232,  232,  232,  418,
       138798  + /*   220 */   678,  981,  981,  981,  981,  766,  271,  911,  510,  429,
       138799  + /*   230 */   617,  786,  786,  830,  617,  830,    4,  730,  595,  768,
       138800  + /*   240 */   786,  561,  768,  768,  732,  535,   55, 1165, 1105, 1105,
       138801  + /*   250 */  1191, 1191, 1105, 1228, 1209, 1130, 1257, 1257, 1257, 1257,
       138802  + /*   260 */  1105, 1280, 1130, 1228, 1209, 1209, 1130, 1105, 1280, 1198,
       138803  + /*   270 */  1282, 1105, 1105, 1280, 1328, 1105, 1280, 1105, 1280, 1328,
       138804  + /*   280 */  1244, 1244, 1244, 1308, 1328, 1244, 1267, 1244, 1308, 1244,
       138805  + /*   290 */  1244, 1250, 1273, 1250, 1273, 1250, 1273, 1250, 1273, 1105,
       138806  + /*   300 */  1360, 1105, 1260, 1328, 1306, 1306, 1328, 1274, 1276, 1277,
       138807  + /*   310 */  1279, 1130, 1379, 1383, 1400, 1400, 1410, 1410, 1410, 1541,
       138808  + /*   320 */  1541, 1541, 1541, 1541, 1541, 1541, 1541, 1541, 1541, 1541,
       138809  + /*   330 */  1541, 1541, 1541, 1541, 1541,   34,  407,  463,  511,  417,
       138810  + /*   340 */   479, 1272,  778,  941,  785,  825,  831,  833,  872,  877,
       138811  + /*   350 */   756,  793,  900,  804,  919, 1045,  969, 1049,  803,  909,
       138812  + /*   360 */  1051,  983, 1059, 1428, 1434, 1416, 1300, 1429, 1362, 1430,
       138813  + /*   370 */  1422, 1424, 1330, 1317, 1340, 1329, 1433, 1331, 1438, 1454,
       138814  + /*   380 */  1334, 1327, 1406, 1407, 1408, 1409, 1352, 1368, 1431, 1344,
       138815  + /*   390 */  1466, 1463, 1447, 1363, 1332, 1404, 1448, 1411, 1399, 1435,
       138816  + /*   400 */  1370, 1451, 1459, 1461, 1369, 1377, 1462, 1417, 1464, 1465,
       138817  + /*   410 */  1467, 1469, 1419, 1460, 1470, 1423, 1456, 1471, 1472, 1473,
       138818  + /*   420 */  1474, 1381, 1476, 1478, 1477, 1479, 1380, 1480, 1481, 1412,
       138819  + /*   430 */  1475, 1484, 1386, 1485, 1482, 1486, 1483, 1490, 1485, 1491,
       138820  + /*   440 */  1492, 1495, 1493, 1497, 1499, 1496, 1500, 1502, 1498, 1501,
       138821  + /*   450 */  1503, 1506, 1505, 1501, 1507, 1509, 1510, 1511, 1513, 1403,
       138822  + /*   460 */  1414, 1415, 1418, 1515, 1517, 1538,
       138823  +};
       138824  +#define YY_REDUCE_COUNT (334)
       138825  +#define YY_REDUCE_MIN   (-217)
       138826  +#define YY_REDUCE_MAX   (1278)
137685 138827   static const short yy_reduce_ofst[] = {
137686         - /*     0 */  -143,  -65,  140,  840,   76,  180,  182,  233,  488,  -25,
137687         - /*    10 */    12,   16,   59,  885,  907,  935,  390,  705,  954,  285,
137688         - /*    20 */   997, 1017, 1018, -118, 1025,  139,  171,  171,  171,  171,
137689         - /*    30 */   171,  171,  171,  171,  171,  171,  171,  171,  171,  171,
137690         - /*    40 */   171,  171,  171,  171,  171,  171,  171,  171,  171,  171,
137691         - /*    50 */   171,  171,  171,  171,  171,  171,  171,  171,  -69,  287,
137692         - /*    60 */   441,  658,  708,  856, 1050, 1073, 1076, 1079, 1081, 1084,
137693         - /*    70 */  1086, 1088, 1091, 1113, 1115, 1117, 1122, 1124, 1126, 1128,
137694         - /*    80 */  1130, 1141, 1153, 1156, 1159, 1163, 1165, 1167, 1170, 1172,
137695         - /*    90 */  1175, 1178, 1181, 1189, 1194, 1197, 1200, 1203, 1205, 1207,
137696         - /*   100 */  1211, 1213, 1216, 1219,  171,  171,  171,  171,  171,  171,
137697         - /*   110 */   171,  171,  171,   49,  176,  220,  275,  278,  290,  171,
137698         - /*   120 */   300,  171,  171,  171,  171,  -85,  -85,  -85,  -28,   77,
137699         - /*   130 */   313,  317,  -56,  252,  252,  446, -129,  243,  361,  403,
137700         - /*   140 */   406,  513,  517,  409,  502,  518,  504,  509,  621,  553,
137701         - /*   150 */   562,  619,  559,   93,  620,  465,  453,  550,  591,  571,
137702         - /*   160 */   615,  666,  750,  752,  797,  819,  463,  548,  -73,   28,
137703         - /*   170 */    68,  120,  257,  206,  359,  405,  413,  452,  457,  560,
137704         - /*   180 */   566,  617,  670,  720,  723,  769,  773,  775,  780,  813,
137705         - /*   190 */   814,  821,  822,  823,  826,  360,  436,  783,  829,  835,
137706         - /*   200 */   707,  862,  867,  878,  830,  911,  915,  883,  936,  937,
137707         - /*   210 */   940,  359,  942,  943,  944,  979,  982,  984,  886,  899,
137708         - /*   220 */   928,  929,  931,  707,  947,  945,  998,  949,  932,  969,
137709         - /*   230 */   918,  953,  924,  992, 1005, 1010, 1016,  971,  965, 1019,
137710         - /*   240 */  1049, 1000, 1028, 1074,  989, 1078, 1080, 1026, 1031, 1109,
137711         - /*   250 */  1053, 1090, 1103, 1092, 1099, 1114, 1118, 1148, 1151, 1111,
137712         - /*   260 */  1096, 1129, 1131, 1133, 1162, 1202, 1138, 1146, 1231, 1234,
137713         - /*   270 */  1206, 1218, 1237, 1239, 1240, 1242, 1221, 1228, 1229, 1230,
137714         - /*   280 */  1224, 1233, 1235, 1236, 1241, 1226, 1243, 1244, 1198, 1201,
137715         - /*   290 */  1209, 1212, 1210, 1214, 1215, 1217, 1260, 1199, 1262, 1220,
137716         - /*   300 */  1247, 1222, 1223, 1253, 1238, 1245, 1251, 1246, 1249, 1276,
137717         - /*   310 */  1279, 1289, 1291, 1296, 1302, 1305, 1225, 1227, 1248, 1290,
137718         - /*   320 */  1292, 1280, 1281, 1295,
       138828  + /*     0 */  -144, -139, -134, -136, -141,   64,  114,  116, -158, -148,
       138829  + /*    10 */  -217,   96,  819,  871,  878,  219,  270,  886,  272, -110,
       138830  + /*    20 */   413,  918,  972,  228, -214, -214, -214, -214, -214, -214,
       138831  + /*    30 */  -214, -214, -214, -214, -214, -214, -214, -214, -214, -214,
       138832  + /*    40 */  -214, -214, -214, -214, -214, -214, -214, -214, -214, -214,
       138833  + /*    50 */  -214, -214, -214, -214, -214, -214,   62,  323,  377,  536,
       138834  + /*    60 */   539,  834,  948, 1020, 1024, 1031, 1039, 1048, 1050, 1063,
       138835  + /*    70 */  1065, 1068, 1074, 1080, 1083, 1088, 1091, 1094, 1097, 1099,
       138836  + /*    80 */  1103, 1106, 1111, 1114, 1122, 1126, 1129, 1131, 1137, 1139,
       138837  + /*    90 */  1141, 1145, 1149, 1154, 1156, 1164, 1168, 1173, 1180, 1184,
       138838  + /*   100 */  1187, 1192, -214, -214, -214, -214, -214, -214, -214, -214,
       138839  + /*   110 */  -214,  132,  -45,   97,    8,  164,  379,  175,  255, -214,
       138840  + /*   120 */   178, -214, -214, -214, -214, -168, -168, -168,  124,  329,
       138841  + /*   130 */   399,  401, -129,  347,  347,  331,  133,  451,  452,  498,
       138842  + /*   140 */   500,  502,  503,  505,  487,  506,  488,  490,  507,  543,
       138843  + /*   150 */   547, -126,  588,  290,   27,  572,  501,  597,  537,  582,
       138844  + /*   160 */   183,  599,  600,  601,  649,  650,  653,  508,  538,  -29,
       138845  + /*   170 */  -156, -152, -137,  -79,  135,   74,  130,  242,  338,  378,
       138846  + /*   180 */   393,  397,  607,  648,  691,  700,  708,  709,  728,  757,
       138847  + /*   190 */   763,  769,  796,  810,  818,  845,  202,  748,  792,  861,
       138848  + /*   200 */   862,  815,  866,  903,  905,  850,  931,  932,  896,  937,
       138849  + /*   210 */   939,  945,   74,  949,  951,  964,  965,  967,  968,  888,
       138850  + /*   220 */   820,  923,  926,  959,  960,  815,  980,  908, 1009,  985,
       138851  + /*   230 */   986,  970,  974,  942,  988,  947, 1018, 1011, 1022, 1025,
       138852  + /*   240 */   991,  982, 1032, 1038, 1015, 1019, 1064,  984, 1071, 1072,
       138853  + /*   250 */   992,  993, 1085, 1061, 1069, 1067, 1093, 1116, 1124, 1128,
       138854  + /*   260 */  1133, 1132, 1138, 1086, 1136, 1143, 1146, 1175, 1166, 1108,
       138855  + /*   270 */  1113, 1193, 1196, 1201, 1178, 1203, 1205, 1208, 1206, 1190,
       138856  + /*   280 */  1195, 1197, 1199, 1194, 1200, 1204, 1207, 1210, 1211, 1212,
       138857  + /*   290 */  1213, 1159, 1167, 1169, 1174, 1172, 1176, 1179, 1177, 1222,
       138858  + /*   300 */  1170, 1232, 1171, 1221, 1181, 1182, 1223, 1202, 1214, 1216,
       138859  + /*   310 */  1215, 1219, 1245, 1249, 1261, 1262, 1266, 1271, 1275, 1183,
       138860  + /*   320 */  1185, 1189, 1258, 1253, 1254, 1256, 1259, 1263, 1252, 1255,
       138861  + /*   330 */  1268, 1269, 1270, 1278, 1264,
137719 138862   };
137720 138863   static const YYACTIONTYPE yy_default[] = {
137721         - /*     0 */  1270, 1260, 1260, 1260, 1193, 1193, 1193, 1193, 1260, 1088,
137722         - /*    10 */  1117, 1117, 1244, 1322, 1322, 1322, 1322, 1322, 1322, 1192,
137723         - /*    20 */  1322, 1322, 1322, 1322, 1260, 1092, 1123, 1322, 1322, 1322,
137724         - /*    30 */  1322, 1194, 1195, 1322, 1322, 1322, 1243, 1245, 1133, 1132,
137725         - /*    40 */  1131, 1130, 1226, 1104, 1128, 1121, 1125, 1194, 1188, 1189,
137726         - /*    50 */  1187, 1191, 1195, 1322, 1124, 1158, 1172, 1157, 1322, 1322,
137727         - /*    60 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137728         - /*    70 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137729         - /*    80 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137730         - /*    90 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137731         - /*   100 */  1322, 1322, 1322, 1322, 1166, 1171, 1178, 1170, 1167, 1160,
137732         - /*   110 */  1159, 1161, 1162, 1322, 1011, 1059, 1322, 1322, 1322, 1163,
137733         - /*   120 */  1322, 1164, 1175, 1174, 1173, 1251, 1278, 1277, 1322, 1322,
137734         - /*   130 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137735         - /*   140 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137736         - /*   150 */  1322, 1322, 1322, 1322, 1322, 1270, 1260, 1017, 1017, 1322,
137737         - /*   160 */  1260, 1260, 1260, 1260, 1260, 1260, 1256, 1092, 1083, 1322,
137738         - /*   170 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137739         - /*   180 */  1248, 1246, 1322, 1208, 1322, 1322, 1322, 1322, 1322, 1322,
137740         - /*   190 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137741         - /*   200 */  1322, 1322, 1322, 1322, 1088, 1322, 1322, 1322, 1322, 1322,
137742         - /*   210 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1272, 1322, 1221,
137743         - /*   220 */  1088, 1088, 1088, 1090, 1072, 1082,  997, 1127, 1106, 1106,
137744         - /*   230 */  1311, 1127, 1311, 1034, 1292, 1031, 1117, 1106, 1190, 1117,
137745         - /*   240 */  1117, 1089, 1082, 1322, 1314, 1097, 1097, 1313, 1313, 1097,
137746         - /*   250 */  1138, 1062, 1127, 1068, 1068, 1068, 1068, 1097, 1008, 1127,
137747         - /*   260 */  1138, 1062, 1062, 1127, 1097, 1008, 1225, 1308, 1097, 1097,
137748         - /*   270 */  1008, 1201, 1097, 1008, 1097, 1008, 1201, 1060, 1060, 1060,
137749         - /*   280 */  1049, 1201, 1060, 1034, 1060, 1049, 1060, 1060, 1110, 1105,
137750         - /*   290 */  1110, 1105, 1110, 1105, 1110, 1105, 1097, 1196, 1097, 1322,
137751         - /*   300 */  1201, 1205, 1205, 1201, 1122, 1111, 1120, 1118, 1127, 1014,
137752         - /*   310 */  1052, 1275, 1275, 1271, 1271, 1271, 1319, 1319, 1256, 1287,
137753         - /*   320 */  1287, 1036, 1036, 1287, 1322, 1322, 1322, 1322, 1322, 1322,
137754         - /*   330 */  1282, 1322, 1210, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137755         - /*   340 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137756         - /*   350 */  1322, 1143, 1322,  993, 1253, 1322, 1322, 1252, 1322, 1322,
137757         - /*   360 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137758         - /*   370 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1310, 1322,
137759         - /*   380 */  1322, 1322, 1322, 1322, 1322, 1224, 1223, 1322, 1322, 1322,
137760         - /*   390 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137761         - /*   400 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322,
137762         - /*   410 */  1074, 1322, 1322, 1322, 1296, 1322, 1322, 1322, 1322, 1322,
137763         - /*   420 */  1322, 1322, 1119, 1322, 1112, 1322, 1322, 1301, 1322, 1322,
137764         - /*   430 */  1322, 1322, 1322, 1322, 1322, 1322, 1322, 1322, 1262, 1322,
137765         - /*   440 */  1322, 1322, 1261, 1322, 1322, 1322, 1322, 1322, 1145, 1322,
137766         - /*   450 */  1144, 1148, 1322, 1002, 1322,
       138864  + /*     0 */  1286, 1276, 1276, 1276, 1209, 1209, 1209, 1209, 1133, 1133,
       138865  + /*    10 */  1260, 1036, 1005, 1005, 1005, 1005, 1005, 1005, 1208, 1005,
       138866  + /*    20 */  1005, 1005, 1005, 1108, 1139, 1005, 1005, 1005, 1005, 1210,
       138867  + /*    30 */  1211, 1005, 1005, 1005, 1259, 1261, 1149, 1148, 1147, 1146,
       138868  + /*    40 */  1242, 1120, 1144, 1137, 1141, 1210, 1204, 1205, 1203, 1207,
       138869  + /*    50 */  1211, 1005, 1140, 1174, 1188, 1173, 1005, 1005, 1005, 1005,
       138870  + /*    60 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138871  + /*    70 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138872  + /*    80 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138873  + /*    90 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138874  + /*   100 */  1005, 1005, 1182, 1187, 1194, 1186, 1183, 1176, 1175, 1177,
       138875  + /*   110 */  1178, 1005, 1026, 1075, 1005, 1005, 1005, 1276, 1036, 1179,
       138876  + /*   120 */  1005, 1180, 1191, 1190, 1189, 1267, 1294, 1293, 1005, 1005,
       138877  + /*   130 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138878  + /*   140 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138879  + /*   150 */  1005, 1005, 1005, 1005, 1005, 1036, 1286, 1276, 1032, 1032,
       138880  + /*   160 */  1005, 1276, 1276, 1276, 1276, 1276, 1276, 1272, 1108, 1099,
       138881  + /*   170 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138882  + /*   180 */  1005, 1264, 1262, 1005, 1224, 1005, 1005, 1005, 1005, 1005,
       138883  + /*   190 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138884  + /*   200 */  1005, 1005, 1005, 1005, 1005, 1104, 1005, 1005, 1005, 1005,
       138885  + /*   210 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1288, 1005,
       138886  + /*   220 */  1237, 1104, 1104, 1104, 1104, 1106, 1088, 1098, 1036, 1012,
       138887  + /*   230 */  1143, 1122, 1122, 1327, 1143, 1327, 1050, 1308, 1047, 1133,
       138888  + /*   240 */  1122, 1206, 1133, 1133, 1105, 1098, 1005, 1330, 1113, 1113,
       138889  + /*   250 */  1329, 1329, 1113, 1154, 1078, 1143, 1084, 1084, 1084, 1084,
       138890  + /*   260 */  1113, 1023, 1143, 1154, 1078, 1078, 1143, 1113, 1023, 1241,
       138891  + /*   270 */  1324, 1113, 1113, 1023, 1217, 1113, 1023, 1113, 1023, 1217,
       138892  + /*   280 */  1076, 1076, 1076, 1065, 1217, 1076, 1050, 1076, 1065, 1076,
       138893  + /*   290 */  1076, 1126, 1121, 1126, 1121, 1126, 1121, 1126, 1121, 1113,
       138894  + /*   300 */  1212, 1113, 1005, 1217, 1221, 1221, 1217, 1138, 1127, 1136,
       138895  + /*   310 */  1134, 1143, 1029, 1068, 1291, 1291, 1287, 1287, 1287, 1335,
       138896  + /*   320 */  1335, 1272, 1303, 1036, 1036, 1036, 1036, 1303, 1052, 1052,
       138897  + /*   330 */  1036, 1036, 1036, 1036, 1303, 1005, 1005, 1005, 1005, 1005,
       138898  + /*   340 */  1005, 1298, 1005, 1226, 1005, 1005, 1005, 1005, 1005, 1005,
       138899  + /*   350 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138900  + /*   360 */  1005, 1005, 1159, 1005, 1008, 1269, 1005, 1005, 1268, 1005,
       138901  + /*   370 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138902  + /*   380 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1326,
       138903  + /*   390 */  1005, 1005, 1005, 1005, 1005, 1005, 1240, 1239, 1005, 1005,
       138904  + /*   400 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138905  + /*   410 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005,
       138906  + /*   420 */  1005, 1090, 1005, 1005, 1005, 1312, 1005, 1005, 1005, 1005,
       138907  + /*   430 */  1005, 1005, 1005, 1135, 1005, 1128, 1005, 1005, 1317, 1005,
       138908  + /*   440 */  1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1005, 1278,
       138909  + /*   450 */  1005, 1005, 1005, 1277, 1005, 1005, 1005, 1005, 1005, 1161,
       138910  + /*   460 */  1005, 1160, 1164, 1005, 1017, 1005,
137767 138911   };
137768 138912   /********** End of lemon-generated parsing tables *****************************/
137769 138913   
137770 138914   /* The next table maps tokens (terminal symbols) into fallback tokens.  
137771 138915   ** If a construct like the following:
137772 138916   ** 
137773 138917   **      %fallback ID X Y Z.
................................................................................
137944 139088     yyTraceFILE = TraceFILE;
137945 139089     yyTracePrompt = zTracePrompt;
137946 139090     if( yyTraceFILE==0 ) yyTracePrompt = 0;
137947 139091     else if( yyTracePrompt==0 ) yyTraceFILE = 0;
137948 139092   }
137949 139093   #endif /* NDEBUG */
137950 139094   
137951         -#ifndef NDEBUG
       139095  +#if defined(YYCOVERAGE) || !defined(NDEBUG)
137952 139096   /* For tracing shifts, the names of all terminals and nonterminals
137953 139097   ** are required.  The following table supplies these names */
137954 139098   static const char *const yyTokenName[] = { 
137955         -  "$",             "SEMI",          "EXPLAIN",       "QUERY",       
137956         -  "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",    
137957         -  "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",         
137958         -  "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",          
137959         -  "TABLE",         "CREATE",        "IF",            "NOT",         
137960         -  "EXISTS",        "TEMP",          "LP",            "RP",          
137961         -  "AS",            "WITHOUT",       "COMMA",         "ABORT",       
137962         -  "ACTION",        "AFTER",         "ANALYZE",       "ASC",         
137963         -  "ATTACH",        "BEFORE",        "BY",            "CASCADE",     
137964         -  "CAST",          "CONFLICT",      "DATABASE",      "DESC",        
137965         -  "DETACH",        "EACH",          "FAIL",          "OR",          
137966         -  "AND",           "IS",            "MATCH",         "LIKE_KW",     
137967         -  "BETWEEN",       "IN",            "ISNULL",        "NOTNULL",     
137968         -  "NE",            "EQ",            "GT",            "LE",          
137969         -  "LT",            "GE",            "ESCAPE",        "ID",          
137970         -  "COLUMNKW",      "FOR",           "IGNORE",        "INITIALLY",   
137971         -  "INSTEAD",       "NO",            "KEY",           "OF",          
137972         -  "OFFSET",        "PRAGMA",        "RAISE",         "RECURSIVE",   
137973         -  "REPLACE",       "RESTRICT",      "ROW",           "TRIGGER",     
137974         -  "VACUUM",        "VIEW",          "VIRTUAL",       "WITH",        
137975         -  "REINDEX",       "RENAME",        "CTIME_KW",      "ANY",         
137976         -  "BITAND",        "BITOR",         "LSHIFT",        "RSHIFT",      
137977         -  "PLUS",          "MINUS",         "STAR",          "SLASH",       
137978         -  "REM",           "CONCAT",        "COLLATE",       "BITNOT",      
137979         -  "INDEXED",       "STRING",        "JOIN_KW",       "CONSTRAINT",  
137980         -  "DEFAULT",       "NULL",          "PRIMARY",       "UNIQUE",      
137981         -  "CHECK",         "REFERENCES",    "AUTOINCR",      "ON",          
137982         -  "INSERT",        "DELETE",        "UPDATE",        "SET",         
137983         -  "DEFERRABLE",    "FOREIGN",       "DROP",          "UNION",       
137984         -  "ALL",           "EXCEPT",        "INTERSECT",     "SELECT",      
137985         -  "VALUES",        "DISTINCT",      "DOT",           "FROM",        
137986         -  "JOIN",          "USING",         "ORDER",         "GROUP",       
137987         -  "HAVING",        "LIMIT",         "WHERE",         "INTO",        
137988         -  "FLOAT",         "BLOB",          "INTEGER",       "VARIABLE",    
137989         -  "CASE",          "WHEN",          "THEN",          "ELSE",        
137990         -  "INDEX",         "ALTER",         "ADD",           "error",       
137991         -  "input",         "cmdlist",       "ecmd",          "explain",     
137992         -  "cmdx",          "cmd",           "transtype",     "trans_opt",   
137993         -  "nm",            "savepoint_opt",  "create_table",  "create_table_args",
137994         -  "createkw",      "temp",          "ifnotexists",   "dbnm",        
137995         -  "columnlist",    "conslist_opt",  "table_options",  "select",      
137996         -  "columnname",    "carglist",      "typetoken",     "typename",    
137997         -  "signed",        "plus_num",      "minus_num",     "ccons",       
137998         -  "term",          "expr",          "onconf",        "sortorder",   
137999         -  "autoinc",       "eidlist_opt",   "refargs",       "defer_subclause",
138000         -  "refarg",        "refact",        "init_deferred_pred_opt",  "conslist",    
138001         -  "tconscomma",    "tcons",         "sortlist",      "eidlist",     
138002         -  "defer_subclause_opt",  "orconf",        "resolvetype",   "raisetype",   
138003         -  "ifexists",      "fullname",      "selectnowith",  "oneselect",   
138004         -  "with",          "multiselect_op",  "distinct",      "selcollist",  
138005         -  "from",          "where_opt",     "groupby_opt",   "having_opt",  
138006         -  "orderby_opt",   "limit_opt",     "values",        "nexprlist",   
138007         -  "exprlist",      "sclp",          "as",            "seltablist",  
138008         -  "stl_prefix",    "joinop",        "indexed_opt",   "on_opt",      
138009         -  "using_opt",     "idlist",        "setlist",       "insert_cmd",  
138010         -  "idlist_opt",    "likeop",        "between_op",    "in_op",       
138011         -  "paren_exprlist",  "case_operand",  "case_exprlist",  "case_else",   
138012         -  "uniqueflag",    "collate",       "nmnum",         "trigger_decl",
138013         -  "trigger_cmd_list",  "trigger_time",  "trigger_event",  "foreach_clause",
138014         -  "when_clause",   "trigger_cmd",   "trnm",          "tridxby",     
138015         -  "database_kw_opt",  "key_opt",       "add_column_fullname",  "kwcolumn_opt",
138016         -  "create_vtab",   "vtabarglist",   "vtabarg",       "vtabargtoken",
138017         -  "lp",            "anylist",       "wqlist",      
138018         -};
138019         -#endif /* NDEBUG */
       139099  +  /*    0 */ "$",
       139100  +  /*    1 */ "SEMI",
       139101  +  /*    2 */ "EXPLAIN",
       139102  +  /*    3 */ "QUERY",
       139103  +  /*    4 */ "PLAN",
       139104  +  /*    5 */ "BEGIN",
       139105  +  /*    6 */ "TRANSACTION",
       139106  +  /*    7 */ "DEFERRED",
       139107  +  /*    8 */ "IMMEDIATE",
       139108  +  /*    9 */ "EXCLUSIVE",
       139109  +  /*   10 */ "COMMIT",
       139110  +  /*   11 */ "END",
       139111  +  /*   12 */ "ROLLBACK",
       139112  +  /*   13 */ "SAVEPOINT",
       139113  +  /*   14 */ "RELEASE",
       139114  +  /*   15 */ "TO",
       139115  +  /*   16 */ "TABLE",
       139116  +  /*   17 */ "CREATE",
       139117  +  /*   18 */ "IF",
       139118  +  /*   19 */ "NOT",
       139119  +  /*   20 */ "EXISTS",
       139120  +  /*   21 */ "TEMP",
       139121  +  /*   22 */ "LP",
       139122  +  /*   23 */ "RP",
       139123  +  /*   24 */ "AS",
       139124  +  /*   25 */ "WITHOUT",
       139125  +  /*   26 */ "COMMA",
       139126  +  /*   27 */ "ABORT",
       139127  +  /*   28 */ "ACTION",
       139128  +  /*   29 */ "AFTER",
       139129  +  /*   30 */ "ANALYZE",
       139130  +  /*   31 */ "ASC",
       139131  +  /*   32 */ "ATTACH",
       139132  +  /*   33 */ "BEFORE",
       139133  +  /*   34 */ "BY",
       139134  +  /*   35 */ "CASCADE",
       139135  +  /*   36 */ "CAST",
       139136  +  /*   37 */ "CONFLICT",
       139137  +  /*   38 */ "DATABASE",
       139138  +  /*   39 */ "DESC",
       139139  +  /*   40 */ "DETACH",
       139140  +  /*   41 */ "EACH",
       139141  +  /*   42 */ "FAIL",
       139142  +  /*   43 */ "OR",
       139143  +  /*   44 */ "AND",
       139144  +  /*   45 */ "IS",
       139145  +  /*   46 */ "MATCH",
       139146  +  /*   47 */ "LIKE_KW",
       139147  +  /*   48 */ "BETWEEN",
       139148  +  /*   49 */ "IN",
       139149  +  /*   50 */ "ISNULL",
       139150  +  /*   51 */ "NOTNULL",
       139151  +  /*   52 */ "NE",
       139152  +  /*   53 */ "EQ",
       139153  +  /*   54 */ "GT",
       139154  +  /*   55 */ "LE",
       139155  +  /*   56 */ "LT",
       139156  +  /*   57 */ "GE",
       139157  +  /*   58 */ "ESCAPE",
       139158  +  /*   59 */ "ID",
       139159  +  /*   60 */ "COLUMNKW",
       139160  +  /*   61 */ "FOR",
       139161  +  /*   62 */ "IGNORE",
       139162  +  /*   63 */ "INITIALLY",
       139163  +  /*   64 */ "INSTEAD",
       139164  +  /*   65 */ "NO",
       139165  +  /*   66 */ "KEY",
       139166  +  /*   67 */ "OF",
       139167  +  /*   68 */ "OFFSET",
       139168  +  /*   69 */ "PRAGMA",
       139169  +  /*   70 */ "RAISE",
       139170  +  /*   71 */ "RECURSIVE",
       139171  +  /*   72 */ "REPLACE",
       139172  +  /*   73 */ "RESTRICT",
       139173  +  /*   74 */ "ROW",
       139174  +  /*   75 */ "TRIGGER",
       139175  +  /*   76 */ "VACUUM",
       139176  +  /*   77 */ "VIEW",
       139177  +  /*   78 */ "VIRTUAL",
       139178  +  /*   79 */ "WITH",
       139179  +  /*   80 */ "REINDEX",
       139180  +  /*   81 */ "RENAME",
       139181  +  /*   82 */ "CTIME_KW",
       139182  +  /*   83 */ "ANY",
       139183  +  /*   84 */ "BITAND",
       139184  +  /*   85 */ "BITOR",
       139185  +  /*   86 */ "LSHIFT",
       139186  +  /*   87 */ "RSHIFT",
       139187  +  /*   88 */ "PLUS",
       139188  +  /*   89 */ "MINUS",
       139189  +  /*   90 */ "STAR",
       139190  +  /*   91 */ "SLASH",
       139191  +  /*   92 */ "REM",
       139192  +  /*   93 */ "CONCAT",
       139193  +  /*   94 */ "COLLATE",
       139194  +  /*   95 */ "BITNOT",
       139195  +  /*   96 */ "INDEXED",
       139196  +  /*   97 */ "STRING",
       139197  +  /*   98 */ "JOIN_KW",
       139198  +  /*   99 */ "CONSTRAINT",
       139199  +  /*  100 */ "DEFAULT",
       139200  +  /*  101 */ "NULL",
       139201  +  /*  102 */ "PRIMARY",
       139202  +  /*  103 */ "UNIQUE",
       139203  +  /*  104 */ "CHECK",
       139204  +  /*  105 */ "REFERENCES",
       139205  +  /*  106 */ "AUTOINCR",
       139206  +  /*  107 */ "ON",
       139207  +  /*  108 */ "INSERT",
       139208  +  /*  109 */ "DELETE",
       139209  +  /*  110 */ "UPDATE",
       139210  +  /*  111 */ "SET",
       139211  +  /*  112 */ "DEFERRABLE",
       139212  +  /*  113 */ "FOREIGN",
       139213  +  /*  114 */ "DROP",
       139214  +  /*  115 */ "UNION",
       139215  +  /*  116 */ "ALL",
       139216  +  /*  117 */ "EXCEPT",
       139217  +  /*  118 */ "INTERSECT",
       139218  +  /*  119 */ "SELECT",
       139219  +  /*  120 */ "VALUES",
       139220  +  /*  121 */ "DISTINCT",
       139221  +  /*  122 */ "DOT",
       139222  +  /*  123 */ "FROM",
       139223  +  /*  124 */ "JOIN",
       139224  +  /*  125 */ "USING",
       139225  +  /*  126 */ "ORDER",
       139226  +  /*  127 */ "GROUP",
       139227  +  /*  128 */ "HAVING",
       139228  +  /*  129 */ "LIMIT",
       139229  +  /*  130 */ "WHERE",
       139230  +  /*  131 */ "INTO",
       139231  +  /*  132 */ "FLOAT",
       139232  +  /*  133 */ "BLOB",
       139233  +  /*  134 */ "INTEGER",
       139234  +  /*  135 */ "VARIABLE",
       139235  +  /*  136 */ "CASE",
       139236  +  /*  137 */ "WHEN",
       139237  +  /*  138 */ "THEN",
       139238  +  /*  139 */ "ELSE",
       139239  +  /*  140 */ "INDEX",
       139240  +  /*  141 */ "ALTER",
       139241  +  /*  142 */ "ADD",
       139242  +  /*  143 */ "error",
       139243  +  /*  144 */ "input",
       139244  +  /*  145 */ "cmdlist",
       139245  +  /*  146 */ "ecmd",
       139246  +  /*  147 */ "explain",
       139247  +  /*  148 */ "cmdx",
       139248  +  /*  149 */ "cmd",
       139249  +  /*  150 */ "transtype",
       139250  +  /*  151 */ "trans_opt",
       139251  +  /*  152 */ "nm",
       139252  +  /*  153 */ "savepoint_opt",
       139253  +  /*  154 */ "create_table",
       139254  +  /*  155 */ "create_table_args",
       139255  +  /*  156 */ "createkw",
       139256  +  /*  157 */ "temp",
       139257  +  /*  158 */ "ifnotexists",
       139258  +  /*  159 */ "dbnm",
       139259  +  /*  160 */ "columnlist",
       139260  +  /*  161 */ "conslist_opt",
       139261  +  /*  162 */ "table_options",
       139262  +  /*  163 */ "select",
       139263  +  /*  164 */ "columnname",
       139264  +  /*  165 */ "carglist",
       139265  +  /*  166 */ "typetoken",
       139266  +  /*  167 */ "typename",
       139267  +  /*  168 */ "signed",
       139268  +  /*  169 */ "plus_num",
       139269  +  /*  170 */ "minus_num",
       139270  +  /*  171 */ "scanpt",
       139271  +  /*  172 */ "ccons",
       139272  +  /*  173 */ "term",
       139273  +  /*  174 */ "expr",
       139274  +  /*  175 */ "onconf",
       139275  +  /*  176 */ "sortorder",
       139276  +  /*  177 */ "autoinc",
       139277  +  /*  178 */ "eidlist_opt",
       139278  +  /*  179 */ "refargs",
       139279  +  /*  180 */ "defer_subclause",
       139280  +  /*  181 */ "refarg",
       139281  +  /*  182 */ "refact",
       139282  +  /*  183 */ "init_deferred_pred_opt",
       139283  +  /*  184 */ "conslist",
       139284  +  /*  185 */ "tconscomma",
       139285  +  /*  186 */ "tcons",
       139286  +  /*  187 */ "sortlist",
       139287  +  /*  188 */ "eidlist",
       139288  +  /*  189 */ "defer_subclause_opt",
       139289  +  /*  190 */ "orconf",
       139290  +  /*  191 */ "resolvetype",
       139291  +  /*  192 */ "raisetype",
       139292  +  /*  193 */ "ifexists",
       139293  +  /*  194 */ "fullname",
       139294  +  /*  195 */ "selectnowith",
       139295  +  /*  196 */ "oneselect",
       139296  +  /*  197 */ "with",
       139297  +  /*  198 */ "multiselect_op",
       139298  +  /*  199 */ "distinct",
       139299  +  /*  200 */ "selcollist",
       139300  +  /*  201 */ "from",
       139301  +  /*  202 */ "where_opt",
       139302  +  /*  203 */ "groupby_opt",
       139303  +  /*  204 */ "having_opt",
       139304  +  /*  205 */ "orderby_opt",
       139305  +  /*  206 */ "limit_opt",
       139306  +  /*  207 */ "values",
       139307  +  /*  208 */ "nexprlist",
       139308  +  /*  209 */ "exprlist",
       139309  +  /*  210 */ "sclp",
       139310  +  /*  211 */ "as",
       139311  +  /*  212 */ "seltablist",
       139312  +  /*  213 */ "stl_prefix",
       139313  +  /*  214 */ "joinop",
       139314  +  /*  215 */ "indexed_opt",
       139315  +  /*  216 */ "on_opt",
       139316  +  /*  217 */ "using_opt",
       139317  +  /*  218 */ "idlist",
       139318  +  /*  219 */ "setlist",
       139319  +  /*  220 */ "insert_cmd",
       139320  +  /*  221 */ "idlist_opt",
       139321  +  /*  222 */ "likeop",
       139322  +  /*  223 */ "between_op",
       139323  +  /*  224 */ "in_op",
       139324  +  /*  225 */ "paren_exprlist",
       139325  +  /*  226 */ "case_operand",
       139326  +  /*  227 */ "case_exprlist",
       139327  +  /*  228 */ "case_else",
       139328  +  /*  229 */ "uniqueflag",
       139329  +  /*  230 */ "collate",
       139330  +  /*  231 */ "nmnum",
       139331  +  /*  232 */ "trigger_decl",
       139332  +  /*  233 */ "trigger_cmd_list",
       139333  +  /*  234 */ "trigger_time",
       139334  +  /*  235 */ "trigger_event",
       139335  +  /*  236 */ "foreach_clause",
       139336  +  /*  237 */ "when_clause",
       139337  +  /*  238 */ "trigger_cmd",
       139338  +  /*  239 */ "trnm",
       139339  +  /*  240 */ "tridxby",
       139340  +  /*  241 */ "database_kw_opt",
       139341  +  /*  242 */ "key_opt",
       139342  +  /*  243 */ "add_column_fullname",
       139343  +  /*  244 */ "kwcolumn_opt",
       139344  +  /*  245 */ "create_vtab",
       139345  +  /*  246 */ "vtabarglist",
       139346  +  /*  247 */ "vtabarg",
       139347  +  /*  248 */ "vtabargtoken",
       139348  +  /*  249 */ "lp",
       139349  +  /*  250 */ "anylist",
       139350  +  /*  251 */ "wqlist",
       139351  +};
       139352  +#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
138020 139353   
138021 139354   #ifndef NDEBUG
138022 139355   /* For tracing reduce actions, the names of all rules are required.
138023 139356   */
138024 139357   static const char *const yyRuleName[] = {
138025 139358    /*   0 */ "explain ::= EXPLAIN",
138026 139359    /*   1 */ "explain ::= EXPLAIN QUERY PLAN",
................................................................................
138046 139379    /*  21 */ "table_options ::=",
138047 139380    /*  22 */ "table_options ::= WITHOUT nm",
138048 139381    /*  23 */ "columnname ::= nm typetoken",
138049 139382    /*  24 */ "typetoken ::=",
138050 139383    /*  25 */ "typetoken ::= typename LP signed RP",
138051 139384    /*  26 */ "typetoken ::= typename LP signed COMMA signed RP",
138052 139385    /*  27 */ "typename ::= typename ID|STRING",
138053         - /*  28 */ "ccons ::= CONSTRAINT nm",
138054         - /*  29 */ "ccons ::= DEFAULT term",
138055         - /*  30 */ "ccons ::= DEFAULT LP expr RP",
138056         - /*  31 */ "ccons ::= DEFAULT PLUS term",
138057         - /*  32 */ "ccons ::= DEFAULT MINUS term",
138058         - /*  33 */ "ccons ::= DEFAULT ID|INDEXED",
138059         - /*  34 */ "ccons ::= NOT NULL onconf",
138060         - /*  35 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
138061         - /*  36 */ "ccons ::= UNIQUE onconf",
138062         - /*  37 */ "ccons ::= CHECK LP expr RP",
138063         - /*  38 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
138064         - /*  39 */ "ccons ::= defer_subclause",
138065         - /*  40 */ "ccons ::= COLLATE ID|STRING",
138066         - /*  41 */ "autoinc ::=",
138067         - /*  42 */ "autoinc ::= AUTOINCR",
138068         - /*  43 */ "refargs ::=",
138069         - /*  44 */ "refargs ::= refargs refarg",
138070         - /*  45 */ "refarg ::= MATCH nm",
138071         - /*  46 */ "refarg ::= ON INSERT refact",
138072         - /*  47 */ "refarg ::= ON DELETE refact",
138073         - /*  48 */ "refarg ::= ON UPDATE refact",
138074         - /*  49 */ "refact ::= SET NULL",
138075         - /*  50 */ "refact ::= SET DEFAULT",
138076         - /*  51 */ "refact ::= CASCADE",
138077         - /*  52 */ "refact ::= RESTRICT",
138078         - /*  53 */ "refact ::= NO ACTION",
138079         - /*  54 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
138080         - /*  55 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
138081         - /*  56 */ "init_deferred_pred_opt ::=",
138082         - /*  57 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
138083         - /*  58 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
138084         - /*  59 */ "conslist_opt ::=",
138085         - /*  60 */ "tconscomma ::= COMMA",
138086         - /*  61 */ "tcons ::= CONSTRAINT nm",
138087         - /*  62 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
138088         - /*  63 */ "tcons ::= UNIQUE LP sortlist RP onconf",
138089         - /*  64 */ "tcons ::= CHECK LP expr RP onconf",
138090         - /*  65 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
138091         - /*  66 */ "defer_subclause_opt ::=",
138092         - /*  67 */ "onconf ::=",
138093         - /*  68 */ "onconf ::= ON CONFLICT resolvetype",
138094         - /*  69 */ "orconf ::=",
138095         - /*  70 */ "orconf ::= OR resolvetype",
138096         - /*  71 */ "resolvetype ::= IGNORE",
138097         - /*  72 */ "resolvetype ::= REPLACE",
138098         - /*  73 */ "cmd ::= DROP TABLE ifexists fullname",
138099         - /*  74 */ "ifexists ::= IF EXISTS",
138100         - /*  75 */ "ifexists ::=",
138101         - /*  76 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
138102         - /*  77 */ "cmd ::= DROP VIEW ifexists fullname",
138103         - /*  78 */ "cmd ::= select",
138104         - /*  79 */ "select ::= with selectnowith",
138105         - /*  80 */ "selectnowith ::= selectnowith multiselect_op oneselect",
138106         - /*  81 */ "multiselect_op ::= UNION",
138107         - /*  82 */ "multiselect_op ::= UNION ALL",
138108         - /*  83 */ "multiselect_op ::= EXCEPT|INTERSECT",
138109         - /*  84 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
138110         - /*  85 */ "values ::= VALUES LP nexprlist RP",
138111         - /*  86 */ "values ::= values COMMA LP exprlist RP",
138112         - /*  87 */ "distinct ::= DISTINCT",
138113         - /*  88 */ "distinct ::= ALL",
138114         - /*  89 */ "distinct ::=",
138115         - /*  90 */ "sclp ::=",
138116         - /*  91 */ "selcollist ::= sclp expr as",
138117         - /*  92 */ "selcollist ::= sclp STAR",
138118         - /*  93 */ "selcollist ::= sclp nm DOT STAR",
138119         - /*  94 */ "as ::= AS nm",
138120         - /*  95 */ "as ::=",
138121         - /*  96 */ "from ::=",
138122         - /*  97 */ "from ::= FROM seltablist",
138123         - /*  98 */ "stl_prefix ::= seltablist joinop",
138124         - /*  99 */ "stl_prefix ::=",
138125         - /* 100 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
138126         - /* 101 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt",
138127         - /* 102 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
138128         - /* 103 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
138129         - /* 104 */ "dbnm ::=",
138130         - /* 105 */ "dbnm ::= DOT nm",
138131         - /* 106 */ "fullname ::= nm dbnm",
138132         - /* 107 */ "joinop ::= COMMA|JOIN",
138133         - /* 108 */ "joinop ::= JOIN_KW JOIN",
138134         - /* 109 */ "joinop ::= JOIN_KW nm JOIN",
138135         - /* 110 */ "joinop ::= JOIN_KW nm nm JOIN",
138136         - /* 111 */ "on_opt ::= ON expr",
138137         - /* 112 */ "on_opt ::=",
138138         - /* 113 */ "indexed_opt ::=",
138139         - /* 114 */ "indexed_opt ::= INDEXED BY nm",
138140         - /* 115 */ "indexed_opt ::= NOT INDEXED",
138141         - /* 116 */ "using_opt ::= USING LP idlist RP",
138142         - /* 117 */ "using_opt ::=",
138143         - /* 118 */ "orderby_opt ::=",
138144         - /* 119 */ "orderby_opt ::= ORDER BY sortlist",
138145         - /* 120 */ "sortlist ::= sortlist COMMA expr sortorder",
138146         - /* 121 */ "sortlist ::= expr sortorder",
138147         - /* 122 */ "sortorder ::= ASC",
138148         - /* 123 */ "sortorder ::= DESC",
138149         - /* 124 */ "sortorder ::=",
138150         - /* 125 */ "groupby_opt ::=",
138151         - /* 126 */ "groupby_opt ::= GROUP BY nexprlist",
138152         - /* 127 */ "having_opt ::=",
138153         - /* 128 */ "having_opt ::= HAVING expr",
138154         - /* 129 */ "limit_opt ::=",
138155         - /* 130 */ "limit_opt ::= LIMIT expr",
138156         - /* 131 */ "limit_opt ::= LIMIT expr OFFSET expr",
138157         - /* 132 */ "limit_opt ::= LIMIT expr COMMA expr",
138158         - /* 133 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt",
138159         - /* 134 */ "where_opt ::=",
138160         - /* 135 */ "where_opt ::= WHERE expr",
138161         - /* 136 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt",
138162         - /* 137 */ "setlist ::= setlist COMMA nm EQ expr",
138163         - /* 138 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
138164         - /* 139 */ "setlist ::= nm EQ expr",
138165         - /* 140 */ "setlist ::= LP idlist RP EQ expr",
138166         - /* 141 */ "cmd ::= with insert_cmd INTO fullname idlist_opt select",
138167         - /* 142 */ "cmd ::= with insert_cmd INTO fullname idlist_opt DEFAULT VALUES",
138168         - /* 143 */ "insert_cmd ::= INSERT orconf",
138169         - /* 144 */ "insert_cmd ::= REPLACE",
138170         - /* 145 */ "idlist_opt ::=",
138171         - /* 146 */ "idlist_opt ::= LP idlist RP",
138172         - /* 147 */ "idlist ::= idlist COMMA nm",
138173         - /* 148 */ "idlist ::= nm",
138174         - /* 149 */ "expr ::= LP expr RP",
138175         - /* 150 */ "expr ::= ID|INDEXED",
138176         - /* 151 */ "expr ::= JOIN_KW",
138177         - /* 152 */ "expr ::= nm DOT nm",
138178         - /* 153 */ "expr ::= nm DOT nm DOT nm",
138179         - /* 154 */ "term ::= NULL|FLOAT|BLOB",
138180         - /* 155 */ "term ::= STRING",
138181         - /* 156 */ "term ::= INTEGER",
138182         - /* 157 */ "expr ::= VARIABLE",
138183         - /* 158 */ "expr ::= expr COLLATE ID|STRING",
138184         - /* 159 */ "expr ::= CAST LP expr AS typetoken RP",
138185         - /* 160 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
138186         - /* 161 */ "expr ::= ID|INDEXED LP STAR RP",
138187         - /* 162 */ "term ::= CTIME_KW",
138188         - /* 163 */ "expr ::= LP nexprlist COMMA expr RP",
138189         - /* 164 */ "expr ::= expr AND expr",
138190         - /* 165 */ "expr ::= expr OR expr",
138191         - /* 166 */ "expr ::= expr LT|GT|GE|LE expr",
138192         - /* 167 */ "expr ::= expr EQ|NE expr",
138193         - /* 168 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
138194         - /* 169 */ "expr ::= expr PLUS|MINUS expr",
138195         - /* 170 */ "expr ::= expr STAR|SLASH|REM expr",
138196         - /* 171 */ "expr ::= expr CONCAT expr",
138197         - /* 172 */ "likeop ::= NOT LIKE_KW|MATCH",
138198         - /* 173 */ "expr ::= expr likeop expr",
138199         - /* 174 */ "expr ::= expr likeop expr ESCAPE expr",
138200         - /* 175 */ "expr ::= expr ISNULL|NOTNULL",
138201         - /* 176 */ "expr ::= expr NOT NULL",
138202         - /* 177 */ "expr ::= expr IS expr",
138203         - /* 178 */ "expr ::= expr IS NOT expr",
138204         - /* 179 */ "expr ::= NOT expr",
138205         - /* 180 */ "expr ::= BITNOT expr",
138206         - /* 181 */ "expr ::= MINUS expr",
138207         - /* 182 */ "expr ::= PLUS expr",
138208         - /* 183 */ "between_op ::= BETWEEN",
138209         - /* 184 */ "between_op ::= NOT BETWEEN",
138210         - /* 185 */ "expr ::= expr between_op expr AND expr",
138211         - /* 186 */ "in_op ::= IN",
138212         - /* 187 */ "in_op ::= NOT IN",
138213         - /* 188 */ "expr ::= expr in_op LP exprlist RP",
138214         - /* 189 */ "expr ::= LP select RP",
138215         - /* 190 */ "expr ::= expr in_op LP select RP",
138216         - /* 191 */ "expr ::= expr in_op nm dbnm paren_exprlist",
138217         - /* 192 */ "expr ::= EXISTS LP select RP",
138218         - /* 193 */ "expr ::= CASE case_operand case_exprlist case_else END",
138219         - /* 194 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
138220         - /* 195 */ "case_exprlist ::= WHEN expr THEN expr",
138221         - /* 196 */ "case_else ::= ELSE expr",
138222         - /* 197 */ "case_else ::=",
138223         - /* 198 */ "case_operand ::= expr",
138224         - /* 199 */ "case_operand ::=",
138225         - /* 200 */ "exprlist ::=",
138226         - /* 201 */ "nexprlist ::= nexprlist COMMA expr",
138227         - /* 202 */ "nexprlist ::= expr",
138228         - /* 203 */ "paren_exprlist ::=",
138229         - /* 204 */ "paren_exprlist ::= LP exprlist RP",
138230         - /* 205 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
138231         - /* 206 */ "uniqueflag ::= UNIQUE",
138232         - /* 207 */ "uniqueflag ::=",
138233         - /* 208 */ "eidlist_opt ::=",
138234         - /* 209 */ "eidlist_opt ::= LP eidlist RP",
138235         - /* 210 */ "eidlist ::= eidlist COMMA nm collate sortorder",
138236         - /* 211 */ "eidlist ::= nm collate sortorder",
138237         - /* 212 */ "collate ::=",
138238         - /* 213 */ "collate ::= COLLATE ID|STRING",
138239         - /* 214 */ "cmd ::= DROP INDEX ifexists fullname",
138240         - /* 215 */ "cmd ::= VACUUM",
138241         - /* 216 */ "cmd ::= VACUUM nm",
138242         - /* 217 */ "cmd ::= PRAGMA nm dbnm",
138243         - /* 218 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
138244         - /* 219 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
138245         - /* 220 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
138246         - /* 221 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
138247         - /* 222 */ "plus_num ::= PLUS INTEGER|FLOAT",
138248         - /* 223 */ "minus_num ::= MINUS INTEGER|FLOAT",
138249         - /* 224 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
138250         - /* 225 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
138251         - /* 226 */ "trigger_time ::= BEFORE|AFTER",
138252         - /* 227 */ "trigger_time ::= INSTEAD OF",
138253         - /* 228 */ "trigger_time ::=",
138254         - /* 229 */ "trigger_event ::= DELETE|INSERT",
138255         - /* 230 */ "trigger_event ::= UPDATE",
138256         - /* 231 */ "trigger_event ::= UPDATE OF idlist",
138257         - /* 232 */ "when_clause ::=",
138258         - /* 233 */ "when_clause ::= WHEN expr",
138259         - /* 234 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
138260         - /* 235 */ "trigger_cmd_list ::= trigger_cmd SEMI",
138261         - /* 236 */ "trnm ::= nm DOT nm",
138262         - /* 237 */ "tridxby ::= INDEXED BY nm",
138263         - /* 238 */ "tridxby ::= NOT INDEXED",
138264         - /* 239 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
138265         - /* 240 */ "trigger_cmd ::= insert_cmd INTO trnm idlist_opt select",
138266         - /* 241 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
138267         - /* 242 */ "trigger_cmd ::= select",
138268         - /* 243 */ "expr ::= RAISE LP IGNORE RP",
138269         - /* 244 */ "expr ::= RAISE LP raisetype COMMA nm RP",
138270         - /* 245 */ "raisetype ::= ROLLBACK",
138271         - /* 246 */ "raisetype ::= ABORT",
138272         - /* 247 */ "raisetype ::= FAIL",
138273         - /* 248 */ "cmd ::= DROP TRIGGER ifexists fullname",
138274         - /* 249 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
138275         - /* 250 */ "cmd ::= DETACH database_kw_opt expr",
138276         - /* 251 */ "key_opt ::=",
138277         - /* 252 */ "key_opt ::= KEY expr",
138278         - /* 253 */ "cmd ::= REINDEX",
138279         - /* 254 */ "cmd ::= REINDEX nm dbnm",
138280         - /* 255 */ "cmd ::= ANALYZE",
138281         - /* 256 */ "cmd ::= ANALYZE nm dbnm",
138282         - /* 257 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
138283         - /* 258 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
138284         - /* 259 */ "add_column_fullname ::= fullname",
138285         - /* 260 */ "cmd ::= create_vtab",
138286         - /* 261 */ "cmd ::= create_vtab LP vtabarglist RP",
138287         - /* 262 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
138288         - /* 263 */ "vtabarg ::=",
138289         - /* 264 */ "vtabargtoken ::= ANY",
138290         - /* 265 */ "vtabargtoken ::= lp anylist RP",
138291         - /* 266 */ "lp ::= LP",
138292         - /* 267 */ "with ::=",
138293         - /* 268 */ "with ::= WITH wqlist",
138294         - /* 269 */ "with ::= WITH RECURSIVE wqlist",
138295         - /* 270 */ "wqlist ::= nm eidlist_opt AS LP select RP",
138296         - /* 271 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
138297         - /* 272 */ "input ::= cmdlist",
138298         - /* 273 */ "cmdlist ::= cmdlist ecmd",
138299         - /* 274 */ "cmdlist ::= ecmd",
138300         - /* 275 */ "ecmd ::= SEMI",
138301         - /* 276 */ "ecmd ::= explain cmdx SEMI",
138302         - /* 277 */ "explain ::=",
138303         - /* 278 */ "trans_opt ::=",
138304         - /* 279 */ "trans_opt ::= TRANSACTION",
138305         - /* 280 */ "trans_opt ::= TRANSACTION nm",
138306         - /* 281 */ "savepoint_opt ::= SAVEPOINT",
138307         - /* 282 */ "savepoint_opt ::=",
138308         - /* 283 */ "cmd ::= create_table create_table_args",
138309         - /* 284 */ "columnlist ::= columnlist COMMA columnname carglist",
138310         - /* 285 */ "columnlist ::= columnname carglist",
138311         - /* 286 */ "nm ::= ID|INDEXED",
138312         - /* 287 */ "nm ::= STRING",
138313         - /* 288 */ "nm ::= JOIN_KW",
138314         - /* 289 */ "typetoken ::= typename",
138315         - /* 290 */ "typename ::= ID|STRING",
138316         - /* 291 */ "signed ::= plus_num",
138317         - /* 292 */ "signed ::= minus_num",
138318         - /* 293 */ "carglist ::= carglist ccons",
138319         - /* 294 */ "carglist ::=",
138320         - /* 295 */ "ccons ::= NULL onconf",
138321         - /* 296 */ "conslist_opt ::= COMMA conslist",
138322         - /* 297 */ "conslist ::= conslist tconscomma tcons",
138323         - /* 298 */ "conslist ::= tcons",
138324         - /* 299 */ "tconscomma ::=",
138325         - /* 300 */ "defer_subclause_opt ::= defer_subclause",
138326         - /* 301 */ "resolvetype ::= raisetype",
138327         - /* 302 */ "selectnowith ::= oneselect",
138328         - /* 303 */ "oneselect ::= values",
138329         - /* 304 */ "sclp ::= selcollist COMMA",
138330         - /* 305 */ "as ::= ID|STRING",
138331         - /* 306 */ "expr ::= term",
138332         - /* 307 */ "likeop ::= LIKE_KW|MATCH",
138333         - /* 308 */ "exprlist ::= nexprlist",
138334         - /* 309 */ "nmnum ::= plus_num",
138335         - /* 310 */ "nmnum ::= nm",
138336         - /* 311 */ "nmnum ::= ON",
138337         - /* 312 */ "nmnum ::= DELETE",
138338         - /* 313 */ "nmnum ::= DEFAULT",
138339         - /* 314 */ "plus_num ::= INTEGER|FLOAT",
138340         - /* 315 */ "foreach_clause ::=",
138341         - /* 316 */ "foreach_clause ::= FOR EACH ROW",
138342         - /* 317 */ "trnm ::= nm",
138343         - /* 318 */ "tridxby ::=",
138344         - /* 319 */ "database_kw_opt ::= DATABASE",
138345         - /* 320 */ "database_kw_opt ::=",
138346         - /* 321 */ "kwcolumn_opt ::=",
138347         - /* 322 */ "kwcolumn_opt ::= COLUMNKW",
138348         - /* 323 */ "vtabarglist ::= vtabarg",
138349         - /* 324 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
138350         - /* 325 */ "vtabarg ::= vtabarg vtabargtoken",
138351         - /* 326 */ "anylist ::=",
138352         - /* 327 */ "anylist ::= anylist LP anylist RP",
138353         - /* 328 */ "anylist ::= anylist ANY",
       139386  + /*  28 */ "scanpt ::=",
       139387  + /*  29 */ "ccons ::= CONSTRAINT nm",
       139388  + /*  30 */ "ccons ::= DEFAULT scanpt term scanpt",
       139389  + /*  31 */ "ccons ::= DEFAULT LP expr RP",
       139390  + /*  32 */ "ccons ::= DEFAULT PLUS term scanpt",
       139391  + /*  33 */ "ccons ::= DEFAULT MINUS term scanpt",
       139392  + /*  34 */ "ccons ::= DEFAULT scanpt ID|INDEXED",
       139393  + /*  35 */ "ccons ::= NOT NULL onconf",
       139394  + /*  36 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
       139395  + /*  37 */ "ccons ::= UNIQUE onconf",
       139396  + /*  38 */ "ccons ::= CHECK LP expr RP",
       139397  + /*  39 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
       139398  + /*  40 */ "ccons ::= defer_subclause",
       139399  + /*  41 */ "ccons ::= COLLATE ID|STRING",
       139400  + /*  42 */ "autoinc ::=",
       139401  + /*  43 */ "autoinc ::= AUTOINCR",
       139402  + /*  44 */ "refargs ::=",
       139403  + /*  45 */ "refargs ::= refargs refarg",
       139404  + /*  46 */ "refarg ::= MATCH nm",
       139405  + /*  47 */ "refarg ::= ON INSERT refact",
       139406  + /*  48 */ "refarg ::= ON DELETE refact",
       139407  + /*  49 */ "refarg ::= ON UPDATE refact",
       139408  + /*  50 */ "refact ::= SET NULL",
       139409  + /*  51 */ "refact ::= SET DEFAULT",
       139410  + /*  52 */ "refact ::= CASCADE",
       139411  + /*  53 */ "refact ::= RESTRICT",
       139412  + /*  54 */ "refact ::= NO ACTION",
       139413  + /*  55 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
       139414  + /*  56 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
       139415  + /*  57 */ "init_deferred_pred_opt ::=",
       139416  + /*  58 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
       139417  + /*  59 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
       139418  + /*  60 */ "conslist_opt ::=",
       139419  + /*  61 */ "tconscomma ::= COMMA",
       139420  + /*  62 */ "tcons ::= CONSTRAINT nm",
       139421  + /*  63 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
       139422  + /*  64 */ "tcons ::= UNIQUE LP sortlist RP onconf",
       139423  + /*  65 */ "tcons ::= CHECK LP expr RP onconf",
       139424  + /*  66 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
       139425  + /*  67 */ "defer_subclause_opt ::=",
       139426  + /*  68 */ "onconf ::=",
       139427  + /*  69 */ "onconf ::= ON CONFLICT resolvetype",
       139428  + /*  70 */ "orconf ::=",
       139429  + /*  71 */ "orconf ::= OR resolvetype",
       139430  + /*  72 */ "resolvetype ::= IGNORE",
       139431  + /*  73 */ "resolvetype ::= REPLACE",
       139432  + /*  74 */ "cmd ::= DROP TABLE ifexists fullname",
       139433  + /*  75 */ "ifexists ::= IF EXISTS",
       139434  + /*  76 */ "ifexists ::=",
       139435  + /*  77 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
       139436  + /*  78 */ "cmd ::= DROP VIEW ifexists fullname",
       139437  + /*  79 */ "cmd ::= select",
       139438  + /*  80 */ "select ::= with selectnowith",
       139439  + /*  81 */ "selectnowith ::= selectnowith multiselect_op oneselect",
       139440  + /*  82 */ "multiselect_op ::= UNION",
       139441  + /*  83 */ "multiselect_op ::= UNION ALL",
       139442  + /*  84 */ "multiselect_op ::= EXCEPT|INTERSECT",
       139443  + /*  85 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
       139444  + /*  86 */ "values ::= VALUES LP nexprlist RP",
       139445  + /*  87 */ "values ::= values COMMA LP exprlist RP",
       139446  + /*  88 */ "distinct ::= DISTINCT",
       139447  + /*  89 */ "distinct ::= ALL",
       139448  + /*  90 */ "distinct ::=",
       139449  + /*  91 */ "sclp ::=",
       139450  + /*  92 */ "selcollist ::= sclp scanpt expr scanpt as",
       139451  + /*  93 */ "selcollist ::= sclp scanpt STAR",
       139452  + /*  94 */ "selcollist ::= sclp scanpt nm DOT STAR",
       139453  + /*  95 */ "as ::= AS nm",
       139454  + /*  96 */ "as ::=",
       139455  + /*  97 */ "from ::=",
       139456  + /*  98 */ "from ::= FROM seltablist",
       139457  + /*  99 */ "stl_prefix ::= seltablist joinop",
       139458  + /* 100 */ "stl_prefix ::=",
       139459  + /* 101 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
       139460  + /* 102 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt",
       139461  + /* 103 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
       139462  + /* 104 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
       139463  + /* 105 */ "dbnm ::=",
       139464  + /* 106 */ "dbnm ::= DOT nm",
       139465  + /* 107 */ "fullname ::= nm dbnm",
       139466  + /* 108 */ "joinop ::= COMMA|JOIN",
       139467  + /* 109 */ "joinop ::= JOIN_KW JOIN",
       139468  + /* 110 */ "joinop ::= JOIN_KW nm JOIN",
       139469  + /* 111 */ "joinop ::= JOIN_KW nm nm JOIN",
       139470  + /* 112 */ "on_opt ::= ON expr",
       139471  + /* 113 */ "on_opt ::=",
       139472  + /* 114 */ "indexed_opt ::=",
       139473  + /* 115 */ "indexed_opt ::= INDEXED BY nm",
       139474  + /* 116 */ "indexed_opt ::= NOT INDEXED",
       139475  + /* 117 */ "using_opt ::= USING LP idlist RP",
       139476  + /* 118 */ "using_opt ::=",
       139477  + /* 119 */ "orderby_opt ::=",
       139478  + /* 120 */ "orderby_opt ::= ORDER BY sortlist",
       139479  + /* 121 */ "sortlist ::= sortlist COMMA expr sortorder",
       139480  + /* 122 */ "sortlist ::= expr sortorder",
       139481  + /* 123 */ "sortorder ::= ASC",
       139482  + /* 124 */ "sortorder ::= DESC",
       139483  + /* 125 */ "sortorder ::=",
       139484  + /* 126 */ "groupby_opt ::=",
       139485  + /* 127 */ "groupby_opt ::= GROUP BY nexprlist",
       139486  + /* 128 */ "having_opt ::=",
       139487  + /* 129 */ "having_opt ::= HAVING expr",
       139488  + /* 130 */ "limit_opt ::=",
       139489  + /* 131 */ "limit_opt ::= LIMIT expr",
       139490  + /* 132 */ "limit_opt ::= LIMIT expr OFFSET expr",
       139491  + /* 133 */ "limit_opt ::= LIMIT expr COMMA expr",
       139492  + /* 134 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt",
       139493  + /* 135 */ "where_opt ::=",
       139494  + /* 136 */ "where_opt ::= WHERE expr",
       139495  + /* 137 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt",
       139496  + /* 138 */ "setlist ::= setlist COMMA nm EQ expr",
       139497  + /* 139 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
       139498  + /* 140 */ "setlist ::= nm EQ expr",
       139499  + /* 141 */ "setlist ::= LP idlist RP EQ expr",
       139500  + /* 142 */ "cmd ::= with insert_cmd INTO fullname idlist_opt select",
       139501  + /* 143 */ "cmd ::= with insert_cmd INTO fullname idlist_opt DEFAULT VALUES",
       139502  + /* 144 */ "insert_cmd ::= INSERT orconf",
       139503  + /* 145 */ "insert_cmd ::= REPLACE",
       139504  + /* 146 */ "idlist_opt ::=",
       139505  + /* 147 */ "idlist_opt ::= LP idlist RP",
       139506  + /* 148 */ "idlist ::= idlist COMMA nm",
       139507  + /* 149 */ "idlist ::= nm",
       139508  + /* 150 */ "expr ::= LP expr RP",
       139509  + /* 151 */ "expr ::= ID|INDEXED",
       139510  + /* 152 */ "expr ::= JOIN_KW",
       139511  + /* 153 */ "expr ::= nm DOT nm",
       139512  + /* 154 */ "expr ::= nm DOT nm DOT nm",
       139513  + /* 155 */ "term ::= NULL|FLOAT|BLOB",
       139514  + /* 156 */ "term ::= STRING",
       139515  + /* 157 */ "term ::= INTEGER",
       139516  + /* 158 */ "expr ::= VARIABLE",
       139517  + /* 159 */ "expr ::= expr COLLATE ID|STRING",
       139518  + /* 160 */ "expr ::= CAST LP expr AS typetoken RP",
       139519  + /* 161 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
       139520  + /* 162 */ "expr ::= ID|INDEXED LP STAR RP",
       139521  + /* 163 */ "term ::= CTIME_KW",
       139522  + /* 164 */ "expr ::= LP nexprlist COMMA expr RP",
       139523  + /* 165 */ "expr ::= expr AND expr",
       139524  + /* 166 */ "expr ::= expr OR expr",
       139525  + /* 167 */ "expr ::= expr LT|GT|GE|LE expr",
       139526  + /* 168 */ "expr ::= expr EQ|NE expr",
       139527  + /* 169 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
       139528  + /* 170 */ "expr ::= expr PLUS|MINUS expr",
       139529  + /* 171 */ "expr ::= expr STAR|SLASH|REM expr",
       139530  + /* 172 */ "expr ::= expr CONCAT expr",
       139531  + /* 173 */ "likeop ::= NOT LIKE_KW|MATCH",
       139532  + /* 174 */ "expr ::= expr likeop expr",
       139533  + /* 175 */ "expr ::= expr likeop expr ESCAPE expr",
       139534  + /* 176 */ "expr ::= expr ISNULL|NOTNULL",
       139535  + /* 177 */ "expr ::= expr NOT NULL",
       139536  + /* 178 */ "expr ::= expr IS expr",
       139537  + /* 179 */ "expr ::= expr IS NOT expr",
       139538  + /* 180 */ "expr ::= NOT expr",
       139539  + /* 181 */ "expr ::= BITNOT expr",
       139540  + /* 182 */ "expr ::= MINUS expr",
       139541  + /* 183 */ "expr ::= PLUS expr",
       139542  + /* 184 */ "between_op ::= BETWEEN",
       139543  + /* 185 */ "between_op ::= NOT BETWEEN",
       139544  + /* 186 */ "expr ::= expr between_op expr AND expr",
       139545  + /* 187 */ "in_op ::= IN",
       139546  + /* 188 */ "in_op ::= NOT IN",
       139547  + /* 189 */ "expr ::= expr in_op LP exprlist RP",
       139548  + /* 190 */ "expr ::= LP select RP",
       139549  + /* 191 */ "expr ::= expr in_op LP select RP",
       139550  + /* 192 */ "expr ::= expr in_op nm dbnm paren_exprlist",
       139551  + /* 193 */ "expr ::= EXISTS LP select RP",
       139552  + /* 194 */ "expr ::= CASE case_operand case_exprlist case_else END",
       139553  + /* 195 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
       139554  + /* 196 */ "case_exprlist ::= WHEN expr THEN expr",
       139555  + /* 197 */ "case_else ::= ELSE expr",
       139556  + /* 198 */ "case_else ::=",
       139557  + /* 199 */ "case_operand ::= expr",
       139558  + /* 200 */ "case_operand ::=",
       139559  + /* 201 */ "exprlist ::=",
       139560  + /* 202 */ "nexprlist ::= nexprlist COMMA expr",
       139561  + /* 203 */ "nexprlist ::= expr",
       139562  + /* 204 */ "paren_exprlist ::=",
       139563  + /* 205 */ "paren_exprlist ::= LP exprlist RP",
       139564  + /* 206 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
       139565  + /* 207 */ "uniqueflag ::= UNIQUE",
       139566  + /* 208 */ "uniqueflag ::=",
       139567  + /* 209 */ "eidlist_opt ::=",
       139568  + /* 210 */ "eidlist_opt ::= LP eidlist RP",
       139569  + /* 211 */ "eidlist ::= eidlist COMMA nm collate sortorder",
       139570  + /* 212 */ "eidlist ::= nm collate sortorder",
       139571  + /* 213 */ "collate ::=",
       139572  + /* 214 */ "collate ::= COLLATE ID|STRING",
       139573  + /* 215 */ "cmd ::= DROP INDEX ifexists fullname",
       139574  + /* 216 */ "cmd ::= VACUUM",
       139575  + /* 217 */ "cmd ::= VACUUM nm",
       139576  + /* 218 */ "cmd ::= PRAGMA nm dbnm",
       139577  + /* 219 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
       139578  + /* 220 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
       139579  + /* 221 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
       139580  + /* 222 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
       139581  + /* 223 */ "plus_num ::= PLUS INTEGER|FLOAT",
       139582  + /* 224 */ "minus_num ::= MINUS INTEGER|FLOAT",
       139583  + /* 225 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
       139584  + /* 226 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
       139585  + /* 227 */ "trigger_time ::= BEFORE|AFTER",
       139586  + /* 228 */ "trigger_time ::= INSTEAD OF",
       139587  + /* 229 */ "trigger_time ::=",
       139588  + /* 230 */ "trigger_event ::= DELETE|INSERT",
       139589  + /* 231 */ "trigger_event ::= UPDATE",
       139590  + /* 232 */ "trigger_event ::= UPDATE OF idlist",
       139591  + /* 233 */ "when_clause ::=",
       139592  + /* 234 */ "when_clause ::= WHEN expr",
       139593  + /* 235 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
       139594  + /* 236 */ "trigger_cmd_list ::= trigger_cmd SEMI",
       139595  + /* 237 */ "trnm ::= nm DOT nm",
       139596  + /* 238 */ "tridxby ::= INDEXED BY nm",
       139597  + /* 239 */ "tridxby ::= NOT INDEXED",
       139598  + /* 240 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt",
       139599  + /* 241 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select scanpt",
       139600  + /* 242 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
       139601  + /* 243 */ "trigger_cmd ::= scanpt select scanpt",
       139602  + /* 244 */ "expr ::= RAISE LP IGNORE RP",
       139603  + /* 245 */ "expr ::= RAISE LP raisetype COMMA nm RP",
       139604  + /* 246 */ "raisetype ::= ROLLBACK",
       139605  + /* 247 */ "raisetype ::= ABORT",
       139606  + /* 248 */ "raisetype ::= FAIL",
       139607  + /* 249 */ "cmd ::= DROP TRIGGER ifexists fullname",
       139608  + /* 250 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
       139609  + /* 251 */ "cmd ::= DETACH database_kw_opt expr",
       139610  + /* 252 */ "key_opt ::=",
       139611  + /* 253 */ "key_opt ::= KEY expr",
       139612  + /* 254 */ "cmd ::= REINDEX",
       139613  + /* 255 */ "cmd ::= REINDEX nm dbnm",
       139614  + /* 256 */ "cmd ::= ANALYZE",
       139615  + /* 257 */ "cmd ::= ANALYZE nm dbnm",
       139616  + /* 258 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
       139617  + /* 259 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
       139618  + /* 260 */ "add_column_fullname ::= fullname",
       139619  + /* 261 */ "cmd ::= create_vtab",
       139620  + /* 262 */ "cmd ::= create_vtab LP vtabarglist RP",
       139621  + /* 263 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
       139622  + /* 264 */ "vtabarg ::=",
       139623  + /* 265 */ "vtabargtoken ::= ANY",
       139624  + /* 266 */ "vtabargtoken ::= lp anylist RP",
       139625  + /* 267 */ "lp ::= LP",
       139626  + /* 268 */ "with ::=",
       139627  + /* 269 */ "with ::= WITH wqlist",
       139628  + /* 270 */ "with ::= WITH RECURSIVE wqlist",
       139629  + /* 271 */ "wqlist ::= nm eidlist_opt AS LP select RP",
       139630  + /* 272 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
       139631  + /* 273 */ "input ::= cmdlist",
       139632  + /* 274 */ "cmdlist ::= cmdlist ecmd",
       139633  + /* 275 */ "cmdlist ::= ecmd",
       139634  + /* 276 */ "ecmd ::= SEMI",
       139635  + /* 277 */ "ecmd ::= explain cmdx SEMI",
       139636  + /* 278 */ "explain ::=",
       139637  + /* 279 */ "trans_opt ::=",
       139638  + /* 280 */ "trans_opt ::= TRANSACTION",
       139639  + /* 281 */ "trans_opt ::= TRANSACTION nm",
       139640  + /* 282 */ "savepoint_opt ::= SAVEPOINT",
       139641  + /* 283 */ "savepoint_opt ::=",
       139642  + /* 284 */ "cmd ::= create_table create_table_args",
       139643  + /* 285 */ "columnlist ::= columnlist COMMA columnname carglist",
       139644  + /* 286 */ "columnlist ::= columnname carglist",
       139645  + /* 287 */ "nm ::= ID|INDEXED",
       139646  + /* 288 */ "nm ::= STRING",
       139647  + /* 289 */ "nm ::= JOIN_KW",
       139648  + /* 290 */ "typetoken ::= typename",
       139649  + /* 291 */ "typename ::= ID|STRING",
       139650  + /* 292 */ "signed ::= plus_num",
       139651  + /* 293 */ "signed ::= minus_num",
       139652  + /* 294 */ "carglist ::= carglist ccons",
       139653  + /* 295 */ "carglist ::=",
       139654  + /* 296 */ "ccons ::= NULL onconf",
       139655  + /* 297 */ "conslist_opt ::= COMMA conslist",
       139656  + /* 298 */ "conslist ::= conslist tconscomma tcons",
       139657  + /* 299 */ "conslist ::= tcons",
       139658  + /* 300 */ "tconscomma ::=",
       139659  + /* 301 */ "defer_subclause_opt ::= defer_subclause",
       139660  + /* 302 */ "resolvetype ::= raisetype",
       139661  + /* 303 */ "selectnowith ::= oneselect",
       139662  + /* 304 */ "oneselect ::= values",
       139663  + /* 305 */ "sclp ::= selcollist COMMA",
       139664  + /* 306 */ "as ::= ID|STRING",
       139665  + /* 307 */ "expr ::= term",
       139666  + /* 308 */ "likeop ::= LIKE_KW|MATCH",
       139667  + /* 309 */ "exprlist ::= nexprlist",
       139668  + /* 310 */ "nmnum ::= plus_num",
       139669  + /* 311 */ "nmnum ::= nm",
       139670  + /* 312 */ "nmnum ::= ON",
       139671  + /* 313 */ "nmnum ::= DELETE",
       139672  + /* 314 */ "nmnum ::= DEFAULT",
       139673  + /* 315 */ "plus_num ::= INTEGER|FLOAT",
       139674  + /* 316 */ "foreach_clause ::=",
       139675  + /* 317 */ "foreach_clause ::= FOR EACH ROW",
       139676  + /* 318 */ "trnm ::= nm",
       139677  + /* 319 */ "tridxby ::=",
       139678  + /* 320 */ "database_kw_opt ::= DATABASE",
       139679  + /* 321 */ "database_kw_opt ::=",
       139680  + /* 322 */ "kwcolumn_opt ::=",
       139681  + /* 323 */ "kwcolumn_opt ::= COLUMNKW",
       139682  + /* 324 */ "vtabarglist ::= vtabarg",
       139683  + /* 325 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
       139684  + /* 326 */ "vtabarg ::= vtabarg vtabargtoken",
       139685  + /* 327 */ "anylist ::=",
       139686  + /* 328 */ "anylist ::= anylist LP anylist RP",
       139687  + /* 329 */ "anylist ::= anylist ANY",
138354 139688   };
138355 139689   #endif /* NDEBUG */
138356 139690   
138357 139691   
138358 139692   #if YYSTACKDEPTH<=0
138359 139693   /*
138360 139694   ** Try to increase the size of the parser stack.  Return the number
................................................................................
138468 139802       **
138469 139803       ** Note: during a reduce, the only symbols destroyed are those
138470 139804       ** which appear on the RHS of the rule, but which are *not* used
138471 139805       ** inside the C code.
138472 139806       */
138473 139807   /********* Begin destructor definitions ***************************************/
138474 139808       case 163: /* select */
138475         -    case 194: /* selectnowith */
138476         -    case 195: /* oneselect */
138477         -    case 206: /* values */
138478         -{
138479         -sqlite3SelectDelete(pParse->db, (yypminor->yy243));
138480         -}
138481         -      break;
138482         -    case 172: /* term */
138483         -    case 173: /* expr */
138484         -{
138485         -sqlite3ExprDelete(pParse->db, (yypminor->yy190).pExpr);
138486         -}
138487         -      break;
138488         -    case 177: /* eidlist_opt */
138489