sqllogictest
Check-in [db74fbad74]
Not logged in

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

Overview
Comment:Update the built-in SQLite to the 3rd 3.18.0 beta.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: db74fbad74fef6a0df63fc613fdb0e731dfa8a31
User & Date: drh 2017-03-27 15:15:49
Context
2017-05-13
20:31
Update the built-in SQLite to the first 3.19.0 beta. Add -DSQLITE_DEBUG to the Makefile.no-odbc. Fix a compiler warning in sqllogictest.c. check-in: 2e07d66b27 user: drh tags: trunk
2017-03-27
15:15
Update the built-in SQLite to the 3rd 3.18.0 beta. check-in: db74fbad74 user: drh tags: trunk
2017-02-08
18:31
Adjust two requirement marks due to clarification of wording. check-in: 2f02c1ea3e user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.17.0.  By combining all the individual C code files into this
            3  +** version 3.18.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
................................................................................
   200    200   # define _LARGE_FILE       1
   201    201   # ifndef _FILE_OFFSET_BITS
   202    202   #   define _FILE_OFFSET_BITS 64
   203    203   # endif
   204    204   # define _LARGEFILE_SOURCE 1
   205    205   #endif
   206    206   
   207         -/* What version of GCC is being used.  0 means GCC is not being used */
   208         -#ifdef __GNUC__
          207  +/* The GCC_VERSION and MSVC_VERSION macros are used to
          208  +** conditionally include optimizations for each of these compilers.  A
          209  +** value of 0 means that compiler is not being used.  The
          210  +** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
          211  +** optimizations, and hence set all compiler macros to 0
          212  +**
          213  +** There was once also a CLANG_VERSION macro.  However, we learn that the
          214  +** version numbers in clang are for "marketing" only and are inconsistent
          215  +** and unreliable.  Fortunately, all versions of clang also recognize the
          216  +** gcc version numbers and have reasonable settings for gcc version numbers,
          217  +** so the GCC_VERSION macro will be set to a correct non-zero value even
          218  +** when compiling with clang.
          219  +*/
          220  +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
   209    221   # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
   210    222   #else
   211    223   # define GCC_VERSION 0
   212    224   #endif
   213         -
   214         -/* What version of CLANG is being used.  0 means CLANG is not being used */
   215         -#if defined(__clang__) && !defined(_WIN32)
   216         -# define CLANG_VERSION \
   217         -            (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
          225  +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
          226  +# define MSVC_VERSION _MSC_VER
   218    227   #else
   219         -# define CLANG_VERSION 0
          228  +# define MSVC_VERSION 0
   220    229   #endif
   221    230   
   222    231   /* Needed for various definitions... */
   223    232   #if defined(__GNUC__) && !defined(_GNU_SOURCE)
   224    233   # define _GNU_SOURCE
   225    234   #endif
   226    235   
................................................................................
   378    387   **
   379    388   ** Since [version 3.6.18] ([dateof:3.6.18]), 
   380    389   ** SQLite source code has been stored in the
   381    390   ** <a href="http://www.fossil-scm.org/">Fossil configuration management
   382    391   ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
   383    392   ** a string which identifies a particular check-in of SQLite
   384    393   ** within its configuration management system.  ^The SQLITE_SOURCE_ID
   385         -** string contains the date and time of the check-in (UTC) and an SHA1
   386         -** hash of the entire source tree.
          394  +** string contains the date and time of the check-in (UTC) and a SHA1
          395  +** or SHA3-256 hash of the entire source tree.
   387    396   **
   388    397   ** See also: [sqlite3_libversion()],
   389    398   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   390    399   ** [sqlite_version()] and [sqlite_source_id()].
   391    400   */
   392         -#define SQLITE_VERSION        "3.17.0"
   393         -#define SQLITE_VERSION_NUMBER 3017000
   394         -#define SQLITE_SOURCE_ID      "2017-02-08 16:01:57 77b470b0df73dc5ae5ad2f0170ef7c50558c7c88"
          401  +#define SQLITE_VERSION        "3.18.0"
          402  +#define SQLITE_VERSION_NUMBER 3018000
          403  +#define SQLITE_SOURCE_ID      "2017-03-25 19:16:41 8469fc0d48d6af0accef9b8a84e08ad2ca32351907510d177b4ca4815c1ea7cb"
   395    404   
   396    405   /*
   397    406   ** CAPI3REF: Run-Time Library Version Numbers
   398    407   ** KEYWORDS: sqlite3_version sqlite3_sourceid
   399    408   **
   400    409   ** These interfaces provide the same information as the [SQLITE_VERSION],
   401    410   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  2304   2313   ** has a unique 64-bit signed
  2305   2314   ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  2306   2315   ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  2307   2316   ** names are not also used by explicitly declared columns. ^If
  2308   2317   ** the table has a column of type [INTEGER PRIMARY KEY] then that column
  2309   2318   ** is another alias for the rowid.
  2310   2319   **
  2311         -** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the 
  2312         -** most recent successful [INSERT] into a rowid table or [virtual table]
  2313         -** on database connection D.
  2314         -** ^Inserts into [WITHOUT ROWID] tables are not recorded.
  2315         -** ^If no successful [INSERT]s into rowid tables
  2316         -** have ever occurred on the database connection D, 
  2317         -** then sqlite3_last_insert_rowid(D) returns zero.
         2320  +** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
         2321  +** the most recent successful [INSERT] into a rowid table or [virtual table]
         2322  +** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
         2323  +** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred 
         2324  +** on the database connection D, then sqlite3_last_insert_rowid(D) returns 
         2325  +** zero.
  2318   2326   **
  2319         -** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
  2320         -** method, then this routine will return the [rowid] of the inserted
  2321         -** row as long as the trigger or virtual table method is running.
  2322         -** But once the trigger or virtual table method ends, the value returned 
  2323         -** by this routine reverts to what it was before the trigger or virtual
  2324         -** table method began.)^
         2327  +** As well as being set automatically as rows are inserted into database
         2328  +** tables, the value returned by this function may be set explicitly by
         2329  +** [sqlite3_set_last_insert_rowid()]
         2330  +**
         2331  +** Some virtual table implementations may INSERT rows into rowid tables as
         2332  +** part of committing a transaction (e.g. to flush data accumulated in memory
         2333  +** to disk). In this case subsequent calls to this function return the rowid
         2334  +** associated with these internal INSERT operations, which leads to 
         2335  +** unintuitive results. Virtual table implementations that do write to rowid
         2336  +** tables in this way can avoid this problem by restoring the original 
         2337  +** rowid value using [sqlite3_set_last_insert_rowid()] before returning 
         2338  +** control to the user.
         2339  +**
         2340  +** ^(If an [INSERT] occurs within a trigger then this routine will 
         2341  +** return the [rowid] of the inserted row as long as the trigger is 
         2342  +** running. Once the trigger program ends, the value returned 
         2343  +** by this routine reverts to what it was before the trigger was fired.)^
  2325   2344   **
  2326   2345   ** ^An [INSERT] that fails due to a constraint violation is not a
  2327   2346   ** successful [INSERT] and does not change the value returned by this
  2328   2347   ** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
  2329   2348   ** and INSERT OR ABORT make no changes to the return value of this
  2330   2349   ** routine when their insertion fails.  ^(When INSERT OR REPLACE
  2331   2350   ** encounters a constraint violation, it does not fail.  The
................................................................................
  2344   2363   ** function is running and thus changes the last insert [rowid],
  2345   2364   ** then the value returned by [sqlite3_last_insert_rowid()] is
  2346   2365   ** unpredictable and might not equal either the old or the new
  2347   2366   ** last insert [rowid].
  2348   2367   */
  2349   2368   SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
  2350   2369   
         2370  +/*
         2371  +** CAPI3REF: Set the Last Insert Rowid value.
         2372  +** METHOD: sqlite3
         2373  +**
         2374  +** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
         2375  +** set the value returned by calling sqlite3_last_insert_rowid(D) to R 
         2376  +** without inserting a row into the database.
         2377  +*/
         2378  +SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
         2379  +
  2351   2380   /*
  2352   2381   ** CAPI3REF: Count The Number Of Rows Modified
  2353   2382   ** METHOD: sqlite3
  2354   2383   **
  2355   2384   ** ^This function returns the number of rows modified, inserted or
  2356   2385   ** deleted by the most recently completed INSERT, UPDATE or DELETE
  2357   2386   ** statement on the database connection specified by the only parameter.
................................................................................
  3668   3697   ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
  3669   3698   **
  3670   3699   ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
  3671   3700   ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
  3672   3701   **
  3673   3702   ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
  3674   3703   ** <dd>The maximum number of instructions in a virtual machine program
  3675         -** used to implement an SQL statement.  This limit is not currently
  3676         -** enforced, though that might be added in some future release of
  3677         -** SQLite.</dd>)^
         3704  +** used to implement an SQL statement.  If [sqlite3_prepare_v2()] or
         3705  +** the equivalent tries to allocate space for more than this many opcodes
         3706  +** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
  3678   3707   **
  3679   3708   ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
  3680   3709   ** <dd>The maximum number of arguments on a function.</dd>)^
  3681   3710   **
  3682   3711   ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
  3683   3712   ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
  3684   3713   **
................................................................................
  3707   3736   #define SQLITE_LIMIT_VDBE_OP                   5
  3708   3737   #define SQLITE_LIMIT_FUNCTION_ARG              6
  3709   3738   #define SQLITE_LIMIT_ATTACHED                  7
  3710   3739   #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
  3711   3740   #define SQLITE_LIMIT_VARIABLE_NUMBER           9
  3712   3741   #define SQLITE_LIMIT_TRIGGER_DEPTH            10
  3713   3742   #define SQLITE_LIMIT_WORKER_THREADS           11
         3743  +
  3714   3744   
  3715   3745   /*
  3716   3746   ** CAPI3REF: Compiling An SQL Statement
  3717   3747   ** KEYWORDS: {SQL statement compiler}
  3718   3748   ** METHOD: sqlite3
  3719   3749   ** CONSTRUCTOR: sqlite3_stmt
  3720   3750   **
................................................................................
 10835  10865   #endif
 10836  10866   
 10837  10867   /*
 10838  10868   ** The maximum number of opcodes in a VDBE program.
 10839  10869   ** Not currently enforced.
 10840  10870   */
 10841  10871   #ifndef SQLITE_MAX_VDBE_OP
 10842         -# define SQLITE_MAX_VDBE_OP 25000
        10872  +# define SQLITE_MAX_VDBE_OP 250000000
 10843  10873   #endif
 10844  10874   
 10845  10875   /*
 10846  10876   ** The maximum number of arguments to an SQL function.
 10847  10877   */
 10848  10878   #ifndef SQLITE_MAX_FUNCTION_ARG
 10849  10879   # define SQLITE_MAX_FUNCTION_ARG 127
................................................................................
 11033  11063   */
 11034  11064   #if !defined(SQLITE_DISABLE_INTRINSIC)
 11035  11065   #  if defined(_MSC_VER) && _MSC_VER>=1400
 11036  11066   #    if !defined(_WIN32_WCE)
 11037  11067   #      include <intrin.h>
 11038  11068   #      pragma intrinsic(_byteswap_ushort)
 11039  11069   #      pragma intrinsic(_byteswap_ulong)
        11070  +#      pragma intrinsic(_byteswap_uint64)
 11040  11071   #      pragma intrinsic(_ReadWriteBarrier)
 11041  11072   #    else
 11042  11073   #      include <cmnintrin.h>
 11043  11074   #    endif
 11044  11075   #  endif
 11045  11076   #endif
 11046  11077   
................................................................................
 11847  11878   
 11848  11879   /*
 11849  11880   ** Macros to determine whether the machine is big or little endian,
 11850  11881   ** and whether or not that determination is run-time or compile-time.
 11851  11882   **
 11852  11883   ** For best performance, an attempt is made to guess at the byte-order
 11853  11884   ** using C-preprocessor macros.  If that is unsuccessful, or if
 11854         -** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
        11885  +** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
 11855  11886   ** at run-time.
 11856  11887   */
 11857         -#if (defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
        11888  +#ifndef SQLITE_BYTEORDER
        11889  +# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
 11858  11890        defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
 11859  11891        defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
 11860         -     defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
 11861         -# define SQLITE_BYTEORDER    1234
 11862         -# define SQLITE_BIGENDIAN    0
 11863         -# define SQLITE_LITTLEENDIAN 1
 11864         -# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
        11892  +     defined(__arm__)
        11893  +#   define SQLITE_BYTEORDER    1234
        11894  +# elif defined(sparc)    || defined(__ppc__)
        11895  +#   define SQLITE_BYTEORDER    4321
        11896  +# else
        11897  +#   define SQLITE_BYTEORDER 0
        11898  +# endif
 11865  11899   #endif
 11866         -#if (defined(sparc)    || defined(__ppc__))  \
 11867         -    && !defined(SQLITE_RUNTIME_BYTEORDER)
 11868         -# define SQLITE_BYTEORDER    4321
        11900  +#if SQLITE_BYTEORDER==4321
 11869  11901   # define SQLITE_BIGENDIAN    1
 11870  11902   # define SQLITE_LITTLEENDIAN 0
 11871  11903   # define SQLITE_UTF16NATIVE  SQLITE_UTF16BE
 11872         -#endif
 11873         -#if !defined(SQLITE_BYTEORDER)
        11904  +#elif SQLITE_BYTEORDER==1234
        11905  +# define SQLITE_BIGENDIAN    0
        11906  +# define SQLITE_LITTLEENDIAN 1
        11907  +# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
        11908  +#else
 11874  11909   # ifdef SQLITE_AMALGAMATION
 11875  11910     const int sqlite3one = 1;
 11876  11911   # else
 11877  11912     extern const int sqlite3one;
 11878  11913   # endif
 11879         -# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
 11880  11914   # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
 11881  11915   # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
 11882  11916   # define SQLITE_UTF16NATIVE  (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
 11883  11917   #endif
 11884  11918   
 11885  11919   /*
 11886  11920   ** Constants for the largest and smallest possible 64-bit signed integers.
................................................................................
 12442  12476   SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
 12443  12477   SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
 12444  12478   SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
 12445  12479   SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
 12446  12480   
 12447  12481   SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
 12448  12482   SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
        12483  +SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*);
 12449  12484   
 12450  12485   #ifndef SQLITE_OMIT_INCRBLOB
 12451  12486   SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
 12452  12487   SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
 12453  12488   SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
 12454  12489   #endif
 12455  12490   SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
................................................................................
 12608  12643   ** A sub-routine used to implement a trigger program.
 12609  12644   */
 12610  12645   struct SubProgram {
 12611  12646     VdbeOp *aOp;                  /* Array of opcodes for sub-program */
 12612  12647     int nOp;                      /* Elements in aOp[] */
 12613  12648     int nMem;                     /* Number of memory cells required */
 12614  12649     int nCsr;                     /* Number of cursors required */
        12650  +  u8 *aOnce;                    /* Array of OP_Once flags */
 12615  12651     void *token;                  /* id that may be used to recursive triggers */
 12616  12652     SubProgram *pNext;            /* Next sub-program already visited */
 12617  12653   };
 12618  12654   
 12619  12655   /*
 12620  12656   ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
 12621  12657   ** it takes up less space.
................................................................................
 12742  12778   #define OP_Subtract       48 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 12743  12779   #define OP_Multiply       49 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 12744  12780   #define OP_Divide         50 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 12745  12781   #define OP_Remainder      51 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 12746  12782   #define OP_Concat         52 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 12747  12783   #define OP_Last           53
 12748  12784   #define OP_BitNot         54 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 12749         -#define OP_SorterSort     55
 12750         -#define OP_Sort           56
 12751         -#define OP_Rewind         57
 12752         -#define OP_IdxLE          58 /* synopsis: key=r[P3@P4]                     */
 12753         -#define OP_IdxGT          59 /* synopsis: key=r[P3@P4]                     */
 12754         -#define OP_IdxLT          60 /* synopsis: key=r[P3@P4]                     */
 12755         -#define OP_IdxGE          61 /* synopsis: key=r[P3@P4]                     */
 12756         -#define OP_RowSetRead     62 /* synopsis: r[P3]=rowset(P1)                 */
 12757         -#define OP_RowSetTest     63 /* synopsis: if r[P3] in rowset(P1) goto P2   */
 12758         -#define OP_Program        64
 12759         -#define OP_FkIfZero       65 /* synopsis: if fkctr[P1]==0 goto P2          */
 12760         -#define OP_IfPos          66 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
 12761         -#define OP_IfNotZero      67 /* synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
 12762         -#define OP_DecrJumpZero   68 /* synopsis: if (--r[P1])==0 goto P2          */
 12763         -#define OP_IncrVacuum     69
 12764         -#define OP_VNext          70
 12765         -#define OP_Init           71 /* synopsis: Start at P2                      */
 12766         -#define OP_Return         72
 12767         -#define OP_EndCoroutine   73
 12768         -#define OP_HaltIfNull     74 /* synopsis: if r[P3]=null halt               */
 12769         -#define OP_Halt           75
 12770         -#define OP_Integer        76 /* synopsis: r[P2]=P1                         */
 12771         -#define OP_Int64          77 /* synopsis: r[P2]=P4                         */
 12772         -#define OP_String         78 /* synopsis: r[P2]='P4' (len=P1)              */
 12773         -#define OP_Null           79 /* synopsis: r[P2..P3]=NULL                   */
 12774         -#define OP_SoftNull       80 /* synopsis: r[P1]=NULL                       */
 12775         -#define OP_Blob           81 /* synopsis: r[P2]=P4 (len=P1)                */
 12776         -#define OP_Variable       82 /* synopsis: r[P2]=parameter(P1,P4)           */
 12777         -#define OP_Move           83 /* synopsis: r[P2@P3]=r[P1@P3]                */
 12778         -#define OP_Copy           84 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
 12779         -#define OP_SCopy          85 /* synopsis: r[P2]=r[P1]                      */
 12780         -#define OP_IntCopy        86 /* synopsis: r[P2]=r[P1]                      */
 12781         -#define OP_ResultRow      87 /* synopsis: output=r[P1@P2]                  */
 12782         -#define OP_CollSeq        88
 12783         -#define OP_Function0      89 /* synopsis: r[P3]=func(r[P2@P5])             */
 12784         -#define OP_Function       90 /* synopsis: r[P3]=func(r[P2@P5])             */
 12785         -#define OP_AddImm         91 /* synopsis: r[P1]=r[P1]+P2                   */
 12786         -#define OP_RealAffinity   92
 12787         -#define OP_Cast           93 /* synopsis: affinity(r[P1])                  */
 12788         -#define OP_Permutation    94
 12789         -#define OP_Compare        95 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 12790         -#define OP_Column         96 /* synopsis: r[P3]=PX                         */
        12785  +#define OP_IfSmaller      55
        12786  +#define OP_SorterSort     56
        12787  +#define OP_Sort           57
        12788  +#define OP_Rewind         58
        12789  +#define OP_IdxLE          59 /* synopsis: key=r[P3@P4]                     */
        12790  +#define OP_IdxGT          60 /* synopsis: key=r[P3@P4]                     */
        12791  +#define OP_IdxLT          61 /* synopsis: key=r[P3@P4]                     */
        12792  +#define OP_IdxGE          62 /* synopsis: key=r[P3@P4]                     */
        12793  +#define OP_RowSetRead     63 /* synopsis: r[P3]=rowset(P1)                 */
        12794  +#define OP_RowSetTest     64 /* synopsis: if r[P3] in rowset(P1) goto P2   */
        12795  +#define OP_Program        65
        12796  +#define OP_FkIfZero       66 /* synopsis: if fkctr[P1]==0 goto P2          */
        12797  +#define OP_IfPos          67 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
        12798  +#define OP_IfNotZero      68 /* synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
        12799  +#define OP_DecrJumpZero   69 /* synopsis: if (--r[P1])==0 goto P2          */
        12800  +#define OP_IncrVacuum     70
        12801  +#define OP_VNext          71
        12802  +#define OP_Init           72 /* synopsis: Start at P2                      */
        12803  +#define OP_Return         73
        12804  +#define OP_EndCoroutine   74
        12805  +#define OP_HaltIfNull     75 /* synopsis: if r[P3]=null halt               */
        12806  +#define OP_Halt           76
        12807  +#define OP_Integer        77 /* synopsis: r[P2]=P1                         */
        12808  +#define OP_Int64          78 /* synopsis: r[P2]=P4                         */
        12809  +#define OP_String         79 /* synopsis: r[P2]='P4' (len=P1)              */
        12810  +#define OP_Null           80 /* synopsis: r[P2..P3]=NULL                   */
        12811  +#define OP_SoftNull       81 /* synopsis: r[P1]=NULL                       */
        12812  +#define OP_Blob           82 /* synopsis: r[P2]=P4 (len=P1)                */
        12813  +#define OP_Variable       83 /* synopsis: r[P2]=parameter(P1,P4)           */
        12814  +#define OP_Move           84 /* synopsis: r[P2@P3]=r[P1@P3]                */
        12815  +#define OP_Copy           85 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
        12816  +#define OP_SCopy          86 /* synopsis: r[P2]=r[P1]                      */
        12817  +#define OP_IntCopy        87 /* synopsis: r[P2]=r[P1]                      */
        12818  +#define OP_ResultRow      88 /* synopsis: output=r[P1@P2]                  */
        12819  +#define OP_CollSeq        89
        12820  +#define OP_Function0      90 /* synopsis: r[P3]=func(r[P2@P5])             */
        12821  +#define OP_Function       91 /* synopsis: r[P3]=func(r[P2@P5])             */
        12822  +#define OP_AddImm         92 /* synopsis: r[P1]=r[P1]+P2                   */
        12823  +#define OP_RealAffinity   93
        12824  +#define OP_Cast           94 /* synopsis: affinity(r[P1])                  */
        12825  +#define OP_Permutation    95
        12826  +#define OP_Compare        96 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 12791  12827   #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 12792         -#define OP_Affinity       98 /* synopsis: affinity(r[P1@P2])               */
 12793         -#define OP_MakeRecord     99 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 12794         -#define OP_Count         100 /* synopsis: r[P2]=count()                    */
 12795         -#define OP_ReadCookie    101
 12796         -#define OP_SetCookie     102
 12797         -#define OP_ReopenIdx     103 /* synopsis: root=P2 iDb=P3                   */
 12798         -#define OP_OpenRead      104 /* synopsis: root=P2 iDb=P3                   */
 12799         -#define OP_OpenWrite     105 /* synopsis: root=P2 iDb=P3                   */
 12800         -#define OP_OpenAutoindex 106 /* synopsis: nColumn=P2                       */
 12801         -#define OP_OpenEphemeral 107 /* synopsis: nColumn=P2                       */
 12802         -#define OP_SorterOpen    108
 12803         -#define OP_SequenceTest  109 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 12804         -#define OP_OpenPseudo    110 /* synopsis: P3 columns in r[P2]              */
 12805         -#define OP_Close         111
 12806         -#define OP_ColumnsUsed   112
 12807         -#define OP_Sequence      113 /* synopsis: r[P2]=cursor[P1].ctr++           */
 12808         -#define OP_NewRowid      114 /* synopsis: r[P2]=rowid                      */
 12809         -#define OP_Insert        115 /* synopsis: intkey=r[P3] data=r[P2]          */
 12810         -#define OP_InsertInt     116 /* synopsis: intkey=P3 data=r[P2]             */
 12811         -#define OP_Delete        117
 12812         -#define OP_ResetCount    118
 12813         -#define OP_SorterCompare 119 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 12814         -#define OP_SorterData    120 /* synopsis: r[P2]=data                       */
 12815         -#define OP_RowData       121 /* synopsis: r[P2]=data                       */
 12816         -#define OP_Rowid         122 /* synopsis: r[P2]=rowid                      */
 12817         -#define OP_NullRow       123
 12818         -#define OP_SorterInsert  124 /* synopsis: key=r[P2]                        */
 12819         -#define OP_IdxInsert     125 /* synopsis: key=r[P2]                        */
 12820         -#define OP_IdxDelete     126 /* synopsis: key=r[P2@P3]                     */
 12821         -#define OP_Seek          127 /* synopsis: Move P3 to P1.rowid              */
 12822         -#define OP_IdxRowid      128 /* synopsis: r[P2]=rowid                      */
 12823         -#define OP_Destroy       129
 12824         -#define OP_Clear         130
 12825         -#define OP_ResetSorter   131
        12828  +#define OP_Column         98 /* synopsis: r[P3]=PX                         */
        12829  +#define OP_Affinity       99 /* synopsis: affinity(r[P1@P2])               */
        12830  +#define OP_MakeRecord    100 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        12831  +#define OP_Count         101 /* synopsis: r[P2]=count()                    */
        12832  +#define OP_ReadCookie    102
        12833  +#define OP_SetCookie     103
        12834  +#define OP_ReopenIdx     104 /* synopsis: root=P2 iDb=P3                   */
        12835  +#define OP_OpenRead      105 /* synopsis: root=P2 iDb=P3                   */
        12836  +#define OP_OpenWrite     106 /* synopsis: root=P2 iDb=P3                   */
        12837  +#define OP_OpenAutoindex 107 /* synopsis: nColumn=P2                       */
        12838  +#define OP_OpenEphemeral 108 /* synopsis: nColumn=P2                       */
        12839  +#define OP_SorterOpen    109
        12840  +#define OP_SequenceTest  110 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
        12841  +#define OP_OpenPseudo    111 /* synopsis: P3 columns in r[P2]              */
        12842  +#define OP_Close         112
        12843  +#define OP_ColumnsUsed   113
        12844  +#define OP_Sequence      114 /* synopsis: r[P2]=cursor[P1].ctr++           */
        12845  +#define OP_NewRowid      115 /* synopsis: r[P2]=rowid                      */
        12846  +#define OP_Insert        116 /* synopsis: intkey=r[P3] data=r[P2]          */
        12847  +#define OP_InsertInt     117 /* synopsis: intkey=P3 data=r[P2]             */
        12848  +#define OP_Delete        118
        12849  +#define OP_ResetCount    119
        12850  +#define OP_SorterCompare 120 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
        12851  +#define OP_SorterData    121 /* synopsis: r[P2]=data                       */
        12852  +#define OP_RowData       122 /* synopsis: r[P2]=data                       */
        12853  +#define OP_Rowid         123 /* synopsis: r[P2]=rowid                      */
        12854  +#define OP_NullRow       124
        12855  +#define OP_SorterInsert  125 /* synopsis: key=r[P2]                        */
        12856  +#define OP_IdxInsert     126 /* synopsis: key=r[P2]                        */
        12857  +#define OP_IdxDelete     127 /* synopsis: key=r[P2@P3]                     */
        12858  +#define OP_Seek          128 /* synopsis: Move P3 to P1.rowid              */
        12859  +#define OP_IdxRowid      129 /* synopsis: r[P2]=rowid                      */
        12860  +#define OP_Destroy       130
        12861  +#define OP_Clear         131
 12826  12862   #define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 12827         -#define OP_CreateIndex   133 /* synopsis: r[P2]=root iDb=P1                */
 12828         -#define OP_CreateTable   134 /* synopsis: r[P2]=root iDb=P1                */
 12829         -#define OP_ParseSchema   135
 12830         -#define OP_LoadAnalysis  136
 12831         -#define OP_DropTable     137
 12832         -#define OP_DropIndex     138
 12833         -#define OP_DropTrigger   139
 12834         -#define OP_IntegrityCk   140
 12835         -#define OP_RowSetAdd     141 /* synopsis: rowset(P1)=r[P2]                 */
 12836         -#define OP_Param         142
 12837         -#define OP_FkCounter     143 /* synopsis: fkctr[P1]+=P2                    */
 12838         -#define OP_MemMax        144 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 12839         -#define OP_OffsetLimit   145 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 12840         -#define OP_AggStep0      146 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 12841         -#define OP_AggStep       147 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 12842         -#define OP_AggFinal      148 /* synopsis: accum=r[P1] N=P2                 */
 12843         -#define OP_Expire        149
 12844         -#define OP_TableLock     150 /* synopsis: iDb=P1 root=P2 write=P3          */
 12845         -#define OP_VBegin        151
 12846         -#define OP_VCreate       152
 12847         -#define OP_VDestroy      153
 12848         -#define OP_VOpen         154
 12849         -#define OP_VColumn       155 /* synopsis: r[P3]=vcolumn(P2)                */
 12850         -#define OP_VRename       156
 12851         -#define OP_Pagecount     157
 12852         -#define OP_MaxPgcnt      158
 12853         -#define OP_CursorHint    159
 12854         -#define OP_Noop          160
 12855         -#define OP_Explain       161
        12863  +#define OP_ResetSorter   133
        12864  +#define OP_CreateIndex   134 /* synopsis: r[P2]=root iDb=P1                */
        12865  +#define OP_CreateTable   135 /* synopsis: r[P2]=root iDb=P1                */
        12866  +#define OP_SqlExec       136
        12867  +#define OP_ParseSchema   137
        12868  +#define OP_LoadAnalysis  138
        12869  +#define OP_DropTable     139
        12870  +#define OP_DropIndex     140
        12871  +#define OP_DropTrigger   141
        12872  +#define OP_IntegrityCk   142
        12873  +#define OP_RowSetAdd     143 /* synopsis: rowset(P1)=r[P2]                 */
        12874  +#define OP_Param         144
        12875  +#define OP_FkCounter     145 /* synopsis: fkctr[P1]+=P2                    */
        12876  +#define OP_MemMax        146 /* synopsis: r[P1]=max(r[P1],r[P2])           */
        12877  +#define OP_OffsetLimit   147 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
        12878  +#define OP_AggStep0      148 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        12879  +#define OP_AggStep       149 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        12880  +#define OP_AggFinal      150 /* synopsis: accum=r[P1] N=P2                 */
        12881  +#define OP_Expire        151
        12882  +#define OP_TableLock     152 /* synopsis: iDb=P1 root=P2 write=P3          */
        12883  +#define OP_VBegin        153
        12884  +#define OP_VCreate       154
        12885  +#define OP_VDestroy      155
        12886  +#define OP_VOpen         156
        12887  +#define OP_VColumn       157 /* synopsis: r[P3]=vcolumn(P2)                */
        12888  +#define OP_VRename       158
        12889  +#define OP_Pagecount     159
        12890  +#define OP_MaxPgcnt      160
        12891  +#define OP_CursorHint    161
        12892  +#define OP_Noop          162
        12893  +#define OP_Explain       163
 12856  12894   
 12857  12895   /* Properties such as "out2" or "jump" that are specified in
 12858  12896   ** comments following the "case" for each opcode in the vdbe.c
 12859  12897   ** are encoded into bitvectors as follows:
 12860  12898   */
 12861  12899   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 12862  12900   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 12868  12906   /*   0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,\
 12869  12907   /*   8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01,\
 12870  12908   /*  16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x03, 0x03, 0x09,\
 12871  12909   /*  24 */ 0x09, 0x09, 0x09, 0x26, 0x26, 0x09, 0x09, 0x09,\
 12872  12910   /*  32 */ 0x09, 0x09, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 12873  12911   /*  40 */ 0x0b, 0x0b, 0x01, 0x26, 0x26, 0x26, 0x26, 0x26,\
 12874  12912   /*  48 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x01, 0x12, 0x01,\
 12875         -/*  56 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x23, 0x0b,\
 12876         -/*  64 */ 0x01, 0x01, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01,\
 12877         -/*  72 */ 0x02, 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10,\
 12878         -/*  80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00,\
 12879         -/*  88 */ 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
 12880         -/*  96 */ 0x00, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
        12913  +/*  56 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x23,\
        12914  +/*  64 */ 0x0b, 0x01, 0x01, 0x03, 0x03, 0x03, 0x01, 0x01,\
        12915  +/*  72 */ 0x01, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10, 0x10,\
        12916  +/*  80 */ 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10,\
        12917  +/*  88 */ 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00,\
        12918  +/*  96 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
 12881  12919   /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 12882         -/* 112 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
 12883         -/* 120 */ 0x00, 0x00, 0x10, 0x00, 0x04, 0x04, 0x00, 0x00,\
 12884         -/* 128 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
 12885         -/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00,\
 12886         -/* 144 */ 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 12887         -/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
 12888         -/* 160 */ 0x00, 0x00,}
        12920  +/* 112 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
        12921  +/* 120 */ 0x00, 0x00, 0x00, 0x10, 0x00, 0x04, 0x04, 0x00,\
        12922  +/* 128 */ 0x00, 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10,\
        12923  +/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,\
        12924  +/* 144 */ 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00,\
        12925  +/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
        12926  +/* 160 */ 0x10, 0x00, 0x00, 0x00,}
 12889  12927   
 12890  12928   /* The sqlite3P2Values() routine is able to run faster if it knows
 12891  12929   ** the value of the largest JUMP opcode.  The smaller the maximum
 12892  12930   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 12893  12931   ** generated this include file strives to group all JUMP opcodes
 12894  12932   ** together near the beginning of the list.
 12895  12933   */
 12896         -#define SQLITE_MX_JUMP_OPCODE  71  /* Maximum JUMP opcode */
        12934  +#define SQLITE_MX_JUMP_OPCODE  72  /* Maximum JUMP opcode */
 12897  12935   
 12898  12936   /************** End of opcodes.h *********************************************/
 12899  12937   /************** Continuing where we left off in vdbe.h ***********************/
 12900  12938   
 12901  12939   /*
 12902  12940   ** Prototypes for the VDBE interface.  See comments on the implementation
 12903  12941   ** for a description of what each of these routines does.
................................................................................
 13853  13891   **   EXTRA         4                         3
 13854  13892   **
 13855  13893   ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
 13856  13894   ** In other words, the zero-based numbers are used for all external interfaces
 13857  13895   ** and the one-based values are used internally.
 13858  13896   */
 13859  13897   #ifndef SQLITE_DEFAULT_SYNCHRONOUS
 13860         -# define SQLITE_DEFAULT_SYNCHRONOUS (PAGER_SYNCHRONOUS_FULL-1)
        13898  +# define SQLITE_DEFAULT_SYNCHRONOUS 2
 13861  13899   #endif
 13862  13900   #ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
 13863  13901   # define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
 13864  13902   #endif
 13865  13903   
 13866  13904   /*
 13867  13905   ** Each database file to be accessed by the system is an instance
................................................................................
 14059  14097     u8 dfltLockMode;              /* Default locking-mode for attached dbs */
 14060  14098     signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
 14061  14099     u8 suppressErr;               /* Do not issue error messages if true */
 14062  14100     u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
 14063  14101     u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
 14064  14102     u8 mTrace;                    /* zero or more SQLITE_TRACE flags */
 14065  14103     u8 skipBtreeMutex;            /* True if no shared-cache backends */
        14104  +  u8 nSqlExec;                  /* Number of pending OP_SqlExec opcodes */
 14066  14105     int nextPagesize;             /* Pagesize after VACUUM if >0 */
 14067  14106     u32 magic;                    /* Magic number for detect library misuse */
 14068  14107     int nChange;                  /* Value returned by sqlite3_changes() */
 14069  14108     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
 14070  14109     int aLimit[SQLITE_N_LIMIT];   /* Limits */
 14071  14110     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
 14072  14111     struct sqlite3InitInfo {      /* Information used during initialization */
................................................................................
 14574  14613     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
 14575  14614     FKey *pFKey;         /* Linked list of all foreign keys in this table */
 14576  14615     char *zColAff;       /* String defining the affinity of each column */
 14577  14616     ExprList *pCheck;    /* All CHECK constraints */
 14578  14617                          /*   ... also used as column name list in a VIEW */
 14579  14618     int tnum;            /* Root BTree page for this table */
 14580  14619     u32 nTabRef;         /* Number of pointers to this Table */
        14620  +  u32 tabFlags;        /* Mask of TF_* values */
 14581  14621     i16 iPKey;           /* If not negative, use aCol[iPKey] as the rowid */
 14582  14622     i16 nCol;            /* Number of columns in this table */
 14583  14623     LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
 14584  14624     LogEst szTabRow;     /* Estimated size of each table row in bytes */
 14585  14625   #ifdef SQLITE_ENABLE_COSTMULT
 14586  14626     LogEst costMult;     /* Cost multiplier for using this table */
 14587  14627   #endif
 14588         -  u8 tabFlags;         /* Mask of TF_* values */
 14589  14628     u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
 14590  14629   #ifndef SQLITE_OMIT_ALTERTABLE
 14591  14630     int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
 14592  14631   #endif
 14593  14632   #ifndef SQLITE_OMIT_VIRTUALTABLE
 14594  14633     int nModuleArg;      /* Number of arguments to the module */
 14595  14634     char **azModuleArg;  /* 0: module 1: schema 2: vtab name 3...: args */
................................................................................
 14605  14644   **
 14606  14645   ** TF_OOOHidden applies to tables or view that have hidden columns that are
 14607  14646   ** followed by non-hidden columns.  Example:  "CREATE VIRTUAL TABLE x USING
 14608  14647   ** vtab1(a HIDDEN, b);".  Since "b" is a non-hidden column but "a" is hidden,
 14609  14648   ** the TF_OOOHidden attribute would apply in this case.  Such tables require
 14610  14649   ** special handling during INSERT processing.
 14611  14650   */
 14612         -#define TF_Readonly        0x01    /* Read-only system table */
 14613         -#define TF_Ephemeral       0x02    /* An ephemeral table */
 14614         -#define TF_HasPrimaryKey   0x04    /* Table has a primary key */
 14615         -#define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
 14616         -#define TF_Virtual         0x10    /* Is a virtual table */
 14617         -#define TF_WithoutRowid    0x20    /* No rowid.  PRIMARY KEY is the key */
 14618         -#define TF_NoVisibleRowid  0x40    /* No user-visible "rowid" column */
 14619         -#define TF_OOOHidden       0x80    /* Out-of-Order hidden columns */
 14620         -
        14651  +#define TF_Readonly        0x0001    /* Read-only system table */
        14652  +#define TF_Ephemeral       0x0002    /* An ephemeral table */
        14653  +#define TF_HasPrimaryKey   0x0004    /* Table has a primary key */
        14654  +#define TF_Autoincrement   0x0008    /* Integer primary key is autoincrement */
        14655  +#define TF_HasStat1        0x0010    /* nRowLogEst set from sqlite_stat1 */
        14656  +#define TF_WithoutRowid    0x0020    /* No rowid.  PRIMARY KEY is the key */
        14657  +#define TF_NoVisibleRowid  0x0040    /* No user-visible "rowid" column */
        14658  +#define TF_OOOHidden       0x0080    /* Out-of-Order hidden columns */
        14659  +#define TF_StatsUsed       0x0100    /* Query planner decisions affected by
        14660  +                                     ** Index.aiRowLogEst[] values */
        14661  +#define TF_HasNotNull      0x0200    /* Contains NOT NULL constraints */
 14621  14662   
 14622  14663   /*
 14623  14664   ** Test to see whether or not a table is a virtual table.  This is
 14624  14665   ** done as a macro so that it will be optimized out when virtual
 14625  14666   ** table support is omitted from the build.
 14626  14667   */
 14627  14668   #ifndef SQLITE_OMIT_VIRTUALTABLE
 14628         -#  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
        14669  +#  define IsVirtual(X)      ((X)->nModuleArg)
 14629  14670   #else
 14630  14671   #  define IsVirtual(X)      0
 14631  14672   #endif
 14632  14673   
 14633  14674   /*
 14634  14675   ** Macros to determine if a column is hidden.  IsOrdinaryHiddenColumn()
 14635  14676   ** only works for non-virtual tables (ordinary tables and views) and is
................................................................................
 14856  14897     u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 14857  14898     unsigned idxType:2;      /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
 14858  14899     unsigned bUnordered:1;   /* Use this index for == or IN queries only */
 14859  14900     unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
 14860  14901     unsigned isResized:1;    /* True if resizeIndexObject() has been called */
 14861  14902     unsigned isCovering:1;   /* True if this is a covering index */
 14862  14903     unsigned noSkipScan:1;   /* Do not try to use skip-scan if true */
        14904  +  unsigned hasStat1:1;     /* aiRowLogEst values come from sqlite_stat1 */
 14863  14905   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 14864  14906     int nSample;             /* Number of elements in aSample[] */
 14865  14907     int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
 14866  14908     tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
 14867  14909     IndexSample *aSample;    /* Samples of the left-most key */
 14868  14910     tRowcnt *aiRowEst;       /* Non-logarithmic stat1 data for this index */
 14869  14911     tRowcnt nRowEst0;        /* Non-logarithmic number of rows in the index */
................................................................................
 15166  15208   ** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
 15167  15209   ** of the result column in the form: DATABASE.TABLE.COLUMN.  This later
 15168  15210   ** form is used for name resolution with nested FROM clauses.
 15169  15211   */
 15170  15212   struct ExprList {
 15171  15213     int nExpr;             /* Number of expressions on the list */
 15172  15214     struct ExprList_item { /* For each expression in the list */
 15173         -    Expr *pExpr;            /* The list of expressions */
        15215  +    Expr *pExpr;            /* The parse tree for this expression */
 15174  15216       char *zName;            /* Token associated with this expression */
 15175  15217       char *zSpan;            /* Original text of the expression */
 15176  15218       u8 sortOrder;           /* 1 for DESC or 0 for ASC */
 15177  15219       unsigned done :1;       /* A flag to indicate when processing is finished */
 15178  15220       unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
 15179  15221       unsigned reusable :1;   /* Constant expression is reusable */
 15180  15222       union {
................................................................................
 16489  16531   SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
 16490  16532   SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
 16491  16533   SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
 16492  16534   SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*);
 16493  16535   SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int);
 16494  16536   SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
 16495  16537   SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int);
        16538  +SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int);
 16496  16539   SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
 16497  16540   SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
 16498  16541   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 16499  16542   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 16500  16543   SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
 16501  16544   SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
 16502  16545   SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
................................................................................
 17435  17478     "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
 17436  17479   #elif defined(__GNUC__) && defined(__VERSION__)
 17437  17480     "COMPILER=gcc-" __VERSION__,
 17438  17481   #endif
 17439  17482   #if SQLITE_COVERAGE_TEST
 17440  17483     "COVERAGE_TEST",
 17441  17484   #endif
 17442         -#if SQLITE_DEBUG
        17485  +#ifdef SQLITE_DEBUG
 17443  17486     "DEBUG",
 17444  17487   #endif
 17445  17488   #if SQLITE_DEFAULT_LOCKING_MODE
 17446  17489     "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
 17447  17490   #endif
 17448  17491   #if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
 17449  17492     "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
 17450  17493   #endif
        17494  +#if SQLITE_DEFAULT_SYNCHRONOUS
        17495  +  "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS),
        17496  +#endif
        17497  +#if SQLITE_DEFAULT_WAL_SYNCHRONOUS
        17498  +  "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS),
        17499  +#endif
 17451  17500   #if SQLITE_DIRECT_OVERFLOW_READ
 17452  17501     "DIRECT_OVERFLOW_READ",
 17453  17502   #endif
 17454  17503   #if SQLITE_DISABLE_DIRSYNC
 17455  17504     "DISABLE_DIRSYNC",
 17456  17505   #endif
 17457  17506   #if SQLITE_DISABLE_LFS
................................................................................
 18020  18069   struct VdbeFrame {
 18021  18070     Vdbe *v;                /* VM this frame belongs to */
 18022  18071     VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
 18023  18072     Op *aOp;                /* Program instructions for parent frame */
 18024  18073     i64 *anExec;            /* Event counters from parent frame */
 18025  18074     Mem *aMem;              /* Array of memory cells for parent frame */
 18026  18075     VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
        18076  +  u8 *aOnce;              /* Bitmask used by OP_Once */
 18027  18077     void *token;            /* Copy of SubProgram.token */
 18028  18078     i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
 18029  18079     AuxData *pAuxData;      /* Linked list of auxdata allocations */
 18030  18080     int nCursor;            /* Number of entries in apCsr */
 18031  18081     int pc;                 /* Program Counter in parent (calling) frame */
 18032  18082     int nOp;                /* Size of aOp array */
 18033  18083     int nMem;               /* Number of entries in aMem */
................................................................................
 19490  19540         /*
 19491  19541         **    start of TTTTT
 19492  19542         **
 19493  19543         ** Move the date backwards to the beginning of the current day,
 19494  19544         ** or month or year.
 19495  19545         */
 19496  19546         if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
        19547  +      if( !p->validJD && !p->validYMD && !p->validHMS ) break;
 19497  19548         z += 9;
 19498  19549         computeYMD(p);
 19499  19550         p->validHMS = 1;
 19500  19551         p->h = p->m = 0;
 19501  19552         p->s = 0.0;
        19553  +      p->rawS = 0;
 19502  19554         p->validTZ = 0;
 19503  19555         p->validJD = 0;
 19504  19556         if( sqlite3_stricmp(z,"month")==0 ){
 19505  19557           p->D = 1;
 19506  19558           rc = 0;
 19507  19559         }else if( sqlite3_stricmp(z,"year")==0 ){
 19508         -        computeYMD(p);
 19509  19560           p->M = 1;
 19510  19561           p->D = 1;
 19511  19562           rc = 0;
 19512  19563         }else if( sqlite3_stricmp(z,"day")==0 ){
 19513  19564           rc = 0;
 19514  19565         }
 19515  19566         break;
................................................................................
 20623  20674   
 20624  20675   /*
 20625  20676   ** Use the zone allocator available on apple products unless the
 20626  20677   ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
 20627  20678   */
 20628  20679   #include <sys/sysctl.h>
 20629  20680   #include <malloc/malloc.h>
        20681  +#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
 20630  20682   #include <libkern/OSAtomic.h>
        20683  +#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
 20631  20684   static malloc_zone_t* _sqliteZone_;
 20632  20685   #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
 20633  20686   #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
 20634  20687   #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
 20635  20688   #define SQLITE_MALLOCSIZE(x) \
 20636  20689           (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
 20637  20690   
................................................................................
 20816  20869     sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
 20817  20870     if( cpuCount>1 ){
 20818  20871       /* defer MT decisions to system malloc */
 20819  20872       _sqliteZone_ = malloc_default_zone();
 20820  20873     }else{
 20821  20874       /* only 1 core, use our own zone to contention over global locks, 
 20822  20875       ** e.g. we have our own dedicated locks */
 20823         -    bool success;
 20824         -    malloc_zone_t* newzone = malloc_create_zone(4096, 0);
 20825         -    malloc_set_zone_name(newzone, "Sqlite_Heap");
 20826         -    do{
 20827         -      success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
 20828         -                                 (void * volatile *)&_sqliteZone_);
 20829         -    }while(!_sqliteZone_);
 20830         -    if( !success ){
 20831         -      /* somebody registered a zone first */
 20832         -      malloc_destroy_zone(newzone);
 20833         -    }
        20876  +    _sqliteZone_ = malloc_create_zone(4096, 0);
        20877  +    malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
 20834  20878     }
 20835         -#endif
        20879  +#endif /*  defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
 20836  20880     UNUSED_PARAMETER(NotUsed);
 20837  20881     return SQLITE_OK;
 20838  20882   }
 20839  20883   
 20840  20884   /*
 20841  20885   ** Deinitialize this module.
 20842  20886   */
................................................................................
 23824  23868   ** compiled without mutexes (SQLITE_THREADSAFE=0).
 23825  23869   */
 23826  23870   SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
 23827  23871   #if defined(SQLITE_MEMORY_BARRIER)
 23828  23872     SQLITE_MEMORY_BARRIER;
 23829  23873   #elif defined(__GNUC__)
 23830  23874     __sync_synchronize();
 23831         -#elif !defined(SQLITE_DISABLE_INTRINSIC) && \
 23832         -      defined(_MSC_VER) && _MSC_VER>=1300
        23875  +#elif MSVC_VERSION>=1300
 23833  23876     _ReadWriteBarrier();
 23834  23877   #elif defined(MemoryBarrier)
 23835  23878     MemoryBarrier();
 23836  23879   #endif
 23837  23880   }
 23838  23881   
 23839  23882   /*
................................................................................
 24036  24079     assert( winMutex_isInit==1 );
 24037  24080     EnterCriticalSection(&p->mutex);
 24038  24081   #ifdef SQLITE_DEBUG
 24039  24082     assert( p->nRef>0 || p->owner==0 );
 24040  24083     p->owner = tid;
 24041  24084     p->nRef++;
 24042  24085     if( p->trace ){
 24043         -    OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
 24044         -             tid, p, p->trace, p->nRef));
        24086  +    OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
        24087  +             tid, p->id, p, p->trace, p->nRef));
 24045  24088     }
 24046  24089   #endif
 24047  24090   }
 24048  24091   
 24049  24092   static int winMutexTry(sqlite3_mutex *p){
 24050  24093   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
 24051  24094     DWORD tid = GetCurrentThreadId();
................................................................................
 24079  24122       rc = SQLITE_OK;
 24080  24123     }
 24081  24124   #else
 24082  24125     UNUSED_PARAMETER(p);
 24083  24126   #endif
 24084  24127   #ifdef SQLITE_DEBUG
 24085  24128     if( p->trace ){
 24086         -    OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
 24087         -             tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
        24129  +    OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
        24130  +             tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
 24088  24131     }
 24089  24132   #endif
 24090  24133     return rc;
 24091  24134   }
 24092  24135   
 24093  24136   /*
 24094  24137   ** The sqlite3_mutex_leave() routine exits a mutex that was
................................................................................
 24108  24151     if( p->nRef==0 ) p->owner = 0;
 24109  24152     assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
 24110  24153   #endif
 24111  24154     assert( winMutex_isInit==1 );
 24112  24155     LeaveCriticalSection(&p->mutex);
 24113  24156   #ifdef SQLITE_DEBUG
 24114  24157     if( p->trace ){
 24115         -    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
 24116         -             tid, p, p->trace, p->nRef));
        24158  +    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
        24159  +             tid, p->id, p, p->trace, p->nRef));
 24117  24160     }
 24118  24161   #endif
 24119  24162   }
 24120  24163   
 24121  24164   SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 24122  24165     static const sqlite3_mutex_methods sMutex = {
 24123  24166       winMutexInit,
................................................................................
 24363  24406   */
 24364  24407   static void mallocWithAlarm(int n, void **pp){
 24365  24408     void *p;
 24366  24409     int nFull;
 24367  24410     assert( sqlite3_mutex_held(mem0.mutex) );
 24368  24411     assert( n>0 );
 24369  24412   
 24370         -  /* In Firefox (circa 2017-02-08), xRoundup is remapped to an internal
        24413  +  /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
 24371  24414     ** implementation of malloc_good_size(), which must be called in debug
 24372  24415     ** mode and specifically when the DMD "Dark Matter Detector" is enabled
 24373         -  ** or else a crash results.  Hence, do not attempt to optimization out
 24374         -  ** the following xRoundup() call. */
        24416  +  ** or else a crash results.  Hence, do not attempt to optimize out the
        24417  +  ** following xRoundup() call. */
 24375  24418     nFull = sqlite3GlobalConfig.m.xRoundup(n);
        24419  +
        24420  +#ifdef SQLITE_MAX_MEMORY
        24421  +  if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){
        24422  +    *pp = 0;
        24423  +    return;
        24424  +  }
        24425  +#endif
 24376  24426   
 24377  24427     sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
 24378  24428     if( mem0.alarmThreshold>0 ){
 24379  24429       sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 24380  24430       if( nUsed >= mem0.alarmThreshold - nFull ){
 24381  24431         mem0.nearlyFull = 1;
 24382  24432         sqlite3MallocAlarm(nFull);
................................................................................
 24558  24608   SQLITE_PRIVATE int sqlite3MallocSize(void *p){
 24559  24609     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
 24560  24610     return sqlite3GlobalConfig.m.xSize(p);
 24561  24611   }
 24562  24612   SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
 24563  24613     assert( p!=0 );
 24564  24614     if( db==0 || !isLookaside(db,p) ){
 24565         -#if SQLITE_DEBUG
        24615  +#ifdef SQLITE_DEBUG
 24566  24616       if( db==0 ){
 24567  24617         assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
 24568  24618         assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
 24569  24619       }else{
 24570  24620         assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
 24571  24621         assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
 24572  24622       }
................................................................................
 24619  24669     if( db ){
 24620  24670       if( db->pnBytesFreed ){
 24621  24671         measureAllocationSize(db, p);
 24622  24672         return;
 24623  24673       }
 24624  24674       if( isLookaside(db, p) ){
 24625  24675         LookasideSlot *pBuf = (LookasideSlot*)p;
 24626         -#if SQLITE_DEBUG
        24676  +#ifdef SQLITE_DEBUG
 24627  24677         /* Trash all content in the buffer being freed */
 24628  24678         memset(p, 0xaa, db->lookaside.sz);
 24629  24679   #endif
 24630  24680         pBuf->pNext = db->lookaside.pFree;
 24631  24681         db->lookaside.pFree = pBuf;
 24632  24682         db->lookaside.nOut--;
 24633  24683         return;
................................................................................
 24988  25038   */
 24989  25039   /* #include "sqliteInt.h" */
 24990  25040   
 24991  25041   /*
 24992  25042   ** Conversion types fall into various categories as defined by the
 24993  25043   ** following enumeration.
 24994  25044   */
 24995         -#define etRADIX       0 /* Integer types.  %d, %x, %o, and so forth */
        25045  +#define etRADIX       0 /* non-decimal integer types.  %x %o */
 24996  25046   #define etFLOAT       1 /* Floating point.  %f */
 24997  25047   #define etEXP         2 /* Exponentional notation. %e and %E */
 24998  25048   #define etGENERIC     3 /* Floating or exponential, depending on exponent. %g */
 24999  25049   #define etSIZE        4 /* Return number of characters processed so far. %n */
 25000  25050   #define etSTRING      5 /* Strings. %s */
 25001  25051   #define etDYNSTRING   6 /* Dynamically allocated strings. %z */
 25002  25052   #define etPERCENT     7 /* Percent symbol. %% */
................................................................................
 25006  25056   #define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
 25007  25057                             NULL pointers replaced by SQL NULL.  %Q */
 25008  25058   #define etTOKEN      11 /* a pointer to a Token structure */
 25009  25059   #define etSRCLIST    12 /* a pointer to a SrcList */
 25010  25060   #define etPOINTER    13 /* The %p conversion */
 25011  25061   #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
 25012  25062   #define etORDINAL    15 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
        25063  +#define etDECIMAL    16 /* %d or %u, but not %x, %o */
 25013  25064   
 25014         -#define etINVALID    16 /* Any unrecognized conversion type */
        25065  +#define etINVALID    17 /* Any unrecognized conversion type */
 25015  25066   
 25016  25067   
 25017  25068   /*
 25018  25069   ** An "etByte" is an 8-bit unsigned value.
 25019  25070   */
 25020  25071   typedef unsigned char etByte;
 25021  25072   
................................................................................
 25031  25082     etByte charset;          /* Offset into aDigits[] of the digits string */
 25032  25083     etByte prefix;           /* Offset into aPrefix[] of the prefix string */
 25033  25084   } et_info;
 25034  25085   
 25035  25086   /*
 25036  25087   ** Allowed values for et_info.flags
 25037  25088   */
 25038         -#define FLAG_SIGNED  1     /* True if the value to convert is signed */
 25039         -#define FLAG_STRING  4     /* Allow infinity precision */
        25089  +#define FLAG_SIGNED    1     /* True if the value to convert is signed */
        25090  +#define FLAG_STRING    4     /* Allow infinite precision */
 25040  25091   
 25041  25092   
 25042  25093   /*
 25043  25094   ** The following table is searched linearly, so it is good to put the
 25044  25095   ** most frequently used conversion types first.
 25045  25096   */
 25046  25097   static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
 25047  25098   static const char aPrefix[] = "-x0\000X0";
 25048  25099   static const et_info fmtinfo[] = {
 25049         -  {  'd', 10, 1, etRADIX,      0,  0 },
        25100  +  {  'd', 10, 1, etDECIMAL,    0,  0 },
 25050  25101     {  's',  0, 4, etSTRING,     0,  0 },
 25051  25102     {  'g',  0, 1, etGENERIC,    30, 0 },
 25052  25103     {  'z',  0, 4, etDYNSTRING,  0,  0 },
 25053  25104     {  'q',  0, 4, etSQLESCAPE,  0,  0 },
 25054  25105     {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
 25055  25106     {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
 25056  25107     {  'c',  0, 0, etCHARX,      0,  0 },
 25057  25108     {  'o',  8, 0, etRADIX,      0,  2 },
 25058         -  {  'u', 10, 0, etRADIX,      0,  0 },
        25109  +  {  'u', 10, 0, etDECIMAL,    0,  0 },
 25059  25110     {  'x', 16, 0, etRADIX,      16, 1 },
 25060  25111     {  'X', 16, 0, etRADIX,      0,  4 },
 25061  25112   #ifndef SQLITE_OMIT_FLOATING_POINT
 25062  25113     {  'f',  0, 1, etFLOAT,      0,  0 },
 25063  25114     {  'e',  0, 1, etEXP,        30, 0 },
 25064  25115     {  'E',  0, 1, etEXP,        14, 0 },
 25065  25116     {  'G',  0, 1, etGENERIC,    14, 0 },
 25066  25117   #endif
 25067         -  {  'i', 10, 1, etRADIX,      0,  0 },
        25118  +  {  'i', 10, 1, etDECIMAL,    0,  0 },
 25068  25119     {  'n',  0, 0, etSIZE,       0,  0 },
 25069  25120     {  '%',  0, 0, etPERCENT,    0,  0 },
 25070  25121     {  'p', 16, 0, etPOINTER,    0,  1 },
 25071  25122   
 25072  25123     /* All the rest are undocumented and are for internal use only */
 25073  25124     {  'T',  0, 0, etTOKEN,      0,  0 },
 25074  25125     {  'S',  0, 0, etSRCLIST,    0,  0 },
................................................................................
 25152  25203     int c;                     /* Next character in the format string */
 25153  25204     char *bufpt;               /* Pointer to the conversion buffer */
 25154  25205     int precision;             /* Precision of the current field */
 25155  25206     int length;                /* Length of the field */
 25156  25207     int idx;                   /* A general purpose loop counter */
 25157  25208     int width;                 /* Width of the current field */
 25158  25209     etByte flag_leftjustify;   /* True if "-" flag is present */
 25159         -  etByte flag_plussign;      /* True if "+" flag is present */
 25160         -  etByte flag_blanksign;     /* True if " " flag is present */
        25210  +  etByte flag_prefix;        /* '+' or ' ' or 0 for prefix */
 25161  25211     etByte flag_alternateform; /* True if "#" flag is present */
 25162  25212     etByte flag_altform2;      /* True if "!" flag is present */
 25163  25213     etByte flag_zeropad;       /* True if field width constant starts with zero */
 25164         -  etByte flag_long;          /* True if "l" flag is present */
 25165         -  etByte flag_longlong;      /* True if the "ll" flag is present */
        25214  +  etByte flag_long;          /* 1 for the "l" flag, 2 for "ll", 0 by default */
 25166  25215     etByte done;               /* Loop termination flag */
        25216  +  etByte cThousand;          /* Thousands separator for %d and %u */
 25167  25217     etByte xtype = etINVALID;  /* Conversion paradigm */
 25168  25218     u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
 25169  25219     char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
 25170  25220     sqlite_uint64 longvalue;   /* Value for integer types */
 25171  25221     LONGDOUBLE_TYPE realvalue; /* Value for real types */
 25172  25222     const et_info *infop;      /* Pointer to the appropriate info structure */
 25173  25223     char *zOut;                /* Rendering buffer */
................................................................................
 25202  25252         if( *fmt==0 ) break;
 25203  25253       }
 25204  25254       if( (c=(*++fmt))==0 ){
 25205  25255         sqlite3StrAccumAppend(pAccum, "%", 1);
 25206  25256         break;
 25207  25257       }
 25208  25258       /* Find out what flags are present */
 25209         -    flag_leftjustify = flag_plussign = flag_blanksign = 
        25259  +    flag_leftjustify = flag_prefix = cThousand =
 25210  25260        flag_alternateform = flag_altform2 = flag_zeropad = 0;
 25211  25261       done = 0;
 25212  25262       do{
 25213  25263         switch( c ){
 25214  25264           case '-':   flag_leftjustify = 1;     break;
 25215         -        case '+':   flag_plussign = 1;        break;
 25216         -        case ' ':   flag_blanksign = 1;       break;
        25265  +        case '+':   flag_prefix = '+';        break;
        25266  +        case ' ':   flag_prefix = ' ';        break;
 25217  25267           case '#':   flag_alternateform = 1;   break;
 25218  25268           case '!':   flag_altform2 = 1;        break;
 25219  25269           case '0':   flag_zeropad = 1;         break;
        25270  +        case ',':   cThousand = ',';          break;
 25220  25271           default:    done = 1;                 break;
 25221  25272         }
 25222  25273       }while( !done && (c=(*++fmt))!=0 );
 25223  25274       /* Get the field width */
 25224  25275       if( c=='*' ){
 25225  25276         if( bArgList ){
 25226  25277           width = (int)getIntArg(pArgList);
................................................................................
 25282  25333   
 25283  25334   
 25284  25335       /* Get the conversion type modifier */
 25285  25336       if( c=='l' ){
 25286  25337         flag_long = 1;
 25287  25338         c = *++fmt;
 25288  25339         if( c=='l' ){
 25289         -        flag_longlong = 1;
        25340  +        flag_long = 2;
 25290  25341           c = *++fmt;
 25291         -      }else{
 25292         -        flag_longlong = 0;
 25293  25342         }
 25294  25343       }else{
 25295         -      flag_long = flag_longlong = 0;
        25344  +      flag_long = 0;
 25296  25345       }
 25297  25346       /* Fetch the info entry for the field */
 25298  25347       infop = &fmtinfo[0];
 25299  25348       xtype = etINVALID;
 25300  25349       for(idx=0; idx<ArraySize(fmtinfo); idx++){
 25301  25350         if( c==fmtinfo[idx].fmttype ){
 25302  25351           infop = &fmtinfo[idx];
................................................................................
 25306  25355       }
 25307  25356   
 25308  25357       /*
 25309  25358       ** At this point, variables are initialized as follows:
 25310  25359       **
 25311  25360       **   flag_alternateform          TRUE if a '#' is present.
 25312  25361       **   flag_altform2               TRUE if a '!' is present.
 25313         -    **   flag_plussign               TRUE if a '+' is present.
        25362  +    **   flag_prefix                 '+' or ' ' or zero
 25314  25363       **   flag_leftjustify            TRUE if a '-' is present or if the
 25315  25364       **                               field width was negative.
 25316  25365       **   flag_zeropad                TRUE if the width began with 0.
 25317         -    **   flag_long                   TRUE if the letter 'l' (ell) prefixed
 25318         -    **                               the conversion character.
 25319         -    **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
 25320         -    **                               the conversion character.
 25321         -    **   flag_blanksign              TRUE if a ' ' is present.
        25366  +    **   flag_long                   1 for "l", 2 for "ll"
 25322  25367       **   width                       The specified field width.  This is
 25323  25368       **                               always non-negative.  Zero is the default.
 25324  25369       **   precision                   The specified precision.  The default
 25325  25370       **                               is -1.
 25326  25371       **   xtype                       The class of the conversion.
 25327  25372       **   infop                       Pointer to the appropriate info struct.
 25328  25373       */
 25329  25374       switch( xtype ){
 25330  25375         case etPOINTER:
 25331         -        flag_longlong = sizeof(char*)==sizeof(i64);
 25332         -        flag_long = sizeof(char*)==sizeof(long int);
        25376  +        flag_long = sizeof(char*)==sizeof(i64) ? 2 :
        25377  +                     sizeof(char*)==sizeof(long int) ? 1 : 0;
 25333  25378           /* Fall through into the next case */
 25334  25379         case etORDINAL:
 25335         -      case etRADIX:
        25380  +      case etRADIX:      
        25381  +        cThousand = 0;
        25382  +        /* Fall through into the next case */
        25383  +      case etDECIMAL:
 25336  25384           if( infop->flags & FLAG_SIGNED ){
 25337  25385             i64 v;
 25338  25386             if( bArgList ){
 25339  25387               v = getIntArg(pArgList);
 25340         -          }else if( flag_longlong ){
 25341         -            v = va_arg(ap,i64);
 25342  25388             }else if( flag_long ){
 25343         -            v = va_arg(ap,long int);
        25389  +            if( flag_long==2 ){
        25390  +              v = va_arg(ap,i64) ;
        25391  +            }else{
        25392  +              v = va_arg(ap,long int);
        25393  +            }
 25344  25394             }else{
 25345  25395               v = va_arg(ap,int);
 25346  25396             }
 25347  25397             if( v<0 ){
 25348  25398               if( v==SMALLEST_INT64 ){
 25349  25399                 longvalue = ((u64)1)<<63;
 25350  25400               }else{
 25351  25401                 longvalue = -v;
 25352  25402               }
 25353  25403               prefix = '-';
 25354  25404             }else{
 25355  25405               longvalue = v;
 25356         -            if( flag_plussign )        prefix = '+';
 25357         -            else if( flag_blanksign )  prefix = ' ';
 25358         -            else                       prefix = 0;
        25406  +            prefix = flag_prefix;
 25359  25407             }
 25360  25408           }else{
 25361  25409             if( bArgList ){
 25362  25410               longvalue = (u64)getIntArg(pArgList);
 25363         -          }else if( flag_longlong ){
 25364         -            longvalue = va_arg(ap,u64);
 25365  25411             }else if( flag_long ){
 25366         -            longvalue = va_arg(ap,unsigned long int);
        25412  +            if( flag_long==2 ){
        25413  +              longvalue = va_arg(ap,u64);
        25414  +            }else{
        25415  +              longvalue = va_arg(ap,unsigned long int);
        25416  +            }
 25367  25417             }else{
 25368  25418               longvalue = va_arg(ap,unsigned int);
 25369  25419             }
 25370  25420             prefix = 0;
 25371  25421           }
 25372  25422           if( longvalue==0 ) flag_alternateform = 0;
 25373  25423           if( flag_zeropad && precision<width-(prefix!=0) ){
 25374  25424             precision = width-(prefix!=0);
 25375  25425           }
 25376         -        if( precision<etBUFSIZE-10 ){
        25426  +        if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
 25377  25427             nOut = etBUFSIZE;
 25378  25428             zOut = buf;
 25379  25429           }else{
 25380         -          nOut = precision + 10;
 25381         -          zOut = zExtra = sqlite3Malloc( nOut );
        25430  +          u64 n = (u64)precision + 10 + precision/3;
        25431  +          zOut = zExtra = sqlite3Malloc( n );
 25382  25432             if( zOut==0 ){
 25383  25433               setStrAccumError(pAccum, STRACCUM_NOMEM);
 25384  25434               return;
 25385  25435             }
        25436  +          nOut = (int)n;
 25386  25437           }
 25387  25438           bufpt = &zOut[nOut-1];
 25388  25439           if( xtype==etORDINAL ){
 25389  25440             static const char zOrd[] = "thstndrd";
 25390  25441             int x = (int)(longvalue % 10);
 25391  25442             if( x>=4 || (longvalue/10)%10==1 ){
 25392  25443               x = 0;
................................................................................
 25399  25450             u8 base = infop->base;
 25400  25451             do{                                           /* Convert to ascii */
 25401  25452               *(--bufpt) = cset[longvalue%base];
 25402  25453               longvalue = longvalue/base;
 25403  25454             }while( longvalue>0 );
 25404  25455           }
 25405  25456           length = (int)(&zOut[nOut-1]-bufpt);
 25406         -        for(idx=precision-length; idx>0; idx--){
        25457  +        while( precision>length ){
 25407  25458             *(--bufpt) = '0';                             /* Zero pad */
        25459  +          length++;
        25460  +        }
        25461  +        if( cThousand ){
        25462  +          int nn = (length - 1)/3;  /* Number of "," to insert */
        25463  +          int ix = (length - 1)%3 + 1;
        25464  +          bufpt -= nn;
        25465  +          for(idx=0; nn>0; idx++){
        25466  +            bufpt[idx] = bufpt[idx+nn];
        25467  +            ix--;
        25468  +            if( ix==0 ){
        25469  +              bufpt[++idx] = cThousand;
        25470  +              nn--;
        25471  +              ix = 3;
        25472  +            }
        25473  +          }
 25408  25474           }
 25409  25475           if( prefix ) *(--bufpt) = prefix;               /* Add sign */
 25410  25476           if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
 25411  25477             const char *pre;
 25412  25478             char x;
 25413  25479             pre = &aPrefix[infop->prefix];
 25414  25480             for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
................................................................................
 25427  25493           length = 0;
 25428  25494   #else
 25429  25495           if( precision<0 ) precision = 6;         /* Set default precision */
 25430  25496           if( realvalue<0.0 ){
 25431  25497             realvalue = -realvalue;
 25432  25498             prefix = '-';
 25433  25499           }else{
 25434         -          if( flag_plussign )          prefix = '+';
 25435         -          else if( flag_blanksign )    prefix = ' ';
 25436         -          else                         prefix = 0;
        25500  +          prefix = flag_prefix;
 25437  25501           }
 25438  25502           if( xtype==etGENERIC && precision>0 ) precision--;
 25439  25503           testcase( precision>0xfff );
 25440  25504           for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
 25441  25505           if( xtype==etFLOAT ) realvalue += rounder;
 25442  25506           /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
 25443  25507           exp = 0;
................................................................................
 26201  26265   
 26202  26266   /*
 26203  26267   ** Generate a human-readable description of a Select object.
 26204  26268   */
 26205  26269   SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
 26206  26270     int n = 0;
 26207  26271     int cnt = 0;
        26272  +  if( p==0 ){
        26273  +    sqlite3TreeViewLine(pView, "nil-SELECT");
        26274  +    return;
        26275  +  } 
 26208  26276     pView = sqlite3TreeViewPush(pView, moreToFollow);
 26209  26277     if( p->pWith ){
 26210  26278       sqlite3TreeViewWith(pView, p->pWith, 1);
 26211  26279       cnt = 1;
 26212  26280       sqlite3TreeViewPush(pView, 1);
 26213  26281     }
 26214  26282     do{
................................................................................
 28682  28750   ** Read or write a four-byte big-endian integer value.
 28683  28751   */
 28684  28752   SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
 28685  28753   #if SQLITE_BYTEORDER==4321
 28686  28754     u32 x;
 28687  28755     memcpy(&x,p,4);
 28688  28756     return x;
 28689         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28690         -    && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
        28757  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
 28691  28758     u32 x;
 28692  28759     memcpy(&x,p,4);
 28693  28760     return __builtin_bswap32(x);
 28694         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28695         -    && defined(_MSC_VER) && _MSC_VER>=1300
        28761  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 28696  28762     u32 x;
 28697  28763     memcpy(&x,p,4);
 28698  28764     return _byteswap_ulong(x);
 28699  28765   #else
 28700  28766     testcase( p[0]&0x80 );
 28701  28767     return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
 28702  28768   #endif
 28703  28769   }
 28704  28770   SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
 28705  28771   #if SQLITE_BYTEORDER==4321
 28706  28772     memcpy(p,&v,4);
 28707         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28708         -    && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
        28773  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
 28709  28774     u32 x = __builtin_bswap32(v);
 28710  28775     memcpy(p,&x,4);
 28711         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 28712         -    && defined(_MSC_VER) && _MSC_VER>=1300
        28776  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 28713  28777     u32 x = _byteswap_ulong(v);
 28714  28778     memcpy(p,&x,4);
 28715  28779   #else
 28716  28780     p[0] = (u8)(v>>24);
 28717  28781     p[1] = (u8)(v>>16);
 28718  28782     p[2] = (u8)(v>>8);
 28719  28783     p[3] = (u8)v;
................................................................................
 28821  28885   /*
 28822  28886   ** Attempt to add, substract, or multiply the 64-bit signed value iB against
 28823  28887   ** the other 64-bit signed integer at *pA and store the result in *pA.
 28824  28888   ** Return 0 on success.  Or if the operation would have resulted in an
 28825  28889   ** overflow, leave *pA unchanged and return 1.
 28826  28890   */
 28827  28891   SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
 28828         -#if !defined(SQLITE_DISABLE_INTRINSIC) \
 28829         -    && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
        28892  +#if GCC_VERSION>=5004000
 28830  28893     return __builtin_add_overflow(*pA, iB, pA);
 28831  28894   #else
 28832  28895     i64 iA = *pA;
 28833  28896     testcase( iA==0 ); testcase( iA==1 );
 28834  28897     testcase( iB==-1 ); testcase( iB==0 );
 28835  28898     if( iB>=0 ){
 28836  28899       testcase( iA>0 && LARGEST_INT64 - iA == iB );
................................................................................
 28842  28905       if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
 28843  28906     }
 28844  28907     *pA += iB;
 28845  28908     return 0; 
 28846  28909   #endif
 28847  28910   }
 28848  28911   SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
 28849         -#if !defined(SQLITE_DISABLE_INTRINSIC) \
 28850         -    && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
        28912  +#if GCC_VERSION>=5004000
 28851  28913     return __builtin_sub_overflow(*pA, iB, pA);
 28852  28914   #else
 28853  28915     testcase( iB==SMALLEST_INT64+1 );
 28854  28916     if( iB==SMALLEST_INT64 ){
 28855  28917       testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
 28856  28918       if( (*pA)>=0 ) return 1;
 28857  28919       *pA -= iB;
................................................................................
 28858  28920       return 0;
 28859  28921     }else{
 28860  28922       return sqlite3AddInt64(pA, -iB);
 28861  28923     }
 28862  28924   #endif
 28863  28925   }
 28864  28926   SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
 28865         -#if !defined(SQLITE_DISABLE_INTRINSIC) \
 28866         -    && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
        28927  +#if GCC_VERSION>=5004000
 28867  28928     return __builtin_mul_overflow(*pA, iB, pA);
 28868  28929   #else
 28869  28930     i64 iA = *pA;
 28870  28931     if( iB>0 ){
 28871  28932       if( iA>LARGEST_INT64/iB ) return 1;
 28872  28933       if( iA<SMALLEST_INT64/iB ) return 1;
 28873  28934     }else if( iB<0 ){
................................................................................
 29457  29518       /*  48 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 29458  29519       /*  49 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 29459  29520       /*  50 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 29460  29521       /*  51 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 29461  29522       /*  52 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 29462  29523       /*  53 */ "Last"             OpHelp(""),
 29463  29524       /*  54 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 29464         -    /*  55 */ "SorterSort"       OpHelp(""),
 29465         -    /*  56 */ "Sort"             OpHelp(""),
 29466         -    /*  57 */ "Rewind"           OpHelp(""),
 29467         -    /*  58 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
 29468         -    /*  59 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
 29469         -    /*  60 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
 29470         -    /*  61 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
 29471         -    /*  62 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
 29472         -    /*  63 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
 29473         -    /*  64 */ "Program"          OpHelp(""),
 29474         -    /*  65 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
 29475         -    /*  66 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
 29476         -    /*  67 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
 29477         -    /*  68 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
 29478         -    /*  69 */ "IncrVacuum"       OpHelp(""),
 29479         -    /*  70 */ "VNext"            OpHelp(""),
 29480         -    /*  71 */ "Init"             OpHelp("Start at P2"),
 29481         -    /*  72 */ "Return"           OpHelp(""),
 29482         -    /*  73 */ "EndCoroutine"     OpHelp(""),
 29483         -    /*  74 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
 29484         -    /*  75 */ "Halt"             OpHelp(""),
 29485         -    /*  76 */ "Integer"          OpHelp("r[P2]=P1"),
 29486         -    /*  77 */ "Int64"            OpHelp("r[P2]=P4"),
 29487         -    /*  78 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
 29488         -    /*  79 */ "Null"             OpHelp("r[P2..P3]=NULL"),
 29489         -    /*  80 */ "SoftNull"         OpHelp("r[P1]=NULL"),
 29490         -    /*  81 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
 29491         -    /*  82 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
 29492         -    /*  83 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
 29493         -    /*  84 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
 29494         -    /*  85 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
 29495         -    /*  86 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
 29496         -    /*  87 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
 29497         -    /*  88 */ "CollSeq"          OpHelp(""),
 29498         -    /*  89 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
 29499         -    /*  90 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 29500         -    /*  91 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
 29501         -    /*  92 */ "RealAffinity"     OpHelp(""),
 29502         -    /*  93 */ "Cast"             OpHelp("affinity(r[P1])"),
 29503         -    /*  94 */ "Permutation"      OpHelp(""),
 29504         -    /*  95 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 29505         -    /*  96 */ "Column"           OpHelp("r[P3]=PX"),
        29525  +    /*  55 */ "IfSmaller"        OpHelp(""),
        29526  +    /*  56 */ "SorterSort"       OpHelp(""),
        29527  +    /*  57 */ "Sort"             OpHelp(""),
        29528  +    /*  58 */ "Rewind"           OpHelp(""),
        29529  +    /*  59 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
        29530  +    /*  60 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
        29531  +    /*  61 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
        29532  +    /*  62 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
        29533  +    /*  63 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
        29534  +    /*  64 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
        29535  +    /*  65 */ "Program"          OpHelp(""),
        29536  +    /*  66 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
        29537  +    /*  67 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
        29538  +    /*  68 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
        29539  +    /*  69 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
        29540  +    /*  70 */ "IncrVacuum"       OpHelp(""),
        29541  +    /*  71 */ "VNext"            OpHelp(""),
        29542  +    /*  72 */ "Init"             OpHelp("Start at P2"),
        29543  +    /*  73 */ "Return"           OpHelp(""),
        29544  +    /*  74 */ "EndCoroutine"     OpHelp(""),
        29545  +    /*  75 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
        29546  +    /*  76 */ "Halt"             OpHelp(""),
        29547  +    /*  77 */ "Integer"          OpHelp("r[P2]=P1"),
        29548  +    /*  78 */ "Int64"            OpHelp("r[P2]=P4"),
        29549  +    /*  79 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
        29550  +    /*  80 */ "Null"             OpHelp("r[P2..P3]=NULL"),
        29551  +    /*  81 */ "SoftNull"         OpHelp("r[P1]=NULL"),
        29552  +    /*  82 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
        29553  +    /*  83 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
        29554  +    /*  84 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
        29555  +    /*  85 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
        29556  +    /*  86 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
        29557  +    /*  87 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
        29558  +    /*  88 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
        29559  +    /*  89 */ "CollSeq"          OpHelp(""),
        29560  +    /*  90 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
        29561  +    /*  91 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
        29562  +    /*  92 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
        29563  +    /*  93 */ "RealAffinity"     OpHelp(""),
        29564  +    /*  94 */ "Cast"             OpHelp("affinity(r[P1])"),
        29565  +    /*  95 */ "Permutation"      OpHelp(""),
        29566  +    /*  96 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 29506  29567       /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 29507         -    /*  98 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 29508         -    /*  99 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 29509         -    /* 100 */ "Count"            OpHelp("r[P2]=count()"),
 29510         -    /* 101 */ "ReadCookie"       OpHelp(""),
 29511         -    /* 102 */ "SetCookie"        OpHelp(""),
 29512         -    /* 103 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 29513         -    /* 104 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 29514         -    /* 105 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 29515         -    /* 106 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 29516         -    /* 107 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 29517         -    /* 108 */ "SorterOpen"       OpHelp(""),
 29518         -    /* 109 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 29519         -    /* 110 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 29520         -    /* 111 */ "Close"            OpHelp(""),
 29521         -    /* 112 */ "ColumnsUsed"      OpHelp(""),
 29522         -    /* 113 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 29523         -    /* 114 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 29524         -    /* 115 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 29525         -    /* 116 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 29526         -    /* 117 */ "Delete"           OpHelp(""),
 29527         -    /* 118 */ "ResetCount"       OpHelp(""),
 29528         -    /* 119 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 29529         -    /* 120 */ "SorterData"       OpHelp("r[P2]=data"),
 29530         -    /* 121 */ "RowData"          OpHelp("r[P2]=data"),
 29531         -    /* 122 */ "Rowid"            OpHelp("r[P2]=rowid"),
 29532         -    /* 123 */ "NullRow"          OpHelp(""),
 29533         -    /* 124 */ "SorterInsert"     OpHelp("key=r[P2]"),
 29534         -    /* 125 */ "IdxInsert"        OpHelp("key=r[P2]"),
 29535         -    /* 126 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 29536         -    /* 127 */ "Seek"             OpHelp("Move P3 to P1.rowid"),
 29537         -    /* 128 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 29538         -    /* 129 */ "Destroy"          OpHelp(""),
 29539         -    /* 130 */ "Clear"            OpHelp(""),
 29540         -    /* 131 */ "ResetSorter"      OpHelp(""),
        29568  +    /*  98 */ "Column"           OpHelp("r[P3]=PX"),
        29569  +    /*  99 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
        29570  +    /* 100 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        29571  +    /* 101 */ "Count"            OpHelp("r[P2]=count()"),
        29572  +    /* 102 */ "ReadCookie"       OpHelp(""),
        29573  +    /* 103 */ "SetCookie"        OpHelp(""),
        29574  +    /* 104 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        29575  +    /* 105 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        29576  +    /* 106 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        29577  +    /* 107 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
        29578  +    /* 108 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
        29579  +    /* 109 */ "SorterOpen"       OpHelp(""),
        29580  +    /* 110 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
        29581  +    /* 111 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
        29582  +    /* 112 */ "Close"            OpHelp(""),
        29583  +    /* 113 */ "ColumnsUsed"      OpHelp(""),
        29584  +    /* 114 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
        29585  +    /* 115 */ "NewRowid"         OpHelp("r[P2]=rowid"),
        29586  +    /* 116 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
        29587  +    /* 117 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
        29588  +    /* 118 */ "Delete"           OpHelp(""),
        29589  +    /* 119 */ "ResetCount"       OpHelp(""),
        29590  +    /* 120 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        29591  +    /* 121 */ "SorterData"       OpHelp("r[P2]=data"),
        29592  +    /* 122 */ "RowData"          OpHelp("r[P2]=data"),
        29593  +    /* 123 */ "Rowid"            OpHelp("r[P2]=rowid"),
        29594  +    /* 124 */ "NullRow"          OpHelp(""),
        29595  +    /* 125 */ "SorterInsert"     OpHelp("key=r[P2]"),
        29596  +    /* 126 */ "IdxInsert"        OpHelp("key=r[P2]"),
        29597  +    /* 127 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        29598  +    /* 128 */ "Seek"             OpHelp("Move P3 to P1.rowid"),
        29599  +    /* 129 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        29600  +    /* 130 */ "Destroy"          OpHelp(""),
        29601  +    /* 131 */ "Clear"            OpHelp(""),
 29541  29602       /* 132 */ "Real"             OpHelp("r[P2]=P4"),
 29542         -    /* 133 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
 29543         -    /* 134 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
 29544         -    /* 135 */ "ParseSchema"      OpHelp(""),
 29545         -    /* 136 */ "LoadAnalysis"     OpHelp(""),
 29546         -    /* 137 */ "DropTable"        OpHelp(""),
 29547         -    /* 138 */ "DropIndex"        OpHelp(""),
 29548         -    /* 139 */ "DropTrigger"      OpHelp(""),
 29549         -    /* 140 */ "IntegrityCk"      OpHelp(""),
 29550         -    /* 141 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 29551         -    /* 142 */ "Param"            OpHelp(""),
 29552         -    /* 143 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 29553         -    /* 144 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 29554         -    /* 145 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
 29555         -    /* 146 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
 29556         -    /* 147 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 29557         -    /* 148 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 29558         -    /* 149 */ "Expire"           OpHelp(""),
 29559         -    /* 150 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 29560         -    /* 151 */ "VBegin"           OpHelp(""),
 29561         -    /* 152 */ "VCreate"          OpHelp(""),
 29562         -    /* 153 */ "VDestroy"         OpHelp(""),
 29563         -    /* 154 */ "VOpen"            OpHelp(""),
 29564         -    /* 155 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 29565         -    /* 156 */ "VRename"          OpHelp(""),
 29566         -    /* 157 */ "Pagecount"        OpHelp(""),
 29567         -    /* 158 */ "MaxPgcnt"         OpHelp(""),
 29568         -    /* 159 */ "CursorHint"       OpHelp(""),
 29569         -    /* 160 */ "Noop"             OpHelp(""),
 29570         -    /* 161 */ "Explain"          OpHelp(""),
        29603  +    /* 133 */ "ResetSorter"      OpHelp(""),
        29604  +    /* 134 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
        29605  +    /* 135 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
        29606  +    /* 136 */ "SqlExec"          OpHelp(""),
        29607  +    /* 137 */ "ParseSchema"      OpHelp(""),
        29608  +    /* 138 */ "LoadAnalysis"     OpHelp(""),
        29609  +    /* 139 */ "DropTable"        OpHelp(""),
        29610  +    /* 140 */ "DropIndex"        OpHelp(""),
        29611  +    /* 141 */ "DropTrigger"      OpHelp(""),
        29612  +    /* 142 */ "IntegrityCk"      OpHelp(""),
        29613  +    /* 143 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        29614  +    /* 144 */ "Param"            OpHelp(""),
        29615  +    /* 145 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        29616  +    /* 146 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        29617  +    /* 147 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
        29618  +    /* 148 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
        29619  +    /* 149 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
        29620  +    /* 150 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        29621  +    /* 151 */ "Expire"           OpHelp(""),
        29622  +    /* 152 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        29623  +    /* 153 */ "VBegin"           OpHelp(""),
        29624  +    /* 154 */ "VCreate"          OpHelp(""),
        29625  +    /* 155 */ "VDestroy"         OpHelp(""),
        29626  +    /* 156 */ "VOpen"            OpHelp(""),
        29627  +    /* 157 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        29628  +    /* 158 */ "VRename"          OpHelp(""),
        29629  +    /* 159 */ "Pagecount"        OpHelp(""),
        29630  +    /* 160 */ "MaxPgcnt"         OpHelp(""),
        29631  +    /* 161 */ "CursorHint"       OpHelp(""),
        29632  +    /* 162 */ "Noop"             OpHelp(""),
        29633  +    /* 163 */ "Explain"          OpHelp(""),
 29571  29634     };
 29572  29635     return azName[i];
 29573  29636   }
 29574  29637   #endif
 29575  29638   
 29576  29639   /************** End of opcodes.c *********************************************/
 29577  29640   /************** Begin file os_unix.c *****************************************/
................................................................................
 37945  38008    *          winMemShutdown function is called (e.g. by the sqlite3_shutdown
 37946  38009    *          function), all data that was allocated using the isolated heap will
 37947  38010    *          be freed immediately and any attempt to access any of that freed
 37948  38011    *          data will almost certainly result in an immediate access violation.
 37949  38012    ******************************************************************************
 37950  38013    */
 37951  38014   #ifndef SQLITE_WIN32_HEAP_CREATE
 37952         -#  define SQLITE_WIN32_HEAP_CREATE    (TRUE)
        38015  +#  define SQLITE_WIN32_HEAP_CREATE        (TRUE)
        38016  +#endif
        38017  +
        38018  +/*
        38019  + * This is the maximum possible initial size of the Win32-specific heap, in
        38020  + * bytes.
        38021  + */
        38022  +#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
        38023  +#  define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
        38024  +#endif
        38025  +
        38026  +/*
        38027  + * This is the extra space for the initial size of the Win32-specific heap,
        38028  + * in bytes.  This value may be zero.
        38029  + */
        38030  +#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
        38031  +#  define SQLITE_WIN32_HEAP_INIT_EXTRA  (4194304)
        38032  +#endif
        38033  +
        38034  +/*
        38035  + * Calculate the maximum legal cache size, in pages, based on the maximum
        38036  + * possible initial heap size and the default page size, setting aside the
        38037  + * needed extra space.
        38038  + */
        38039  +#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
        38040  +#  define SQLITE_WIN32_MAX_CACHE_SIZE   (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
        38041  +                                          (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
        38042  +                                         (SQLITE_DEFAULT_PAGE_SIZE))
 37953  38043   #endif
 37954  38044   
 37955  38045   /*
 37956  38046    * This is cache size used in the calculation of the initial size of the
 37957  38047    * Win32-specific heap.  It cannot be negative.
 37958  38048    */
 37959  38049   #ifndef SQLITE_WIN32_CACHE_SIZE
 37960  38050   #  if SQLITE_DEFAULT_CACHE_SIZE>=0
 37961         -#    define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
        38051  +#    define SQLITE_WIN32_CACHE_SIZE     (SQLITE_DEFAULT_CACHE_SIZE)
 37962  38052   #  else
 37963         -#    define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
        38053  +#    define SQLITE_WIN32_CACHE_SIZE     (-(SQLITE_DEFAULT_CACHE_SIZE))
 37964  38054   #  endif
 37965  38055   #endif
 37966  38056   
        38057  +/*
        38058  + * Make sure that the calculated cache size, in pages, cannot cause the
        38059  + * initial size of the Win32-specific heap to exceed the maximum amount
        38060  + * of memory that can be specified in the call to HeapCreate.
        38061  + */
        38062  +#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
        38063  +#  undef SQLITE_WIN32_CACHE_SIZE
        38064  +#  define SQLITE_WIN32_CACHE_SIZE       (2000)
        38065  +#endif
        38066  +
 37967  38067   /*
 37968  38068    * The initial size of the Win32-specific heap.  This value may be zero.
 37969  38069    */
 37970  38070   #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
 37971         -#  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
 37972         -                                       (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
        38071  +#  define SQLITE_WIN32_HEAP_INIT_SIZE   ((SQLITE_WIN32_CACHE_SIZE) * \
        38072  +                                         (SQLITE_DEFAULT_PAGE_SIZE) + \
        38073  +                                         (SQLITE_WIN32_HEAP_INIT_EXTRA))
 37973  38074   #endif
 37974  38075   
 37975  38076   /*
 37976  38077    * The maximum size of the Win32-specific heap.  This value may be zero.
 37977  38078    */
 37978  38079   #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
 37979         -#  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
        38080  +#  define SQLITE_WIN32_HEAP_MAX_SIZE    (0)
 37980  38081   #endif
 37981  38082   
 37982  38083   /*
 37983  38084    * The extra flags to use in calls to the Win32 heap APIs.  This value may be
 37984  38085    * zero for the default behavior.
 37985  38086    */
 37986  38087   #ifndef SQLITE_WIN32_HEAP_FLAGS
 37987         -#  define SQLITE_WIN32_HEAP_FLAGS     (0)
        38088  +#  define SQLITE_WIN32_HEAP_FLAGS       (0)
 37988  38089   #endif
 37989  38090   
 37990  38091   
 37991  38092   /*
 37992  38093   ** The winMemData structure stores information required by the Win32-specific
 37993  38094   ** sqlite3_mem_methods implementation.
 37994  38095   */
................................................................................
 44077  44178   ** Return false if any invariant is violated.
 44078  44179   **
 44079  44180   ** This routine is for use inside of assert() statements only.  For
 44080  44181   ** example:
 44081  44182   **
 44082  44183   **          assert( sqlite3PcachePageSanity(pPg) );
 44083  44184   */
 44084         -#if SQLITE_DEBUG
        44185  +#ifdef SQLITE_DEBUG
 44085  44186   SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
 44086  44187     PCache *pCache;
 44087  44188     assert( pPg!=0 );
 44088  44189     assert( pPg->pgno>0 || pPg->pPager==0 );    /* Page number is 1 or more */
 44089  44190     pCache = pPg->pCache;
 44090  44191     assert( pCache!=0 );      /* Every page has an associated PCache */
 44091  44192     if( pPg->flags & PGHDR_CLEAN ){
................................................................................
 58548  58649   /*
 58549  58650   ** get2byteAligned(), unlike get2byte(), requires that its argument point to a
 58550  58651   ** two-byte aligned address.  get2bytea() is only used for accessing the
 58551  58652   ** cell addresses in a btree header.
 58552  58653   */
 58553  58654   #if SQLITE_BYTEORDER==4321
 58554  58655   # define get2byteAligned(x)  (*(u16*)(x))
 58555         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 58556         -    && GCC_VERSION>=4008000
        58656  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
 58557  58657   # define get2byteAligned(x)  __builtin_bswap16(*(u16*)(x))
 58558         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
 58559         -    && defined(_MSC_VER) && _MSC_VER>=1300
        58658  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 58560  58659   # define get2byteAligned(x)  _byteswap_ushort(*(u16*)(x))
 58561  58660   #else
 58562  58661   # define get2byteAligned(x)  ((x)[0]<<8 | (x)[1])
 58563  58662   #endif
 58564  58663   
 58565  58664   /************** End of btreeInt.h ********************************************/
 58566  58665   /************** Continuing where we left off in btmutex.c ********************/
................................................................................
 60172  60271       ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
 60173  60272     }
 60174  60273   }
 60175  60274   #endif
 60176  60275   
 60177  60276   
 60178  60277   /*
 60179         -** Defragment the page given.  All Cells are moved to the
 60180         -** end of the page and all free space is collected into one
 60181         -** big FreeBlk that occurs in between the header and cell
 60182         -** pointer array and the cell content area.
        60278  +** Defragment the page given. This routine reorganizes cells within the
        60279  +** page so that there are no free-blocks on the free-block list.
        60280  +**
        60281  +** Parameter nMaxFrag is the maximum amount of fragmented space that may be
        60282  +** present in the page after this routine returns.
 60183  60283   **
 60184  60284   ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
 60185  60285   ** b-tree page so that there are no freeblocks or fragment bytes, all
 60186  60286   ** unused bytes are contained in the unallocated space region, and all
 60187  60287   ** cells are packed tightly at the end of the page.
 60188  60288   */
 60189         -static int defragmentPage(MemPage *pPage){
        60289  +static int defragmentPage(MemPage *pPage, int nMaxFrag){
 60190  60290     int i;                     /* Loop counter */
 60191  60291     int pc;                    /* Address of the i-th cell */
 60192  60292     int hdr;                   /* Offset to the page header */
 60193  60293     int size;                  /* Size of a cell */
 60194  60294     int usableSize;            /* Number of usable bytes on a page */
 60195  60295     int cellOffset;            /* Offset to the cell pointer array */
 60196  60296     int cbrk;                  /* Offset to the cell content area */
................................................................................
 60197  60297     int nCell;                 /* Number of cells on the page */
 60198  60298     unsigned char *data;       /* The page data */
 60199  60299     unsigned char *temp;       /* Temp area for cell content */
 60200  60300     unsigned char *src;        /* Source of content */
 60201  60301     int iCellFirst;            /* First allowable cell index */
 60202  60302     int iCellLast;             /* Last possible cell index */
 60203  60303   
 60204         -
 60205  60304     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 60206  60305     assert( pPage->pBt!=0 );
 60207  60306     assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
 60208  60307     assert( pPage->nOverflow==0 );
 60209  60308     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 60210  60309     temp = 0;
 60211  60310     src = data = pPage->aData;
 60212  60311     hdr = pPage->hdrOffset;
 60213  60312     cellOffset = pPage->cellOffset;
 60214  60313     nCell = pPage->nCell;
 60215  60314     assert( nCell==get2byte(&data[hdr+3]) );
        60315  +  iCellFirst = cellOffset + 2*nCell;
 60216  60316     usableSize = pPage->pBt->usableSize;
        60317  +
        60318  +  /* This block handles pages with two or fewer free blocks and nMaxFrag
        60319  +  ** or fewer fragmented bytes. In this case it is faster to move the
        60320  +  ** two (or one) blocks of cells using memmove() and add the required
        60321  +  ** offsets to each pointer in the cell-pointer array than it is to 
        60322  +  ** reconstruct the entire page.  */
        60323  +  if( (int)data[hdr+7]<=nMaxFrag ){
        60324  +    int iFree = get2byte(&data[hdr+1]);
        60325  +    if( iFree ){
        60326  +      int iFree2 = get2byte(&data[iFree]);
        60327  +
        60328  +      /* pageFindSlot() has already verified that free blocks are sorted
        60329  +      ** in order of offset within the page, and that no block extends
        60330  +      ** past the end of the page. Provided the two free slots do not 
        60331  +      ** overlap, this guarantees that the memmove() calls below will not
        60332  +      ** overwrite the usableSize byte buffer, even if the database page
        60333  +      ** is corrupt.  */
        60334  +      assert( iFree2==0 || iFree2>iFree );
        60335  +      assert( iFree+get2byte(&data[iFree+2]) <= usableSize );
        60336  +      assert( iFree2==0 || iFree2+get2byte(&data[iFree2+2]) <= usableSize );
        60337  +
        60338  +      if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
        60339  +        u8 *pEnd = &data[cellOffset + nCell*2];
        60340  +        u8 *pAddr;
        60341  +        int sz2 = 0;
        60342  +        int sz = get2byte(&data[iFree+2]);
        60343  +        int top = get2byte(&data[hdr+5]);
        60344  +        if( iFree2 ){
        60345  +          if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_BKPT;
        60346  +          sz2 = get2byte(&data[iFree2+2]);
        60347  +          assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
        60348  +          memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
        60349  +          sz += sz2;
        60350  +        }
        60351  +        cbrk = top+sz;
        60352  +        assert( cbrk+(iFree-top) <= usableSize );
        60353  +        memmove(&data[cbrk], &data[top], iFree-top);
        60354  +        for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
        60355  +          pc = get2byte(pAddr);
        60356  +          if( pc<iFree ){ put2byte(pAddr, pc+sz); }
        60357  +          else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
        60358  +        }
        60359  +        goto defragment_out;
        60360  +      }
        60361  +    }
        60362  +  }
        60363  +
 60217  60364     cbrk = usableSize;
 60218         -  iCellFirst = cellOffset + 2*nCell;
 60219  60365     iCellLast = usableSize - 4;
 60220  60366     for(i=0; i<nCell; i++){
 60221  60367       u8 *pAddr;     /* The i-th cell pointer */
 60222  60368       pAddr = &data[cellOffset + i*2];
 60223  60369       pc = get2byte(pAddr);
 60224  60370       testcase( pc==iCellFirst );
 60225  60371       testcase( pc==iCellLast );
................................................................................
 60245  60391         temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
 60246  60392         x = get2byte(&data[hdr+5]);
 60247  60393         memcpy(&temp[x], &data[x], (cbrk+size) - x);
 60248  60394         src = temp;
 60249  60395       }
 60250  60396       memcpy(&data[cbrk], &src[pc], size);
 60251  60397     }
        60398  +  data[hdr+7] = 0;
        60399  +
        60400  + defragment_out:
        60401  +  if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
        60402  +    return SQLITE_CORRUPT_BKPT;
        60403  +  }
 60252  60404     assert( cbrk>=iCellFirst );
 60253  60405     put2byte(&data[hdr+5], cbrk);
 60254  60406     data[hdr+1] = 0;
 60255  60407     data[hdr+2] = 0;
 60256         -  data[hdr+7] = 0;
 60257  60408     memset(&data[iCellFirst], 0, cbrk-iCellFirst);
 60258  60409     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 60259         -  if( cbrk-iCellFirst!=pPage->nFree ){
 60260         -    return SQLITE_CORRUPT_BKPT;
 60261         -  }
 60262  60410     return SQLITE_OK;
 60263  60411   }
 60264  60412   
 60265  60413   /*
 60266  60414   ** Search the free-list on page pPg for space to store a cell nByte bytes in
 60267  60415   ** size. If one can be found, return a pointer to the space and remove it
 60268  60416   ** from the free-list.
................................................................................
 60392  60540   
 60393  60541     /* The request could not be fulfilled using a freelist slot.  Check
 60394  60542     ** to see if defragmentation is necessary.
 60395  60543     */
 60396  60544     testcase( gap+2+nByte==top );
 60397  60545     if( gap+2+nByte>top ){
 60398  60546       assert( pPage->nCell>0 || CORRUPT_DB );
 60399         -    rc = defragmentPage(pPage);
        60547  +    rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
 60400  60548       if( rc ) return rc;
 60401  60549       top = get2byteNotZero(&data[hdr+5]);
 60402         -    assert( gap+nByte<=top );
        60550  +    assert( gap+2+nByte<=top );
 60403  60551     }
 60404  60552   
 60405  60553   
 60406  60554     /* Allocate memory from the gap in between the cell pointer array
 60407  60555     ** and the cell content area.  The btreeInitPage() call has already
 60408  60556     ** validated the freelist.  Given that the freelist is valid, there
 60409  60557     ** is no way that the allocation can extend off the end of the page.
................................................................................
 61667  61815       BTREE_AUTOVACUUM_INCR
 61668  61816     );
 61669  61817     sqlite3BtreeLeave(p);
 61670  61818     return rc;
 61671  61819   #endif
 61672  61820   }
 61673  61821   
        61822  +/*
        61823  +** If the user has not set the safety-level for this database connection
        61824  +** using "PRAGMA synchronous", and if the safety-level is not already
        61825  +** set to the value passed to this function as the second parameter,
        61826  +** set it so.
        61827  +*/
        61828  +#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
        61829  +static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
        61830  +  sqlite3 *db;
        61831  +  Db *pDb;
        61832  +  if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
        61833  +    while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
        61834  +    if( pDb->bSyncSet==0 
        61835  +     && pDb->safety_level!=safety_level 
        61836  +     && pDb!=&db->aDb[1] 
        61837  +    ){
        61838  +      pDb->safety_level = safety_level;
        61839  +      sqlite3PagerSetFlags(pBt->pPager,
        61840  +          pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
        61841  +    }
        61842  +  }
        61843  +}
        61844  +#else
        61845  +# define setDefaultSyncFlag(pBt,safety_level)
        61846  +#endif
 61674  61847   
 61675  61848   /*
 61676  61849   ** Get a reference to pPage1 of the database file.  This will
 61677  61850   ** also acquire a readlock on that file.
 61678  61851   **
 61679  61852   ** SQLITE_OK is returned on success.  If the file is not a
 61680  61853   ** well-formed database file, then SQLITE_CORRUPT is returned.
................................................................................
 61740  61913       */
 61741  61914       if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
 61742  61915         int isOpen = 0;
 61743  61916         rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
 61744  61917         if( rc!=SQLITE_OK ){
 61745  61918           goto page1_init_failed;
 61746  61919         }else{
 61747         -#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
 61748         -        sqlite3 *db;
 61749         -        Db *pDb;
 61750         -        if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
 61751         -          while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
 61752         -          if( pDb->bSyncSet==0
 61753         -           && pDb->safety_level==SQLITE_DEFAULT_SYNCHRONOUS+1
 61754         -          ){
 61755         -            pDb->safety_level = SQLITE_DEFAULT_WAL_SYNCHRONOUS+1;
 61756         -            sqlite3PagerSetFlags(pBt->pPager,
 61757         -               pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
 61758         -          }
 61759         -        }
 61760         -#endif
        61920  +        setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
 61761  61921           if( isOpen==0 ){
 61762  61922             releasePage(pPage1);
 61763  61923             return SQLITE_OK;
 61764  61924           }
 61765  61925         }
 61766  61926         rc = SQLITE_NOTADB;
        61927  +    }else{
        61928  +      setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
 61767  61929       }
 61768  61930   #endif
 61769  61931   
 61770  61932       /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
 61771  61933       ** fractions and the leaf payload fraction values must be 64, 32, and 32.
 61772  61934       **
 61773  61935       ** The original design allowed these amounts to vary, but as of
................................................................................
 63631  63793     pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
 63632  63794     pCur->iPage++;
 63633  63795     pCur->aiIdx[pCur->iPage] = 0;
 63634  63796     return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage],
 63635  63797                           pCur, pCur->curPagerFlags);
 63636  63798   }
 63637  63799   
 63638         -#if SQLITE_DEBUG
        63800  +#ifdef SQLITE_DEBUG
 63639  63801   /*
 63640  63802   ** Page pParent is an internal (non-leaf) tree page. This function 
 63641  63803   ** asserts that page number iChild is the left-child if the iIdx'th
 63642  63804   ** cell in page pParent. Or, if iIdx is equal to the total number of
 63643  63805   ** cells in pParent, that page number iChild is the right-child of
 63644  63806   ** the page.
 63645  63807   */
................................................................................
 64173  64335   SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
 64174  64336     /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
 64175  64337     ** have been deleted? This API will need to change to return an error code
 64176  64338     ** as well as the boolean result value.
 64177  64339     */
 64178  64340     return (CURSOR_VALID!=pCur->eState);
 64179  64341   }
        64342  +
        64343  +/*
        64344  +** Return an estimate for the number of rows in the table that pCur is
        64345  +** pointing to.  Return a negative number if no estimate is currently 
        64346  +** available.
        64347  +*/
        64348  +SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
        64349  +  i64 n;
        64350  +  u8 i;
        64351  +
        64352  +  assert( cursorOwnsBtShared(pCur) );
        64353  +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
        64354  +
        64355  +  /* Currently this interface is only called by the OP_IfSmaller
        64356  +  ** opcode, and it that case the cursor will always be valid and
        64357  +  ** will always point to a leaf node. */
        64358  +  if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
        64359  +  if( NEVER(pCur->apPage[pCur->iPage]->leaf==0) ) return -1;
        64360  +
        64361  +  for(n=1, i=0; i<=pCur->iPage; i++){
        64362  +    n *= pCur->apPage[i]->nCell;
        64363  +  }
        64364  +  return n;
        64365  +}
 64180  64366   
 64181  64367   /*
 64182  64368   ** Advance the cursor to the next entry in the database.  If
 64183  64369   ** successful then set *pRes=0.  If the cursor
 64184  64370   ** was already pointing to the last entry in the database before
 64185  64371   ** this routine was called, then set *pRes=1.
 64186  64372   **
................................................................................
 65018  65204     **                      that means content must spill into overflow pages.
 65019  65205     **   *pnSize            Size of the local cell (not counting overflow pages)
 65020  65206     **   pPrior             Where to write the pgno of the first overflow page
 65021  65207     **
 65022  65208     ** Use a call to btreeParseCellPtr() to verify that the values above
 65023  65209     ** were computed correctly.
 65024  65210     */
 65025         -#if SQLITE_DEBUG
        65211  +#ifdef SQLITE_DEBUG
 65026  65212     {
 65027  65213       CellInfo info;
 65028  65214       pPage->xParseCell(pPage, pCell, &info);
 65029  65215       assert( nHeader==(int)(info.pPayload - pCell) );
 65030  65216       assert( info.nKey==pX->nKey );
 65031  65217       assert( *pnSize == info.nSize );
 65032  65218       assert( spaceLeft == info.nLocal );
................................................................................
 66544  66730       **
 66545  66731       ** It is critical that the child page be defragmented before being
 66546  66732       ** copied into the parent, because if the parent is page 1 then it will
 66547  66733       ** by smaller than the child due to the database header, and so all the
 66548  66734       ** free space needs to be up front.
 66549  66735       */
 66550  66736       assert( nNew==1 || CORRUPT_DB );
 66551         -    rc = defragmentPage(apNew[0]);
        66737  +    rc = defragmentPage(apNew[0], -1);
 66552  66738       testcase( rc!=SQLITE_OK );
 66553  66739       assert( apNew[0]->nFree == 
 66554  66740           (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
 66555  66741         || rc!=SQLITE_OK
 66556  66742       );
 66557  66743       copyNodeContent(apNew[0], pParent, &rc);
 66558  66744       freePage(apNew[0], &rc);
................................................................................
 71196  71382   
 71197  71383   /*
 71198  71384   ** Remember the SQL string for a prepared statement.
 71199  71385   */
 71200  71386   SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
 71201  71387     assert( isPrepareV2==1 || isPrepareV2==0 );
 71202  71388     if( p==0 ) return;
        71389  +  if( !isPrepareV2 ) p->expmask = 0;
 71203  71390   #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
 71204  71391     if( !isPrepareV2 ) return;
 71205  71392   #endif
 71206  71393     assert( p->zSql==0 );
 71207  71394     p->zSql = sqlite3DbStrNDup(p->db, z, n);
 71208  71395     p->isPrepareV2 = (u8)isPrepareV2;
 71209  71396   }
................................................................................
 71224  71411     pTmp = pA->pPrev;
 71225  71412     pA->pPrev = pB->pPrev;
 71226  71413     pB->pPrev = pTmp;
 71227  71414     zTmp = pA->zSql;
 71228  71415     pA->zSql = pB->zSql;
 71229  71416     pB->zSql = zTmp;
 71230  71417     pB->isPrepareV2 = pA->isPrepareV2;
        71418  +  pB->expmask = pA->expmask;
 71231  71419   }
 71232  71420   
 71233  71421   /*
 71234  71422   ** Resize the Vdbe.aOp array so that it is at least nOp elements larger 
 71235  71423   ** than its current size. nOp is guaranteed to be less than or equal
 71236  71424   ** to 1024/sizeof(Op).
 71237  71425   **
................................................................................
 71253  71441     ** size of the op array or add 1KB of space, whichever is smaller. */
 71254  71442   #ifdef SQLITE_TEST_REALLOC_STRESS
 71255  71443     int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
 71256  71444   #else
 71257  71445     int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
 71258  71446     UNUSED_PARAMETER(nOp);
 71259  71447   #endif
        71448  +
        71449  +  /* Ensure that the size of a VDBE does not grow too large */
        71450  +  if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
        71451  +    sqlite3OomFault(p->db);
        71452  +    return SQLITE_NOMEM;
        71453  +  }
 71260  71454   
 71261  71455     assert( nOp<=(1024/sizeof(Op)) );
 71262  71456     assert( nNew>=(p->nOpAlloc+nOp) );
 71263  71457     pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
 71264  71458     if( pNew ){
 71265  71459       p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
 71266  71460       p->nOpAlloc = p->szOpAlloc/sizeof(Op);
................................................................................
 73748  73942     **     SQLITE_INTERRUPT
 73749  73943     **
 73750  73944     ** Then the internal cache might have been left in an inconsistent
 73751  73945     ** state.  We need to rollback the statement transaction, if there is
 73752  73946     ** one, or the complete transaction if there is no statement transaction.
 73753  73947     */
 73754  73948   
        73949  +  if( p->magic!=VDBE_MAGIC_RUN ){
        73950  +    return SQLITE_OK;
        73951  +  }
 73755  73952     if( db->mallocFailed ){
 73756  73953       p->rc = SQLITE_NOMEM_BKPT;
 73757  73954     }
 73758  73955     closeAllCursors(p);
 73759         -  if( p->magic!=VDBE_MAGIC_RUN ){
 73760         -    return SQLITE_OK;
 73761         -  }
 73762  73956     checkActiveVdbeCnt(db);
 73763  73957   
 73764  73958     /* No commit or rollback needed if the program never started or if the
 73765  73959     ** SQL statement does not read or write a database file.  */
 73766  73960     if( p->pc>=0 && p->bIsReader ){
 73767  73961       int mrc;   /* Primary error code from p->rc */
 73768  73962       int eStatementOp = 0;
................................................................................
 74701  74895       pMem++;
 74702  74896       if( (++u)>=p->nField ) break;
 74703  74897     }
 74704  74898     assert( u<=pKeyInfo->nField + 1 );
 74705  74899     p->nField = u;
 74706  74900   }
 74707  74901   
 74708         -#if SQLITE_DEBUG
        74902  +#ifdef SQLITE_DEBUG
 74709  74903   /*
 74710  74904   ** This function compares two index or table record keys in the same way
 74711  74905   ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
 74712  74906   ** this function deserializes and compares values using the
 74713  74907   ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
 74714  74908   ** in assert() statements to ensure that the optimized code in
 74715  74909   ** sqlite3VdbeRecordCompare() returns results with these two primitives.
................................................................................
 74806  75000     if( desiredResult>0 && rc>0 ) return 1;
 74807  75001     if( CORRUPT_DB ) return 1;
 74808  75002     if( pKeyInfo->db->mallocFailed ) return 1;
 74809  75003     return 0;
 74810  75004   }
 74811  75005   #endif
 74812  75006   
 74813         -#if SQLITE_DEBUG
        75007  +#ifdef SQLITE_DEBUG
 74814  75008   /*
 74815  75009   ** Count the number of fields (a.k.a. columns) in the record given by
 74816  75010   ** pKey,nKey.  The verify that this count is less than or equal to the
 74817  75011   ** limit given by pKeyInfo->nField + pKeyInfo->nXField.
 74818  75012   **
 74819  75013   ** If this constraint is not satisfied, it means that the high-speed
 74820  75014   ** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
................................................................................
 75689  75883   /*
 75690  75884   ** Configure SQL variable iVar so that binding a new value to it signals
 75691  75885   ** to sqlite3_reoptimize() that re-preparing the statement may result
 75692  75886   ** in a better query plan.
 75693  75887   */
 75694  75888   SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
 75695  75889     assert( iVar>0 );
 75696         -  if( iVar>32 ){
 75697         -    v->expmask = 0xffffffff;
        75890  +  if( iVar>=32 ){
        75891  +    v->expmask |= 0x80000000;
 75698  75892     }else{
 75699  75893       v->expmask |= ((u32)1 << (iVar-1));
 75700  75894     }
 75701  75895   }
 75702  75896   
 75703  75897   #ifndef SQLITE_OMIT_VIRTUALTABLE
 75704  75898   /*
................................................................................
 75960  76154     sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
 75961  76155   #endif
 75962  76156     sqlite3_mutex_enter(mutex);
 75963  76157     for(i=0; i<p->nVar; i++){
 75964  76158       sqlite3VdbeMemRelease(&p->aVar[i]);
 75965  76159       p->aVar[i].flags = MEM_Null;
 75966  76160     }
 75967         -  if( p->isPrepareV2 && p->expmask ){
        76161  +  assert( p->isPrepareV2 || p->expmask==0 );
        76162  +  if( p->expmask ){
 75968  76163       p->expired = 1;
 75969  76164     }
 75970  76165     sqlite3_mutex_leave(mutex);
 75971  76166     return rc;
 75972  76167   }
 75973  76168   
 75974  76169   
................................................................................
 77064  77259     **
 77065  77260     ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
 77066  77261     ** parameter in the WHERE clause might influence the choice of query plan
 77067  77262     ** for a statement, then the statement will be automatically recompiled,
 77068  77263     ** as if there had been a schema change, on the first sqlite3_step() call
 77069  77264     ** following any change to the bindings of that parameter.
 77070  77265     */
 77071         -  if( p->isPrepareV2 &&
 77072         -     ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
 77073         -  ){
        77266  +  assert( p->isPrepareV2 || p->expmask==0 );
        77267  +  if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
 77074  77268       p->expired = 1;
 77075  77269     }
 77076  77270     return SQLITE_OK;
 77077  77271   }
 77078  77272   
 77079  77273   /*
 77080  77274   ** Bind a text or BLOB value.
................................................................................
 77329  77523   */
 77330  77524   SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
 77331  77525     Vdbe *pFrom = (Vdbe*)pFromStmt;
 77332  77526     Vdbe *pTo = (Vdbe*)pToStmt;
 77333  77527     if( pFrom->nVar!=pTo->nVar ){
 77334  77528       return SQLITE_ERROR;
 77335  77529     }
 77336         -  if( pTo->isPrepareV2 && pTo->expmask ){
        77530  +  assert( pTo->isPrepareV2 || pTo->expmask==0 );
        77531  +  if( pTo->expmask ){
 77337  77532       pTo->expired = 1;
 77338  77533     }
 77339         -  if( pFrom->isPrepareV2 && pFrom->expmask ){
        77534  +  assert( pFrom->isPrepareV2 || pFrom->expmask==0 );
        77535  +  if( pFrom->expmask ){
 77340  77536       pFrom->expired = 1;
 77341  77537     }
 77342  77538     return sqlite3TransferBindings(pFromStmt, pToStmt);
 77343  77539   }
 77344  77540   #endif
 77345  77541   
 77346  77542   /*
................................................................................
 78298  78494         assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
 78299  78495       }else if( f & MEM_Ephem ){
 78300  78496         c = 'e';
 78301  78497         assert( (f & (MEM_Static|MEM_Dyn))==0 );
 78302  78498       }else{
 78303  78499         c = 's';
 78304  78500       }
 78305         -
 78306         -    sqlite3_snprintf(100, zCsr, "%c", c);
 78307         -    zCsr += sqlite3Strlen30(zCsr);
        78501  +    *(zCsr++) = c;
 78308  78502       sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
 78309  78503       zCsr += sqlite3Strlen30(zCsr);
 78310  78504       for(i=0; i<16 && i<pMem->n; i++){
 78311  78505         sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
 78312  78506         zCsr += sqlite3Strlen30(zCsr);
 78313  78507       }
 78314  78508       for(i=0; i<16 && i<pMem->n; i++){
 78315  78509         char z = pMem->z[i];
 78316  78510         if( z<32 || z>126 ) *zCsr++ = '.';
 78317  78511         else *zCsr++ = z;
 78318  78512       }
 78319         -
 78320         -    sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
 78321         -    zCsr += sqlite3Strlen30(zCsr);
        78513  +    *(zCsr++) = ']';
 78322  78514       if( f & MEM_Zero ){
 78323  78515         sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
 78324  78516         zCsr += sqlite3Strlen30(zCsr);
 78325  78517       }
 78326  78518       *zCsr = '\0';
 78327  78519     }else if( f & MEM_Str ){
 78328  78520       int j, k;
................................................................................
 79653  79845     ** reinitializes the relavant parts of the sqlite3_context object */
 79654  79846     pOut = &aMem[pOp->p3];
 79655  79847     if( pCtx->pOut != pOut ){
 79656  79848       pCtx->pOut = pOut;
 79657  79849       for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
 79658  79850     }
 79659  79851   
 79660         -  memAboutToChange(p, pCtx->pOut);
        79852  +  memAboutToChange(p, pOut);
 79661  79853   #ifdef SQLITE_DEBUG
 79662  79854     for(i=0; i<pCtx->argc; i++){
 79663  79855       assert( memIsValid(pCtx->argv[i]) );
 79664  79856       REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
 79665  79857     }
 79666  79858   #endif
 79667         -  MemSetTypeFlag(pCtx->pOut, MEM_Null);
        79859  +  MemSetTypeFlag(pOut, MEM_Null);
 79668  79860     pCtx->fErrorOrAux = 0;
 79669  79861     (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
 79670  79862   
 79671  79863     /* If the function returned an error, throw an exception */
 79672  79864     if( pCtx->fErrorOrAux ){
 79673  79865       if( pCtx->isError ){
 79674         -      sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
        79866  +      sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
 79675  79867         rc = pCtx->isError;
 79676  79868       }
 79677  79869       sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
 79678  79870       if( rc ) goto abort_due_to_error;
 79679  79871     }
 79680  79872   
 79681  79873     /* Copy the result of the function into register P3 */
 79682  79874     if( pOut->flags & (MEM_Str|MEM_Blob) ){
 79683         -    sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
 79684         -    if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
        79875  +    sqlite3VdbeChangeEncoding(pOut, encoding);
        79876  +    if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
 79685  79877     }
 79686  79878   
 79687         -  REGISTER_TRACE(pOp->p3, pCtx->pOut);
 79688         -  UPDATE_MAX_BLOBSIZE(pCtx->pOut);
        79879  +  REGISTER_TRACE(pOp->p3, pOut);
        79880  +  UPDATE_MAX_BLOBSIZE(pOut);
 79689  79881     break;
 79690  79882   }
 79691  79883   
 79692  79884   /* Opcode: BitAnd P1 P2 P3 * *
 79693  79885   ** Synopsis: r[P3]=r[P1]&r[P2]
 79694  79886   **
 79695  79887   ** Take the bit-wise AND of the values in register P1 and P2 and
................................................................................
 80182  80374     }
 80183  80375     n = pOp->p3;
 80184  80376     pKeyInfo = pOp->p4.pKeyInfo;
 80185  80377     assert( n>0 );
 80186  80378     assert( pKeyInfo!=0 );
 80187  80379     p1 = pOp->p1;
 80188  80380     p2 = pOp->p2;
 80189         -#if SQLITE_DEBUG
        80381  +#ifdef SQLITE_DEBUG
 80190  80382     if( aPermute ){
 80191  80383       int k, mx = 0;
 80192  80384       for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
 80193  80385       assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
 80194  80386       assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
 80195  80387     }else{
 80196  80388       assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
................................................................................
 80320  80512       pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
 80321  80513     }
 80322  80514     break;
 80323  80515   }
 80324  80516   
 80325  80517   /* Opcode: Once P1 P2 * * *
 80326  80518   **
 80327         -** If the P1 value is equal to the P1 value on the OP_Init opcode at
 80328         -** instruction 0, then jump to P2.  If the two P1 values differ, then
 80329         -** set the P1 value on this opcode to equal the P1 value on the OP_Init
 80330         -** and fall through.
        80519  +** Fall through to the next instruction the first time this opcode is
        80520  +** encountered on each invocation of the byte-code program.  Jump to P2
        80521  +** on the second and all subsequent encounters during the same invocation.
        80522  +**
        80523  +** Top-level programs determine first invocation by comparing the P1
        80524  +** operand against the P1 operand on the OP_Init opcode at the beginning
        80525  +** of the program.  If the P1 values differ, then fall through and make
        80526  +** the P1 of this opcode equal to the P1 of OP_Init.  If P1 values are
        80527  +** the same then take the jump.
        80528  +**
        80529  +** For subprograms, there is a bitmask in the VdbeFrame that determines
        80530  +** whether or not the jump should be taken.  The bitmask is necessary
        80531  +** because the self-altering code trick does not work for recursive
        80532  +** triggers.
 80331  80533   */
 80332  80534   case OP_Once: {             /* jump */
        80535  +  u32 iAddr;                /* Address of this instruction */
 80333  80536     assert( p->aOp[0].opcode==OP_Init );
 80334         -  VdbeBranchTaken(p->aOp[0].p1==pOp->p1, 2);
 80335         -  if( p->aOp[0].p1==pOp->p1 ){
 80336         -    goto jump_to_p2;
        80537  +  if( p->pFrame ){
        80538  +    iAddr = (int)(pOp - p->aOp);
        80539  +    if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
        80540  +      VdbeBranchTaken(1, 2);
        80541  +      goto jump_to_p2;
        80542  +    }
        80543  +    p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
 80337  80544     }else{
 80338         -    pOp->p1 = p->aOp[0].p1;
        80545  +    if( p->aOp[0].p1==pOp->p1 ){
        80546  +      VdbeBranchTaken(1, 2);
        80547  +      goto jump_to_p2;
        80548  +    }
 80339  80549     }
        80550  +  VdbeBranchTaken(0, 2);
        80551  +  pOp->p1 = p->aOp[0].p1;
 80340  80552     break;
 80341  80553   }
 80342  80554   
 80343  80555   /* Opcode: If P1 P2 P3 * *
 80344  80556   **
 80345  80557   ** Jump to P2 if the value in register P1 is true.  The value
 80346  80558   ** is considered true if it is numeric and non-zero.  If the value
................................................................................
 80645  80857        || (len = sqlite3VdbeSerialTypeLen(t))==0
 80646  80858       ){
 80647  80859         /* Content is irrelevant for
 80648  80860         **    1. the typeof() function,
 80649  80861         **    2. the length(X) function if X is a blob, and
 80650  80862         **    3. if the content length is zero.
 80651  80863         ** So we might as well use bogus content rather than reading
 80652         -      ** content from disk. */
 80653         -      static u8 aZero[8];  /* This is the bogus content */
        80864  +      ** content from disk. 
        80865  +      **
        80866  +      ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
        80867  +      ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
        80868  +      ** read up to 16. So 16 bytes of bogus content is supplied.
        80869  +      */
        80870  +      static u8 aZero[16];  /* This is the bogus content */
 80654  80871         sqlite3VdbeSerialGet(aZero, t, pDest);
 80655  80872       }else{
 80656  80873         rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
 80657  80874         if( rc!=SQLITE_OK ) goto abort_due_to_error;
 80658  80875         sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
 80659  80876         pDest->flags &= ~MEM_Ephem;
 80660  80877       }
................................................................................
 82831  83048       }
 82832  83049     }else{
 82833  83050       assert( pOp->p2==0 );
 82834  83051     }
 82835  83052     break;
 82836  83053   }
 82837  83054   
        83055  +/* Opcode: IfSmaller P1 P2 P3 * *
        83056  +**
        83057  +** Estimate the number of rows in the table P1.  Jump to P2 if that
        83058  +** estimate is less than approximately 2**(0.1*P3).
        83059  +*/
        83060  +case OP_IfSmaller: {        /* jump */
        83061  +  VdbeCursor *pC;
        83062  +  BtCursor *pCrsr;
        83063  +  int res;
        83064  +  i64 sz;
        83065  +
        83066  +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
        83067  +  pC = p->apCsr[pOp->p1];
        83068  +  assert( pC!=0 );
        83069  +  pCrsr = pC->uc.pCursor;
        83070  +  assert( pCrsr );
        83071  +  rc = sqlite3BtreeFirst(pCrsr, &res);
        83072  +  if( rc ) goto abort_due_to_error;
        83073  +  if( res==0 ){
        83074  +    sz = sqlite3BtreeRowCountEst(pCrsr);
        83075  +    if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
        83076  +  }
        83077  +  VdbeBranchTaken(res!=0,2);
        83078  +  if( res ) goto jump_to_p2;
        83079  +  break;
        83080  +}
        83081  +
 82838  83082   
 82839  83083   /* Opcode: SorterSort P1 P2 * * *
 82840  83084   **
 82841  83085   ** After all records have been inserted into the Sorter object
 82842  83086   ** identified by P1, invoke this opcode to actually do the sorting.
 82843  83087   ** Jump to P2 if there are no records to be sorted.
 82844  83088   **
................................................................................
 83474  83718       flags = BTREE_BLOBKEY;
 83475  83719     }
 83476  83720     rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
 83477  83721     if( rc ) goto abort_due_to_error;
 83478  83722     pOut->u.i = pgno;
 83479  83723     break;
 83480  83724   }
        83725  +
        83726  +/* Opcode: SqlExec * * * P4 *
        83727  +**
        83728  +** Run the SQL statement or statements specified in the P4 string.
        83729  +*/
        83730  +case OP_SqlExec: {
        83731  +  db->nSqlExec++;
        83732  +  rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
        83733  +  db->nSqlExec--;
        83734  +  if( rc ) goto abort_due_to_error;
        83735  +  break;
        83736  +}
 83481  83737   
 83482  83738   /* Opcode: ParseSchema P1 * * P4 *
 83483  83739   **
 83484  83740   ** Read and parse all entries from the SQLITE_MASTER table of database P1
 83485  83741   ** that match the WHERE clause P4. 
 83486  83742   **
 83487  83743   ** This opcode invokes the parser to create a new virtual machine,
................................................................................
 83595  83851   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 83596  83852   /* Opcode: IntegrityCk P1 P2 P3 P4 P5
 83597  83853   **
 83598  83854   ** Do an analysis of the currently open database.  Store in
 83599  83855   ** register P1 the text of an error message describing any problems.
 83600  83856   ** If no problems are found, store a NULL in register P1.
 83601  83857   **
 83602         -** The register P3 contains the maximum number of allowed errors.
        83858  +** The register P3 contains one less than the maximum number of allowed errors.
 83603  83859   ** At most reg(P3) errors will be reported.
 83604  83860   ** In other words, the analysis stops as soon as reg(P1) errors are 
 83605  83861   ** seen.  Reg(P1) is updated with the number of errors remaining.
 83606  83862   **
 83607  83863   ** The root page numbers of all tables in the database are integers
 83608  83864   ** stored in P4_INTARRAY argument.
 83609  83865   **
................................................................................
 83628  83884     pnErr = &aMem[pOp->p3];
 83629  83885     assert( (pnErr->flags & MEM_Int)!=0 );
 83630  83886     assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
 83631  83887     pIn1 = &aMem[pOp->p1];
 83632  83888     assert( pOp->p5<db->nDb );
 83633  83889     assert( DbMaskTest(p->btreeMask, pOp->p5) );
 83634  83890     z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
 83635         -                                 (int)pnErr->u.i, &nErr);
 83636         -  pnErr->u.i -= nErr;
        83891  +                                 (int)pnErr->u.i+1, &nErr);
 83637  83892     sqlite3VdbeMemSetNull(pIn1);
 83638  83893     if( nErr==0 ){
 83639  83894       assert( z==0 );
 83640  83895     }else if( z==0 ){
 83641  83896       goto no_mem;
 83642  83897     }else{
        83898  +    pnErr->u.i -= nErr-1;
 83643  83899       sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
 83644  83900     }
 83645  83901     UPDATE_MAX_BLOBSIZE(pIn1);
 83646  83902     sqlite3VdbeChangeEncoding(pIn1, encoding);
 83647  83903     break;
 83648  83904   }
 83649  83905   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
................................................................................
 83814  84070       ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
 83815  84071       */
 83816  84072       nMem = pProgram->nMem + pProgram->nCsr;
 83817  84073       assert( nMem>0 );
 83818  84074       if( pProgram->nCsr==0 ) nMem++;
 83819  84075       nByte = ROUND8(sizeof(VdbeFrame))
 83820  84076                 + nMem * sizeof(Mem)
 83821         -              + pProgram->nCsr * sizeof(VdbeCursor *);
        84077  +              + pProgram->nCsr * sizeof(VdbeCursor*)
        84078  +              + (pProgram->nOp + 7)/8;
 83822  84079       pFrame = sqlite3DbMallocZero(db, nByte);
 83823  84080       if( !pFrame ){
 83824  84081         goto no_mem;
 83825  84082       }
 83826  84083       sqlite3VdbeMemRelease(pRt);
 83827  84084       pRt->flags = MEM_Frame;
 83828  84085       pRt->u.pFrame = pFrame;
................................................................................
 83865  84122     p->pAuxData = 0;
 83866  84123     p->nChange = 0;
 83867  84124     p->pFrame = pFrame;
 83868  84125     p->aMem = aMem = VdbeFrameMem(pFrame);
 83869  84126     p->nMem = pFrame->nChildMem;
 83870  84127     p->nCursor = (u16)pFrame->nChildCsr;
 83871  84128     p->apCsr = (VdbeCursor **)&aMem[p->nMem];
        84129  +  pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
        84130  +  memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
 83872  84131     p->aOp = aOp = pProgram->aOp;
 83873  84132     p->nOp = pProgram->nOp;
 83874  84133   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 83875  84134     p->anExec = 0;
 83876  84135   #endif
 83877  84136     pOp = &aOp[-1];
 83878  84137   
................................................................................
 84894  85153       if( db->mTrace & SQLITE_TRACE_LEGACY ){
 84895  85154         void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
 84896  85155         char *z = sqlite3VdbeExpandSql(p, zTrace);
 84897  85156         x(db->pTraceArg, z);
 84898  85157         sqlite3_free(z);
 84899  85158       }else
 84900  85159   #endif
 84901         -    {
        85160  +    if( db->nVdbeExec>1 ){
        85161  +      char *z = sqlite3MPrintf(db, "-- %s", zTrace);
        85162  +      (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
        85163  +      sqlite3DbFree(db, z);
        85164  +    }else{
 84902  85165         (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
 84903  85166       }
 84904  85167     }
 84905  85168   #ifdef SQLITE_USE_FCNTL_TRACE
 84906  85169     zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
 84907  85170     if( zTrace ){
 84908  85171       int j;
................................................................................
 88782  89045   ** is invoked before visiting children.)
 88783  89046   **
 88784  89047   ** The return value from the callback should be one of the WRC_*
 88785  89048   ** constants to specify how to proceed with the walk.
 88786  89049   **
 88787  89050   **    WRC_Continue      Continue descending down the tree.
 88788  89051   **
 88789         -**    WRC_Prune         Do not descend into child nodes.  But allow
        89052  +**    WRC_Prune         Do not descend into child nodes, but allow
 88790  89053   **                      the walk to continue with sibling nodes.
 88791  89054   **
 88792  89055   **    WRC_Abort         Do no more callbacks.  Unwind the stack and
 88793         -**                      return the top-level walk call.
        89056  +**                      return from the top-level walk call.
 88794  89057   **
 88795  89058   ** The return value from this routine is WRC_Abort to abandon the tree walk
 88796  89059   ** and WRC_Continue to continue.
 88797  89060   */
 88798  89061   static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
 88799  89062     int rc;
 88800  89063     testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
................................................................................
 89148  89411             break;
 89149  89412           }
 89150  89413         }
 89151  89414       }
 89152  89415     }
 89153  89416   
 89154  89417     /* Start at the inner-most context and move outward until a match is found */
 89155         -  while( pNC && cnt==0 ){
        89418  +  assert( pNC && cnt==0 );
        89419  +  do{
 89156  89420       ExprList *pEList;
 89157  89421       SrcList *pSrcList = pNC->pSrcList;
 89158  89422   
 89159  89423       if( pSrcList ){
 89160  89424         for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
 89161  89425           pTab = pItem->pTab;
 89162  89426           assert( pTab!=0 && pTab->zName!=0 );
................................................................................
 89333  89597           }
 89334  89598         } 
 89335  89599       }
 89336  89600   
 89337  89601       /* Advance to the next name context.  The loop will exit when either
 89338  89602       ** we have a match (cnt>0) or when we run out of name contexts.
 89339  89603       */
 89340         -    if( cnt==0 ){
 89341         -      pNC = pNC->pNext;
 89342         -      nSubquery++;
 89343         -    }
 89344         -  }
        89604  +    if( cnt ) break;
        89605  +    pNC = pNC->pNext;
        89606  +    nSubquery++;
        89607  +  }while( pNC );
        89608  +
 89345  89609   
 89346  89610     /*
 89347  89611     ** If X and Y are NULL (in other words if only the column name Z is
 89348  89612     ** supplied) and the value of Z is enclosed in double-quotes, then
 89349  89613     ** Z is a string literal if it doesn't match any column names.  In that
 89350  89614     ** case, we need to return right away and not make any changes to
 89351  89615     ** pExpr.
................................................................................
 89527  89791         pExpr->iColumn = -1;
 89528  89792         pExpr->affinity = SQLITE_AFF_INTEGER;
 89529  89793         break;
 89530  89794       }
 89531  89795   #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
 89532  89796             && !defined(SQLITE_OMIT_SUBQUERY) */
 89533  89797   
 89534         -    /* A lone identifier is the name of a column.
 89535         -    */
 89536         -    case TK_ID: {
 89537         -      return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
 89538         -    }
 89539         -  
 89540         -    /* A table name and column name:     ID.ID
        89798  +    /* A column name:                    ID
        89799  +    ** Or table name and column name:    ID.ID
 89541  89800       ** Or a database, table and column:  ID.ID.ID
        89801  +    **
        89802  +    ** The TK_ID and TK_OUT cases are combined so that there will only
        89803  +    ** be one call to lookupName().  Then the compiler will in-line 
        89804  +    ** lookupName() for a size reduction and performance increase.
 89542  89805       */
        89806  +    case TK_ID:
 89543  89807       case TK_DOT: {
 89544  89808         const char *zColumn;
 89545  89809         const char *zTable;
 89546  89810         const char *zDb;
 89547  89811         Expr *pRight;
 89548  89812   
 89549         -      /* if( pSrcList==0 ) break; */
 89550         -      notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
 89551         -      pRight = pExpr->pRight;
 89552         -      if( pRight->op==TK_ID ){
        89813  +      if( pExpr->op==TK_ID ){
 89553  89814           zDb = 0;
 89554         -        zTable = pExpr->pLeft->u.zToken;
 89555         -        zColumn = pRight->u.zToken;
        89815  +        zTable = 0;
        89816  +        zColumn = pExpr->u.zToken;
 89556  89817         }else{
 89557         -        assert( pRight->op==TK_DOT );
 89558         -        zDb = pExpr->pLeft->u.zToken;
 89559         -        zTable = pRight->pLeft->u.zToken;
 89560         -        zColumn = pRight->pRight->u.zToken;
        89818  +        notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
        89819  +        pRight = pExpr->pRight;
        89820  +        if( pRight->op==TK_ID ){
        89821  +          zDb = 0;
        89822  +          zTable = pExpr->pLeft->u.zToken;
        89823  +          zColumn = pRight->u.zToken;
        89824  +        }else{
        89825  +          assert( pRight->op==TK_DOT );
        89826  +          zDb = pExpr->pLeft->u.zToken;
        89827  +          zTable = pRight->pLeft->u.zToken;
        89828  +          zColumn = pRight->pRight->u.zToken;
        89829  +        }
 89561  89830         }
 89562  89831         return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
 89563  89832       }
 89564  89833   
 89565  89834       /* Resolve function names
 89566  89835       */
 89567  89836       case TK_FUNCTION: {
................................................................................
 90692  90961             pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
 90693  90962     assert( pExpr->pLeft );
 90694  90963     aff = sqlite3ExprAffinity(pExpr->pLeft);
 90695  90964     if( pExpr->pRight ){
 90696  90965       aff = sqlite3CompareAffinity(pExpr->pRight, aff);
 90697  90966     }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 90698  90967       aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
 90699         -  }else if( NEVER(aff==0) ){
        90968  +  }else if( aff==0 ){
 90700  90969       aff = SQLITE_AFF_BLOB;
 90701  90970     }
 90702  90971     return aff;
 90703  90972   }
 90704  90973   
 90705  90974   /*
 90706  90975   ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
................................................................................
 92321  92590   ** If the expression p codes a constant integer that is small enough
 92322  92591   ** to fit in a 32-bit integer, return 1 and put the value of the integer
 92323  92592   ** in *pValue.  If the expression is not an integer or if it is too big
 92324  92593   ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
 92325  92594   */
 92326  92595   SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
 92327  92596     int rc = 0;
        92597  +  if( p==0 ) return 0;  /* Can only happen following on OOM */
 92328  92598   
 92329  92599     /* If an expression is an integer literal that fits in a signed 32-bit
 92330  92600     ** integer, then the EP_IntValue flag will have already been set */
 92331  92601     assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
 92332  92602              || sqlite3GetInt32(p->u.zToken, &rc)==0 );
 92333  92603   
 92334  92604     if( p->flags & EP_IntValue ){
................................................................................
 92828  93098   static char *exprINAffinity(Parse *pParse, Expr *pExpr){
 92829  93099     Expr *pLeft = pExpr->pLeft;
 92830  93100     int nVal = sqlite3ExprVectorSize(pLeft);
 92831  93101     Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
 92832  93102     char *zRet;
 92833  93103   
 92834  93104     assert( pExpr->op==TK_IN );
 92835         -  zRet = sqlite3DbMallocZero(pParse->db, nVal+1);
        93105  +  zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
 92836  93106     if( zRet ){
 92837  93107       int i;
 92838  93108       for(i=0; i<nVal; i++){
 92839  93109         Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
 92840  93110         char a = sqlite3ExprAffinity(pA);
 92841  93111         if( pSelect ){
 92842  93112           zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
................................................................................
 92993  93263           /* If the LHS and RHS of the IN operator do not match, that
 92994  93264           ** error will have been caught long before we reach this point. */
 92995  93265           if( ALWAYS(pEList->nExpr==nVal) ){
 92996  93266             SelectDest dest;
 92997  93267             int i;
 92998  93268             sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
 92999  93269             dest.zAffSdst = exprINAffinity(pParse, pExpr);
 93000         -          assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
 93001  93270             pSelect->iLimit = 0;
 93002  93271             testcase( pSelect->selFlags & SF_Distinct );
 93003  93272             testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
 93004  93273             if( sqlite3Select(pParse, pSelect, &dest) ){
 93005  93274               sqlite3DbFree(pParse->db, dest.zAffSdst);
 93006  93275               sqlite3KeyInfoUnref(pKeyInfo);
 93007  93276               return 0;
................................................................................
 93733  94002   
 93734  94003   /*
 93735  94004   ** Clear all column cache entries.
 93736  94005   */
 93737  94006   SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
 93738  94007     int i;
 93739  94008   
 93740         -#if SQLITE_DEBUG
        94009  +#ifdef SQLITE_DEBUG
 93741  94010     if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
 93742  94011       printf("CLEAR\n");
 93743  94012     }
 93744  94013   #endif
 93745  94014     for(i=0; i<pParse->nColCache; i++){
 93746  94015       if( pParse->aColCache[i].tempReg
 93747  94016        && pParse->nTempReg<ArraySize(pParse->aTempReg)
................................................................................
 95140  95409       Expr *pExprA = pA->a[i].pExpr;
 95141  95410       Expr *pExprB = pB->a[i].pExpr;
 95142  95411       if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
 95143  95412       if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
 95144  95413     }
 95145  95414     return 0;
 95146  95415   }
        95416  +
        95417  +/*
        95418  +** Like sqlite3ExprCompare() except COLLATE operators at the top-level
        95419  +** are ignored.
        95420  +*/
        95421  +SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
        95422  +  return sqlite3ExprCompare(
        95423  +             sqlite3ExprSkipCollate(pA),
        95424  +             sqlite3ExprSkipCollate(pB),
        95425  +             iTab);
        95426  +}
 95147  95427   
 95148  95428   /*
 95149  95429   ** Return true if we can prove the pE2 will always be true if pE1 is
 95150  95430   ** true.  Return false if we cannot complete the proof or if pE2 might
 95151  95431   ** be false.  Examples:
 95152  95432   **
 95153  95433   **     pE1: x==5       pE2: x==5             Result: true
................................................................................
 96693  96973     int nKeyCol;              /* Number of index columns w/o the pk/rowid */
 96694  96974     int mxSample;             /* Maximum number of samples to accumulate */
 96695  96975     Stat4Sample current;      /* Current row as a Stat4Sample */
 96696  96976     u32 iPrn;                 /* Pseudo-random number used for sampling */
 96697  96977     Stat4Sample *aBest;       /* Array of nCol best samples */
 96698  96978     int iMin;                 /* Index in a[] of entry with minimum score */
 96699  96979     int nSample;              /* Current number of samples */
        96980  +  int nMaxEqZero;           /* Max leading 0 in anEq[] for any a[] entry */
 96700  96981     int iGet;                 /* Index of current sample accessed by stat_get() */
 96701  96982     Stat4Sample *a;           /* Array of mxSample Stat4Sample objects */
 96702  96983     sqlite3 *db;              /* Database connection, for malloc() */
 96703  96984   };
 96704  96985   
 96705  96986   /* Reclaim memory used by a Stat4Sample
 96706  96987   */
................................................................................
 96957  97238   static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
 96958  97239     Stat4Sample *pSample = 0;
 96959  97240     int i;
 96960  97241   
 96961  97242     assert( IsStat4 || nEqZero==0 );
 96962  97243   
 96963  97244   #ifdef SQLITE_ENABLE_STAT4
        97245  +  /* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
        97246  +  ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
        97247  +  ** other words, if nMaxEqZero is n, then it is guaranteed that there
        97248  +  ** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
        97249  +  if( nEqZero>p->nMaxEqZero ){
        97250  +    p->nMaxEqZero = nEqZero;
        97251  +  }
 96964  97252     if( pNew->isPSample==0 ){
 96965  97253       Stat4Sample *pUpgrade = 0;
 96966  97254       assert( pNew->anEq[pNew->iCol]>0 );
 96967  97255   
 96968  97256       /* This sample is being added because the prefix that ends in column 
 96969  97257       ** iCol occurs many times in the table. However, if we have already
 96970  97258       ** added a sample that shares this prefix, there is no need to add
................................................................................
 97054  97342       Stat4Sample *pBest = &p->aBest[i];
 97055  97343       pBest->anEq[i] = p->current.anEq[i];
 97056  97344       if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
 97057  97345         sampleInsert(p, pBest, i);
 97058  97346       }
 97059  97347     }
 97060  97348   
 97061         -  /* Update the anEq[] fields of any samples already collected. */
        97349  +  /* Check that no sample contains an anEq[] entry with an index of
        97350  +  ** p->nMaxEqZero or greater set to zero. */
 97062  97351     for(i=p->nSample-1; i>=0; i--){
 97063  97352       int j;
 97064         -    for(j=iChng; j<p->nCol; j++){
 97065         -      if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
        97353  +    for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
        97354  +  }
        97355  +
        97356  +  /* Update the anEq[] fields of any samples already collected. */
        97357  +  if( iChng<p->nMaxEqZero ){
        97358  +    for(i=p->nSample-1; i>=0; i--){
        97359  +      int j;
        97360  +      for(j=iChng; j<p->nCol; j++){
        97361  +        if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
        97362  +      }
 97066  97363       }
        97364  +    p->nMaxEqZero = iChng;
 97067  97365     }
 97068  97366   #endif
 97069  97367   
 97070  97368   #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
 97071  97369     if( iChng==0 ){
 97072  97370       tRowcnt nLt = p->current.anLt[0];
 97073  97371       tRowcnt nEq = p->current.anEq[0];
................................................................................
 97589  97887         sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
 97590  97888       }else{
 97591  97889         Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
 97592  97890         int j, k, regKey;
 97593  97891         regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
 97594  97892         for(j=0; j<pPk->nKeyCol; j++){
 97595  97893           k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
 97596         -        assert( k>=0 && k<pTab->nCol );
        97894  +        assert( k>=0 && k<pIdx->nColumn );
 97597  97895           sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
 97598  97896           VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
 97599  97897         }
 97600  97898         sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
 97601  97899         sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
 97602  97900       }
 97603  97901   #endif
................................................................................
 97773  98071     assert( pName2!=0 || pName1==0 );
 97774  98072     if( pName1==0 ){
 97775  98073       /* Form 1:  Analyze everything */
 97776  98074       for(i=0; i<db->nDb; i++){
 97777  98075         if( i==1 ) continue;  /* Do not analyze the TEMP database */
 97778  98076         analyzeDatabase(pParse, i);
 97779  98077       }
 97780         -  }else if( pName2->n==0 ){
 97781         -    /* Form 2:  Analyze the database or table named */
 97782         -    iDb = sqlite3FindDb(db, pName1);
 97783         -    if( iDb>=0 ){
 97784         -      analyzeDatabase(pParse, iDb);
 97785         -    }else{
 97786         -      z = sqlite3NameFromToken(db, pName1);
 97787         -      if( z ){
 97788         -        if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
 97789         -          analyzeTable(pParse, pIdx->pTable, pIdx);
 97790         -        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
 97791         -          analyzeTable(pParse, pTab, 0);
 97792         -        }
 97793         -        sqlite3DbFree(db, z);
 97794         -      }
 97795         -    }
        98078  +  }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
        98079  +    /* Analyze the schema named as the argument */
        98080  +    analyzeDatabase(pParse, iDb);
 97796  98081     }else{
 97797         -    /* Form 3: Analyze the fully qualified table name */
        98082  +    /* Form 3: Analyze the table or index named as an argument */
 97798  98083       iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
 97799  98084       if( iDb>=0 ){
 97800         -      zDb = db->aDb[iDb].zDbSName;
        98085  +      zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
 97801  98086         z = sqlite3NameFromToken(db, pTableName);
 97802  98087         if( z ){
 97803  98088           if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
 97804  98089             analyzeTable(pParse, pIdx->pTable, pIdx);
 97805  98090           }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
 97806  98091             analyzeTable(pParse, pTab, 0);
 97807  98092           }
 97808  98093           sqlite3DbFree(db, z);
 97809  98094         }
 97810         -    }   
        98095  +    }
 97811  98096     }
 97812         -  v = sqlite3GetVdbe(pParse);
 97813         -  if( v ) sqlite3VdbeAddOp0(v, OP_Expire);
        98097  +  if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
        98098  +    sqlite3VdbeAddOp0(v, OP_Expire);
        98099  +  }
 97814  98100   }
 97815  98101   
 97816  98102   /*
 97817  98103   ** Used to pass information from the analyzer reader through to the
 97818  98104   ** callback routine.
 97819  98105   */
 97820  98106   typedef struct analysisInfo analysisInfo;
................................................................................
 97935  98221         pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
 97936  98222         if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
 97937  98223       }
 97938  98224       aiRowEst = pIndex->aiRowEst;
 97939  98225   #endif
 97940  98226       pIndex->bUnordered = 0;
 97941  98227       decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
 97942         -    if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
        98228  +    pIndex->hasStat1 = 1;
        98229  +    if( pIndex->pPartIdxWhere==0 ){
        98230  +      pTable->nRowLogEst = pIndex->aiRowLogEst[0];
        98231  +      pTable->tabFlags |= TF_HasStat1;
        98232  +    }
 97943  98233     }else{
 97944  98234       Index fakeIdx;
 97945  98235       fakeIdx.szIdxRow = pTable->szTabRow;
 97946  98236   #ifdef SQLITE_ENABLE_COSTMULT
 97947  98237       fakeIdx.pTable = pTable;
 97948  98238   #endif
 97949  98239       decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
 97950  98240       pTable->szTabRow = fakeIdx.szIdxRow;
        98241  +    pTable->tabFlags |= TF_HasStat1;
 97951  98242     }
 97952  98243   
 97953  98244     return 0;
 97954  98245   }
 97955  98246   
 97956  98247   /*
 97957  98248   ** If the Index.aSample variable is not NULL, delete the aSample[] array
................................................................................
 98238  98529   ** code may be ignored.
 98239  98530   */
 98240  98531   SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
 98241  98532     analysisInfo sInfo;
 98242  98533     HashElem *i;
 98243  98534     char *zSql;
 98244  98535     int rc = SQLITE_OK;
        98536  +  Schema *pSchema = db->aDb[iDb].pSchema;
 98245  98537   
 98246  98538     assert( iDb>=0 && iDb<db->nDb );
 98247  98539     assert( db->aDb[iDb].pBt!=0 );
 98248  98540   
 98249  98541     /* Clear any prior statistics */
 98250  98542     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 98251         -  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
        98543  +  for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
        98544  +    Table *pTab = sqliteHashData(i);
        98545  +    pTab->tabFlags &= ~TF_HasStat1;
        98546  +  }
        98547  +  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
 98252  98548       Index *pIdx = sqliteHashData(i);
 98253         -    pIdx->aiRowLogEst[0] = 0;
        98549  +    pIdx->hasStat1 = 0;
 98254  98550   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 98255  98551       sqlite3DeleteIndexSamples(db, pIdx);
 98256  98552       pIdx->aSample = 0;
 98257  98553   #endif
 98258  98554     }
 98259  98555   
 98260  98556     /* Load new statistics out of the sqlite_stat1 table */
................................................................................
 98269  98565         rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
 98270  98566         sqlite3DbFree(db, zSql);
 98271  98567       }
 98272  98568     }
 98273  98569   
 98274  98570     /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
 98275  98571     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 98276         -  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
        98572  +  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
 98277  98573       Index *pIdx = sqliteHashData(i);
 98278         -    if( pIdx->aiRowLogEst[0]==0 ) sqlite3DefaultRowEst(pIdx);
        98574  +    if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
 98279  98575     }
 98280  98576   
 98281  98577     /* Load the statistics from the sqlite_stat4 table. */
 98282  98578   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 98283  98579     if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
 98284  98580       db->lookaside.bDisable++;
 98285  98581       rc = loadStat4(db, sInfo.zDatabase);
 98286  98582       db->lookaside.bDisable--;
 98287  98583     }
 98288         -  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
        98584  +  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
 98289  98585       Index *pIdx = sqliteHashData(i);
 98290  98586       sqlite3_free(pIdx->aiRowEst);
 98291  98587       pIdx->aiRowEst = 0;
 98292  98588     }
 98293  98589   #endif
 98294  98590   
 98295  98591     if( rc==SQLITE_NOMEM ){
................................................................................
100266 100562   ** the column currently under construction.
100267 100563   */
100268 100564   SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
100269 100565     Table *p;
100270 100566     p = pParse->pNewTable;
100271 100567     if( p==0 || NEVER(p->nCol<1) ) return;
100272 100568     p->aCol[p->nCol-1].notNull = (u8)onError;
       100569  +  p->tabFlags |= TF_HasNotNull;
100273 100570   }
100274 100571   
100275 100572   /*
100276 100573   ** Scan the column type name zType (length nType) and return the
100277 100574   ** associated affinity type.
100278 100575   **
100279 100576   ** This routine does a case-independent search of zType for the 
................................................................................
102604 102901   */
102605 102902   SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
102606 102903     /*                10,  9,  8,  7,  6 */
102607 102904     LogEst aVal[] = { 33, 32, 30, 28, 26 };
102608 102905     LogEst *a = pIdx->aiRowLogEst;
102609 102906     int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
102610 102907     int i;
       102908  +
       102909  +  /* Indexes with default row estimates should not have stat1 data */
       102910  +  assert( !pIdx->hasStat1 );
102611 102911   
102612 102912     /* Set the first entry (number of rows in the index) to the estimated 
102613 102913     ** number of rows in the table, or half the number of rows in the table
102614 102914     ** for a partial index.   But do not let the estimate drop below 10. */
102615 102915     a[0] = pIdx->pTable->nRowLogEst;
102616 102916     if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10;  assert( 10==sqlite3LogEst(2) );
102617 102917     if( a[0]<33 ) a[0] = 33;                  assert( 33==sqlite3LogEst(10) );
................................................................................
105188 105488     if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
105189 105489     nHaystack = sqlite3_value_bytes(argv[0]);
105190 105490     nNeedle = sqlite3_value_bytes(argv[1]);
105191 105491     if( nNeedle>0 ){
105192 105492       if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
105193 105493         zHaystack = sqlite3_value_blob(argv[0]);
105194 105494         zNeedle = sqlite3_value_blob(argv[1]);
105195         -      assert( zNeedle!=0 );
105196         -      assert( zHaystack!=0 || nHaystack==0 );
105197 105495         isText = 0;
105198 105496       }else{
105199 105497         zHaystack = sqlite3_value_text(argv[0]);
105200 105498         zNeedle = sqlite3_value_text(argv[1]);
105201 105499         isText = 1;
105202         -      if( zHaystack==0 || zNeedle==0 ) return;
105203 105500       }
       105501  +    if( zNeedle==0 || (nHaystack && zHaystack==0) ) return;
105204 105502       while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
105205 105503         N++;
105206 105504         do{
105207 105505           nHaystack--;
105208 105506           zHaystack++;
105209 105507         }while( isText && (zHaystack[0]&0xc0)==0x80 );
105210 105508       }
................................................................................
109793 110091           }
109794 110092           sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
109795 110093           VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
109796 110094         }
109797 110095       }
109798 110096       sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
109799 110097       VdbeComment((v, "for %s", pIdx->zName));
       110098  +#ifdef SQLITE_ENABLE_NULL_TRIM
       110099  +    if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
       110100  +#endif
109800 110101   
109801 110102       /* In an UPDATE operation, if this index is the PRIMARY KEY index 
109802 110103       ** of a WITHOUT ROWID table and there has been no change the
109803 110104       ** primary key, then no collision is possible.  The collision detection
109804 110105       ** logic below can all be skipped. */
109805 110106       if( isUpdate && pPk==pIdx && pkChng==0 ){
109806 110107         sqlite3VdbeResolveLabel(v, addrUniqueOk);
................................................................................
109948 110249   SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
109949 110250     u16 i;
109950 110251   
109951 110252     /* Records with omitted columns are only allowed for schema format
109952 110253     ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
109953 110254     if( pTab->pSchema->file_format<2 ) return;
109954 110255   
109955         -  for(i=pTab->nCol; i>1 && pTab->aCol[i-1].pDflt==0; i--){}
109956         -  sqlite3VdbeChangeP5(v, i);
       110256  +  for(i=pTab->nCol-1; i>0; i--){
       110257  +    if( pTab->aCol[i].pDflt!=0 ) break;
       110258  +    if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
       110259  +  }
       110260  +  sqlite3VdbeChangeP5(v, i+1);
109957 110261   }
109958 110262   #endif
109959 110263   
109960 110264   /*
109961 110265   ** This routine generates code to finish the INSERT or UPDATE operation
109962 110266   ** that was started by a prior call to sqlite3GenerateConstraintChecks.
109963 110267   ** A consecutive range of registers starting at regNewData contains the
................................................................................
110239 110543       ** error if pSelect reads from a CTE named "xxx".  */
110240 110544       return 0;
110241 110545     }
110242 110546     if( sqlite3TriggerList(pParse, pDest) ){
110243 110547       return 0;   /* tab1 must not have triggers */
110244 110548     }
110245 110549   #ifndef SQLITE_OMIT_VIRTUALTABLE
110246         -  if( pDest->tabFlags & TF_Virtual ){
       110550  +  if( IsVirtual(pDest) ){
110247 110551       return 0;   /* tab1 must not be a virtual table */
110248 110552     }
110249 110553   #endif
110250 110554     if( onError==OE_Default ){
110251 110555       if( pDest->iPKey>=0 ) onError = pDest->keyConf;
110252 110556       if( onError==OE_Default ) onError = OE_Abort;
110253 110557     }
................................................................................
110301 110605     if( pSrc==pDest ){
110302 110606       return 0;   /* tab1 and tab2 may not be the same table */
110303 110607     }
110304 110608     if( HasRowid(pDest)!=HasRowid(pSrc) ){
110305 110609       return 0;   /* source and destination must both be WITHOUT ROWID or not */
110306 110610     }
110307 110611   #ifndef SQLITE_OMIT_VIRTUALTABLE
110308         -  if( pSrc->tabFlags & TF_Virtual ){
       110612  +  if( IsVirtual(pSrc) ){
110309 110613       return 0;   /* tab2 must not be a virtual table */
110310 110614     }
110311 110615   #endif
110312 110616     if( pSrc->pSelect ){
110313 110617       return 0;   /* tab2 may not be a view */
110314 110618     }
110315 110619     if( pDest->nCol!=pSrc->nCol ){
................................................................................
110487 110791         ** If any of the indexed columns use a collation sequence other than
110488 110792         ** BINARY, this optimization is disabled. This is because the user 
110489 110793         ** might change the definition of a collation sequence and then run
110490 110794         ** a VACUUM command. In that case keys may not be written in strictly
110491 110795         ** sorted order.  */
110492 110796         for(i=0; i<pSrcIdx->nColumn; i++){
110493 110797           const char *zColl = pSrcIdx->azColl[i];
110494         -        assert( sqlite3_stricmp(sqlite3StrBINARY, zColl)!=0
110495         -                    || sqlite3StrBINARY==zColl );
110496 110798           if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
110497 110799         }
110498 110800         if( i==pSrcIdx->nColumn ){
110499 110801           idxInsFlags = OPFLAG_USESEEKRESULT;
110500 110802           sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
110501 110803         }
110502 110804       }
................................................................................
110598 110900         rc = sqlite3_step(pStmt);
110599 110901   
110600 110902         /* Invoke the callback function if required */
110601 110903         if( xCallback && (SQLITE_ROW==rc || 
110602 110904             (SQLITE_DONE==rc && !callbackIsInit
110603 110905                              && db->flags&SQLITE_NullCallback)) ){
110604 110906           if( !callbackIsInit ){
110605         -          azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
       110907  +          azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
110606 110908             if( azCols==0 ){
110607 110909               goto exec_out;
110608 110910             }
110609 110911             for(i=0; i<nCol; i++){
110610 110912               azCols[i] = (char *)sqlite3_column_name(pStmt, i);
110611 110913               /* sqlite3VdbeSetColName() installs column names as UTF8
110612 110914               ** strings so there is no way for sqlite3_column_name() to fail. */
................................................................................
110619 110921             for(i=0; i<nCol; i++){
110620 110922               azVals[i] = (char *)sqlite3_column_text(pStmt, i);
110621 110923               if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
110622 110924                 sqlite3OomFault(db);
110623 110925                 goto exec_out;
110624 110926               }
110625 110927             }
       110928  +          azVals[i] = 0;
110626 110929           }
110627 110930           if( xCallback(pArg, nCol, azVals, azCols) ){
110628 110931             /* EVIDENCE-OF: R-38229-40159 If the callback function to
110629 110932             ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
110630 110933             ** return SQLITE_ABORT. */
110631 110934             rc = SQLITE_ABORT;
110632 110935             sqlite3VdbeFinalize((Vdbe *)pStmt);
................................................................................
110974 111277     int (*strlike)(const char*,const char*,unsigned int);
110975 111278     int (*db_cacheflush)(sqlite3*);
110976 111279     /* Version 3.12.0 and later */
110977 111280     int (*system_errno)(sqlite3*);
110978 111281     /* Version 3.14.0 and later */
110979 111282     int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
110980 111283     char *(*expanded_sql)(sqlite3_stmt*);
       111284  +  /* Version 3.18.0 and later */
       111285  +  void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
110981 111286   };
110982 111287   
110983 111288   /*
110984 111289   ** This is the function signature used for all extension entry points.  It
110985 111290   ** is also defined in the file "loadext.c".
110986 111291   */
110987 111292   typedef int (*sqlite3_loadext_entry)(
................................................................................
111232 111537   #define sqlite3_strlike                sqlite3_api->strlike
111233 111538   #define sqlite3_db_cacheflush          sqlite3_api->db_cacheflush
111234 111539   /* Version 3.12.0 and later */
111235 111540   #define sqlite3_system_errno           sqlite3_api->system_errno
111236 111541   /* Version 3.14.0 and later */
111237 111542   #define sqlite3_trace_v2               sqlite3_api->trace_v2
111238 111543   #define sqlite3_expanded_sql           sqlite3_api->expanded_sql
       111544  +/* Version 3.18.0 and later */
       111545  +#define sqlite3_set_last_insert_rowid  sqlite3_api->set_last_insert_rowid
111239 111546   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
111240 111547   
111241 111548   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
111242 111549     /* This case when the file really is being compiled as a loadable 
111243 111550     ** extension */
111244 111551   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
111245 111552   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
111657 111964     sqlite3_status64,
111658 111965     sqlite3_strlike,
111659 111966     sqlite3_db_cacheflush,
111660 111967     /* Version 3.12.0 and later */
111661 111968     sqlite3_system_errno,
111662 111969     /* Version 3.14.0 and later */
111663 111970     sqlite3_trace_v2,
111664         -  sqlite3_expanded_sql
       111971  +  sqlite3_expanded_sql,
       111972  +  /* Version 3.18.0 and later */
       111973  +  sqlite3_set_last_insert_rowid
111665 111974   };
111666 111975   
111667 111976   /*
111668 111977   ** Attempt to load an SQLite extension library contained in the file
111669 111978   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
111670 111979   ** default entry point name (sqlite3_extension_init) is used.  Use
111671 111980   ** of the default name is recommended.
................................................................................
112089 112398   #define PragTyp_INTEGRITY_CHECK               18
112090 112399   #define PragTyp_JOURNAL_MODE                  19
112091 112400   #define PragTyp_JOURNAL_SIZE_LIMIT            20
112092 112401   #define PragTyp_LOCK_PROXY_FILE               21
112093 112402   #define PragTyp_LOCKING_MODE                  22
112094 112403   #define PragTyp_PAGE_COUNT                    23
112095 112404   #define PragTyp_MMAP_SIZE                     24
112096         -#define PragTyp_PAGE_SIZE                     25
112097         -#define PragTyp_SECURE_DELETE                 26
112098         -#define PragTyp_SHRINK_MEMORY                 27
112099         -#define PragTyp_SOFT_HEAP_LIMIT               28
112100         -#define PragTyp_STATS                         29
       112405  +#define PragTyp_OPTIMIZE                      25
       112406  +#define PragTyp_PAGE_SIZE                     26
       112407  +#define PragTyp_SECURE_DELETE                 27
       112408  +#define PragTyp_SHRINK_MEMORY                 28
       112409  +#define PragTyp_SOFT_HEAP_LIMIT               29
112101 112410   #define PragTyp_SYNCHRONOUS                   30
112102 112411   #define PragTyp_TABLE_INFO                    31
112103 112412   #define PragTyp_TEMP_STORE                    32
112104 112413   #define PragTyp_TEMP_STORE_DIRECTORY          33
112105 112414   #define PragTyp_THREADS                       34
112106 112415   #define PragTyp_WAL_AUTOCHECKPOINT            35
112107 112416   #define PragTyp_WAL_CHECKPOINT                36
112108 112417   #define PragTyp_ACTIVATE_EXTENSIONS           37
112109 112418   #define PragTyp_HEXKEY                        38
112110 112419   #define PragTyp_KEY                           39
112111 112420   #define PragTyp_REKEY                         40
112112 112421   #define PragTyp_LOCK_STATUS                   41
112113 112422   #define PragTyp_PARSER_TRACE                  42
       112423  +#define PragTyp_STATS                         43
112114 112424   
112115 112425   /* Property flags associated with various pragma. */
112116 112426   #define PragFlg_NeedSchema 0x01 /* Force schema load before running */
112117 112427   #define PragFlg_NoColumns  0x02 /* OP_ResultRow called with zero columns */
112118 112428   #define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
112119 112429   #define PragFlg_ReadOnly   0x08 /* Read-only HEADER_VALUE */
112120 112430   #define PragFlg_Result0    0x10 /* Acts as query when no argument */
................................................................................
112130 112440     /*   0 */ "cache_size",  /* Used by: default_cache_size */
112131 112441     /*   1 */ "cid",         /* Used by: table_info */
112132 112442     /*   2 */ "name",       
112133 112443     /*   3 */ "type",       
112134 112444     /*   4 */ "notnull",    
112135 112445     /*   5 */ "dflt_value", 
112136 112446     /*   6 */ "pk",         
112137         -  /*   7 */ "table",       /* Used by: stats */
112138         -  /*   8 */ "index",      
112139         -  /*   9 */ "width",      
112140         -  /*  10 */ "height",     
112141         -  /*  11 */ "seqno",       /* Used by: index_info */
112142         -  /*  12 */ "cid",        
112143         -  /*  13 */ "name",       
112144         -  /*  14 */ "seqno",       /* Used by: index_xinfo */
112145         -  /*  15 */ "cid",        
112146         -  /*  16 */ "name",       
112147         -  /*  17 */ "desc",       
112148         -  /*  18 */ "coll",       
112149         -  /*  19 */ "key",        
112150         -  /*  20 */ "seq",         /* Used by: index_list */
112151         -  /*  21 */ "name",       
112152         -  /*  22 */ "unique",     
112153         -  /*  23 */ "origin",     
112154         -  /*  24 */ "partial",    
112155         -  /*  25 */ "seq",         /* Used by: database_list */
112156         -  /*  26 */ "name",       
112157         -  /*  27 */ "file",       
112158         -  /*  28 */ "seq",         /* Used by: collation_list */
112159         -  /*  29 */ "name",       
112160         -  /*  30 */ "id",          /* Used by: foreign_key_list */
112161         -  /*  31 */ "seq",        
112162         -  /*  32 */ "table",      
112163         -  /*  33 */ "from",       
112164         -  /*  34 */ "to",         
112165         -  /*  35 */ "on_update",  
112166         -  /*  36 */ "on_delete",  
112167         -  /*  37 */ "match",      
112168         -  /*  38 */ "table",       /* Used by: foreign_key_check */
112169         -  /*  39 */ "rowid",      
112170         -  /*  40 */ "parent",     
112171         -  /*  41 */ "fkid",       
112172         -  /*  42 */ "busy",        /* Used by: wal_checkpoint */
112173         -  /*  43 */ "log",        
112174         -  /*  44 */ "checkpointed",
112175         -  /*  45 */ "timeout",     /* Used by: busy_timeout */
112176         -  /*  46 */ "database",    /* Used by: lock_status */
112177         -  /*  47 */ "status",     
       112447  +  /*   7 */ "tbl",         /* Used by: stats */
       112448  +  /*   8 */ "idx",        
       112449  +  /*   9 */ "wdth",       
       112450  +  /*  10 */ "hght",       
       112451  +  /*  11 */ "flgs",       
       112452  +  /*  12 */ "seqno",       /* Used by: index_info */
       112453  +  /*  13 */ "cid",        
       112454  +  /*  14 */ "name",       
       112455  +  /*  15 */ "seqno",       /* Used by: index_xinfo */
       112456  +  /*  16 */ "cid",        
       112457  +  /*  17 */ "name",       
       112458  +  /*  18 */ "desc",       
       112459  +  /*  19 */ "coll",       
       112460  +  /*  20 */ "key",        
       112461  +  /*  21 */ "seq",         /* Used by: index_list */
       112462  +  /*  22 */ "name",       
       112463  +  /*  23 */ "unique",     
       112464  +  /*  24 */ "origin",     
       112465  +  /*  25 */ "partial",    
       112466  +  /*  26 */ "seq",         /* Used by: database_list */
       112467  +  /*  27 */ "name",       
       112468  +  /*  28 */ "file",       
       112469  +  /*  29 */ "seq",         /* Used by: collation_list */
       112470  +  /*  30 */ "name",       
       112471  +  /*  31 */ "id",          /* Used by: foreign_key_list */
       112472  +  /*  32 */ "seq",        
       112473  +  /*  33 */ "table",      
       112474  +  /*  34 */ "from",       
       112475  +  /*  35 */ "to",         
       112476  +  /*  36 */ "on_update",  
       112477  +  /*  37 */ "on_delete",  
       112478  +  /*  38 */ "match",      
       112479  +  /*  39 */ "table",       /* Used by: foreign_key_check */
       112480  +  /*  40 */ "rowid",      
       112481  +  /*  41 */ "parent",     
       112482  +  /*  42 */ "fkid",       
       112483  +  /*  43 */ "busy",        /* Used by: wal_checkpoint */
       112484  +  /*  44 */ "log",        
       112485  +  /*  45 */ "checkpointed",
       112486  +  /*  46 */ "timeout",     /* Used by: busy_timeout */
       112487  +  /*  47 */ "database",    /* Used by: lock_status */
       112488  +  /*  48 */ "status",     
112178 112489   };
112179 112490   
112180 112491   /* Definitions of all built-in pragmas */
112181 112492   typedef struct PragmaName {
112182 112493     const char *const zName; /* Name of pragma */
112183 112494     u8 ePragTyp;             /* PragTyp_XXX value */
112184 112495     u8 mPragFlg;             /* Zero or more PragFlg_XXX values */
................................................................................
112216 112527     /* ColNames:  */ 0, 0,
112217 112528     /* iArg:      */ SQLITE_AutoIndex },
112218 112529   #endif
112219 112530   #endif
112220 112531    {/* zName:     */ "busy_timeout",
112221 112532     /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
112222 112533     /* ePragFlg:  */ PragFlg_Result0,
112223         -  /* ColNames:  */ 45, 1,
       112534  +  /* ColNames:  */ 46, 1,
112224 112535     /* iArg:      */ 0 },
112225 112536   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112226 112537    {/* zName:     */ "cache_size",
112227 112538     /* ePragTyp:  */ PragTyp_CACHE_SIZE,
112228 112539     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
112229 112540     /* ColNames:  */ 0, 0,
112230 112541     /* iArg:      */ 0 },
................................................................................
112253 112564     /* ColNames:  */ 0, 0,
112254 112565     /* iArg:      */ SQLITE_CkptFullFSync },
112255 112566   #endif
112256 112567   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
112257 112568    {/* zName:     */ "collation_list",
112258 112569     /* ePragTyp:  */ PragTyp_COLLATION_LIST,
112259 112570     /* ePragFlg:  */ PragFlg_Result0,
112260         -  /* ColNames:  */ 28, 2,
       112571  +  /* ColNames:  */ 29, 2,
112261 112572     /* iArg:      */ 0 },
112262 112573   #endif
112263 112574   #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
112264 112575    {/* zName:     */ "compile_options",
112265 112576     /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
112266 112577     /* ePragFlg:  */ PragFlg_Result0,
112267 112578     /* ColNames:  */ 0, 0,
................................................................................
112288 112599     /* ColNames:  */ 0, 0,
112289 112600     /* iArg:      */ BTREE_DATA_VERSION },
112290 112601   #endif
112291 112602   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
112292 112603    {/* zName:     */ "database_list",
112293 112604     /* ePragTyp:  */ PragTyp_DATABASE_LIST,
112294 112605     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
112295         -  /* ColNames:  */ 25, 3,
       112606  +  /* ColNames:  */ 26, 3,
112296 112607     /* iArg:      */ 0 },
112297 112608   #endif
112298 112609   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
112299 112610    {/* zName:     */ "default_cache_size",
112300 112611     /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
112301 112612     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
112302 112613     /* ColNames:  */ 0, 1,
................................................................................
112325 112636     /* ColNames:  */ 0, 0,
112326 112637     /* iArg:      */ 0 },
112327 112638   #endif
112328 112639   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
112329 112640    {/* zName:     */ "foreign_key_check",
112330 112641     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
112331 112642     /* ePragFlg:  */ PragFlg_NeedSchema,
112332         -  /* ColNames:  */ 38, 4,
       112643  +  /* ColNames:  */ 39, 4,
112333 112644     /* iArg:      */ 0 },
112334 112645   #endif
112335 112646   #if !defined(SQLITE_OMIT_FOREIGN_KEY)
112336 112647    {/* zName:     */ "foreign_key_list",
112337 112648     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
112338 112649     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
112339         -  /* ColNames:  */ 30, 8,
       112650  +  /* ColNames:  */ 31, 8,
112340 112651     /* iArg:      */ 0 },
112341 112652   #endif
112342 112653   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112343 112654   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
112344 112655    {/* zName:     */ "foreign_keys",
112345 112656     /* ePragTyp:  */ PragTyp_FLAG,
112346 112657     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
................................................................................
112395 112706     /* ColNames:  */ 0, 0,
112396 112707     /* iArg:      */ 0 },
112397 112708   #endif
112398 112709   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
112399 112710    {/* zName:     */ "index_info",
112400 112711     /* ePragTyp:  */ PragTyp_INDEX_INFO,
112401 112712     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
112402         -  /* ColNames:  */ 11, 3,
       112713  +  /* ColNames:  */ 12, 3,
112403 112714     /* iArg:      */ 0 },
112404 112715    {/* zName:     */ "index_list",
112405 112716     /* ePragTyp:  */ PragTyp_INDEX_LIST,
112406 112717     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
112407         -  /* ColNames:  */ 20, 5,
       112718  +  /* ColNames:  */ 21, 5,
112408 112719     /* iArg:      */ 0 },
112409 112720    {/* zName:     */ "index_xinfo",
112410 112721     /* ePragTyp:  */ PragTyp_INDEX_INFO,
112411 112722     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
112412         -  /* ColNames:  */ 14, 6,
       112723  +  /* ColNames:  */ 15, 6,
112413 112724     /* iArg:      */ 1 },
112414 112725   #endif
112415 112726   #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
112416 112727    {/* zName:     */ "integrity_check",
112417 112728     /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
112418 112729     /* ePragFlg:  */ PragFlg_NeedSchema,
112419 112730     /* ColNames:  */ 0, 0,
................................................................................
112452 112763     /* ColNames:  */ 0, 0,
112453 112764     /* iArg:      */ 0 },
112454 112765   #endif
112455 112766   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
112456 112767    {/* zName:     */ "lock_status",
112457 112768     /* ePragTyp:  */ PragTyp_LOCK_STATUS,
112458 112769     /* ePragFlg:  */ PragFlg_Result0,
112459         -  /* ColNames:  */ 46, 2,
       112770  +  /* ColNames:  */ 47, 2,
112460 112771     /* iArg:      */ 0 },
112461 112772   #endif
112462 112773   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112463 112774    {/* zName:     */ "locking_mode",
112464 112775     /* ePragTyp:  */ PragTyp_LOCKING_MODE,
112465 112776     /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
112466 112777     /* ColNames:  */ 0, 0,
................................................................................
112471 112782     /* ColNames:  */ 0, 0,
112472 112783     /* iArg:      */ 0 },
112473 112784    {/* zName:     */ "mmap_size",
112474 112785     /* ePragTyp:  */ PragTyp_MMAP_SIZE,
112475 112786     /* ePragFlg:  */ 0,
112476 112787     /* ColNames:  */ 0, 0,
112477 112788     /* iArg:      */ 0 },
       112789  +#endif
       112790  + {/* zName:     */ "optimize",
       112791  +  /* ePragTyp:  */ PragTyp_OPTIMIZE,
       112792  +  /* ePragFlg:  */ PragFlg_Result1,
       112793  +  /* ColNames:  */ 0, 0,
       112794  +  /* iArg:      */ 0 },
       112795  +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112478 112796    {/* zName:     */ "page_count",
112479 112797     /* ePragTyp:  */ PragTyp_PAGE_COUNT,
112480 112798     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
112481 112799     /* ColNames:  */ 0, 0,
112482 112800     /* iArg:      */ 0 },
112483 112801    {/* zName:     */ "page_size",
112484 112802     /* ePragTyp:  */ PragTyp_PAGE_SIZE,
................................................................................
112569 112887    {/* zName:     */ "sql_trace",
112570 112888     /* ePragTyp:  */ PragTyp_FLAG,
112571 112889     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112572 112890     /* ColNames:  */ 0, 0,
112573 112891     /* iArg:      */ SQLITE_SqlTrace },
112574 112892   #endif
112575 112893   #endif
112576         -#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
       112894  +#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
112577 112895    {/* zName:     */ "stats",
112578 112896     /* ePragTyp:  */ PragTyp_STATS,
112579 112897     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
112580         -  /* ColNames:  */ 7, 4,
       112898  +  /* ColNames:  */ 7, 5,
112581 112899     /* iArg:      */ 0 },
112582 112900   #endif
112583 112901   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112584 112902    {/* zName:     */ "synchronous",
112585 112903     /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
112586 112904     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
112587 112905     /* ColNames:  */ 0, 0,
................................................................................
112652 112970     /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
112653 112971     /* ePragFlg:  */ 0,
112654 112972     /* ColNames:  */ 0, 0,
112655 112973     /* iArg:      */ 0 },
112656 112974    {/* zName:     */ "wal_checkpoint",
112657 112975     /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
112658 112976     /* ePragFlg:  */ PragFlg_NeedSchema,
112659         -  /* ColNames:  */ 42, 3,
       112977  +  /* ColNames:  */ 43, 3,
112660 112978     /* iArg:      */ 0 },
112661 112979   #endif
112662 112980   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
112663 112981    {/* zName:     */ "writable_schema",
112664 112982     /* ePragTyp:  */ PragTyp_FLAG,
112665 112983     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
112666 112984     /* ColNames:  */ 0, 0,
112667 112985     /* iArg:      */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
112668 112986   #endif
112669 112987   };
112670         -/* Number of pragmas: 60 on by default, 73 total. */
       112988  +/* Number of pragmas: 60 on by default, 74 total. */
112671 112989   
112672 112990   /************** End of pragma.h **********************************************/
112673 112991   /************** Continuing where we left off in pragma.c *********************/
112674 112992   
112675 112993   /*
112676 112994   ** Interpret the given string as a safety level.  Return 0 for OFF,
112677 112995   ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA.  Return 1 for an empty or 
................................................................................
112932 113250         upr = mid - 1;
112933 113251       }else{
112934 113252         lwr = mid + 1;
112935 113253       }
112936 113254     }
112937 113255     return lwr>upr ? 0 : &aPragmaName[mid];
112938 113256   }
       113257  +
       113258  +/*
       113259  +** Helper subroutine for PRAGMA integrity_check:
       113260  +**
       113261  +** Generate code to output a single-column result row with the result
       113262  +** held in register regResult.  Decrement the result count and halt if
       113263  +** the maximum number of result rows have been issued.
       113264  +*/
       113265  +static int integrityCheckResultRow(Vdbe *v, int regResult){
       113266  +  int addr;
       113267  +  sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 1);
       113268  +  addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
       113269  +  VdbeCoverage(v);
       113270  +  sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
       113271  +  return addr;
       113272  +}
112939 113273   
112940 113274   /*
112941 113275   ** Process a pragma statement.  
112942 113276   **
112943 113277   ** Pragmas are of this form:
112944 113278   **
112945 113279   **      PRAGMA [schema.]id [= value]
................................................................................
113637 113971     case PragTyp_SYNCHRONOUS: {
113638 113972       if( !zRight ){
113639 113973         returnSingleInt(v, pDb->safety_level-1);
113640 113974       }else{
113641 113975         if( !db->autoCommit ){
113642 113976           sqlite3ErrorMsg(pParse, 
113643 113977               "Safety level may not be changed inside a transaction");
113644         -      }else{
       113978  +      }else if( iDb!=1 ){
113645 113979           int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
113646 113980           if( iLevel==0 ) iLevel = 1;
113647 113981           pDb->safety_level = iLevel;
113648 113982           pDb->bSyncSet = 1;
113649 113983           setAllPagerFlags(db);
113650 113984         }
113651 113985       }
................................................................................
113736 114070                  k);
113737 114071           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
113738 114072         }
113739 114073       }
113740 114074     }
113741 114075     break;
113742 114076   
       114077  +#ifdef SQLITE_DEBUG
113743 114078     case PragTyp_STATS: {
113744 114079       Index *pIdx;
113745 114080       HashElem *i;
113746         -    pParse->nMem = 4;
       114081  +    pParse->nMem = 5;
113747 114082       sqlite3CodeVerifySchema(pParse, iDb);
113748 114083       for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
113749 114084         Table *pTab = sqliteHashData(i);
113750         -      sqlite3VdbeMultiLoad(v, 1, "ssii",
       114085  +      sqlite3VdbeMultiLoad(v, 1, "ssiii",
113751 114086              pTab->zName,
113752 114087              0,
113753 114088              pTab->szTabRow,
113754         -           pTab->nRowLogEst);
113755         -      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
       114089  +           pTab->nRowLogEst,
       114090  +           pTab->tabFlags);
       114091  +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
113756 114092         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113757         -        sqlite3VdbeMultiLoad(v, 2, "sii",
       114093  +        sqlite3VdbeMultiLoad(v, 2, "siii",
113758 114094              pIdx->zName,
113759 114095              pIdx->szIdxRow,
113760         -           pIdx->aiRowLogEst[0]);
113761         -        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
       114096  +           pIdx->aiRowLogEst[0],
       114097  +           pIdx->hasStat1);
       114098  +        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
113762 114099         }
113763 114100       }
113764 114101     }
113765 114102     break;
       114103  +#endif
113766 114104   
113767 114105     case PragTyp_INDEX_INFO: if( zRight ){
113768 114106       Index *pIdx;
113769 114107       Table *pTab;
113770 114108       pIdx = sqlite3FindIndex(db, zRight, zDb);
113771 114109       if( pIdx ){
113772 114110         int i;
................................................................................
114015 114353     break;
114016 114354   
114017 114355   #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
114018 114356   # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
114019 114357   #endif
114020 114358   
114021 114359   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
114022         -  /* Pragma "quick_check" is reduced version of 
       114360  +  /*    PRAGMA integrity_check
       114361  +  **    PRAGMA integrity_check(N)
       114362  +  **    PRAGMA quick_check
       114363  +  **    PRAGMA quick_check(N)
       114364  +  **
       114365  +  ** Verify the integrity of the database.
       114366  +  **
       114367  +  ** The "quick_check" is reduced version of 
114023 114368     ** integrity_check designed to detect most database corruption
114024         -  ** without most of the overhead of a full integrity-check.
       114369  +  ** without the overhead of cross-checking indexes.  Quick_check
       114370  +  ** is linear time wherease integrity_check is O(NlogN).
114025 114371     */
114026 114372     case PragTyp_INTEGRITY_CHECK: {
114027 114373       int i, j, addr, mxErr;
114028 114374   
114029 114375       int isQuick = (sqlite3Tolower(zLeft[0])=='q');
114030 114376   
114031 114377       /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
................................................................................
114048 114394       mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
114049 114395       if( zRight ){
114050 114396         sqlite3GetInt32(zRight, &mxErr);
114051 114397         if( mxErr<=0 ){
114052 114398           mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
114053 114399         }
114054 114400       }
114055         -    sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
       114401  +    sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
114056 114402   
114057 114403       /* Do an integrity check on each database file */
114058 114404       for(i=0; i<db->nDb; i++){
114059 114405         HashElem *x;
114060 114406         Hash *pTbls;
114061 114407         int *aRoot;
114062 114408         int cnt = 0;
................................................................................
114063 114409         int mxIdx = 0;
114064 114410         int nIdx;
114065 114411   
114066 114412         if( OMIT_TEMPDB && i==1 ) continue;
114067 114413         if( iDb>=0 && i!=iDb ) continue;
114068 114414   
114069 114415         sqlite3CodeVerifySchema(pParse, i);
114070         -      addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
114071         -      VdbeCoverage(v);
114072         -      sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
114073         -      sqlite3VdbeJumpHere(v, addr);
114074 114416   
114075 114417         /* Do an integrity check of the B-Tree
114076 114418         **
114077 114419         ** Begin by finding the root pages numbers
114078 114420         ** for all tables and indices in the database.
114079 114421         */
114080 114422         assert( sqlite3SchemaMutexHeld(db, i, 0) );
................................................................................
114106 114448         sqlite3VdbeChangeP5(v, (u8)i);
114107 114449         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
114108 114450         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
114109 114451            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
114110 114452            P4_DYNAMIC);
114111 114453         sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
114112 114454         sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
114113         -      sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
       114455  +      integrityCheckResultRow(v, 2);
114114 114456         sqlite3VdbeJumpHere(v, addr);
114115 114457   
114116 114458         /* Make sure all the indices are constructed correctly.
114117 114459         */
114118         -      for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
       114460  +      for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
114119 114461           Table *pTab = sqliteHashData(x);
114120 114462           Index *pIdx, *pPk;
114121 114463           Index *pPrior = 0;
114122 114464           int loopTop;
114123 114465           int iDataCur, iIdxCur;
114124 114466           int r1 = -1;
114125 114467   
114126         -        if( pTab->pIndex==0 ) continue;
       114468  +        if( pTab->tnum<1 ) continue;  /* Skip VIEWs or VIRTUAL TABLEs */
       114469  +        if( pTab->pCheck==0
       114470  +         && (pTab->tabFlags & TF_HasNotNull)==0
       114471  +         && (pTab->pIndex==0 || isQuick)
       114472  +        ){
       114473  +          continue;  /* No additional checks needed for this table */
       114474  +        }
114127 114475           pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
114128         -        addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
114129         -        VdbeCoverage(v);
114130         -        sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
114131         -        sqlite3VdbeJumpHere(v, addr);
114132 114476           sqlite3ExprCacheClear(pParse);
114133 114477           sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
114134 114478                                      1, 0, &iDataCur, &iIdxCur);
114135 114479           sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
114136 114480           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
114137 114481             sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
114138 114482           }
................................................................................
114139 114483           assert( pParse->nMem>=8+j );
114140 114484           assert( sqlite3NoTempsInRange(pParse,1,7+j) );
114141 114485           sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
114142 114486           loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
114143 114487           /* Verify that all NOT NULL columns really are NOT NULL */
114144 114488           for(j=0; j<pTab->nCol; j++){
114145 114489             char *zErr;
114146         -          int jmp2, jmp3;
       114490  +          int jmp2;
114147 114491             if( j==pTab->iPKey ) continue;
114148 114492             if( pTab->aCol[j].notNull==0 ) continue;
114149 114493             sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
114150 114494             sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
114151 114495             jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
114152         -          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
114153 114496             zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
114154 114497                                 pTab->aCol[j].zName);
114155 114498             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
114156         -          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
114157         -          jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
114158         -          sqlite3VdbeAddOp0(v, OP_Halt);
       114499  +          integrityCheckResultRow(v, 3);
114159 114500             sqlite3VdbeJumpHere(v, jmp2);
114160         -          sqlite3VdbeJumpHere(v, jmp3);
       114501  +        }
       114502  +        /* Verify CHECK constraints */
       114503  +        if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
       114504  +          int addrCkFault = sqlite3VdbeMakeLabel(v);
       114505  +          int addrCkOk = sqlite3VdbeMakeLabel(v);
       114506  +          ExprList *pCheck = pTab->pCheck;
       114507  +          char *zErr;
       114508  +          int k;
       114509  +          pParse->iSelfTab = iDataCur;
       114510  +          sqlite3ExprCachePush(pParse);
       114511  +          for(k=pCheck->nExpr-1; k>0; k--){
       114512  +            sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
       114513  +          }
       114514  +          sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, 
       114515  +                            SQLITE_JUMPIFNULL);
       114516  +          sqlite3VdbeResolveLabel(v, addrCkFault);
       114517  +          zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
       114518  +                                pTab->zName);
       114519  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
       114520  +          integrityCheckResultRow(v, 3);
       114521  +          sqlite3VdbeResolveLabel(v, addrCkOk);
       114522  +          sqlite3ExprCachePop(pParse);
114161 114523           }
114162 114524           /* Validate index entries for the current row */
114163         -        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
       114525  +        for(j=0, pIdx=pTab->pIndex; pIdx && !isQuick; pIdx=pIdx->pNext, j++){
114164 114526             int jmp2, jmp3, jmp4, jmp5;
114165 114527             int ckUniq = sqlite3VdbeMakeLabel(v);
114166 114528             if( pPk==pIdx ) continue;
114167 114529             r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
114168 114530                                          pPrior, r1);
114169 114531             pPrior = pIdx;
114170 114532             sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
114171 114533             /* Verify that an index entry exists for the current table row */
114172 114534             jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
114173 114535                                         pIdx->nColumn); VdbeCoverage(v);
114174         -          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
114175 114536             sqlite3VdbeLoadString(v, 3, "row ");
114176 114537             sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
114177 114538             sqlite3VdbeLoadString(v, 4, " missing from index ");
114178 114539             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
114179 114540             jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
114180 114541             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
114181         -          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
114182         -          jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
114183         -          sqlite3VdbeAddOp0(v, OP_Halt);
       114542  +          jmp4 = integrityCheckResultRow(v, 3);
114184 114543             sqlite3VdbeJumpHere(v, jmp2);
114185 114544             /* For UNIQUE indexes, verify that only one entry exists with the
114186 114545             ** current key.  The entry is unique if (1) any column is NULL
114187 114546             ** or (2) the next entry has a different key */
114188 114547             if( IsUniqueIndex(pIdx) ){
114189 114548               int uniqOk = sqlite3VdbeMakeLabel(v);
114190 114549               int jmp6;
................................................................................
114197 114556                 VdbeCoverage(v);
114198 114557               }
114199 114558               jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
114200 114559               sqlite3VdbeGoto(v, uniqOk);
114201 114560               sqlite3VdbeJumpHere(v, jmp6);
114202 114561               sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
114203 114562                                    pIdx->nKeyCol); VdbeCoverage(v);
114204         -            sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
114205 114563               sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
114206 114564               sqlite3VdbeGoto(v, jmp5);
114207 114565               sqlite3VdbeResolveLabel(v, uniqOk);
114208 114566             }
114209 114567             sqlite3VdbeJumpHere(v, jmp4);
114210 114568             sqlite3ResolvePartIdxLabel(pParse, jmp3);
114211 114569           }
114212 114570           sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
114213 114571           sqlite3VdbeJumpHere(v, loopTop-1);
114214 114572   #ifndef SQLITE_OMIT_BTREECOUNT
114215         -        sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
114216         -        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
114217         -          if( pPk==pIdx ) continue;
114218         -          addr = sqlite3VdbeCurrentAddr(v);
114219         -          sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
114220         -          sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
114221         -          sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
114222         -          sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
114223         -          sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
114224         -          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
114225         -          sqlite3VdbeLoadString(v, 3, pIdx->zName);
114226         -          sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
114227         -          sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
       114573  +        if( !isQuick ){
       114574  +          sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
       114575  +          for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
       114576  +            if( pPk==pIdx ) continue;
       114577  +            sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
       114578  +            addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
       114579  +            sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
       114580  +            sqlite3VdbeLoadString(v, 3, pIdx->zName);
       114581  +            sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
       114582  +            integrityCheckResultRow(v, 7);
       114583  +            sqlite3VdbeJumpHere(v, addr);
       114584  +          }
114228 114585           }
114229 114586   #endif /* SQLITE_OMIT_BTREECOUNT */
114230 114587         } 
114231 114588       }
114232 114589       {
114233 114590         static const int iLn = VDBE_OFFSET_LINENO(2);
114234 114591         static const VdbeOpList endCode[] = {
114235 114592           { OP_AddImm,      1, 0,        0},    /* 0 */
114236         -        { OP_If,          1, 4,        0},    /* 1 */
       114593  +        { OP_IfNotZero,   1, 4,        0},    /* 1 */
114237 114594           { OP_String8,     0, 3,        0},    /* 2 */
114238 114595           { OP_ResultRow,   3, 1,        0},    /* 3 */
114239 114596         };
114240 114597         VdbeOp *aOp;
114241 114598   
114242 114599         aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
114243 114600         if( aOp ){
114244         -        aOp[0].p2 = -mxErr;
       114601  +        aOp[0].p2 = 1-mxErr;
114245 114602           aOp[2].p4type = P4_STATIC;
114246 114603           aOp[2].p4.z = "ok";
114247 114604         }
114248 114605       }
114249 114606     }
114250 114607     break;
114251 114608   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
................................................................................
114461 114818     ** connection on which it is invoked to free up as much memory as it
114462 114819     ** can, by calling sqlite3_db_release_memory().
114463 114820     */
114464 114821     case PragTyp_SHRINK_MEMORY: {
114465 114822       sqlite3_db_release_memory(db);
114466 114823       break;
114467 114824     }
       114825  +
       114826  +  /*
       114827  +  **  PRAGMA optimize
       114828  +  **  PRAGMA optimize(MASK)
       114829  +  **  PRAGMA schema.optimize
       114830  +  **  PRAGMA schema.optimize(MASK)
       114831  +  **
       114832  +  ** Attempt to optimize the database.  All schemas are optimized in the first
       114833  +  ** two forms, and only the specified schema is optimized in the latter two.
       114834  +  **
       114835  +  ** The details of optimizations performed by this pragma are expected
       114836  +  ** to change and improve over time.  Applications should anticipate that
       114837  +  ** this pragma will perform new optimizations in future releases.
       114838  +  **
       114839  +  ** The optional argument is a bitmask of optimizations to perform:
       114840  +  **
       114841  +  **    0x0001    Debugging mode.  Do not actually perform any optimizations
       114842  +  **              but instead return one line of text for each optimization
       114843  +  **              that would have been done.  Off by default.
       114844  +  **
       114845  +  **    0x0002    Run ANALYZE on tables that might benefit.  On by default.
       114846  +  **              See below for additional information.
       114847  +  **
       114848  +  **    0x0004    (Not yet implemented) Record usage and performance 
       114849  +  **              information from the current session in the
       114850  +  **              database file so that it will be available to "optimize"
       114851  +  **              pragmas run by future database connections.
       114852  +  **
       114853  +  **    0x0008    (Not yet implemented) Create indexes that might have
       114854  +  **              been helpful to recent queries
       114855  +  **
       114856  +  ** The default MASK is and always shall be 0xfffe.  0xfffe means perform all    ** of the optimizations listed above except Debug Mode, including new
       114857  +  ** optimizations that have not yet been invented.  If new optimizations are
       114858  +  ** ever added that should be off by default, those off-by-default 
       114859  +  ** optimizations will have bitmasks of 0x10000 or larger.
       114860  +  **
       114861  +  ** DETERMINATION OF WHEN TO RUN ANALYZE
       114862  +  **
       114863  +  ** In the current implementation, a table is analyzed if only if all of
       114864  +  ** the following are true:
       114865  +  **
       114866  +  ** (1) MASK bit 0x02 is set.
       114867  +  **
       114868  +  ** (2) The query planner used sqlite_stat1-style statistics for one or
       114869  +  **     more indexes of the table at some point during the lifetime of
       114870  +  **     the current connection.
       114871  +  **
       114872  +  ** (3) One or more indexes of the table are currently unanalyzed OR
       114873  +  **     the number of rows in the table has increased by 25 times or more
       114874  +  **     since the last time ANALYZE was run.
       114875  +  **
       114876  +  ** The rules for when tables are analyzed are likely to change in
       114877  +  ** future releases.
       114878  +  */
       114879  +  case PragTyp_OPTIMIZE: {
       114880  +    int iDbLast;           /* Loop termination point for the schema loop */
       114881  +    int iTabCur;           /* Cursor for a table whose size needs checking */
       114882  +    HashElem *k;           /* Loop over tables of a schema */
       114883  +    Schema *pSchema;       /* The current schema */
       114884  +    Table *pTab;           /* A table in the schema */
       114885  +    Index *pIdx;           /* An index of the table */
       114886  +    LogEst szThreshold;    /* Size threshold above which reanalysis is needd */
       114887  +    char *zSubSql;         /* SQL statement for the OP_SqlExec opcode */
       114888  +    u32 opMask;            /* Mask of operations to perform */
       114889  +
       114890  +    if( zRight ){
       114891  +      opMask = (u32)sqlite3Atoi(zRight);
       114892  +      if( (opMask & 0x02)==0 ) break;
       114893  +    }else{
       114894  +      opMask = 0xfffe;
       114895  +    }
       114896  +    iTabCur = pParse->nTab++;
       114897  +    for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
       114898  +      if( iDb==1 ) continue;
       114899  +      sqlite3CodeVerifySchema(pParse, iDb);
       114900  +      pSchema = db->aDb[iDb].pSchema;
       114901  +      for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
       114902  +        pTab = (Table*)sqliteHashData(k);
       114903  +
       114904  +        /* If table pTab has not been used in a way that would benefit from
       114905  +        ** having analysis statistics during the current session, then skip it.
       114906  +        ** This also has the effect of skipping virtual tables and views */
       114907  +        if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
       114908  +
       114909  +        /* Reanalyze if the table is 25 times larger than the last analysis */
       114910  +        szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
       114911  +        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       114912  +          if( !pIdx->hasStat1 ){
       114913  +            szThreshold = 0; /* Always analyze if any index lacks statistics */
       114914  +            break;
       114915  +          }
       114916  +        }
       114917  +        if( szThreshold ){
       114918  +          sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
       114919  +          sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur, 
       114920  +                         sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
       114921  +          VdbeCoverage(v);
       114922  +        }
       114923  +        zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
       114924  +                                 db->aDb[iDb].zDbSName, pTab->zName);
       114925  +        if( opMask & 0x01 ){
       114926  +          int r1 = sqlite3GetTempReg(pParse);
       114927  +          sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
       114928  +          sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
       114929  +        }else{
       114930  +          sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
       114931  +        }
       114932  +      }
       114933  +    }
       114934  +    sqlite3VdbeAddOp0(v, OP_Expire);
       114935  +    break;
       114936  +  }
114468 114937   
114469 114938     /*
114470 114939     **   PRAGMA busy_timeout
114471 114940     **   PRAGMA busy_timeout = N
114472 114941     **
114473 114942     ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
114474 114943     ** if one is set.  If no busy handler or a different busy handler is set
................................................................................
119523 119992             sqlite3ExprDup(db, pSub->pHaving, 0), pParent->pHaving
119524 119993         );
119525 119994         assert( pParent->pGroupBy==0 );
119526 119995         pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
119527 119996       }else{
119528 119997         pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
119529 119998       }
119530         -    substSelect(pParse, pParent, iParent, pSub->pEList, 0);
       119999  +    if( db->mallocFailed==0 ){
       120000  +      substSelect(pParse, pParent, iParent, pSub->pEList, 0);
       120001  +    }
119531 120002     
119532 120003       /* The flattened query is distinct if either the inner or the
119533 120004       ** outer query is distinct. 
119534 120005       */
119535 120006       pParent->selFlags |= pSub->selFlags & SF_Distinct;
119536 120007     
119537 120008       /*
................................................................................
123777 124248   ** not work if other processes are attached to the original database.
123778 124249   ** And a power loss in between deleting the original and renaming the
123779 124250   ** transient would cause the database file to appear to be deleted
123780 124251   ** following reboot.
123781 124252   */
123782 124253   SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm){
123783 124254     Vdbe *v = sqlite3GetVdbe(pParse);
123784         -  int iDb = pNm ? sqlite3TwoPartName(pParse, pNm, pNm, &pNm) : 0;
123785         -  if( v && (iDb>=2 || iDb==0) ){
       124255  +  int iDb = 0;
       124256  +  if( v==0 ) return;
       124257  +  if( pNm ){
       124258  +#ifndef SQLITE_BUG_COMPATIBLE_20160819
       124259  +    /* Default behavior:  Report an error if the argument to VACUUM is
       124260  +    ** not recognized */
       124261  +    iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
       124262  +    if( iDb<0 ) return;
       124263  +#else
       124264  +    /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
       124265  +    ** to VACUUM are silently ignored.  This is a back-out of a bug fix that
       124266  +    ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
       124267  +    ** The buggy behavior is required for binary compatibility with some
       124268  +    ** legacy applications. */
       124269  +    iDb = sqlite3FindDb(pParse->db, pNm);
       124270  +    if( iDb<0 ) iDb = 0;
       124271  +#endif
       124272  +  }
       124273  +  if( iDb!=1 ){
123786 124274       sqlite3VdbeAddOp1(v, OP_Vacuum, iDb);
123787 124275       sqlite3VdbeUsesBtree(v, iDb);
123788 124276     }
123789 124277     return;
123790 124278   }
123791 124279   
123792 124280   /*
................................................................................
124372 124860     if( pTable==0 ) return;
124373 124861     assert( 0==pTable->pIndex );
124374 124862   
124375 124863     db = pParse->db;
124376 124864     iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
124377 124865     assert( iDb>=0 );
124378 124866   
124379         -  pTable->tabFlags |= TF_Virtual;
124380         -  pTable->nModuleArg = 0;
       124867  +  assert( pTable->nModuleArg==0 );
124381 124868     addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
124382 124869     addModuleArgument(db, pTable, 0);
124383 124870     addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
124384 124871     assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
124385 124872          || (pParse->sNameToken.z==pName1->z && pName2->z==0)
124386 124873     );
124387 124874     pParse->sNameToken.n = (int)(
................................................................................
124661 125148   SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
124662 125149     sqlite3 *db = pParse->db;
124663 125150     const char *zMod;
124664 125151     Module *pMod;
124665 125152     int rc;
124666 125153   
124667 125154     assert( pTab );
124668         -  if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
       125155  +  if( !IsVirtual(pTab) || sqlite3GetVTable(db, pTab) ){
124669 125156       return SQLITE_OK;
124670 125157     }
124671 125158   
124672 125159     /* Locate the required virtual table module */
124673 125160     zMod = pTab->azModuleArg[0];
124674 125161     pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
124675 125162   
................................................................................
124731 125218   SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
124732 125219     int rc = SQLITE_OK;
124733 125220     Table *pTab;
124734 125221     Module *pMod;
124735 125222     const char *zMod;
124736 125223   
124737 125224     pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
124738         -  assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
       125225  +  assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
124739 125226   
124740 125227     /* Locate the required virtual table module */
124741 125228     zMod = pTab->azModuleArg[0];
124742 125229     pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
124743 125230   
124744 125231     /* If the module has been registered and includes a Create method, 
124745 125232     ** invoke it now. If the module has not been registered, return an 
................................................................................
124785 125272     pCtx = db->pVtabCtx;
124786 125273     if( !pCtx || pCtx->bDeclared ){
124787 125274       sqlite3Error(db, SQLITE_MISUSE);
124788 125275       sqlite3_mutex_leave(db->mutex);
124789 125276       return SQLITE_MISUSE_BKPT;
124790 125277     }
124791 125278     pTab = pCtx->pTab;
124792         -  assert( (pTab->tabFlags & TF_Virtual)!=0 );
       125279  +  assert( IsVirtual(pTab) );
124793 125280   
124794 125281     pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
124795 125282     if( pParse==0 ){
124796 125283       rc = SQLITE_NOMEM_BKPT;
124797 125284     }else{
124798 125285       pParse->declareVtab = 1;
124799 125286       pParse->db = db;
124800 125287       pParse->nQueryLoop = 1;
124801 125288     
124802 125289       if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) 
124803 125290        && pParse->pNewTable
124804 125291        && !db->mallocFailed
124805 125292        && !pParse->pNewTable->pSelect
124806         -     && (pParse->pNewTable->tabFlags & TF_Virtual)==0
       125293  +     && !IsVirtual(pParse->pNewTable)
124807 125294       ){
124808 125295         if( !pTab->aCol ){
124809 125296           Table *pNew = pParse->pNewTable;
124810 125297           Index *pIdx;
124811 125298           pTab->aCol = pNew->aCol;
124812 125299           pTab->nCol = pNew->nCol;
124813 125300           pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
................................................................................
125088 125575   
125089 125576   
125090 125577     /* Check to see the left operand is a column in a virtual table */
125091 125578     if( NEVER(pExpr==0) ) return pDef;
125092 125579     if( pExpr->op!=TK_COLUMN ) return pDef;
125093 125580     pTab = pExpr->pTab;
125094 125581     if( NEVER(pTab==0) ) return pDef;
125095         -  if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
       125582  +  if( !IsVirtual(pTab) ) return pDef;
125096 125583     pVtab = sqlite3GetVTable(db, pTab)->pVtab;
125097 125584     assert( pVtab!=0 );
125098 125585     assert( pVtab->pModule!=0 );
125099 125586     pMod = (sqlite3_module *)pVtab->pModule;
125100 125587     if( pMod->xFindFunction==0 ) return pDef;
125101 125588    
125102 125589     /* Call the xFindFunction method on the virtual table implementation
................................................................................
125183 125670     if( pTab->zName==0 ){
125184 125671       sqlite3DbFree(db, pTab);
125185 125672       return 0;
125186 125673     }
125187 125674     pMod->pEpoTab = pTab;
125188 125675     pTab->nTabRef = 1;
125189 125676     pTab->pSchema = db->aDb[0].pSchema;
125190         -  pTab->tabFlags |= TF_Virtual;
125191         -  pTab->nModuleArg = 0;
       125677  +  assert( pTab->nModuleArg==0 );
125192 125678     pTab->iPKey = -1;
125193 125679     addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
125194 125680     addModuleArgument(db, pTab, 0);
125195 125681     addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
125196 125682     rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
125197 125683     if( rc ){
125198 125684       sqlite3ErrorMsg(pParse, "%s", zErr);
................................................................................
125255 125741     va_start(ap, op);
125256 125742     switch( op ){
125257 125743       case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
125258 125744         VtabCtx *p = db->pVtabCtx;
125259 125745         if( !p ){
125260 125746           rc = SQLITE_MISUSE_BKPT;
125261 125747         }else{
125262         -        assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
       125748  +        assert( p->pTab==0 || IsVirtual(p->pTab) );
125263 125749           p->pVTable->bConstraint = (u8)va_arg(ap, int);
125264 125750         }
125265 125751         break;
125266 125752       }
125267 125753       default:
125268 125754         rc = SQLITE_MISUSE_BKPT;
125269 125755         break;
................................................................................
125694 126180     ExprList *pOrderBy;       /* ORDER BY clause */
125695 126181     WhereLoop *pNew;          /* Template WhereLoop */
125696 126182     WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
125697 126183   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
125698 126184     UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
125699 126185     int nRecValid;            /* Number of valid fields currently in pRec */
125700 126186   #endif
       126187  +  unsigned int bldFlags;    /* SQLITE_BLDF_* flags */
125701 126188   };
125702 126189   
       126190  +/* Allowed values for WhereLoopBuider.bldFlags */
       126191  +#define SQLITE_BLDF_INDEXED  0x0001   /* An index is used */
       126192  +#define SQLITE_BLDF_UNIQUE   0x0002   /* All keys of a UNIQUE index used */
       126193  +
125703 126194   /*
125704 126195   ** The WHERE clause processing routine has two halves.  The
125705 126196   ** first part does the start of the WHERE loop and the second
125706 126197   ** half does the tail of the WHERE loop.  An instance of
125707 126198   ** this structure is returned by the first half and passed
125708 126199   ** into the second half to give some continuity.
125709 126200   **
................................................................................
125710 126201   ** An instance of this object holds the complete state of the query
125711 126202   ** planner.
125712 126203   */
125713 126204   struct WhereInfo {
125714 126205     Parse *pParse;            /* Parsing and code generating context */
125715 126206     SrcList *pTabList;        /* List of tables in the join */
125716 126207     ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
125717         -  ExprList *pDistinctSet;   /* DISTINCT over all these values */
       126208  +  ExprList *pResultSet;     /* Result set of the query */
125718 126209     LogEst iLimit;            /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
125719 126210     int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
125720 126211     int iContinue;            /* Jump here to continue with next record */
125721 126212     int iBreak;               /* Jump here to break out of the loop */
125722 126213     int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
125723 126214     u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
125724 126215     u8 nLevel;                /* Number of nested loop */
................................................................................
126894 127385     WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
126895 127386     WhereTerm *pTerm;               /* A WHERE clause term */
126896 127387     Parse *pParse;                  /* Parsing context */
126897 127388     sqlite3 *db;                    /* Database connection */
126898 127389     Vdbe *v;                        /* The prepared stmt under constructions */
126899 127390     struct SrcList_item *pTabItem;  /* FROM clause term being coded */
126900 127391     int addrBrk;                    /* Jump here to break out of the loop */
       127392  +  int addrHalt;                   /* addrBrk for the outermost loop */
126901 127393     int addrCont;                   /* Jump here to continue with next cycle */
126902 127394     int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
126903 127395     int iReleaseReg = 0;      /* Temp register to free before returning */
126904 127396   
126905 127397     pParse = pWInfo->pParse;
126906 127398     v = pParse->pVdbe;
126907 127399     pWC = &pWInfo->sWC;
................................................................................
126934 127426     ** row of the left table of the join.
126935 127427     */
126936 127428     if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
126937 127429       pLevel->iLeftJoin = ++pParse->nMem;
126938 127430       sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
126939 127431       VdbeComment((v, "init LEFT JOIN no-match flag"));
126940 127432     }
       127433  +
       127434  +  /* Compute a safe address to jump to if we discover that the table for
       127435  +  ** this loop is empty and can never contribute content. */
       127436  +  for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
       127437  +  addrHalt = pWInfo->a[j].addrBrk;
126941 127438   
126942 127439     /* Special case of a FROM clause subquery implemented as a co-routine */
126943 127440     if( pTabItem->fg.viaCoroutine ){
126944 127441       int regYield = pTabItem->regReturn;
126945 127442       sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
126946 127443       pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
126947 127444       VdbeCoverage(v);
................................................................................
127119 127616         VdbeCoverageIf(v, pX->op==TK_GT);
127120 127617         VdbeCoverageIf(v, pX->op==TK_LE);
127121 127618         VdbeCoverageIf(v, pX->op==TK_LT);
127122 127619         VdbeCoverageIf(v, pX->op==TK_GE);
127123 127620         sqlite3ExprCacheAffinityChange(pParse, r1, 1);
127124 127621         sqlite3ReleaseTempReg(pParse, rTemp);
127125 127622       }else{
127126         -      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
       127623  +      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrHalt);
127127 127624         VdbeCoverageIf(v, bRev==0);
127128 127625         VdbeCoverageIf(v, bRev!=0);
127129 127626       }
127130 127627       if( pEnd ){
127131 127628         Expr *pX;
127132 127629         pX = pEnd->pExpr;
127133 127630         assert( pX!=0 );
................................................................................
127765 128262         /* Tables marked isRecursive have only a single row that is stored in
127766 128263         ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
127767 128264         pLevel->op = OP_Noop;
127768 128265       }else{
127769 128266         codeCursorHint(pTabItem, pWInfo, pLevel, 0);
127770 128267         pLevel->op = aStep[bRev];
127771 128268         pLevel->p1 = iCur;
127772         -      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
       128269  +      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
127773 128270         VdbeCoverageIf(v, bRev==0);
127774 128271         VdbeCoverageIf(v, bRev!=0);
127775 128272         pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
127776 128273       }
127777 128274     }
127778 128275   
127779 128276   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
................................................................................
128090 128587       return 0;
128091 128588     }
128092 128589   #ifdef SQLITE_EBCDIC
128093 128590     if( *pnoCase ) return 0;
128094 128591   #endif
128095 128592     pList = pExpr->x.pList;
128096 128593     pLeft = pList->a[1].pExpr;
128097         -  if( pLeft->op!=TK_COLUMN 
128098         -   || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
128099         -   || IsVirtual(pLeft->pTab)  /* Value might be numeric */
128100         -  ){
128101         -    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
128102         -    ** be the name of an indexed column with TEXT affinity. */
128103         -    return 0;
128104         -  }
128105         -  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
128106 128594   
128107 128595     pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
128108 128596     op = pRight->op;
128109 128597     if( op==TK_VARIABLE ){
128110 128598       Vdbe *pReprepare = pParse->pReprepare;
128111 128599       int iCol = pRight->iColumn;
128112 128600       pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
................................................................................
128115 128603       }
128116 128604       sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
128117 128605       assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
128118 128606     }else if( op==TK_STRING ){
128119 128607       z = pRight->u.zToken;
128120 128608     }
128121 128609     if( z ){
       128610  +
       128611  +    /* If the RHS begins with a digit or a minus sign, then the LHS must
       128612  +    ** be an ordinary column (not a virtual table column) with TEXT affinity.
       128613  +    ** Otherwise the LHS might be numeric and "lhs >= rhs" would be false
       128614  +    ** even though "lhs LIKE rhs" is true.  But if the RHS does not start
       128615  +    ** with a digit or '-', then "lhs LIKE rhs" will always be false if
       128616  +    ** the LHS is numeric and so the optimization still works.
       128617  +    */
       128618  +    if( sqlite3Isdigit(z[0]) || z[0]=='-' ){
       128619  +      if( pLeft->op!=TK_COLUMN 
       128620  +       || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
       128621  +       || IsVirtual(pLeft->pTab)  /* Value might be numeric */
       128622  +      ){
       128623  +        sqlite3ValueFree(pVal);
       128624  +        return 0;
       128625  +      }
       128626  +    }
128122 128627       cnt = 0;
128123 128628       while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
128124 128629         cnt++;
128125 128630       }
128126 128631       if( cnt!=0 && 255!=(u8)z[cnt-1] ){
128127 128632         Expr *pPrefix;
128128 128633         *pisComplete = c==wc[0] && z[cnt+1]==0;
................................................................................
128743 129248     if( (mPrereq&(mPrereq-1))!=0 ) return 0;   /* Refs more than one table */
128744 129249     for(i=0; mPrereq>1; i++, mPrereq>>=1){}
128745 129250     iCur = pFrom->a[i].iCursor;
128746 129251     for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
128747 129252       if( pIdx->aColExpr==0 ) continue;
128748 129253       for(i=0; i<pIdx->nKeyCol; i++){
128749 129254         if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
128750         -      if( sqlite3ExprCompare(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
       129255  +      if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
128751 129256           *piCur = iCur;
128752 129257           *piColumn = XN_EXPR;
128753 129258           return 1;
128754 129259         }
128755 129260       }
128756 129261     }
128757 129262     return 0;
................................................................................
129534 130039       iCur = pScan->aiCur[pScan->iEquiv-1];
129535 130040       assert( pWC!=0 );
129536 130041       do{
129537 130042         for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
129538 130043           if( pTerm->leftCursor==iCur
129539 130044            && pTerm->u.leftColumn==iColumn
129540 130045            && (iColumn!=XN_EXPR
129541         -             || sqlite3ExprCompare(pTerm->pExpr->pLeft,pScan->pIdxExpr,iCur)==0)
       130046  +             || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
       130047  +                                       pScan->pIdxExpr,iCur)==0)
129542 130048            && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
129543 130049           ){
129544 130050             if( (pTerm->eOperator & WO_EQUIV)!=0
129545 130051              && pScan->nEquiv<ArraySize(pScan->aiCur)
129546 130052              && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN
129547 130053             ){
129548 130054               int j;
................................................................................
129633 130139     pScan->idxaff = 0;
129634 130140     pScan->zCollName = 0;
129635 130141     if( pIdx ){
129636 130142       int j = iColumn;
129637 130143       iColumn = pIdx->aiColumn[j];
129638 130144       if( iColumn==XN_EXPR ){
129639 130145         pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
       130146  +      pScan->zCollName = pIdx->azColl[j];
129640 130147       }else if( iColumn==pIdx->pTable->iPKey ){
129641 130148         iColumn = XN_ROWID;
129642 130149       }else if( iColumn>=0 ){
129643 130150         pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
129644 130151         pScan->zCollName = pIdx->azColl[j];
129645 130152       }
129646 130153     }else if( iColumn==XN_EXPR ){
................................................................................
129840 130347   **
129841 130348   ** If the bIncrRowid parameter is 0, then any OP_Rowid instructions on
129842 130349   ** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero,
129843 130350   ** then each OP_Rowid is transformed into an instruction to increment the
129844 130351   ** value stored in its output register.
129845 130352   */
129846 130353   static void translateColumnToCopy(
129847         -  Vdbe *v,            /* The VDBE containing code to translate */
       130354  +  Parse *pParse,      /* Parsing context */
129848 130355     int iStart,         /* Translate from this opcode to the end */
129849 130356     int iTabCur,        /* OP_Column/OP_Rowid references to this table */
129850 130357     int iRegister,      /* The first column is in this register */
129851 130358     int bIncrRowid      /* If non-zero, transform OP_rowid to OP_AddImm(1) */
129852 130359   ){
       130360  +  Vdbe *v = pParse->pVdbe;
129853 130361     VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
129854 130362     int iEnd = sqlite3VdbeCurrentAddr(v);
       130363  +  if( pParse->db->mallocFailed ) return;
129855 130364     for(; iStart<iEnd; iStart++, pOp++){
129856 130365       if( pOp->p1!=iTabCur ) continue;
129857 130366       if( pOp->opcode==OP_Column ){
129858 130367         pOp->opcode = OP_Copy;
129859 130368         pOp->p1 = pOp->p2 + iRegister;
129860 130369         pOp->p2 = pOp->p3;
129861 130370         pOp->p3 = 0;
................................................................................
130125 130634         pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
130126 130635     );
130127 130636     sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
130128 130637     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
130129 130638     if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
130130 130639     if( pTabItem->fg.viaCoroutine ){
130131 130640       sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
130132         -    translateColumnToCopy(v, addrTop, pLevel->iTabCur, pTabItem->regResult, 1);
       130641  +    testcase( pParse->db->mallocFailed );
       130642  +    translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
       130643  +                          pTabItem->regResult, 1);
130133 130644       sqlite3VdbeGoto(v, addrTop);
130134 130645       pTabItem->fg.viaCoroutine = 0;
130135 130646     }else{
130136 130647       sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
130137 130648     }
130138 130649     sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
130139 130650     sqlite3VdbeJumpHere(v, addrTop);
................................................................................
131710 132221        && (eOp & (WO_IS|WO_ISNULL))!=0
131711 132222       ){
131712 132223         testcase( eOp & WO_IS );
131713 132224         testcase( eOp & WO_ISNULL );
131714 132225         continue;
131715 132226       }
131716 132227   
       132228  +    if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
       132229  +      pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE;
       132230  +    }else{
       132231  +      pBuilder->bldFlags |= SQLITE_BLDF_INDEXED;
       132232  +    }
131717 132233       pNew->wsFlags = saved_wsFlags;
131718 132234       pNew->u.btree.nEq = saved_nEq;
131719 132235       pNew->u.btree.nBtm = saved_nBtm;
131720 132236       pNew->u.btree.nTop = saved_nTop;
131721 132237       pNew->nLTerm = saved_nLTerm;
131722 132238       if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
131723 132239       pNew->aLTerm[pNew->nLTerm++] = pTerm;
................................................................................
132257 132773           whereLoopOutputAdjust(pWC, pNew, rSize);
132258 132774           rc = whereLoopInsert(pBuilder, pNew);
132259 132775           pNew->nOut = rSize;
132260 132776           if( rc ) break;
132261 132777         }
132262 132778       }
132263 132779   
       132780  +    pBuilder->bldFlags = 0;
132264 132781       rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
       132782  +    if( pBuilder->bldFlags==SQLITE_BLDF_INDEXED ){
       132783  +      /* If a non-unique index is used, or if a prefix of the key for
       132784  +      ** unique index is used (making the index functionally non-unique)
       132785  +      ** then the sqlite_stat1 data becomes important for scoring the
       132786  +      ** plan */
       132787  +      pTab->tabFlags |= TF_StatsUsed;
       132788  +    }
132265 132789   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
132266 132790       sqlite3Stat4ProbeFree(pBuilder->pRec);
132267 132791       pBuilder->nRecValid = 0;
132268 132792       pBuilder->pRec = 0;
132269 132793   #endif
132270 132794   
132271 132795       /* If there was an INDEXED BY clause, then only that one index is
................................................................................
133437 133961     }
133438 133962     if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
133439 133963      && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
133440 133964      && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
133441 133965      && nRowEst
133442 133966     ){
133443 133967       Bitmask notUsed;
133444         -    int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pDistinctSet, pFrom,
       133968  +    int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
133445 133969                    WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
133446         -    if( rc==pWInfo->pDistinctSet->nExpr ){
       133970  +    if( rc==pWInfo->pResultSet->nExpr ){
133447 133971         pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
133448 133972       }
133449 133973     }
133450 133974     if( pWInfo->pOrderBy ){
133451 133975       if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
133452 133976         if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
133453 133977           pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
................................................................................
133676 134200   ** used.
133677 134201   */
133678 134202   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
133679 134203     Parse *pParse,          /* The parser context */
133680 134204     SrcList *pTabList,      /* FROM clause: A list of all tables to be scanned */
133681 134205     Expr *pWhere,           /* The WHERE clause */
133682 134206     ExprList *pOrderBy,     /* An ORDER BY (or GROUP BY) clause, or NULL */
133683         -  ExprList *pDistinctSet, /* Try not to output two rows that duplicate these */
       134207  +  ExprList *pResultSet,   /* Query result set.  Req'd for DISTINCT */
133684 134208     u16 wctrlFlags,         /* The WHERE_* flags defined in sqliteInt.h */
133685 134209     int iAuxArg             /* If WHERE_OR_SUBCLAUSE is set, index cursor number
133686 134210                             ** If WHERE_USE_LIMIT, then the limit amount */
133687 134211   ){
133688 134212     int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
133689 134213     int nTabList;              /* Number of elements in pTabList */
133690 134214     WhereInfo *pWInfo;         /* Will become the return value of this function */
................................................................................
133752 134276       sqlite3DbFree(db, pWInfo);
133753 134277       pWInfo = 0;
133754 134278       goto whereBeginError;
133755 134279     }
133756 134280     pWInfo->pParse = pParse;
133757 134281     pWInfo->pTabList = pTabList;
133758 134282     pWInfo->pOrderBy = pOrderBy;
133759         -  pWInfo->pDistinctSet = pDistinctSet;
       134283  +  pWInfo->pResultSet = pResultSet;
133760 134284     pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
133761 134285     pWInfo->nLevel = nTabList;
133762 134286     pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v);
133763 134287     pWInfo->wctrlFlags = wctrlFlags;
133764 134288     pWInfo->iLimit = iAuxArg;
133765 134289     pWInfo->savedNQueryLoop = pParse->nQueryLoop;
133766 134290     memset(&pWInfo->nOBSat, 0, 
................................................................................
133830 134354   #endif
133831 134355   
133832 134356     /* Analyze all of the subexpressions. */
133833 134357     sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
133834 134358     if( db->mallocFailed ) goto whereBeginError;
133835 134359   
133836 134360     if( wctrlFlags & WHERE_WANT_DISTINCT ){
133837         -    if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pDistinctSet) ){
       134361  +    if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
133838 134362         /* The DISTINCT marking is pointless.  Ignore it. */
133839 134363         pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
133840 134364       }else if( pOrderBy==0 ){
133841 134365         /* Try to ORDER BY the result set to make distinct processing easier */
133842 134366         pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
133843         -      pWInfo->pOrderBy = pDistinctSet;
       134367  +      pWInfo->pOrderBy = pResultSet;
133844 134368       }
133845 134369     }
133846 134370   
133847 134371     /* Construct the WhereLoop objects */
133848 134372   #if defined(WHERETRACE_ENABLED)
133849 134373     if( sqlite3WhereTrace & 0xffff ){
133850 134374       sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
................................................................................
133912 134436       for(ii=0; ii<pWInfo->nLevel; ii++){
133913 134437         whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
133914 134438       }
133915 134439     }
133916 134440   #endif
133917 134441     /* Attempt to omit tables from the join that do not effect the result */
133918 134442     if( pWInfo->nLevel>=2
133919         -   && pDistinctSet!=0
       134443  +   && pResultSet!=0
133920 134444      && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
133921 134445     ){
133922         -    Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pDistinctSet);
       134446  +    Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
133923 134447       if( sWLB.pOrderBy ){
133924 134448         tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
133925 134449       }
133926 134450       while( pWInfo->nLevel>=2 ){
133927 134451         WhereTerm *pTerm, *pEnd;
133928 134452         pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
133929 134453         if( (pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 ) break;
................................................................................
134230 134754       assert( pTab!=0 );
134231 134755       pLoop = pLevel->pWLoop;
134232 134756   
134233 134757       /* For a co-routine, change all OP_Column references to the table of
134234 134758       ** the co-routine into OP_Copy of result contained in a register.
134235 134759       ** OP_Rowid becomes OP_Null.
134236 134760       */
134237         -    if( pTabItem->fg.viaCoroutine && !db->mallocFailed ){
134238         -      translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur,
       134761  +    if( pTabItem->fg.viaCoroutine ){
       134762  +      testcase( pParse->db->mallocFailed );
       134763  +      translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
134239 134764                               pTabItem->regResult, 0);
134240 134765         continue;
134241 134766       }
134242 134767   
134243 134768       /* If this scan uses an index, make VDBE code substitutions to read data
134244 134769       ** from the index instead of from the table where possible.  In some cases
134245 134770       ** this optimization prevents the table from ever being read, which can
................................................................................
134698 135223   **                     shifting non-terminals after a reduce.
134699 135224   **  yy_default[]       Default action for each state.
134700 135225   **
134701 135226   *********** Begin parsing tables **********************************************/
134702 135227   #define YY_ACTTAB_COUNT (1567)
134703 135228   static const YYACTIONTYPE yy_action[] = {
134704 135229    /*     0 */   325,  832,  351,  825,    5,  203,  203,  819,   99,  100,
134705         - /*    10 */    90,  842,  842,  854,  857,  846,  846,   97,   97,   98,
       135230  + /*    10 */    90,  978,  978,  853,  856,  845,  845,   97,   97,   98,
134706 135231    /*    20 */    98,   98,   98,  301,   96,   96,   96,   96,   95,   95,
134707         - /*    30 */    94,   94,   94,   93,  351,  325,  977,  977,  824,  824,
134708         - /*    40 */   826,  947,  354,   99,  100,   90,  842,  842,  854,  857,
134709         - /*    50 */   846,  846,   97,   97,   98,   98,   98,   98,  338,   96,
       135232  + /*    30 */    94,   94,   94,   93,  351,  325,  976,  976,  824,  824,
       135233  + /*    40 */   826,  946,  354,   99,  100,   90,  978,  978,  853,  856,
       135234  + /*    50 */   845,  845,   97,   97,   98,   98,   98,   98,  338,   96,
134710 135235    /*    60 */    96,   96,   96,   95,   95,   94,   94,   94,   93,  351,
134711         - /*    70 */    95,   95,   94,   94,   94,   93,  351,  791,  977,  977,
       135236  + /*    70 */    95,   95,   94,   94,   94,   93,  351,  791,  976,  976,
134712 135237    /*    80 */   325,   94,   94,   94,   93,  351,  792,   75,   99,  100,
134713         - /*    90 */    90,  842,  842,  854,  857,  846,  846,   97,   97,   98,
       135238  + /*    90 */    90,  978,  978,  853,  856,  845,  845,   97,   97,   98,
134714 135239    /*   100 */    98,   98,   98,  450,   96,   96,   96,   96,   95,   95,
134715 135240    /*   110 */    94,   94,   94,   93,  351, 1333,  155,  155,    2,  325,
134716 135241    /*   120 */   275,  146,  132,   52,   52,   93,  351,   99,  100,   90,
134717         - /*   130 */   842,  842,  854,  857,  846,  846,   97,   97,   98,   98,
       135242  + /*   130 */   978,  978,  853,  856,  845,  845,   97,   97,   98,   98,
134718 135243    /*   140 */    98,   98,  101,   96,   96,   96,   96,   95,   95,   94,
134719         - /*   150 */    94,   94,   93,  351,  958,  958,  325,  268,  428,  413,
134720         - /*   160 */   411,   61,  752,  752,   99,  100,   90,  842,  842,  854,
134721         - /*   170 */   857,  846,  846,   97,   97,   98,   98,   98,   98,   60,
       135244  + /*   150 */    94,   94,   93,  351,  957,  957,  325,  268,  428,  413,
       135245  + /*   160 */   411,   61,  752,  752,   99,  100,   90,  978,  978,  853,
       135246  + /*   170 */   856,  845,  845,   97,   97,   98,   98,   98,   98,   60,
134722 135247    /*   180 */    96,   96,   96,   96,   95,   95,   94,   94,   94,   93,
134723         - /*   190 */   351,  325,  270,  329,  273,  277,  959,  960,  250,   99,
134724         - /*   200 */   100,   90,  842,  842,  854,  857,  846,  846,   97,   97,
       135248  + /*   190 */   351,  325,  270,  329,  273,  277,  958,  959,  250,   99,
       135249  + /*   200 */   100,   90,  978,  978,  853,  856,  845,  845,   97,   97,
134725 135250    /*   210 */    98,   98,   98,   98,  301,   96,   96,   96,   96,   95,
134726         - /*   220 */    95,   94,   94,   94,   93,  351,  325,  938, 1326,  698,
134727         - /*   230 */   706, 1326,  242,  412,   99,  100,   90,  842,  842,  854,
134728         - /*   240 */   857,  846,  846,   97,   97,   98,   98,   98,   98,  347,
       135251  + /*   220 */    95,   94,   94,   94,   93,  351,  325,  937, 1326,  698,
       135252  + /*   230 */   706, 1326,  242,  412,   99,  100,   90,  978,  978,  853,
       135253  + /*   240 */   856,  845,  845,   97,   97,   98,   98,   98,   98,  347,
134729 135254    /*   250 */    96,   96,   96,   96,   95,   95,   94,   94,   94,   93,
134730         - /*   260 */   351,  325,  938, 1327,  384,  699, 1327,  381,  379,   99,
134731         - /*   270 */   100,   90,  842,  842,  854,  857,  846,  846,   97,   97,
       135255  + /*   260 */   351,  325,  937, 1327,  384,  699, 1327,  381,  379,   99,
       135256  + /*   270 */   100,   90,  978,  978,  853,  856,  845,  845,   97,   97,
134732 135257    /*   280 */    98,   98,   98,   98,  701,   96,   96,   96,   96,   95,
134733 135258    /*   290 */    95,   94,   94,   94,   93,  351,  325,   92,   89,  178,
134734         - /*   300 */   833,  936,  373,  700,   99,  100,   90,  842,  842,  854,
134735         - /*   310 */   857,  846,  846,   97,   97,   98,   98,   98,   98,  375,
       135259  + /*   300 */   833,  935,  373,  700,   99,  100,   90,  978,  978,  853,
       135260  + /*   310 */   856,  845,  845,   97,   97,   98,   98,   98,   98,  375,
134736 135261    /*   320 */    96,   96,   96,   96,   95,   95,   94,   94,   94,   93,
134737         - /*   330 */   351,  325, 1276,  947,  354,  818,  936,  739,  739,   99,
134738         - /*   340 */   100,   90,  842,  842,  854,  857,  846,  846,   97,   97,
       135262  + /*   330 */   351,  325, 1275,  946,  354,  818,  935,  739,  739,   99,
       135263  + /*   340 */   100,   90,  978,  978,  853,  856,  845,  845,   97,   97,
134739 135264    /*   350 */    98,   98,   98,   98,  230,   96,   96,   96,   96,   95,
134740         - /*   360 */    95,   94,   94,   94,   93,  351,  325,  969,  227,   92,
134741         - /*   370 */    89,  178,  373,  300,   99,  100,   90,  842,  842,  854,
134742         - /*   380 */   857,  846,  846,   97,   97,   98,   98,   98,   98,  921,
       135265  + /*   360 */    95,   94,   94,   94,   93,  351,  325,  968,  227,   92,
       135266  + /*   370 */    89,  178,  373,  300,   99,  100,   90,  978,  978,  853,
       135267  + /*   380 */   856,  845,  845,   97,   97,   98,   98,   98,   98,  920,
134743 135268    /*   390 */    96,   96,   96,   96,   95,   95,   94,   94,   94,   93,
134744 135269    /*   400 */   351,  325,  449,  447,  447,  447,  147,  737,  737,   99,
134745         - /*   410 */   100,   90,  842,  842,  854,  857,  846,  846,   97,   97,
       135270  + /*   410 */   100,   90,  978,  978,  853,  856,  845,  845,   97,   97,
134746 135271    /*   420 */    98,   98,   98,   98,  296,   96,   96,   96,   96,   95,
134747         - /*   430 */    95,   94,   94,   94,   93,  351,  325,  419,  231,  958,
134748         - /*   440 */   958,  158,   25,  422,   99,  100,   90,  842,  842,  854,
134749         - /*   450 */   857,  846,  846,   97,   97,   98,   98,   98,   98,  450,
       135272  + /*   430 */    95,   94,   94,   94,   93,  351,  325,  419,  231,  957,
       135273  + /*   440 */   957,  158,   25,  422,   99,  100,   90,  978,  978,  853,
       135274  + /*   450 */   856,  845,  845,   97,   97,   98,   98,   98,   98,  450,
134750 135275    /*   460 */    96,   96,   96,   96,   95,   95,   94,   94,   94,   93,
134751         - /*   470 */   351,  443,  224,  224,  420,  958,  958,  962,  325,   52,
134752         - /*   480 */    52,  959,  960,  176,  415,   78,   99,  100,   90,  842,
134753         - /*   490 */   842,  854,  857,  846,  846,   97,   97,   98,   98,   98,
       135276  + /*   470 */   351,  443,  224,  224,  420,  957,  957,  961,  325,   52,
       135277  + /*   480 */    52,  958,  959,  176,  415,   78,   99,  100,   90,  978,
       135278  + /*   490 */   978,  853,  856,  845,  845,   97,   97,   98,   98,   98,
134754 135279    /*   500 */    98,  379,   96,   96,   96,   96,   95,   95,   94,   94,
134755         - /*   510 */    94,   93,  351,  325,  428,  418,  298,  959,  960,  962,
134756         - /*   520 */    81,   99,   88,   90,  842,  842,  854,  857,  846,  846,
       135280  + /*   510 */    94,   93,  351,  325,  428,  418,  298,  958,  959,  961,
       135281  + /*   520 */    81,   99,   88,   90,  978,  978,  853,  856,  845,  845,
134757 135282    /*   530 */    97,   97,   98,   98,   98,   98,  717,   96,   96,   96,
134758         - /*   540 */    96,   95,   95,   94,   94,   94,   93,  351,  325,  843,
134759         - /*   550 */   843,  855,  858,  996,  318,  343,  379,  100,   90,  842,
134760         - /*   560 */   842,  854,  857,  846,  846,   97,   97,   98,   98,   98,
       135283  + /*   540 */    96,   95,   95,   94,   94,   94,   93,  351,  325,  842,
       135284  + /*   550 */   842,  854,  857,  996,  318,  343,  379,  100,   90,  978,
       135285  + /*   560 */   978,  853,  856,  845,  845,   97,   97,   98,   98,   98,
134761 135286    /*   570 */    98,  450,   96,   96,   96,   96,   95,   95,   94,   94,
134762 135287    /*   580 */    94,   93,  351,  325,  350,  350,  350,  260,  377,  340,
134763         - /*   590 */   929,   52,   52,   90,  842,  842,  854,  857,  846,  846,
       135288  + /*   590 */   928,   52,   52,   90,  978,  978,  853,  856,  845,  845,
134764 135289    /*   600 */    97,   97,   98,   98,   98,   98,  361,   96,   96,   96,
134765 135290    /*   610 */    96,   95,   95,   94,   94,   94,   93,  351,   86,  445,
134766         - /*   620 */   847,    3, 1203,  361,  360,  378,  344,  813,  958,  958,
134767         - /*   630 */  1300,   86,  445,  729,    3,  212,  169,  287,  405,  282,
       135291  + /*   620 */   846,    3, 1202,  361,  360,  378,  344,  813,  957,  957,
       135292  + /*   630 */  1299,   86,  445,  729,    3,  212,  169,  287,  405,  282,
134768 135293    /*   640 */   404,  199,  232,  450,  300,  760,   83,   84,  280,  245,
134769 135294    /*   650 */   262,  365,  251,   85,  352,  352,   92,   89,  178,   83,
134770 135295    /*   660 */    84,  242,  412,   52,   52,  448,   85,  352,  352,  246,
134771         - /*   670 */   959,  960,  194,  455,  670,  402,  399,  398,  448,  243,
       135296  + /*   670 */   958,  959,  194,  455,  670,  402,  399,  398,  448,  243,
134772 135297    /*   680 */   221,  114,  434,  776,  361,  450,  397,  268,  747,  224,
134773 135298    /*   690 */   224,  132,  132,  198,  832,  434,  452,  451,  428,  427,
134774 135299    /*   700 */   819,  415,  734,  713,  132,   52,   52,  832,  268,  452,
134775         - /*   710 */   451,  734,  194,  819,  363,  402,  399,  398,  450, 1271,
134776         - /*   720 */  1271,   23,  958,  958,   86,  445,  397,    3,  228,  429,
134777         - /*   730 */   895,  824,  824,  826,  827,   19,  203,  720,   52,   52,
       135300  + /*   710 */   451,  734,  194,  819,  363,  402,  399,  398,  450, 1270,
       135301  + /*   720 */  1270,   23,  957,  957,   86,  445,  397,    3,  228,  429,
       135302  + /*   730 */   894,  824,  824,  826,  827,   19,  203,  720,   52,   52,
134778 135303    /*   740 */   428,  408,  439,  249,  824,  824,  826,  827,   19,  229,
134779 135304    /*   750 */   403,  153,   83,   84,  761,  177,  241,  450,  721,   85,
134780         - /*   760 */   352,  352,  120,  157,  959,  960,   58,  977,  409,  355,
       135305  + /*   760 */   352,  352,  120,  157,  958,  959,   58,  976,  409,  355,
134781 135306    /*   770 */   330,  448,  268,  428,  430,  320,  790,   32,   32,   86,
134782 135307    /*   780 */   445,  776,    3,  341,   98,   98,   98,   98,  434,   96,
134783 135308    /*   790 */    96,   96,   96,   95,   95,   94,   94,   94,   93,  351,
134784         - /*   800 */   832,  120,  452,  451,  813,  887,  819,   83,   84,  977,
134785         - /*   810 */   813,  132,  410,  920,   85,  352,  352,  132,  407,  789,
134786         - /*   820 */   958,  958,   92,   89,  178,  917,  448,  262,  370,  261,
134787         - /*   830 */    82,  914,   80,  262,  370,  261,  776,  824,  824,  826,
134788         - /*   840 */   827,   19,  934,  434,   96,   96,   96,   96,   95,   95,
134789         - /*   850 */    94,   94,   94,   93,  351,  832,   74,  452,  451,  958,
134790         - /*   860 */   958,  819,  959,  960,  120,   92,   89,  178,  945,    2,
134791         - /*   870 */   918,  965,  268,    1,  976,   76,  445,  762,    3,  708,
134792         - /*   880 */   901,  901,  387,  958,  958,  757,  919,  371,  740,  778,
       135309  + /*   800 */   832,  120,  452,  451,  813,  886,  819,   83,   84,  976,
       135310  + /*   810 */   813,  132,  410,  919,   85,  352,  352,  132,  407,  789,
       135311  + /*   820 */   957,  957,   92,   89,  178,  916,  448,  262,  370,  261,
       135312  + /*   830 */    82,  913,   80,  262,  370,  261,  776,  824,  824,  826,
       135313  + /*   840 */   827,   19,  933,  434,   96,   96,   96,   96,   95,   95,
       135314  + /*   850 */    94,   94,   94,   93,  351,  832,   74,  452,  451,  957,
       135315  + /*   860 */   957,  819,  958,  959,  120,   92,   89,  178,  944,    2,
       135316  + /*   870 */   917,  964,  268,    1,  975,   76,  445,  762,    3,  708,
       135317  + /*   880 */   900,  900,  387,  957,  957,  757,  918,  371,  740,  778,
134793 135318    /*   890 */   756,  257,  824,  824,  826,  827,   19,  417,  741,  450,
134794         - /*   900 */    24,  959,  960,   83,   84,  369,  958,  958,  177,  226,
134795         - /*   910 */    85,  352,  352,  885,  315,  314,  313,  215,  311,   10,
134796         - /*   920 */    10,  683,  448,  349,  348,  959,  960,  909,  777,  157,
134797         - /*   930 */   120,  958,  958,  337,  776,  416,  711,  310,  450,  434,
134798         - /*   940 */   450,  321,  450,  791,  103,  200,  175,  450,  959,  960,
134799         - /*   950 */   908,  832,  792,  452,  451,    9,    9,  819,   10,   10,
       135319  + /*   900 */    24,  958,  959,   83,   84,  369,  957,  957,  177,  226,
       135320  + /*   910 */    85,  352,  352,  884,  315,  314,  313,  215,  311,   10,
       135321  + /*   920 */    10,  683,  448,  349,  348,  958,  959,  908,  777,  157,
       135322  + /*   930 */   120,  957,  957,  337,  776,  416,  711,  310,  450,  434,
       135323  + /*   940 */   450,  321,  450,  791,  103,  200,  175,  450,  958,  959,
       135324  + /*   950 */   907,  832,  792,  452,  451,    9,    9,  819,   10,   10,
134800 135325    /*   960 */    52,   52,   51,   51,  180,  716,  248,   10,   10,  171,
134801         - /*   970 */   170,  167,  339,  959,  960,  247,  984,  702,  702,  450,
134802         - /*   980 */   715,  233,  686,  982,  889,  983,  182,  914,  824,  824,
       135326  + /*   970 */   170,  167,  339,  958,  959,  247,  984,  702,  702,  450,
       135327  + /*   980 */   715,  233,  686,  982,  888,  983,  182,  913,  824,  824,
134803 135328    /*   990 */   826,  827,   19,  183,  256,  423,  132,  181,  394,   10,
134804         - /*  1000 */    10,  889,  891,  749,  958,  958,  917,  268,  985,  198,
       135329  + /*  1000 */    10,  888,  890,  749,  957,  957,  916,  268,  985,  198,
134805 135330    /*  1010 */   985,  349,  348,  425,  415,  299,  817,  832,  326,  825,
134806 135331    /*  1020 */   120,  332,  133,  819,  268,   98,   98,   98,   98,   91,
134807 135332    /*  1030 */    96,   96,   96,   96,   95,   95,   94,   94,   94,   93,
134808         - /*  1040 */   351,  157,  810,  371,  382,  359,  959,  960,  358,  268,
134809         - /*  1050 */   450,  918,  368,  324,  824,  824,  826,  450,  709,  450,
134810         - /*  1060 */   264,  380,  889,  450,  877,  746,  253,  919,  255,  433,
       135333  + /*  1040 */   351,  157,  810,  371,  382,  359,  958,  959,  358,  268,
       135334  + /*  1050 */   450,  917,  368,  324,  824,  824,  826,  450,  709,  450,
       135335  + /*  1060 */   264,  380,  888,  450,  876,  746,  253,  918,  255,  433,
134811 135336    /*  1070 */    36,   36,  234,  450,  234,  120,  269,   37,   37,   12,
134812 135337    /*  1080 */    12,  334,  272,   27,   27,  450,  330,  118,  450,  162,
134813 135338    /*  1090 */   742,  280,  450,   38,   38,  450,  985,  356,  985,  450,
134814         - /*  1100 */   709, 1210,  450,  132,  450,   39,   39,  450,   40,   40,
       135339  + /*  1100 */   709, 1209,  450,  132,  450,   39,   39,  450,   40,   40,
134815 135340    /*  1110 */   450,  362,   41,   41,  450,   42,   42,  450,  254,   28,
134816 135341    /*  1120 */    28,  450,   29,   29,   31,   31,  450,   43,   43,  450,
134817 135342    /*  1130 */    44,   44,  450,  714,   45,   45,  450,   11,   11,  767,
134818 135343    /*  1140 */   450,   46,   46,  450,  268,  450,  105,  105,  450,   47,
134819 135344    /*  1150 */    47,  450,   48,   48,  450,  237,   33,   33,  450,  172,
134820 135345    /*  1160 */    49,   49,  450,   50,   50,   34,   34,  274,  122,  122,
134821         - /*  1170 */   450,  123,  123,  450,  124,  124,  450,  898,   56,   56,
134822         - /*  1180 */   450,  897,   35,   35,  450,  267,  450,  817,  450,  817,
       135346  + /*  1170 */   450,  123,  123,  450,  124,  124,  450,  897,   56,   56,
       135347  + /*  1180 */   450,  896,   35,   35,  450,  267,  450,  817,  450,  817,
134823 135348    /*  1190 */   106,  106,  450,   53,   53,  385,  107,  107,  450,  817,
134824 135349    /*  1200 */   108,  108,  817,  450,  104,  104,  121,  121,  119,  119,
134825 135350    /*  1210 */   450,  117,  112,  112,  450,  276,  450,  225,  111,  111,
134826         - /*  1220 */   450,  730,  450,  109,  109,  450,  673,  674,  675,  912,
       135351  + /*  1220 */   450,  730,  450,  109,  109,  450,  673,  674,  675,  911,
134827 135352    /*  1230 */   110,  110,  317,  998,   55,   55,   57,   57,  692,  331,
134828         - /*  1240 */    54,   54,   26,   26,  696,   30,   30,  317,  937,  197,
       135353  + /*  1240 */    54,   54,   26,   26,  696,   30,   30,  317,  936,  197,
134829 135354    /*  1250 */   196,  195,  335,  281,  336,  446,  331,  745,  689,  436,
134830         - /*  1260 */   440,  444,  120,   72,  386,  223,  175,  345,  757,  933,
       135355  + /*  1260 */   440,  444,  120,   72,  386,  223,  175,  345,  757,  932,
134831 135356    /*  1270 */    20,  286,  319,  756,  815,  372,  374,  202,  202,  202,
134832         - /*  1280 */   263,  395,  285,   74,  208,   21,  696,  719,  718,  884,
       135357  + /*  1280 */   263,  395,  285,   74,  208,   21,  696,  719,  718,  883,
134833 135358    /*  1290 */   120,  120,  120,  120,  120,  754,  278,  828,   77,   74,
134834         - /*  1300 */   726,  727,  785,  783,  880,  202,  999,  208,  894,  893,
134835         - /*  1310 */   894,  893,  694,  816,  763,  116,  774, 1290,  431,  432,
       135359  + /*  1300 */   726,  727,  785,  783,  879,  202,  999,  208,  893,  892,
       135360  + /*  1310 */   893,  892,  694,  816,  763,  116,  774, 1289,  431,  432,
134836 135361    /*  1320 */   302,  999,  390,  303,  823,  697,  691,  680,  159,  289,
134837         - /*  1330 */   679,  884,  681,  952,  291,  218,  293,    7,  316,  828,
134838         - /*  1340 */   173,  805,  259,  364,  252,  911,  376,  713,  295,  435,
134839         - /*  1350 */   308,  168,  955,  993,  135,  400,  990,  284,  882,  881,
134840         - /*  1360 */   205,  928,  926,   59,  333,   62,  144,  156,  130,   72,
       135362  + /*  1330 */   679,  883,  681,  951,  291,  218,  293,    7,  316,  828,
       135363  + /*  1340 */   173,  805,  259,  364,  252,  910,  376,  713,  295,  435,
       135364  + /*  1350 */   308,  168,  954,  993,  135,  400,  990,  284,  881,  880,
       135365  + /*  1360 */   205,  927,  925,   59,  333,   62,  144,  156,  130,   72,
134841 135366    /*  1370 */   802,  366,  367,  393,  137,  185,  189,  160,  139,  383,
134842         - /*  1380 */    67,  896,  140,  141,  142,  148,  389,  812,  775,  266,
134843         - /*  1390 */   219,  190,  154,  391,  913,  876,  271,  406,  191,  322,
       135367  + /*  1380 */    67,  895,  140,  141,  142,  148,  389,  812,  775,  266,
       135368  + /*  1390 */   219,  190,  154,  391,  912,  875,  271,  406,  191,  322,
134844 135369    /*  1400 */   682,  733,  192,  342,  732,  724,  731,  711,  723,  421,
134845 135370    /*  1410 */   705,   71,  323,    6,  204,  771,  288,   79,  297,  346,
134846         - /*  1420 */   772,  704,  290,  283,  703,  770,  292,  294,  967,  239,
134847         - /*  1430 */   769,  102,  862,  438,  426,  240,  424,  442,   73,  213,
134848         - /*  1440 */   688,  238,   22,  453,  953,  214,  217,  216,  454,  677,
       135371  + /*  1420 */   772,  704,  290,  283,  703,  770,  292,  294,  966,  239,
       135372  + /*  1430 */   769,  102,  861,  438,  426,  240,  424,  442,   73,  213,
       135373  + /*  1440 */   688,  238,   22,  453,  952,  214,  217,  216,  454,  677,
134849 135374    /*  1450 */   676,  671,  753,  125,  115,  235,  126,  669,  353,  166,
134850         - /*  1460 */   127,  244,  179,  357,  306,  304,  305,  307,  113,  892,
134851         - /*  1470 */   327,  890,  811,  328,  134,  128,  136,  138,  743,  258,
134852         - /*  1480 */   907,  184,  143,  129,  910,  186,   63,   64,  145,  187,
134853         - /*  1490 */   906,   65,    8,   66,   13,  188,  202,  899,  265,  149,
       135375  + /*  1460 */   127,  244,  179,  357,  306,  304,  305,  307,  113,  891,
       135376  + /*  1470 */   327,  889,  811,  328,  134,  128,  136,  138,  743,  258,
       135377  + /*  1480 */   906,  184,  143,  129,  909,  186,   63,   64,  145,  187,
       135378  + /*  1490 */   905,   65,    8,   66,   13,  188,  202,  898,  265,  149,
134854 135379    /*  1500 */   987,  388,  150,  685,  161,  392,  285,  193,  279,  396,
134855 135380    /*  1510 */   151,  401,   68,   14,   15,  722,   69,  236,  831,  131,
134856         - /*  1520 */   830,  860,   70,  751,   16,  414,  755,    4,  174,  220,
134857         - /*  1530 */   222,  784,  201,  152,  779,   77,   74,   17,   18,  875,
134858         - /*  1540 */   861,  859,  916,  864,  915,  207,  206,  942,  163,  437,
134859         - /*  1550 */   948,  943,  164,  209, 1002,  441,  863,  165,  210,  829,
134860         - /*  1560 */   695,   87,  312,  211, 1292, 1291,  309,
       135381  + /*  1520 */   830,  859,   70,  751,   16,  414,  755,    4,  174,  220,
       135382  + /*  1530 */   222,  784,  201,  152,  779,   77,   74,   17,   18,  874,
       135383  + /*  1540 */   860,  858,  915,  863,  914,  207,  206,  941,  163,  437,
       135384  + /*  1550 */   947,  942,  164,  209, 1002,  441,  862,  165,  210,  829,
       135385  + /*  1560 */   695,   87,  312,  211, 1291, 1290,  309,
134861 135386   };
134862 135387   static const YYCODETYPE yy_lookahead[] = {
134863 135388    /*     0 */    19,   95,   53,   97,   22,   24,   24,  101,   27,   28,
134864 135389    /*    10 */    29,   30,   31,   32,   33,   34,   35,   36,   37,   38,
134865 135390    /*    20 */    39,   40,   41,  152,   43,   44,   45,   46,   47,   48,
134866 135391    /*    30 */    49,   50,   51,   52,   53,   19,   55,   55,  132,  133,
134867 135392    /*    40 */   134,    1,    2,   27,   28,   29,   30,   31,   32,   33,
................................................................................
135106 135631    /*   280 */  1223, 1235, 1236, 1245, 1249, 1226, 1250, 1254, 1199, 1201,
135107 135632    /*   290 */  1204, 1207, 1209, 1211, 1214, 1212, 1255, 1208, 1259, 1215,
135108 135633    /*   300 */  1256, 1200, 1206, 1260, 1247, 1261, 1263, 1262, 1266, 1278,
135109 135634    /*   310 */  1282, 1292, 1294, 1297, 1298, 1299, 1300, 1221, 1224, 1228,
135110 135635    /*   320 */  1288, 1291, 1276, 1277, 1295,
135111 135636   };
135112 135637   static const YYACTIONTYPE yy_default[] = {
135113         - /*     0 */  1281, 1271, 1271, 1271, 1203, 1203, 1203, 1203, 1271, 1096,
135114         - /*    10 */  1125, 1125, 1255, 1332, 1332, 1332, 1332, 1332, 1332, 1202,
135115         - /*    20 */  1332, 1332, 1332, 1332, 1271, 1100, 1131, 1332, 1332, 1332,
135116         - /*    30 */  1332, 1204, 1205, 1332, 1332, 1332, 1254, 1256, 1141, 1140,
135117         - /*    40 */  1139, 1138, 1237, 1112, 1136, 1129, 1133, 1204, 1198, 1199,
135118         - /*    50 */  1197, 1201, 1205, 1332, 1132, 1167, 1182, 1166, 1332, 1332,
       135638  + /*     0 */  1280, 1270, 1270, 1270, 1202, 1202, 1202, 1202, 1270, 1096,
       135639  + /*    10 */  1125, 1125, 1254, 1332, 1332, 1332, 1332, 1332, 1332, 1201,
       135640  + /*    20 */  1332, 1332, 1332, 1332, 1270, 1100, 1131, 1332, 1332, 1332,
       135641  + /*    30 */  1332, 1203, 1204, 1332, 1332, 1332, 1253, 1255, 1141, 1140,
       135642  + /*    40 */  1139, 1138, 1236, 1112, 1136, 1129, 1133, 1203, 1197, 1198,
       135643  + /*    50 */  1196, 1200, 1204, 1332, 1132, 1167, 1181, 1166, 1332, 1332,
135119 135644    /*    60 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135120 135645    /*    70 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135121 135646    /*    80 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135122 135647    /*    90 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135123         - /*   100 */  1332, 1332, 1332, 1332, 1176, 1181, 1188, 1180, 1177, 1169,
       135648  + /*   100 */  1332, 1332, 1332, 1332, 1175, 1180, 1187, 1179, 1176, 1169,
135124 135649    /*   110 */  1168, 1170, 1171, 1332, 1019, 1067, 1332, 1332, 1332, 1172,
135125         - /*   120 */  1332, 1173, 1185, 1184, 1183, 1262, 1289, 1288, 1332, 1332,
       135650  + /*   120 */  1332, 1173, 1184, 1183, 1182, 1261, 1288, 1287, 1332, 1332,
135126 135651    /*   130 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135127 135652    /*   140 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135128         - /*   150 */  1332, 1332, 1332, 1332, 1332, 1281, 1271, 1025, 1025, 1332,
135129         - /*   160 */  1271, 1271, 1271, 1271, 1271, 1271, 1267, 1100, 1091, 1332,
       135653  + /*   150 */  1332, 1332, 1332, 1332, 1332, 1280, 1270, 1025, 1025, 1332,
       135654  + /*   160 */  1270, 1270, 1270, 1270, 1270, 1270, 1266, 1100, 1091, 1332,
135130 135655    /*   170 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135131         - /*   180 */  1259, 1257, 1332, 1218, 1332, 1332, 1332, 1332, 1332, 1332,
       135656  + /*   180 */  1258, 1256, 1332, 1217, 1332, 1332, 1332, 1332, 1332, 1332,
135132 135657    /*   190 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135133 135658    /*   200 */  1332, 1332, 1332, 1332, 1096, 1332, 1332, 1332, 1332, 1332,
135134         - /*   210 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1283, 1332, 1232,
       135659  + /*   210 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1282, 1332, 1231,
135135 135660    /*   220 */  1096, 1096, 1096, 1098, 1080, 1090, 1004, 1135, 1114, 1114,
135136         - /*   230 */  1321, 1135, 1321, 1042, 1303, 1039, 1125, 1114, 1200, 1125,
       135661  + /*   230 */  1321, 1135, 1321, 1042, 1302, 1039, 1125, 1114, 1199, 1125,
135137 135662    /*   240 */  1125, 1097, 1090, 1332, 1324, 1105, 1105, 1323, 1323, 1105,
135138 135663    /*   250 */  1146, 1070, 1135, 1076, 1076, 1076, 1076, 1105, 1016, 1135,
135139         - /*   260 */  1146, 1070, 1070, 1135, 1105, 1016, 1236, 1318, 1105, 1105,
135140         - /*   270 */  1016, 1211, 1105, 1016, 1105, 1016, 1211, 1068, 1068, 1068,
135141         - /*   280 */  1057, 1211, 1068, 1042, 1068, 1057, 1068, 1068, 1118, 1113,
135142         - /*   290 */  1118, 1113, 1118, 1113, 1118, 1113, 1105, 1206, 1105, 1332,
135143         - /*   300 */  1211, 1215, 1215, 1211, 1130, 1119, 1128, 1126, 1135, 1022,
135144         - /*   310 */  1060, 1286, 1286, 1282, 1282, 1282, 1282, 1329, 1329, 1267,
135145         - /*   320 */  1298, 1298, 1044, 1044, 1298, 1332, 1332, 1332, 1332, 1332,
135146         - /*   330 */  1332, 1293, 1332, 1220, 1332, 1332, 1332, 1332, 1332, 1332,
       135664  + /*   260 */  1146, 1070, 1070, 1135, 1105, 1016, 1235, 1318, 1105, 1105,
       135665  + /*   270 */  1016, 1210, 1105, 1016, 1105, 1016, 1210, 1068, 1068, 1068,
       135666  + /*   280 */  1057, 1210, 1068, 1042, 1068, 1057, 1068, 1068, 1118, 1113,
       135667  + /*   290 */  1118, 1113, 1118, 1113, 1118, 1113, 1105, 1205, 1105, 1332,
       135668  + /*   300 */  1210, 1214, 1214, 1210, 1130, 1119, 1128, 1126, 1135, 1022,
       135669  + /*   310 */  1060, 1285, 1285, 1281, 1281, 1281, 1281, 1329, 1329, 1266,
       135670  + /*   320 */  1297, 1297, 1044, 1044, 1297, 1332, 1332, 1332, 1332, 1332,
       135671  + /*   330 */  1332, 1292, 1332, 1219, 1332, 1332, 1332, 1332, 1332, 1332,
135147 135672    /*   340 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135148         - /*   350 */  1332, 1332, 1152, 1332, 1000, 1264, 1332, 1332, 1263, 1332,
       135673  + /*   350 */  1332, 1332, 1152, 1332, 1000, 1263, 1332, 1332, 1262, 1332,
135149 135674    /*   360 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135150 135675    /*   370 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1320,
135151         - /*   380 */  1332, 1332, 1332, 1332, 1332, 1332, 1235, 1234, 1332, 1332,
       135676  + /*   380 */  1332, 1332, 1332, 1332, 1332, 1332, 1234, 1233, 1332, 1332,
135152 135677    /*   390 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135153 135678    /*   400 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332,
135154         - /*   410 */  1332, 1082, 1332, 1332, 1332, 1307, 1332, 1332, 1332, 1332,
       135679  + /*   410 */  1332, 1082, 1332, 1332, 1332, 1306, 1332, 1332, 1332, 1332,
135155 135680    /*   420 */  1332, 1332, 1332, 1127, 1332, 1120, 1332, 1332, 1311, 1332,
135156         - /*   430 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1273,
135157         - /*   440 */  1332, 1332, 1332, 1272, 1332, 1332, 1332, 1332, 1332, 1154,
       135681  + /*   430 */  1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1272,
       135682  + /*   440 */  1332, 1332, 1332, 1271, 1332, 1332, 1332, 1332, 1332, 1154,
135158 135683    /*   450 */  1332, 1153, 1157, 1332, 1010, 1332,
135159 135684   };
135160 135685   /********** End of lemon-generated parsing tables *****************************/
135161 135686   
135162 135687   /* The next table maps tokens (terminal symbols) into fallback tokens.  
135163 135688   ** If a construct like the following:
135164 135689   ** 
................................................................................
135596 136121    /* 167 */ "expr ::= expr OR expr",
135597 136122    /* 168 */ "expr ::= expr LT|GT|GE|LE expr",
135598 136123    /* 169 */ "expr ::= expr EQ|NE expr",
135599 136124    /* 170 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
135600 136125    /* 171 */ "expr ::= expr PLUS|MINUS expr",
135601 136126    /* 172 */ "expr ::= expr STAR|SLASH|REM expr",
135602 136127    /* 173 */ "expr ::= expr CONCAT expr",
135603         - /* 174 */ "likeop ::= LIKE_KW|MATCH",
135604         - /* 175 */ "likeop ::= NOT LIKE_KW|MATCH",
135605         - /* 176 */ "expr ::= expr likeop expr",
135606         - /* 177 */ "expr ::= expr likeop expr ESCAPE expr",
135607         - /* 178 */ "expr ::= expr ISNULL|NOTNULL",
135608         - /* 179 */ "expr ::= expr NOT NULL",
135609         - /* 180 */ "expr ::= expr IS expr",
135610         - /* 181 */ "expr ::= expr IS NOT expr",
135611         - /* 182 */ "expr ::= NOT expr",
135612         - /* 183 */ "expr ::= BITNOT expr",
135613         - /* 184 */ "expr ::= MINUS expr",
135614         - /* 185 */ "expr ::= PLUS expr",
135615         - /* 186 */ "between_op ::= BETWEEN",
135616         - /* 187 */ "between_op ::= NOT BETWEEN",
135617         - /* 188 */ "expr ::= expr between_op expr AND expr",
135618         - /* 189 */ "in_op ::= IN",
135619         - /* 190 */ "in_op ::= NOT IN",
135620         - /* 191 */ "expr ::= expr in_op LP exprlist RP",
135621         - /* 192 */ "expr ::= LP select RP",
135622         - /* 193 */ "expr ::= expr in_op LP select RP",
135623         - /* 194 */ "expr ::= expr in_op nm dbnm paren_exprlist",
135624         - /* 195 */ "expr ::= EXISTS LP select RP",
135625         - /* 196 */ "expr ::= CASE case_operand case_exprlist case_else END",
135626         - /* 197 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
135627         - /* 198 */ "case_exprlist ::= WHEN expr THEN expr",
135628         - /* 199 */ "case_else ::= ELSE expr",
135629         - /* 200 */ "case_else ::=",
135630         - /* 201 */ "case_operand ::= expr",
135631         - /* 202 */ "case_operand ::=",
135632         - /* 203 */ "exprlist ::=",
135633         - /* 204 */ "nexprlist ::= nexprlist COMMA expr",
135634         - /* 205 */ "nexprlist ::= expr",
135635         - /* 206 */ "paren_exprlist ::=",
135636         - /* 207 */ "paren_exprlist ::= LP exprlist RP",
135637         - /* 208 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
135638         - /* 209 */ "uniqueflag ::= UNIQUE",
135639         - /* 210 */ "uniqueflag ::=",
135640         - /* 211 */ "eidlist_opt ::=",
135641         - /* 212 */ "eidlist_opt ::= LP eidlist RP",
135642         - /* 213 */ "eidlist ::= eidlist COMMA nm collate sortorder",
135643         - /* 214 */ "eidlist ::= nm collate sortorder",
135644         - /* 215 */ "collate ::=",
135645         - /* 216 */ "collate ::= COLLATE ID|STRING",
135646         - /* 217 */ "cmd ::= DROP INDEX ifexists fullname",
135647         - /* 218 */ "cmd ::= VACUUM",
135648         - /* 219 */ "cmd ::= VACUUM nm",
135649         - /* 220 */ "cmd ::= PRAGMA nm dbnm",
135650         - /* 221 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
135651         - /* 222 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
135652         - /* 223 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
135653         - /* 224 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
135654         - /* 225 */ "plus_num ::= PLUS INTEGER|FLOAT",
135655         - /* 226 */ "minus_num ::= MINUS INTEGER|FLOAT",
135656         - /* 227 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
135657         - /* 228 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
135658         - /* 229 */ "trigger_time ::= BEFORE",
135659         - /* 230 */ "trigger_time ::= AFTER",
135660         - /* 231 */ "trigger_time ::= INSTEAD OF",
135661         - /* 232 */ "trigger_time ::=",
135662         - /* 233 */ "trigger_event ::= DELETE|INSERT",
135663         - /* 234 */ "trigger_event ::= UPDATE",
135664         - /* 235 */ "trigger_event ::= UPDATE OF idlist",
135665         - /* 236 */ "when_clause ::=",
135666         - /* 237 */ "when_clause ::= WHEN expr",
135667         - /* 238 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
135668         - /* 239 */ "trigger_cmd_list ::= trigger_cmd SEMI",
135669         - /* 240 */ "trnm ::= nm DOT nm",
135670         - /* 241 */ "tridxby ::= INDEXED BY nm",
135671         - /* 242 */ "tridxby ::= NOT INDEXED",
135672         - /* 243 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
135673         - /* 244 */ "trigger_cmd ::= insert_cmd INTO trnm idlist_opt select",
135674         - /* 245 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
135675         - /* 246 */ "trigger_cmd ::= select",
135676         - /* 247 */ "expr ::= RAISE LP IGNORE RP",
135677         - /* 248 */ "expr ::= RAISE LP raisetype COMMA nm RP",
135678         - /* 249 */ "raisetype ::= ROLLBACK",
135679         - /* 250 */ "raisetype ::= ABORT",
135680         - /* 251 */ "raisetype ::= FAIL",
135681         - /* 252 */ "cmd ::= DROP TRIGGER ifexists fullname",
135682         - /* 253 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
135683         - /* 254 */ "cmd ::= DETACH database_kw_opt expr",
135684         - /* 255 */ "key_opt ::=",
135685         - /* 256 */ "key_opt ::= KEY expr",
135686         - /* 257 */ "cmd ::= REINDEX",
135687         - /* 258 */ "cmd ::= REINDEX nm dbnm",
135688         - /* 259 */ "cmd ::= ANALYZE",
135689         - /* 260 */ "cmd ::= ANALYZE nm dbnm",
135690         - /* 261 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
135691         - /* 262 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
135692         - /* 263 */ "add_column_fullname ::= fullname",
135693         - /* 264 */ "cmd ::= create_vtab",
135694         - /* 265 */ "cmd ::= create_vtab LP vtabarglist RP",
135695         - /* 266 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
135696         - /* 267 */ "vtabarg ::=",
135697         - /* 268 */ "vtabargtoken ::= ANY",
135698         - /* 269 */ "vtabargtoken ::= lp anylist RP",
135699         - /* 270 */ "lp ::= LP",
135700         - /* 271 */ "with ::=",
135701         - /* 272 */ "with ::= WITH wqlist",
135702         - /* 273 */ "with ::= WITH RECURSIVE wqlist",
135703         - /* 274 */ "wqlist ::= nm eidlist_opt AS LP select RP",
135704         - /* 275 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
135705         - /* 276 */ "input ::= cmdlist",
135706         - /* 277 */ "cmdlist ::= cmdlist ecmd",
135707         - /* 278 */ "cmdlist ::= ecmd",
135708         - /* 279 */ "ecmd ::= SEMI",
135709         - /* 280 */ "ecmd ::= explain cmdx SEMI",
135710         - /* 281 */ "explain ::=",
135711         - /* 282 */ "trans_opt ::=",
135712         - /* 283 */ "trans_opt ::= TRANSACTION",
135713         - /* 284 */ "trans_opt ::= TRANSACTION nm",
135714         - /* 285 */ "savepoint_opt ::= SAVEPOINT",
135715         - /* 286 */ "savepoint_opt ::=",
135716         - /* 287 */ "cmd ::= create_table create_table_args",
135717         - /* 288 */ "columnlist ::= columnlist COMMA columnname carglist",
135718         - /* 289 */ "columnlist ::= columnname carglist",
135719         - /* 290 */ "nm ::= ID|INDEXED",
135720         - /* 291 */ "nm ::= STRING",
135721         - /* 292 */ "nm ::= JOIN_KW",
135722         - /* 293 */ "typetoken ::= typename",
135723         - /* 294 */ "typename ::= ID|STRING",
135724         - /* 295 */ "signed ::= plus_num",
135725         - /* 296 */ "signed ::= minus_num",
135726         - /* 297 */ "carglist ::= carglist ccons",
135727         - /* 298 */ "carglist ::=",
135728         - /* 299 */ "ccons ::= NULL onconf",
135729         - /* 300 */ "conslist_opt ::= COMMA conslist",
135730         - /* 301 */ "conslist ::= conslist tconscomma tcons",
135731         - /* 302 */ "conslist ::= tcons",
135732         - /* 303 */ "tconscomma ::=",
135733         - /* 304 */ "defer_subclause_opt ::= defer_subclause",
135734         - /* 305 */ "resolvetype ::= raisetype",
135735         - /* 306 */ "selectnowith ::= oneselect",
135736         - /* 307 */ "oneselect ::= values",
135737         - /* 308 */ "sclp ::= selcollist COMMA",
135738         - /* 309 */ "as ::= ID|STRING",
135739         - /* 310 */ "expr ::= term",
       136128  + /* 174 */ "likeop ::= NOT LIKE_KW|MATCH",
       136129  + /* 175 */ "expr ::= expr likeop expr",
       136130  + /* 176 */ "expr ::= expr likeop expr ESCAPE expr",
       136131  + /* 177 */ "expr ::= expr ISNULL|NOTNULL",
       136132  + /* 178 */ "expr ::= expr NOT NULL",
       136133  + /* 179 */ "expr ::= expr IS expr",
       136134  + /* 180 */ "expr ::= expr IS NOT expr",
       136135  + /* 181 */ "expr ::= NOT expr",
       136136  + /* 182 */ "expr ::= BITNOT expr",
       136137  + /* 183 */ "expr ::= MINUS expr",
       136138  + /* 184 */ "expr ::= PLUS expr",
       136139  + /* 185 */ "between_op ::= BETWEEN",
       136140  + /* 186 */ "between_op ::= NOT BETWEEN",
       136141  + /* 187 */ "expr ::= expr between_op expr AND expr",
       136142  + /* 188 */ "in_op ::= IN",
       136143  + /* 189 */ "in_op ::= NOT IN",
       136144  + /* 190 */ "expr ::= expr in_op LP exprlist RP",
       136145  + /* 191 */ "expr ::= LP select RP",
       136146  + /* 192 */ "expr ::= expr in_op LP select RP",
       136147  + /* 193 */ "expr ::= expr in_op nm dbnm paren_exprlist",
       136148  + /* 194 */ "expr ::= EXISTS LP select RP",
       136149  + /* 195 */ "expr ::= CASE case_operand case_exprlist case_else END",
       136150  + /* 196 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
       136151  + /* 197 */ "case_exprlist ::= WHEN expr THEN expr",
       136152  + /* 198 */ "case_else ::= ELSE expr",
       136153  + /* 199 */ "case_else ::=",
       136154  + /* 200 */ "case_operand ::= expr",
       136155  + /* 201 */ "case_operand ::=",
       136156  + /* 202 */ "exprlist ::=",
       136157  + /* 203 */ "nexprlist ::= nexprlist COMMA expr",
       136158  + /* 204 */ "nexprlist ::= expr",
       136159  + /* 205 */ "paren_exprlist ::=",
       136160  + /* 206 */ "paren_exprlist ::= LP exprlist RP",
       136161  + /* 207 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
       136162  + /* 208 */ "uniqueflag ::= UNIQUE",
       136163  + /* 209 */ "uniqueflag ::=",
       136164  + /* 210 */ "eidlist_opt ::=",
       136165  + /* 211 */ "eidlist_opt ::= LP eidlist RP",
       136166  + /* 212 */ "eidlist ::= eidlist COMMA nm collate sortorder",
       136167  + /* 213 */ "eidlist ::= nm collate sortorder",
       136168  + /* 214 */ "collate ::=",
       136169  + /* 215 */ "collate ::= COLLATE ID|STRING",
       136170  + /* 216 */ "cmd ::= DROP INDEX ifexists fullname",
       136171  + /* 217 */ "cmd ::= VACUUM",
       136172  + /* 218 */ "cmd ::= VACUUM nm",
       136173  + /* 219 */ "cmd ::= PRAGMA nm dbnm",
       136174  + /* 220 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
       136175  + /* 221 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
       136176  + /* 222 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
       136177  + /* 223 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
       136178  + /* 224 */ "plus_num ::= PLUS INTEGER|FLOAT",
       136179  + /* 225 */ "minus_num ::= MINUS INTEGER|FLOAT",
       136180  + /* 226 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
       136181  + /* 227 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
       136182  + /* 228 */ "trigger_time ::= BEFORE",
       136183  + /* 229 */ "trigger_time ::= AFTER",
       136184  + /* 230 */ "trigger_time ::= INSTEAD OF",
       136185  + /* 231 */ "trigger_time ::=",
       136186  + /* 232 */ "trigger_event ::= DELETE|INSERT",
       136187  + /* 233 */ "trigger_event ::= UPDATE",
       136188  + /* 234 */ "trigger_event ::= UPDATE OF idlist",
       136189  + /* 235 */ "when_clause ::=",
       136190  + /* 236 */ "when_clause ::= WHEN expr",
       136191  + /* 237 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
       136192  + /* 238 */ "trigger_cmd_list ::= trigger_cmd SEMI",
       136193  + /* 239 */ "trnm ::= nm DOT nm",
       136194  + /* 240 */ "tridxby ::= INDEXED BY nm",
       136195  + /* 241 */ "tridxby ::= NOT INDEXED",
       136196  + /* 242 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
       136197  + /* 243 */ "trigger_cmd ::= insert_cmd INTO trnm idlist_opt select",
       136198  + /* 244 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
       136199  + /* 245 */ "trigger_cmd ::= select",
       136200  + /* 246 */ "expr ::= RAISE LP IGNORE RP",
       136201  + /* 247 */ "expr ::= RAISE LP raisetype COMMA nm RP",
       136202  + /* 248 */ "raisetype ::= ROLLBACK",
       136203  + /* 249 */ "raisetype ::= ABORT",
       136204  + /* 250 */ "raisetype ::= FAIL",
       136205  + /* 251 */ "cmd ::= DROP TRIGGER ifexists fullname",
       136206  + /* 252 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
       136207  + /* 253 */ "cmd ::= DETACH database_kw_opt expr",
       136208  + /* 254 */ "key_opt ::=",
       136209  + /* 255 */ "key_opt ::= KEY expr",
       136210  + /* 256 */ "cmd ::= REINDEX",
       136211  + /* 257 */ "cmd ::= REINDEX nm dbnm",
       136212  + /* 258 */ "cmd ::= ANALYZE",
       136213  + /* 259 */ "cmd ::= ANALYZE nm dbnm",
       136214  + /* 260 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
       136215  + /* 261 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
       136216  + /* 262 */ "add_column_fullname ::= fullname",
       136217  + /* 263 */ "cmd ::= create_vtab",
       136218  + /* 264 */ "cmd ::= create_vtab LP vtabarglist RP",
       136219  + /* 265 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
       136220  + /* 266 */ "vtabarg ::=",
       136221  + /* 267 */ "vtabargtoken ::= ANY",
       136222  + /* 268 */ "vtabargtoken ::= lp anylist RP",
       136223  + /* 269 */ "lp ::= LP",
       136224  + /* 270 */ "with ::=",
       136225  + /* 271 */ "with ::= WITH wqlist",
       136226  + /* 272 */ "with ::= WITH RECURSIVE wqlist",
       136227  + /* 273 */ "wqlist ::= nm eidlist_opt AS LP select RP",
       136228  + /* 274 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
       136229  + /* 275 */ "input ::= cmdlist",
       136230  + /* 276 */ "cmdlist ::= cmdlist ecmd",
       136231  + /* 277 */ "cmdlist ::= ecmd",
       136232  + /* 278 */ "ecmd ::= SEMI",
       136233  + /* 279 */ "ecmd ::= explain cmdx SEMI",
       136234  + /* 280 */ "explain ::=",
       136235  + /* 281 */ "trans_opt ::=",
       136236  + /* 282 */ "trans_opt ::= TRANSACTION",
       136237  + /* 283 */ "trans_opt ::= TRANSACTION nm",
       136238  + /* 284 */ "savepoint_opt ::= SAVEPOINT",
       136239  + /* 285 */ "savepoint_opt ::=",
       136240  + /* 286 */ "cmd ::= create_table create_table_args",
       136241  + /* 287 */ "columnlist ::= columnlist COMMA columnname carglist",
       136242  + /* 288 */ "columnlist ::= columnname carglist",
       136243  + /* 289 */ "nm ::= ID|INDEXED",
       136244  + /* 290 */ "nm ::= STRING",
       136245  + /* 291 */ "nm ::= JOIN_KW",
       136246  + /* 292 */ "typetoken ::= typename",
       136247  + /* 293 */ "typename ::= ID|STRING",
       136248  + /* 294 */ "signed ::= plus_num",
       136249  + /* 295 */ "signed ::= minus_num",
       136250  + /* 296 */ "carglist ::= carglist ccons",
       136251  + /* 297 */ "carglist ::=",
       136252  + /* 298 */ "ccons ::= NULL onconf",
       136253  + /* 299 */ "conslist_opt ::= COMMA conslist",
       136254  + /* 300 */ "conslist ::= conslist tconscomma tcons",
       136255  + /* 301 */ "conslist ::= tcons",
       136256  + /* 302 */ "tconscomma ::=",
       136257  + /* 303 */ "defer_subclause_opt ::= defer_subclause",
       136258  + /* 304 */ "resolvetype ::= raisetype",
       136259  + /* 305 */ "selectnowith ::= oneselect",
       136260  + /* 306 */ "oneselect ::= values",
       136261  + /* 307 */ "sclp ::= selcollist COMMA",
       136262  + /* 308 */ "as ::= ID|STRING",
       136263  + /* 309 */ "expr ::= term",
       136264  + /* 310 */ "likeop ::= LIKE_KW|MATCH",
135740 136265    /* 311 */ "exprlist ::= nexprlist",
135741 136266    /* 312 */ "nmnum ::= plus_num",
135742 136267    /* 313 */ "nmnum ::= nm",
135743 136268    /* 314 */ "nmnum ::= ON",
135744 136269    /* 315 */ "nmnum ::= DELETE",
135745 136270    /* 316 */ "nmnum ::= DEFAULT",
135746 136271    /* 317 */ "plus_num ::= INTEGER|FLOAT",
................................................................................
136369 136894     { 173, 3 },
136370 136895     { 173, 3 },
136371 136896     { 173, 3 },
136372 136897     { 173, 3 },
136373 136898     { 173, 3 },
136374 136899     { 173, 3 },
136375 136900     { 173, 3 },
136376         -  { 221, 1 },
136377 136901     { 221, 2 },
136378 136902     { 173, 3 },
136379 136903     { 173, 5 },
136380 136904     { 173, 2 },
136381 136905     { 173, 3 },
136382 136906     { 173, 3 },
136383 136907     { 173, 4 },
................................................................................
136506 137030     { 188, 1 },
136507 137031     { 190, 1 },
136508 137032     { 194, 1 },
136509 137033     { 195, 1 },
136510 137034     { 209, 2 },
136511 137035     { 210, 1 },
136512 137036     { 173, 1 },
       137037  +  { 221, 1 },
136513 137038     { 208, 1 },
136514 137039     { 230, 1 },
136515 137040     { 230, 1 },
136516 137041     { 230, 1 },
136517 137042     { 230, 1 },
136518 137043     { 230, 1 },
136519 137044     { 169, 1 },
................................................................................
136648 137173         case 19: /* temp ::= */ yytestcase(yyruleno==19);
136649 137174         case 22: /* table_options ::= */ yytestcase(yyruleno==22);
136650 137175         case 42: /* autoinc ::= */ yytestcase(yyruleno==42);
136651 137176         case 57: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==57);
136652 137177         case 67: /* defer_subclause_opt ::= */ yytestcase(yyruleno==67);
136653 137178         case 76: /* ifexists ::= */ yytestcase(yyruleno==76);
136654 137179         case 90: /* distinct ::= */ yytestcase(yyruleno==90);
136655         -      case 215: /* collate ::= */ yytestcase(yyruleno==215);
       137180  +      case 214: /* collate ::= */ yytestcase(yyruleno==214);
136656 137181   {yymsp[1].minor.yy194 = 0;}
136657 137182           break;
136658 137183         case 17: /* ifnotexists ::= IF NOT EXISTS */
136659 137184   {yymsp[-2].minor.yy194 = 1;}
136660 137185           break;
136661 137186         case 18: /* temp ::= TEMP */
136662 137187         case 43: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==43);
................................................................................
136792 137317         case 56: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
136793 137318         case 71: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==71);
136794 137319         case 144: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==144);
136795 137320   {yymsp[-1].minor.yy194 = yymsp[0].minor.yy194;}
136796 137321           break;
136797 137322         case 58: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
136798 137323         case 75: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==75);
136799         -      case 187: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==187);
136800         -      case 190: /* in_op ::= NOT IN */ yytestcase(yyruleno==190);
136801         -      case 216: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==216);
       137324  +      case 186: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==186);
       137325  +      case 189: /* in_op ::= NOT IN */ yytestcase(yyruleno==189);
       137326  +      case 215: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==215);
136802 137327   {yymsp[-1].minor.yy194 = 1;}
136803 137328           break;
136804 137329         case 59: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
136805 137330   {yymsp[-1].minor.yy194 = 0;}
136806 137331           break;
136807 137332         case 61: /* tconscomma ::= COMMA */
136808 137333   {pParse->constraintName.n = 0;}
................................................................................
136958 137483           break;
136959 137484         case 89: /* distinct ::= ALL */
136960 137485   {yymsp[0].minor.yy194 = SF_All;}
136961 137486           break;
136962 137487         case 91: /* sclp ::= */
136963 137488         case 119: /* orderby_opt ::= */ yytestcase(yyruleno==119);
136964 137489         case 126: /* groupby_opt ::= */ yytestcase(yyruleno==126);
136965         -      case 203: /* exprlist ::= */ yytestcase(yyruleno==203);
136966         -      case 206: /* paren_exprlist ::= */ yytestcase(yyruleno==206);
136967         -      case 211: /* eidlist_opt ::= */ yytestcase(yyruleno==211);
       137490  +      case 202: /* exprlist ::= */ yytestcase(yyruleno==202);
       137491  +      case 205: /* paren_exprlist ::= */ yytestcase(yyruleno==205);
       137492  +      case 210: /* eidlist_opt ::= */ yytestcase(yyruleno==210);
136968 137493   {yymsp[1].minor.yy148 = 0;}
136969 137494           break;
136970 137495         case 92: /* selcollist ::= sclp expr as */
136971 137496   {
136972 137497      yymsp[-2].minor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy148, yymsp[-1].minor.yy190.pExpr);
136973 137498      if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-2].minor.yy148, &yymsp[0].minor.yy0, 1);
136974 137499      sqlite3ExprListSetSpan(pParse,yymsp[-2].minor.yy148,&yymsp[-1].minor.yy190);
................................................................................
136986 137511     Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
136987 137512     Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
136988 137513     yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148, pDot);
136989 137514   }
136990 137515           break;
136991 137516         case 95: /* as ::= AS nm */
136992 137517         case 106: /* dbnm ::= DOT nm */ yytestcase(yyruleno==106);
136993         -      case 225: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==225);
136994         -      case 226: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==226);
       137518  +      case 224: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==224);
       137519  +      case 225: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==225);
136995 137520   {yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
136996 137521           break;
136997 137522         case 97: /* from ::= */
136998 137523   {yymsp[1].minor.yy185 = sqlite3DbMallocZero(pParse->db, sizeof(*yymsp[1].minor.yy185));}
136999 137524           break;
137000 137525         case 98: /* from ::= FROM seltablist */
137001 137526   {
................................................................................
137070 137595           break;
137071 137596         case 111: /* joinop ::= JOIN_KW nm nm JOIN */
137072 137597   {yymsp[-3].minor.yy194 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/}
137073 137598           break;
137074 137599         case 112: /* on_opt ::= ON expr */
137075 137600         case 129: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==129);
137076 137601         case 136: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==136);
137077         -      case 199: /* case_else ::= ELSE expr */ yytestcase(yyruleno==199);
       137602  +      case 198: /* case_else ::= ELSE expr */ yytestcase(yyruleno==198);
137078 137603   {yymsp[-1].minor.yy72 = yymsp[0].minor.yy190.pExpr;}
137079 137604           break;
137080 137605         case 113: /* on_opt ::= */
137081 137606         case 128: /* having_opt ::= */ yytestcase(yyruleno==128);
137082 137607         case 135: /* where_opt ::= */ yytestcase(yyruleno==135);
137083         -      case 200: /* case_else ::= */ yytestcase(yyruleno==200);
137084         -      case 202: /* case_operand ::= */ yytestcase(yyruleno==202);
       137608  +      case 199: /* case_else ::= */ yytestcase(yyruleno==199);
       137609  +      case 201: /* case_operand ::= */ yytestcase(yyruleno==201);
137085 137610   {yymsp[1].minor.yy72 = 0;}
137086 137611           break;
137087 137612         case 115: /* indexed_opt ::= INDEXED BY nm */
137088 137613   {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
137089 137614           break;
137090 137615         case 116: /* indexed_opt ::= NOT INDEXED */
137091 137616   {yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
................................................................................
137224 137749   }
137225 137750           break;
137226 137751         case 158: /* term ::= INTEGER */
137227 137752   {
137228 137753     yylhsminor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
137229 137754     yylhsminor.yy190.zStart = yymsp[0].minor.yy0.z;
137230 137755     yylhsminor.yy190.zEnd = yymsp[0].minor.yy0.z + yymsp[0].minor.yy0.n;
137231         -  if( yylhsminor.yy190.pExpr ) yylhsminor.yy190.pExpr->flags |= EP_Leaf;
       137756  +  if( yylhsminor.yy190.pExpr ) yylhsminor.yy190.pExpr->flags |= EP_Leaf|EP_Resolved;
137232 137757   }
137233 137758     yymsp[0].minor.yy190 = yylhsminor.yy190;
137234 137759           break;
137235 137760         case 159: /* expr ::= VARIABLE */
137236 137761   {
137237 137762     if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])) ){
137238 137763       u32 n = yymsp[0].minor.yy0.n;
................................................................................
137314 137839         case 169: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==169);
137315 137840         case 170: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==170);
137316 137841         case 171: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==171);
137317 137842         case 172: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==172);
137318 137843         case 173: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==173);
137319 137844   {spanBinaryExpr(pParse,yymsp[-1].major,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy190);}
137320 137845           break;
137321         -      case 174: /* likeop ::= LIKE_KW|MATCH */
137322         -{yymsp[0].minor.yy0=yymsp[0].minor.yy0;/*A-overwrites-X*/}
137323         -        break;
137324         -      case 175: /* likeop ::= NOT LIKE_KW|MATCH */
       137846  +      case 174: /* likeop ::= NOT LIKE_KW|MATCH */
137325 137847   {yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
137326 137848           break;
137327         -      case 176: /* expr ::= expr likeop expr */
       137849  +      case 175: /* expr ::= expr likeop expr */
137328 137850   {
137329 137851     ExprList *pList;
137330 137852     int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
137331 137853     yymsp[-1].minor.yy0.n &= 0x7fffffff;
137332 137854     pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy190.pExpr);
137333 137855     pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy190.pExpr);
137334 137856     yymsp[-2].minor.yy190.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0);
137335 137857     exprNot(pParse, bNot, &yymsp[-2].minor.yy190);
137336 137858     yymsp[-2].minor.yy190.zEnd = yymsp[0].minor.yy190.zEnd;
137337 137859     if( yymsp[-2].minor.yy190.pExpr ) yymsp[-2].minor.yy190.pExpr->flags |= EP_InfixFunc;
137338 137860   }
137339 137861           break;
137340         -      case 177: /* expr ::= expr likeop expr ESCAPE expr */
       137862  +      case 176: /* expr ::= expr likeop expr ESCAPE expr */
137341 137863   {
137342 137864     ExprList *pList;
137343 137865     int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
137344 137866     yymsp[-3].minor.yy0.n &= 0x7fffffff;
137345 137867     pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy190.pExpr);
137346 137868     pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy190.pExpr);
137347 137869     pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy190.pExpr);
137348 137870     yymsp[-4].minor.yy190.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0);
137349 137871     exprNot(pParse, bNot, &yymsp[-4].minor.yy190);
137350 137872     yymsp[-4].minor.yy190.zEnd = yymsp[0].minor.yy190.zEnd;
137351 137873     if( yymsp[-4].minor.yy190.pExpr ) yymsp[-4].minor.yy190.pExpr->flags |= EP_InfixFunc;
137352 137874   }
137353 137875           break;
137354         -      case 178: /* expr ::= expr ISNULL|NOTNULL */
       137876  +      case 177: /* expr ::= expr ISNULL|NOTNULL */
137355 137877   {spanUnaryPostfix(pParse,yymsp[0].major,&yymsp[-1].minor.yy190,&yymsp[0].minor.yy0);}
137356 137878           break;
137357         -      case 179: /* expr ::= expr NOT NULL */
       137879  +      case 178: /* expr ::= expr NOT NULL */
137358 137880   {spanUnaryPostfix(pParse,TK_NOTNULL,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy0);}
137359 137881           break;
137360         -      case 180: /* expr ::= expr IS expr */
       137882  +      case 179: /* expr ::= expr IS expr */
137361 137883   {
137362 137884     spanBinaryExpr(pParse,TK_IS,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy190);
137363 137885     binaryToUnaryIfNull(pParse, yymsp[0].minor.yy190.pExpr, yymsp[-2].minor.yy190.pExpr, TK_ISNULL);
137364 137886   }
137365 137887           break;
137366         -      case 181: /* expr ::= expr IS NOT expr */
       137888  +      case 180: /* expr ::= expr IS NOT expr */
137367 137889   {
137368 137890     spanBinaryExpr(pParse,TK_ISNOT,&yymsp[-3].minor.yy190,&yymsp[0].minor.yy190);
137369 137891     binaryToUnaryIfNull(pParse, yymsp[0].minor.yy190.pExpr, yymsp[-3].minor.yy190.pExpr, TK_NOTNULL);
137370 137892   }
137371 137893           break;
137372         -      case 182: /* expr ::= NOT expr */
137373         -      case 183: /* expr ::= BITNOT expr */ yytestcase(yyruleno==183);
       137894  +      case 181: /* expr ::= NOT expr */
       137895  +      case 182: /* expr ::= BITNOT expr */ yytestcase(yyruleno==182);
137374 137896   {spanUnaryPrefix(&yymsp[-1].minor.yy190,pParse,yymsp[-1].major,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0);/*A-overwrites-B*/}
137375 137897           break;
137376         -      case 184: /* expr ::= MINUS expr */
       137898  +      case 183: /* expr ::= MINUS expr */
137377 137899   {spanUnaryPrefix(&yymsp[-1].minor.yy190,pParse,TK_UMINUS,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0);/*A-overwrites-B*/}
137378 137900           break;
137379         -      case 185: /* expr ::= PLUS expr */
       137901  +      case 184: /* expr ::= PLUS expr */
137380 137902   {spanUnaryPrefix(&yymsp[-1].minor.yy190,pParse,TK_UPLUS,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0);/*A-overwrites-B*/}
137381 137903           break;
137382         -      case 186: /* between_op ::= BETWEEN */
137383         -      case 189: /* in_op ::= IN */ yytestcase(yyruleno==189);
       137904  +      case 185: /* between_op ::= BETWEEN */
       137905  +      case 188: /* in_op ::= IN */ yytestcase(yyruleno==188);
137384 137906   {yymsp[0].minor.yy194 = 0;}
137385 137907           break;
137386         -      case 188: /* expr ::= expr between_op expr AND expr */
       137908  +      case 187: /* expr ::= expr between_op expr AND expr */
137387 137909   {
137388 137910     ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy190.pExpr);
137389 137911     pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy190.pExpr);
137390 137912     yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy190.pExpr, 0);
137391 137913     if( yymsp[-4].minor.yy190.pExpr ){
137392 137914       yymsp[-4].minor.yy190.pExpr->x.pList = pList;
137393 137915     }else{
137394 137916       sqlite3ExprListDelete(pParse->db, pList);
137395 137917     } 
137396 137918     exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190);
137397 137919     yymsp[-4].minor.yy190.zEnd = yymsp[0].minor.yy190.zEnd;
137398 137920   }
137399 137921           break;
137400         -      case 191: /* expr ::= expr in_op LP exprlist RP */
       137922  +      case 190: /* expr ::= expr in_op LP exprlist RP */
137401 137923   {
137402 137924       if( yymsp[-1].minor.yy148==0 ){
137403 137925         /* Expressions of the form
137404 137926         **
137405 137927         **      expr1 IN ()
137406 137928         **      expr1 NOT IN ()
137407 137929         **
................................................................................
137446 137968           sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy148);
137447 137969         }
137448 137970         exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190);
137449 137971       }
137450 137972       yymsp[-4].minor.yy190.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
137451 137973     }
137452 137974           break;
137453         -      case 192: /* expr ::= LP select RP */
       137975  +      case 191: /* expr ::= LP select RP */
137454 137976   {
137455 137977       spanSet(&yymsp[-2].minor.yy190,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-B*/
137456 137978       yymsp[-2].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
137457 137979       sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy190.pExpr, yymsp[-1].minor.yy243);
137458 137980     }
137459 137981           break;
137460         -      case 193: /* expr ::= expr in_op LP select RP */
       137982  +      case 192: /* expr ::= expr in_op LP select RP */
137461 137983   {
137462 137984       yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy190.pExpr, 0);
137463 137985       sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy190.pExpr, yymsp[-1].minor.yy243);
137464 137986       exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190);
137465 137987       yymsp[-4].minor.yy190.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
137466 137988     }
137467 137989           break;
137468         -      case 194: /* expr ::= expr in_op nm dbnm paren_exprlist */
       137990  +      case 193: /* expr ::= expr in_op nm dbnm paren_exprlist */
137469 137991   {
137470 137992       SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
137471 137993       Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
137472 137994       if( yymsp[0].minor.yy148 )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy148);
137473 137995       yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy190.pExpr, 0);
137474 137996       sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy190.pExpr, pSelect);
137475 137997       exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190);
137476 137998       yymsp[-4].minor.yy190.zEnd = yymsp[-1].minor.yy0.z ? &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n] : &yymsp[-2].minor.yy0.z[yymsp[-2].minor.yy0.n];
137477 137999     }
137478 138000           break;
137479         -      case 195: /* expr ::= EXISTS LP select RP */
       138001  +      case 194: /* expr ::= EXISTS LP select RP */
137480 138002   {
137481 138003       Expr *p;
137482 138004       spanSet(&yymsp[-3].minor.yy190,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-B*/
137483 138005       p = yymsp[-3].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
137484 138006       sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy243);
137485 138007     }
137486 138008           break;
137487         -      case 196: /* expr ::= CASE case_operand case_exprlist case_else END */
       138009  +      case 195: /* expr ::= CASE case_operand case_exprlist case_else END */
137488 138010   {
137489 138011     spanSet(&yymsp[-4].minor.yy190,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);  /*A-overwrites-C*/
137490 138012     yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy72, 0);
137491 138013     if( yymsp[-4].minor.yy190.pExpr ){
137492 138014       yymsp[-4].minor.yy190.pExpr->x.pList = yymsp[-1].minor.yy72 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy148,yymsp[-1].minor.yy72) : yymsp[-2].minor.yy148;
137493 138015       sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy190.pExpr);
137494 138016     }else{
137495 138017       sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy148);
137496 138018       sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy72);
137497 138019     }
137498 138020   }
137499 138021           break;
137500         -      case 197: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
       138022  +      case 196: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
137501 138023   {
137502 138024     yymsp[-4].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy148, yymsp[-2].minor.yy190.pExpr);
137503 138025     yymsp[-4].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy148, yymsp[0].minor.yy190.pExpr);
137504 138026   }
137505 138027           break;
137506         -      case 198: /* case_exprlist ::= WHEN expr THEN expr */
       138028  +      case 197: /* case_exprlist ::= WHEN expr THEN expr */
137507 138029   {
137508 138030     yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy190.pExpr);
137509 138031     yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148, yymsp[0].minor.yy190.pExpr);
137510 138032   }
137511 138033           break;
137512         -      case 201: /* case_operand ::= expr */
       138034  +      case 200: /* case_operand ::= expr */
137513 138035   {yymsp[0].minor.yy72 = yymsp[0].minor.yy190.pExpr; /*A-overwrites-X*/}
137514 138036           break;
137515         -      case 204: /* nexprlist ::= nexprlist COMMA expr */
       138037  +      case 203: /* nexprlist ::= nexprlist COMMA expr */
137516 138038   {yymsp[-2].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy148,yymsp[0].minor.yy190.pExpr);}
137517 138039           break;
137518         -      case 205: /* nexprlist ::= expr */
       138040  +      case 204: /* nexprlist ::= expr */
137519 138041   {yymsp[0].minor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy190.pExpr); /*A-overwrites-Y*/}
137520 138042           break;
137521         -      case 207: /* paren_exprlist ::= LP exprlist RP */
137522         -      case 212: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==212);
       138043  +      case 206: /* paren_exprlist ::= LP exprlist RP */
       138044  +      case 211: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==211);
137523 138045   {yymsp[-2].minor.yy148 = yymsp[-1].minor.yy148;}
137524 138046           break;
137525         -      case 208: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
       138047  +      case 207: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
137526 138048   {
137527 138049     sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, 
137528 138050                        sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy148, yymsp[-10].minor.yy194,
137529 138051                         &yymsp[-11].minor.yy0, yymsp[0].minor.yy72, SQLITE_SO_ASC, yymsp[-8].minor.yy194, SQLITE_IDXTYPE_APPDEF);
137530 138052   }
137531 138053           break;
137532         -      case 209: /* uniqueflag ::= UNIQUE */
137533         -      case 250: /* raisetype ::= ABORT */ yytestcase(yyruleno==250);
       138054  +      case 208: /* uniqueflag ::= UNIQUE */
       138055  +      case 249: /* raisetype ::= ABORT */ yytestcase(yyruleno==249);
137534 138056   {yymsp[0].minor.yy194 = OE_Abort;}
137535 138057           break;
137536         -      case 210: /* uniqueflag ::= */
       138058  +      case 209: /* uniqueflag ::= */
137537 138059   {yymsp[1].minor.yy194 = OE_None;}
137538 138060           break;
137539         -      case 213: /* eidlist ::= eidlist COMMA nm collate sortorder */
       138061  +      case 212: /* eidlist ::= eidlist COMMA nm collate sortorder */
137540 138062   {
137541 138063     yymsp[-4].minor.yy148 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy148, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy194, yymsp[0].minor.yy194);
137542 138064   }
137543 138065           break;
137544         -      case 214: /* eidlist ::= nm collate sortorder */
       138066  +      case 213: /* eidlist ::= nm collate sortorder */
137545 138067   {
137546 138068     yymsp[-2].minor.yy148 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy194, yymsp[0].minor.yy194); /*A-overwrites-Y*/
137547 138069   }
137548 138070           break;
137549         -      case 217: /* cmd ::= DROP INDEX ifexists fullname */
       138071  +      case 216: /* cmd ::= DROP INDEX ifexists fullname */
137550 138072   {sqlite3DropIndex(pParse, yymsp[0].minor.yy185, yymsp[-1].minor.yy194);}
137551 138073           break;
137552         -      case 218: /* cmd ::= VACUUM */
       138074  +      case 217: /* cmd ::= VACUUM */
137553 138075   {sqlite3Vacuum(pParse,0);}
137554 138076           break;
137555         -      case 219: /* cmd ::= VACUUM nm */
       138077  +      case 218: /* cmd ::= VACUUM nm */
137556 138078   {sqlite3Vacuum(pParse,&yymsp[0].minor.yy0);}
137557 138079           break;
137558         -      case 220: /* cmd ::= PRAGMA nm dbnm */
       138080  +      case 219: /* cmd ::= PRAGMA nm dbnm */
137559 138081   {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
137560 138082           break;
137561         -      case 221: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
       138083  +      case 220: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
137562 138084   {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
137563 138085           break;
137564         -      case 222: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
       138086  +      case 221: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
137565 138087   {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
137566 138088           break;
137567         -      case 223: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
       138089  +      case 222: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
137568 138090   {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
137569 138091           break;
137570         -      case 224: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
       138092  +      case 223: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
137571 138093   {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
137572 138094           break;
137573         -      case 227: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
       138095  +      case 226: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
137574 138096   {
137575 138097     Token all;
137576 138098     all.z = yymsp[-3].minor.yy0.z;
137577 138099     all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
137578 138100     sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy145, &all);
137579 138101   }
137580 138102           break;
137581         -      case 228: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
       138103  +      case 227: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
137582 138104   {
137583 138105     sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy194, yymsp[-4].minor.yy332.a, yymsp[-4].minor.yy332.b, yymsp[-2].minor.yy185, yymsp[0].minor.yy72, yymsp[-10].minor.yy194, yymsp[-8].minor.yy194);
137584 138106     yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
137585 138107   }
137586 138108           break;
137587         -      case 229: /* trigger_time ::= BEFORE */
       138109  +      case 228: /* trigger_time ::= BEFORE */
137588 138110   { yymsp[0].minor.yy194 = TK_BEFORE; }
137589 138111           break;
137590         -      case 230: /* trigger_time ::= AFTER */
       138112  +      case 229: /* trigger_time ::= AFTER */
137591 138113   { yymsp[0].minor.yy194 = TK_AFTER;  }
137592 138114           break;
137593         -      case 231: /* trigger_time ::= INSTEAD OF */
       138115  +      case 230: /* trigger_time ::= INSTEAD OF */
137594 138116   { yymsp[-1].minor.yy194 = TK_INSTEAD;}
137595 138117           break;
137596         -      case 232: /* trigger_time ::= */
       138118  +      case 231: /* trigger_time ::= */
137597 138119   { yymsp[1].minor.yy194 = TK_BEFORE; }
137598 138120           break;
137599         -      case 233: /* trigger_event ::= DELETE|INSERT */
137600         -      case 234: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==234);
       138121  +      case 232: /* trigger_event ::= DELETE|INSERT */
       138122  +      case 233: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==233);
137601 138123   {yymsp[0].minor.yy332.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy332.b = 0;}
137602 138124           break;
137603         -      case 235: /* trigger_event ::= UPDATE OF idlist */
       138125  +      case 234: /* trigger_event ::= UPDATE OF idlist */
137604 138126   {yymsp[-2].minor.yy332.a = TK_UPDATE; yymsp[-2].minor.yy332.b = yymsp[0].minor.yy254;}
137605 138127           break;
137606         -      case 236: /* when_clause ::= */
137607         -      case 255: /* key_opt ::= */ yytestcase(yyruleno==255);
       138128  +      case 235: /* when_clause ::= */
       138129  +      case 254: /* key_opt ::= */ yytestcase(yyruleno==254);
137608 138130   { yymsp[1].minor.yy72 = 0; }
137609 138131           break;
137610         -      case 237: /* when_clause ::= WHEN expr */
137611         -      case 256: /* key_opt ::= KEY expr */ yytestcase(yyruleno==256);
       138132  +      case 236: /* when_clause ::= WHEN expr */
       138133  +      case 255: /* key_opt ::= KEY expr */ yytestcase(yyruleno==255);
137612 138134   { yymsp[-1].minor.yy72 = yymsp[0].minor.yy190.pExpr; }
137613 138135           break;
137614         -      case 238: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
       138136  +      case 237: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
137615 138137   {
137616 138138     assert( yymsp[-2].minor.yy145!=0 );
137617 138139     yymsp[-2].minor.yy145->pLast->pNext = yymsp[-1].minor.yy145;
137618 138140     yymsp[-2].minor.yy145->pLast = yymsp[-1].minor.yy145;
137619 138141   }
137620 138142           break;
137621         -      case 239: /* trigger_cmd_list ::= trigger_cmd SEMI */
       138143  +      case 238: /* trigger_cmd_list ::= trigger_cmd SEMI */
137622 138144   { 
137623 138145     assert( yymsp[-1].minor.yy145!=0 );
137624 138146     yymsp[-1].minor.yy145->pLast = yymsp[-1].minor.yy145;
137625 138147   }
137626 138148           break;
137627         -      case 240: /* trnm ::= nm DOT nm */
       138149  +      case 239: /* trnm ::= nm DOT nm */
137628 138150   {
137629 138151     yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
137630 138152     sqlite3ErrorMsg(pParse, 
137631 138153           "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
137632 138154           "statements within triggers");
137633 138155   }
137634 138156           break;
137635         -      case 241: /* tridxby ::= INDEXED BY nm */
       138157  +      case 240: /* tridxby ::= INDEXED BY nm */
137636 138158   {
137637 138159     sqlite3ErrorMsg(pParse,
137638 138160           "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
137639 138161           "within triggers");
137640 138162   }
137641 138163           break;
137642         -      case 242: /* tridxby ::= NOT INDEXED */
       138164  +      case 241: /* tridxby ::= NOT INDEXED */
137643 138165   {
137644 138166     sqlite3ErrorMsg(pParse,
137645 138167           "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
137646 138168           "within triggers");
137647 138169   }
137648 138170           break;
137649         -      case 243: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
       138171  +      case 242: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
137650 138172   {yymsp[-6].minor.yy145 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy148, yymsp[0].minor.yy72, yymsp[-5].minor.yy194);}
137651 138173           break;
137652         -      case 244: /* trigger_cmd ::= insert_cmd INTO trnm idlist_opt select */
       138174  +      case 243: /* trigger_cmd ::= insert_cmd INTO trnm idlist_opt select */
137653 138175   {yymsp[-4].minor.yy145 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy254, yymsp[0].minor.yy243, yymsp[-4].minor.yy194);/*A-overwrites-R*/}
137654 138176           break;
137655         -      case 245: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
       138177  +      case 244: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
137656 138178   {yymsp[-4].minor.yy145 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy72);}
137657 138179           break;
137658         -      case 246: /* trigger_cmd ::= select */
       138180  +      case 245: /* trigger_cmd ::= select */
137659 138181   {yymsp[0].minor.yy145 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy243); /*A-overwrites-X*/}
137660 138182           break;
137661         -      case 247: /* expr ::= RAISE LP IGNORE RP */
       138183  +      case 246: /* expr ::= RAISE LP IGNORE RP */
137662 138184   {
137663 138185     spanSet(&yymsp[-3].minor.yy190,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);  /*A-overwrites-X*/
137664 138186     yymsp[-3].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
137665 138187     if( yymsp[-3].minor.yy190.pExpr ){
137666 138188       yymsp[-3].minor.yy190.pExpr->affinity = OE_Ignore;
137667 138189     }
137668 138190   }
137669 138191           break;
137670         -      case 248: /* expr ::= RAISE LP raisetype COMMA nm RP */
       138192  +      case 247: /* expr ::= RAISE LP raisetype COMMA nm RP */
137671 138193   {
137672 138194     spanSet(&yymsp[-5].minor.yy190,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);  /*A-overwrites-X*/
137673 138195     yymsp[-5].minor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1); 
137674 138196     if( yymsp[-5].minor.yy190.pExpr ) {
137675 138197       yymsp[-5].minor.yy190.pExpr->affinity = (char)yymsp[-3].minor.yy194;
137676 138198     }
137677 138199   }
137678 138200           break;
137679         -      case 249: /* raisetype ::= ROLLBACK */
       138201  +      case 248: /* raisetype ::= ROLLBACK */
137680 138202   {yymsp[0].minor.yy194 = OE_Rollback;}
137681 138203           break;
137682         -      case 251: /* raisetype ::= FAIL */
       138204  +      case 250: /* raisetype ::= FAIL */
137683 138205   {yymsp[0].minor.yy194 = OE_Fail;}
137684 138206           break;
137685         -      case 252: /* cmd ::= DROP TRIGGER ifexists fullname */
       138207  +      case 251: /* cmd ::= DROP TRIGGER ifexists fullname */
137686 138208   {
137687 138209     sqlite3DropTrigger(pParse,yymsp[0].minor.yy185,yymsp[-1].minor.yy194);
137688 138210   }
137689 138211           break;
137690         -      case 253: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
       138212  +      case 252: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
137691 138213   {
137692 138214     sqlite3Attach(pParse, yymsp[-3].minor.yy190.pExpr, yymsp[-1].minor.yy190.pExpr, yymsp[0].minor.yy72);
137693 138215   }
137694 138216           break;
137695         -      case 254: /* cmd ::= DETACH database_kw_opt expr */
       138217  +      case 253: /* cmd ::= DETACH database_kw_opt expr */
137696 138218   {
137697 138219     sqlite3Detach(pParse, yymsp[0].minor.yy190.pExpr);
137698 138220   }
137699 138221           break;
137700         -      case 257: /* cmd ::= REINDEX */
       138222  +      case 256: /* cmd ::= REINDEX */
137701 138223   {sqlite3Reindex(pParse, 0, 0);}
137702 138224           break;
137703         -      case 258: /* cmd ::= REINDEX nm dbnm */
       138225  +      case 257: /* cmd ::= REINDEX nm dbnm */
137704 138226   {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
137705 138227           break;
137706         -      case 259: /* cmd ::= ANALYZE */
       138228  +      case 258: /* cmd ::= ANALYZE */
137707 138229   {sqlite3Analyze(pParse, 0, 0);}
137708 138230           break;
137709         -      case 260: /* cmd ::= ANALYZE nm dbnm */
       138231  +      case 259: /* cmd ::= ANALYZE nm dbnm */
137710 138232   {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
137711 138233           break;
137712         -      case 261: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
       138234  +      case 260: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
137713 138235   {
137714 138236     sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy185,&yymsp[0].minor.yy0);
137715 138237   }
137716 138238           break;
137717         -      case 262: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
       138239  +      case 261: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
137718 138240   {
137719 138241     yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
137720 138242     sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
137721 138243   }
137722 138244           break;
137723         -      case 263: /* add_column_fullname ::= fullname */
       138245  +      case 262: /* add_column_fullname ::= fullname */
137724 138246   {
137725 138247     disableLookaside(pParse);
137726 138248     sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy185);
137727 138249   }
137728 138250           break;
137729         -      case 264: /* cmd ::= create_vtab */
       138251  +      case 263: /* cmd ::= create_vtab */
137730 138252   {sqlite3VtabFinishParse(pParse,0);}
137731 138253           break;
137732         -      case 265: /* cmd ::= create_vtab LP vtabarglist RP */
       138254  +      case 264: /* cmd ::= create_vtab LP vtabarglist RP */
137733 138255   {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
137734 138256           break;
137735         -      case 266: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
       138257  +      case 265: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
137736 138258   {
137737 138259       sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy194);
137738 138260   }
137739 138261           break;
137740         -      case 267: /* vtabarg ::= */
       138262  +      case 266: /* vtabarg ::= */
137741 138263   {sqlite3VtabArgInit(pParse);}
137742 138264           break;
137743         -      case 268: /* vtabargtoken ::= ANY */
137744         -      case 269: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==269);
137745         -      case 270: /* lp ::= LP */ yytestcase(yyruleno==270);
       138265  +      case 267: /* vtabargtoken ::= ANY */
       138266  +      case 268: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==268);
       138267  +      case 269: /* lp ::= LP */ yytestcase(yyruleno==269);
137746 138268   {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
137747 138269           break;
137748         -      case 271: /* with ::= */
       138270  +      case 270: /* with ::= */
137749 138271   {yymsp[1].minor.yy285 = 0;}
137750 138272           break;
137751         -      case 272: /* with ::= WITH wqlist */
       138273  +      case 271: /* with ::= WITH wqlist */
137752 138274   { yymsp[-1].minor.yy285 = yymsp[0].minor.yy285; }
137753 138275           break;
137754         -      case 273: /* with ::= WITH RECURSIVE wqlist */
       138276  +      case 272: /* with ::= WITH RECURSIVE wqlist */
137755 138277   { yymsp[-2].minor.yy285 = yymsp[0].minor.yy285; }
137756 138278           break;
137757         -      case 274: /* wqlist ::= nm eidlist_opt AS LP select RP */
       138279  +      case 273: /* wqlist ::= nm eidlist_opt AS LP select RP */
137758 138280   {
137759 138281     yymsp[-5].minor.yy285 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy148, yymsp[-1].minor.yy243); /*A-overwrites-X*/
137760 138282   }
137761 138283           break;
137762         -      case 275: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
       138284  +      case 274: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
137763 138285   {
137764 138286     yymsp[-7].minor.yy285 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy285, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy148, yymsp[-1].minor.yy243);
137765 138287   }
137766 138288           break;
137767 138289         default:
137768         -      /* (276) input ::= cmdlist */ yytestcase(yyruleno==276);
137769         -      /* (277) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==277);
137770         -      /* (278) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=278);
137771         -      /* (279) ecmd ::= SEMI */ yytestcase(yyruleno==279);
137772         -      /* (280) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==280);
137773         -      /* (281) explain ::= */ yytestcase(yyruleno==281);
137774         -      /* (282) trans_opt ::= */ yytestcase(yyruleno==282);
137775         -      /* (283) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==283);
137776         -      /* (284) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==284);
137777         -      /* (285) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==285);
137778         -      /* (286) savepoint_opt ::= */ yytestcase(yyruleno==286);
137779         -      /* (287) cmd ::= create_table create_table_args */ yytestcase(yyruleno==287);
137780         -      /* (288) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==288);
137781         -      /* (289) columnlist ::= columnname carglist */ yytestcase(yyruleno==289);
137782         -      /* (290) nm ::= ID|INDEXED */ yytestcase(yyruleno==290);
137783         -      /* (291) nm ::= STRING */ yytestcase(yyruleno==291);
137784         -      /* (292) nm ::= JOIN_KW */ yytestcase(yyruleno==292);
137785         -      /* (293) typetoken ::= typename */ yytestcase(yyruleno==293);
137786         -      /* (294) typename ::= ID|STRING */ yytestcase(yyruleno==294);
137787         -      /* (295) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=295);
137788         -      /* (296) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=296);
137789         -      /* (297) carglist ::= carglist ccons */ yytestcase(yyruleno==297);
137790         -      /* (298) carglist ::= */ yytestcase(yyruleno==298);
137791         -      /* (299) ccons ::= NULL onconf */ yytestcase(yyruleno==299);
137792         -      /* (300) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==300);
137793         -      /* (301) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==301);
137794         -      /* (302) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=302);
137795         -      /* (303) tconscomma ::= */ yytestcase(yyruleno==303);
137796         -      /* (304) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=304);
137797         -      /* (305) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=305);
137798         -      /* (306) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=306);
137799         -      /* (307) oneselect ::= values */ yytestcase(yyruleno==307);
137800         -      /* (308) sclp ::= selcollist COMMA */ yytestcase(yyruleno==308);
137801         -      /* (309) as ::= ID|STRING */ yytestcase(yyruleno==309);
137802         -      /* (310) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=310);
       138290  +      /* (275) input ::= cmdlist */ yytestcase(yyruleno==275);
       138291  +      /* (276) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==276);
       138292  +      /* (277) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=277);
       138293  +      /* (278) ecmd ::= SEMI */ yytestcase(yyruleno==278);
       138294  +      /* (279) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==279);
       138295  +      /* (280) explain ::= */ yytestcase(yyruleno==280);
       138296  +      /* (281) trans_opt ::= */ yytestcase(yyruleno==281);
       138297  +      /* (282) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==282);
       138298  +      /* (283) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==283);
       138299  +      /* (284) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==284);
       138300  +      /* (285) savepoint_opt ::= */ yytestcase(yyruleno==285);
       138301  +      /* (286) cmd ::= create_table create_table_args */ yytestcase(yyruleno==286);
       138302  +      /* (287) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==287);
       138303  +      /* (288) columnlist ::= columnname carglist */ yytestcase(yyruleno==288);
       138304  +      /* (289) nm ::= ID|INDEXED */ yytestcase(yyruleno==289);
       138305  +      /* (290) nm ::= STRING */ yytestcase(yyruleno==290);
       138306  +      /* (291) nm ::= JOIN_KW */ yytestcase(yyruleno==291);
       138307  +      /* (292) typetoken ::= typename */ yytestcase(yyruleno==292);
       138308  +      /* (293) typename ::= ID|STRING */ yytestcase(yyruleno==293);
       138309  +      /* (294) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=294);
       138310  +      /* (295) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=295);
       138311  +      /* (296) carglist ::= carglist ccons */ yytestcase(yyruleno==296);
       138312  +      /* (297) carglist ::= */ yytestcase(yyruleno==297);
       138313  +      /* (298) ccons ::= NULL onconf */ yytestcase(yyruleno==298);
       138314  +      /* (299) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==299);
       138315  +      /* (300) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==300);
       138316  +      /* (301) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=301);
       138317  +      /* (302) tconscomma ::= */ yytestcase(yyruleno==302);
       138318  +      /* (303) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=303);
       138319  +      /* (304) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=304);
       138320  +      /* (305) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=305);
       138321  +      /* (306) oneselect ::= values */ yytestcase(yyruleno==306);
       138322  +      /* (307) sclp ::= selcollist COMMA */ yytestcase(yyruleno==307);
       138323  +      /* (308) as ::= ID|STRING */ yytestcase(yyruleno==308);
       138324  +      /* (309) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=309);
       138325  +      /* (310) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==310);
137803 138326         /* (311) exprlist ::= nexprlist */ yytestcase(yyruleno==311);
137804 138327         /* (312) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=312);
137805 138328         /* (313) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=313);
137806 138329         /* (314) nmnum ::= ON */ yytestcase(yyruleno==314);
137807 138330         /* (315) nmnum ::= DELETE */ yytestcase(yyruleno==315);
137808 138331         /* (316) nmnum ::= DEFAULT */ yytestcase(yyruleno==316);
137809 138332         /* (317) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==317);
................................................................................
138848 139371   ** passed in.  An SQLITE_ status code is returned.  If an error occurs
138849 139372   ** then an and attempt is made to write an error message into 
138850 139373   ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
138851 139374   ** error message.
138852 139375   */
138853 139376   SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
138854 139377     int nErr = 0;                   /* Number of errors encountered */
138855         -  int i;                          /* Loop counter */
138856 139378     void *pEngine;                  /* The LEMON-generated LALR(1) parser */
       139379  +  int n = 0;                      /* Length of the next token token */
138857 139380     int tokenType;                  /* type of the next token */
138858 139381     int lastTokenParsed = -1;       /* type of the previous token */
138859 139382     sqlite3 *db = pParse->db;       /* The database connection */
138860 139383     int mxSqlLen;                   /* Max length of an SQL string */
138861 139384   #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138862 139385     unsigned char zSpace[sizeof(yyParser)];  /* Space for parser engine object */
138863 139386   #endif
................................................................................
138865 139388     assert( zSql!=0 );
138866 139389     mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
138867 139390     if( db->nVdbeActive==0 ){
138868 139391       db->u1.isInterrupted = 0;
138869 139392     }
138870 139393     pParse->rc = SQLITE_OK;
138871 139394     pParse->zTail = zSql;
138872         -  i = 0;
138873 139395     assert( pzErrMsg!=0 );
138874 139396     /* sqlite3ParserTrace(stdout, "parser: "); */
138875 139397   #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138876 139398     pEngine = zSpace;
138877 139399     sqlite3ParserInit(pEngine);
138878 139400   #else
138879 139401     pEngine = sqlite3ParserAlloc(sqlite3Malloc);
................................................................................
138883 139405     }
138884 139406   #endif
138885 139407     assert( pParse->pNewTable==0 );
138886 139408     assert( pParse->pNewTrigger==0 );
138887 139409     assert( pParse->nVar==0 );
138888 139410     assert( pParse->pVList==0 );
138889 139411     while( 1 ){
138890         -    assert( i>=0 );
138891         -    if( zSql[i]!=0 ){
138892         -      pParse->sLastToken.z = &zSql[i];
138893         -      pParse->sLastToken.n = sqlite3GetToken((u8*)&zSql[i],&tokenType);
138894         -      i += pParse->sLastToken.n;
138895         -      if( i>mxSqlLen ){
       139412  +    if( zSql[0]!=0 ){
       139413  +      n = sqlite3GetToken((u8*)zSql, &tokenType);
       139414  +      mxSqlLen -= n;
       139415  +      if( mxSqlLen<0 ){
138896 139416           pParse->rc = SQLITE_TOOBIG;
138897 139417           break;
138898 139418         }
138899 139419       }else{
138900 139420         /* Upon reaching the end of input, call the parser two more times
138901 139421         ** with tokens TK_SEMI and 0, in that order. */
138902 139422         if( lastTokenParsed==TK_SEMI ){
138903 139423           tokenType = 0;
138904 139424         }else if( lastTokenParsed==0 ){
138905 139425           break;
138906 139426         }else{
138907 139427           tokenType = TK_SEMI;
138908 139428         }
       139429  +      zSql -= n;
138909 139430       }
138910 139431       if( tokenType>=TK_SPACE ){
138911 139432         assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
138912 139433         if( db->u1.isInterrupted ){
138913 139434           pParse->rc = SQLITE_INTERRUPT;
138914 139435           break;
138915 139436         }
138916 139437         if( tokenType==TK_ILLEGAL ){
138917         -        sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
138918         -                        &pParse->sLastToken);
       139438  +        sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
138919 139439           break;
138920 139440         }
       139441  +      zSql += n;
138921 139442       }else{
       139443  +      pParse->sLastToken.z = zSql;
       139444  +      pParse->sLastToken.n = n;
138922 139445         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
138923 139446         lastTokenParsed = tokenType;
       139447  +      zSql += n;
138924 139448         if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
138925 139449       }
138926 139450     }
138927 139451     assert( nErr==0 );
138928         -  pParse->zTail = &zSql[i];
       139452  +  pParse->zTail = zSql;
138929 139453   #ifdef YYTRACKMAXSTACKDEPTH
138930 139454     sqlite3_mutex_enter(sqlite3MallocMutex());
138931 139455     sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
138932 139456         sqlite3ParserStackPeak(pEngine)
138933 139457     );
138934 139458     sqlite3_mutex_leave(sqlite3MallocMutex());
138935 139459   #endif /* YYDEBUG */
................................................................................
140295 140819     if( !sqlite3SafetyCheckOk(db) ){
140296 140820       (void)SQLITE_MISUSE_BKPT;
140297 140821       return 0;
140298 140822     }
140299 140823   #endif
140300 140824     return db->lastRowid;
140301 140825   }
       140826  +
       140827  +/*
       140828  +** Set the value returned by the sqlite3_last_insert_rowid() API function.
       140829  +*/
       140830  +SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
       140831  +#ifdef SQLITE_ENABLE_API_ARMOR
       140832  +  if( !sqlite3SafetyCheckOk(db) ){
       140833  +    (void)SQLITE_MISUSE_BKPT;
       140834  +    return;
       140835  +  }
       140836  +#endif
       140837  +  sqlite3_mutex_enter(db->mutex);
       140838  +  db->lastRowid = iRowid;
       140839  +  sqlite3_mutex_leave(db->mutex);
       140840  +}
140302 140841   
140303 140842   /*
140304 140843   ** Return the number of changes in the most recent call to sqlite3_exec().
140305 140844   */
140306 140845   SQLITE_API int sqlite3_changes(sqlite3 *db){
140307 140846   #ifdef SQLITE_ENABLE_API_ARMOR
140308 140847     if( !sqlite3SafetyCheckOk(db) ){
................................................................................
144596 145135     int nAutoincrmerge;             /* Value configured by 'automerge' */
144597 145136     u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
144598 145137   
144599 145138     /* Precompiled statements used by the implementation. Each of these 
144600 145139     ** statements is run and reset within a single virtual table API call. 
144601 145140     */
144602 145141     sqlite3_stmt *aStmt[40];
       145142  +  sqlite3_stmt *pSeekStmt;        /* Cache for fts3CursorSeekStmt() */
144603 145143   
144604 145144     char *zReadExprlist;
144605 145145     char *zWriteExprlist;
144606 145146   
144607 145147     int nNodeSize;                  /* Soft limit for node size */
144608 145148     u8 bFts4;                       /* True for FTS4, false for FTS3 */
144609 145149     u8 bHasStat;                    /* True if %_stat table exists (2==unknown) */
................................................................................
144665 145205   ** the xOpen method. Cursors are destroyed using the xClose method.
144666 145206   */
144667 145207   struct Fts3Cursor {
144668 145208     sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
144669 145209     i16 eSearch;                    /* Search strategy (see below) */
144670 145210     u8 isEof;                       /* True if at End Of Results */
144671 145211     u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
       145212  +  u8 bSeekStmt;                   /* True if pStmt is a seek */
144672 145213     sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
144673 145214     Fts3Expr *pExpr;                /* Parsed MATCH query string */
144674 145215     int iLangid;                    /* Language being queried for */
144675 145216     int nPhrase;                    /* Number of matchable phrases in query */
144676 145217     Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
144677 145218     sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
144678 145219     char *pNextId;                  /* Pointer into the body of aDoclist */
................................................................................
145044 145585     if( (v & mask2)==0 ){ var = v; return ret; }
145045 145586   
145046 145587   /* 
145047 145588   ** Read a 64-bit variable-length integer from memory starting at p[0].
145048 145589   ** Return the number of bytes read, or 0 on error.
145049 145590   ** The value is stored in *v.
145050 145591   */
145051         -SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
145052         -  const char *pStart = p;
       145592  +SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
       145593  +  const unsigned char *p = (const unsigned char*)pBuf;
       145594  +  const unsigned char *pStart = p;
145053 145595     u32 a;
145054 145596     u64 b;
145055 145597     int shift;
145056 145598   
145057 145599     GETVARINT_INIT(a, p, 0,  0x00,     0x80, *v, 1);
145058 145600     GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *v, 2);
145059 145601     GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *v, 3);
................................................................................
145187 145729     Fts3Table *p = (Fts3Table *)pVtab;
145188 145730     int i;
145189 145731   
145190 145732     assert( p->nPendingData==0 );
145191 145733     assert( p->pSegments==0 );
145192 145734   
145193 145735     /* Free any prepared statements held */
       145736  +  sqlite3_finalize(p->pSeekStmt);
145194 145737     for(i=0; i<SizeofArray(p->aStmt); i++){
145195 145738       sqlite3_finalize(p->aStmt[i]);
145196 145739     }
145197 145740     sqlite3_free(p->zSegmentsTbl);
145198 145741     sqlite3_free(p->zReadExprlist);
145199 145742     sqlite3_free(p->zWriteExprlist);
145200 145743     sqlite3_free(p->zContentTbl);
................................................................................
146091 146634     zCsr += nDb;
146092 146635   
146093 146636     /* Fill in the azColumn array */
146094 146637     for(iCol=0; iCol<nCol; iCol++){
146095 146638       char *z; 
146096 146639       int n = 0;
146097 146640       z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
146098         -    memcpy(zCsr, z, n);
       146641  +    if( n>0 ){
       146642  +      memcpy(zCsr, z, n);
       146643  +    }
146099 146644       zCsr[n] = '\0';
146100 146645       sqlite3Fts3Dequote(zCsr);
146101 146646       p->azColumn[iCol] = zCsr;
146102 146647       zCsr += n+1;
146103 146648       assert( zCsr <= &((char *)p)[nByte] );
146104 146649     }
146105 146650   
................................................................................
146374 146919     *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
146375 146920     if( !pCsr ){
146376 146921       return SQLITE_NOMEM;
146377 146922     }
146378 146923     memset(pCsr, 0, sizeof(Fts3Cursor));
146379 146924     return SQLITE_OK;
146380 146925   }
       146926  +
       146927  +/*
       146928  +** Finalize the statement handle at pCsr->pStmt.
       146929  +**
       146930  +** Or, if that statement handle is one created by fts3CursorSeekStmt(),
       146931  +** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
       146932  +** pointer there instead of finalizing it.
       146933  +*/
       146934  +static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
       146935  +  if( pCsr->bSeekStmt ){
       146936  +    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
       146937  +    if( p->pSeekStmt==0 ){
       146938  +      p->pSeekStmt = pCsr->pStmt;
       146939  +      sqlite3_reset(pCsr->pStmt);
       146940  +      pCsr->pStmt = 0;
       146941  +    }
       146942  +    pCsr->bSeekStmt = 0;
       146943  +  }
       146944  +  sqlite3_finalize(pCsr->pStmt);
       146945  +}
146381 146946   
146382 146947   /*
146383 146948   ** Close the cursor.  For additional information see the documentation
146384 146949   ** on the xClose method of the virtual table interface.
146385 146950   */
146386 146951   static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
146387 146952     Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
146388 146953     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
146389         -  sqlite3_finalize(pCsr->pStmt);
       146954  +  fts3CursorFinalizeStmt(pCsr);
146390 146955     sqlite3Fts3ExprFree(pCsr->pExpr);
146391 146956     sqlite3Fts3FreeDeferredTokens(pCsr);
146392 146957     sqlite3_free(pCsr->aDoclist);
146393 146958     sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
146394 146959     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
146395 146960     sqlite3_free(pCsr);
146396 146961     return SQLITE_OK;
................................................................................
146400 146965   ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
146401 146966   ** compose and prepare an SQL statement of the form:
146402 146967   **
146403 146968   **    "SELECT <columns> FROM %_content WHERE rowid = ?"
146404 146969   **
146405 146970   ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
146406 146971   ** it. If an error occurs, return an SQLite error code.
146407         -**
146408         -** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
146409 146972   */
146410         -static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
       146973  +static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
146411 146974     int rc = SQLITE_OK;
146412 146975     if( pCsr->pStmt==0 ){
146413 146976       Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
146414 146977       char *zSql;
146415         -    zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
146416         -    if( !zSql ) return SQLITE_NOMEM;
146417         -    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
146418         -    sqlite3_free(zSql);
       146978  +    if( p->pSeekStmt ){
       146979  +      pCsr->pStmt = p->pSeekStmt;
       146980  +      p->pSeekStmt = 0;
       146981  +    }else{
       146982  +      zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
       146983  +      if( !zSql ) return SQLITE_NOMEM;
       146984  +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
       146985  +      sqlite3_free(zSql);
       146986  +    }
       146987  +    if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
146419 146988     }
146420         -  *ppStmt = pCsr->pStmt;
146421 146989     return rc;
146422 146990   }
146423 146991   
146424 146992   /*
146425 146993   ** Position the pCsr->pStmt statement so that it is on the row
146426 146994   ** of the %_content table that contains the last match.  Return
146427 146995   ** SQLITE_OK on success.  
146428 146996   */
146429 146997   static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
146430 146998     int rc = SQLITE_OK;
146431 146999     if( pCsr->isRequireSeek ){
146432         -    sqlite3_stmt *pStmt = 0;
146433         -
146434         -    rc = fts3CursorSeekStmt(pCsr, &pStmt);
       147000  +    rc = fts3CursorSeekStmt(pCsr);
146435 147001       if( rc==SQLITE_OK ){
146436 147002         sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
146437 147003         pCsr->isRequireSeek = 0;
146438 147004         if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
146439 147005           return SQLITE_OK;
146440 147006         }else{
146441 147007           rc = sqlite3_reset(pCsr->pStmt);
................................................................................
147885 148451     if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
147886 148452     if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
147887 148453     if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
147888 148454     if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
147889 148455     assert( iIdx==nVal );
147890 148456   
147891 148457     /* In case the cursor has been used before, clear it now. */
147892         -  sqlite3_finalize(pCsr->pStmt);
       148458  +  fts3CursorFinalizeStmt(pCsr);
147893 148459     sqlite3_free(pCsr->aDoclist);
147894 148460     sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
147895 148461     sqlite3Fts3ExprFree(pCsr->pExpr);
147896 148462     memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
147897 148463   
147898 148464     /* Set the lower and upper bounds on docids to return */
147899 148465     pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
................................................................................
147953 148519       if( zSql ){
147954 148520         rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
147955 148521         sqlite3_free(zSql);
147956 148522       }else{
147957 148523         rc = SQLITE_NOMEM;
147958 148524       }
147959 148525     }else if( eSearch==FTS3_DOCID_SEARCH ){
147960         -    rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
       148526  +    rc = fts3CursorSeekStmt(pCsr);
147961 148527       if( rc==SQLITE_OK ){
147962 148528         rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
147963 148529       }
147964 148530     }
147965 148531     if( rc!=SQLITE_OK ) return rc;
147966 148532   
147967 148533     return fts3NextMethod(pCursor);
................................................................................
148081 148647     ** of blocks from the segments table. But this is not considered overhead
148082 148648     ** as it would also be required by a crisis-merge that used the same input 
148083 148649     ** segments.
148084 148650     */
148085 148651     const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
148086 148652   
148087 148653     Fts3Table *p = (Fts3Table*)pVtab;
148088         -  int rc = sqlite3Fts3PendingTermsFlush(p);
       148654  +  int rc;
       148655  +  i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
148089 148656   
       148657  +  rc = sqlite3Fts3PendingTermsFlush(p);
148090 148658     if( rc==SQLITE_OK 
148091 148659      && p->nLeafAdd>(nMinMerge/16) 
148092 148660      && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
148093 148661     ){
148094 148662       int mxLevel = 0;              /* Maximum relative level value in db */
148095 148663       int A;                        /* Incr-merge parameter A */
148096 148664   
................................................................................
148097 148665       rc = sqlite3Fts3MaxLevel(p, &mxLevel);
148098 148666       assert( rc==SQLITE_OK || mxLevel==0 );
148099 148667       A = p->nLeafAdd * mxLevel;
148100 148668       A += (A/2);
148101 148669       if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
148102 148670     }
148103 148671     sqlite3Fts3SegmentsClose(p);
       148672  +  sqlite3_set_last_insert_rowid(p->db, iLastRowid);
148104 148673     return rc;
148105 148674   }
148106 148675   
148107 148676   /*
148108 148677   ** If it is currently unknown whether or not the FTS table has an %_stat
148109 148678   ** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
148110 148679   ** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
................................................................................
159707 160276     return rc;
159708 160277   }
159709 160278   
159710 160279   /*
159711 160280   ** Convert the text beginning at *pz into an integer and return
159712 160281   ** its value.  Advance *pz to point to the first character past
159713 160282   ** the integer.
       160283  +**
       160284  +** This function used for parameters to merge= and incrmerge=
       160285  +** commands. 
159714 160286   */
159715 160287   static int fts3Getint(const char **pz){
159716 160288     const char *z = *pz;
159717 160289     int i = 0;
159718         -  while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
       160290  +  while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
159719 160291     *pz = z;
159720 160292     return i;
159721 160293   }
159722 160294   
159723 160295   /*
159724 160296   ** Process statements of the form:
159725 160297   **
................................................................................
162277 162849     unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
162278 162850     int bAlnum,                     /* Replace Isalnum() return value with this */
162279 162851     const char *zIn,                /* Array of characters to make exceptions */
162280 162852     int nIn                         /* Length of z in bytes */
162281 162853   ){
162282 162854     const unsigned char *z = (const unsigned char *)zIn;
162283 162855     const unsigned char *zTerm = &z[nIn];
162284         -  int iCode;
       162856  +  unsigned int iCode;
162285 162857     int nEntry = 0;
162286 162858   
162287 162859     assert( bAlnum==0 || bAlnum==1 );
162288 162860   
162289 162861     while( z<zTerm ){
162290 162862       READ_UTF8(z, zTerm, iCode);
162291         -    assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
162292         -    if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
162293         -     && sqlite3FtsUnicodeIsdiacritic(iCode)==0 
       162863  +    assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
       162864  +    if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum 
       162865  +     && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0 
162294 162866       ){
162295 162867         nEntry++;
162296 162868       }
162297 162869     }
162298 162870   
162299 162871     if( nEntry ){
162300 162872       int *aNew;                    /* New aiException[] array */
................................................................................
162303 162875       aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
162304 162876       if( aNew==0 ) return SQLITE_NOMEM;
162305 162877       nNew = p->nException;
162306 162878   
162307 162879       z = (const unsigned char *)zIn;
162308 162880       while( z<zTerm ){
162309 162881         READ_UTF8(z, zTerm, iCode);
162310         -      if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
162311         -       && sqlite3FtsUnicodeIsdiacritic(iCode)==0
       162882  +      if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum 
       162883  +       && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
162312 162884         ){
162313 162885           int i, j;
162314         -        for(i=0; i<nNew && aNew[i]<iCode; i++);
       162886  +        for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
162315 162887           for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
162316         -        aNew[i] = iCode;
       162888  +        aNew[i] = (int)iCode;
162317 162889           nNew++;
162318 162890         }
162319 162891       }
162320 162892       p->aiException = aNew;
162321 162893       p->nException = nNew;
162322 162894     }
162323 162895   
................................................................................
162459 163031     int *pnToken,                   /* OUT: Number of bytes at *paToken */
162460 163032     int *piStart,                   /* OUT: Starting offset of token */
162461 163033     int *piEnd,                     /* OUT: Ending offset of token */
162462 163034     int *piPos                      /* OUT: Position integer of token */
162463 163035   ){
162464 163036     unicode_cursor *pCsr = (unicode_cursor *)pC;
162465 163037     unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
162466         -  int iCode = 0;
       163038  +  unsigned int iCode = 0;
162467 163039     char *zOut;
162468 163040     const unsigned char *z = &pCsr->aInput[pCsr->iOff];
162469 163041     const unsigned char *zStart = z;
162470 163042     const unsigned char *zEnd;
162471 163043     const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
162472 163044   
162473 163045     /* Scan past any delimiter characters before the start of the next token.
162474 163046     ** Return SQLITE_DONE early if this takes us all the way to the end of 
162475 163047     ** the input.  */
162476 163048     while( z<zTerm ){
162477 163049       READ_UTF8(z, zTerm, iCode);
162478         -    if( unicodeIsAlnum(p, iCode) ) break;
       163050  +    if( unicodeIsAlnum(p, (int)iCode) ) break;
162479 163051       zStart = z;
162480 163052     }
162481 163053     if( zStart>=zTerm ) return SQLITE_DONE;
162482 163054   
162483 163055     zOut = pCsr->zToken;
162484 163056     do {
162485 163057       int iOut;
................................................................................
162491 163063         zOut = &zNew[zOut - pCsr->zToken];
162492 163064         pCsr->zToken = zNew;
162493 163065         pCsr->nAlloc += 64;
162494 163066       }
162495 163067   
162496 163068       /* Write the folded case of the last character read to the output */
162497 163069       zEnd = z;
162498         -    iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
       163070  +    iOut = sqlite3FtsUnicodeFold((int)iCode, p->bRemoveDiacritic);
162499 163071       if( iOut ){
162500 163072         WRITE_UTF8(zOut, iOut);
162501 163073       }
162502 163074   
162503 163075       /* If the cursor is not at EOF, read the next character */
162504 163076       if( z>=zTerm ) break;
162505 163077       READ_UTF8(z, zTerm, iCode);
162506         -  }while( unicodeIsAlnum(p, iCode) 
162507         -       || sqlite3FtsUnicodeIsdiacritic(iCode)
       163078  +  }while( unicodeIsAlnum(p, (int)iCode) 
       163079  +       || sqlite3FtsUnicodeIsdiacritic((int)iCode)
162508 163080     );
162509 163081   
162510 163082     /* Set the output variables and return. */
162511 163083     pCsr->iOff = (int)(z - pCsr->aInput);
162512 163084     *paToken = pCsr->zToken;
162513 163085     *pnToken = (int)(zOut - pCsr->zToken);
162514 163086     *piStart = (int)(zStart - pCsr->aInput);
................................................................................
162664 163236       0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
162665 163237       0x380400F0,
162666 163238     };
162667 163239     static const unsigned int aAscii[4] = {
162668 163240       0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
162669 163241     };
162670 163242   
162671         -  if( c<128 ){
162672         -    return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
162673         -  }else if( c<(1<<22) ){
       163243  +  if( (unsigned int)c<128 ){
       163244  +    return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
       163245  +  }else if( (unsigned int)c<(1<<22) ){
162674 163246       unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
162675 163247       int iRes = 0;
162676 163248       int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
162677 163249       int iLo = 0;
162678 163250       while( iHi>=iLo ){
162679 163251         int iTest = (iHi + iLo) / 2;
162680 163252         if( key >= aEntry[iTest] ){
................................................................................
162859 163431      65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462, 
162860 163432      65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511, 
162861 163433      65514, 65521, 65527, 65528, 65529, 
162862 163434     };
162863 163435   
162864 163436     int ret = c;
162865 163437   
162866         -  assert( c>=0 );
162867 163438     assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
162868 163439   
162869 163440     if( c<128 ){
162870 163441       if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
162871 163442     }else if( c<65536 ){
       163443  +    const struct TableEntry *p;
162872 163444       int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
162873 163445       int iLo = 0;
162874 163446       int iRes = -1;
162875 163447   
       163448  +    assert( c>aEntry[0].iCode );
162876 163449       while( iHi>=iLo ){
162877 163450         int iTest = (iHi + iLo) / 2;
162878 163451         int cmp = (c - aEntry[iTest].iCode);
162879 163452         if( cmp>=0 ){
162880 163453           iRes = iTest;
162881 163454           iLo = iTest+1;
162882 163455         }else{
162883 163456           iHi = iTest-1;
162884 163457         }
162885 163458       }
162886         -    assert( iRes<0 || c>=aEntry[iRes].iCode );
162887 163459   
162888         -    if( iRes>=0 ){
162889         -      const struct TableEntry *p = &aEntry[iRes];
162890         -      if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
162891         -        ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
162892         -        assert( ret>0 );
162893         -      }
       163460  +    assert( iRes>=0 && c>=aEntry[iRes].iCode );
       163461  +    p = &aEntry[iRes];
       163462  +    if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
       163463  +      ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
       163464  +      assert( ret>0 );
162894 163465       }
162895 163466   
162896 163467       if( bRemoveDiacritic ) ret = remove_diacritic(ret);
162897 163468     }
162898 163469     
162899 163470     else if( c>=66560 && c<66600 ){
162900 163471       ret = c + 40;
................................................................................
163273 163844   #ifndef MAX
163274 163845   # define MAX(x,y) ((x) < (y) ? (y) : (x))
163275 163846   #endif
163276 163847   #ifndef MIN
163277 163848   # define MIN(x,y) ((x) > (y) ? (y) : (x))
163278 163849   #endif
163279 163850   
163280         -/* What version of GCC is being used.  0 means GCC is not being used */
       163851  +/* What version of GCC is being used.  0 means GCC is not being used .
       163852  +** Note that the GCC_VERSION macro will also be set correctly when using
       163853  +** clang, since clang works hard to be gcc compatible.  So the gcc
       163854  +** optimizations will also work when compiling with clang.
       163855  +*/
163281 163856   #ifndef GCC_VERSION
163282         -#ifdef __GNUC__
       163857  +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
163283 163858   # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
163284 163859   #else
163285 163860   # define GCC_VERSION 0
163286 163861   #endif
163287         -#endif
163288         -
163289         -/* What version of CLANG is being used.  0 means CLANG is not being used */
163290         -#ifndef CLANG_VERSION
163291         -#if defined(__clang__) && !defined(_WIN32)
163292         -# define CLANG_VERSION \
163293         -            (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
163294         -#else
163295         -# define CLANG_VERSION 0
163296         -#endif
163297 163862   #endif
163298 163863   
163299 163864   /* The testcase() macro should already be defined in the amalgamation.  If
163300 163865   ** it is not, make it a no-op.
163301 163866   */
163302 163867   #ifndef SQLITE_AMALGAMATION
163303 163868   # define testcase(X)
................................................................................
163309 163874   **
163310 163875   ** For best performance, an attempt is made to guess at the byte-order
163311 163876   ** using C-preprocessor macros.  If that is unsuccessful, or if
163312 163877   ** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
163313 163878   ** at run-time.
163314 163879   */
163315 163880   #ifndef SQLITE_BYTEORDER
163316         -#if (defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
163317         -     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
163318         -     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
163319         -     defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
       163881  +#if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
       163882  +    defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
       163883  +    defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
       163884  +    defined(__arm__)
163320 163885   # define SQLITE_BYTEORDER    1234
163321         -#elif (defined(sparc)    || defined(__ppc__))  \
163322         -    && !defined(SQLITE_RUNTIME_BYTEORDER)
       163886  +#elif defined(sparc)    || defined(__ppc__)
163323 163887   # define SQLITE_BYTEORDER    4321
163324 163888   #else
163325 163889   # define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
163326 163890   #endif
163327 163891   #endif
163328 163892   
163329 163893   
163330 163894   /* What version of MSVC is being used.  0 means MSVC is not being used */
163331 163895   #ifndef MSVC_VERSION
163332         -#if defined(_MSC_VER)
       163896  +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
163333 163897   # define MSVC_VERSION _MSC_VER
163334 163898   #else
163335 163899   # define MSVC_VERSION 0
163336 163900   #endif
163337 163901   #endif
163338 163902   
163339 163903   /*
................................................................................
163343 163907   static int readInt16(u8 *p){
163344 163908     return (p[0]<<8) + p[1];
163345 163909   }
163346 163910   static void readCoord(u8 *p, RtreeCoord *pCoord){
163347 163911     assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
163348 163912   #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163349 163913     pCoord->u = _byteswap_ulong(*(u32*)p);
163350         -#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163914  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
163351 163915     pCoord->u = __builtin_bswap32(*(u32*)p);
163352         -#elif SQLITE_BYTEORDER==1234
163353         -  pCoord->u = ((pCoord->u>>24)&0xff)|((pCoord->u>>8)&0xff00)|
163354         -              ((pCoord->u&0xff)<<24)|((pCoord->u&0xff00)<<8);
163355 163916   #elif SQLITE_BYTEORDER==4321
163356 163917     pCoord->u = *(u32*)p;
163357 163918   #else
163358 163919     pCoord->u = (
163359 163920       (((u32)p[0]) << 24) + 
163360 163921       (((u32)p[1]) << 16) + 
163361 163922       (((u32)p[2]) <<  8) + 
................................................................................
163364 163925   #endif
163365 163926   }
163366 163927   static i64 readInt64(u8 *p){
163367 163928   #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163368 163929     u64 x;
163369 163930     memcpy(&x, p, 8);
163370 163931     return (i64)_byteswap_uint64(x);
163371         -#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163932  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
163372 163933     u64 x;
163373 163934     memcpy(&x, p, 8);
163374 163935     return (i64)__builtin_bswap64(x);
163375 163936   #elif SQLITE_BYTEORDER==4321
163376 163937     i64 x;
163377 163938     memcpy(&x, p, 8);
163378 163939     return x;
163379 163940   #else
163380         -  return (
163381         -    (((i64)p[0]) << 56) + 
163382         -    (((i64)p[1]) << 48) + 
163383         -    (((i64)p[2]) << 40) + 
163384         -    (((i64)p[3]) << 32) + 
163385         -    (((i64)p[4]) << 24) + 
163386         -    (((i64)p[5]) << 16) + 
163387         -    (((i64)p[6]) <<  8) + 
163388         -    (((i64)p[7]) <<  0)
       163941  +  return (i64)(
       163942  +    (((u64)p[0]) << 56) + 
       163943  +    (((u64)p[1]) << 48) + 
       163944  +    (((u64)p[2]) << 40) + 
       163945  +    (((u64)p[3]) << 32) + 
       163946  +    (((u64)p[4]) << 24) + 
       163947  +    (((u64)p[5]) << 16) + 
       163948  +    (((u64)p[6]) <<  8) + 
       163949  +    (((u64)p[7]) <<  0)
163389 163950     );
163390 163951   #endif
163391 163952   }
163392 163953   
163393 163954   /*
163394 163955   ** Functions to serialize a 16 bit integer, 32 bit real number and
163395 163956   ** 64 bit integer. The value returned is the number of bytes written
163396 163957   ** to the argument buffer (always 2, 4 and 8 respectively).
163397 163958   */
163398         -static int writeInt16(u8 *p, int i){
       163959  +static void writeInt16(u8 *p, int i){
163399 163960     p[0] = (i>> 8)&0xFF;
163400 163961     p[1] = (i>> 0)&0xFF;
163401         -  return 2;
163402 163962   }
163403 163963   static int writeCoord(u8 *p, RtreeCoord *pCoord){
163404 163964     u32 i;
163405 163965     assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
163406 163966     assert( sizeof(RtreeCoord)==4 );
163407 163967     assert( sizeof(u32)==4 );
163408         -#if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163968  +#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
163409 163969     i = __builtin_bswap32(pCoord->u);
163410 163970     memcpy(p, &i, 4);
163411 163971   #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163412 163972     i = _byteswap_ulong(pCoord->u);
163413 163973     memcpy(p, &i, 4);
163414 163974   #elif SQLITE_BYTEORDER==4321
163415 163975     i = pCoord->u;
................................................................................
163420 163980     p[1] = (i>>16)&0xFF;
163421 163981     p[2] = (i>> 8)&0xFF;
163422 163982     p[3] = (i>> 0)&0xFF;
163423 163983   #endif
163424 163984     return 4;
163425 163985   }
163426 163986   static int writeInt64(u8 *p, i64 i){
163427         -#if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       163987  +#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
163428 163988     i = (i64)__builtin_bswap64((u64)i);
163429 163989     memcpy(p, &i, 8);
163430 163990   #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163431 163991     i = (i64)_byteswap_uint64((u64)i);
163432 163992     memcpy(p, &i, 8);
163433 163993   #elif SQLITE_BYTEORDER==4321
163434 163994     memcpy(p, &i, 8);
................................................................................
163976 164536   */
163977 164537   #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163978 164538   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
163979 164539       RtreeCoord c;    /* Coordinate decoded */                   \
163980 164540       c.u = _byteswap_ulong(*(u32*)a);                            \
163981 164541       r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163982 164542   }
163983         -#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
       164543  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
163984 164544   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
163985 164545       RtreeCoord c;    /* Coordinate decoded */                   \
163986 164546       c.u = __builtin_bswap32(*(u32*)a);                          \
163987 164547       r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163988 164548   }
163989 164549   #elif SQLITE_BYTEORDER==1234
163990 164550   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
................................................................................
168443 169003     int rc;                         /* Value returned by last rbu_step() call */
168444 169004     char *zErrmsg;                  /* Error message if rc!=SQLITE_OK */
168445 169005     int nStep;                      /* Rows processed for current object */
168446 169006     int nProgress;                  /* Rows processed for all objects */
168447 169007     RbuObjIter objiter;             /* Iterator for skipping through tbl/idx */
168448 169008     const char *zVfsName;           /* Name of automatically created rbu vfs */
168449 169009     rbu_file *pTargetFd;            /* File handle open on target db */
       169010  +  int nPagePerSector;             /* Pages per sector for pTargetFd */
168450 169011     i64 iOalSz;
168451 169012     i64 nPhaseOneStep;
168452 169013   
168453 169014     /* The following state variables are used as part of the incremental
168454 169015     ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
168455 169016     ** function rbuSetupCheckpoint() for details.  */
168456 169017     u32 iMaxFrame;                  /* Largest iWalFrame value in aFrame[] */
................................................................................
170707 171268       p->iWalCksum = rbuShmChecksum(p);
170708 171269     }
170709 171270   
170710 171271     if( p->rc==SQLITE_OK ){
170711 171272       if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
170712 171273         p->rc = SQLITE_DONE;
170713 171274         p->eStage = RBU_STAGE_DONE;
       171275  +    }else{
       171276  +      int nSectorSize;
       171277  +      sqlite3_file *pDb = p->pTargetFd->pReal;
       171278  +      sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
       171279  +      assert( p->nPagePerSector==0 );
       171280  +      nSectorSize = pDb->pMethods->xSectorSize(pDb);
       171281  +      if( nSectorSize>p->pgsz ){
       171282  +        p->nPagePerSector = nSectorSize / p->pgsz;
       171283  +      }else{
       171284  +        p->nPagePerSector = 1;
       171285  +      }
       171286  +
       171287  +      /* Call xSync() on the wal file. This causes SQLite to sync the 
       171288  +      ** directory in which the target database and the wal file reside, in 
       171289  +      ** case it has not been synced since the rename() call in 
       171290  +      ** rbuMoveOalFile(). */
       171291  +      p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
170714 171292       }
170715 171293     }
170716 171294   }
170717 171295   
170718 171296   /*
170719 171297   ** Called when iAmt bytes are read from offset iOff of the wal file while
170720 171298   ** the rbu object is in capture mode. Record the frame number of the frame
................................................................................
171362 171940               }
171363 171941     
171364 171942               if( p->rc==SQLITE_OK ){
171365 171943                 p->eStage = RBU_STAGE_DONE;
171366 171944                 p->rc = SQLITE_DONE;
171367 171945               }
171368 171946             }else{
171369         -            RbuFrame *pFrame = &p->aFrame[p->nStep];
171370         -            rbuCheckpointFrame(p, pFrame);
171371         -            p->nStep++;
       171947  +            /* At one point the following block copied a single frame from the
       171948  +            ** wal file to the database file. So that one call to sqlite3rbu_step()
       171949  +            ** checkpointed a single frame. 
       171950  +            **
       171951  +            ** However, if the sector-size is larger than the page-size, and the
       171952  +            ** application calls sqlite3rbu_savestate() or close() immediately
       171953  +            ** after this step, then rbu_step() again, then a power failure occurs,
       171954  +            ** then the database page written here may be damaged. Work around
       171955  +            ** this by checkpointing frames until the next page in the aFrame[]
       171956  +            ** lies on a different disk sector to the current one. */
       171957  +            u32 iSector;
       171958  +            do{
       171959  +              RbuFrame *pFrame = &p->aFrame[p->nStep];
       171960  +              iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
       171961  +              rbuCheckpointFrame(p, pFrame);
       171962  +              p->nStep++;
       171963  +            }while( p->nStep<p->nFrame 
       171964  +                 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
       171965  +                 && p->rc==SQLITE_OK
       171966  +            );
171372 171967             }
171373 171968             p->nProgress++;
171374 171969           }
171375 171970           break;
171376 171971         }
171377 171972   
171378 171973         default:
................................................................................
171804 172399     int rc;
171805 172400     if( p ){
171806 172401   
171807 172402       /* Commit the transaction to the *-oal file. */
171808 172403       if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
171809 172404         p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
171810 172405       }
       172406  +
       172407  +    /* Sync the db file if currently doing an incremental checkpoint */
       172408  +    if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
       172409  +      sqlite3_file *pDb = p->pTargetFd->pReal;
       172410  +      p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
       172411  +    }
171811 172412   
171812 172413       rbuSaveState(p, p->eStage);
171813 172414   
171814 172415       if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
171815 172416         p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
171816 172417       }
171817 172418   
................................................................................
171928 172529     if( rc==SQLITE_DONE ) return SQLITE_OK;
171929 172530   
171930 172531     assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
171931 172532     if( p->eStage==RBU_STAGE_OAL ){
171932 172533       assert( rc!=SQLITE_DONE );
171933 172534       if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
171934 172535     }
       172536  +
       172537  +  /* Sync the db file */
       172538  +  if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
       172539  +    sqlite3_file *pDb = p->pTargetFd->pReal;
       172540  +    rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
       172541  +  }
171935 172542   
171936 172543     p->rc = rc;
171937 172544     rbuSaveState(p, p->eStage);
171938 172545     rc = p->rc;
171939 172546   
171940 172547     if( p->eStage==RBU_STAGE_OAL ){
171941 172548       assert( rc!=SQLITE_DONE );
................................................................................
178344 178951   };
178345 178952   
178346 178953   /* Bit values for the JsonNode.jnFlag field
178347 178954   */
178348 178955   #define JNODE_RAW     0x01         /* Content is raw, not JSON encoded */
178349 178956   #define JNODE_ESCAPE  0x02         /* Content is text with \ escapes */
178350 178957   #define JNODE_REMOVE  0x04         /* Do not output */
178351         -#define JNODE_REPLACE 0x08         /* Replace with JsonNode.iVal */
178352         -#define JNODE_APPEND  0x10         /* More ARRAY/OBJECT entries at u.iAppend */
178353         -#define JNODE_LABEL   0x20         /* Is a label of an object */
       178958  +#define JNODE_REPLACE 0x08         /* Replace with JsonNode.u.iReplace */
       178959  +#define JNODE_PATCH   0x10         /* Patch with JsonNode.u.pPatch */
       178960  +#define JNODE_APPEND  0x20         /* More ARRAY/OBJECT entries at u.iAppend */
       178961  +#define JNODE_LABEL   0x40         /* Is a label of an object */
178354 178962   
178355 178963   
178356 178964   /* A single node of parsed JSON
178357 178965   */
178358 178966   struct JsonNode {
178359 178967     u8 eType;              /* One of the JSON_ type values */
178360 178968     u8 jnFlags;            /* JNODE flags */
178361         -  u8 iVal;               /* Replacement value when JNODE_REPLACE */
178362 178969     u32 n;                 /* Bytes of content, or number of sub-nodes */
178363 178970     union {
178364 178971       const char *zJContent; /* Content for INT, REAL, and STRING */
178365 178972       u32 iAppend;           /* More terms for ARRAY and OBJECT */
178366 178973       u32 iKey;              /* Key for ARRAY objects in json_tree() */
       178974  +    u32 iReplace;          /* Replacement content for JNODE_REPLACE */
       178975  +    JsonNode *pPatch;      /* Node chain of patch for JNODE_PATCH */
178367 178976     } u;
178368 178977   };
178369 178978   
178370 178979   /* A completely parsed JSON string
178371 178980   */
178372 178981   struct JsonParse {
178373 178982     u32 nNode;         /* Number of slots of aNode[] used */
................................................................................
178616 179225   ** the number of JsonNode objects that are encoded.
178617 179226   */
178618 179227   static void jsonRenderNode(
178619 179228     JsonNode *pNode,               /* The node to render */
178620 179229     JsonString *pOut,              /* Write JSON here */
178621 179230     sqlite3_value **aReplace       /* Replacement values */
178622 179231   ){
       179232  +  if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
       179233  +    if( pNode->jnFlags & JNODE_REPLACE ){
       179234  +      jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
       179235  +      return;
       179236  +    }
       179237  +    pNode = pNode->u.pPatch;
       179238  +  }
178623 179239     switch( pNode->eType ){
178624 179240       default: {
178625 179241         assert( pNode->eType==JSON_NULL );
178626 179242         jsonAppendRaw(pOut, "null", 4);
178627 179243         break;
178628 179244       }
178629 179245       case JSON_TRUE: {
................................................................................
178647 179263         break;
178648 179264       }
178649 179265       case JSON_ARRAY: {
178650 179266         u32 j = 1;
178651 179267         jsonAppendChar(pOut, '[');
178652 179268         for(;;){
178653 179269           while( j<=pNode->n ){
178654         -          if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
178655         -            if( pNode[j].jnFlags & JNODE_REPLACE ){
178656         -              jsonAppendSeparator(pOut);
178657         -              jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
178658         -            }
178659         -          }else{
       179270  +          if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
178660 179271               jsonAppendSeparator(pOut);
178661 179272               jsonRenderNode(&pNode[j], pOut, aReplace);
178662 179273             }
178663 179274             j += jsonNodeSize(&pNode[j]);
178664 179275           }
178665 179276           if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
178666 179277           pNode = &pNode[pNode->u.iAppend];
................................................................................
178674 179285         jsonAppendChar(pOut, '{');
178675 179286         for(;;){
178676 179287           while( j<=pNode->n ){
178677 179288             if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
178678 179289               jsonAppendSeparator(pOut);
178679 179290               jsonRenderNode(&pNode[j], pOut, aReplace);
178680 179291               jsonAppendChar(pOut, ':');
178681         -            if( pNode[j+1].jnFlags & JNODE_REPLACE ){
178682         -              jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
178683         -            }else{
178684         -              jsonRenderNode(&pNode[j+1], pOut, aReplace);
178685         -            }
       179292  +            jsonRenderNode(&pNode[j+1], pOut, aReplace);
178686 179293             }
178687 179294             j += 1 + jsonNodeSize(&pNode[j+1]);
178688 179295           }
178689 179296           if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
178690 179297           pNode = &pNode[pNode->u.iAppend];
178691 179298           j = 1;
178692 179299         }
................................................................................
178905 179512     JsonNode *p;
178906 179513     if( pParse->nNode>=pParse->nAlloc ){
178907 179514       return jsonParseAddNodeExpand(pParse, eType, n, zContent);
178908 179515     }
178909 179516     p = &pParse->aNode[pParse->nNode];
178910 179517     p->eType = (u8)eType;
178911 179518     p->jnFlags = 0;
178912         -  p->iVal = 0;
178913 179519     p->n = n;
178914 179520     p->u.zJContent = zContent;
178915 179521     return pParse->nNode++;
178916 179522   }
178917 179523   
178918 179524   /*
178919 179525   ** Return true if z[] begins with 4 (or more) hexadecimal digits
................................................................................
179371 179977   ){
179372 179978     char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
179373 179979                                  zFuncName);
179374 179980     sqlite3_result_error(pCtx, zMsg, -1);
179375 179981     sqlite3_free(zMsg);     
179376 179982   }
179377 179983   
       179984  +/*
       179985  +** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
       179986  +*/
       179987  +static void jsonRemoveAllNulls(JsonNode *pNode){
       179988  +  int i, n;
       179989  +  assert( pNode->eType==JSON_OBJECT );
       179990  +  n = pNode->n;
       179991  +  for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
       179992  +    switch( pNode[i].eType ){
       179993  +      case JSON_NULL:
       179994  +        pNode[i].jnFlags |= JNODE_REMOVE;
       179995  +        break;
       179996  +      case JSON_OBJECT:
       179997  +        jsonRemoveAllNulls(&pNode[i]);
       179998  +        break;
       179999  +    }
       180000  +  }
       180001  +}
       180002  +
179378 180003   
179379 180004   /****************************************************************************
179380 180005   ** SQL functions used for testing and debugging
179381 180006   ****************************************************************************/
179382 180007   
179383 180008   #ifdef SQLITE_DEBUG
179384 180009   /*
................................................................................
179562 180187       jsonAppendChar(&jx, ']');
179563 180188       jsonResult(&jx);
179564 180189       sqlite3_result_subtype(ctx, JSON_SUBTYPE);
179565 180190     }
179566 180191     jsonReset(&jx);
179567 180192     jsonParseReset(&x);
179568 180193   }
       180194  +
       180195  +/* This is the RFC 7396 MergePatch algorithm.
       180196  +*/
       180197  +static JsonNode *jsonMergePatch(
       180198  +  JsonParse *pParse,   /* The JSON parser that contains the TARGET */
       180199  +  int iTarget,         /* Node of the TARGET in pParse */
       180200  +  JsonNode *pPatch     /* The PATCH */
       180201  +){
       180202  +  u32 i, j;
       180203  +  u32 iRoot;
       180204  +  JsonNode *pTarget;
       180205  +  if( pPatch->eType!=JSON_OBJECT ){
       180206  +    return pPatch;
       180207  +  }
       180208  +  assert( iTarget>=0 && iTarget<pParse->nNode );
       180209  +  pTarget = &pParse->aNode[iTarget];
       180210  +  assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
       180211  +  if( pTarget->eType!=JSON_OBJECT ){
       180212  +    jsonRemoveAllNulls(pPatch);
       180213  +    return pPatch;
       180214  +  }
       180215  +  iRoot = iTarget;
       180216  +  for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
       180217  +    u32 nKey;
       180218  +    const char *zKey;
       180219  +    assert( pPatch[i].eType==JSON_STRING );
       180220  +    assert( pPatch[i].jnFlags & JNODE_LABEL );
       180221  +    nKey = pPatch[i].n;
       180222  +    zKey = pPatch[i].u.zJContent;
       180223  +    assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
       180224  +    for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
       180225  +      assert( pTarget[j].eType==JSON_STRING );
       180226  +      assert( pTarget[j].jnFlags & JNODE_LABEL );
       180227  +      assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
       180228  +      if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
       180229  +        if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
       180230  +        if( pPatch[i+1].eType==JSON_NULL ){
       180231  +          pTarget[j+1].jnFlags |= JNODE_REMOVE;
       180232  +        }else{
       180233  +          JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
       180234  +          if( pNew==0 ) return 0;
       180235  +          pTarget = &pParse->aNode[iTarget];
       180236  +          if( pNew!=&pTarget[j+1] ){
       180237  +            pTarget[j+1].u.pPatch = pNew;
       180238  +            pTarget[j+1].jnFlags |= JNODE_PATCH;
       180239  +          }
       180240  +        }
       180241  +        break;
       180242  +      }
       180243  +    }
       180244  +    if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
       180245  +      int iStart, iPatch;
       180246  +      iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
       180247  +      jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
       180248  +      iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
       180249  +      if( pParse->oom ) return 0;
       180250  +      jsonRemoveAllNulls(pPatch);
       180251  +      pTarget = &pParse->aNode[iTarget];
       180252  +      pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
       180253  +      pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
       180254  +      iRoot = iStart;
       180255  +      pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
       180256  +      pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
       180257  +    }
       180258  +  }
       180259  +  return pTarget;
       180260  +}
       180261  +
       180262  +/*
       180263  +** Implementation of the json_mergepatch(JSON1,JSON2) function.  Return a JSON
       180264  +** object that is the result of running the RFC 7396 MergePatch() algorithm
       180265  +** on the two arguments.
       180266  +*/
       180267  +static void jsonPatchFunc(
       180268  +  sqlite3_context *ctx,
       180269  +  int argc,
       180270  +  sqlite3_value **argv
       180271  +){
       180272  +  JsonParse x;     /* The JSON that is being patched */
       180273  +  JsonParse y;     /* The patch */
       180274  +  JsonNode *pResult;   /* The result of the merge */
       180275  +
       180276  +  UNUSED_PARAM(argc);
       180277  +  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
       180278  +  if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
       180279  +    jsonParseReset(&x);
       180280  +    return;
       180281  +  }
       180282  +  pResult = jsonMergePatch(&x, 0, y.aNode);
       180283  +  assert( pResult!=0 || x.oom );
       180284  +  if( pResult ){
       180285  +    jsonReturnJson(pResult, ctx, 0);
       180286  +  }else{
       180287  +    sqlite3_result_error_nomem(ctx);
       180288  +  }
       180289  +  jsonParseReset(&x);
       180290  +  jsonParseReset(&y);
       180291  +}
       180292  +
179569 180293   
179570 180294   /*
179571 180295   ** Implementation of the json_object(NAME,VALUE,...) function.  Return a JSON
179572 180296   ** object that contains all name/value given in arguments.  Or if any name
179573 180297   ** is not a string or if any value is a BLOB, throw an error.
179574 180298   */
179575 180299   static void jsonObjectFunc(
................................................................................
179666 180390     assert( x.nNode );
179667 180391     for(i=1; i<(u32)argc; i+=2){
179668 180392       zPath = (const char*)sqlite3_value_text(argv[i]);
179669 180393       pNode = jsonLookup(&x, zPath, 0, ctx);
179670 180394       if( x.nErr ) goto replace_err;
179671 180395       if( pNode ){
179672 180396         pNode->jnFlags |= (u8)JNODE_REPLACE;
179673         -      pNode->iVal = (u8)(i+1);
       180397  +      pNode->u.iReplace = i + 1;
179674 180398       }
179675 180399     }
179676 180400     if( x.aNode[0].jnFlags & JNODE_REPLACE ){
179677         -    sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
       180401  +    sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
179678 180402     }else{
179679 180403       jsonReturnJson(x.aNode, ctx, argv);
179680 180404     }
179681 180405   replace_err:
179682 180406     jsonParseReset(&x);
179683 180407   }
179684 180408   
................................................................................
179720 180444       if( x.oom ){
179721 180445         sqlite3_result_error_nomem(ctx);
179722 180446         goto jsonSetDone;
179723 180447       }else if( x.nErr ){
179724 180448         goto jsonSetDone;
179725 180449       }else if( pNode && (bApnd || bIsSet) ){
179726 180450         pNode->jnFlags |= (u8)JNODE_REPLACE;
179727         -      pNode->iVal = (u8)(i+1);
       180451  +      pNode->u.iReplace = i + 1;
179728 180452       }
179729 180453     }
179730 180454     if( x.aNode[0].jnFlags & JNODE_REPLACE ){
179731         -    sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
       180455  +    sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
179732 180456     }else{
179733 180457       jsonReturnJson(x.aNode, ctx, argv);
179734 180458     }
179735 180459   jsonSetDone:
179736 180460     jsonParseReset(&x);
179737 180461   }
179738 180462   
................................................................................
180367 181091       { "json",                 1, 0,   jsonRemoveFunc        },
180368 181092       { "json_array",          -1, 0,   jsonArrayFunc         },
180369 181093       { "json_array_length",    1, 0,   jsonArrayLengthFunc   },
180370 181094       { "json_array_length",    2, 0,   jsonArrayLengthFunc   },
180371 181095       { "json_extract",        -1, 0,   jsonExtractFunc       },
180372 181096       { "json_insert",         -1, 0,   jsonSetFunc           },
180373 181097       { "json_object",         -1, 0,   jsonObjectFunc        },
       181098  +    { "json_patch",           2, 0,   jsonPatchFunc         },
180374 181099       { "json_quote",           1, 0,   jsonQuoteFunc         },
180375 181100       { "json_remove",         -1, 0,   jsonRemoveFunc        },
180376 181101       { "json_replace",        -1, 0,   jsonReplaceFunc       },
180377 181102       { "json_set",            -1, 1,   jsonSetFunc           },
180378 181103       { "json_type",            1, 0,   jsonTypeFunc          },
180379 181104       { "json_type",            2, 0,   jsonTypeFunc          },
180380 181105       { "json_valid",           1, 0,   jsonValidFunc         },
................................................................................
181060 181785   typedef unsigned char  u8;
181061 181786   typedef unsigned int   u32;
181062 181787   typedef unsigned short u16;
181063 181788   typedef short i16;
181064 181789   typedef sqlite3_int64 i64;
181065 181790   typedef sqlite3_uint64 u64;
181066 181791   
181067         -#define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
       181792  +#ifndef ArraySize
       181793  +# define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
       181794  +#endif
181068 181795   
181069 181796   #define testcase(x)
181070 181797   #define ALWAYS(x) 1
181071 181798   #define NEVER(x) 0
181072 181799   
181073 181800   #define MIN(x,y) (((x) < (y)) ? (x) : (y))
181074 181801   #define MAX(x,y) (((x) > (y)) ? (x) : (y))
................................................................................
186283 187010       Fts5ExprNode *p1 = pNode->apChild[i];
186284 187011       assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
186285 187012       if( p1->bEof==0 ){
186286 187013         if( (p1->iRowid==iLast) 
186287 187014          || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
186288 187015         ){
186289 187016           int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
186290         -        if( rc!=SQLITE_OK ) return rc;
       187017  +        if( rc!=SQLITE_OK ){
       187018  +          pNode->bNomatch = 0;
       187019  +          return rc;
       187020  +        }
186291 187021         }
186292 187022       }
186293 187023     }
186294 187024   
186295 187025     fts5ExprNodeTest_OR(pExpr, pNode);
186296 187026     return SQLITE_OK;
186297 187027   }
................................................................................
186314 187044       bMatch = 1;
186315 187045       for(iChild=0; iChild<pAnd->nChild; iChild++){
186316 187046         Fts5ExprNode *pChild = pAnd->apChild[iChild];
186317 187047         int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
186318 187048         if( cmp>0 ){
186319 187049           /* Advance pChild until it points to iLast or laster */
186320 187050           rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
186321         -        if( rc!=SQLITE_OK ) return rc;
       187051  +        if( rc!=SQLITE_OK ){
       187052  +          pAnd->bNomatch = 0;
       187053  +          return rc;
       187054  +        }
186322 187055         }
186323 187056   
186324 187057         /* If the child node is now at EOF, so is the parent AND node. Otherwise,
186325 187058         ** the child node is guaranteed to have advanced at least as far as
186326 187059         ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
186327 187060         ** new lastest rowid seen so far.  */
186328 187061         assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
................................................................................
186353 187086     Fts5ExprNode *pNode,
186354 187087     int bFromValid,
186355 187088     i64 iFrom
186356 187089   ){
186357 187090     int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
186358 187091     if( rc==SQLITE_OK ){
186359 187092       rc = fts5ExprNodeTest_AND(pExpr, pNode);
       187093  +  }else{
       187094  +    pNode->bNomatch = 0;
186360 187095     }
186361 187096     return rc;
186362 187097   }
186363 187098   
186364 187099   static int fts5ExprNodeTest_NOT(
186365 187100     Fts5Expr *pExpr,                /* Expression pPhrase belongs to */
186366 187101     Fts5ExprNode *pNode             /* FTS5_NOT node to advance */
................................................................................
186395 187130     int bFromValid,
186396 187131     i64 iFrom
186397 187132   ){
186398 187133     int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
186399 187134     if( rc==SQLITE_OK ){
186400 187135       rc = fts5ExprNodeTest_NOT(pExpr, pNode);
186401 187136     }
       187137  +  if( rc!=SQLITE_OK ){
       187138  +    pNode->bNomatch = 0;
       187139  +  }
186402 187140     return rc;
186403 187141   }
186404 187142   
186405 187143   /*
186406 187144   ** If pNode currently points to a match, this function returns SQLITE_OK
186407 187145   ** without modifying it. Otherwise, pNode is advanced until it does point
186408 187146   ** to a match or EOF is reached.
................................................................................
197515 198253   static void fts5SourceIdFunc(
197516 198254     sqlite3_context *pCtx,          /* Function call context */
197517 198255     int nArg,                       /* Number of args */
197518 198256     sqlite3_value **apUnused        /* Function arguments */
197519 198257   ){
197520 198258     assert( nArg==0 );
197521 198259     UNUSED_PARAM2(nArg, apUnused);
197522         -  sqlite3_result_text(pCtx, "fts5: 2017-02-07 20:57:00 db6b39937dd9d6fcd5fcc582c8dd529caad755da", -1, SQLITE_TRANSIENT);
       198260  +  sqlite3_result_text(pCtx, "fts5: 2017-03-25 18:15:05 bd5bbe4eece9051cc8c27d834d22fb243d34f261641984555d02a27b7038d4f6", -1, SQLITE_TRANSIENT);
197523 198261   }
197524 198262   
197525 198263   static int fts5Init(sqlite3 *db){
197526 198264     static const sqlite3_module fts5Mod = {
197527 198265       /* iVersion      */ 2,
197528 198266       /* xCreate       */ fts5CreateMethod,
197529 198267       /* xConnect      */ fts5ConnectMethod,
................................................................................
198178 198916       if( rc==SQLITE_OK ){
198179 198917         sqlite3_bind_int64(pDel, 1, iDel);
198180 198918         sqlite3_step(pDel);
198181 198919         rc = sqlite3_reset(pDel);
198182 198920       }
198183 198921     }
198184 198922   
198185         -  /* Write the averages record */
198186         -  if( rc==SQLITE_OK ){
198187         -    rc = fts5StorageSaveTotals(p);
198188         -  }
198189         -
198190 198923     return rc;
198191 198924   }
198192 198925   
198193 198926   /*
198194 198927   ** Delete all entries in the FTS5 index.
198195 198928   */
198196 198929   static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
................................................................................
198386 199119   
198387 199120     /* Write the %_docsize record */
198388 199121     if( rc==SQLITE_OK ){
198389 199122       rc = fts5StorageInsertDocsize(p, iRowid, &buf);
198390 199123     }
198391 199124     sqlite3_free(buf.p);
198392 199125   
198393         -  /* Write the averages record */
198394         -  if( rc==SQLITE_OK ){
198395         -    rc = fts5StorageSaveTotals(p);
198396         -  }
198397         -
198398 199126     return rc;
198399 199127   }
198400 199128   
198401 199129   static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
198402 199130     Fts5Config *pConfig = p->pConfig;
198403 199131     char *zSql;
198404 199132     int rc;
................................................................................
198725 199453     return rc;
198726 199454   }
198727 199455   
198728 199456   /*
198729 199457   ** Flush any data currently held in-memory to disk.
198730 199458   */
198731 199459   static int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit){
198732         -  if( bCommit && p->bTotalsValid ){
198733         -    int rc = fts5StorageSaveTotals(p);
198734         -    p->bTotalsValid = 0;
198735         -    if( rc!=SQLITE_OK ) return rc;
       199460  +  int rc = SQLITE_OK;
       199461  +  i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
       199462  +  if( p->bTotalsValid ){
       199463  +    rc = fts5StorageSaveTotals(p);
       199464  +    if( bCommit ) p->bTotalsValid = 0;
198736 199465     }
198737         -  return sqlite3Fts5IndexSync(p->pIndex, bCommit);
       199466  +  if( rc==SQLITE_OK ){
       199467  +    rc = sqlite3Fts5IndexSync(p->pIndex, bCommit);
       199468  +  }
       199469  +  sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
       199470  +  return rc;
198738 199471   }
198739 199472   
198740 199473   static int sqlite3Fts5StorageRollback(Fts5Storage *p){
198741 199474     p->bTotalsValid = 0;
198742 199475     return sqlite3Fts5IndexRollback(p->pIndex);
198743 199476   }
198744 199477   

Changes to src/sqlite3.h.

   110    110   **
   111    111   ** Since [version 3.6.18] ([dateof:3.6.18]), 
   112    112   ** SQLite source code has been stored in the
   113    113   ** <a href="http://www.fossil-scm.org/">Fossil configuration management
   114    114   ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
   115    115   ** a string which identifies a particular check-in of SQLite
   116    116   ** within its configuration management system.  ^The SQLITE_SOURCE_ID
   117         -** string contains the date and time of the check-in (UTC) and an SHA1
   118         -** hash of the entire source tree.
          117  +** string contains the date and time of the check-in (UTC) and a SHA1
          118  +** or SHA3-256 hash of the entire source tree.
   119    119   **
   120    120   ** See also: [sqlite3_libversion()],
   121    121   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   122    122   ** [sqlite_version()] and [sqlite_source_id()].
   123    123   */
   124         -#define SQLITE_VERSION        "3.17.0"
   125         -#define SQLITE_VERSION_NUMBER 3017000
   126         -#define SQLITE_SOURCE_ID      "2017-02-08 16:01:57 77b470b0df73dc5ae5ad2f0170ef7c50558c7c88"
          124  +#define SQLITE_VERSION        "3.18.0"
          125  +#define SQLITE_VERSION_NUMBER 3018000
          126  +#define SQLITE_SOURCE_ID      "2017-03-25 19:16:41 8469fc0d48d6af0accef9b8a84e08ad2ca32351907510d177b4ca4815c1ea7cb"
   127    127   
   128    128   /*
   129    129   ** CAPI3REF: Run-Time Library Version Numbers
   130    130   ** KEYWORDS: sqlite3_version sqlite3_sourceid
   131    131   **
   132    132   ** These interfaces provide the same information as the [SQLITE_VERSION],
   133    133   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  2036   2036   ** has a unique 64-bit signed
  2037   2037   ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  2038   2038   ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  2039   2039   ** names are not also used by explicitly declared columns. ^If
  2040   2040   ** the table has a column of type [INTEGER PRIMARY KEY] then that column
  2041   2041   ** is another alias for the rowid.
  2042   2042   **
  2043         -** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the 
  2044         -** most recent successful [INSERT] into a rowid table or [virtual table]
  2045         -** on database connection D.
  2046         -** ^Inserts into [WITHOUT ROWID] tables are not recorded.
  2047         -** ^If no successful [INSERT]s into rowid tables
  2048         -** have ever occurred on the database connection D, 
  2049         -** then sqlite3_last_insert_rowid(D) returns zero.
         2043  +** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
         2044  +** the most recent successful [INSERT] into a rowid table or [virtual table]
         2045  +** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
         2046  +** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred 
         2047  +** on the database connection D, then sqlite3_last_insert_rowid(D) returns 
         2048  +** zero.
  2050   2049   **
  2051         -** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
  2052         -** method, then this routine will return the [rowid] of the inserted
  2053         -** row as long as the trigger or virtual table method is running.
  2054         -** But once the trigger or virtual table method ends, the value returned 
  2055         -** by this routine reverts to what it was before the trigger or virtual
  2056         -** table method began.)^
         2050  +** As well as being set automatically as rows are inserted into database
         2051  +** tables, the value returned by this function may be set explicitly by
         2052  +** [sqlite3_set_last_insert_rowid()]
         2053  +**
         2054  +** Some virtual table implementations may INSERT rows into rowid tables as
         2055  +** part of committing a transaction (e.g. to flush data accumulated in memory
         2056  +** to disk). In this case subsequent calls to this function return the rowid
         2057  +** associated with these internal INSERT operations, which leads to 
         2058  +** unintuitive results. Virtual table implementations that do write to rowid
         2059  +** tables in this way can avoid this problem by restoring the original 
         2060  +** rowid value using [sqlite3_set_last_insert_rowid()] before returning 
         2061  +** control to the user.
         2062  +**
         2063  +** ^(If an [INSERT] occurs within a trigger then this routine will 
         2064  +** return the [rowid] of the inserted row as long as the trigger is 
         2065  +** running. Once the trigger program ends, the value returned 
         2066  +** by this routine reverts to what it was before the trigger was fired.)^
  2057   2067   **
  2058   2068   ** ^An [INSERT] that fails due to a constraint violation is not a
  2059   2069   ** successful [INSERT] and does not change the value returned by this
  2060   2070   ** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
  2061   2071   ** and INSERT OR ABORT make no changes to the return value of this
  2062   2072   ** routine when their insertion fails.  ^(When INSERT OR REPLACE
  2063   2073   ** encounters a constraint violation, it does not fail.  The
................................................................................
  2076   2086   ** function is running and thus changes the last insert [rowid],
  2077   2087   ** then the value returned by [sqlite3_last_insert_rowid()] is
  2078   2088   ** unpredictable and might not equal either the old or the new
  2079   2089   ** last insert [rowid].
  2080   2090   */
  2081   2091   SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
  2082   2092   
         2093  +/*
         2094  +** CAPI3REF: Set the Last Insert Rowid value.
         2095  +** METHOD: sqlite3
         2096  +**
         2097  +** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
         2098  +** set the value returned by calling sqlite3_last_insert_rowid(D) to R 
         2099  +** without inserting a row into the database.
         2100  +*/
         2101  +SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
         2102  +
  2083   2103   /*
  2084   2104   ** CAPI3REF: Count The Number Of Rows Modified
  2085   2105   ** METHOD: sqlite3
  2086   2106   **
  2087   2107   ** ^This function returns the number of rows modified, inserted or
  2088   2108   ** deleted by the most recently completed INSERT, UPDATE or DELETE
  2089   2109   ** statement on the database connection specified by the only parameter.
................................................................................
  3400   3420   ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
  3401   3421   **
  3402   3422   ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
  3403   3423   ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
  3404   3424   **
  3405   3425   ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
  3406   3426   ** <dd>The maximum number of instructions in a virtual machine program
  3407         -** used to implement an SQL statement.  This limit is not currently
  3408         -** enforced, though that might be added in some future release of
  3409         -** SQLite.</dd>)^
         3427  +** used to implement an SQL statement.  If [sqlite3_prepare_v2()] or
         3428  +** the equivalent tries to allocate space for more than this many opcodes
         3429  +** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
  3410   3430   **
  3411   3431   ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
  3412   3432   ** <dd>The maximum number of arguments on a function.</dd>)^
  3413   3433   **
  3414   3434   ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
  3415   3435   ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
  3416   3436   **
................................................................................
  3439   3459   #define SQLITE_LIMIT_VDBE_OP                   5
  3440   3460   #define SQLITE_LIMIT_FUNCTION_ARG              6
  3441   3461   #define SQLITE_LIMIT_ATTACHED                  7
  3442   3462   #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
  3443   3463   #define SQLITE_LIMIT_VARIABLE_NUMBER           9
  3444   3464   #define SQLITE_LIMIT_TRIGGER_DEPTH            10
  3445   3465   #define SQLITE_LIMIT_WORKER_THREADS           11
         3466  +
  3446   3467   
  3447   3468   /*
  3448   3469   ** CAPI3REF: Compiling An SQL Statement
  3449   3470   ** KEYWORDS: {SQL statement compiler}
  3450   3471   ** METHOD: sqlite3
  3451   3472   ** CONSTRUCTOR: sqlite3_stmt
  3452   3473   **