System.Data.SQLite
Check-in [50be008078]
Not logged in

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

Overview
Comment:Update SQLite core library to the latest trunk code. Update version history docs.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 50be0080788246eb6c3a654f29ec4913c42f71bb
User & Date: mistachkin 2018-05-28 23:30:03
Context
2018-05-28
23:41
Fix compilation issues due to internal changes in the SQLite core library. check-in: fb73a8c990 user: mistachkin tags: trunk
23:30
Update SQLite core library to the latest trunk code. Update version history docs. check-in: 50be008078 user: mistachkin tags: trunk
2018-05-25
03:29
Pickup changes to Eagle script library in externals. check-in: c5879a3ba6 user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to Doc/Extra/Provider/version.html.

    39     39             </td>
    40     40           </tr>
    41     41         </table>
    42     42       </div>
    43     43       <div id="mainSection">
    44     44       <div id="mainBody">
    45     45       <h1 class="heading">Version History</h1>
    46         -    <p><b>1.0.109.0 - May XX, 2018 <font color="red">(release scheduled)</font></b></p>
           46  +    <p><b>1.0.109.0 - June XX, 2018 <font color="red">(release scheduled)</font></b></p>
    47     47       <ul>
    48         -      <li>Updated to <a href="https://www.sqlite.org/releaselog/3_23_2.html">SQLite 3.23.2</a>.</li>
           48  +      <li>Updated to <a href="https://www.sqlite.org/draft/releaselog/3_24_0.html">SQLite 3.24.0</a>.</li>
    49     49         <li>Prevent GetSchemaTable from throwing InvalidCastException. Fix for <a href="https://system.data.sqlite.org/index.html/info/baf42ee135">[baf42ee135]</a>.</li>
    50     50         <li>Add preliminary support for the .NET Framework 4.7.2.</li>
    51     51         <li>Add preliminary support for .NET Core 2.0 and the .NET Standard 2.0. Pursuant to <a href="https://system.data.sqlite.org/index.html/info/5c89cecd1b">[5c89cecd1b]</a>.</li>
    52     52         <li>Add simpler overload for the SQLiteBlob.Create method. Pursuant to <a href="https://system.data.sqlite.org/index.html/info/dfc8133ba2">[dfc8133ba2]</a>.</li>
    53     53         <li>Add GetFieldAffinity method to the SQLiteDataReader class.</li>
    54     54       </ul>
    55     55       <p><b>1.0.108.0 - March 2, 2018</b></p>

Changes to SQLite.Interop/props/sqlite3.props.

     5      5    *
     6      6    * Written by Joe Mistachkin.
     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    11     11     <PropertyGroup Label="UserMacros">
    12         -    <SQLITE_MANIFEST_VERSION>3.23.2.0</SQLITE_MANIFEST_VERSION>
    13         -    <SQLITE_RC_VERSION>3,23,2,0</SQLITE_RC_VERSION>
           12  +    <SQLITE_MANIFEST_VERSION>3.24.0.0</SQLITE_MANIFEST_VERSION>
           13  +    <SQLITE_RC_VERSION>3,24,0,0</SQLITE_RC_VERSION>
    14     14       <SQLITE_COMMON_DEFINES>_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;_CRT_NONSTDC_NO_WARNINGS;SQLITE_THREADSAFE=1;SQLITE_USE_URI=1;SQLITE_ENABLE_COLUMN_METADATA=1;SQLITE_ENABLE_STAT4=1;SQLITE_ENABLE_FTS3=1;SQLITE_ENABLE_LOAD_EXTENSION=1;SQLITE_ENABLE_RTREE=1;SQLITE_SOUNDEX=1;SQLITE_ENABLE_MEMORY_MANAGEMENT=1;SQLITE_ENABLE_API_ARMOR=1;SQLITE_ENABLE_DBSTAT_VTAB=1;SQLITE_ENABLE_STMTVTAB=1</SQLITE_COMMON_DEFINES>
    15     15       <SQLITE_EXTRA_DEFINES>SQLITE_PLACEHOLDER=1;SQLITE_HAS_CODEC=1</SQLITE_EXTRA_DEFINES>
    16     16       <SQLITE_WINCE_200X_DEFINES>SQLITE_OMIT_WAL=1</SQLITE_WINCE_200X_DEFINES>
    17     17       <SQLITE_WINCE_2013_DEFINES>HAVE_ERRNO_H=1;SQLITE_MSVC_LOCALTIME_API=1</SQLITE_WINCE_2013_DEFINES>
    18     18       <SQLITE_DEBUG_DEFINES>SQLITE_DEBUG=1;SQLITE_MEMDEBUG=1;SQLITE_ENABLE_EXPENSIVE_ASSERT=1</SQLITE_DEBUG_DEFINES>
    19     19       <SQLITE_RELEASE_DEFINES>SQLITE_WIN32_MALLOC=1</SQLITE_RELEASE_DEFINES>
    20     20       <SQLITE_DISABLE_WARNINGS>4055;4100;4127;4146;4210;4232;4244;4245;4267;4306;4389;4701;4703;4706</SQLITE_DISABLE_WARNINGS>

Changes to SQLite.Interop/props/sqlite3.vsprops.

    10     10   <VisualStudioPropertySheet
    11     11   	ProjectType="Visual C++"
    12     12   	Version="8.00"
    13     13   	Name="sqlite3"
    14     14   	>
    15     15   	<UserMacro
    16     16   		Name="SQLITE_MANIFEST_VERSION"
    17         -		Value="3.23.2.0"
           17  +		Value="3.24.0.0"
    18     18   		PerformEnvironmentSet="true"
    19     19   	/>
    20     20   	<UserMacro
    21     21   		Name="SQLITE_RC_VERSION"
    22         -		Value="3,23,2,0"
           22  +		Value="3,24,0,0"
    23     23   		PerformEnvironmentSet="true"
    24     24   	/>
    25     25   	<UserMacro
    26     26   		Name="SQLITE_COMMON_DEFINES"
    27     27   		Value="_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;_CRT_NONSTDC_NO_WARNINGS;SQLITE_THREADSAFE=1;SQLITE_USE_URI=1;SQLITE_ENABLE_COLUMN_METADATA=1;SQLITE_ENABLE_STAT4=1;SQLITE_ENABLE_FTS3=1;SQLITE_ENABLE_LOAD_EXTENSION=1;SQLITE_ENABLE_RTREE=1;SQLITE_SOUNDEX=1;SQLITE_ENABLE_MEMORY_MANAGEMENT=1;SQLITE_ENABLE_API_ARMOR=1;SQLITE_ENABLE_DBSTAT_VTAB=1;SQLITE_ENABLE_STMTVTAB=1"
    28     28   		PerformEnvironmentSet="true"
    29     29   	/>

Changes to SQLite.Interop/src/core/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.23.2.  By combining all the individual C code files into this
            3  +** version 3.24.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
................................................................................
   306    306     "ENABLE_SELECTTRACE",
   307    307   #endif
   308    308   #if SQLITE_ENABLE_SESSION
   309    309     "ENABLE_SESSION",
   310    310   #endif
   311    311   #if SQLITE_ENABLE_SNAPSHOT
   312    312     "ENABLE_SNAPSHOT",
          313  +#endif
          314  +#if SQLITE_ENABLE_SORTER_REFERENCES
          315  +  "ENABLE_SORTER_REFERENCES",
   313    316   #endif
   314    317   #if SQLITE_ENABLE_SQLLOG
   315    318     "ENABLE_SQLLOG",
   316    319   #endif
   317    320   #if defined(SQLITE_ENABLE_STAT4)
   318    321     "ENABLE_STAT4",
   319    322   #elif defined(SQLITE_ENABLE_STAT3)
................................................................................
  1143   1146   ** been edited in any way since it was last checked in, then the last
  1144   1147   ** four hexadecimal digits of the hash may be modified.
  1145   1148   **
  1146   1149   ** See also: [sqlite3_libversion()],
  1147   1150   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1148   1151   ** [sqlite_version()] and [sqlite_source_id()].
  1149   1152   */
  1150         -#define SQLITE_VERSION        "3.23.2"
  1151         -#define SQLITE_VERSION_NUMBER 3023002
  1152         -#define SQLITE_SOURCE_ID      "2018-04-28 01:48:40 f677065aab61037914fc980456caaa60132a19b387d6d717ec1a6b6fa7c6alt1"
         1153  +#define SQLITE_VERSION        "3.24.0"
         1154  +#define SQLITE_VERSION_NUMBER 3024000
         1155  +#define SQLITE_SOURCE_ID      "2018-05-28 18:29:46 469a62ca33081854e54f3af6d93ab5a350484b149c8c8c4ee8be0ae5418382d9"
  1153   1156   
  1154   1157   /*
  1155   1158   ** CAPI3REF: Run-Time Library Version Numbers
  1156   1159   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1157   1160   **
  1158   1161   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1159   1162   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1524   1527   #define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
  1525   1528   #define SQLITE_IOERR_VNODE             (SQLITE_IOERR | (27<<8))
  1526   1529   #define SQLITE_IOERR_AUTH              (SQLITE_IOERR | (28<<8))
  1527   1530   #define SQLITE_IOERR_BEGIN_ATOMIC      (SQLITE_IOERR | (29<<8))
  1528   1531   #define SQLITE_IOERR_COMMIT_ATOMIC     (SQLITE_IOERR | (30<<8))
  1529   1532   #define SQLITE_IOERR_ROLLBACK_ATOMIC   (SQLITE_IOERR | (31<<8))
  1530   1533   #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
         1534  +#define SQLITE_LOCKED_VTAB             (SQLITE_LOCKED |  (2<<8))
  1531   1535   #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
  1532   1536   #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
  1533   1537   #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
  1534   1538   #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
  1535   1539   #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
  1536   1540   #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
  1537   1541   #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
         1542  +#define SQLITE_CORRUPT_SEQUENCE        (SQLITE_CORRUPT | (2<<8))
  1538   1543   #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
  1539   1544   #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
  1540   1545   #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
  1541   1546   #define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
  1542   1547   #define SQLITE_READONLY_CANTINIT       (SQLITE_READONLY | (5<<8))
  1543   1548   #define SQLITE_READONLY_DIRECTORY      (SQLITE_READONLY | (6<<8))
  1544   1549   #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
................................................................................
  2950   2955   ** Or if the threshold is -1, statement journals are always held
  2951   2956   ** exclusively in memory.
  2952   2957   ** Since many statement journals never become large, setting the spill
  2953   2958   ** threshold to a value such as 64KiB can greatly reduce the amount of
  2954   2959   ** I/O required to support statement rollback.
  2955   2960   ** The default value for this setting is controlled by the
  2956   2961   ** [SQLITE_STMTJRNL_SPILL] compile-time option.
         2962  +**
         2963  +** [[SQLITE_CONFIG_SORTERREF_SIZE]]
         2964  +** <dt>SQLITE_CONFIG_SORTERREF_SIZE
         2965  +** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
         2966  +** of type (int) - the new value of the sorter-reference size threshold.
         2967  +** Usually, when SQLite uses an external sort to order records according
         2968  +** to an ORDER BY clause, all fields required by the caller are present in the
         2969  +** sorted records. However, if SQLite determines based on the declared type
         2970  +** of a table column that its values are likely to be very large - larger
         2971  +** than the configured sorter-reference size threshold - then a reference
         2972  +** is stored in each sorted record and the required column values loaded
         2973  +** from the database as records are returned in sorted order. The default
         2974  +** value for this option is to never use this optimization. Specifying a 
         2975  +** negative value for this option restores the default behaviour.
         2976  +** This option is only available if SQLite is compiled with the
         2977  +** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
  2957   2978   ** </dl>
  2958   2979   */
  2959   2980   #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  2960   2981   #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  2961   2982   #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
  2962   2983   #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
  2963   2984   #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
................................................................................
  2979   3000   #define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
  2980   3001   #define SQLITE_CONFIG_MMAP_SIZE    22  /* sqlite3_int64, sqlite3_int64 */
  2981   3002   #define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
  2982   3003   #define SQLITE_CONFIG_PCACHE_HDRSZ        24  /* int *psz */
  2983   3004   #define SQLITE_CONFIG_PMASZ               25  /* unsigned int szPma */
  2984   3005   #define SQLITE_CONFIG_STMTJRNL_SPILL      26  /* int nByte */
  2985   3006   #define SQLITE_CONFIG_SMALL_MALLOC        27  /* boolean */
         3007  +#define SQLITE_CONFIG_SORTERREF_SIZE      28  /* int nByte */
  2986   3008   
  2987   3009   /*
  2988   3010   ** CAPI3REF: Database Connection Configuration Options
  2989   3011   **
  2990   3012   ** These constants are the available integer configuration options that
  2991   3013   ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  2992   3014   **
................................................................................
  3115   3137   ** behavior. The first parameter passed to this operation is an integer -
  3116   3138   ** positive to enable output for trigger programs, or zero to disable it,
  3117   3139   ** or negative to leave the setting unchanged.
  3118   3140   ** The second parameter is a pointer to an integer into which is written 
  3119   3141   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  3120   3142   ** it is not disabled, 1 if it is.  
  3121   3143   ** </dd>
         3144  +**
         3145  +** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
         3146  +** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
         3147  +** [VACUUM] in order to reset a database back to an empty database
         3148  +** with no schema and no content. The following process works even for
         3149  +** a badly corrupted database file:
         3150  +** <ol>
         3151  +** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
         3152  +** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
         3153  +** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
         3154  +** </ol>
         3155  +** Because resetting a database is destructive and irreversible, the
         3156  +** process requires the use of this obscure API and multiple steps to help
         3157  +** ensure that it does not happen by accident.
         3158  +** </dd>
  3122   3159   ** </dl>
  3123   3160   */
  3124   3161   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  3125   3162   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  3126   3163   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  3127   3164   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  3128   3165   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  3129   3166   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  3130   3167   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  3131   3168   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  3132   3169   #define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
  3133         -#define SQLITE_DBCONFIG_MAX                   1008 /* Largest DBCONFIG */
         3170  +#define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
         3171  +#define SQLITE_DBCONFIG_MAX                   1009 /* Largest DBCONFIG */
  3134   3172   
  3135   3173   /*
  3136   3174   ** CAPI3REF: Enable Or Disable Extended Result Codes
  3137   3175   ** METHOD: sqlite3
  3138   3176   **
  3139   3177   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  3140   3178   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  7279   7317     int idxFlags;              /* Mask of SQLITE_INDEX_SCAN_* flags */
  7280   7318     /* Fields below are only available in SQLite 3.10.0 and later */
  7281   7319     sqlite3_uint64 colUsed;    /* Input: Mask of columns used by statement */
  7282   7320   };
  7283   7321   
  7284   7322   /*
  7285   7323   ** CAPI3REF: Virtual Table Scan Flags
         7324  +**
         7325  +** Virtual table implementations are allowed to set the 
         7326  +** [sqlite3_index_info].idxFlags field to some combination of
         7327  +** these bits.
  7286   7328   */
  7287   7329   #define SQLITE_INDEX_SCAN_UNIQUE      1     /* Scan visits at most 1 row */
  7288   7330   
  7289   7331   /*
  7290   7332   ** CAPI3REF: Virtual Table Constraint Operator Codes
  7291   7333   **
  7292   7334   ** These macros defined the allowed values for the
................................................................................
  8054   8096   #define SQLITE_TESTCTRL_FAULT_INSTALL            9
  8055   8097   #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
  8056   8098   #define SQLITE_TESTCTRL_PENDING_BYTE            11
  8057   8099   #define SQLITE_TESTCTRL_ASSERT                  12
  8058   8100   #define SQLITE_TESTCTRL_ALWAYS                  13
  8059   8101   #define SQLITE_TESTCTRL_RESERVE                 14
  8060   8102   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  8061         -#define SQLITE_TESTCTRL_ISKEYWORD               16
         8103  +#define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
  8062   8104   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
  8063   8105   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  8064   8106   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
  8065   8107   #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  8066   8108   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  8067   8109   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  8068   8110   #define SQLITE_TESTCTRL_BYTEORDER               22
  8069   8111   #define SQLITE_TESTCTRL_ISINIT                  23
  8070   8112   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  8071   8113   #define SQLITE_TESTCTRL_IMPOSTER                25
  8072   8114   #define SQLITE_TESTCTRL_PARSER_COVERAGE         26
  8073   8115   #define SQLITE_TESTCTRL_LAST                    26  /* Largest TESTCTRL */
  8074   8116   
         8117  +/*
         8118  +** CAPI3REF: SQL Keyword Checking
         8119  +**
         8120  +** These routines provide access to the set of SQL language keywords 
         8121  +** recognized by SQLite.  Applications can uses these routines to determine
         8122  +** whether or not a specific identifier needs to be escaped (for example,
         8123  +** by enclosing in double-quotes) so as not to confuse the parser.
         8124  +**
         8125  +** The sqlite3_keyword_count() interface returns the number of distinct
         8126  +** keywords understood by SQLite.
         8127  +**
         8128  +** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
         8129  +** makes *Z point to that keyword expressed as UTF8 and writes the number
         8130  +** of bytes in the keyword into *L.  The string that *Z points to is not
         8131  +** zero-terminated.  The sqlite3_keyword_name(N,Z,L) routine returns
         8132  +** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
         8133  +** or L are NULL or invalid pointers then calls to
         8134  +** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
         8135  +**
         8136  +** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
         8137  +** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
         8138  +** if it is and zero if not.
         8139  +**
         8140  +** The parser used by SQLite is forgiving.  It is often possible to use
         8141  +** a keyword as an identifier as long as such use does not result in a
         8142  +** parsing ambiguity.  For example, the statement
         8143  +** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
         8144  +** creates a new table named "BEGIN" with three columns named
         8145  +** "REPLACE", "PRAGMA", and "END".  Nevertheless, best practice is to avoid
         8146  +** using keywords as identifiers.  Common techniques used to avoid keyword
         8147  +** name collisions include:
         8148  +** <ul>
         8149  +** <li> Put all identifier names inside double-quotes.  This is the official
         8150  +**      SQL way to escape identifier names.
         8151  +** <li> Put identifier names inside &#91;...&#93;.  This is not standard SQL,
         8152  +**      but it is what SQL Server does and so lots of programmers use this
         8153  +**      technique.
         8154  +** <li> Begin every identifier with the letter "Z" as no SQL keywords start
         8155  +**      with "Z".
         8156  +** <li> Include a digit somewhere in every identifier name.
         8157  +** </ul>
         8158  +**
         8159  +** Note that the number of keywords understood by SQLite can depend on
         8160  +** compile-time options.  For example, "VACUUM" is not a keyword if
         8161  +** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option.  Also,
         8162  +** new keywords may be added to future releases of SQLite.
         8163  +*/
         8164  +SQLITE_API int sqlite3_keyword_count(void);
         8165  +SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
         8166  +SQLITE_API int sqlite3_keyword_check(const char*,int);
         8167  +
         8168  +/*
         8169  +** CAPI3REF: Dynamic String Object
         8170  +** KEYWORDS: {dynamic string}
         8171  +**
         8172  +** An instance of the sqlite3_str object contains a dynamically-sized
         8173  +** string under construction.
         8174  +**
         8175  +** The lifecycle of an sqlite3_str object is as follows:
         8176  +** <ol>
         8177  +** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
         8178  +** <li> ^Text is appended to the sqlite3_str object using various
         8179  +** methods, such as [sqlite3_str_appendf()].
         8180  +** <li> ^The sqlite3_str object is destroyed and the string it created
         8181  +** is returned using the [sqlite3_str_finish()] interface.
         8182  +** </ol>
         8183  +*/
         8184  +typedef struct sqlite3_str sqlite3_str;
         8185  +
         8186  +/*
         8187  +** CAPI3REF: Create A New Dynamic String Object
         8188  +** CONSTRUCTOR: sqlite3_str
         8189  +**
         8190  +** ^The [sqlite3_str_new(D)] interface allocates and initializes
         8191  +** a new [sqlite3_str] object.  To avoid memory leaks, the object returned by
         8192  +** [sqlite3_str_new()] must be freed by a subsequent call to 
         8193  +** [sqlite3_str_finish(X)].
         8194  +**
         8195  +** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
         8196  +** valid [sqlite3_str] object, though in the event of an out-of-memory
         8197  +** error the returned object might be a special singleton that will
         8198  +** silently reject new text, always return SQLITE_NOMEM from 
         8199  +** [sqlite3_str_errcode()], always return 0 for 
         8200  +** [sqlite3_str_length()], and always return NULL from
         8201  +** [sqlite3_str_finish(X)].  It is always safe to use the value
         8202  +** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
         8203  +** to any of the other [sqlite3_str] methods.
         8204  +**
         8205  +** The D parameter to [sqlite3_str_new(D)] may be NULL.  If the
         8206  +** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
         8207  +** length of the string contained in the [sqlite3_str] object will be
         8208  +** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
         8209  +** of [SQLITE_MAX_LENGTH].
         8210  +*/
         8211  +SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
         8212  +
         8213  +/*
         8214  +** CAPI3REF: Finalize A Dynamic String
         8215  +** DESTRUCTOR: sqlite3_str
         8216  +**
         8217  +** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
         8218  +** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
         8219  +** that contains the constructed string.  The calling application should
         8220  +** pass the returned value to [sqlite3_free()] to avoid a memory leak.
         8221  +** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
         8222  +** errors were encountered during construction of the string.  ^The
         8223  +** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
         8224  +** string in [sqlite3_str] object X is zero bytes long.
         8225  +*/
         8226  +SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
         8227  +
         8228  +/*
         8229  +** CAPI3REF: Add Content To A Dynamic String
         8230  +** METHOD: sqlite3_str
         8231  +**
         8232  +** These interfaces add content to an sqlite3_str object previously obtained
         8233  +** from [sqlite3_str_new()].
         8234  +**
         8235  +** ^The [sqlite3_str_appendf(X,F,...)] and 
         8236  +** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
         8237  +** functionality of SQLite to append formatted text onto the end of 
         8238  +** [sqlite3_str] object X.
         8239  +**
         8240  +** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
         8241  +** onto the end of the [sqlite3_str] object X.  N must be non-negative.
         8242  +** S must contain at least N non-zero bytes of content.  To append a
         8243  +** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
         8244  +** method instead.
         8245  +**
         8246  +** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
         8247  +** zero-terminated string S onto the end of [sqlite3_str] object X.
         8248  +**
         8249  +** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
         8250  +** single-byte character C onto the end of [sqlite3_str] object X.
         8251  +** ^This method can be used, for example, to add whitespace indentation.
         8252  +**
         8253  +** ^The [sqlite3_str_reset(X)] method resets the string under construction
         8254  +** inside [sqlite3_str] object X back to zero bytes in length.  
         8255  +**
         8256  +** These methods do not return a result code.  ^If an error occurs, that fact
         8257  +** is recorded in the [sqlite3_str] object and can be recovered by a
         8258  +** subsequent call to [sqlite3_str_errcode(X)].
         8259  +*/
         8260  +SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
         8261  +SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
         8262  +SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
         8263  +SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
         8264  +SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
         8265  +SQLITE_API void sqlite3_str_reset(sqlite3_str*);
         8266  +
         8267  +/*
         8268  +** CAPI3REF: Status Of A Dynamic String
         8269  +** METHOD: sqlite3_str
         8270  +**
         8271  +** These interfaces return the current status of an [sqlite3_str] object.
         8272  +**
         8273  +** ^If any prior errors have occurred while constructing the dynamic string
         8274  +** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
         8275  +** an appropriate error code.  ^The [sqlite3_str_errcode(X)] method returns
         8276  +** [SQLITE_NOMEM] following any out-of-memory error, or
         8277  +** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
         8278  +** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
         8279  +**
         8280  +** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
         8281  +** of the dynamic string under construction in [sqlite3_str] object X.
         8282  +** ^The length returned by [sqlite3_str_length(X)] does not include the
         8283  +** zero-termination byte.
         8284  +**
         8285  +** ^The [sqlite3_str_value(X)] method returns a pointer to the current
         8286  +** content of the dynamic string under construction in X.  The value
         8287  +** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
         8288  +** and might be freed or altered by any subsequent method on the same
         8289  +** [sqlite3_str] object.  Applications must not used the pointer returned
         8290  +** [sqlite3_str_value(X)] after any subsequent method call on the same
         8291  +** object.  ^Applications may change the content of the string returned
         8292  +** by [sqlite3_str_value(X)] as long as they do not write into any bytes
         8293  +** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
         8294  +** write any byte after any subsequent sqlite3_str method call.
         8295  +*/
         8296  +SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
         8297  +SQLITE_API int sqlite3_str_length(sqlite3_str*);
         8298  +SQLITE_API char *sqlite3_str_value(sqlite3_str*);
         8299  +
  8075   8300   /*
  8076   8301   ** CAPI3REF: SQLite Runtime Status
  8077   8302   **
  8078   8303   ** ^These interfaces are used to retrieve runtime status information
  8079   8304   ** about the performance of SQLite, and optionally to reset various
  8080   8305   ** highwater marks.  ^The first argument is an integer code for
  8081   8306   ** the specific parameter to measure.  ^(Recognized integer codes
................................................................................
  9337   9562   /*
  9338   9563   ** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
  9339   9564   **
  9340   9565   ** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
  9341   9566   ** method of a [virtual table], then it returns true if and only if the
  9342   9567   ** column is being fetched as part of an UPDATE operation during which the
  9343   9568   ** column value will not change.  Applications might use this to substitute
  9344         -** a lighter-weight value to return that the corresponding [xUpdate] method
  9345         -** understands as a "no-change" value.
         9569  +** a return value that is less expensive to compute and that the corresponding
         9570  +** [xUpdate] method understands as a "no-change" value.
  9346   9571   **
  9347   9572   ** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
  9348         -** the column is not changed by the UPDATE statement, they the xColumn
         9573  +** the column is not changed by the UPDATE statement, then the xColumn
  9349   9574   ** method can optionally return without setting a result, without calling
  9350   9575   ** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
  9351   9576   ** In that case, [sqlite3_value_nochange(X)] will return true for the
  9352   9577   ** same column in the [xUpdate] method.
  9353   9578   */
  9354   9579   SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
  9355   9580   
................................................................................
 12972  13197   #define TK_GT                              54
 12973  13198   #define TK_LE                              55
 12974  13199   #define TK_LT                              56
 12975  13200   #define TK_GE                              57
 12976  13201   #define TK_ESCAPE                          58
 12977  13202   #define TK_ID                              59
 12978  13203   #define TK_COLUMNKW                        60
 12979         -#define TK_FOR                             61
 12980         -#define TK_IGNORE                          62
 12981         -#define TK_INITIALLY                       63
 12982         -#define TK_INSTEAD                         64
 12983         -#define TK_NO                              65
 12984         -#define TK_KEY                             66
 12985         -#define TK_OF                              67
 12986         -#define TK_OFFSET                          68
 12987         -#define TK_PRAGMA                          69
 12988         -#define TK_RAISE                           70
 12989         -#define TK_RECURSIVE                       71
 12990         -#define TK_REPLACE                         72
 12991         -#define TK_RESTRICT                        73
 12992         -#define TK_ROW                             74
 12993         -#define TK_TRIGGER                         75
 12994         -#define TK_VACUUM                          76
 12995         -#define TK_VIEW                            77
 12996         -#define TK_VIRTUAL                         78
 12997         -#define TK_WITH                            79
 12998         -#define TK_REINDEX                         80
 12999         -#define TK_RENAME                          81
 13000         -#define TK_CTIME_KW                        82
 13001         -#define TK_ANY                             83
 13002         -#define TK_BITAND                          84
 13003         -#define TK_BITOR                           85
 13004         -#define TK_LSHIFT                          86
 13005         -#define TK_RSHIFT                          87
 13006         -#define TK_PLUS                            88
 13007         -#define TK_MINUS                           89
 13008         -#define TK_STAR                            90
 13009         -#define TK_SLASH                           91
 13010         -#define TK_REM                             92
 13011         -#define TK_CONCAT                          93
 13012         -#define TK_COLLATE                         94
 13013         -#define TK_BITNOT                          95
 13014         -#define TK_INDEXED                         96
 13015         -#define TK_STRING                          97
 13016         -#define TK_JOIN_KW                         98
 13017         -#define TK_CONSTRAINT                      99
 13018         -#define TK_DEFAULT                        100
 13019         -#define TK_NULL                           101
 13020         -#define TK_PRIMARY                        102
 13021         -#define TK_UNIQUE                         103
 13022         -#define TK_CHECK                          104
 13023         -#define TK_REFERENCES                     105
 13024         -#define TK_AUTOINCR                       106
 13025         -#define TK_ON                             107
 13026         -#define TK_INSERT                         108
 13027         -#define TK_DELETE                         109
 13028         -#define TK_UPDATE                         110
 13029         -#define TK_SET                            111
 13030         -#define TK_DEFERRABLE                     112
 13031         -#define TK_FOREIGN                        113
 13032         -#define TK_DROP                           114
 13033         -#define TK_UNION                          115
 13034         -#define TK_ALL                            116
 13035         -#define TK_EXCEPT                         117
 13036         -#define TK_INTERSECT                      118
 13037         -#define TK_SELECT                         119
 13038         -#define TK_VALUES                         120
 13039         -#define TK_DISTINCT                       121
 13040         -#define TK_DOT                            122
 13041         -#define TK_FROM                           123
 13042         -#define TK_JOIN                           124
 13043         -#define TK_USING                          125
 13044         -#define TK_ORDER                          126
 13045         -#define TK_GROUP                          127
 13046         -#define TK_HAVING                         128
 13047         -#define TK_LIMIT                          129
 13048         -#define TK_WHERE                          130
 13049         -#define TK_INTO                           131
 13050         -#define TK_FLOAT                          132
 13051         -#define TK_BLOB                           133
 13052         -#define TK_INTEGER                        134
 13053         -#define TK_VARIABLE                       135
 13054         -#define TK_CASE                           136
 13055         -#define TK_WHEN                           137
 13056         -#define TK_THEN                           138
 13057         -#define TK_ELSE                           139
 13058         -#define TK_INDEX                          140
 13059         -#define TK_ALTER                          141
 13060         -#define TK_ADD                            142
 13061         -#define TK_TRUEFALSE                      143
 13062         -#define TK_ISNOT                          144
 13063         -#define TK_FUNCTION                       145
 13064         -#define TK_COLUMN                         146
 13065         -#define TK_AGG_FUNCTION                   147
 13066         -#define TK_AGG_COLUMN                     148
 13067         -#define TK_UMINUS                         149
 13068         -#define TK_UPLUS                          150
 13069         -#define TK_TRUTH                          151
 13070         -#define TK_REGISTER                       152
 13071         -#define TK_VECTOR                         153
 13072         -#define TK_SELECT_COLUMN                  154
 13073         -#define TK_IF_NULL_ROW                    155
 13074         -#define TK_ASTERISK                       156
 13075         -#define TK_SPAN                           157
 13076         -#define TK_END_OF_FILE                    158
 13077         -#define TK_UNCLOSED_STRING                159
 13078         -#define TK_SPACE                          160
 13079         -#define TK_ILLEGAL                        161
        13204  +#define TK_DO                              61
        13205  +#define TK_FOR                             62
        13206  +#define TK_IGNORE                          63
        13207  +#define TK_INITIALLY                       64
        13208  +#define TK_INSTEAD                         65
        13209  +#define TK_NO                              66
        13210  +#define TK_KEY                             67
        13211  +#define TK_OF                              68
        13212  +#define TK_OFFSET                          69
        13213  +#define TK_PRAGMA                          70
        13214  +#define TK_RAISE                           71
        13215  +#define TK_RECURSIVE                       72
        13216  +#define TK_REPLACE                         73
        13217  +#define TK_RESTRICT                        74
        13218  +#define TK_ROW                             75
        13219  +#define TK_TRIGGER                         76
        13220  +#define TK_VACUUM                          77
        13221  +#define TK_VIEW                            78
        13222  +#define TK_VIRTUAL                         79
        13223  +#define TK_WITH                            80
        13224  +#define TK_REINDEX                         81
        13225  +#define TK_RENAME                          82
        13226  +#define TK_CTIME_KW                        83
        13227  +#define TK_ANY                             84
        13228  +#define TK_BITAND                          85
        13229  +#define TK_BITOR                           86
        13230  +#define TK_LSHIFT                          87
        13231  +#define TK_RSHIFT                          88
        13232  +#define TK_PLUS                            89
        13233  +#define TK_MINUS                           90
        13234  +#define TK_STAR                            91
        13235  +#define TK_SLASH                           92
        13236  +#define TK_REM                             93
        13237  +#define TK_CONCAT                          94
        13238  +#define TK_COLLATE                         95
        13239  +#define TK_BITNOT                          96
        13240  +#define TK_ON                              97
        13241  +#define TK_INDEXED                         98
        13242  +#define TK_STRING                          99
        13243  +#define TK_JOIN_KW                        100
        13244  +#define TK_CONSTRAINT                     101
        13245  +#define TK_DEFAULT                        102
        13246  +#define TK_NULL                           103
        13247  +#define TK_PRIMARY                        104
        13248  +#define TK_UNIQUE                         105
        13249  +#define TK_CHECK                          106
        13250  +#define TK_REFERENCES                     107
        13251  +#define TK_AUTOINCR                       108
        13252  +#define TK_INSERT                         109
        13253  +#define TK_DELETE                         110
        13254  +#define TK_UPDATE                         111
        13255  +#define TK_SET                            112
        13256  +#define TK_DEFERRABLE                     113
        13257  +#define TK_FOREIGN                        114
        13258  +#define TK_DROP                           115
        13259  +#define TK_UNION                          116
        13260  +#define TK_ALL                            117
        13261  +#define TK_EXCEPT                         118
        13262  +#define TK_INTERSECT                      119
        13263  +#define TK_SELECT                         120
        13264  +#define TK_VALUES                         121
        13265  +#define TK_DISTINCT                       122
        13266  +#define TK_DOT                            123
        13267  +#define TK_FROM                           124
        13268  +#define TK_JOIN                           125
        13269  +#define TK_USING                          126
        13270  +#define TK_ORDER                          127
        13271  +#define TK_GROUP                          128
        13272  +#define TK_HAVING                         129
        13273  +#define TK_LIMIT                          130
        13274  +#define TK_WHERE                          131
        13275  +#define TK_INTO                           132
        13276  +#define TK_NOTHING                        133
        13277  +#define TK_FLOAT                          134
        13278  +#define TK_BLOB                           135
        13279  +#define TK_INTEGER                        136
        13280  +#define TK_VARIABLE                       137
        13281  +#define TK_CASE                           138
        13282  +#define TK_WHEN                           139
        13283  +#define TK_THEN                           140
        13284  +#define TK_ELSE                           141
        13285  +#define TK_INDEX                          142
        13286  +#define TK_ALTER                          143
        13287  +#define TK_ADD                            144
        13288  +#define TK_TRUEFALSE                      145
        13289  +#define TK_ISNOT                          146
        13290  +#define TK_FUNCTION                       147
        13291  +#define TK_COLUMN                         148
        13292  +#define TK_AGG_FUNCTION                   149
        13293  +#define TK_AGG_COLUMN                     150
        13294  +#define TK_UMINUS                         151
        13295  +#define TK_UPLUS                          152
        13296  +#define TK_TRUTH                          153
        13297  +#define TK_REGISTER                       154
        13298  +#define TK_VECTOR                         155
        13299  +#define TK_SELECT_COLUMN                  156
        13300  +#define TK_IF_NULL_ROW                    157
        13301  +#define TK_ASTERISK                       158
        13302  +#define TK_SPAN                           159
        13303  +#define TK_END_OF_FILE                    160
        13304  +#define TK_UNCLOSED_STRING                161
        13305  +#define TK_SPACE                          162
        13306  +#define TK_ILLEGAL                        163
 13080  13307   
 13081  13308   /* The token codes above must all fit in 8 bits */
 13082  13309   #define TKFLG_MASK           0xff  
 13083  13310   
 13084  13311   /* Flags that can be added to a token code when it is not
 13085  13312   ** being stored in a u8: */
 13086  13313   #define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
................................................................................
 13192  13419   ** The default value of "20" was choosen to minimize the run-time of the
 13193  13420   ** speedtest1 test program with options: --shrink-memory --reprepare
 13194  13421   */
 13195  13422   #ifndef SQLITE_DEFAULT_PCACHE_INITSZ
 13196  13423   # define SQLITE_DEFAULT_PCACHE_INITSZ 20
 13197  13424   #endif
 13198  13425   
        13426  +/*
        13427  +** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
        13428  +*/
        13429  +#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
        13430  +# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
        13431  +#endif
        13432  +
 13199  13433   /*
 13200  13434   ** The compile-time options SQLITE_MMAP_READWRITE and 
 13201  13435   ** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
 13202  13436   ** You must choose one or the other (or neither) but not both.
 13203  13437   */
 13204  13438   #if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
 13205  13439   #error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
................................................................................
 13641  13875   typedef struct PrintfArguments PrintfArguments;
 13642  13876   typedef struct RowSet RowSet;
 13643  13877   typedef struct Savepoint Savepoint;
 13644  13878   typedef struct Select Select;
 13645  13879   typedef struct SQLiteThread SQLiteThread;
 13646  13880   typedef struct SelectDest SelectDest;
 13647  13881   typedef struct SrcList SrcList;
 13648         -typedef struct StrAccum StrAccum;
        13882  +typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
 13649  13883   typedef struct Table Table;
 13650  13884   typedef struct TableLock TableLock;
 13651  13885   typedef struct Token Token;
 13652  13886   typedef struct TreeView TreeView;
 13653  13887   typedef struct Trigger Trigger;
 13654  13888   typedef struct TriggerPrg TriggerPrg;
 13655  13889   typedef struct TriggerStep TriggerStep;
 13656  13890   typedef struct UnpackedRecord UnpackedRecord;
        13891  +typedef struct Upsert Upsert;
 13657  13892   typedef struct VTable VTable;
 13658  13893   typedef struct VtabCtx VtabCtx;
 13659  13894   typedef struct Walker Walker;
 13660  13895   typedef struct WhereInfo WhereInfo;
 13661  13896   typedef struct With With;
 13662  13897   
 13663  13898   /* A VList object records a mapping between parameters/variables/wildcards
................................................................................
 13932  14167   #define BTREE_AUXDELETE    0x04  /* not the primary delete operation */
 13933  14168   #define BTREE_APPEND       0x08  /* Insert is likely an append */
 13934  14169   
 13935  14170   /* An instance of the BtreePayload object describes the content of a single
 13936  14171   ** entry in either an index or table btree.
 13937  14172   **
 13938  14173   ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
 13939         -** an arbitrary key and no data.  These btrees have pKey,nKey set to their
 13940         -** key and pData,nData,nZero set to zero.
        14174  +** an arbitrary key and no data.  These btrees have pKey,nKey set to the
        14175  +** key and the pData,nData,nZero fields are uninitialized.  The aMem,nMem
        14176  +** fields give an array of Mem objects that are a decomposition of the key.
        14177  +** The nMem field might be zero, indicating that no decomposition is available.
 13941  14178   **
 13942  14179   ** Table btrees (used for rowid tables) contain an integer rowid used as
 13943  14180   ** the key and passed in the nKey field.  The pKey field is zero.  
 13944  14181   ** pData,nData hold the content of the new entry.  nZero extra zero bytes
 13945  14182   ** are appended to the end of the content when constructing the entry.
        14183  +** The aMem,nMem fields are uninitialized for table btrees.
        14184  +**
        14185  +** Field usage summary:
        14186  +**
        14187  +**               Table BTrees                   Index Btrees
        14188  +**
        14189  +**   pKey        always NULL                    encoded key
        14190  +**   nKey        the ROWID                      length of pKey
        14191  +**   pData       data                           not used
        14192  +**   aMem        not used                       decomposed key value
        14193  +**   nMem        not used                       entries in aMem
        14194  +**   nData       length of pData                not used
        14195  +**   nZero       extra zeros after pData        not used
 13946  14196   **
 13947  14197   ** This object is used to pass information into sqlite3BtreeInsert().  The
 13948  14198   ** same information used to be passed as five separate parameters.  But placing
 13949  14199   ** the information into this object helps to keep the interface more 
 13950  14200   ** organized and understandable, and it also helps the resulting code to
 13951  14201   ** run a little faster by using fewer registers for parameter passing.
 13952  14202   */
 13953  14203   struct BtreePayload {
 13954  14204     const void *pKey;       /* Key content for indexes.  NULL for tables */
 13955  14205     sqlite3_int64 nKey;     /* Size of pKey for indexes.  PRIMARY KEY for tabs */
 13956         -  const void *pData;      /* Data for tables.  NULL for indexes */
        14206  +  const void *pData;      /* Data for tables. */
 13957  14207     sqlite3_value *aMem;    /* First of nMem value in the unpacked pKey */
 13958  14208     u16 nMem;               /* Number of aMem[] value.  Might be zero */
 13959  14209     int nData;              /* Size of pData.  0 if none. */
 13960  14210     int nZero;              /* Extra zero data appended after pData,nData */
 13961  14211   };
 13962  14212   
 13963  14213   SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
................................................................................
 14307  14557   #define OP_IntCopy        77 /* synopsis: r[P2]=r[P1]                      */
 14308  14558   #define OP_ResultRow      78 /* synopsis: output=r[P1@P2]                  */
 14309  14559   #define OP_CollSeq        79
 14310  14560   #define OP_AddImm         80 /* synopsis: r[P1]=r[P1]+P2                   */
 14311  14561   #define OP_RealAffinity   81
 14312  14562   #define OP_Cast           82 /* synopsis: affinity(r[P1])                  */
 14313  14563   #define OP_Permutation    83
 14314         -#define OP_BitAnd         84 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
 14315         -#define OP_BitOr          85 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
 14316         -#define OP_ShiftLeft      86 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
 14317         -#define OP_ShiftRight     87 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
 14318         -#define OP_Add            88 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
 14319         -#define OP_Subtract       89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 14320         -#define OP_Multiply       90 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 14321         -#define OP_Divide         91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 14322         -#define OP_Remainder      92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 14323         -#define OP_Concat         93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 14324         -#define OP_Compare        94 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 14325         -#define OP_BitNot         95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 14326         -#define OP_IsTrue         96 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
 14327         -#define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 14328         -#define OP_Offset         98 /* synopsis: r[P3] = sqlite_offset(P1)        */
 14329         -#define OP_Column         99 /* synopsis: r[P3]=PX                         */
        14564  +#define OP_Compare        84 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
        14565  +#define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
        14566  +#define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
        14567  +#define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
        14568  +#define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
        14569  +#define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
        14570  +#define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
        14571  +#define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
        14572  +#define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
        14573  +#define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
        14574  +#define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
        14575  +#define OP_IsTrue         95 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
        14576  +#define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
        14577  +#define OP_Offset         97 /* synopsis: r[P3] = sqlite_offset(P1)        */
        14578  +#define OP_Column         98 /* synopsis: r[P3]=PX                         */
        14579  +#define OP_String8        99 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 14330  14580   #define OP_Affinity      100 /* synopsis: affinity(r[P1@P2])               */
 14331  14581   #define OP_MakeRecord    101 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 14332  14582   #define OP_Count         102 /* synopsis: r[P2]=count()                    */
 14333  14583   #define OP_ReadCookie    103
 14334  14584   #define OP_SetCookie     104
 14335  14585   #define OP_ReopenIdx     105 /* synopsis: root=P2 iDb=P3                   */
 14336  14586   #define OP_OpenRead      106 /* synopsis: root=P2 iDb=P3                   */
................................................................................
 14355  14605   #define OP_Rowid         125 /* synopsis: r[P2]=rowid                      */
 14356  14606   #define OP_NullRow       126
 14357  14607   #define OP_SeekEnd       127
 14358  14608   #define OP_SorterInsert  128 /* synopsis: key=r[P2]                        */
 14359  14609   #define OP_IdxInsert     129 /* synopsis: key=r[P2]                        */
 14360  14610   #define OP_IdxDelete     130 /* synopsis: key=r[P2@P3]                     */
 14361  14611   #define OP_DeferredSeek  131 /* synopsis: Move P3 to P1.rowid if needed    */
 14362         -#define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 14363         -#define OP_IdxRowid      133 /* synopsis: r[P2]=rowid                      */
 14364         -#define OP_Destroy       134
        14612  +#define OP_IdxRowid      132 /* synopsis: r[P2]=rowid                      */
        14613  +#define OP_Destroy       133
        14614  +#define OP_Real          134 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 14365  14615   #define OP_Clear         135
 14366  14616   #define OP_ResetSorter   136
 14367  14617   #define OP_CreateBtree   137 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 14368  14618   #define OP_SqlExec       138
 14369  14619   #define OP_ParseSchema   139
 14370  14620   #define OP_LoadAnalysis  140
 14371  14621   #define OP_DropTable     141
................................................................................
 14394  14644   #define OP_Function0     164 /* synopsis: r[P3]=func(r[P2@P5])             */
 14395  14645   #define OP_PureFunc      165
 14396  14646   #define OP_Function      166 /* synopsis: r[P3]=func(r[P2@P5])             */
 14397  14647   #define OP_Trace         167
 14398  14648   #define OP_CursorHint    168
 14399  14649   #define OP_Noop          169
 14400  14650   #define OP_Explain       170
        14651  +#define OP_Abortable     171
 14401  14652   
 14402  14653   /* Properties such as "out2" or "jump" that are specified in
 14403  14654   ** comments following the "case" for each opcode in the vdbe.c
 14404  14655   ** are encoded into bitvectors as follows:
 14405  14656   */
 14406  14657   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 14407  14658   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 14416  14667   /*  24 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
 14417  14668   /*  32 */ 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
 14418  14669   /*  40 */ 0x01, 0x01, 0x23, 0x26, 0x26, 0x0b, 0x01, 0x01,\
 14419  14670   /*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 14420  14671   /*  56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
 14421  14672   /*  64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
 14422  14673   /*  72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 14423         -/*  80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
 14424         -/*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 14425         -/*  96 */ 0x12, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10,\
        14674  +/*  80 */ 0x02, 0x02, 0x02, 0x00, 0x00, 0x26, 0x26, 0x26,\
        14675  +/*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x12,\
        14676  +/*  96 */ 0x12, 0x20, 0x00, 0x10, 0x00, 0x00, 0x10, 0x10,\
 14426  14677   /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14427  14678   /* 112 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 14428  14679   /* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,\
 14429  14680   /* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
 14430  14681   /* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14431  14682   /* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
 14432  14683   /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14433  14684   /* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14434         -/* 168 */ 0x00, 0x00, 0x00,}
        14685  +/* 168 */ 0x00, 0x00, 0x00, 0x00,}
 14435  14686   
 14436  14687   /* The sqlite3P2Values() routine is able to run faster if it knows
 14437  14688   ** the value of the largest JUMP opcode.  The smaller the maximum
 14438  14689   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 14439  14690   ** generated this include file strives to group all JUMP opcodes
 14440  14691   ** together near the beginning of the list.
 14441  14692   */
................................................................................
 14469  14720   #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
 14470  14721   SQLITE_PRIVATE   void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
 14471  14722   SQLITE_PRIVATE   void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
 14472  14723   #else
 14473  14724   # define sqlite3VdbeVerifyNoMallocRequired(A,B)
 14474  14725   # define sqlite3VdbeVerifyNoResultRow(A)
 14475  14726   #endif
        14727  +#if defined(SQLITE_DEBUG)
        14728  +SQLITE_PRIVATE   void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
        14729  +#else
        14730  +# define sqlite3VdbeVerifyAbortable(A,B)
        14731  +#endif
 14476  14732   SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
        14733  +#ifndef SQLITE_OMIT_EXPLAIN
        14734  +SQLITE_PRIVATE   void sqlite3VdbeExplain(Parse*,u8,const char*,...);
        14735  +SQLITE_PRIVATE   void sqlite3VdbeExplainPop(Parse*);
        14736  +SQLITE_PRIVATE   int sqlite3VdbeExplainParent(Parse*);
        14737  +# define ExplainQueryPlan(P)        sqlite3VdbeExplain P
        14738  +# define ExplainQueryPlanPop(P)     sqlite3VdbeExplainPop(P)
        14739  +# define ExplainQueryPlanParent(P)  sqlite3VdbeExplainParent(P)
        14740  +#else
        14741  +# define ExplainQueryPlan(P)
        14742  +# define ExplainQueryPlanPop(P)
        14743  +# define ExplainQueryPlanParent(P) 0
        14744  +#endif
 14477  14745   SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
 14478  14746   SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
 14479  14747   SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
 14480  14748   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
 14481  14749   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
 14482  14750   SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
 14483  14751   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
................................................................................
 14492  14760   SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
 14493  14761   SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
 14494  14762   SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
 14495  14763   SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
 14496  14764   SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
 14497  14765   SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
 14498  14766   SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
        14767  +#ifdef SQLITE_COVERAGE_TEST
        14768  +SQLITE_PRIVATE   int sqlite3VdbeLabelHasBeenResolved(Vdbe*,int);
        14769  +#endif
 14499  14770   SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
 14500  14771   #ifdef SQLITE_DEBUG
 14501  14772   SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
 14502  14773   #endif
 14503  14774   SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
 14504  14775   SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
 14505  14776   SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
................................................................................
 15626  15897     u8 bBenignMalloc;             /* Do not require OOMs if true */
 15627  15898     u8 dfltLockMode;              /* Default locking-mode for attached dbs */
 15628  15899     signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
 15629  15900     u8 suppressErr;               /* Do not issue error messages if true */
 15630  15901     u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
 15631  15902     u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
 15632  15903     u8 mTrace;                    /* zero or more SQLITE_TRACE flags */
 15633         -  u8 skipBtreeMutex;            /* True if no shared-cache backends */
        15904  +  u8 noSharedCache;             /* True if no shared-cache backends */
 15634  15905     u8 nSqlExec;                  /* Number of pending OP_SqlExec opcodes */
 15635  15906     int nextPagesize;             /* Pagesize after VACUUM if >0 */
 15636  15907     u32 magic;                    /* Magic number for detect library misuse */
 15637  15908     int nChange;                  /* Value returned by sqlite3_changes() */
 15638  15909     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
 15639  15910     int aLimit[SQLITE_N_LIMIT];   /* Limits */
 15640  15911     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
................................................................................
 15770  16041   #define SQLITE_EnableTrigger  0x00040000  /* True to enable triggers */
 15771  16042   #define SQLITE_DeferFKs       0x00080000  /* Defer all FK constraints */
 15772  16043   #define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
 15773  16044   #define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
 15774  16045   #define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
 15775  16046   #define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee*/
 15776  16047   #define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
        16048  +#define SQLITE_ResetDatabase  0x02000000  /* Reset the database */
 15777  16049   
 15778  16050   /* Flags used only if debugging */
 15779  16051   #ifdef SQLITE_DEBUG
 15780  16052   #define SQLITE_SqlTrace       0x08000000  /* Debug print SQL as it executes */
 15781  16053   #define SQLITE_VdbeListing    0x10000000  /* Debug listings of VDBE programs */
 15782  16054   #define SQLITE_VdbeTrace      0x20000000  /* True to trace VDBE execution */
 15783  16055   #define SQLITE_VdbeAddopTrace 0x40000000  /* Trace sqlite3VdbeAddOp() calls */
................................................................................
 15786  16058   
 15787  16059   /*
 15788  16060   ** Allowed values for sqlite3.mDbFlags
 15789  16061   */
 15790  16062   #define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
 15791  16063   #define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
 15792  16064   #define DBFLAG_Vacuum         0x0004  /* Currently in a VACUUM */
        16065  +#define DBFLAG_SchemaKnownOk  0x0008  /* Schema is known to be valid */
 15793  16066   
 15794  16067   /*
 15795  16068   ** Bits of the sqlite3.dbOptFlags field that are used by the
 15796  16069   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
 15797  16070   ** selectively disable various optimizations.
 15798  16071   */
 15799  16072   #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
................................................................................
 16031  16304   
 16032  16305   /* Allowed values for Column.colFlags:
 16033  16306   */
 16034  16307   #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
 16035  16308   #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
 16036  16309   #define COLFLAG_HASTYPE  0x0004    /* Type name follows column name */
 16037  16310   #define COLFLAG_UNIQUE   0x0008    /* Column def contains "UNIQUE" or "PK" */
        16311  +#define COLFLAG_SORTERREF 0x0010   /* Use sorter-refs with this column */
 16038  16312   
 16039  16313   /*
 16040  16314   ** A "Collating Sequence" is defined by an instance of the following
 16041  16315   ** structure. Conceptually, a collating sequence consists of a name and
 16042  16316   ** a comparison routine that defines the order of that sequence.
 16043  16317   **
 16044  16318   ** If CollSeq.xCmp is NULL, it means that the
................................................................................
 16318  16592   */
 16319  16593   #define OE_None     0   /* There is no constraint to check */
 16320  16594   #define OE_Rollback 1   /* Fail the operation and rollback the transaction */
 16321  16595   #define OE_Abort    2   /* Back out changes but do no rollback transaction */
 16322  16596   #define OE_Fail     3   /* Stop the operation but leave all prior changes */
 16323  16597   #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
 16324  16598   #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
 16325         -
 16326         -#define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
 16327         -#define OE_SetNull  7   /* Set the foreign key value to NULL */
 16328         -#define OE_SetDflt  8   /* Set the foreign key value to its default */
 16329         -#define OE_Cascade  9   /* Cascade the changes */
 16330         -
 16331         -#define OE_Default  10  /* Do whatever the default action is */
        16599  +#define OE_Update   6   /* Process as a DO UPDATE in an upsert */
        16600  +#define OE_Restrict 7   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
        16601  +#define OE_SetNull  8   /* Set the foreign key value to NULL */
        16602  +#define OE_SetDflt  9   /* Set the foreign key value to its default */
        16603  +#define OE_Cascade  10  /* Cascade the changes */
        16604  +#define OE_Default  11  /* Do whatever the default action is */
 16332  16605   
 16333  16606   
 16334  16607   /*
 16335  16608   ** An instance of the following structure is passed as the first
 16336  16609   ** argument to sqlite3VdbeKeyCompare and is used to control the
 16337  16610   ** comparison of the two index keys.
 16338  16611   **
................................................................................
 16771  17044       Expr *pExpr;            /* The parse tree for this expression */
 16772  17045       char *zName;            /* Token associated with this expression */
 16773  17046       char *zSpan;            /* Original text of the expression */
 16774  17047       u8 sortOrder;           /* 1 for DESC or 0 for ASC */
 16775  17048       unsigned done :1;       /* A flag to indicate when processing is finished */
 16776  17049       unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
 16777  17050       unsigned reusable :1;   /* Constant expression is reusable */
        17051  +    unsigned bSorterRef :1; /* Defer evaluation until after sorting */
 16778  17052       union {
 16779  17053         struct {
 16780  17054           u16 iOrderByCol;      /* For ORDER BY, column number in result set */
 16781  17055           u16 iAlias;           /* Index into Parse.aAlias[] for zName */
 16782  17056         } x;
 16783  17057         int iConstExprReg;      /* Register in which Expr value is cached */
 16784  17058       } u;
................................................................................
 16870  17144         unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
 16871  17145         unsigned isIndexedBy :1;   /* True if there is an INDEXED BY clause */
 16872  17146         unsigned isTabFunc :1;     /* True if table-valued-function syntax */
 16873  17147         unsigned isCorrelated :1;  /* True if sub-query is correlated */
 16874  17148         unsigned viaCoroutine :1;  /* Implemented as a co-routine */
 16875  17149         unsigned isRecursive :1;   /* True for recursive reference in WITH */
 16876  17150       } fg;
 16877         -#ifndef SQLITE_OMIT_EXPLAIN
 16878         -    u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
 16879         -#endif
 16880  17151       int iCursor;      /* The VDBE cursor number used to access this table */
 16881  17152       Expr *pOn;        /* The ON clause of a join */
 16882  17153       IdList *pUsing;   /* The USING clause of a join */
 16883  17154       Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
 16884  17155       union {
 16885  17156         char *zIndexedBy;    /* Identifier from "INDEXED BY <zIndex>" clause */
 16886  17157         ExprList *pFuncArg;  /* Arguments to table-valued-function */
................................................................................
 16954  17225   ** NameContext in the parent query.  Thus the process of scanning the
 16955  17226   ** NameContext list corresponds to searching through successively outer
 16956  17227   ** subqueries looking for a match.
 16957  17228   */
 16958  17229   struct NameContext {
 16959  17230     Parse *pParse;       /* The parser */
 16960  17231     SrcList *pSrcList;   /* One or more tables used to resolve names */
        17232  +  union {
 16961  17233     ExprList *pEList;    /* Optional list of result-set columns */
 16962  17234     AggInfo *pAggInfo;   /* Information about aggregates at this level */
        17235  +    Upsert *pUpsert;     /* ON CONFLICT clause information from an upsert */
        17236  +  } uNC;
 16963  17237     NameContext *pNext;  /* Next outer name context.  NULL for outermost */
 16964  17238     int nRef;            /* Number of names resolved by this context */
 16965  17239     int nErr;            /* Number of errors encountered while resolving names */
 16966  17240     u16 ncFlags;         /* Zero or more NC_* flags defined below */
 16967  17241   };
 16968  17242   
 16969  17243   /*
................................................................................
 16977  17251   #define NC_AllowAgg  0x0001  /* Aggregate functions are allowed here */
 16978  17252   #define NC_PartIdx   0x0002  /* True if resolving a partial index WHERE */
 16979  17253   #define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
 16980  17254   #define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
 16981  17255   #define NC_HasAgg    0x0010  /* One or more aggregate functions seen */
 16982  17256   #define NC_IdxExpr   0x0020  /* True if resolving columns of CREATE INDEX */
 16983  17257   #define NC_VarSelect 0x0040  /* A correlated subquery has been seen */
        17258  +#define NC_UEList    0x0080  /* True if uNC.pEList is used */
        17259  +#define NC_UAggInfo  0x0100  /* True if uNC.pAggInfo is used */
        17260  +#define NC_UUpsert   0x0200  /* True if uNC.pUpsert is used */
 16984  17261   #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
 16985  17262   #define NC_Complex   0x2000  /* True if a function or subquery seen */
        17263  +
        17264  +/*
        17265  +** An instance of the following object describes a single ON CONFLICT
        17266  +** clause in an upsert.
        17267  +**
        17268  +** The pUpsertTarget field is only set if the ON CONFLICT clause includes
        17269  +** conflict-target clause.  (In "ON CONFLICT(a,b)" the "(a,b)" is the
        17270  +** conflict-target clause.)  The pUpsertTargetWhere is the optional
        17271  +** WHERE clause used to identify partial unique indexes.
        17272  +**
        17273  +** pUpsertSet is the list of column=expr terms of the UPDATE statement. 
        17274  +** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING.  The
        17275  +** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
        17276  +** WHERE clause is omitted.
        17277  +*/
        17278  +struct Upsert {
        17279  +  ExprList *pUpsertTarget;  /* Optional description of conflicting index */
        17280  +  Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
        17281  +  ExprList *pUpsertSet;     /* The SET clause from an ON CONFLICT UPDATE */
        17282  +  Expr *pUpsertWhere;       /* WHERE clause for the ON CONFLICT UPDATE */
        17283  +  /* The fields above comprise the parse tree for the upsert clause.
        17284  +  ** The fields below are used to transfer information from the INSERT
        17285  +  ** processing down into the UPDATE processing while generating code.
        17286  +  ** Upsert owns the memory allocated above, but not the memory below. */
        17287  +  Index *pUpsertIdx;        /* Constraint that pUpsertTarget identifies */
        17288  +  SrcList *pUpsertSrc;      /* Table to be updated */
        17289  +  int regData;              /* First register holding array of VALUES */
        17290  +  int iDataCur;             /* Index of the data cursor */
        17291  +  int iIdxCur;              /* Index of the first index cursor */
        17292  +};
 16986  17293   
 16987  17294   /*
 16988  17295   ** An instance of the following structure contains all information
 16989  17296   ** needed to generate code for a single SELECT statement.
 16990  17297   **
 16991         -** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
 16992         -** If there is a LIMIT clause, the parser sets nLimit to the value of the
 16993         -** limit and nOffset to the value of the offset (or 0 if there is not
 16994         -** offset).  But later on, nLimit and nOffset become the memory locations
 16995         -** in the VDBE that record the limit and offset counters.
        17298  +** See the header comment on the computeLimitRegisters() routine for a
        17299  +** detailed description of the meaning of the iLimit and iOffset fields.
 16996  17300   **
 16997  17301   ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
 16998  17302   ** These addresses must be stored so that we can go back and fill in
 16999  17303   ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
 17000  17304   ** the number of columns in P2 can be computed at the same time
 17001  17305   ** as the OP_OpenEphm instruction is coded because not
 17002  17306   ** enough information about the compound query is known at that point.
................................................................................
 17048  17352   #define SF_NestedFrom     0x00800  /* Part of a parenthesized FROM clause */
 17049  17353   #define SF_MinMaxAgg      0x01000  /* Aggregate containing min() or max() */
 17050  17354   #define SF_Recursive      0x02000  /* The recursive part of a recursive CTE */
 17051  17355   #define SF_FixedLimit     0x04000  /* nSelectRow set by a constant LIMIT */
 17052  17356   #define SF_MaybeConvert   0x08000  /* Need convertCompoundSelectToSubquery() */
 17053  17357   #define SF_Converted      0x10000  /* By convertCompoundSelectToSubquery() */
 17054  17358   #define SF_IncludeHidden  0x20000  /* Include hidden columns in output */
 17055         -#define SF_ComplexResult  0x40000  /* Result set contains subquery or function */
 17056         -
        17359  +#define SF_ComplexResult  0x40000  /* Result contains subquery or function */
 17057  17360   
 17058  17361   /*
 17059  17362   ** The results of a SELECT can be distributed in several ways, as defined
 17060  17363   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
 17061  17364   ** Type".
 17062  17365   **
 17063  17366   **     SRT_Union       Store results as a key in a temporary index
................................................................................
 17319  17622     u8 explain;               /* True if the EXPLAIN flag is found on the query */
 17320  17623   #ifndef SQLITE_OMIT_VIRTUALTABLE
 17321  17624     u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
 17322  17625     int nVtabLock;            /* Number of virtual tables to lock */
 17323  17626   #endif
 17324  17627     int nHeight;              /* Expression tree height of current sub-select */
 17325  17628   #ifndef SQLITE_OMIT_EXPLAIN
 17326         -  int iSelectId;            /* ID of current select for EXPLAIN output */
 17327         -  int iNextSelectId;        /* Next available select ID for EXPLAIN output */
        17629  +  int addrExplain;          /* Address of current OP_Explain opcode */
 17328  17630   #endif
 17329  17631     VList *pVList;            /* Mapping between variable names and numbers */
 17330  17632     Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
 17331  17633     const char *zTail;        /* All SQL text past the last semicolon parsed */
 17332  17634     Table *pNewTable;         /* A table being constructed by CREATE TABLE */
 17333  17635     Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
 17334  17636     const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
................................................................................
 17479  17781   struct TriggerStep {
 17480  17782     u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
 17481  17783     u8 orconf;           /* OE_Rollback etc. */
 17482  17784     Trigger *pTrig;      /* The trigger that this step is a part of */
 17483  17785     Select *pSelect;     /* SELECT statement or RHS of INSERT INTO SELECT ... */
 17484  17786     char *zTarget;       /* Target table for DELETE, UPDATE, INSERT */
 17485  17787     Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
 17486         -  ExprList *pExprList; /* SET clause for UPDATE. */
        17788  +  ExprList *pExprList; /* SET clause for UPDATE */
 17487  17789     IdList *pIdList;     /* Column names for INSERT */
        17790  +  Upsert *pUpsert;     /* Upsert clauses on an INSERT */
 17488  17791     char *zSpan;         /* Original SQL text of this command */
 17489  17792     TriggerStep *pNext;  /* Next in the link-list */
 17490  17793     TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
 17491  17794   };
 17492  17795   
 17493  17796   /*
 17494  17797   ** The following structure contains information used by the sqliteFix...
................................................................................
 17505  17808     const Token *pName; /* Name of the container - used for error messages */
 17506  17809   };
 17507  17810   
 17508  17811   /*
 17509  17812   ** An objected used to accumulate the text of a string where we
 17510  17813   ** do not necessarily know how big the string will be in the end.
 17511  17814   */
 17512         -struct StrAccum {
        17815  +struct sqlite3_str {
 17513  17816     sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
 17514  17817     char *zText;         /* The string collected so far */
 17515  17818     u32  nAlloc;         /* Amount of space allocated in zText */
 17516  17819     u32  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
 17517  17820     u32  nChar;          /* Length of the string so far */
 17518         -  u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
        17821  +  u8   accError;       /* SQLITE_NOMEM or SQLITE_TOOBIG */
 17519  17822     u8   printfFlags;    /* SQLITE_PRINTF flags below */
 17520  17823   };
 17521         -#define STRACCUM_NOMEM   1
 17522         -#define STRACCUM_TOOBIG  2
 17523  17824   #define SQLITE_PRINTF_INTERNAL 0x01  /* Internal-use-only converters allowed */
 17524  17825   #define SQLITE_PRINTF_SQLFUNC  0x02  /* SQL function arguments to VXPrintf */
 17525  17826   #define SQLITE_PRINTF_MALLOCED 0x04  /* True if xText is allocated space */
 17526  17827   
 17527  17828   #define isMalloced(X)  (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
 17528  17829   
 17529  17830   
................................................................................
 17592  17893     void *pVdbeBranchArg;                                     /* 1st argument */
 17593  17894   #endif
 17594  17895   #ifndef SQLITE_UNTESTABLE
 17595  17896     int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
 17596  17897   #endif
 17597  17898     int bLocaltimeFault;              /* True to fail localtime() calls */
 17598  17899     int iOnceResetThreshold;          /* When to reset OP_Once counters */
        17900  +  u32 szSorterRef;                  /* Min size in bytes to use sorter-refs */
 17599  17901   };
 17600  17902   
 17601  17903   /*
 17602  17904   ** This macro is used inside of assert() statements to indicate that
 17603  17905   ** the assert is only valid on a well-formed database.  Instead of:
 17604  17906   **
 17605  17907   **     assert( X );
................................................................................
 17883  18185   */
 17884  18186   struct PrintfArguments {
 17885  18187     int nArg;                /* Total number of arguments */
 17886  18188     int nUsed;               /* Number of arguments used so far */
 17887  18189     sqlite3_value **apArg;   /* The argument values */
 17888  18190   };
 17889  18191   
 17890         -SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, const char*, va_list);
 17891         -SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
 17892  18192   SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
 17893  18193   SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
 17894  18194   #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
 17895  18195   SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
 17896  18196   #endif
 17897  18197   #if defined(SQLITE_TEST)
 17898  18198   SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
................................................................................
 18012  18312   #ifndef SQLITE_OMIT_AUTOINCREMENT
 18013  18313   SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
 18014  18314   SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
 18015  18315   #else
 18016  18316   # define sqlite3AutoincrementBegin(X)
 18017  18317   # define sqlite3AutoincrementEnd(X)
 18018  18318   #endif
 18019         -SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
        18319  +SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
 18020  18320   SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
 18021  18321   SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
 18022  18322   SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
 18023  18323   SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
 18024  18324   SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
 18025  18325   SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
 18026  18326                                         Token*, Select*, Expr*, IdList*);
................................................................................
 18042  18342   SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
 18043  18343   SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
 18044  18344   SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
 18045  18345   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 18046  18346   SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
 18047  18347   #endif
 18048  18348   SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
 18049         -SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*);
        18349  +SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
        18350  +                   Upsert*);
 18050  18351   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 18051  18352   SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
 18052  18353   SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
 18053  18354   SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
 18054  18355   SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
 18055  18356   SQLITE_PRIVATE int sqlite3WhereOrderedInnerLoop(WhereInfo*);
 18056  18357   SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
................................................................................
 18135  18436   SQLITE_PRIVATE int sqlite3IsRowid(const char*);
 18136  18437   SQLITE_PRIVATE void sqlite3GenerateRowDelete(
 18137  18438       Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
 18138  18439   SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
 18139  18440   SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
 18140  18441   SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
 18141  18442   SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
 18142         -                                     u8,u8,int,int*,int*);
        18443  +                                     u8,u8,int,int*,int*,Upsert*);
 18143  18444   #ifdef SQLITE_ENABLE_NULL_TRIM
 18144  18445   SQLITE_PRIVATE   void sqlite3SetMakeRecordP5(Vdbe*,Table*);
 18145  18446   #else
 18146  18447   # define sqlite3SetMakeRecordP5(A,B)
 18147  18448   #endif
 18148  18449   SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
 18149  18450   SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
................................................................................
 18188  18489                               int, int, int);
 18189  18490   SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
 18190  18491     void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
 18191  18492   SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
 18192  18493   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
 18193  18494                                           const char*,const char*);
 18194  18495   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
 18195         -                                        Select*,u8,const char*,const char*);
        18496  +                                        Select*,u8,Upsert*,
        18497  +                                        const char*,const char*);
 18196  18498   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8,
 18197  18499                                           const char*,const char*);
 18198  18500   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*,
 18199  18501                                           const char*,const char*);
 18200  18502   SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
 18201  18503   SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
 18202  18504   SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
................................................................................
 18374  18676   SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
 18375  18677   SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
 18376  18678   SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 18377  18679   SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 18378  18680   SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
 18379  18681   SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
 18380  18682   SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
 18381         -SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
        18683  +SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*);
 18382  18684   SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
 18383  18685   SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
 18384  18686   SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
 18385  18687   SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
 18386  18688   SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 18387  18689   SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
 18388  18690   SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
................................................................................
 18399  18701   SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
 18400  18702   #endif
 18401  18703   SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
 18402  18704     void (*)(sqlite3_context*,int,sqlite3_value **),
 18403  18705     void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
 18404  18706     FuncDestructor *pDestructor
 18405  18707   );
        18708  +SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
 18406  18709   SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
 18407  18710   SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
 18408  18711   SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
 18409  18712   SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
 18410  18713   
 18411  18714   SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
 18412         -SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
 18413         -SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
 18414         -SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char);
 18415  18715   SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
 18416         -SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
 18417  18716   SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
 18418  18717   SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
 18419  18718   
 18420  18719   SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
 18421  18720   SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
 18422  18721   
 18423  18722   #ifndef SQLITE_OMIT_SUBQUERY
................................................................................
 18436  18735   SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
 18437  18736   #endif
 18438  18737   
 18439  18738   /*
 18440  18739   ** The interface to the LEMON-generated parser
 18441  18740   */
 18442  18741   #ifndef SQLITE_AMALGAMATION
 18443         -SQLITE_PRIVATE   void *sqlite3ParserAlloc(void*(*)(u64));
        18742  +SQLITE_PRIVATE   void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
 18444  18743   SQLITE_PRIVATE   void sqlite3ParserFree(void*, void(*)(void*));
 18445  18744   #endif
 18446         -SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
        18745  +SQLITE_PRIVATE void sqlite3Parser(void*, int, Token);
 18447  18746   #ifdef YYTRACKMAXSTACKDEPTH
 18448  18747   SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
 18449  18748   #endif
 18450  18749   
 18451  18750   SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
 18452  18751   #ifndef SQLITE_OMIT_LOAD_EXTENSION
 18453  18752   SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
................................................................................
 18505  18804   SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
 18506  18805   SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
 18507  18806   SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
 18508  18807   SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
 18509  18808   SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
 18510  18809   SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
 18511  18810   SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
 18512         -SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
 18513  18811   SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
 18514  18812   SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
 18515  18813   SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 18516  18814   SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
 18517  18815   SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 18518  18816   SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 18519  18817   SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
................................................................................
 18527  18825   SQLITE_PRIVATE   With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
 18528  18826   SQLITE_PRIVATE   void sqlite3WithDelete(sqlite3*,With*);
 18529  18827   SQLITE_PRIVATE   void sqlite3WithPush(Parse*, With*, u8);
 18530  18828   #else
 18531  18829   #define sqlite3WithPush(x,y,z)
 18532  18830   #define sqlite3WithDelete(x,y)
 18533  18831   #endif
        18832  +#ifndef SQLITE_OMIT_UPSERT
        18833  +SQLITE_PRIVATE   Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
        18834  +SQLITE_PRIVATE   void sqlite3UpsertDelete(sqlite3*,Upsert*);
        18835  +SQLITE_PRIVATE   Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
        18836  +SQLITE_PRIVATE   int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
        18837  +SQLITE_PRIVATE   void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
        18838  +#else
        18839  +#define sqlite3UpsertNew(v,w,x,y,z) ((Upsert*)0)
        18840  +#define sqlite3UpsertDelete(x,y)
        18841  +#define sqlite3UpsertDup(x,y)       ((Upsert*)0)
        18842  +#endif
        18843  +
 18534  18844   
 18535  18845   /* Declarations for functions in fkey.c. All of these are replaced by
 18536  18846   ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
 18537  18847   ** key functionality is available. If OMIT_TRIGGER is defined but
 18538  18848   ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
 18539  18849   ** this case foreign keys are parsed, but no other functionality is
 18540  18850   ** provided (enforcement of FK constraints requires the triggers sub-system).
................................................................................
 18959  19269      0,                         /* xVdbeBranch */
 18960  19270      0,                         /* pVbeBranchArg */
 18961  19271   #endif
 18962  19272   #ifndef SQLITE_UNTESTABLE
 18963  19273      0,                         /* xTestCallback */
 18964  19274   #endif
 18965  19275      0,                         /* bLocaltimeFault */
 18966         -   0x7ffffffe                 /* iOnceResetThreshold */
        19276  +   0x7ffffffe,                /* iOnceResetThreshold */
        19277  +   SQLITE_DEFAULT_SORTERREF_SIZE   /* szSorterRef */
 18967  19278   };
 18968  19279   
 18969  19280   /*
 18970  19281   ** Hash table for global functions - functions common to all
 18971  19282   ** database connections.  After initialization, this table is
 18972  19283   ** read-only.
 18973  19284   */
................................................................................
 19422  19733     VList *pVList;          /* Name of variables */
 19423  19734   #ifndef SQLITE_OMIT_TRACE
 19424  19735     i64 startTime;          /* Time when query started - used for profiling */
 19425  19736   #endif
 19426  19737     int nOp;                /* Number of instructions in the program */
 19427  19738   #ifdef SQLITE_DEBUG
 19428  19739     int rcApp;              /* errcode set by sqlite3_result_error_code() */
        19740  +  u32 nWrite;             /* Number of write operations that have occurred */
 19429  19741   #endif
 19430  19742     u16 nResColumn;         /* Number of columns in one row of the result set */
 19431  19743     u8 errorAction;         /* Recovery action to do in case of an error */
 19432  19744     u8 minWriteFileFormat;  /* Minimum file format for writable database files */
 19433  19745     u8 prepFlags;           /* SQLITE_PREPARE_* flags */
 19434  19746     bft expired:1;          /* True if the VM needs to be recompiled */
 19435  19747     bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
................................................................................
 19556  19868   SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
 19557  19869   SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
 19558  19870   SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
 19559  19871   SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
 19560  19872   SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
 19561  19873   SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
 19562  19874   SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
        19875  +
        19876  +#ifdef SQLITE_DEBUG
        19877  +SQLITE_PRIVATE   void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
        19878  +SQLITE_PRIVATE   void sqlite3VdbeAssertAbortable(Vdbe*);
        19879  +#else
        19880  +# define sqlite3VdbeIncrWriteCounter(V,C)
        19881  +# define sqlite3VdbeAssertAbortable(V)
        19882  +#endif
 19563  19883   
 19564  19884   #if !defined(SQLITE_OMIT_SHARED_CACHE) 
 19565  19885   SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
 19566  19886   #else
 19567  19887   # define sqlite3VdbeEnter(X)
 19568  19888   #endif
 19569  19889   
................................................................................
 24249  24569     assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
 24250  24570     return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
 24251  24571   }
 24252  24572   #endif
 24253  24573   
 24254  24574   #endif /* !defined(SQLITE_MUTEX_OMIT) */
 24255  24575   
 24256         -
 24257  24576   /************** End of mutex.c ***********************************************/
 24258  24577   /************** Begin file mutex_noop.c **************************************/
 24259  24578   /*
 24260  24579   ** 2008 October 07
 24261  24580   **
 24262  24581   ** The author disclaims copyright to this source code.  In place of
 24263  24582   ** a legal notice, here is a blessing:
................................................................................
 26413  26732   }
 26414  26733   #endif /* SQLITE_OMIT_FLOATING_POINT */
 26415  26734   
 26416  26735   /*
 26417  26736   ** Set the StrAccum object to an error mode.
 26418  26737   */
 26419  26738   static void setStrAccumError(StrAccum *p, u8 eError){
 26420         -  assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG );
        26739  +  assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
 26421  26740     p->accError = eError;
 26422  26741     p->nAlloc = 0;
 26423  26742   }
 26424  26743   
 26425  26744   /*
 26426  26745   ** Extra argument values from a PrintfArguments object
 26427  26746   */
................................................................................
 26447  26766   # define SQLITE_PRINT_BUF_SIZE 70
 26448  26767   #endif
 26449  26768   #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
 26450  26769   
 26451  26770   /*
 26452  26771   ** Render a string given by "fmt" into the StrAccum object.
 26453  26772   */
 26454         -SQLITE_PRIVATE void sqlite3VXPrintf(
 26455         -  StrAccum *pAccum,          /* Accumulate results here */
        26773  +SQLITE_API void sqlite3_str_vappendf(
        26774  +  sqlite3_str *pAccum,       /* Accumulate results here */
 26456  26775     const char *fmt,           /* Format string */
 26457  26776     va_list ap                 /* arguments */
 26458  26777   ){
 26459  26778     int c;                     /* Next character in the format string */
 26460  26779     char *bufpt;               /* Pointer to the conversion buffer */
 26461  26780     int precision;             /* Precision of the current field */
 26462  26781     int length;                /* Length of the field */
................................................................................
 26505  26824       if( c!='%' ){
 26506  26825         bufpt = (char *)fmt;
 26507  26826   #if HAVE_STRCHRNUL
 26508  26827         fmt = strchrnul(fmt, '%');
 26509  26828   #else
 26510  26829         do{ fmt++; }while( *fmt && *fmt != '%' );
 26511  26830   #endif
 26512         -      sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
        26831  +      sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
 26513  26832         if( *fmt==0 ) break;
 26514  26833       }
 26515  26834       if( (c=(*++fmt))==0 ){
 26516         -      sqlite3StrAccumAppend(pAccum, "%", 1);
        26835  +      sqlite3_str_append(pAccum, "%", 1);
 26517  26836         break;
 26518  26837       }
 26519  26838       /* Find out what flags are present */
 26520  26839       flag_leftjustify = flag_prefix = cThousand =
 26521  26840        flag_alternateform = flag_altform2 = flag_zeropad = 0;
 26522  26841       done = 0;
 26523  26842       do{
................................................................................
 26687  27006           if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
 26688  27007             nOut = etBUFSIZE;
 26689  27008             zOut = buf;
 26690  27009           }else{
 26691  27010             u64 n = (u64)precision + 10 + precision/3;
 26692  27011             zOut = zExtra = sqlite3Malloc( n );
 26693  27012             if( zOut==0 ){
 26694         -            setStrAccumError(pAccum, STRACCUM_NOMEM);
        27013  +            setStrAccumError(pAccum, SQLITE_NOMEM);
 26695  27014               return;
 26696  27015             }
 26697  27016             nOut = (int)n;
 26698  27017           }
 26699  27018           bufpt = &zOut[nOut-1];
 26700  27019           if( xtype==etORDINAL ){
 26701  27020             static const char zOrd[] = "thstndrd";
................................................................................
 26812  27131           }else{
 26813  27132             e2 = exp;
 26814  27133           }
 26815  27134           if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
 26816  27135             bufpt = zExtra 
 26817  27136                 = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
 26818  27137             if( bufpt==0 ){
 26819         -            setStrAccumError(pAccum, STRACCUM_NOMEM);
        27138  +            setStrAccumError(pAccum, SQLITE_NOMEM);
 26820  27139               return;
 26821  27140             }
 26822  27141           }
 26823  27142           zOut = bufpt;
 26824  27143           nsd = 16 + flag_altform2*10;
 26825  27144           flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
 26826  27145           /* The sign in front of the number */
................................................................................
 26944  27263               buf[3] = 0x80 + (u8)(ch & 0x3f);
 26945  27264               length = 4;
 26946  27265             }
 26947  27266           }
 26948  27267           if( precision>1 ){
 26949  27268             width -= precision-1;
 26950  27269             if( width>1 && !flag_leftjustify ){
 26951         -            sqlite3AppendChar(pAccum, width-1, ' ');
        27270  +            sqlite3_str_appendchar(pAccum, width-1, ' ');
 26952  27271               width = 0;
 26953  27272             }
 26954  27273             while( precision-- > 1 ){
 26955         -            sqlite3StrAccumAppend(pAccum, buf, length);
        27274  +            sqlite3_str_append(pAccum, buf, length);
 26956  27275             }
 26957  27276           }
 26958  27277           bufpt = buf;
 26959  27278           flag_altform2 = 1;
 26960  27279           goto adjust_width_for_utf8;
 26961  27280         case etSTRING:
 26962  27281         case etDYNSTRING:
................................................................................
 27034  27353             }
 27035  27354           }
 27036  27355           needQuote = !isnull && xtype==etSQLESCAPE2;
 27037  27356           n += i + 3;
 27038  27357           if( n>etBUFSIZE ){
 27039  27358             bufpt = zExtra = sqlite3Malloc( n );
 27040  27359             if( bufpt==0 ){
 27041         -            setStrAccumError(pAccum, STRACCUM_NOMEM);
        27360  +            setStrAccumError(pAccum, SQLITE_NOMEM);
 27042  27361               return;
 27043  27362             }
 27044  27363           }else{
 27045  27364             bufpt = buf;
 27046  27365           }
 27047  27366           j = 0;
 27048  27367           if( needQuote ) bufpt[j++] = q;
................................................................................
 27058  27377         }
 27059  27378         case etTOKEN: {
 27060  27379           Token *pToken;
 27061  27380           if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
 27062  27381           pToken = va_arg(ap, Token*);
 27063  27382           assert( bArgList==0 );
 27064  27383           if( pToken && pToken->n ){
 27065         -          sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
        27384  +          sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
 27066  27385           }
 27067  27386           length = width = 0;
 27068  27387           break;
 27069  27388         }
 27070  27389         case etSRCLIST: {
 27071  27390           SrcList *pSrc;
 27072  27391           int k;
................................................................................
 27074  27393           if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
 27075  27394           pSrc = va_arg(ap, SrcList*);
 27076  27395           k = va_arg(ap, int);
 27077  27396           pItem = &pSrc->a[k];
 27078  27397           assert( bArgList==0 );
 27079  27398           assert( k>=0 && k<pSrc->nSrc );
 27080  27399           if( pItem->zDatabase ){
 27081         -          sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
 27082         -          sqlite3StrAccumAppend(pAccum, ".", 1);
        27400  +          sqlite3_str_appendall(pAccum, pItem->zDatabase);
        27401  +          sqlite3_str_append(pAccum, ".", 1);
 27083  27402           }
 27084         -        sqlite3StrAccumAppendAll(pAccum, pItem->zName);
        27403  +        sqlite3_str_appendall(pAccum, pItem->zName);
 27085  27404           length = width = 0;
 27086  27405           break;
 27087  27406         }
 27088  27407         default: {
 27089  27408           assert( xtype==etINVALID );
 27090  27409           return;
 27091  27410         }
................................................................................
 27096  27415       ** the output.  Both length and width are in bytes, not characters,
 27097  27416       ** at this point.  If the "!" flag was present on string conversions
 27098  27417       ** indicating that width and precision should be expressed in characters,
 27099  27418       ** then the values have been translated prior to reaching this point.
 27100  27419       */
 27101  27420       width -= length;
 27102  27421       if( width>0 ){
 27103         -      if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 27104         -      sqlite3StrAccumAppend(pAccum, bufpt, length);
 27105         -      if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
        27422  +      if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
        27423  +      sqlite3_str_append(pAccum, bufpt, length);
        27424  +      if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
 27106  27425       }else{
 27107         -      sqlite3StrAccumAppend(pAccum, bufpt, length);
        27426  +      sqlite3_str_append(pAccum, bufpt, length);
 27108  27427       }
 27109  27428   
 27110  27429       if( zExtra ){
 27111  27430         sqlite3DbFree(pAccum->db, zExtra);
 27112  27431         zExtra = 0;
 27113  27432       }
 27114  27433     }/* End for loop over the format string */
................................................................................
 27121  27440   ** Return the number of bytes of text that StrAccum is able to accept
 27122  27441   ** after the attempted enlargement.  The value returned might be zero.
 27123  27442   */
 27124  27443   static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
 27125  27444     char *zNew;
 27126  27445     assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
 27127  27446     if( p->accError ){
 27128         -    testcase(p->accError==STRACCUM_TOOBIG);
 27129         -    testcase(p->accError==STRACCUM_NOMEM);
        27447  +    testcase(p->accError==SQLITE_TOOBIG);
        27448  +    testcase(p->accError==SQLITE_NOMEM);
 27130  27449       return 0;
 27131  27450     }
 27132  27451     if( p->mxAlloc==0 ){
 27133  27452       N = p->nAlloc - p->nChar - 1;
 27134         -    setStrAccumError(p, STRACCUM_TOOBIG);
        27453  +    setStrAccumError(p, SQLITE_TOOBIG);
 27135  27454       return N;
 27136  27455     }else{
 27137  27456       char *zOld = isMalloced(p) ? p->zText : 0;
 27138  27457       i64 szNew = p->nChar;
 27139  27458       szNew += N + 1;
 27140  27459       if( szNew+p->nChar<=p->mxAlloc ){
 27141  27460         /* Force exponential buffer size growth as long as it does not overflow,
 27142  27461         ** to avoid having to call this routine too often */
 27143  27462         szNew += p->nChar;
 27144  27463       }
 27145  27464       if( szNew > p->mxAlloc ){
 27146         -      sqlite3StrAccumReset(p);
 27147         -      setStrAccumError(p, STRACCUM_TOOBIG);
        27465  +      sqlite3_str_reset(p);
        27466  +      setStrAccumError(p, SQLITE_TOOBIG);
 27148  27467         return 0;
 27149  27468       }else{
 27150  27469         p->nAlloc = (int)szNew;
 27151  27470       }
 27152  27471       if( p->db ){
 27153  27472         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
 27154  27473       }else{
................................................................................
 27157  27476       if( zNew ){
 27158  27477         assert( p->zText!=0 || p->nChar==0 );
 27159  27478         if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
 27160  27479         p->zText = zNew;
 27161  27480         p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
 27162  27481         p->printfFlags |= SQLITE_PRINTF_MALLOCED;
 27163  27482       }else{
 27164         -      sqlite3StrAccumReset(p);
 27165         -      setStrAccumError(p, STRACCUM_NOMEM);
        27483  +      sqlite3_str_reset(p);
        27484  +      setStrAccumError(p, SQLITE_NOMEM);
 27166  27485         return 0;
 27167  27486       }
 27168  27487     }
 27169  27488     return N;
 27170  27489   }
 27171  27490   
 27172  27491   /*
 27173  27492   ** Append N copies of character c to the given string buffer.
 27174  27493   */
 27175         -SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
        27494  +SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
 27176  27495     testcase( p->nChar + (i64)N > 0x7fffffff );
 27177  27496     if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
 27178  27497       return;
 27179  27498     }
 27180  27499     while( (N--)>0 ) p->zText[p->nChar++] = c;
 27181  27500   }
 27182  27501   
 27183  27502   /*
 27184  27503   ** The StrAccum "p" is not large enough to accept N new bytes of z[].
 27185  27504   ** So enlarge if first, then do the append.
 27186  27505   **
 27187         -** This is a helper routine to sqlite3StrAccumAppend() that does special-case
        27506  +** This is a helper routine to sqlite3_str_append() that does special-case
 27188  27507   ** work (enlarging the buffer) using tail recursion, so that the
 27189         -** sqlite3StrAccumAppend() routine can use fast calling semantics.
        27508  +** sqlite3_str_append() routine can use fast calling semantics.
 27190  27509   */
 27191  27510   static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
 27192  27511     N = sqlite3StrAccumEnlarge(p, N);
 27193  27512     if( N>0 ){
 27194  27513       memcpy(&p->zText[p->nChar], z, N);
 27195  27514       p->nChar += N;
 27196  27515     }
 27197  27516   }
 27198  27517   
 27199  27518   /*
 27200  27519   ** Append N bytes of text from z to the StrAccum object.  Increase the
 27201  27520   ** size of the memory allocation for StrAccum if necessary.
 27202  27521   */
 27203         -SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
        27522  +SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
 27204  27523     assert( z!=0 || N==0 );
 27205  27524     assert( p->zText!=0 || p->nChar==0 || p->accError );
 27206  27525     assert( N>=0 );
 27207  27526     assert( p->accError==0 || p->nAlloc==0 );
 27208  27527     if( p->nChar+N >= p->nAlloc ){
 27209  27528       enlargeAndAppend(p,z,N);
 27210  27529     }else if( N ){
................................................................................
 27213  27532       memcpy(&p->zText[p->nChar-N], z, N);
 27214  27533     }
 27215  27534   }
 27216  27535   
 27217  27536   /*
 27218  27537   ** Append the complete text of zero-terminated string z[] to the p string.
 27219  27538   */
 27220         -SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
 27221         -  sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
        27539  +SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
        27540  +  sqlite3_str_append(p, z, sqlite3Strlen30(z));
 27222  27541   }
 27223  27542   
 27224  27543   
 27225  27544   /*
 27226  27545   ** Finish off a string by making sure it is zero-terminated.
 27227  27546   ** Return a pointer to the resulting string.  Return a NULL
 27228  27547   ** pointer if any kind of error was encountered.
................................................................................
 27231  27550     char *zText;
 27232  27551     assert( p->mxAlloc>0 && !isMalloced(p) );
 27233  27552     zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
 27234  27553     if( zText ){
 27235  27554       memcpy(zText, p->zText, p->nChar+1);
 27236  27555       p->printfFlags |= SQLITE_PRINTF_MALLOCED;
 27237  27556     }else{
 27238         -    setStrAccumError(p, STRACCUM_NOMEM);
        27557  +    setStrAccumError(p, SQLITE_NOMEM);
 27239  27558     }
 27240  27559     p->zText = zText;
 27241  27560     return zText;
 27242  27561   }
 27243  27562   SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
 27244  27563     if( p->zText ){
 27245  27564       p->zText[p->nChar] = 0;
 27246  27565       if( p->mxAlloc>0 && !isMalloced(p) ){
 27247  27566         return strAccumFinishRealloc(p);
 27248  27567       }
 27249  27568     }
 27250  27569     return p->zText;
 27251  27570   }
        27571  +
        27572  +/*
        27573  +** This singleton is an sqlite3_str object that is returned if
        27574  +** sqlite3_malloc() fails to provide space for a real one.  This
        27575  +** sqlite3_str object accepts no new text and always returns
        27576  +** an SQLITE_NOMEM error.
        27577  +*/
        27578  +static sqlite3_str sqlite3OomStr = {
        27579  +   0, 0, 0, 0, 0, SQLITE_NOMEM
        27580  +};
        27581  +
        27582  +/* Finalize a string created using sqlite3_str_new().
        27583  +*/
        27584  +SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
        27585  +  char *z;
        27586  +  if( p!=0 && p!=&sqlite3OomStr ){
        27587  +    z = sqlite3StrAccumFinish(p);
        27588  +    sqlite3_free(p);
        27589  +  }else{
        27590  +    z = 0;
        27591  +  }
        27592  +  return z;
        27593  +}
        27594  +
        27595  +/* Return any error code associated with p */
        27596  +SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
        27597  +  return p ? p->accError : SQLITE_NOMEM;
        27598  +}
        27599  +
        27600  +/* Return the current length of p in bytes */
        27601  +SQLITE_API int sqlite3_str_length(sqlite3_str *p){
        27602  +  return p ? p->nChar : 0;
        27603  +}
        27604  +
        27605  +/* Return the current value for p */
        27606  +SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
        27607  +  if( p==0 || p->nChar==0 ) return 0;
        27608  +  p->zText[p->nChar] = 0;
        27609  +  return p->zText;
        27610  +}
 27252  27611   
 27253  27612   /*
 27254  27613   ** Reset an StrAccum string.  Reclaim all malloced memory.
 27255  27614   */
 27256         -SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
        27615  +SQLITE_API void sqlite3_str_reset(StrAccum *p){
 27257  27616     if( isMalloced(p) ){
 27258  27617       sqlite3DbFree(p->db, p->zText);
 27259  27618       p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
 27260  27619     }
        27620  +  p->nAlloc = 0;
        27621  +  p->nChar = 0;
 27261  27622     p->zText = 0;
 27262  27623   }
 27263  27624   
 27264  27625   /*
 27265  27626   ** Initialize a string accumulator.
 27266  27627   **
 27267  27628   ** p:     The accumulator to be initialized.
................................................................................
 27280  27641     p->db = db;
 27281  27642     p->nAlloc = n;
 27282  27643     p->mxAlloc = mx;
 27283  27644     p->nChar = 0;
 27284  27645     p->accError = 0;
 27285  27646     p->printfFlags = 0;
 27286  27647   }
        27648  +
        27649  +/* Allocate and initialize a new dynamic string object */
        27650  +SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
        27651  +  sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
        27652  +  if( p ){
        27653  +    sqlite3StrAccumInit(p, 0, 0, 0,
        27654  +            db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
        27655  +  }else{
        27656  +    p = &sqlite3OomStr;
        27657  +  }
        27658  +  return p;
        27659  +}
 27287  27660   
 27288  27661   /*
 27289  27662   ** Print into memory obtained from sqliteMalloc().  Use the internal
 27290  27663   ** %-conversion extensions.
 27291  27664   */
 27292  27665   SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
 27293  27666     char *z;
 27294  27667     char zBase[SQLITE_PRINT_BUF_SIZE];
 27295  27668     StrAccum acc;
 27296  27669     assert( db!=0 );
 27297  27670     sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
 27298  27671                         db->aLimit[SQLITE_LIMIT_LENGTH]);
 27299  27672     acc.printfFlags = SQLITE_PRINTF_INTERNAL;
 27300         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27673  +  sqlite3_str_vappendf(&acc, zFormat, ap);
 27301  27674     z = sqlite3StrAccumFinish(&acc);
 27302         -  if( acc.accError==STRACCUM_NOMEM ){
        27675  +  if( acc.accError==SQLITE_NOMEM ){
 27303  27676       sqlite3OomFault(db);
 27304  27677     }
 27305  27678     return z;
 27306  27679   }
 27307  27680   
 27308  27681   /*
 27309  27682   ** Print into memory obtained from sqliteMalloc().  Use the internal
................................................................................
 27333  27706       return 0;
 27334  27707     }
 27335  27708   #endif
 27336  27709   #ifndef SQLITE_OMIT_AUTOINIT
 27337  27710     if( sqlite3_initialize() ) return 0;
 27338  27711   #endif
 27339  27712     sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
 27340         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27713  +  sqlite3_str_vappendf(&acc, zFormat, ap);
 27341  27714     z = sqlite3StrAccumFinish(&acc);
 27342  27715     return z;
 27343  27716   }
 27344  27717   
 27345  27718   /*
 27346  27719   ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
 27347  27720   ** %-conversion extensions.
................................................................................
 27378  27751     if( zBuf==0 || zFormat==0 ) {
 27379  27752       (void)SQLITE_MISUSE_BKPT;
 27380  27753       if( zBuf ) zBuf[0] = 0;
 27381  27754       return zBuf;
 27382  27755     }
 27383  27756   #endif
 27384  27757     sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
 27385         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27758  +  sqlite3_str_vappendf(&acc, zFormat, ap);
 27386  27759     zBuf[acc.nChar] = 0;
 27387  27760     return zBuf;
 27388  27761   }
 27389  27762   SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
 27390  27763     char *z;
 27391  27764     va_list ap;
 27392  27765     va_start(ap,zFormat);
................................................................................
 27400  27773   ** We house it in a separate routine from sqlite3_log() to avoid using
 27401  27774   ** stack space on small-stack systems when logging is disabled.
 27402  27775   **
 27403  27776   ** sqlite3_log() must render into a static buffer.  It cannot dynamically
 27404  27777   ** allocate memory because it might be called while the memory allocator
 27405  27778   ** mutex is held.
 27406  27779   **
 27407         -** sqlite3VXPrintf() might ask for *temporary* memory allocations for
        27780  +** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
 27408  27781   ** certain format characters (%q) or for very large precisions or widths.
 27409  27782   ** Care must be taken that any sqlite3_log() calls that occur while the
 27410  27783   ** memory mutex is held do not use these mechanisms.
 27411  27784   */
 27412  27785   static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
 27413  27786     StrAccum acc;                          /* String accumulator */
 27414  27787     char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
 27415  27788   
 27416  27789     sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
 27417         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27790  +  sqlite3_str_vappendf(&acc, zFormat, ap);
 27418  27791     sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
 27419  27792                              sqlite3StrAccumFinish(&acc));
 27420  27793   }
 27421  27794   
 27422  27795   /*
 27423  27796   ** Format and write a message to the log if logging is enabled.
 27424  27797   */
................................................................................
 27439  27812   */
 27440  27813   SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
 27441  27814     va_list ap;
 27442  27815     StrAccum acc;
 27443  27816     char zBuf[500];
 27444  27817     sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
 27445  27818     va_start(ap,zFormat);
 27446         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27819  +  sqlite3_str_vappendf(&acc, zFormat, ap);
 27447  27820     va_end(ap);
 27448  27821     sqlite3StrAccumFinish(&acc);
 27449  27822   #ifdef SQLITE_OS_TRACE_PROC
 27450  27823     {
 27451  27824       extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
 27452  27825       SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
 27453  27826     }
................................................................................
 27456  27829     fflush(stdout);
 27457  27830   #endif
 27458  27831   }
 27459  27832   #endif
 27460  27833   
 27461  27834   
 27462  27835   /*
 27463         -** variable-argument wrapper around sqlite3VXPrintf().  The bFlags argument
        27836  +** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
 27464  27837   ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
 27465  27838   */
 27466         -SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
        27839  +SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
 27467  27840     va_list ap;
 27468  27841     va_start(ap,zFormat);
 27469         -  sqlite3VXPrintf(p, zFormat, ap);
        27842  +  sqlite3_str_vappendf(p, zFormat, ap);
 27470  27843     va_end(ap);
 27471  27844   }
 27472  27845   
 27473  27846   /************** End of printf.c **********************************************/
 27474  27847   /************** Begin file treeview.c ****************************************/
 27475  27848   /*
 27476  27849   ** 2015-06-08
................................................................................
 27528  27901     va_list ap;
 27529  27902     int i;
 27530  27903     StrAccum acc;
 27531  27904     char zBuf[500];
 27532  27905     sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
 27533  27906     if( p ){
 27534  27907       for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
 27535         -      sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
        27908  +      sqlite3_str_append(&acc, p->bLine[i] ? "|   " : "    ", 4);
 27536  27909       }
 27537         -    sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
        27910  +    sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
 27538  27911     }
        27912  +  if( zFormat!=0 ){
 27539  27913     va_start(ap, zFormat);
 27540         -  sqlite3VXPrintf(&acc, zFormat, ap);
        27914  +    sqlite3_str_vappendf(&acc, zFormat, ap);
 27541  27915     va_end(ap);
 27542  27916     assert( acc.nChar>0 );
 27543         -  if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
        27917  +    sqlite3_str_append(&acc, "\n", 1);
        27918  +  }
 27544  27919     sqlite3StrAccumFinish(&acc);
 27545  27920     fprintf(stdout,"%s", zBuf);
 27546  27921     fflush(stdout);
 27547  27922   }
 27548  27923   
 27549  27924   /*
 27550  27925   ** Shorthand for starting a new tree item that consists of a single label
................................................................................
 27569  27944     if( pWith->nCte>0 ){
 27570  27945       pView = sqlite3TreeViewPush(pView, 1);
 27571  27946       for(i=0; i<pWith->nCte; i++){
 27572  27947         StrAccum x;
 27573  27948         char zLine[1000];
 27574  27949         const struct Cte *pCte = &pWith->a[i];
 27575  27950         sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
 27576         -      sqlite3XPrintf(&x, "%s", pCte->zName);
        27951  +      sqlite3_str_appendf(&x, "%s", pCte->zName);
 27577  27952         if( pCte->pCols && pCte->pCols->nExpr>0 ){
 27578  27953           char cSep = '(';
 27579  27954           int j;
 27580  27955           for(j=0; j<pCte->pCols->nExpr; j++){
 27581         -          sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
        27956  +          sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
 27582  27957             cSep = ',';
 27583  27958           }
 27584         -        sqlite3XPrintf(&x, ")");
        27959  +        sqlite3_str_appendf(&x, ")");
 27585  27960         }
 27586         -      sqlite3XPrintf(&x, " AS");
        27961  +      sqlite3_str_appendf(&x, " AS");
 27587  27962         sqlite3StrAccumFinish(&x);
 27588  27963         sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
 27589  27964         sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
 27590  27965         sqlite3TreeViewPop(pView);
 27591  27966       }
 27592  27967       sqlite3TreeViewPop(pView);
 27593  27968     }
................................................................................
 27644  28019         pView = sqlite3TreeViewPush(pView, (n--)>0);
 27645  28020         sqlite3TreeViewLine(pView, "FROM");
 27646  28021         for(i=0; i<p->pSrc->nSrc; i++){
 27647  28022           struct SrcList_item *pItem = &p->pSrc->a[i];
 27648  28023           StrAccum x;
 27649  28024           char zLine[100];
 27650  28025           sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
 27651         -        sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor);
        28026  +        sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor);
 27652  28027           if( pItem->zDatabase ){
 27653         -          sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
        28028  +          sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
 27654  28029           }else if( pItem->zName ){
 27655         -          sqlite3XPrintf(&x, " %s", pItem->zName);
        28030  +          sqlite3_str_appendf(&x, " %s", pItem->zName);
 27656  28031           }
 27657  28032           if( pItem->pTab ){
 27658         -          sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName);
        28033  +          sqlite3_str_appendf(&x, " tabname=%Q", pItem->pTab->zName);
 27659  28034           }
 27660  28035           if( pItem->zAlias ){
 27661         -          sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias);
        28036  +          sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
 27662  28037           }
 27663  28038           if( pItem->fg.jointype & JT_LEFT ){
 27664         -          sqlite3XPrintf(&x, " LEFT-JOIN");
        28039  +          sqlite3_str_appendf(&x, " LEFT-JOIN");
 27665  28040           }
 27666  28041           sqlite3StrAccumFinish(&x);
 27667  28042           sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
 27668  28043           if( pItem->pSelect ){
 27669  28044             sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
 27670  28045           }
 27671  28046           if( pItem->fg.isTabFunc ){
................................................................................
 28006  28381       sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
 28007  28382     }else{
 28008  28383       int i;
 28009  28384       sqlite3TreeViewLine(pView, "%s", zLabel);
 28010  28385       for(i=0; i<pList->nExpr; i++){
 28011  28386         int j = pList->a[i].u.x.iOrderByCol;
 28012  28387         char *zName = pList->a[i].zName;
        28388  +      int moreToFollow = i<pList->nExpr - 1;
 28013  28389         if( j || zName ){
 28014         -        sqlite3TreeViewPush(pView, 0);
 28015         -      }
        28390  +        sqlite3TreeViewPush(pView, moreToFollow);
        28391  +        moreToFollow = 0;
        28392  +        sqlite3TreeViewLine(pView, 0);
 28016  28393         if( zName ){
 28017         -        sqlite3TreeViewLine(pView, "AS %s", zName);
        28394  +          fprintf(stdout, "AS %s ", zName);
 28018  28395         }
 28019  28396         if( j ){
 28020         -        sqlite3TreeViewLine(pView, "iOrderByCol=%d", j);
        28397  +          fprintf(stdout, "iOrderByCol=%d", j);
 28021  28398         }
 28022         -      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
        28399  +        fprintf(stdout, "\n");
        28400  +        fflush(stdout);
        28401  +      }
        28402  +      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
 28023  28403         if( j || zName ){
 28024  28404           sqlite3TreeViewPop(pView);
 28025  28405         }
 28026  28406       }
 28027  28407     }
 28028  28408   }
 28029  28409   SQLITE_PRIVATE void sqlite3TreeViewExprList(
................................................................................
 30981  31361       /*  77 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
 30982  31362       /*  78 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
 30983  31363       /*  79 */ "CollSeq"          OpHelp(""),
 30984  31364       /*  80 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
 30985  31365       /*  81 */ "RealAffinity"     OpHelp(""),
 30986  31366       /*  82 */ "Cast"             OpHelp("affinity(r[P1])"),
 30987  31367       /*  83 */ "Permutation"      OpHelp(""),
 30988         -    /*  84 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 30989         -    /*  85 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 30990         -    /*  86 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 30991         -    /*  87 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 30992         -    /*  88 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 30993         -    /*  89 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 30994         -    /*  90 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 30995         -    /*  91 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 30996         -    /*  92 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 30997         -    /*  93 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 30998         -    /*  94 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 30999         -    /*  95 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 31000         -    /*  96 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
 31001         -    /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 31002         -    /*  98 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
 31003         -    /*  99 */ "Column"           OpHelp("r[P3]=PX"),
        31368  +    /*  84 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
        31369  +    /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
        31370  +    /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
        31371  +    /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
        31372  +    /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
        31373  +    /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
        31374  +    /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
        31375  +    /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
        31376  +    /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
        31377  +    /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
        31378  +    /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
        31379  +    /*  95 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
        31380  +    /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
        31381  +    /*  97 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
        31382  +    /*  98 */ "Column"           OpHelp("r[P3]=PX"),
        31383  +    /*  99 */ "String8"          OpHelp("r[P2]='P4'"),
 31004  31384       /* 100 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 31005  31385       /* 101 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 31006  31386       /* 102 */ "Count"            OpHelp("r[P2]=count()"),
 31007  31387       /* 103 */ "ReadCookie"       OpHelp(""),
 31008  31388       /* 104 */ "SetCookie"        OpHelp(""),
 31009  31389       /* 105 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 31010  31390       /* 106 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
................................................................................
 31029  31409       /* 125 */ "Rowid"            OpHelp("r[P2]=rowid"),
 31030  31410       /* 126 */ "NullRow"          OpHelp(""),
 31031  31411       /* 127 */ "SeekEnd"          OpHelp(""),
 31032  31412       /* 128 */ "SorterInsert"     OpHelp("key=r[P2]"),
 31033  31413       /* 129 */ "IdxInsert"        OpHelp("key=r[P2]"),
 31034  31414       /* 130 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 31035  31415       /* 131 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 31036         -    /* 132 */ "Real"             OpHelp("r[P2]=P4"),
 31037         -    /* 133 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 31038         -    /* 134 */ "Destroy"          OpHelp(""),
        31416  +    /* 132 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        31417  +    /* 133 */ "Destroy"          OpHelp(""),
        31418  +    /* 134 */ "Real"             OpHelp("r[P2]=P4"),
 31039  31419       /* 135 */ "Clear"            OpHelp(""),
 31040  31420       /* 136 */ "ResetSorter"      OpHelp(""),
 31041  31421       /* 137 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 31042  31422       /* 138 */ "SqlExec"          OpHelp(""),
 31043  31423       /* 139 */ "ParseSchema"      OpHelp(""),
 31044  31424       /* 140 */ "LoadAnalysis"     OpHelp(""),
 31045  31425       /* 141 */ "DropTable"        OpHelp(""),
................................................................................
 31068  31448       /* 164 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
 31069  31449       /* 165 */ "PureFunc"         OpHelp(""),
 31070  31450       /* 166 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 31071  31451       /* 167 */ "Trace"            OpHelp(""),
 31072  31452       /* 168 */ "CursorHint"       OpHelp(""),
 31073  31453       /* 169 */ "Noop"             OpHelp(""),
 31074  31454       /* 170 */ "Explain"          OpHelp(""),
        31455  +    /* 171 */ "Abortable"        OpHelp(""),
 31075  31456     };
 31076  31457     return azName[i];
 31077  31458   }
 31078  31459   #endif
 31079  31460   
 31080  31461   /************** End of opcodes.c *********************************************/
 31081  31462   /************** Begin file os_unix.c *****************************************/
................................................................................
 61530  61911     for(i=0; i<db->nDb; i++){
 61531  61912       p = db->aDb[i].pBt;
 61532  61913       if( p && p->sharable ){
 61533  61914         sqlite3BtreeEnter(p);
 61534  61915         skipOk = 0;
 61535  61916       }
 61536  61917     }
 61537         -  db->skipBtreeMutex = skipOk;
        61918  +  db->noSharedCache = skipOk;
 61538  61919   }
 61539  61920   SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
 61540         -  if( db->skipBtreeMutex==0 ) btreeEnterAll(db);
        61921  +  if( db->noSharedCache==0 ) btreeEnterAll(db);
 61541  61922   }
 61542  61923   static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
 61543  61924     int i;
 61544  61925     Btree *p;
 61545  61926     assert( sqlite3_mutex_held(db->mutex) );
 61546  61927     for(i=0; i<db->nDb; i++){
 61547  61928       p = db->aDb[i].pBt;
 61548  61929       if( p ) sqlite3BtreeLeave(p);
 61549  61930     }
 61550  61931   }
 61551  61932   SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
 61552         -  if( db->skipBtreeMutex==0 ) btreeLeaveAll(db);
        61933  +  if( db->noSharedCache==0 ) btreeLeaveAll(db);
 61553  61934   }
 61554  61935   
 61555  61936   #ifndef NDEBUG
 61556  61937   /*
 61557  61938   ** Return true if the current thread holds the database connection
 61558  61939   ** mutex and all required BtShared mutexes.
 61559  61940   **
................................................................................
 64617  64998             pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
 64618  64999       }
 64619  65000     }
 64620  65001   }
 64621  65002   #else
 64622  65003   # define setDefaultSyncFlag(pBt,safety_level)
 64623  65004   #endif
        65005  +
        65006  +/* Forward declaration */
        65007  +static int newDatabase(BtShared*);
        65008  +
 64624  65009   
 64625  65010   /*
 64626  65011   ** Get a reference to pPage1 of the database file.  This will
 64627  65012   ** also acquire a readlock on that file.
 64628  65013   **
 64629  65014   ** SQLITE_OK is returned on success.  If the file is not a
 64630  65015   ** well-formed database file, then SQLITE_CORRUPT is returned.
................................................................................
 64648  65033     /* Do some checking to help insure the file we opened really is
 64649  65034     ** a valid database file. 
 64650  65035     */
 64651  65036     nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
 64652  65037     sqlite3PagerPagecount(pBt->pPager, &nPageFile);
 64653  65038     if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
 64654  65039       nPage = nPageFile;
        65040  +  }
        65041  +  if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
        65042  +    nPage = 0;
 64655  65043     }
 64656  65044     if( nPage>0 ){
 64657  65045       u32 pageSize;
 64658  65046       u32 usableSize;
 64659  65047       u8 *page1 = pPage1->aData;
 64660  65048       rc = SQLITE_NOTADB;
 64661  65049       /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
................................................................................
 67865  68253     u32 ovflPageSize;
 67866  68254   
 67867  68255     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 67868  68256     pPage->xParseCell(pPage, pCell, pInfo);
 67869  68257     if( pInfo->nLocal==pInfo->nPayload ){
 67870  68258       return SQLITE_OK;  /* No overflow pages. Return without doing anything */
 67871  68259     }
 67872         -  if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
        68260  +  testcase( pCell + pInfo->nSize == pPage->aDataEnd );
        68261  +  testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
        68262  +  if( pCell + pInfo->nSize > pPage->aDataEnd ){
 67873  68263       /* Cell extends past end of page */
 67874  68264       return SQLITE_CORRUPT_PAGE(pPage);
 67875  68265     }
 67876  68266     ovflPgno = get4byte(pCell + pInfo->nSize - 4);
 67877  68267     pBt = pPage->pBt;
 67878  68268     assert( pBt->usableSize > 4 );
 67879  68269     ovflPageSize = pBt->usableSize - 4;
................................................................................
 69791  70181   
 69792  70182     if( pFree ){
 69793  70183       sqlite3PageFree(pFree);
 69794  70184     }
 69795  70185     return rc;
 69796  70186   }
 69797  70187   
        70188  +/* Overwrite content from pX into pDest.  Only do the write if the
        70189  +** content is different from what is already there.
        70190  +*/
        70191  +static int btreeOverwriteContent(
        70192  +  MemPage *pPage,           /* MemPage on which writing will occur */
        70193  +  u8 *pDest,                /* Pointer to the place to start writing */
        70194  +  const BtreePayload *pX,   /* Source of data to write */
        70195  +  int iOffset,              /* Offset of first byte to write */
        70196  +  int iAmt                  /* Number of bytes to be written */
        70197  +){
        70198  +  int nData = pX->nData - iOffset;
        70199  +  if( nData<=0 ){
        70200  +    /* Overwritting with zeros */
        70201  +    int i;
        70202  +    for(i=0; i<iAmt && pDest[i]==0; i++){}
        70203  +    if( i<iAmt ){
        70204  +      int rc = sqlite3PagerWrite(pPage->pDbPage);
        70205  +      if( rc ) return rc;
        70206  +      memset(pDest + i, 0, iAmt - i);
        70207  +    }
        70208  +  }else{
        70209  +    if( nData<iAmt ){
        70210  +      /* Mixed read data and zeros at the end.  Make a recursive call
        70211  +      ** to write the zeros then fall through to write the real data */
        70212  +      int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
        70213  +                                 iAmt-nData);
        70214  +      if( rc ) return rc;
        70215  +      iAmt = nData;
        70216  +    }
        70217  +    if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
        70218  +      int rc = sqlite3PagerWrite(pPage->pDbPage);
        70219  +      if( rc ) return rc;
        70220  +      memcpy(pDest, ((u8*)pX->pData) + iOffset, iAmt);
        70221  +    }
        70222  +  }
        70223  +  return SQLITE_OK;
        70224  +}
        70225  +
        70226  +/*
        70227  +** Overwrite the cell that cursor pCur is pointing to with fresh content
        70228  +** contained in pX.
        70229  +*/
        70230  +static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
        70231  +  int iOffset;                        /* Next byte of pX->pData to write */
        70232  +  int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
        70233  +  int rc;                             /* Return code */
        70234  +  MemPage *pPage = pCur->pPage;       /* Page being written */
        70235  +  BtShared *pBt;                      /* Btree */
        70236  +  Pgno ovflPgno;                      /* Next overflow page to write */
        70237  +  u32 ovflPageSize;                   /* Size to write on overflow page */
        70238  +
        70239  +  if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd ){
        70240  +    return SQLITE_CORRUPT_BKPT;
        70241  +  }
        70242  +  /* Overwrite the local portion first */
        70243  +  rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
        70244  +                             0, pCur->info.nLocal);
        70245  +  if( rc ) return rc;
        70246  +  if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
        70247  +
        70248  +  /* Now overwrite the overflow pages */
        70249  +  iOffset = pCur->info.nLocal;
        70250  +  assert( nTotal>=0 );
        70251  +  assert( iOffset>=0 );
        70252  +  ovflPgno = get4byte(pCur->info.pPayload + iOffset);
        70253  +  pBt = pPage->pBt;
        70254  +  ovflPageSize = pBt->usableSize - 4;
        70255  +  do{
        70256  +    rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
        70257  +    if( rc ) return rc;
        70258  +    if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
        70259  +      rc = SQLITE_CORRUPT_BKPT;
        70260  +    }else{
        70261  +      if( iOffset+ovflPageSize<(u32)nTotal ){
        70262  +        ovflPgno = get4byte(pPage->aData);
        70263  +      }else{
        70264  +        ovflPageSize = nTotal - iOffset;
        70265  +      }
        70266  +      rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
        70267  +                                 iOffset, ovflPageSize);
        70268  +    }
        70269  +    sqlite3PagerUnref(pPage->pDbPage);
        70270  +    if( rc ) return rc;
        70271  +    iOffset += ovflPageSize;
        70272  +  }while( iOffset<nTotal );
        70273  +  return SQLITE_OK;    
        70274  +}
        70275  +
 69798  70276   
 69799  70277   /*
 69800  70278   ** Insert a new record into the BTree.  The content of the new record
 69801  70279   ** is described by the pX object.  The pCur cursor is used only to
 69802  70280   ** define what table the record should be inserted into, and is left
 69803  70281   ** pointing at a random location.
 69804  70282   **
................................................................................
 69881  70359     if( pCur->pKeyInfo==0 ){
 69882  70360       assert( pX->pKey==0 );
 69883  70361       /* If this is an insert into a table b-tree, invalidate any incrblob 
 69884  70362       ** cursors open on the row being replaced */
 69885  70363       invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
 69886  70364   
 69887  70365       /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 
 69888         -    ** to a row with the same key as the new entry being inserted.  */
 69889         -    assert( (flags & BTREE_SAVEPOSITION)==0 || 
 69890         -            ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
        70366  +    ** to a row with the same key as the new entry being inserted.
        70367  +    */
        70368  +#ifdef SQLITE_DEBUG
        70369  +    if( flags & BTREE_SAVEPOSITION ){
        70370  +      assert( pCur->curFlags & BTCF_ValidNKey );
        70371  +      assert( pX->nKey==pCur->info.nKey );
        70372  +      assert( pCur->info.nSize!=0 );
        70373  +      assert( loc==0 );
        70374  +    }
        70375  +#endif
 69891  70376   
 69892         -    /* If the cursor is currently on the last row and we are appending a
 69893         -    ** new row onto the end, set the "loc" to avoid an unnecessary
 69894         -    ** btreeMoveto() call */
        70377  +    /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
        70378  +    ** that the cursor is not pointing to a row to be overwritten.
        70379  +    ** So do a complete check.
        70380  +    */
 69895  70381       if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
 69896         -      loc = 0;
        70382  +      /* The cursor is pointing to the entry that is to be
        70383  +      ** overwritten */
        70384  +      assert( pX->nData>=0 && pX->nZero>=0 );
        70385  +      if( pCur->info.nSize!=0
        70386  +       && pCur->info.nPayload==(u32)pX->nData+pX->nZero
        70387  +      ){
        70388  +        /* New entry is the same size as the old.  Do an overwrite */
        70389  +        return btreeOverwriteCell(pCur, pX);
        70390  +      }
        70391  +      assert( loc==0 );
 69897  70392       }else if( loc==0 ){
        70393  +      /* The cursor is *not* pointing to the cell to be overwritten, nor
        70394  +      ** to an adjacent cell.  Move the cursor so that it is pointing either
        70395  +      ** to the cell to be overwritten or an adjacent cell.
        70396  +      */
 69898  70397         rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
 69899  70398         if( rc ) return rc;
 69900  70399       }
 69901         -  }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
        70400  +  }else{
        70401  +    /* This is an index or a WITHOUT ROWID table */
        70402  +
        70403  +    /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 
        70404  +    ** to a row with the same key as the new entry being inserted.
        70405  +    */
        70406  +    assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
        70407  +
        70408  +    /* If the cursor is not already pointing either to the cell to be
        70409  +    ** overwritten, or if a new cell is being inserted, if the cursor is
        70410  +    ** not pointing to an immediately adjacent cell, then move the cursor
        70411  +    ** so that it does.
        70412  +    */
        70413  +    if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
 69902  70414       if( pX->nMem ){
 69903  70415         UnpackedRecord r;
 69904  70416         r.pKeyInfo = pCur->pKeyInfo;
 69905  70417         r.aMem = pX->aMem;
 69906  70418         r.nField = pX->nMem;
 69907  70419         r.default_rc = 0;
 69908  70420         r.errCode = 0;
................................................................................
 69911  70423         r.eqSeen = 0;
 69912  70424         rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
 69913  70425       }else{
 69914  70426         rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
 69915  70427       }
 69916  70428       if( rc ) return rc;
 69917  70429     }
        70430  +
        70431  +    /* If the cursor is currently pointing to an entry to be overwritten
        70432  +    ** and the new content is the same as as the old, then use the
        70433  +    ** overwrite optimization.
        70434  +    */
        70435  +    if( loc==0 ){
        70436  +      getCellInfo(pCur);
        70437  +      if( pCur->info.nKey==pX->nKey ){
        70438  +        BtreePayload x2;
        70439  +        x2.pData = pX->pKey;
        70440  +        x2.nData = pX->nKey;
        70441  +        x2.nZero = 0;
        70442  +        return btreeOverwriteCell(pCur, &x2);
        70443  +      }
        70444  +    }
        70445  +
        70446  +  }
 69918  70447     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 69919  70448   
 69920  70449     pPage = pCur->pPage;
 69921  70450     assert( pPage->intKey || pX->nKey>=0 );
 69922  70451     assert( pPage->leaf || !pPage->intKey );
 69923  70452   
 69924  70453     TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
................................................................................
 70748  71277   ){
 70749  71278     va_list ap;
 70750  71279     if( !pCheck->mxErr ) return;
 70751  71280     pCheck->mxErr--;
 70752  71281     pCheck->nErr++;
 70753  71282     va_start(ap, zFormat);
 70754  71283     if( pCheck->errMsg.nChar ){
 70755         -    sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
        71284  +    sqlite3_str_append(&pCheck->errMsg, "\n", 1);
 70756  71285     }
 70757  71286     if( pCheck->zPfx ){
 70758         -    sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
        71287  +    sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
 70759  71288     }
 70760         -  sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap);
        71289  +  sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
 70761  71290     va_end(ap);
 70762         -  if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
        71291  +  if( pCheck->errMsg.accError==SQLITE_NOMEM ){
 70763  71292       pCheck->mallocFailed = 1;
 70764  71293     }
 70765  71294   }
 70766  71295   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 70767  71296   
 70768  71297   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 70769  71298   
................................................................................
 71339  71868   
 71340  71869     /* Clean  up and report errors.
 71341  71870     */
 71342  71871   integrity_ck_cleanup:
 71343  71872     sqlite3PageFree(sCheck.heap);
 71344  71873     sqlite3_free(sCheck.aPgRef);
 71345  71874     if( sCheck.mallocFailed ){
 71346         -    sqlite3StrAccumReset(&sCheck.errMsg);
        71875  +    sqlite3_str_reset(&sCheck.errMsg);
 71347  71876       sCheck.nErr++;
 71348  71877     }
 71349  71878     *pnErr = sCheck.nErr;
 71350         -  if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
        71879  +  if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
 71351  71880     /* Make sure this analysis did not leave any unref() pages. */
 71352  71881     assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
 71353  71882     sqlite3BtreeLeave(p);
 71354  71883     return sqlite3StrAccumFinish(&sCheck.errMsg);
 71355  71884   }
 71356  71885   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 71357  71886   
................................................................................
 73243  73772     }else{
 73244  73773       pMem->u.i = val;
 73245  73774       pMem->flags = MEM_Int;
 73246  73775     }
 73247  73776   }
 73248  73777   
 73249  73778   /* A no-op destructor */
 73250         -static void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
        73779  +SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
 73251  73780   
 73252  73781   /*
 73253  73782   ** Set the value stored in *pMem should already be a NULL.
 73254  73783   ** Also store a pointer to go with it.
 73255  73784   */
 73256  73785   SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
 73257  73786     Mem *pMem,
................................................................................
 73921  74450       zVal = &pExpr->u.zToken[2];
 73922  74451       nVal = sqlite3Strlen30(zVal)-1;
 73923  74452       assert( zVal[nVal]=='\'' );
 73924  74453       sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
 73925  74454                            0, SQLITE_DYNAMIC);
 73926  74455     }
 73927  74456   #endif
 73928         -
 73929  74457   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 73930  74458     else if( op==TK_FUNCTION && pCtx!=0 ){
 73931  74459       rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
 73932  74460     }
 73933  74461   #endif
        74462  +  else if( op==TK_TRUEFALSE ){
        74463  +     pVal = valueNew(db, pCtx);
        74464  +     pVal->flags = MEM_Int;
        74465  +     pVal->u.i = pExpr->u.zToken[4]==0;
        74466  +  }
 73934  74467   
 73935  74468     *ppVal = pVal;
 73936  74469     return rc;
 73937  74470   
 73938  74471   no_mem:
 73939  74472   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 73940  74473     if( pCtx==0 || pCtx->pParse->nErr==0 )
................................................................................
 74585  75118     int p4type          /* P4 operand type */
 74586  75119   ){
 74587  75120     char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
 74588  75121     if( p4copy ) memcpy(p4copy, zP4, 8);
 74589  75122     return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
 74590  75123   }
 74591  75124   
        75125  +#ifndef SQLITE_OMIT_EXPLAIN
        75126  +/*
        75127  +** Return the address of the current EXPLAIN QUERY PLAN baseline.
        75128  +** 0 means "none".
        75129  +*/
        75130  +SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
        75131  +  VdbeOp *pOp;
        75132  +  if( pParse->addrExplain==0 ) return 0;
        75133  +  pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
        75134  +  return pOp->p2;
        75135  +}
        75136  +
        75137  +/*
        75138  +** Add a new OP_Explain opcode.
        75139  +**
        75140  +** If the bPush flag is true, then make this opcode the parent for
        75141  +** subsequent Explains until sqlite3VdbeExplainPop() is called.
        75142  +*/
        75143  +SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
        75144  +  if( pParse->explain==2 ){
        75145  +    char *zMsg;
        75146  +    Vdbe *v = pParse->pVdbe;
        75147  +    va_list ap;
        75148  +    int iThis;
        75149  +    va_start(ap, zFmt);
        75150  +    zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
        75151  +    va_end(ap);
        75152  +    v = pParse->pVdbe;
        75153  +    iThis = v->nOp;
        75154  +    sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
        75155  +                      zMsg, P4_DYNAMIC);
        75156  +    if( bPush) pParse->addrExplain = iThis;
        75157  +  }
        75158  +}
        75159  +
        75160  +/*
        75161  +** Pop the EXPLAIN QUERY PLAN stack one level.
        75162  +*/
        75163  +SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
        75164  +  pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
        75165  +}
        75166  +#endif /* SQLITE_OMIT_EXPLAIN */
        75167  +
 74592  75168   /*
 74593  75169   ** Add an OP_ParseSchema opcode.  This routine is broken out from
 74594  75170   ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
 74595  75171   ** as having been used.
 74596  75172   **
 74597  75173   ** The zWhere string must have been obtained from sqlite3_malloc().
 74598  75174   ** This routine will take ownership of the allocated memory.
................................................................................
 74674  75250   SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
 74675  75251     Parse *p = v->pParse;
 74676  75252     int j = ADDR(x);
 74677  75253     assert( v->magic==VDBE_MAGIC_INIT );
 74678  75254     assert( j<p->nLabel );
 74679  75255     assert( j>=0 );
 74680  75256     if( p->aLabel ){
        75257  +#ifdef SQLITE_DEBUG
        75258  +    if( p->db->flags & SQLITE_VdbeAddopTrace ){
        75259  +      printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
        75260  +    }
        75261  +#endif
        75262  +    assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
 74681  75263       p->aLabel[j] = v->nOp;
 74682  75264     }
 74683  75265   }
 74684  75266   
        75267  +#ifdef SQLITE_COVERAGE_TEST
        75268  +/*
        75269  +** Return TRUE if and only if the label x has already been resolved.
        75270  +** Return FALSE (zero) if label x is still unresolved.
        75271  +**
        75272  +** This routine is only used inside of testcase() macros, and so it
        75273  +** only exists when measuring test coverage.
        75274  +*/
        75275  +SQLITE_PRIVATE int sqlite3VdbeLabelHasBeenResolved(Vdbe *v, int x){
        75276  +  return v->pParse->aLabel && v->pParse->aLabel[ADDR(x)]>=0;
        75277  +}
        75278  +#endif /* SQLITE_COVERAGE_TEST */
        75279  +
 74685  75280   /*
 74686  75281   ** Mark the VDBE as one that can only be run one time.
 74687  75282   */
 74688  75283   SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
 74689  75284     p->runOnlyOnce = 1;
 74690  75285   }
 74691  75286   
................................................................................
 74822  75417     ** through all opcodes and hasAbort may be set incorrectly. Return
 74823  75418     ** true for this case to prevent the assert() in the callers frame
 74824  75419     ** from failing.  */
 74825  75420     return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
 74826  75421                 || (hasCreateTable && hasInitCoroutine) );
 74827  75422   }
 74828  75423   #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
        75424  +
        75425  +#ifdef SQLITE_DEBUG
        75426  +/*
        75427  +** Increment the nWrite counter in the VDBE if the cursor is not an
        75428  +** ephemeral cursor, or if the cursor argument is NULL.
        75429  +*/
        75430  +SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
        75431  +  if( pC==0
        75432  +   || (pC->eCurType!=CURTYPE_SORTER
        75433  +       && pC->eCurType!=CURTYPE_PSEUDO
        75434  +       && !pC->isEphemeral)
        75435  +  ){
        75436  +    p->nWrite++;
        75437  +  }
        75438  +}
        75439  +#endif
        75440  +
        75441  +#ifdef SQLITE_DEBUG
        75442  +/*
        75443  +** Assert if an Abort at this point in time might result in a corrupt
        75444  +** database.
        75445  +*/
        75446  +SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
        75447  +  assert( p->nWrite==0 || p->usesStmtJournal );
        75448  +}
        75449  +#endif
 74829  75450   
 74830  75451   /*
 74831  75452   ** This routine is called after all opcodes have been inserted.  It loops
 74832  75453   ** through all the opcodes and fixes up some details.
 74833  75454   **
 74834  75455   ** (1) For each jump instruction with a negative P2 value (a label)
 74835  75456   **     resolve the P2 value to an actual address.
................................................................................
 74982  75603     int i;
 74983  75604     for(i=0; i<p->nOp; i++){
 74984  75605       assert( p->aOp[i].opcode!=OP_ResultRow );
 74985  75606     }
 74986  75607   }
 74987  75608   #endif
 74988  75609   
        75610  +/*
        75611  +** Generate code (a single OP_Abortable opcode) that will
        75612  +** verify that the VDBE program can safely call Abort in the current
        75613  +** context.
        75614  +*/
        75615  +#if defined(SQLITE_DEBUG)
        75616  +SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
        75617  +  if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
        75618  +}
        75619  +#endif
        75620  +
 74989  75621   /*
 74990  75622   ** This function returns a pointer to the array of opcodes associated with
 74991  75623   ** the Vdbe passed as the first argument. It is the callers responsibility
 74992  75624   ** to arrange for the returned array to be eventually freed using the 
 74993  75625   ** vdbeFreeOpArray() function.
 74994  75626   **
 74995  75627   ** Before returning, *pnOp is set to the number of entries in the returned
................................................................................
 75526  76158   ** Translate the P4.pExpr value for an OP_CursorHint opcode into text
 75527  76159   ** that can be displayed in the P4 column of EXPLAIN output.
 75528  76160   */
 75529  76161   static void displayP4Expr(StrAccum *p, Expr *pExpr){
 75530  76162     const char *zOp = 0;
 75531  76163     switch( pExpr->op ){
 75532  76164       case TK_STRING:
 75533         -      sqlite3XPrintf(p, "%Q", pExpr->u.zToken);
        76165  +      sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
 75534  76166         break;
 75535  76167       case TK_INTEGER:
 75536         -      sqlite3XPrintf(p, "%d", pExpr->u.iValue);
        76168  +      sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
 75537  76169         break;
 75538  76170       case TK_NULL:
 75539         -      sqlite3XPrintf(p, "NULL");
        76171  +      sqlite3_str_appendf(p, "NULL");
 75540  76172         break;
 75541  76173       case TK_REGISTER: {
 75542         -      sqlite3XPrintf(p, "r[%d]", pExpr->iTable);
        76174  +      sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
 75543  76175         break;
 75544  76176       }
 75545  76177       case TK_COLUMN: {
 75546  76178         if( pExpr->iColumn<0 ){
 75547         -        sqlite3XPrintf(p, "rowid");
        76179  +        sqlite3_str_appendf(p, "rowid");
 75548  76180         }else{
 75549         -        sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn);
        76181  +        sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
 75550  76182         }
 75551  76183         break;
 75552  76184       }
 75553  76185       case TK_LT:      zOp = "LT";      break;
 75554  76186       case TK_LE:      zOp = "LE";      break;
 75555  76187       case TK_GT:      zOp = "GT";      break;
 75556  76188       case TK_GE:      zOp = "GE";      break;
................................................................................
 75574  76206       case TK_UPLUS:   zOp = "PLUS";    break;
 75575  76207       case TK_BITNOT:  zOp = "BITNOT";  break;
 75576  76208       case TK_NOT:     zOp = "NOT";     break;
 75577  76209       case TK_ISNULL:  zOp = "ISNULL";  break;
 75578  76210       case TK_NOTNULL: zOp = "NOTNULL"; break;
 75579  76211   
 75580  76212       default:
 75581         -      sqlite3XPrintf(p, "%s", "expr");
        76213  +      sqlite3_str_appendf(p, "%s", "expr");
 75582  76214         break;
 75583  76215     }
 75584  76216   
 75585  76217     if( zOp ){
 75586         -    sqlite3XPrintf(p, "%s(", zOp);
        76218  +    sqlite3_str_appendf(p, "%s(", zOp);
 75587  76219       displayP4Expr(p, pExpr->pLeft);
 75588  76220       if( pExpr->pRight ){
 75589         -      sqlite3StrAccumAppend(p, ",", 1);
        76221  +      sqlite3_str_append(p, ",", 1);
 75590  76222         displayP4Expr(p, pExpr->pRight);
 75591  76223       }
 75592         -    sqlite3StrAccumAppend(p, ")", 1);
        76224  +    sqlite3_str_append(p, ")", 1);
 75593  76225     }
 75594  76226   }
 75595  76227   #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
 75596  76228   
 75597  76229   
 75598  76230   #if VDBE_DISPLAY_P4
 75599  76231   /*
................................................................................
 75606  76238     assert( nTemp>=20 );
 75607  76239     sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
 75608  76240     switch( pOp->p4type ){
 75609  76241       case P4_KEYINFO: {
 75610  76242         int j;
 75611  76243         KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
 75612  76244         assert( pKeyInfo->aSortOrder!=0 );
 75613         -      sqlite3XPrintf(&x, "k(%d", pKeyInfo->nKeyField);
        76245  +      sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
 75614  76246         for(j=0; j<pKeyInfo->nKeyField; j++){
 75615  76247           CollSeq *pColl = pKeyInfo->aColl[j];
 75616  76248           const char *zColl = pColl ? pColl->zName : "";
 75617  76249           if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
 75618         -        sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
        76250  +        sqlite3_str_appendf(&x, ",%s%s", 
        76251  +               pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
 75619  76252         }
 75620         -      sqlite3StrAccumAppend(&x, ")", 1);
        76253  +      sqlite3_str_append(&x, ")", 1);
 75621  76254         break;
 75622  76255       }
 75623  76256   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 75624  76257       case P4_EXPR: {
 75625  76258         displayP4Expr(&x, pOp->p4.pExpr);
 75626  76259         break;
 75627  76260       }
 75628  76261   #endif
 75629  76262       case P4_COLLSEQ: {
 75630  76263         CollSeq *pColl = pOp->p4.pColl;
 75631         -      sqlite3XPrintf(&x, "(%.20s)", pColl->zName);
        76264  +      sqlite3_str_appendf(&x, "(%.20s)", pColl->zName);
 75632  76265         break;
 75633  76266       }
 75634  76267       case P4_FUNCDEF: {
 75635  76268         FuncDef *pDef = pOp->p4.pFunc;
 75636         -      sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
        76269  +      sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
 75637  76270         break;
 75638  76271       }
 75639  76272   #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
 75640  76273       case P4_FUNCCTX: {
 75641  76274         FuncDef *pDef = pOp->p4.pCtx->pFunc;
 75642         -      sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
        76275  +      sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
 75643  76276         break;
 75644  76277       }
 75645  76278   #endif
 75646  76279       case P4_INT64: {
 75647         -      sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64);
        76280  +      sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
 75648  76281         break;
 75649  76282       }
 75650  76283       case P4_INT32: {
 75651         -      sqlite3XPrintf(&x, "%d", pOp->p4.i);
        76284  +      sqlite3_str_appendf(&x, "%d", pOp->p4.i);
 75652  76285         break;
 75653  76286       }
 75654  76287       case P4_REAL: {
 75655         -      sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal);
        76288  +      sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
 75656  76289         break;
 75657  76290       }
 75658  76291       case P4_MEM: {
 75659  76292         Mem *pMem = pOp->p4.pMem;
 75660  76293         if( pMem->flags & MEM_Str ){
 75661  76294           zP4 = pMem->z;
 75662  76295         }else if( pMem->flags & MEM_Int ){
 75663         -        sqlite3XPrintf(&x, "%lld", pMem->u.i);
        76296  +        sqlite3_str_appendf(&x, "%lld", pMem->u.i);
 75664  76297         }else if( pMem->flags & MEM_Real ){
 75665         -        sqlite3XPrintf(&x, "%.16g", pMem->u.r);
        76298  +        sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
 75666  76299         }else if( pMem->flags & MEM_Null ){
 75667  76300           zP4 = "NULL";
 75668  76301         }else{
 75669  76302           assert( pMem->flags & MEM_Blob );
 75670  76303           zP4 = "(blob)";
 75671  76304         }
 75672  76305         break;
 75673  76306       }
 75674  76307   #ifndef SQLITE_OMIT_VIRTUALTABLE
 75675  76308       case P4_VTAB: {
 75676  76309         sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
 75677         -      sqlite3XPrintf(&x, "vtab:%p", pVtab);
        76310  +      sqlite3_str_appendf(&x, "vtab:%p", pVtab);
 75678  76311         break;
 75679  76312       }
 75680  76313   #endif
 75681  76314       case P4_INTARRAY: {
 75682  76315         int i;
 75683  76316         int *ai = pOp->p4.ai;
 75684  76317         int n = ai[0];   /* The first element of an INTARRAY is always the
 75685  76318                          ** count of the number of elements to follow */
 75686  76319         for(i=1; i<=n; i++){
 75687         -        sqlite3XPrintf(&x, ",%d", ai[i]);
        76320  +        sqlite3_str_appendf(&x, ",%d", ai[i]);
 75688  76321         }
 75689  76322         zTemp[0] = '[';
 75690         -      sqlite3StrAccumAppend(&x, "]", 1);
        76323  +      sqlite3_str_append(&x, "]", 1);
 75691  76324         break;
 75692  76325       }
 75693  76326       case P4_SUBPROGRAM: {
 75694         -      sqlite3XPrintf(&x, "program");
        76327  +      sqlite3_str_appendf(&x, "program");
 75695  76328         break;
 75696  76329       }
 75697  76330       case P4_DYNBLOB:
 75698  76331       case P4_ADVANCE: {
 75699  76332         zTemp[0] = 0;
 75700  76333         break;
 75701  76334       }
 75702  76335       case P4_TABLE: {
 75703         -      sqlite3XPrintf(&x, "%s", pOp->p4.pTab->zName);
        76336  +      sqlite3_str_appendf(&x, "%s", pOp->p4.pTab->zName);
 75704  76337         break;
 75705  76338       }
 75706  76339       default: {
 75707  76340         zP4 = pOp->p4.z;
 75708  76341         if( zP4==0 ){
 75709  76342           zP4 = zTemp;
 75710  76343           zTemp[0] = 0;
................................................................................
 75908  76541   ** running the code, it invokes the callback once for each instruction.
 75909  76542   ** This feature is used to implement "EXPLAIN".
 75910  76543   **
 75911  76544   ** When p->explain==1, each instruction is listed.  When
 75912  76545   ** p->explain==2, only OP_Explain instructions are listed and these
 75913  76546   ** are shown in a different format.  p->explain==2 is used to implement
 75914  76547   ** EXPLAIN QUERY PLAN.
        76548  +** 2018-04-24:  In p->explain==2 mode, the OP_Init opcodes of triggers
        76549  +** are also shown, so that the boundaries between the main program and
        76550  +** each trigger are clear.
 75915  76551   **
 75916  76552   ** When p->explain==1, first the main program is listed, then each of
 75917  76553   ** the trigger subprograms are listed one by one.
 75918  76554   */
 75919  76555   SQLITE_PRIVATE int sqlite3VdbeList(
 75920  76556     Vdbe *p                   /* The VDBE */
 75921  76557   ){
................................................................................
 75970  76606         apSub = (SubProgram **)pSub->z;
 75971  76607       }
 75972  76608       for(i=0; i<nSub; i++){
 75973  76609         nRow += apSub[i]->nOp;
 75974  76610       }
 75975  76611     }
 75976  76612   
 75977         -  do{
        76613  +  while(1){  /* Loop exits via break */
 75978  76614       i = p->pc++;
 75979  76615       if( i>=nRow ){
 75980  76616         p->rc = SQLITE_OK;
 75981  76617         rc = SQLITE_DONE;
 75982  76618         break;
 75983  76619       }
 75984  76620       if( i<p->nOp ){
................................................................................
 76016  76652           apSub = (SubProgram **)pSub->z;
 76017  76653           apSub[nSub++] = pOp->p4.pProgram;
 76018  76654           pSub->flags |= MEM_Blob;
 76019  76655           pSub->n = nSub*sizeof(SubProgram*);
 76020  76656           nRow += pOp->p4.pProgram->nOp;
 76021  76657         }
 76022  76658       }
 76023         -  }while( p->explain==2 && pOp->opcode!=OP_Explain );
        76659  +    if( p->explain<2 ) break;
        76660  +    if( pOp->opcode==OP_Explain ) break;
        76661  +    if( pOp->opcode==OP_Init && p->pc>1 ) break;
        76662  +  }
 76024  76663   
 76025  76664     if( rc==SQLITE_OK ){
 76026  76665       if( db->u1.isInterrupted ){
 76027  76666         p->rc = SQLITE_INTERRUPT;
 76028  76667         rc = SQLITE_ERROR;
 76029  76668         sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
 76030  76669       }else{
................................................................................
 77208  77847     if( p->aMem ){
 77209  77848       for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
 77210  77849     }
 77211  77850   #endif
 77212  77851     sqlite3DbFree(db, p->zErrMsg);
 77213  77852     p->zErrMsg = 0;
 77214  77853     p->pResultSet = 0;
        77854  +#ifdef SQLITE_DEBUG
        77855  +  p->nWrite = 0;
        77856  +#endif
 77215  77857   
 77216  77858     /* Save profiling information from this VDBE run.
 77217  77859     */
 77218  77860   #ifdef VDBE_PROFILE
 77219  77861     {
 77220  77862       FILE *out = fopen("vdbe_profile.out", "a");
 77221  77863       if( out ){
................................................................................
 78130  78772       if( x<r ) return -1;
 78131  78773       if( x>r ) return +1;
 78132  78774       return 0;
 78133  78775     }else{
 78134  78776       i64 y;
 78135  78777       double s;
 78136  78778       if( r<-9223372036854775808.0 ) return +1;
 78137         -    if( r>9223372036854775807.0 ) return -1;
        78779  +    if( r>=9223372036854775808.0 ) return -1;
 78138  78780       y = (i64)r;
 78139  78781       if( i<y ) return -1;
 78140         -    if( i>y ){
 78141         -      if( y==SMALLEST_INT64 && r>0.0 ) return -1;
 78142         -      return +1;
 78143         -    }
        78782  +    if( i>y ) return +1;
 78144  78783       s = (double)i;
 78145  78784       if( s<r ) return -1;
 78146  78785       if( s>r ) return +1;
 78147  78786       return 0;
 78148  78787     }
 78149  78788   }
 78150  78789   
................................................................................
 79814  80453     if( *piTime==0 ){
 79815  80454       rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
 79816  80455       if( rc ) *piTime = 0;
 79817  80456     }
 79818  80457     return *piTime;
 79819  80458   }
 79820  80459   
 79821         -/*
 79822         -** The following is the implementation of an SQL function that always
 79823         -** fails with an error message stating that the function is used in the
 79824         -** wrong context.  The sqlite3_overload_function() API might construct
 79825         -** SQL function that use this routine so that the functions will exist
 79826         -** for name resolution but are actually overloaded by the xFindFunction
 79827         -** method of virtual tables.
 79828         -*/
 79829         -SQLITE_PRIVATE void sqlite3InvalidFunction(
 79830         -  sqlite3_context *context,  /* The function calling context */
 79831         -  int NotUsed,               /* Number of arguments to the function */
 79832         -  sqlite3_value **NotUsed2   /* Value of each argument */
 79833         -){
 79834         -  const char *zName = context->pFunc->zName;
 79835         -  char *zErr;
 79836         -  UNUSED_PARAMETER2(NotUsed, NotUsed2);
 79837         -  zErr = sqlite3_mprintf(
 79838         -      "unable to use function %s in the requested context", zName);
 79839         -  sqlite3_result_error(context, zErr, -1);
 79840         -  sqlite3_free(zErr);
 79841         -}
 79842         -
 79843  80460   /*
 79844  80461   ** Create a new aggregate context for p and return a pointer to
 79845  80462   ** its pMem->z element.
 79846  80463   */
 79847  80464   static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
 79848  80465     Mem *pMem = p->pMem;
 79849  80466     assert( (pMem->flags & MEM_Agg)==0 );
................................................................................
 81103  81720     db = p->db;
 81104  81721     sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase), 
 81105  81722                         db->aLimit[SQLITE_LIMIT_LENGTH]);
 81106  81723     if( db->nVdbeExec>1 ){
 81107  81724       while( *zRawSql ){
 81108  81725         const char *zStart = zRawSql;
 81109  81726         while( *(zRawSql++)!='\n' && *zRawSql );
 81110         -      sqlite3StrAccumAppend(&out, "-- ", 3);
        81727  +      sqlite3_str_append(&out, "-- ", 3);
 81111  81728         assert( (zRawSql - zStart) > 0 );
 81112         -      sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
        81729  +      sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
 81113  81730       }
 81114  81731     }else if( p->nVar==0 ){
 81115         -    sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql));
        81732  +    sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
 81116  81733     }else{
 81117  81734       while( zRawSql[0] ){
 81118  81735         n = findNextHostParameter(zRawSql, &nToken);
 81119  81736         assert( n>0 );
 81120         -      sqlite3StrAccumAppend(&out, zRawSql, n);
        81737  +      sqlite3_str_append(&out, zRawSql, n);
 81121  81738         zRawSql += n;
 81122  81739         assert( zRawSql[0] || nToken==0 );
 81123  81740         if( nToken==0 ) break;
 81124  81741         if( zRawSql[0]=='?' ){
 81125  81742           if( nToken>1 ){
 81126  81743             assert( sqlite3Isdigit(zRawSql[1]) );
 81127  81744             sqlite3GetInt32(&zRawSql[1], &idx);
................................................................................
 81139  81756           assert( idx>0 );
 81140  81757         }
 81141  81758         zRawSql += nToken;
 81142  81759         nextIndex = idx + 1;
 81143  81760         assert( idx>0 && idx<=p->nVar );
 81144  81761         pVar = &p->aVar[idx-1];
 81145  81762         if( pVar->flags & MEM_Null ){
 81146         -        sqlite3StrAccumAppend(&out, "NULL", 4);
        81763  +        sqlite3_str_append(&out, "NULL", 4);
 81147  81764         }else if( pVar->flags & MEM_Int ){
 81148         -        sqlite3XPrintf(&out, "%lld", pVar->u.i);
        81765  +        sqlite3_str_appendf(&out, "%lld", pVar->u.i);
 81149  81766         }else if( pVar->flags & MEM_Real ){
 81150         -        sqlite3XPrintf(&out, "%!.15g", pVar->u.r);
        81767  +        sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
 81151  81768         }else if( pVar->flags & MEM_Str ){
 81152  81769           int nOut;  /* Number of bytes of the string text to include in output */
 81153  81770   #ifndef SQLITE_OMIT_UTF16
 81154  81771           u8 enc = ENC(db);
 81155  81772           if( enc!=SQLITE_UTF8 ){
 81156  81773             memset(&utf8, 0, sizeof(utf8));
 81157  81774             utf8.db = db;
 81158  81775             sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
 81159  81776             if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){
 81160         -            out.accError = STRACCUM_NOMEM;
        81777  +            out.accError = SQLITE_NOMEM;
 81161  81778               out.nAlloc = 0;
 81162  81779             }
 81163  81780             pVar = &utf8;
 81164  81781           }
 81165  81782   #endif
 81166  81783           nOut = pVar->n;
 81167  81784   #ifdef SQLITE_TRACE_SIZE_LIMIT
 81168  81785           if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
 81169  81786             nOut = SQLITE_TRACE_SIZE_LIMIT;
 81170  81787             while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
 81171  81788           }
 81172  81789   #endif    
 81173         -        sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
        81790  +        sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
 81174  81791   #ifdef SQLITE_TRACE_SIZE_LIMIT
 81175  81792           if( nOut<pVar->n ){
 81176         -          sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
        81793  +          sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
 81177  81794           }
 81178  81795   #endif
 81179  81796   #ifndef SQLITE_OMIT_UTF16
 81180  81797           if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
 81181  81798   #endif
 81182  81799         }else if( pVar->flags & MEM_Zero ){
 81183         -        sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
        81800  +        sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
 81184  81801         }else{
 81185  81802           int nOut;  /* Number of bytes of the blob to include in output */
 81186  81803           assert( pVar->flags & MEM_Blob );
 81187         -        sqlite3StrAccumAppend(&out, "x'", 2);
        81804  +        sqlite3_str_append(&out, "x'", 2);
 81188  81805           nOut = pVar->n;
 81189  81806   #ifdef SQLITE_TRACE_SIZE_LIMIT
 81190  81807           if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
 81191  81808   #endif
 81192  81809           for(i=0; i<nOut; i++){
 81193         -          sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
        81810  +          sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
 81194  81811           }
 81195         -        sqlite3StrAccumAppend(&out, "'", 1);
        81812  +        sqlite3_str_append(&out, "'", 1);
 81196  81813   #ifdef SQLITE_TRACE_SIZE_LIMIT
 81197  81814           if( nOut<pVar->n ){
 81198         -          sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
        81815  +          sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
 81199  81816           }
 81200  81817   #endif
 81201  81818         }
 81202  81819       }
 81203  81820     }
 81204         -  if( out.accError ) sqlite3StrAccumReset(&out);
        81821  +  if( out.accError ) sqlite3_str_reset(&out);
 81205  81822     return sqlite3StrAccumFinish(&out);
 81206  81823   }
 81207  81824   
 81208  81825   #endif /* #ifndef SQLITE_OMIT_TRACE */
 81209  81826   
 81210  81827   /************** End of vdbetrace.c *******************************************/
 81211  81828   /************** Begin file vdbe.c ********************************************/
................................................................................
 82211  82828   ** Check the value in register P3.  If it is NULL then Halt using
 82212  82829   ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
 82213  82830   ** value in register P3 is not NULL, then this routine is a no-op.
 82214  82831   ** The P5 parameter should be 1.
 82215  82832   */
 82216  82833   case OP_HaltIfNull: {      /* in3 */
 82217  82834     pIn3 = &aMem[pOp->p3];
        82835  +#ifdef SQLITE_DEBUG
        82836  +  if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
        82837  +#endif
 82218  82838     if( (pIn3->flags & MEM_Null)==0 ) break;
 82219  82839     /* Fall through into OP_Halt */
 82220  82840   }
 82221  82841   
 82222  82842   /* Opcode:  Halt P1 P2 * P4 P5
 82223  82843   **
 82224  82844   ** Exit immediately.  All open cursors, etc are closed
................................................................................
 82250  82870   ** is the same as executing Halt.
 82251  82871   */
 82252  82872   case OP_Halt: {
 82253  82873     VdbeFrame *pFrame;
 82254  82874     int pcx;
 82255  82875   
 82256  82876     pcx = (int)(pOp - aOp);
        82877  +#ifdef SQLITE_DEBUG
        82878  +  if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
        82879  +#endif
 82257  82880     if( pOp->p1==SQLITE_OK && p->pFrame ){
 82258  82881       /* Halt the sub-program. Return control to the parent frame. */
 82259  82882       pFrame = p->pFrame;
 82260  82883       p->pFrame = pFrame->pParent;
 82261  82884       p->nFrame--;
 82262  82885       sqlite3VdbeSetChanges(db, p->nChange);
 82263  82886       pcx = sqlite3VdbeFrameRestore(pFrame);
................................................................................
 84620  85243   ** size, and so forth.  P1==0 is the main database file and P1==1 is the 
 84621  85244   ** database file used to store temporary tables.
 84622  85245   **
 84623  85246   ** A transaction must be started before executing this opcode.
 84624  85247   */
 84625  85248   case OP_SetCookie: {
 84626  85249     Db *pDb;
        85250  +
        85251  +  sqlite3VdbeIncrWriteCounter(p, 0);
 84627  85252     assert( pOp->p2<SQLITE_N_BTREE_META );
 84628  85253     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 84629  85254     assert( DbMaskTest(p->btreeMask, pOp->p1) );
 84630  85255     assert( p->readOnly==0 );
 84631  85256     pDb = &db->aDb[pOp->p1];
 84632  85257     assert( pDb->pBt!=0 );
 84633  85258     assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
................................................................................
 85583  86208     VdbeFrame *pFrame;     /* Root frame of VDBE */
 85584  86209   
 85585  86210     v = 0;
 85586  86211     res = 0;
 85587  86212     pOut = out2Prerelease(p, pOp);
 85588  86213     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 85589  86214     pC = p->apCsr[pOp->p1];
 85590         -  if( !pC->isTable ){
 85591         -    rc = SQLITE_CORRUPT_BKPT;
 85592         -    goto abort_due_to_error;
 85593         -  }
 85594  86215     assert( pC!=0 );
        86216  +  assert( pC->isTable );
 85595  86217     assert( pC->eCurType==CURTYPE_BTREE );
 85596  86218     assert( pC->uc.pCursor!=0 );
 85597  86219     {
 85598  86220       /* The next rowid or record number (different terms for the same
 85599  86221       ** thing) is obtained in a two-step algorithm.
 85600  86222       **
 85601  86223       ** First we attempt to find the largest existing rowid and add one
................................................................................
 85756  86378     pC = p->apCsr[pOp->p1];
 85757  86379     assert( pC!=0 );
 85758  86380     assert( pC->eCurType==CURTYPE_BTREE );
 85759  86381     assert( pC->uc.pCursor!=0 );
 85760  86382     assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
 85761  86383     assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
 85762  86384     REGISTER_TRACE(pOp->p2, pData);
        86385  +  sqlite3VdbeIncrWriteCounter(p, pC);
 85763  86386   
 85764  86387     if( pOp->opcode==OP_Insert ){
 85765  86388       pKey = &aMem[pOp->p3];
 85766  86389       assert( pKey->flags & MEM_Int );
 85767  86390       assert( memIsValid(pKey) );
 85768  86391       REGISTER_TRACE(pOp->p3, pKey);
 85769  86392       x.nKey = pKey->u.i;
................................................................................
 85870  86493     opflags = pOp->p2;
 85871  86494     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 85872  86495     pC = p->apCsr[pOp->p1];
 85873  86496     assert( pC!=0 );
 85874  86497     assert( pC->eCurType==CURTYPE_BTREE );
 85875  86498     assert( pC->uc.pCursor!=0 );
 85876  86499     assert( pC->deferredMoveto==0 );
        86500  +  sqlite3VdbeIncrWriteCounter(p, pC);
 85877  86501   
 85878  86502   #ifdef SQLITE_DEBUG
 85879  86503     if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
 85880  86504       /* If p5 is zero, the seek operation that positioned the cursor prior to
 85881  86505       ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
 85882  86506       ** the row that is being deleted */
 85883  86507       i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
................................................................................
 86488  87112   case OP_SorterInsert:       /* in2 */
 86489  87113   case OP_IdxInsert: {        /* in2 */
 86490  87114     VdbeCursor *pC;
 86491  87115     BtreePayload x;
 86492  87116   
 86493  87117     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 86494  87118     pC = p->apCsr[pOp->p1];
        87119  +  sqlite3VdbeIncrWriteCounter(p, pC);
 86495  87120     assert( pC!=0 );
 86496  87121     assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
 86497  87122     pIn2 = &aMem[pOp->p2];
 86498  87123     assert( pIn2->flags & MEM_Blob );
 86499  87124     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 86500  87125     assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
 86501  87126     assert( pC->isTable==0 );
................................................................................
 86534  87159   
 86535  87160     assert( pOp->p3>0 );
 86536  87161     assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
 86537  87162     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 86538  87163     pC = p->apCsr[pOp->p1];
 86539  87164     assert( pC!=0 );
 86540  87165     assert( pC->eCurType==CURTYPE_BTREE );
        87166  +  sqlite3VdbeIncrWriteCounter(p, pC);
 86541  87167     pCrsr = pC->uc.pCursor;
 86542  87168     assert( pCrsr!=0 );
 86543  87169     assert( pOp->p5==0 );
 86544  87170     r.pKeyInfo = pC->pKeyInfo;
 86545  87171     r.nField = (u16)pOp->p3;
 86546  87172     r.default_rc = 0;
 86547  87173     r.aMem = &aMem[pOp->p2];
................................................................................
 86756  87382   **
 86757  87383   ** See also: Clear
 86758  87384   */
 86759  87385   case OP_Destroy: {     /* out2 */
 86760  87386     int iMoved;
 86761  87387     int iDb;
 86762  87388   
        87389  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86763  87390     assert( p->readOnly==0 );
 86764  87391     assert( pOp->p1>1 );
 86765  87392     pOut = out2Prerelease(p, pOp);
 86766  87393     pOut->flags = MEM_Null;
 86767  87394     if( db->nVdbeRead > db->nVDestroy+1 ){
 86768  87395       rc = SQLITE_LOCKED;
 86769  87396       p->errorAction = OE_Abort;
................................................................................
 86805  87432   ** also incremented by the number of rows in the table being cleared.
 86806  87433   **
 86807  87434   ** See also: Destroy
 86808  87435   */
 86809  87436   case OP_Clear: {
 86810  87437     int nChange;
 86811  87438    
        87439  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86812  87440     nChange = 0;
 86813  87441     assert( p->readOnly==0 );
 86814  87442     assert( DbMaskTest(p->btreeMask, pOp->p2) );
 86815  87443     rc = sqlite3BtreeClearTable(
 86816  87444         db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
 86817  87445     );
 86818  87446     if( pOp->p3 ){
................................................................................
 86861  87489   ** it must be 2 (BTREE_BLOBKEY) for a index or WITHOUT ROWID table.
 86862  87490   ** The root page number of the new b-tree is stored in register P2.
 86863  87491   */
 86864  87492   case OP_CreateBtree: {          /* out2 */
 86865  87493     int pgno;
 86866  87494     Db *pDb;
 86867  87495   
        87496  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86868  87497     pOut = out2Prerelease(p, pOp);
 86869  87498     pgno = 0;
 86870  87499     assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
 86871  87500     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 86872  87501     assert( DbMaskTest(p->btreeMask, pOp->p1) );
 86873  87502     assert( p->readOnly==0 );
 86874  87503     pDb = &db->aDb[pOp->p1];
................................................................................
 86880  87509   }
 86881  87510   
 86882  87511   /* Opcode: SqlExec * * * P4 *
 86883  87512   **
 86884  87513   ** Run the SQL statement or statements specified in the P4 string.
 86885  87514   */
 86886  87515   case OP_SqlExec: {
        87516  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86887  87517     db->nSqlExec++;
 86888  87518     rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
 86889  87519     db->nSqlExec--;
 86890  87520     if( rc ) goto abort_due_to_error;
 86891  87521     break;
 86892  87522   }
 86893  87523   
................................................................................
 86969  87599   ** Remove the internal (in-memory) data structures that describe
 86970  87600   ** the table named P4 in database P1.  This is called after a table
 86971  87601   ** is dropped from disk (using the Destroy opcode) in order to keep 
 86972  87602   ** the internal representation of the
 86973  87603   ** schema consistent with what is on disk.
 86974  87604   */
 86975  87605   case OP_DropTable: {
        87606  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86976  87607     sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
 86977  87608     break;
 86978  87609   }
 86979  87610   
 86980  87611   /* Opcode: DropIndex P1 * * P4 *
 86981  87612   **
 86982  87613   ** Remove the internal (in-memory) data structures that describe
 86983  87614   ** the index named P4 in database P1.  This is called after an index
 86984  87615   ** is dropped from disk (using the Destroy opcode)
 86985  87616   ** in order to keep the internal representation of the
 86986  87617   ** schema consistent with what is on disk.
 86987  87618   */
 86988  87619   case OP_DropIndex: {
        87620  +  sqlite3VdbeIncrWriteCounter(p, 0);
 86989  87621     sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
 86990  87622     break;
 86991  87623   }
 86992  87624   
 86993  87625   /* Opcode: DropTrigger P1 * * P4 *
 86994  87626   **
 86995  87627   ** Remove the internal (in-memory) data structures that describe
 86996  87628   ** the trigger named P4 in database P1.  This is called after a trigger
 86997  87629   ** is dropped from disk (using the Destroy opcode) in order to keep 
 86998  87630   ** the internal representation of the
 86999  87631   ** schema consistent with what is on disk.
 87000  87632   */
 87001  87633   case OP_DropTrigger: {
        87634  +  sqlite3VdbeIncrWriteCounter(p, 0);
 87002  87635     sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
 87003  87636     break;
 87004  87637   }
 87005  87638   
 87006  87639   
 87007  87640   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 87008  87641   /* Opcode: IntegrityCk P1 P2 P3 P4 P5
................................................................................
 88041  88674   **
 88042  88675   ** Store in register P3 the value of the P2-th column of
 88043  88676   ** the current row of the virtual-table of cursor P1.
 88044  88677   **
 88045  88678   ** If the VColumn opcode is being used to fetch the value of
 88046  88679   ** an unchanging column during an UPDATE operation, then the P5
 88047  88680   ** value is 1.  Otherwise, P5 is 0.  The P5 value is returned
 88048         -** by sqlite3_vtab_nochange() routine can can be used
        88681  +** by sqlite3_vtab_nochange() routine and can be used
 88049  88682   ** by virtual table implementations to return special "no-change"
 88050  88683   ** marks which can be more efficient, depending on the virtual table.
 88051  88684   */
 88052  88685   case OP_VColumn: {
 88053  88686     sqlite3_vtab *pVtab;
 88054  88687     const sqlite3_module *pModule;
 88055  88688     Mem *pDest;
................................................................................
 88204  88837     Mem **apArg;
 88205  88838     Mem *pX;
 88206  88839   
 88207  88840     assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback 
 88208  88841          || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
 88209  88842     );
 88210  88843     assert( p->readOnly==0 );
        88844  +  sqlite3VdbeIncrWriteCounter(p, 0);
 88211  88845     pVtab = pOp->p4.pVtab->pVtab;
 88212  88846     if( pVtab==0 || NEVER(pVtab->pModule==0) ){
 88213  88847       rc = SQLITE_LOCKED;
 88214  88848       goto abort_due_to_error;
 88215  88849     }
 88216  88850     pModule = pVtab->pModule;
 88217  88851     nArg = pOp->p2;
................................................................................
 88520  89154       assert( pC->eCurType==CURTYPE_BTREE );
 88521  89155       sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
 88522  89156                              pOp->p4.pExpr, aMem);
 88523  89157     }
 88524  89158     break;
 88525  89159   }
 88526  89160   #endif /* SQLITE_ENABLE_CURSOR_HINTS */
        89161  +
        89162  +#ifdef SQLITE_DEBUG
        89163  +/* Opcode:  Abortable   * * * * *
        89164  +**
        89165  +** Verify that an Abort can happen.  Assert if an Abort at this point
        89166  +** might cause database corruption.  This opcode only appears in debugging
        89167  +** builds.
        89168  +**
        89169  +** An Abort is safe if either there have been no writes, or if there is
        89170  +** an active statement journal.
        89171  +*/
        89172  +case OP_Abortable: {
        89173  +  sqlite3VdbeAssertAbortable(p);
        89174  +  break;
        89175  +}
        89176  +#endif
 88527  89177   
 88528  89178   /* Opcode: Noop * * * * *
 88529  89179   **
 88530  89180   ** Do nothing.  This instruction is often useful as a jump
 88531  89181   ** destination.
 88532  89182   */
 88533  89183   /*
 88534  89184   ** The magic Explain opcode are only inserted when explain==2 (which
 88535  89185   ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
 88536  89186   ** This opcode records information from the optimizer.  It is the
 88537  89187   ** the same as a no-op.  This opcodesnever appears in a real VM program.
 88538  89188   */
 88539         -default: {          /* This is really OP_Noop and OP_Explain */
        89189  +default: {          /* This is really OP_Noop, OP_Explain */
 88540  89190     assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
        89191  +
 88541  89192     break;
 88542  89193   }
 88543  89194   
 88544  89195   /*****************************************************************************
 88545  89196   ** The cases of the switch statement above this line should all be indented
 88546  89197   ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
 88547  89198   ** readability.  From this point on down, the normal indentation rules are
................................................................................
 92694  93345     int cntTab = 0;                   /* Number of matching table names */
 92695  93346     int nSubquery = 0;                /* How many levels of subquery */
 92696  93347     sqlite3 *db = pParse->db;         /* The database connection */
 92697  93348     struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
 92698  93349     struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
 92699  93350     NameContext *pTopNC = pNC;        /* First namecontext in the list */
 92700  93351     Schema *pSchema = 0;              /* Schema of the expression */
 92701         -  int isTrigger = 0;                /* True if resolved to a trigger column */
        93352  +  int eNewExprOp = TK_COLUMN;       /* New value for pExpr->op on success */
 92702  93353     Table *pTab = 0;                  /* Table hold the row */
 92703  93354     Column *pCol;                     /* A column of pTab */
 92704  93355   
 92705  93356     assert( pNC );     /* the name context cannot be NULL. */
 92706  93357     assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
 92707  93358     assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 92708  93359   
................................................................................
 92799  93450           if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
 92800  93451             ExprSetProperty(pExpr, EP_CanBeNull);
 92801  93452           }
 92802  93453           pSchema = pExpr->pTab->pSchema;
 92803  93454         }
 92804  93455       } /* if( pSrcList ) */
 92805  93456   
 92806         -#ifndef SQLITE_OMIT_TRIGGER
        93457  +#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
 92807  93458       /* If we have not already resolved the name, then maybe 
 92808         -    ** it is a new.* or old.* trigger argument reference
        93459  +    ** it is a new.* or old.* trigger argument reference.  Or
        93460  +    ** maybe it is an excluded.* from an upsert.
 92809  93461       */
 92810         -    if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
        93462  +    if( zDb==0 && zTab!=0 && cntTab==0 ){
        93463  +      pTab = 0;
        93464  +#ifndef SQLITE_OMIT_TRIGGER
        93465  +      if( pParse->pTriggerTab!=0 ){
 92811  93466         int op = pParse->eTriggerOp;
 92812  93467         assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
 92813  93468         if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
 92814  93469           pExpr->iTable = 1;
 92815  93470           pTab = pParse->pTriggerTab;
 92816  93471         }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
 92817  93472           pExpr->iTable = 0;
 92818  93473           pTab = pParse->pTriggerTab;
 92819         -      }else{
 92820         -        pTab = 0;
 92821  93474         }
        93475  +      }
        93476  +#endif /* SQLITE_OMIT_TRIGGER */
        93477  +#ifndef SQLITE_OMIT_UPSERT
        93478  +      if( (pNC->ncFlags & NC_UUpsert)!=0 ){
        93479  +        Upsert *pUpsert = pNC->uNC.pUpsert;
        93480  +        if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
        93481  +          pTab = pUpsert->pUpsertSrc->a[0].pTab;
        93482  +          pExpr->iTable = 2;
        93483  +        }
        93484  +      }
        93485  +#endif /* SQLITE_OMIT_UPSERT */
 92822  93486   
 92823  93487         if( pTab ){ 
 92824  93488           int iCol;
 92825  93489           pSchema = pTab->pSchema;
 92826  93490           cntTab++;
 92827  93491           for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
 92828  93492             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
................................................................................
 92834  93498           }
 92835  93499           if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
 92836  93500             /* IMP: R-51414-32910 */
 92837  93501             iCol = -1;
 92838  93502           }
 92839  93503           if( iCol<pTab->nCol ){
 92840  93504             cnt++;
        93505  +#ifndef SQLITE_OMIT_UPSERT
        93506  +          if( pExpr->iTable==2 ){
        93507  +            testcase( iCol==(-1) );
        93508  +            pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
        93509  +            eNewExprOp = TK_REGISTER;
        93510  +          }else
        93511  +#endif /* SQLITE_OMIT_UPSERT */
        93512  +          {
        93513  +#ifndef SQLITE_OMIT_TRIGGER
 92841  93514             if( iCol<0 ){
 92842  93515               pExpr->affinity = SQLITE_AFF_INTEGER;
 92843  93516             }else if( pExpr->iTable==0 ){
 92844  93517               testcase( iCol==31 );
 92845  93518               testcase( iCol==32 );
 92846  93519               pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 92847  93520             }else{
 92848  93521               testcase( iCol==31 );
 92849  93522               testcase( iCol==32 );
 92850  93523               pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 92851  93524             }
 92852         -          pExpr->iColumn = (i16)iCol;
 92853  93525             pExpr->pTab = pTab;
 92854         -          isTrigger = 1;
        93526  +            pExpr->iColumn = (i16)iCol;
        93527  +            eNewExprOp = TK_TRIGGER;
        93528  +#endif /* SQLITE_OMIT_TRIGGER */
 92855  93529           }
 92856  93530         }
 92857  93531       }
 92858         -#endif /* !defined(SQLITE_OMIT_TRIGGER) */
        93532  +    }
        93533  +#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
 92859  93534   
 92860  93535       /*
 92861  93536       ** Perhaps the name is a reference to the ROWID
 92862  93537       */
 92863  93538       if( cnt==0
 92864  93539        && cntTab==1
 92865  93540        && pMatch
................................................................................
 92886  93561       **
 92887  93562       ** The ability to use an output result-set column in the WHERE, GROUP BY,
 92888  93563       ** or HAVING clauses, or as part of a larger expression in the ORDER BY
 92889  93564       ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
 92890  93565       ** is supported for backwards compatibility only. Hence, we issue a warning
 92891  93566       ** on sqlite3_log() whenever the capability is used.
 92892  93567       */
 92893         -    if( (pEList = pNC->pEList)!=0
 92894         -     && zTab==0
        93568  +    if( (pNC->ncFlags & NC_UEList)!=0
 92895  93569        && cnt==0
        93570  +     && zTab==0
 92896  93571       ){
        93572  +      pEList = pNC->uNC.pEList;
        93573  +      assert( pEList!=0 );
 92897  93574         for(j=0; j<pEList->nExpr; j++){
 92898  93575           char *zAs = pEList->a[j].zName;
 92899  93576           if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
 92900  93577             Expr *pOrig;
 92901  93578             assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 92902  93579             assert( pExpr->x.pList==0 );
 92903  93580             assert( pExpr->x.pSelect==0 );
................................................................................
 92986  93663   
 92987  93664     /* Clean up and return
 92988  93665     */
 92989  93666     sqlite3ExprDelete(db, pExpr->pLeft);
 92990  93667     pExpr->pLeft = 0;
 92991  93668     sqlite3ExprDelete(db, pExpr->pRight);
 92992  93669     pExpr->pRight = 0;
 92993         -  pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
        93670  +  pExpr->op = eNewExprOp;
 92994  93671     ExprSetProperty(pExpr, EP_Leaf);
 92995  93672   lookupname_end:
 92996  93673     if( cnt==1 ){
 92997  93674       assert( pNC!=0 );
 92998  93675       if( !ExprHasProperty(pExpr, EP_Alias) ){
 92999  93676         sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
 93000  93677       }
................................................................................
 93418  94095     pEList = pSelect->pEList;
 93419  94096   
 93420  94097     /* Resolve all names in the ORDER BY term expression
 93421  94098     */
 93422  94099     memset(&nc, 0, sizeof(nc));
 93423  94100     nc.pParse = pParse;
 93424  94101     nc.pSrcList = pSelect->pSrc;
 93425         -  nc.pEList = pEList;
 93426         -  nc.ncFlags = NC_AllowAgg;
        94102  +  nc.uNC.pEList = pEList;
        94103  +  nc.ncFlags = NC_AllowAgg|NC_UEList;
 93427  94104     nc.nErr = 0;
 93428  94105     db = pParse->db;
 93429  94106     savedSuppErr = db->suppressErr;
 93430  94107     db->suppressErr = 1;
 93431  94108     rc = sqlite3ResolveExprNames(&nc, pE);
 93432  94109     db->suppressErr = savedSuppErr;
 93433  94110     if( rc ) return 0;
................................................................................
 93802  94479       ** other expressions in the SELECT statement. This is so that
 93803  94480       ** expressions in the WHERE clause (etc.) can refer to expressions by
 93804  94481       ** aliases in the result set.
 93805  94482       **
 93806  94483       ** Minor point: If this is the case, then the expression will be
 93807  94484       ** re-evaluated for each reference to it.
 93808  94485       */
 93809         -    sNC.pEList = p->pEList;
        94486  +    assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert))==0 );
        94487  +    sNC.uNC.pEList = p->pEList;
        94488  +    sNC.ncFlags |= NC_UEList;
 93810  94489       if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
 93811  94490       if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
 93812  94491   
 93813  94492       /* Resolve names in table-valued-function arguments */
 93814  94493       for(i=0; i<p->pSrc->nSrc; i++){
 93815  94494         struct SrcList_item *pItem = &p->pSrc->a[i];
 93816  94495         if( pItem->fg.isTabFunc
................................................................................
 94035  94714   ** Any errors cause an error message to be set in pParse.
 94036  94715   */
 94037  94716   SQLITE_PRIVATE void sqlite3ResolveSelfReference(
 94038  94717     Parse *pParse,      /* Parsing context */
 94039  94718     Table *pTab,        /* The table being referenced */
 94040  94719     int type,           /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */
 94041  94720     Expr *pExpr,        /* Expression to resolve.  May be NULL. */
 94042         -  ExprList *pList     /* Expression list to resolve.  May be NUL. */
        94721  +  ExprList *pList     /* Expression list to resolve.  May be NULL. */
 94043  94722   ){
 94044  94723     SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
 94045  94724     NameContext sNC;                /* Name context for pParse->pNewTable */
 94046  94725   
 94047  94726     assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr );
 94048  94727     memset(&sNC, 0, sizeof(sNC));
 94049  94728     memset(&sSrc, 0, sizeof(sSrc));
................................................................................
 95421  96100         }
 95422  96101       }
 95423  96102       pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 95424  96103       pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
 95425  96104       pItem->sortOrder = pOldItem->sortOrder;
 95426  96105       pItem->done = 0;
 95427  96106       pItem->bSpanIsTab = pOldItem->bSpanIsTab;
        96107  +    pItem->bSorterRef = pOldItem->bSorterRef;
 95428  96108       pItem->u = pOldItem->u;
 95429  96109     }
 95430  96110     return pNew;
 95431  96111   }
 95432  96112   
 95433  96113   /*
 95434  96114   ** If cursors, triggers, views and subqueries are all omitted from
................................................................................
 95883  96563         testcase( pExpr->op==TK_AGG_FUNCTION );
 95884  96564         testcase( pExpr->op==TK_AGG_COLUMN );
 95885  96565         if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
 95886  96566           return WRC_Continue;
 95887  96567         }
 95888  96568         /* Fall through */
 95889  96569       case TK_IF_NULL_ROW:
        96570  +    case TK_REGISTER:
        96571  +      testcase( pExpr->op==TK_REGISTER );
 95890  96572         testcase( pExpr->op==TK_IF_NULL_ROW );
 95891  96573         pWalker->eCode = 0;
 95892  96574         return WRC_Abort;
 95893  96575       case TK_VARIABLE:
 95894  96576         if( pWalker->eCode==5 ){
 95895  96577           /* Silently convert bound parameters that appear inside of CREATE
 95896  96578           ** statements into a NULL when parsing the CREATE statement text out
................................................................................
 95900  96582           /* A bound parameter in a CREATE statement that originates from
 95901  96583           ** sqlite3_prepare() causes an error */
 95902  96584           pWalker->eCode = 0;
 95903  96585           return WRC_Abort;
 95904  96586         }
 95905  96587         /* Fall through */
 95906  96588       default:
 95907         -      testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail will disallow */
 95908         -      testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail will disallow */
        96589  +      testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
        96590  +      testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
 95909  96591         return WRC_Continue;
 95910  96592     }
 95911  96593   }
 95912  96594   static int exprIsConst(Expr *p, int initFlag, int iCur){
 95913  96595     Walker w;
 95914  96596     w.eCode = initFlag;
 95915  96597     w.xExprCallback = exprNodeIsConstant;
................................................................................
 96465  97147               if( aiMap ) aiMap[i] = j;
 96466  97148             }
 96467  97149     
 96468  97150             assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
 96469  97151             if( colUsed==(MASKBIT(nExpr)-1) ){
 96470  97152               /* If we reach this point, that means the index pIdx is usable */
 96471  97153               int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
 96472         -#ifndef SQLITE_OMIT_EXPLAIN
 96473         -            sqlite3VdbeAddOp4(v, OP_Explain, 0, 0, 0,
 96474         -              sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName),
 96475         -              P4_DYNAMIC);
 96476         -#endif
        97154  +            ExplainQueryPlan((pParse, 0,
        97155  +                              "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
 96477  97156               sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
 96478  97157               sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 96479  97158               VdbeComment((v, "%s", pIdx->zName));
 96480  97159               assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
 96481  97160               eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
 96482  97161     
 96483  97162               if( prRhsHasNull ){
................................................................................
 96664  97343     ** If all of the above are false, then we can run this code just once
 96665  97344     ** save the results, and reuse the same result on subsequent invocations.
 96666  97345     */
 96667  97346     if( !ExprHasProperty(pExpr, EP_VarSelect) ){
 96668  97347       jmpIfDynamic = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
 96669  97348     }
 96670  97349   
 96671         -#ifndef SQLITE_OMIT_EXPLAIN
 96672         -  if( pParse->explain==2 ){
 96673         -    char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
 96674         -        jmpIfDynamic>=0?"":"CORRELATED ",
 96675         -        pExpr->op==TK_IN?"LIST":"SCALAR",
 96676         -        pParse->iNextSelectId
 96677         -    );
 96678         -    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 96679         -  }
 96680         -#endif
 96681         -
 96682  97350     switch( pExpr->op ){
 96683  97351       case TK_IN: {
 96684  97352         int addr;                   /* Address of OP_OpenEphemeral instruction */
 96685  97353         Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
 96686  97354         KeyInfo *pKeyInfo = 0;      /* Key information */
 96687  97355         int nVal;                   /* Size of vector pLeft */
 96688  97356         
................................................................................
 96712  97380           **
 96713  97381           ** Generate code to write the results of the select into the temporary
 96714  97382           ** table allocated and opened above.
 96715  97383           */
 96716  97384           Select *pSelect = pExpr->x.pSelect;
 96717  97385           ExprList *pEList = pSelect->pEList;
 96718  97386   
        97387  +        ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY",
        97388  +            jmpIfDynamic>=0?"":"CORRELATED "
        97389  +        ));
 96719  97390           assert( !isRowid );
 96720  97391           /* If the LHS and RHS of the IN operator do not match, that
 96721  97392           ** error will have been caught long before we reach this point. */
 96722  97393           if( ALWAYS(pEList->nExpr==nVal) ){
 96723  97394             SelectDest dest;
 96724  97395             int i;
 96725  97396             sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
................................................................................
 96753  97424           ** a column, use numeric affinity.
 96754  97425           */
 96755  97426           char affinity;            /* Affinity of the LHS of the IN */
 96756  97427           int i;
 96757  97428           ExprList *pList = pExpr->x.pList;
 96758  97429           struct ExprList_item *pItem;
 96759  97430           int r1, r2, r3;
 96760         -
 96761  97431           affinity = sqlite3ExprAffinity(pLeft);
 96762  97432           if( !affinity ){
 96763  97433             affinity = SQLITE_AFF_BLOB;
 96764  97434           }
 96765  97435           if( pKeyInfo ){
 96766  97436             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 96767  97437             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
................................................................................
 96834  97504   
 96835  97505         testcase( pExpr->op==TK_EXISTS );
 96836  97506         testcase( pExpr->op==TK_SELECT );
 96837  97507         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
 96838  97508         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 96839  97509   
 96840  97510         pSel = pExpr->x.pSelect;
        97511  +      ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY",
        97512  +            jmpIfDynamic>=0?"":"CORRELATED "));
 96841  97513         nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
 96842  97514         sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
 96843  97515         pParse->nMem += nReg;
 96844  97516         if( pExpr->op==TK_SELECT ){
 96845  97517           dest.eDest = SRT_Mem;
 96846  97518           dest.iSdst = dest.iSDParm;
 96847  97519           dest.nSdst = nReg;
................................................................................
 97596  98268   
 97597  98269     assert( target>0 && target<=pParse->nMem );
 97598  98270     if( v==0 ){
 97599  98271       assert( pParse->db->mallocFailed );
 97600  98272       return 0;
 97601  98273     }
 97602  98274   
        98275  +expr_code_doover:
 97603  98276     if( pExpr==0 ){
 97604  98277       op = TK_NULL;
 97605  98278     }else{
 97606  98279       op = pExpr->op;
 97607  98280     }
 97608  98281     switch( op ){
 97609  98282       case TK_AGG_COLUMN: {
................................................................................
 98056  98729       case TK_BETWEEN: {
 98057  98730         exprCodeBetween(pParse, pExpr, target, 0, 0);
 98058  98731         return target;
 98059  98732       }
 98060  98733       case TK_SPAN:
 98061  98734       case TK_COLLATE: 
 98062  98735       case TK_UPLUS: {
 98063         -      return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
        98736  +      pExpr = pExpr->pLeft;
        98737  +      goto expr_code_doover;
 98064  98738       }
 98065  98739   
 98066  98740       case TK_TRIGGER: {
 98067  98741         /* If the opcode is TK_TRIGGER, then the expression is a reference
 98068  98742         ** to a column in the new.* or old.* pseudo-tables available to
 98069  98743         ** trigger programs. In this case Expr.iTable is set to 1 for the
 98070  98744         ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
................................................................................
 98094  98768   
 98095  98769         assert( pExpr->iTable==0 || pExpr->iTable==1 );
 98096  98770         assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
 98097  98771         assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
 98098  98772         assert( p1>=0 && p1<(pTab->nCol*2+2) );
 98099  98773   
 98100  98774         sqlite3VdbeAddOp2(v, OP_Param, p1, target);
 98101         -      VdbeComment((v, "%s.%s -> $%d",
        98775  +      VdbeComment((v, "r[%d]=%s.%s", target,
 98102  98776           (pExpr->iTable ? "new" : "old"),
 98103         -        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
 98104         -        target
        98777  +        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName)
 98105  98778         ));
 98106  98779   
 98107  98780   #ifndef SQLITE_OMIT_FLOATING_POINT
 98108  98781         /* If the column has REAL affinity, it may currently be stored as an
 98109  98782         ** integer. Use OP_RealAffinity to make sure it is really real.
 98110  98783         **
 98111  98784         ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
................................................................................
 98429  99102     assert( pList!=0 );
 98430  99103     assert( target>0 );
 98431  99104     assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
 98432  99105     n = pList->nExpr;
 98433  99106     if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
 98434  99107     for(pItem=pList->a, i=0; i<n; i++, pItem++){
 98435  99108       Expr *pExpr = pItem->pExpr;
        99109  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
        99110  +    if( pItem->bSorterRef ){
        99111  +      i--;
        99112  +      n--;
        99113  +    }else
        99114  +#endif
 98436  99115       if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
 98437  99116         if( flags & SQLITE_ECEL_OMITREF ){
 98438  99117           i--;
 98439  99118           n--;
 98440  99119         }else{
 98441  99120           sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
 98442  99121         }
................................................................................
 98957  99636         return 1;
 98958  99637       }
 98959  99638       return 2;
 98960  99639     }
 98961  99640     if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
 98962  99641       if( pA->op==TK_FUNCTION ){
 98963  99642         if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
        99643  +    }else if( pA->op==TK_COLLATE ){
        99644  +      if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
 98964  99645       }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
 98965         -      return pA->op==TK_COLLATE ? 1 : 2;
        99646  +      return 2;
 98966  99647       }
 98967  99648     }
 98968  99649     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
 98969  99650     if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
 98970  99651       if( combinedFlags & EP_xIsSelect ) return 2;
 98971  99652       if( sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
 98972  99653       if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
 98973  99654       if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
 98974         -    if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
        99655  +    assert( (combinedFlags & EP_Reduced)==0 );
        99656  +    if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){
 98975  99657         if( pA->iColumn!=pB->iColumn ) return 2;
 98976  99658         if( pA->iTable!=pB->iTable 
 98977  99659          && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
 98978  99660       }
 98979  99661     }
 98980  99662     return 0;
 98981  99663   }
................................................................................
 99313  99995   ** for additional information.
 99314  99996   */
 99315  99997   static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
 99316  99998     int i;
 99317  99999     NameContext *pNC = pWalker->u.pNC;
 99318 100000     Parse *pParse = pNC->pParse;
 99319 100001     SrcList *pSrcList = pNC->pSrcList;
 99320         -  AggInfo *pAggInfo = pNC->pAggInfo;
       100002  +  AggInfo *pAggInfo = pNC->uNC.pAggInfo;
 99321 100003   
       100004  +  assert( pNC->ncFlags & NC_UAggInfo );
 99322 100005     switch( pExpr->op ){
 99323 100006       case TK_AGG_COLUMN:
 99324 100007       case TK_COLUMN: {
 99325 100008         testcase( pExpr->op==TK_AGG_COLUMN );
 99326 100009         testcase( pExpr->op==TK_COLUMN );
 99327 100010         /* Check to see if the column is in one of the tables in the FROM
 99328 100011         ** clause of the aggregate query */
................................................................................
102484 103167       }
102485 103168       assert( pVfs );
102486 103169       flags |= SQLITE_OPEN_MAIN_DB;
102487 103170       rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
102488 103171       sqlite3_free( zPath );
102489 103172       db->nDb++;
102490 103173     }
102491         -  db->skipBtreeMutex = 0;
       103174  +  db->noSharedCache = 0;
102492 103175     if( rc==SQLITE_CONSTRAINT ){
102493 103176       rc = SQLITE_ERROR;
102494 103177       zErrDyn = sqlite3MPrintf(db, "database is already attached");
102495 103178     }else if( rc==SQLITE_OK ){
102496 103179       Pager *pPager;
102497 103180       pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
102498 103181       if( !pNew->pSchema ){
................................................................................
102556 103239     ** If this fails, or if opening the file failed, then close the file and 
102557 103240     ** remove the entry from the db->aDb[] array. i.e. put everything back the
102558 103241     ** way we found it.
102559 103242     */
102560 103243     if( rc==SQLITE_OK ){
102561 103244       sqlite3BtreeEnterAll(db);
102562 103245       db->init.iDb = 0;
       103246  +    db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
102563 103247       rc = sqlite3Init(db, &zErrDyn);
102564 103248       sqlite3BtreeLeaveAll(db);
102565 103249       assert( zErrDyn==0 || rc!=SQLITE_OK );
102566 103250     }
102567 103251   #ifdef SQLITE_USER_AUTHENTICATION
102568 103252     if( rc==SQLITE_OK ){
102569 103253       u8 newAuth = 0;
................................................................................
102828 103512         pItem->zDatabase = 0;
102829 103513         pItem->pSchema = pFix->pSchema;
102830 103514       }
102831 103515   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
102832 103516       if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
102833 103517       if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
102834 103518   #endif
       103519  +    if( pItem->fg.isTabFunc && sqlite3FixExprList(pFix, pItem->u1.pFuncArg) ){
       103520  +      return 1;
       103521  +    }
102835 103522     }
102836 103523     return 0;
102837 103524   }
102838 103525   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
102839 103526   SQLITE_PRIVATE int sqlite3FixSelect(
102840 103527     DbFixer *pFix,       /* Context of the fixation */
102841 103528     Select *pSelect      /* The SELECT statement to be fixed to one database */
................................................................................
102927 103614       }
102928 103615       if( sqlite3FixExpr(pFix, pStep->pWhere) ){
102929 103616         return 1;
102930 103617       }
102931 103618       if( sqlite3FixExprList(pFix, pStep->pExprList) ){
102932 103619         return 1;
102933 103620       }
       103621  +#ifndef SQLITE_OMIT_UPSERT
       103622  +    if( pStep->pUpsert ){
       103623  +      Upsert *pUp = pStep->pUpsert;
       103624  +      if( sqlite3FixExprList(pFix, pUp->pUpsertTarget)
       103625  +       || sqlite3FixExpr(pFix, pUp->pUpsertTargetWhere)
       103626  +       || sqlite3FixExprList(pFix, pUp->pUpsertSet)
       103627  +       || sqlite3FixExpr(pFix, pUp->pUpsertWhere)
       103628  +      ){
       103629  +        return 1;
       103630  +      }
       103631  +    }
       103632  +#endif
102934 103633       pStep = pStep->pNext;
102935 103634     }
102936 103635     return 0;
102937 103636   }
102938 103637   #endif
102939 103638   
102940 103639   /************** End of attach.c **********************************************/
................................................................................
103554 104253   SQLITE_PRIVATE Table *sqlite3LocateTable(
103555 104254     Parse *pParse,         /* context in which to report errors */
103556 104255     u32 flags,             /* LOCATE_VIEW or LOCATE_NOERR */
103557 104256     const char *zName,     /* Name of the table we are looking for */
103558 104257     const char *zDbase     /* Name of the database.  Might be NULL */
103559 104258   ){
103560 104259     Table *p;
       104260  +  sqlite3 *db = pParse->db;
103561 104261   
103562 104262     /* Read the database schema. If an error occurs, leave an error message
103563 104263     ** and code in pParse and return NULL. */
103564         -  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
       104264  +  if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 
       104265  +   && SQLITE_OK!=sqlite3ReadSchema(pParse)
       104266  +  ){
103565 104267       return 0;
103566 104268     }
103567 104269   
103568         -  p = sqlite3FindTable(pParse->db, zName, zDbase);
       104270  +  p = sqlite3FindTable(db, zName, zDbase);
103569 104271     if( p==0 ){
103570 104272       const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
103571 104273   #ifndef SQLITE_OMIT_VIRTUALTABLE
103572         -    if( sqlite3FindDbName(pParse->db, zDbase)<1 ){
       104274  +    if( sqlite3FindDbName(db, zDbase)<1 ){
103573 104275         /* If zName is the not the name of a table in the schema created using
103574 104276         ** CREATE, then check to see if it is the name of an virtual table that
103575 104277         ** can be an eponymous virtual table. */
103576         -      Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName);
       104278  +      Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
103577 104279         if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
103578         -        pMod = sqlite3PragmaVtabRegister(pParse->db, zName);
       104280  +        pMod = sqlite3PragmaVtabRegister(db, zName);
103579 104281         }
103580 104282         if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
103581 104283           return pMod->pEpoTab;
103582 104284         }
103583 104285       }
103584 104286   #endif
103585 104287       if( (flags & LOCATE_NOERR)==0 ){
................................................................................
103736 104438     int i;
103737 104439     assert( iDb<db->nDb );
103738 104440   
103739 104441     if( iDb>=0 ){
103740 104442       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
103741 104443       DbSetProperty(db, iDb, DB_ResetWanted);
103742 104444       DbSetProperty(db, 1, DB_ResetWanted);
       104445  +    db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
103743 104446     }
103744 104447   
103745 104448     if( db->nSchemaLock==0 ){
103746 104449       for(i=0; i<db->nDb; i++){
103747 104450         if( DbHasProperty(db, i, DB_ResetWanted) ){
103748 104451           sqlite3SchemaClear(db->aDb[i].pSchema);
103749 104452         }
................................................................................
103761 104464     assert( db->nSchemaLock==0 );
103762 104465     for(i=0; i<db->nDb; i++){
103763 104466       Db *pDb = &db->aDb[i];
103764 104467       if( pDb->pSchema ){
103765 104468         sqlite3SchemaClear(pDb->pSchema);
103766 104469       }
103767 104470     }
103768         -  db->mDbFlags &= ~DBFLAG_SchemaChange;
       104471  +  db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
103769 104472     sqlite3VtabUnlockList(db);
103770 104473     sqlite3BtreeLeaveAll(db);
103771 104474     sqlite3CollapseDatabaseArray(db);
103772 104475   }
103773 104476   
103774 104477   /*
103775 104478   ** This routine is called when a commit occurs.
................................................................................
104306 105009     pCol = &p->aCol[p->nCol];
104307 105010     memset(pCol, 0, sizeof(p->aCol[0]));
104308 105011     pCol->zName = z;
104309 105012     sqlite3ColumnPropertiesFromName(p, pCol);
104310 105013    
104311 105014     if( pType->n==0 ){
104312 105015       /* If there is no type specified, columns have the default affinity
104313         -    ** 'BLOB'. */
       105016  +    ** 'BLOB' with a default size of 4 bytes. */
104314 105017       pCol->affinity = SQLITE_AFF_BLOB;
104315 105018       pCol->szEst = 1;
       105019  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       105020  +    if( 4>=sqlite3GlobalConfig.szSorterRef ){
       105021  +      pCol->colFlags |= COLFLAG_SORTERREF;
       105022  +    }
       105023  +#endif
104316 105024     }else{
104317 105025       zType = z + sqlite3Strlen30(z) + 1;
104318 105026       memcpy(zType, pType->z, pType->n);
104319 105027       zType[pType->n] = 0;
104320 105028       sqlite3Dequote(zType);
104321         -    pCol->affinity = sqlite3AffinityType(zType, &pCol->szEst);
       105029  +    pCol->affinity = sqlite3AffinityType(zType, pCol);
104322 105030       pCol->colFlags |= COLFLAG_HASTYPE;
104323 105031     }
104324 105032     p->nCol++;
104325 105033     pParse->constraintName.n = 0;
104326 105034   }
104327 105035   
104328 105036   /*
................................................................................
104374 105082   ** 'REAL'        | SQLITE_AFF_REAL
104375 105083   ** 'FLOA'        | SQLITE_AFF_REAL
104376 105084   ** 'DOUB'        | SQLITE_AFF_REAL
104377 105085   **
104378 105086   ** If none of the substrings in the above table are found,
104379 105087   ** SQLITE_AFF_NUMERIC is returned.
104380 105088   */
104381         -SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
       105089  +SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, Column *pCol){
104382 105090     u32 h = 0;
104383 105091     char aff = SQLITE_AFF_NUMERIC;
104384 105092     const char *zChar = 0;
104385 105093   
104386 105094     assert( zIn!=0 );
104387 105095     while( zIn[0] ){
104388 105096       h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
................................................................................
104411 105119   #endif
104412 105120       }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
104413 105121         aff = SQLITE_AFF_INTEGER;
104414 105122         break;
104415 105123       }
104416 105124     }
104417 105125   
104418         -  /* If pszEst is not NULL, store an estimate of the field size.  The
       105126  +  /* If pCol is not NULL, store an estimate of the field size.  The
104419 105127     ** estimate is scaled so that the size of an integer is 1.  */
104420         -  if( pszEst ){
104421         -    *pszEst = 1;   /* default size is approx 4 bytes */
       105128  +  if( pCol ){
       105129  +    int v = 0;   /* default size is approx 4 bytes */
104422 105130       if( aff<SQLITE_AFF_NUMERIC ){
104423 105131         if( zChar ){
104424 105132           while( zChar[0] ){
104425 105133             if( sqlite3Isdigit(zChar[0]) ){
104426         -            int v = 0;
       105134  +            /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
104427 105135               sqlite3GetInt32(zChar, &v);
104428         -            v = v/4 + 1;
104429         -            if( v>255 ) v = 255;
104430         -            *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
104431 105136               break;
104432 105137             }
104433 105138             zChar++;
104434 105139           }
104435 105140         }else{
104436         -        *pszEst = 5;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
       105141  +        v = 16;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
104437 105142         }
104438 105143       }
       105144  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       105145  +    if( v>=sqlite3GlobalConfig.szSorterRef ){
       105146  +      pCol->colFlags |= COLFLAG_SORTERREF;
       105147  +    }
       105148  +#endif
       105149  +    v = v/4 + 1;
       105150  +    if( v>255 ) v = 255;
       105151  +    pCol->szEst = v;
104439 105152     }
104440 105153     return aff;
104441 105154   }
104442 105155   
104443 105156   /*
104444 105157   ** The expression is the default value for the most recently added column
104445 105158   ** of the table currently under construction.
................................................................................
106040 106753                       sqlite3KeyInfoRef(pKey), P4_KEYINFO);
106041 106754   
106042 106755     /* Open the table. Loop through all rows of the table, inserting index
106043 106756     ** records into the sorter. */
106044 106757     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
106045 106758     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
106046 106759     regRecord = sqlite3GetTempReg(pParse);
       106760  +  sqlite3MultiWrite(pParse);
106047 106761   
106048 106762     sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
106049 106763     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
106050 106764     sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
106051 106765     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
106052 106766     sqlite3VdbeJumpHere(v, addr1);
106053 106767     if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
106054 106768     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
106055 106769                       (char *)pKey, P4_KEYINFO);
106056 106770     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
106057 106771   
106058 106772     addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
106059 106773     if( IsUniqueIndex(pIndex) ){
106060         -    int j2 = sqlite3VdbeCurrentAddr(v) + 3;
106061         -    sqlite3VdbeGoto(v, j2);
       106774  +    int j2 = sqlite3VdbeGoto(v, 1);
106062 106775       addr2 = sqlite3VdbeCurrentAddr(v);
       106776  +    sqlite3VdbeVerifyAbortable(v, OE_Abort);
106063 106777       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
106064 106778                            pIndex->nKeyCol); VdbeCoverage(v);
106065 106779       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
       106780  +    sqlite3VdbeJumpHere(v, j2);
106066 106781     }else{
106067 106782       addr2 = sqlite3VdbeCurrentAddr(v);
106068 106783     }
106069 106784     sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
106070 106785     sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
106071 106786     sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
106072 106787     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
................................................................................
106221 106936     assert( pTab!=0 );
106222 106937     assert( pParse->nErr==0 );
106223 106938     if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
106224 106939          && db->init.busy==0
106225 106940   #if SQLITE_USER_AUTHENTICATION
106226 106941          && sqlite3UserAuthTable(pTab->zName)==0
106227 106942   #endif
106228         -       && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
       106943  +#ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
       106944  +       && sqlite3StrICmp(&pTab->zName[7],"master")!=0
       106945  +#endif
       106946  +       && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
       106947  + ){
106229 106948       sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
106230 106949       goto exit_create_index;
106231 106950     }
106232 106951   #ifndef SQLITE_OMIT_VIEW
106233 106952     if( pTab->pSelect ){
106234 106953       sqlite3ErrorMsg(pParse, "views may not be indexed");
106235 106954       goto exit_create_index;
................................................................................
107400 108119     char *zErr;
107401 108120     int j;
107402 108121     StrAccum errMsg;
107403 108122     Table *pTab = pIdx->pTable;
107404 108123   
107405 108124     sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
107406 108125     if( pIdx->aColExpr ){
107407         -    sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName);
       108126  +    sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
107408 108127     }else{
107409 108128       for(j=0; j<pIdx->nKeyCol; j++){
107410 108129         char *zCol;
107411 108130         assert( pIdx->aiColumn[j]>=0 );
107412 108131         zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
107413         -      if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
107414         -      sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
107415         -      sqlite3StrAccumAppend(&errMsg, ".", 1);
107416         -      sqlite3StrAccumAppendAll(&errMsg, zCol);
       108132  +      if( j ) sqlite3_str_append(&errMsg, ", ", 2);
       108133  +      sqlite3_str_appendall(&errMsg, pTab->zName);
       108134  +      sqlite3_str_append(&errMsg, ".", 1);
       108135  +      sqlite3_str_appendall(&errMsg, zCol);
107417 108136       }
107418 108137     }
107419 108138     zErr = sqlite3StrAccumFinish(&errMsg);
107420 108139     sqlite3HaltConstraint(pParse, 
107421 108140       IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY 
107422 108141                               : SQLITE_CONSTRAINT_UNIQUE,
107423 108142       onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
................................................................................
108095 108814     /* If the createFlag parameter is true and the search did not reveal an
108096 108815     ** exact match for the name, number of arguments and encoding, then add a
108097 108816     ** new entry to the hash table and return it.
108098 108817     */
108099 108818     if( createFlag && bestScore<FUNC_PERFECT_MATCH && 
108100 108819         (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
108101 108820       FuncDef *pOther;
       108821  +    u8 *z;
108102 108822       pBest->zName = (const char*)&pBest[1];
108103 108823       pBest->nArg = (u16)nArg;
108104 108824       pBest->funcFlags = enc;
108105 108825       memcpy((char*)&pBest[1], zName, nName+1);
       108826  +    for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
108106 108827       pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
108107 108828       if( pOther==pBest ){
108108 108829         sqlite3DbFree(db, pBest);
108109 108830         sqlite3OomFault(db);
108110 108831         return 0;
108111 108832       }else{
108112 108833         pBest->pNext = pOther;
................................................................................
108416 109137     int iDataCur = 0;      /* VDBE cursor for the canonical data source */
108417 109138     int iIdxCur = 0;       /* Cursor number of the first index */
108418 109139     int nIdx;              /* Number of indices */
108419 109140     sqlite3 *db;           /* Main database structure */
108420 109141     AuthContext sContext;  /* Authorization context */
108421 109142     NameContext sNC;       /* Name context to resolve expressions in */
108422 109143     int iDb;               /* Database number */
108423         -  int memCnt = -1;       /* Memory cell used for change counting */
       109144  +  int memCnt = 0;        /* Memory cell used for change counting */
108424 109145     int rcauth;            /* Value returned by authorization callback */
108425 109146     int eOnePass;          /* ONEPASS_OFF or _SINGLE or _MULTI */
108426 109147     int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
108427 109148     u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
108428 109149     Index *pPk;            /* The PRIMARY KEY index on the table */
108429 109150     int iPk = 0;           /* First of nPk registers holding PRIMARY KEY value */
108430 109151     i16 nPk = 1;           /* Number of columns in the PRIMARY KEY */
................................................................................
108521 109242     /* Begin generating code.
108522 109243     */
108523 109244     v = sqlite3GetVdbe(pParse);
108524 109245     if( v==0 ){
108525 109246       goto delete_from_cleanup;
108526 109247     }
108527 109248     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
108528         -  sqlite3BeginWriteOperation(pParse, 1, iDb);
       109249  +  sqlite3BeginWriteOperation(pParse, bComplex, iDb);
108529 109250   
108530 109251     /* If we are trying to delete from a view, realize that view into
108531 109252     ** an ephemeral table.
108532 109253     */
108533 109254   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
108534 109255     if( isView ){
108535 109256       sqlite3MaterializeView(pParse, pTab, 
................................................................................
108549 109270     if( sqlite3ResolveExprNames(&sNC, pWhere) ){
108550 109271       goto delete_from_cleanup;
108551 109272     }
108552 109273   
108553 109274     /* Initialize the counter of the number of rows deleted, if
108554 109275     ** we are counting rows.
108555 109276     */
108556         -  if( db->flags & SQLITE_CountRows ){
       109277  +  if( (db->flags & SQLITE_CountRows)!=0
       109278  +   && !pParse->nested
       109279  +   && !pParse->pTriggerTab
       109280  +  ){
108557 109281       memCnt = ++pParse->nMem;
108558 109282       sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
108559 109283     }
108560 109284   
108561 109285   #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
108562 109286     /* Special case: A DELETE without a WHERE clause deletes everything.
108563 109287     ** It is easier just to erase the whole table. Prior to version 3.6.5,
................................................................................
108577 109301   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108578 109302      && db->xPreUpdateCallback==0
108579 109303   #endif
108580 109304     ){
108581 109305       assert( !isView );
108582 109306       sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
108583 109307       if( HasRowid(pTab) ){
108584         -      sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
       109308  +      sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
108585 109309                           pTab->zName, P4_STATIC);
108586 109310       }
108587 109311       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108588 109312         assert( pIdx->pSchema==pTab->pSchema );
108589 109313         sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
108590 109314       }
108591 109315     }else
................................................................................
108622 109346       **  ONEPASS_MULTI:  One-pass approach - any number of rows may be deleted.
108623 109347       */
108624 109348       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
108625 109349       if( pWInfo==0 ) goto delete_from_cleanup;
108626 109350       eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
108627 109351       assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
108628 109352       assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
       109353  +    if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
108629 109354     
108630 109355       /* Keep track of the number of rows to be deleted */
108631         -    if( db->flags & SQLITE_CountRows ){
       109356  +    if( memCnt ){
108632 109357         sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
108633 109358       }
108634 109359     
108635 109360       /* Extract the rowid or primary key for the current row */
108636 109361       if( pPk ){
108637 109362         for(i=0; i<nPk; i++){
108638 109363           assert( pPk->aiColumn[i]>=0 );
................................................................................
108727 109452       }  
108728 109453     
108729 109454       /* Delete the row */
108730 109455   #ifndef SQLITE_OMIT_VIRTUALTABLE
108731 109456       if( IsVirtual(pTab) ){
108732 109457         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
108733 109458         sqlite3VtabMakeWritable(pParse, pTab);
108734         -      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
108735         -      sqlite3VdbeChangeP5(v, OE_Abort);
108736 109459         assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
108737 109460         sqlite3MayAbort(pParse);
108738         -      if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){
       109461  +      if( eOnePass==ONEPASS_SINGLE ){
       109462  +        sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
       109463  +        if( sqlite3IsToplevel(pParse) ){
108739 109464           pParse->isMultiWrite = 0;
108740 109465         }
       109466  +      }
       109467  +      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
       109468  +      sqlite3VdbeChangeP5(v, OE_Abort);
108741 109469       }else
108742 109470   #endif
108743 109471       {
108744 109472         int count = (pParse->nested==0);    /* True to count changes */
108745 109473         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
108746 109474             iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
108747 109475       }
................................................................................
108767 109495       sqlite3AutoincrementEnd(pParse);
108768 109496     }
108769 109497   
108770 109498     /* Return the number of rows that were deleted. If this routine is 
108771 109499     ** generating code because of a call to sqlite3NestedParse(), do not
108772 109500     ** invoke the callback function.
108773 109501     */
108774         -  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
       109502  +  if( memCnt ){
108775 109503       sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
108776 109504       sqlite3VdbeSetNumCols(v, 1);
108777 109505       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
108778 109506     }
108779 109507   
108780 109508   delete_from_cleanup:
108781 109509     sqlite3AuthContextPop(&sContext);
................................................................................
109375 110103   
109376 110104     if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
109377 110105       x.nArg = argc-1;
109378 110106       x.nUsed = 0;
109379 110107       x.apArg = argv+1;
109380 110108       sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
109381 110109       str.printfFlags = SQLITE_PRINTF_SQLFUNC;
109382         -    sqlite3XPrintf(&str, zFormat, &x);
       110110  +    sqlite3_str_appendf(&str, zFormat, &x);
109383 110111       n = str.nChar;
109384 110112       sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
109385 110113                           SQLITE_DYNAMIC);
109386 110114     }
109387 110115   }
109388 110116   
109389 110117   /*
................................................................................
110778 111506         if( argc==2 ){
110779 111507           zSep = (char*)sqlite3_value_text(argv[1]);
110780 111508           nSep = sqlite3_value_bytes(argv[1]);
110781 111509         }else{
110782 111510           zSep = ",";
110783 111511           nSep = 1;
110784 111512         }
110785         -      if( zSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
       111513  +      if( zSep ) sqlite3_str_append(pAccum, zSep, nSep);
110786 111514       }
110787 111515       zVal = (char*)sqlite3_value_text(argv[0]);
110788 111516       nVal = sqlite3_value_bytes(argv[0]);
110789         -    if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
       111517  +    if( zVal ) sqlite3_str_append(pAccum, zVal, nVal);
110790 111518     }
110791 111519   }
110792 111520   static void groupConcatFinalize(sqlite3_context *context){
110793 111521     StrAccum *pAccum;
110794 111522     pAccum = sqlite3_aggregate_context(context, 0);
110795 111523     if( pAccum ){
110796         -    if( pAccum->accError==STRACCUM_TOOBIG ){
       111524  +    if( pAccum->accError==SQLITE_TOOBIG ){
110797 111525         sqlite3_result_error_toobig(context);
110798         -    }else if( pAccum->accError==STRACCUM_NOMEM ){
       111526  +    }else if( pAccum->accError==SQLITE_NOMEM ){
110799 111527         sqlite3_result_error_nomem(context);
110800 111528       }else{    
110801 111529         sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 
110802 111530                             sqlite3_free);
110803 111531       }
110804 111532     }
110805 111533   }
................................................................................
111368 112096     int isIgnore          /* If true, pretend pTab contains all NULL values */
111369 112097   ){
111370 112098     int i;                                    /* Iterator variable */
111371 112099     Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
111372 112100     int iCur = pParse->nTab - 1;              /* Cursor number to use */
111373 112101     int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
111374 112102   
       112103  +  sqlite3VdbeVerifyAbortable(v,
       112104  +    (!pFKey->isDeferred
       112105  +      && !(pParse->db->flags & SQLITE_DeferFKs)
       112106  +      && !pParse->pToplevel 
       112107  +      && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
       112108  +
111375 112109     /* If nIncr is less than zero, then check at runtime if there are any
111376 112110     ** outstanding constraints to resolve. If there are not, there is no need
111377 112111     ** to check if deleting this row resolves any outstanding violations.
111378 112112     **
111379 112113     ** Check if any of the key columns in the child table row are NULL. If 
111380 112114     ** any are, then the constraint is considered satisfied. No need to 
111381 112115     ** search for a matching row in the parent table.  */
................................................................................
111775 112509       ** transactions are not able to rollback schema changes.  
111776 112510       **
111777 112511       ** If the SQLITE_DeferFKs flag is set, then this is not required, as
111778 112512       ** the statement transaction will not be rolled back even if FK
111779 112513       ** constraints are violated.
111780 112514       */
111781 112515       if( (db->flags & SQLITE_DeferFKs)==0 ){
       112516  +      sqlite3VdbeVerifyAbortable(v, OE_Abort);
111782 112517         sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
111783 112518         VdbeCoverage(v);
111784 112519         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
111785 112520             OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
111786 112521       }
111787 112522   
111788 112523       if( iSkip ){
................................................................................
112687 113422   */
112688 113423   static int autoIncBegin(
112689 113424     Parse *pParse,      /* Parsing context */
112690 113425     int iDb,            /* Index of the database holding pTab */
112691 113426     Table *pTab         /* The table we are writing to */
112692 113427   ){
112693 113428     int memId = 0;      /* Register holding maximum rowid */
       113429  +  assert( pParse->db->aDb[iDb].pSchema!=0 );
112694 113430     if( (pTab->tabFlags & TF_Autoincrement)!=0
112695 113431      && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
112696 113432     ){
112697 113433       Parse *pToplevel = sqlite3ParseToplevel(pParse);
112698 113434       AutoincInfo *pInfo;
       113435  +    Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
       113436  +
       113437  +    /* Verify that the sqlite_sequence table exists and is an ordinary
       113438  +    ** rowid table with exactly two columns.
       113439  +    ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
       113440  +    if( pSeqTab==0
       113441  +     || !HasRowid(pSeqTab)
       113442  +     || IsVirtual(pSeqTab)
       113443  +     || pSeqTab->nCol!=2
       113444  +    ){
       113445  +      pParse->nErr++;
       113446  +      pParse->rc = SQLITE_CORRUPT_SEQUENCE;
       113447  +      return 0;
       113448  +    }
112699 113449   
112700 113450       pInfo = pToplevel->pAinc;
112701 113451       while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
112702 113452       if( pInfo==0 ){
112703 113453         pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
112704 113454         if( pInfo==0 ) return 0;
112705 113455         pInfo->pNext = pToplevel->pAinc;
................................................................................
112949 113699   **      D: cleanup
112950 113700   */
112951 113701   SQLITE_PRIVATE void sqlite3Insert(
112952 113702     Parse *pParse,        /* Parser context */
112953 113703     SrcList *pTabList,    /* Name of table into which we are inserting */
112954 113704     Select *pSelect,      /* A SELECT statement to use as the data source */
112955 113705     IdList *pColumn,      /* Column names corresponding to IDLIST. */
112956         -  int onError           /* How to handle constraint errors */
       113706  +  int onError,          /* How to handle constraint errors */
       113707  +  Upsert *pUpsert       /* ON CONFLICT clauses for upsert, or NULL */
112957 113708   ){
112958 113709     sqlite3 *db;          /* The main database structure */
112959 113710     Table *pTab;          /* The table to insert into.  aka TABLE */
112960 113711     int i, j;             /* Loop counters */
112961 113712     Vdbe *v;              /* Generate code into this virtual machine */
112962 113713     Index *pIdx;          /* For looping over indices of the table */
112963 113714     int nColumn;          /* Number of columns in the data */
................................................................................
113244 113995     if( pColumn!=0 && nColumn!=pColumn->nId ){
113245 113996       sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
113246 113997       goto insert_cleanup;
113247 113998     }
113248 113999       
113249 114000     /* Initialize the count of rows to be inserted
113250 114001     */
113251         -  if( db->flags & SQLITE_CountRows ){
       114002  +  if( (db->flags & SQLITE_CountRows)!=0
       114003  +   && !pParse->nested
       114004  +   && !pParse->pTriggerTab
       114005  +  ){
113252 114006       regRowCount = ++pParse->nMem;
113253 114007       sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
113254 114008     }
113255 114009   
113256 114010     /* If this is not a view, open the table and and all indices */
113257 114011     if( !isView ){
113258 114012       int nIdx;
................................................................................
113264 114018       }
113265 114019       for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
113266 114020         assert( pIdx );
113267 114021         aRegIdx[i] = ++pParse->nMem;
113268 114022         pParse->nMem += pIdx->nColumn;
113269 114023       }
113270 114024     }
       114025  +#ifndef SQLITE_OMIT_UPSERT
       114026  +  if( pUpsert ){
       114027  +    pTabList->a[0].iCursor = iDataCur;
       114028  +    pUpsert->pUpsertSrc = pTabList;
       114029  +    pUpsert->regData = regData;
       114030  +    pUpsert->iDataCur = iDataCur;
       114031  +    pUpsert->iIdxCur = iIdxCur;
       114032  +    if( pUpsert->pUpsertTarget ){
       114033  +      sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
       114034  +    }
       114035  +  }
       114036  +#endif
       114037  +
113271 114038   
113272 114039     /* This is the top of the main insertion loop */
113273 114040     if( useTempTable ){
113274 114041       /* This block codes the top of loop only.  The complete loop is the
113275 114042       ** following pseudocode (template 4):
113276 114043       **
113277 114044       **         rewind temp table, if empty goto D
................................................................................
113466 114233         sqlite3MayAbort(pParse);
113467 114234       }else
113468 114235   #endif
113469 114236       {
113470 114237         int isReplace;    /* Set to true if constraints may cause a replace */
113471 114238         int bUseSeek;     /* True to use OPFLAG_SEEKRESULT */
113472 114239         sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
113473         -          regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0
       114240  +          regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
113474 114241         );
113475 114242         sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
113476 114243   
113477 114244         /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
113478 114245         ** constraints or (b) there are no triggers and this table is not a
113479 114246         ** parent table in a foreign key constraint. It is safe to set the
113480 114247         ** flag in the second case as if any REPLACE constraint is hit, an
................................................................................
113489 114256             regIns, aRegIdx, 0, appendFlag, bUseSeek
113490 114257         );
113491 114258       }
113492 114259     }
113493 114260   
113494 114261     /* Update the count of rows that are inserted
113495 114262     */
113496         -  if( (db->flags & SQLITE_CountRows)!=0 ){
       114263  +  if( regRowCount ){
113497 114264       sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
113498 114265     }
113499 114266   
113500 114267     if( pTrigger ){
113501 114268       /* Code AFTER triggers */
113502 114269       sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 
113503 114270           pTab, regData-2-pTab->nCol, onError, endOfLoop);
................................................................................
113526 114293     }
113527 114294   
113528 114295     /*
113529 114296     ** Return the number of rows inserted. If this routine is 
113530 114297     ** generating code because of a call to sqlite3NestedParse(), do not
113531 114298     ** invoke the callback function.
113532 114299     */
113533         -  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
       114300  +  if( regRowCount ){
113534 114301       sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
113535 114302       sqlite3VdbeSetNumCols(v, 1);
113536 114303       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
113537 114304     }
113538 114305   
113539 114306   insert_cleanup:
113540 114307     sqlite3SrcListDelete(db, pTabList);
113541 114308     sqlite3ExprListDelete(db, pList);
       114309  +  sqlite3UpsertDelete(db, pUpsert);
113542 114310     sqlite3SelectDelete(db, pSelect);
113543 114311     sqlite3IdListDelete(db, pColumn);
113544 114312     sqlite3DbFree(db, aRegIdx);
113545 114313   }
113546 114314   
113547 114315   /* Make sure "isView" and other macros defined above are undefined. Otherwise
113548 114316   ** they may interfere with compilation of other functions in this file
................................................................................
113605 114373     }
113606 114374     testcase( w.eCode==0 );
113607 114375     testcase( w.eCode==CKCNSTRNT_COLUMN );
113608 114376     testcase( w.eCode==CKCNSTRNT_ROWID );
113609 114377     testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
113610 114378     return !w.eCode;
113611 114379   }
       114380  +
       114381  +/*
       114382  +** An instance of the ConstraintAddr object remembers the byte-code addresses
       114383  +** for sections of the constraint checks that deal with uniqueness constraints
       114384  +** on the rowid and on the upsert constraint.
       114385  +**
       114386  +** This information is passed into checkReorderConstraintChecks() to insert
       114387  +** some OP_Goto operations so that the rowid and upsert constraints occur
       114388  +** in the correct order relative to other constraints.
       114389  +*/
       114390  +typedef struct ConstraintAddr ConstraintAddr;
       114391  +struct ConstraintAddr {
       114392  +  int ipkTop;          /* Subroutine for rowid constraint check */
       114393  +  int upsertTop;       /* Label for upsert constraint check subroutine */
       114394  +  int upsertTop2;      /* Copy of upsertTop not cleared by the call */
       114395  +  int upsertBtm;       /* upsert constraint returns to this label */
       114396  +  int ipkBtm;          /* Return opcode rowid constraint check */
       114397  +};
       114398  +
       114399  +/*
       114400  +** Generate any OP_Goto operations needed to cause constraints to be
       114401  +** run that haven't already been run.
       114402  +*/
       114403  +static void reorderConstraintChecks(Vdbe *v, ConstraintAddr *p){
       114404  +  if( p->upsertTop ){
       114405  +    testcase( sqlite3VdbeLabelHasBeenResolved(v, p->upsertTop) );
       114406  +    sqlite3VdbeGoto(v, p->upsertTop);
       114407  +    VdbeComment((v, "call upsert subroutine"));
       114408  +    sqlite3VdbeResolveLabel(v, p->upsertBtm);
       114409  +    p->upsertTop = 0;
       114410  +  }
       114411  +  if( p->ipkTop ){
       114412  +    sqlite3VdbeGoto(v, p->ipkTop);
       114413  +    VdbeComment((v, "call rowid unique-check subroutine"));
       114414  +    sqlite3VdbeJumpHere(v, p->ipkBtm);
       114415  +    p->ipkTop = 0;
       114416  +  }
       114417  +}
113612 114418   
113613 114419   /*
113614 114420   ** Generate code to do constraint checks prior to an INSERT or an UPDATE
113615 114421   ** on table pTab.
113616 114422   **
113617 114423   ** The regNewData parameter is the first register in a range that contains
113618 114424   ** the data to be inserted or the data after the update.  There will be
................................................................................
113701 114507     int iIdxCur,         /* First index cursor */
113702 114508     int regNewData,      /* First register in a range holding values to insert */
113703 114509     int regOldData,      /* Previous content.  0 for INSERTs */
113704 114510     u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
113705 114511     u8 overrideError,    /* Override onError to this if not OE_Default */
113706 114512     int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
113707 114513     int *pbMayReplace,   /* OUT: Set to true if constraint may cause a replace */
113708         -  int *aiChng          /* column i is unchanged if aiChng[i]<0 */
       114514  +  int *aiChng,         /* column i is unchanged if aiChng[i]<0 */
       114515  +  Upsert *pUpsert      /* ON CONFLICT clauses, if any.  NULL otherwise */
113709 114516   ){
113710 114517     Vdbe *v;             /* VDBE under constrution */
113711 114518     Index *pIdx;         /* Pointer to one of the indices */
113712 114519     Index *pPk = 0;      /* The PRIMARY KEY index */
113713 114520     sqlite3 *db;         /* Database connection */
113714 114521     int i;               /* loop counter */
113715 114522     int ix;              /* Index loop counter */
113716 114523     int nCol;            /* Number of columns */
113717 114524     int onError;         /* Conflict resolution strategy */
113718 114525     int addr1;           /* Address of jump instruction */
113719 114526     int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
113720 114527     int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
113721         -  int ipkTop = 0;      /* Top of the rowid change constraint check */
113722         -  int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
       114528  +  ConstraintAddr sAddr;/* Address information for constraint reordering */
       114529  +  Index *pUpIdx = 0;   /* Index to which to apply the upsert */
113723 114530     u8 isUpdate;         /* True if this is an UPDATE operation */
113724 114531     u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
       114532  +  int upsertBypass = 0;  /* Address of Goto to bypass upsert subroutine */
113725 114533   
113726 114534     isUpdate = regOldData!=0;
113727 114535     db = pParse->db;
113728 114536     v = sqlite3GetVdbe(pParse);
113729 114537     assert( v!=0 );
113730 114538     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
113731 114539     nCol = pTab->nCol;
       114540  +  memset(&sAddr, 0, sizeof(sAddr));
113732 114541     
113733 114542     /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
113734 114543     ** normal rowid tables.  nPkField is the number of key fields in the 
113735 114544     ** pPk index or 1 for a rowid table.  In other words, nPkField is the
113736 114545     ** number of fields in the true primary key of the table. */
113737 114546     if( HasRowid(pTab) ){
113738 114547       pPk = 0;
................................................................................
113807 114616       pParse->iSelfTab = -(regNewData+1);
113808 114617       onError = overrideError!=OE_Default ? overrideError : OE_Abort;
113809 114618       for(i=0; i<pCheck->nExpr; i++){
113810 114619         int allOk;
113811 114620         Expr *pExpr = pCheck->a[i].pExpr;
113812 114621         if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
113813 114622         allOk = sqlite3VdbeMakeLabel(v);
       114623  +      sqlite3VdbeVerifyAbortable(v, onError);
113814 114624         sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
113815 114625         if( onError==OE_Ignore ){
113816 114626           sqlite3VdbeGoto(v, ignoreDest);
113817 114627         }else{
113818 114628           char *zName = pCheck->a[i].zName;
113819 114629           if( zName==0 ) zName = pTab->zName;
113820 114630           if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
................................................................................
113823 114633                                 P5_ConstraintCheck);
113824 114634         }
113825 114635         sqlite3VdbeResolveLabel(v, allOk);
113826 114636       }
113827 114637       pParse->iSelfTab = 0;
113828 114638     }
113829 114639   #endif /* !defined(SQLITE_OMIT_CHECK) */
       114640  +
       114641  +  /* UNIQUE and PRIMARY KEY constraints should be handled in the following
       114642  +  ** order:
       114643  +  **
       114644  +  **   (1)  OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
       114645  +  **   (2)  OE_Update
       114646  +  **   (3)  OE_Replace
       114647  +  **
       114648  +  ** OE_Fail and OE_Ignore must happen before any changes are made.
       114649  +  ** OE_Update guarantees that only a single row will change, so it
       114650  +  ** must happen before OE_Replace.  Technically, OE_Abort and OE_Rollback
       114651  +  ** could happen in any order, but they are grouped up front for
       114652  +  ** convenience.
       114653  +  **
       114654  +  ** Constraint checking code is generated in this order:
       114655  +  **   (A)  The rowid constraint
       114656  +  **   (B)  Unique index constraints that do not have OE_Replace as their
       114657  +  **        default conflict resolution strategy
       114658  +  **   (C)  Unique index that do use OE_Replace by default.
       114659  +  **
       114660  +  ** The ordering of (2) and (3) is accomplished by making sure the linked
       114661  +  ** list of indexes attached to a table puts all OE_Replace indexes last
       114662  +  ** in the list.  See sqlite3CreateIndex() for where that happens.
       114663  +  */
       114664  +
       114665  +  if( pUpsert ){
       114666  +    if( pUpsert->pUpsertTarget==0 ){
       114667  +      /* An ON CONFLICT DO NOTHING clause, without a constraint-target.
       114668  +      ** Make all unique constraint resolution be OE_Ignore */
       114669  +      assert( pUpsert->pUpsertSet==0 );
       114670  +      overrideError = OE_Ignore;
       114671  +      pUpsert = 0;
       114672  +    }else if( (pUpIdx = pUpsert->pUpsertIdx)!=0 ){
       114673  +      /* If the constraint-target is on some column other than
       114674  +      ** then ROWID, then we might need to move the UPSERT around
       114675  +      ** so that it occurs in the correct order. */
       114676  +      sAddr.upsertTop = sAddr.upsertTop2 = sqlite3VdbeMakeLabel(v);
       114677  +      sAddr.upsertBtm = sqlite3VdbeMakeLabel(v);
       114678  +    }
       114679  +  }
113830 114680   
113831 114681     /* If rowid is changing, make sure the new rowid does not previously
113832 114682     ** exist in the table.
113833 114683     */
113834 114684     if( pkChng && pPk==0 ){
113835 114685       int addrRowidOk = sqlite3VdbeMakeLabel(v);
113836 114686   
................................................................................
113837 114687       /* Figure out what action to take in case of a rowid collision */
113838 114688       onError = pTab->keyConf;
113839 114689       if( overrideError!=OE_Default ){
113840 114690         onError = overrideError;
113841 114691       }else if( onError==OE_Default ){
113842 114692         onError = OE_Abort;
113843 114693       }
       114694  +
       114695  +    /* figure out whether or not upsert applies in this case */
       114696  +    if( pUpsert && pUpsert->pUpsertIdx==0 ){
       114697  +      if( pUpsert->pUpsertSet==0 ){
       114698  +        onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
       114699  +      }else{
       114700  +        onError = OE_Update;  /* DO UPDATE */
       114701  +      }
       114702  +    }
       114703  +
       114704  +    /* If the response to a rowid conflict is REPLACE but the response
       114705  +    ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
       114706  +    ** to defer the running of the rowid conflict checking until after
       114707  +    ** the UNIQUE constraints have run.
       114708  +    */
       114709  +    assert( OE_Update>OE_Replace );
       114710  +    assert( OE_Ignore<OE_Replace );
       114711  +    assert( OE_Fail<OE_Replace );
       114712  +    assert( OE_Abort<OE_Replace );
       114713  +    assert( OE_Rollback<OE_Replace );
       114714  +    if( onError>=OE_Replace
       114715  +     && (pUpsert || onError!=overrideError)
       114716  +     && pTab->pIndex
       114717  +    ){
       114718  +      sAddr.ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
       114719  +    }
113844 114720   
113845 114721       if( isUpdate ){
113846 114722         /* pkChng!=0 does not mean that the rowid has changed, only that
113847 114723         ** it might have changed.  Skip the conflict logic below if the rowid
113848 114724         ** is unchanged. */
113849 114725         sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
113850 114726         sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
113851 114727         VdbeCoverage(v);
113852 114728       }
113853 114729   
113854         -    /* If the response to a rowid conflict is REPLACE but the response
113855         -    ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
113856         -    ** to defer the running of the rowid conflict checking until after
113857         -    ** the UNIQUE constraints have run.
113858         -    */
113859         -    if( onError==OE_Replace && overrideError!=OE_Replace ){
113860         -      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113861         -        if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
113862         -          ipkTop = sqlite3VdbeAddOp0(v, OP_Goto);
113863         -          break;
113864         -        }
113865         -      }
113866         -    }
113867         -
113868 114730       /* Check to see if the new rowid already exists in the table.  Skip
113869 114731       ** the following conflict logic if it does not. */
       114732  +    VdbeNoopComment((v, "uniqueness check for ROWID"));
       114733  +    sqlite3VdbeVerifyAbortable(v, onError);
113870 114734       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
113871 114735       VdbeCoverage(v);
113872 114736   
113873         -    /* Generate code that deals with a rowid collision */
113874 114737       switch( onError ){
113875 114738         default: {
113876 114739           onError = OE_Abort;
113877 114740           /* Fall thru into the next case */
113878 114741         }
113879 114742         case OE_Rollback:
113880 114743         case OE_Abort:
113881 114744         case OE_Fail: {
       114745  +        testcase( onError==OE_Rollback );
       114746  +        testcase( onError==OE_Abort );
       114747  +        testcase( onError==OE_Fail );
113882 114748           sqlite3RowidConstraint(pParse, onError, pTab);
113883 114749           break;
113884 114750         }
113885 114751         case OE_Replace: {
113886 114752           /* If there are DELETE triggers on this table and the
113887 114753           ** recursive-triggers flag is set, call GenerateRowDelete() to
113888 114754           ** remove the conflicting row from the table. This will fire
................................................................................
113911 114777           }
113912 114778           if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
113913 114779             sqlite3MultiWrite(pParse);
113914 114780             sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
113915 114781                                      regNewData, 1, 0, OE_Replace, 1, -1);
113916 114782           }else{
113917 114783   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
113918         -          if( HasRowid(pTab) ){
       114784  +          assert( HasRowid(pTab) );
113919 114785               /* This OP_Delete opcode fires the pre-update-hook only. It does
113920 114786               ** not modify the b-tree. It is more efficient to let the coming
113921 114787               ** OP_Insert replace the existing entry than it is to delete the
113922 114788               ** existing entry and then insert a new one. */
113923 114789               sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
113924 114790               sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
113925         -          }
113926 114791   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
113927 114792             if( pTab->pIndex ){
113928 114793               sqlite3MultiWrite(pParse);
113929 114794               sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
113930 114795             }
113931 114796           }
113932 114797           seenReplace = 1;
113933 114798           break;
113934 114799         }
       114800  +#ifndef SQLITE_OMIT_UPSERT
       114801  +      case OE_Update: {
       114802  +        sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
       114803  +        /* Fall through */
       114804  +      }
       114805  +#endif
113935 114806         case OE_Ignore: {
113936         -        /*assert( seenReplace==0 );*/
       114807  +        testcase( onError==OE_Ignore );
113937 114808           sqlite3VdbeGoto(v, ignoreDest);
113938 114809           break;
113939 114810         }
113940 114811       }
113941 114812       sqlite3VdbeResolveLabel(v, addrRowidOk);
113942         -    if( ipkTop ){
113943         -      ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
113944         -      sqlite3VdbeJumpHere(v, ipkTop);
       114813  +    if( sAddr.ipkTop ){
       114814  +      sAddr.ipkBtm = sqlite3VdbeAddOp0(v, OP_Goto);
       114815  +      sqlite3VdbeJumpHere(v, sAddr.ipkTop-1);
113945 114816       }
113946 114817     }
113947 114818   
113948 114819     /* Test all UNIQUE constraints by creating entries for each UNIQUE
113949 114820     ** index and making sure that duplicate entries do not already exist.
113950 114821     ** Compute the revised record entries for indices as we go.
113951 114822     **
................................................................................
113955 114826     for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
113956 114827       int regIdx;          /* Range of registers hold conent for pIdx */
113957 114828       int regR;            /* Range of registers holding conflicting PK */
113958 114829       int iThisCur;        /* Cursor for this UNIQUE index */
113959 114830       int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
113960 114831   
113961 114832       if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
       114833  +    if( pUpIdx==pIdx ){
       114834  +      addrUniqueOk = sAddr.upsertBtm;
       114835  +      upsertBypass = sqlite3VdbeGoto(v, 0);
       114836  +      VdbeComment((v, "Skip upsert subroutine"));
       114837  +      sqlite3VdbeResolveLabel(v, sAddr.upsertTop2);
       114838  +    }else{
       114839  +      addrUniqueOk = sqlite3VdbeMakeLabel(v);
       114840  +    }
       114841  +    VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName));
113962 114842       if( bAffinityDone==0 ){
113963 114843         sqlite3TableAffinity(v, pTab, regNewData+1);
113964 114844         bAffinityDone = 1;
113965 114845       }
113966 114846       iThisCur = iIdxCur+ix;
113967         -    addrUniqueOk = sqlite3VdbeMakeLabel(v);
       114847  +
113968 114848   
113969 114849       /* Skip partial indices for which the WHERE clause is not true */
113970 114850       if( pIdx->pPartIdxWhere ){
113971 114851         sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
113972 114852         pParse->iSelfTab = -(regNewData+1);
113973 114853         sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
113974 114854                               SQLITE_JUMPIFNULL);
................................................................................
114019 114899         continue;  /* pIdx is not a UNIQUE index */
114020 114900       }
114021 114901       if( overrideError!=OE_Default ){
114022 114902         onError = overrideError;
114023 114903       }else if( onError==OE_Default ){
114024 114904         onError = OE_Abort;
114025 114905       }
       114906  +
       114907  +    /* Figure out if the upsert clause applies to this index */
       114908  +    if( pUpIdx==pIdx ){
       114909  +      if( pUpsert->pUpsertSet==0 ){
       114910  +        onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
       114911  +      }else{
       114912  +        onError = OE_Update;  /* DO UPDATE */
       114913  +      }
       114914  +    }
       114915  +
       114916  +    /* Invoke subroutines to handle IPK replace and upsert prior to running
       114917  +    ** the first REPLACE constraint check. */
       114918  +    if( onError==OE_Replace ){
       114919  +      testcase( sAddr.ipkTop );
       114920  +      testcase( sAddr.upsertTop
       114921  +             && sqlite3VdbeLabelHasBeenResolved(v,sAddr.upsertTop) );
       114922  +      reorderConstraintChecks(v, &sAddr);
       114923  +    }
114026 114924   
114027 114925       /* Collision detection may be omitted if all of the following are true:
114028 114926       **   (1) The conflict resolution algorithm is REPLACE
114029 114927       **   (2) The table is a WITHOUT ROWID table
114030 114928       **   (3) There are no secondary indexes on the table
114031 114929       **   (4) No delete triggers need to be fired if there is a conflict
114032 114930       **   (5) No FK constraint counters need to be updated if a conflict occurs.
................................................................................
114041 114939       ){
114042 114940         sqlite3VdbeResolveLabel(v, addrUniqueOk);
114043 114941         continue;
114044 114942       }
114045 114943   
114046 114944       /* Check to see if the new index entry will be unique */
114047 114945       sqlite3ExprCachePush(pParse);
       114946  +    sqlite3VdbeVerifyAbortable(v, onError);
114048 114947       sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
114049 114948                            regIdx, pIdx->nKeyCol); VdbeCoverage(v);
114050 114949   
114051 114950       /* Generate code to handle collisions */
114052 114951       regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
114053 114952       if( isUpdate || onError==OE_Replace ){
114054 114953         if( HasRowid(pTab) ){
................................................................................
114102 115001             }
114103 115002           }
114104 115003         }
114105 115004       }
114106 115005   
114107 115006       /* Generate code that executes if the new index entry is not unique */
114108 115007       assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
114109         -        || onError==OE_Ignore || onError==OE_Replace );
       115008  +        || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
114110 115009       switch( onError ){
114111 115010         case OE_Rollback:
114112 115011         case OE_Abort:
114113 115012         case OE_Fail: {
       115013  +        testcase( onError==OE_Rollback );
       115014  +        testcase( onError==OE_Abort );
       115015  +        testcase( onError==OE_Fail );
114114 115016           sqlite3UniqueConstraint(pParse, onError, pIdx);
114115 115017           break;
114116 115018         }
       115019  +#ifndef SQLITE_OMIT_UPSERT
       115020  +      case OE_Update: {
       115021  +        sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
       115022  +        /* Fall through */
       115023  +      }
       115024  +#endif
114117 115025         case OE_Ignore: {
       115026  +        testcase( onError==OE_Ignore );
114118 115027           sqlite3VdbeGoto(v, ignoreDest);
114119 115028           break;
114120 115029         }
114121 115030         default: {
114122 115031           Trigger *pTrigger = 0;
114123 115032           assert( onError==OE_Replace );
114124         -        sqlite3MultiWrite(pParse);
114125 115033           if( db->flags&SQLITE_RecTriggers ){
114126 115034             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
       115035  +        }
       115036  +        if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
       115037  +          sqlite3MultiWrite(pParse);
114127 115038           }
114128 115039           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
114129 115040               regR, nPkField, 0, OE_Replace,
114130 115041               (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
114131 115042           seenReplace = 1;
114132 115043           break;
114133 115044         }
114134 115045       }
       115046  +    if( pUpIdx==pIdx ){
       115047  +      sqlite3VdbeJumpHere(v, upsertBypass);
       115048  +    }else{
114135 115049       sqlite3VdbeResolveLabel(v, addrUniqueOk);
       115050  +    }
114136 115051       sqlite3ExprCachePop(pParse);
114137 115052       if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
       115053  +
114138 115054     }
114139         -  if( ipkTop ){
114140         -    sqlite3VdbeGoto(v, ipkTop+1);
114141         -    sqlite3VdbeJumpHere(v, ipkBottom);
114142         -  }
       115055  +  testcase( sAddr.ipkTop!=0 );
       115056  +  testcase( sAddr.upsertTop
       115057  +         && sqlite3VdbeLabelHasBeenResolved(v,sAddr.upsertTop) );
       115058  +  reorderConstraintChecks(v, &sAddr);
114143 115059     
114144 115060     *pbMayReplace = seenReplace;
114145 115061     VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
114146 115062   }
114147 115063   
114148 115064   #ifdef SQLITE_ENABLE_NULL_TRIM
114149 115065   /*
................................................................................
114635 115551     }
114636 115552     if( HasRowid(pSrc) ){
114637 115553       u8 insFlags;
114638 115554       sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
114639 115555       emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
114640 115556       if( pDest->iPKey>=0 ){
114641 115557         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
       115558  +      sqlite3VdbeVerifyAbortable(v, onError);
114642 115559         addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
114643 115560         VdbeCoverage(v);
114644 115561         sqlite3RowidConstraint(pParse, onError, pDest);
114645 115562         sqlite3VdbeJumpHere(v, addr2);
114646 115563         autoIncStep(pParse, regAutoinc, regRowid);
114647 115564       }else if( pDest->pIndex==0 ){
114648 115565         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
................................................................................
115192 116109                         sqlite3_stmt**,const void**);
115193 116110     int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
115194 116111     void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
115195 116112     void *(*value_pointer)(sqlite3_value*,const char*);
115196 116113     int (*vtab_nochange)(sqlite3_context*);
115197 116114     int (*value_nochange)(sqlite3_value*);
115198 116115     const char *(*vtab_collation)(sqlite3_index_info*,int);
       116116  +  /* Version 3.24.0 and later */
       116117  +  int (*keyword_count)(void);
       116118  +  int (*keyword_name)(int,const char**,int*);
       116119  +  int (*keyword_check)(const char*,int);
       116120  +  sqlite3_str *(*str_new)(sqlite3*);
       116121  +  char *(*str_finish)(sqlite3_str*);
       116122  +  void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
       116123  +  void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
       116124  +  void (*str_append)(sqlite3_str*, const char *zIn, int N);
       116125  +  void (*str_appendall)(sqlite3_str*, const char *zIn);
       116126  +  void (*str_appendchar)(sqlite3_str*, int N, char C);
       116127  +  void (*str_reset)(sqlite3_str*);
       116128  +  int (*str_errcode)(sqlite3_str*);
       116129  +  int (*str_length)(sqlite3_str*);
       116130  +  char *(*str_value)(sqlite3_str*);
115199 116131   };
115200 116132   
115201 116133   /*
115202 116134   ** This is the function signature used for all extension entry points.  It
115203 116135   ** is also defined in the file "loadext.c".
115204 116136   */
115205 116137   typedef int (*sqlite3_loadext_entry)(
................................................................................
115462 116394   #define sqlite3_bind_pointer           sqlite3_api->bind_pointer
115463 116395   #define sqlite3_result_pointer         sqlite3_api->result_pointer
115464 116396   #define sqlite3_value_pointer          sqlite3_api->value_pointer
115465 116397   /* Version 3.22.0 and later */
115466 116398   #define sqlite3_vtab_nochange          sqlite3_api->vtab_nochange
115467 116399   #define sqlite3_value_nochange         sqlite3_api->value_nochange
115468 116400   #define sqlite3_vtab_collation         sqlite3_api->vtab_collation
       116401  +/* Version 3.24.0 and later */
       116402  +#define sqlite3_keyword_count          sqlite3_api->keyword_count
       116403  +#define sqlite3_keyword_name           sqlite3_api->keyword_name
       116404  +#define sqlite3_keyword_check          sqlite3_api->keyword_check
       116405  +#define sqlite3_str_new                sqlite3_api->str_new
       116406  +#define sqlite3_str_finish             sqlite3_api->str_finish
       116407  +#define sqlite3_str_appendf            sqlite3_api->str_appendf
       116408  +#define sqlite3_str_vappendf           sqlite3_api->str_vappendf
       116409  +#define sqlite3_str_append             sqlite3_api->str_append
       116410  +#define sqlite3_str_appendall          sqlite3_api->str_appendall
       116411  +#define sqlite3_str_appendchar         sqlite3_api->str_appendchar
       116412  +#define sqlite3_str_reset              sqlite3_api->str_reset
       116413  +#define sqlite3_str_errcode            sqlite3_api->str_errcode
       116414  +#define sqlite3_str_length             sqlite3_api->str_length
       116415  +#define sqlite3_str_value              sqlite3_api->str_value
115469 116416   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
115470 116417   
115471 116418   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
115472 116419     /* This case when the file really is being compiled as a loadable 
115473 116420     ** extension */
115474 116421   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
115475 116422   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
115900 116847     sqlite3_prepare16_v3,
115901 116848     sqlite3_bind_pointer,
115902 116849     sqlite3_result_pointer,
115903 116850     sqlite3_value_pointer,
115904 116851     /* Version 3.22.0 and later */
115905 116852     sqlite3_vtab_nochange,
115906 116853     sqlite3_value_nochange,
115907         -  sqlite3_vtab_collation
       116854  +  sqlite3_vtab_collation,
       116855  +  /* Version 3.24.0 and later */
       116856  +  sqlite3_keyword_count,
       116857  +  sqlite3_keyword_name,
       116858  +  sqlite3_keyword_check,
       116859  +  sqlite3_str_new,
       116860  +  sqlite3_str_finish,
       116861  +  sqlite3_str_appendf,
       116862  +  sqlite3_str_vappendf,
       116863  +  sqlite3_str_append,
       116864  +  sqlite3_str_appendall,
       116865  +  sqlite3_str_appendchar,
       116866  +  sqlite3_str_reset,
       116867  +  sqlite3_str_errcode,
       116868  +  sqlite3_str_length,
       116869  +  sqlite3_str_value
115908 116870   };
115909 116871   
115910 116872   /*
115911 116873   ** Attempt to load an SQLite extension library contained in the file
115912 116874   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
115913 116875   ** default entry point name (sqlite3_extension_init) is used.  Use
115914 116876   ** of the default name is recommended.
................................................................................
115966 116928   
115967 116929     zEntry = zProc ? zProc : "sqlite3_extension_init";
115968 116930   
115969 116931     handle = sqlite3OsDlOpen(pVfs, zFile);
115970 116932   #if SQLITE_OS_UNIX || SQLITE_OS_WIN
115971 116933     for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
115972 116934       char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
115973         -    int bExists = 0;
115974 116935       if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
115975         -    sqlite3OsAccess(pVfs, zAltFile, SQLITE_ACCESS_EXISTS, &bExists);
115976         -    if( bExists )  handle = sqlite3OsDlOpen(pVfs, zAltFile);
       116936  +    handle = sqlite3OsDlOpen(pVfs, zAltFile);
115977 116937       sqlite3_free(zAltFile);
115978 116938     }
115979 116939   #endif
115980 116940     if( handle==0 ){
115981 116941       if( pzErrMsg ){
115982 116942         *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
115983 116943         if( zErrmsg ){
................................................................................
119141 120101     char cSep = '(';
119142 120102     StrAccum acc;
119143 120103     char zBuf[200];
119144 120104   
119145 120105     UNUSED_PARAMETER(argc);
119146 120106     UNUSED_PARAMETER(argv);
119147 120107     sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
119148         -  sqlite3StrAccumAppendAll(&acc, "CREATE TABLE x");
       120108  +  sqlite3_str_appendall(&acc, "CREATE TABLE x");
119149 120109     for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
119150         -    sqlite3XPrintf(&acc, "%c\"%s\"", cSep, pragCName[j]);
       120110  +    sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
119151 120111       cSep = ',';
119152 120112     }
119153 120113     if( i==0 ){
119154         -    sqlite3XPrintf(&acc, "(\"%s\"", pPragma->zName);
       120114  +    sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
119155 120115       cSep = ',';
119156 120116       i++;
119157 120117     }
119158 120118     j = 0;
119159 120119     if( pPragma->mPragFlg & PragFlg_Result1 ){
119160         -    sqlite3StrAccumAppendAll(&acc, ",arg HIDDEN");
       120120  +    sqlite3_str_appendall(&acc, ",arg HIDDEN");
119161 120121       j++;
119162 120122     }
119163 120123     if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
119164         -    sqlite3StrAccumAppendAll(&acc, ",schema HIDDEN");
       120124  +    sqlite3_str_appendall(&acc, ",schema HIDDEN");
119165 120125       j++;
119166 120126     }
119167         -  sqlite3StrAccumAppend(&acc, ")", 1);
       120127  +  sqlite3_str_append(&acc, ")", 1);
119168 120128     sqlite3StrAccumFinish(&acc);
119169 120129     assert( strlen(zBuf) < sizeof(zBuf)-1 );
119170 120130     rc = sqlite3_declare_vtab(db, zBuf);
119171 120131     if( rc==SQLITE_OK ){
119172 120132       pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
119173 120133       if( pTab==0 ){
119174 120134         rc = SQLITE_NOMEM;
................................................................................
119312 120272         pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
119313 120273         if( pCsr->azArg[j]==0 ){
119314 120274           return SQLITE_NOMEM;
119315 120275         }
119316 120276       }
119317 120277     }
119318 120278     sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
119319         -  sqlite3StrAccumAppendAll(&acc, "PRAGMA ");
       120279  +  sqlite3_str_appendall(&acc, "PRAGMA ");
119320 120280     if( pCsr->azArg[1] ){
119321         -    sqlite3XPrintf(&acc, "%Q.", pCsr->azArg[1]);
       120281  +    sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
119322 120282     }
119323         -  sqlite3StrAccumAppendAll(&acc, pTab->pName->zName);
       120283  +  sqlite3_str_appendall(&acc, pTab->pName->zName);
119324 120284     if( pCsr->azArg[0] ){
119325         -    sqlite3XPrintf(&acc, "=%Q", pCsr->azArg[0]);
       120285  +    sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
119326 120286     }
119327 120287     zSql = sqlite3StrAccumFinish(&acc);
119328 120288     if( zSql==0 ) return SQLITE_NOMEM;
119329 120289     rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
119330 120290     sqlite3_free(zSql);
119331 120291     if( rc!=SQLITE_OK ){
119332 120292       pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
................................................................................
119561 120521     Db *pDb;
119562 120522     char const *azArg[4];
119563 120523     int meta[5];
119564 120524     InitData initData;
119565 120525     const char *zMasterName;
119566 120526     int openedTransaction = 0;
119567 120527   
       120528  +  assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
119568 120529     assert( iDb>=0 && iDb<db->nDb );
119569 120530     assert( db->aDb[iDb].pSchema );
119570 120531     assert( sqlite3_mutex_held(db->mutex) );
119571 120532     assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
119572 120533   
119573 120534     db->init.busy = 1;
119574 120535   
................................................................................
119630 120591     **    meta[9]   unused
119631 120592     **
119632 120593     ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
119633 120594     ** the possible values of meta[4].
119634 120595     */
119635 120596     for(i=0; i<ArraySize(meta); i++){
119636 120597       sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
       120598  +  }
       120599  +  if( (db->flags & SQLITE_ResetDatabase)!=0 ){
       120600  +    memset(meta, 0, sizeof(meta));
119637 120601     }
119638 120602     pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
119639 120603   
119640 120604     /* If opening a non-empty database, check the text encoding. For the
119641 120605     ** main database, set sqlite3.enc to the encoding of the main database.
119642 120606     ** For an attached db, it is an error if the encoding is not the same
119643 120607     ** as sqlite3.enc.
................................................................................
119790 120754     /* Do the main schema first */
119791 120755     if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
119792 120756       rc = sqlite3InitOne(db, 0, pzErrMsg);
119793 120757       if( rc ) return rc;
119794 120758     }
119795 120759     /* All other schemas after the main schema. The "temp" schema must be last */
119796 120760     for(i=db->nDb-1; i>0; i--){
       120761  +    assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
119797 120762       if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
119798 120763         rc = sqlite3InitOne(db, i, pzErrMsg);
119799 120764         if( rc ) return rc;
119800 120765       }
119801 120766     }
119802 120767     if( commit_internal ){
119803 120768       sqlite3CommitInternalChanges(db);
................................................................................
119811 120776   */
119812 120777   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
119813 120778     int rc = SQLITE_OK;
119814 120779     sqlite3 *db = pParse->db;
119815 120780     assert( sqlite3_mutex_held(db->mutex) );
119816 120781     if( !db->init.busy ){
119817 120782       rc = sqlite3Init(db, &pParse->zErrMsg);
119818         -  }
119819 120783     if( rc!=SQLITE_OK ){
119820 120784       pParse->rc = rc;
119821 120785       pParse->nErr++;
       120786  +    }else if( db->noSharedCache ){
       120787  +      db->mDbFlags |= DBFLAG_SchemaKnownOk;
       120788  +    }
119822 120789     }
119823 120790     return rc;
119824 120791   }
119825 120792   
119826 120793   
119827 120794   /*
119828 120795   ** Check schema cookies in all databases.  If any cookie is out
................................................................................
120025 120992     }
120026 120993     rc = sParse.rc;
120027 120994   
120028 120995   #ifndef SQLITE_OMIT_EXPLAIN
120029 120996     if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
120030 120997       static const char * const azColName[] = {
120031 120998          "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
120032         -       "selectid", "order", "from", "detail"
       120999  +       "id", "parent", "notused", "detail"
120033 121000       };
120034 121001       int iFirst, mx;
120035 121002       if( sParse.explain==2 ){
120036 121003         sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
120037 121004         iFirst = 8;
120038 121005         mx = 12;
120039 121006       }else{
................................................................................
120339 121306   /*
120340 121307   ** Trace output macros
120341 121308   */
120342 121309   #if SELECTTRACE_ENABLED
120343 121310   /***/ int sqlite3SelectTrace = 0;
120344 121311   # define SELECTTRACE(K,P,S,X)  \
120345 121312     if(sqlite3SelectTrace&(K))   \
120346         -    sqlite3DebugPrintf("%s/%p: ",(S)->zSelName,(S)),\
       121313  +    sqlite3DebugPrintf("%s/%d/%p: ",(S)->zSelName,(P)->addrExplain,(S)),\
120347 121314       sqlite3DebugPrintf X
120348 121315   #else
120349 121316   # define SELECTTRACE(K,P,S,X)
120350 121317   #endif
120351 121318   
120352 121319   
120353 121320   /*
................................................................................
120362 121329     int tabTnct;    /* Ephemeral table used for DISTINCT processing */
120363 121330     int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
120364 121331   };
120365 121332   
120366 121333   /*
120367 121334   ** An instance of the following object is used to record information about
120368 121335   ** the ORDER BY (or GROUP BY) clause of query is being coded.
       121336  +**
       121337  +** The aDefer[] array is used by the sorter-references optimization. For
       121338  +** example, assuming there is no index that can be used for the ORDER BY,
       121339  +** for the query:
       121340  +**
       121341  +**     SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
       121342  +**
       121343  +** it may be more efficient to add just the "a" values to the sorter, and
       121344  +** retrieve the associated "bigblob" values directly from table t1 as the
       121345  +** 10 smallest "a" values are extracted from the sorter.
       121346  +**
       121347  +** When the sorter-reference optimization is used, there is one entry in the
       121348  +** aDefer[] array for each database table that may be read as values are
       121349  +** extracted from the sorter.
120369 121350   */
120370 121351   typedef struct SortCtx SortCtx;
120371 121352   struct SortCtx {
120372 121353     ExprList *pOrderBy;   /* The ORDER BY (or GROUP BY clause) */
120373 121354     int nOBSat;           /* Number of ORDER BY terms satisfied by indices */
120374 121355     int iECursor;         /* Cursor number for the sorter */
120375 121356     int regReturn;        /* Register holding block-output return address */
120376 121357     int labelBkOut;       /* Start label for the block-output subroutine */
120377 121358     int addrSortIndex;    /* Address of the OP_SorterOpen or OP_OpenEphemeral */
120378 121359     int labelDone;        /* Jump here when done, ex: LIMIT reached */
120379 121360     u8 sortFlags;         /* Zero or more SORTFLAG_* bits */
120380 121361     u8 bOrderedInnerLoop; /* ORDER BY correctly sorts the inner loop */
       121362  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       121363  +  u8 nDefer;            /* Number of valid entries in aDefer[] */
       121364  +  struct DeferredCsr {
       121365  +    Table *pTab;        /* Table definition */
       121366  +    int iCsr;           /* Cursor number for table */
       121367  +    int nKey;           /* Number of PK columns for table pTab (>=1) */
       121368  +  } aDefer[4];
       121369  +#endif
       121370  +  struct RowLoadInfo *pDeferredRowLoad;  /* Deferred row loading info or NULL */
120381 121371   };
120382 121372   #define SORTFLAG_UseSorter  0x01   /* Use SorterOpen instead of OpenEphemeral */
120383 121373   
120384 121374   /*
120385 121375   ** Delete all the content of a Select structure.  Deallocate the structure
120386 121376   ** itself only if bFree is true.
120387 121377   */
................................................................................
120831 121821   /* Forward reference */
120832 121822   static KeyInfo *keyInfoFromExprList(
120833 121823     Parse *pParse,       /* Parsing context */
120834 121824     ExprList *pList,     /* Form the KeyInfo object from this ExprList */
120835 121825     int iStart,          /* Begin with this column of pList */
120836 121826     int nExtra           /* Add this many extra columns to the end */
120837 121827   );
       121828  +
       121829  +/*
       121830  +** An instance of this object holds information (beyond pParse and pSelect)
       121831  +** needed to load the next result row that is to be added to the sorter.
       121832  +*/
       121833  +typedef struct RowLoadInfo RowLoadInfo;
       121834  +struct RowLoadInfo {
       121835  +  int regResult;               /* Store results in array of registers here */
       121836  +  u8 ecelFlags;                /* Flag argument to ExprCodeExprList() */
       121837  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       121838  +  ExprList *pExtra;            /* Extra columns needed by sorter refs */
       121839  +  int regExtraResult;          /* Where to load the extra columns */
       121840  +#endif
       121841  +};
       121842  +
       121843  +/*
       121844  +** This routine does the work of loading query data into an array of
       121845  +** registers so that it can be added to the sorter.
       121846  +*/
       121847  +static void innerLoopLoadRow(
       121848  +  Parse *pParse,             /* Statement under construction */
       121849  +  Select *pSelect,           /* The query being coded */
       121850  +  RowLoadInfo *pInfo         /* Info needed to complete the row load */
       121851  +){
       121852  +  sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
       121853  +                          0, pInfo->ecelFlags);
       121854  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       121855  +  if( pInfo->pExtra ){
       121856  +    sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
       121857  +    sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
       121858  +  }
       121859  +#endif
       121860  +}
       121861  +
       121862  +/*
       121863  +** Code the OP_MakeRecord instruction that generates the entry to be
       121864  +** added into the sorter.
       121865  +**
       121866  +** Return the register in which the result is stored.
       121867  +*/
       121868  +static int makeSorterRecord(
       121869  +  Parse *pParse,
       121870  +  SortCtx *pSort,
       121871  +  Select *pSelect,
       121872  +  int regBase,
       121873  +  int nBase
       121874  +){
       121875  +  int nOBSat = pSort->nOBSat;
       121876  +  Vdbe *v = pParse->pVdbe;
       121877  +  int regOut = ++pParse->nMem;
       121878  +  if( pSort->pDeferredRowLoad ){
       121879  +    innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
       121880  +  }
       121881  +  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
       121882  +  return regOut;
       121883  +}
120838 121884   
120839 121885   /*
120840 121886   ** Generate code that will push the record in registers regData
120841 121887   ** through regData+nData-1 onto the sorter.
120842 121888   */
120843 121889   static void pushOntoSorter(
120844 121890     Parse *pParse,         /* Parser context */
120845 121891     SortCtx *pSort,        /* Information about the ORDER BY clause */
120846 121892     Select *pSelect,       /* The whole SELECT statement */
120847 121893     int regData,           /* First register holding data to be sorted */
120848 121894     int regOrigData,       /* First register holding data before packing */
120849         -  int nData,             /* Number of elements in the data array */
       121895  +  int nData,             /* Number of elements in the regData data array */
120850 121896     int nPrefixReg         /* No. of reg prior to regData available for use */
120851 121897   ){
120852 121898     Vdbe *v = pParse->pVdbe;                         /* Stmt under construction */
120853 121899     int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
120854 121900     int nExpr = pSort->pOrderBy->nExpr;              /* No. of ORDER BY terms */
120855 121901     int nBase = nExpr + bSeq + nData;                /* Fields in sorter record */
120856 121902     int regBase;                                     /* Regs for sorter record */
120857         -  int regRecord = ++pParse->nMem;                  /* Assembled sorter record */
       121903  +  int regRecord = 0;                               /* Assembled sorter record */
120858 121904     int nOBSat = pSort->nOBSat;                      /* ORDER BY terms to skip */
120859 121905     int op;                            /* Opcode to add sorter record to sorter */
120860 121906     int iLimit;                        /* LIMIT counter */
       121907  +  int iSkip = 0;                     /* End of the sorter insert loop */
120861 121908   
120862 121909     assert( bSeq==0 || bSeq==1 );
       121910  +
       121911  +  /* Three cases:
       121912  +  **   (1) The data to be sorted has already been packed into a Record
       121913  +  **       by a prior OP_MakeRecord.  In this case nData==1 and regData
       121914  +  **       will be completely unrelated to regOrigData.
       121915  +  **   (2) All output columns are included in the sort record.  In that
       121916  +  **       case regData==regOrigData.
       121917  +  **   (3) Some output columns are omitted from the sort record due to
       121918  +  **       the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
       121919  +  **       SQLITE_ECEL_OMITREF optimization, or due to the 
       121920  +  **       SortCtx.pDeferredRowLoad optimiation.  In any of these cases
       121921  +  **       regOrigData is 0 to prevent this routine from trying to copy
       121922  +  **       values that might not yet exist.
       121923  +  */
120863 121924     assert( nData==1 || regData==regOrigData || regOrigData==0 );
       121925  +
120864 121926     if( nPrefixReg ){
120865 121927       assert( nPrefixReg==nExpr+bSeq );
120866         -    regBase = regData - nExpr - bSeq;
       121928  +    regBase = regData - nPrefixReg;
120867 121929     }else{
120868 121930       regBase = pParse->nMem + 1;
120869 121931       pParse->nMem += nBase;
120870 121932     }
120871 121933     assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
120872 121934     iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
120873 121935     pSort->labelDone = sqlite3VdbeMakeLabel(v);
................................................................................
120875 121937                             SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
120876 121938     if( bSeq ){
120877 121939       sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
120878 121940     }
120879 121941     if( nPrefixReg==0 && nData>0 ){
120880 121942       sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
120881 121943     }
120882         -  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord);
120883 121944     if( nOBSat>0 ){
120884 121945       int regPrevKey;   /* The first nOBSat columns of the previous row */
120885 121946       int addrFirst;    /* Address of the OP_IfNot opcode */
120886 121947       int addrJmp;      /* Address of the OP_Jump opcode */
120887 121948       VdbeOp *pOp;      /* Opcode that opens the sorter */
120888 121949       int nKey;         /* Number of sorting key columns, including OP_Sequence */
120889 121950       KeyInfo *pKI;     /* Original KeyInfo on the sorter table */
120890 121951   
       121952  +    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
120891 121953       regPrevKey = pParse->nMem+1;
120892 121954       pParse->nMem += pSort->nOBSat;
120893 121955       nKey = nExpr - pSort->nOBSat + bSeq;
120894 121956       if( bSeq ){
120895 121957         addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr); 
120896 121958       }else{
120897 121959         addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
................................................................................
120917 121979         sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
120918 121980         VdbeCoverage(v);
120919 121981       }
120920 121982       sqlite3VdbeJumpHere(v, addrFirst);
120921 121983       sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
120922 121984       sqlite3VdbeJumpHere(v, addrJmp);
120923 121985     }
       121986  +  if( iLimit ){
       121987  +    /* At this point the values for the new sorter entry are stored
       121988  +    ** in an array of registers. They need to be composed into a record
       121989  +    ** and inserted into the sorter if either (a) there are currently
       121990  +    ** less than LIMIT+OFFSET items or (b) the new record is smaller than 
       121991  +    ** the largest record currently in the sorter. If (b) is true and there
       121992  +    ** are already LIMIT+OFFSET items in the sorter, delete the largest
       121993  +    ** entry before inserting the new one. This way there are never more 
       121994  +    ** than LIMIT+OFFSET items in the sorter.
       121995  +    **
       121996  +    ** If the new record does not need to be inserted into the sorter,
       121997  +    ** jump to the next iteration of the loop. Or, if the
       121998  +    ** pSort->bOrderedInnerLoop flag is set to indicate that the inner
       121999  +    ** loop delivers items in sorted order, jump to the next iteration
       122000  +    ** of the outer loop.
       122001  +    */
       122002  +    int iCsr = pSort->iECursor;
       122003  +    sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
       122004  +    VdbeCoverage(v);
       122005  +    sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
       122006  +    iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
       122007  +                                 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
       122008  +    VdbeCoverage(v);
       122009  +    sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
       122010  +  }
       122011  +  if( regRecord==0 ){
       122012  +    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
       122013  +  }
120924 122014     if( pSort->sortFlags & SORTFLAG_UseSorter ){
120925 122015       op = OP_SorterInsert;
120926 122016     }else{
120927 122017       op = OP_IdxInsert;
120928 122018     }
120929 122019     sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
120930 122020                          regBase+nOBSat, nBase-nOBSat);
120931         -  if( iLimit ){
120932         -    int addr;
120933         -    int r1 = 0;
120934         -    /* Fill the sorter until it contains LIMIT+OFFSET entries.  (The iLimit
120935         -    ** register is initialized with value of LIMIT+OFFSET.)  After the sorter
120936         -    ** fills up, delete the least entry in the sorter after each insert.
120937         -    ** Thus we never hold more than the LIMIT+OFFSET rows in memory at once */
120938         -    addr = sqlite3VdbeAddOp1(v, OP_IfNotZero, iLimit); VdbeCoverage(v);
120939         -    sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor);
120940         -    if( pSort->bOrderedInnerLoop ){
120941         -      r1 = ++pParse->nMem;
120942         -      sqlite3VdbeAddOp3(v, OP_Column, pSort->iECursor, nExpr, r1);
120943         -      VdbeComment((v, "seq"));
120944         -    }
120945         -    sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor);
120946         -    if( pSort->bOrderedInnerLoop ){
120947         -      /* If the inner loop is driven by an index such that values from
120948         -      ** the same iteration of the inner loop are in sorted order, then
120949         -      ** immediately jump to the next iteration of an inner loop if the
120950         -      ** entry from the current iteration does not fit into the top
120951         -      ** LIMIT+OFFSET entries of the sorter. */
120952         -      int iBrk = sqlite3VdbeCurrentAddr(v) + 2;
120953         -      sqlite3VdbeAddOp3(v, OP_Eq, regBase+nExpr, iBrk, r1);
120954         -      sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
120955         -      VdbeCoverage(v);
120956         -    }
120957         -    sqlite3VdbeJumpHere(v, addr);
       122021  +  if( iSkip ){
       122022  +    assert( pSort->bOrderedInnerLoop==0 || pSort->bOrderedInnerLoop==1 );
       122023  +    sqlite3VdbeChangeP2(v, iSkip,
       122024  +         sqlite3VdbeCurrentAddr(v) + pSort->bOrderedInnerLoop);
120958 122025     }
120959 122026   }
120960 122027   
120961 122028   /*
120962 122029   ** Add code to implement the OFFSET
120963 122030   */
120964 122031   static void codeOffset(
................................................................................
120996 122063     sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
120997 122064     sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
120998 122065     sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
120999 122066     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
121000 122067     sqlite3ReleaseTempReg(pParse, r1);
121001 122068   }
121002 122069   
       122070  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122071  +/*
       122072  +** This function is called as part of inner-loop generation for a SELECT
       122073  +** statement with an ORDER BY that is not optimized by an index. It 
       122074  +** determines the expressions, if any, that the sorter-reference 
       122075  +** optimization should be used for. The sorter-reference optimization
       122076  +** is used for SELECT queries like:
       122077  +**
       122078  +**   SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
       122079  +**
       122080  +** If the optimization is used for expression "bigblob", then instead of
       122081  +** storing values read from that column in the sorter records, the PK of
       122082  +** the row from table t1 is stored instead. Then, as records are extracted from
       122083  +** the sorter to return to the user, the required value of bigblob is
       122084  +** retrieved directly from table t1. If the values are very large, this 
       122085  +** can be more efficient than storing them directly in the sorter records.
       122086  +**
       122087  +** The ExprList_item.bSorterRef flag is set for each expression in pEList 
       122088  +** for which the sorter-reference optimization should be enabled. 
       122089  +** Additionally, the pSort->aDefer[] array is populated with entries
       122090  +** for all cursors required to evaluate all selected expressions. Finally.
       122091  +** output variable (*ppExtra) is set to an expression list containing
       122092  +** expressions for all extra PK values that should be stored in the
       122093  +** sorter records.
       122094  +*/
       122095  +static void selectExprDefer(
       122096  +  Parse *pParse,                  /* Leave any error here */
       122097  +  SortCtx *pSort,                 /* Sorter context */
       122098  +  ExprList *pEList,               /* Expressions destined for sorter */
       122099  +  ExprList **ppExtra              /* Expressions to append to sorter record */
       122100  +){
       122101  +  int i;
       122102  +  int nDefer = 0;
       122103  +  ExprList *pExtra = 0;
       122104  +  for(i=0; i<pEList->nExpr; i++){
       122105  +    struct ExprList_item *pItem = &pEList->a[i];
       122106  +    if( pItem->u.x.iOrderByCol==0 ){
       122107  +      Expr *pExpr = pItem->pExpr;
       122108  +      Table *pTab = pExpr->pTab;
       122109  +      if( pExpr->op==TK_COLUMN && pTab && !IsVirtual(pTab)
       122110  +       && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
       122111  +      ){
       122112  +        int j;
       122113  +        for(j=0; j<nDefer; j++){
       122114  +          if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
       122115  +        }
       122116  +        if( j==nDefer ){
       122117  +          if( nDefer==ArraySize(pSort->aDefer) ){
       122118  +            continue;
       122119  +          }else{
       122120  +            int nKey = 1;
       122121  +            int k;
       122122  +            Index *pPk = 0;
       122123  +            if( !HasRowid(pTab) ){
       122124  +              pPk = sqlite3PrimaryKeyIndex(pTab);
       122125  +              nKey = pPk->nKeyCol;
       122126  +            }
       122127  +            for(k=0; k<nKey; k++){
       122128  +              Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
       122129  +              if( pNew ){
       122130  +                pNew->iTable = pExpr->iTable;
       122131  +                pNew->pTab = pExpr->pTab;
       122132  +                pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
       122133  +                pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
       122134  +              }
       122135  +            }
       122136  +            pSort->aDefer[nDefer].pTab = pExpr->pTab;
       122137  +            pSort->aDefer[nDefer].iCsr = pExpr->iTable;
       122138  +            pSort->aDefer[nDefer].nKey = nKey;
       122139  +            nDefer++;
       122140  +          }
       122141  +        }
       122142  +        pItem->bSorterRef = 1;
       122143  +      }
       122144  +    }
       122145  +  }
       122146  +  pSort->nDefer = (u8)nDefer;
       122147  +  *ppExtra = pExtra;
       122148  +}
       122149  +#endif
       122150  +
121003 122151   /*
121004 122152   ** This routine generates the code for the inside of the inner loop
121005 122153   ** of a SELECT.
121006 122154   **
121007 122155   ** If srcTab is negative, then the p->pEList expressions
121008 122156   ** are evaluated in order to get the data for this row.  If srcTab is
121009 122157   ** zero or more, then data is pulled from srcTab and p->pEList is used only 
................................................................................
121022 122170     Vdbe *v = pParse->pVdbe;
121023 122171     int i;
121024 122172     int hasDistinct;            /* True if the DISTINCT keyword is present */
121025 122173     int eDest = pDest->eDest;   /* How to dispose of results */
121026 122174     int iParm = pDest->iSDParm; /* First argument to disposal method */
121027 122175     int nResultCol;             /* Number of result columns */
121028 122176     int nPrefixReg = 0;         /* Number of extra registers before regResult */
       122177  +  RowLoadInfo sRowLoadInfo;   /* Info for deferred row loading */
121029 122178   
121030 122179     /* Usually, regResult is the first cell in an array of memory cells
121031 122180     ** containing the current result row. In this case regOrig is set to the
121032 122181     ** same value. However, if the results are being sent to the sorter, the
121033 122182     ** values for any expressions that are also part of the sort-key are omitted
121034 122183     ** from this array. In this case regOrig is set to zero.  */
121035 122184     int regResult;              /* Start of memory holding current results */
................................................................................
121068 122217     regOrig = regResult = pDest->iSdst;
121069 122218     if( srcTab>=0 ){
121070 122219       for(i=0; i<nResultCol; i++){
121071 122220         sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
121072 122221         VdbeComment((v, "%s", p->pEList->a[i].zName));
121073 122222       }
121074 122223     }else if( eDest!=SRT_Exists ){
       122224  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122225  +    ExprList *pExtra = 0;
       122226  +#endif
121075 122227       /* If the destination is an EXISTS(...) expression, the actual
121076 122228       ** values returned by the SELECT are not required.
121077 122229       */
121078         -    u8 ecelFlags;
       122230  +    u8 ecelFlags;    /* "ecel" is an abbreviation of "ExprCodeExprList" */
       122231  +    ExprList *pEList;
121079 122232       if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
121080 122233         ecelFlags = SQLITE_ECEL_DUP;
121081 122234       }else{
121082 122235         ecelFlags = 0;
121083 122236       }
121084 122237       if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
121085 122238         /* For each expression in p->pEList that is a copy of an expression in
121086 122239         ** the ORDER BY clause (pSort->pOrderBy), set the associated 
121087 122240         ** iOrderByCol value to one more than the index of the ORDER BY 
121088 122241         ** expression within the sort-key that pushOntoSorter() will generate.
121089 122242         ** This allows the p->pEList field to be omitted from the sorted record,
121090 122243         ** saving space and CPU cycles.  */
121091 122244         ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
       122245  +
121092 122246         for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
121093 122247           int j;
121094 122248           if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
121095 122249             p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
121096 122250           }
121097 122251         }
       122252  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122253  +      selectExprDefer(pParse, pSort, p->pEList, &pExtra);
       122254  +      if( pExtra && pParse->db->mallocFailed==0 ){
       122255  +        /* If there are any extra PK columns to add to the sorter records,
       122256  +        ** allocate extra memory cells and adjust the OpenEphemeral 
       122257  +        ** instruction to account for the larger records. This is only
       122258  +        ** required if there are one or more WITHOUT ROWID tables with
       122259  +        ** composite primary keys in the SortCtx.aDefer[] array.  */
       122260  +        VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
       122261  +        pOp->p2 += (pExtra->nExpr - pSort->nDefer);
       122262  +        pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
       122263  +        pParse->nMem += pExtra->nExpr;
       122264  +      }
       122265  +#endif
       122266  +
       122267  +      /* Adjust nResultCol to account for columns that are omitted
       122268  +      ** from the sorter by the optimizations in this branch */
       122269  +      pEList = p->pEList;
       122270  +      for(i=0; i<pEList->nExpr; i++){
       122271  +        if( pEList->a[i].u.x.iOrderByCol>0
       122272  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122273  +         || pEList->a[i].bSorterRef
       122274  +#endif
       122275  +        ){
       122276  +          nResultCol--;
121098 122277         regOrig = 0;
       122278  +        }
       122279  +      }
       122280  +
       122281  +      testcase( regOrig );
       122282  +      testcase( eDest==SRT_Set );
       122283  +      testcase( eDest==SRT_Mem );
       122284  +      testcase( eDest==SRT_Coroutine );
       122285  +      testcase( eDest==SRT_Output );
121099 122286         assert( eDest==SRT_Set || eDest==SRT_Mem 
121100 122287              || eDest==SRT_Coroutine || eDest==SRT_Output );
121101 122288       }
121102         -    nResultCol = sqlite3ExprCodeExprList(pParse,p->pEList,regResult,
121103         -                                         0,ecelFlags);
       122289  +    sRowLoadInfo.regResult = regResult;
       122290  +    sRowLoadInfo.ecelFlags = ecelFlags;
       122291  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122292  +    sRowLoadInfo.pExtra = pExtra;
       122293  +    sRowLoadInfo.regExtraResult = regResult + nResultCol;
       122294  +    if( pExtra ) nResultCol += pExtra->nExpr;
       122295  +#endif
       122296  +    if( p->iLimit
       122297  +     && (ecelFlags & SQLITE_ECEL_OMITREF)!=0 
       122298  +     && nPrefixReg>0
       122299  +    ){
       122300  +      assert( pSort!=0 );
       122301  +      assert( hasDistinct==0 );
       122302  +      pSort->pDeferredRowLoad = &sRowLoadInfo;
       122303  +      regOrig = 0;
       122304  +    }else{
       122305  +      innerLoopLoadRow(pParse, p, &sRowLoadInfo);
       122306  +    }
121104 122307     }
121105 122308   
121106 122309     /* If the DISTINCT keyword was present on the SELECT statement
121107 122310     ** and this row has been seen before, then do not make this row
121108 122311     ** part of the result.
121109 122312     */
121110 122313     if( hasDistinct ){
................................................................................
121212 122415           sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
121213 122416           VdbeCoverage(v);
121214 122417           sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
121215 122418           assert( pSort==0 );
121216 122419         }
121217 122420   #endif
121218 122421         if( pSort ){
121219         -        pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg);
       122422  +        assert( regResult==regOrig );
       122423  +        pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
121220 122424         }else{
121221 122425           int r2 = sqlite3GetTempReg(pParse);
121222 122426           sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
121223 122427           sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
121224 122428           sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
121225 122429           sqlite3ReleaseTempReg(pParse, r2);
121226 122430         }
................................................................................
121479 122683   **
121480 122684   **   "USE TEMP B-TREE FOR xxx"
121481 122685   **
121482 122686   ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
121483 122687   ** is determined by the zUsage argument.
121484 122688   */
121485 122689   static void explainTempTable(Parse *pParse, const char *zUsage){
121486         -  if( pParse->explain==2 ){
121487         -    Vdbe *v = pParse->pVdbe;
121488         -    char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
121489         -    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
121490         -  }
       122690  +  ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
121491 122691   }
121492 122692   
121493 122693   /*
121494 122694   ** Assign expression b to lvalue a. A second, no-op, version of this macro
121495 122695   ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
121496 122696   ** in sqlite3Select() to assign values to structure member variables that
121497 122697   ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
................................................................................
121501 122701   
121502 122702   #else
121503 122703   /* No-op versions of the explainXXX() functions and macros. */
121504 122704   # define explainTempTable(y,z)
121505 122705   # define explainSetInteger(y,z)
121506 122706   #endif
121507 122707   
121508         -#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
121509         -/*
121510         -** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
121511         -** is a no-op. Otherwise, it adds a single row of output to the EQP result,
121512         -** where the caption is of one of the two forms:
121513         -**
121514         -**   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
121515         -**   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
121516         -**
121517         -** where iSub1 and iSub2 are the integers passed as the corresponding
121518         -** function parameters, and op is the text representation of the parameter
121519         -** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
121520         -** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is 
121521         -** false, or the second form if it is true.
121522         -*/
121523         -static void explainComposite(
121524         -  Parse *pParse,                  /* Parse context */
121525         -  int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
121526         -  int iSub1,                      /* Subquery id 1 */
121527         -  int iSub2,                      /* Subquery id 2 */
121528         -  int bUseTmp                     /* True if a temp table was used */
121529         -){
121530         -  assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
121531         -  if( pParse->explain==2 ){
121532         -    Vdbe *v = pParse->pVdbe;
121533         -    char *zMsg = sqlite3MPrintf(
121534         -        pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
121535         -        bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
121536         -    );
121537         -    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
121538         -  }
121539         -}
121540         -#else
121541         -/* No-op versions of the explainXXX() functions and macros. */
121542         -# define explainComposite(v,w,x,y,z)
121543         -#endif
121544 122708   
121545 122709   /*
121546 122710   ** If the inner loop was generated using a non-null pOrderBy argument,
121547 122711   ** then the results were placed in a sorter.  After the loop is terminated
121548 122712   ** we need to run the sorter and output the results.  The following
121549 122713   ** routine generates the code needed to do that.
121550 122714   */
................................................................................
121554 122718     SortCtx *pSort,   /* Information on the ORDER BY clause */
121555 122719     int nColumn,      /* Number of columns of data */
121556 122720     SelectDest *pDest /* Write the sorted results here */
121557 122721   ){
121558 122722     Vdbe *v = pParse->pVdbe;                     /* The prepared statement */
121559 122723     int addrBreak = pSort->labelDone;            /* Jump here to exit loop */
121560 122724     int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
121561         -  int addr;
       122725  +  int addr;                       /* Top of output loop. Jump for Next. */
121562 122726     int addrOnce = 0;
121563 122727     int iTab;
121564 122728     ExprList *pOrderBy = pSort->pOrderBy;
121565 122729     int eDest = pDest->eDest;
121566 122730     int iParm = pDest->iSDParm;
121567 122731     int regRow;
121568 122732     int regRowid;
121569 122733     int iCol;
121570         -  int nKey;
       122734  +  int nKey;                       /* Number of key columns in sorter record */
121571 122735     int iSortTab;                   /* Sorter cursor to read from */
121572         -  int nSortData;                  /* Trailing values to read from sorter */
121573 122736     int i;
121574 122737     int bSeq;                       /* True if sorter record includes seq. no. */
       122738  +  int nRefKey = 0;
121575 122739     struct ExprList_item *aOutEx = p->pEList->a;
121576 122740   
121577 122741     assert( addrBreak<0 );
121578 122742     if( pSort->labelBkOut ){
121579 122743       sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
121580 122744       sqlite3VdbeGoto(v, addrBreak);
121581 122745       sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
121582 122746     }
       122747  +
       122748  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122749  +  /* Open any cursors needed for sorter-reference expressions */
       122750  +  for(i=0; i<pSort->nDefer; i++){
       122751  +    Table *pTab = pSort->aDefer[i].pTab;
       122752  +    int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       122753  +    sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
       122754  +    nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
       122755  +  }
       122756  +#endif
       122757  +
121583 122758     iTab = pSort->iECursor;
121584 122759     if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
121585 122760       regRowid = 0;
121586 122761       regRow = pDest->iSdst;
121587         -    nSortData = nColumn;
121588 122762     }else{
121589 122763       regRowid = sqlite3GetTempReg(pParse);
121590 122764       regRow = sqlite3GetTempRange(pParse, nColumn);
121591         -    nSortData = nColumn;
121592 122765     }
121593 122766     nKey = pOrderBy->nExpr - pSort->nOBSat;
121594 122767     if( pSort->sortFlags & SORTFLAG_UseSorter ){
121595 122768       int regSortOut = ++pParse->nMem;
121596 122769       iSortTab = pParse->nTab++;
121597 122770       if( pSort->labelBkOut ){
121598 122771         addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
121599 122772       }
121600         -    sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData);
       122773  +    sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, 
       122774  +        nKey+1+nColumn+nRefKey);
121601 122775       if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
121602 122776       addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
121603 122777       VdbeCoverage(v);
121604 122778       codeOffset(v, p->iOffset, addrContinue);
121605 122779       sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
121606 122780       bSeq = 0;
121607 122781     }else{
121608 122782       addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
121609 122783       codeOffset(v, p->iOffset, addrContinue);
121610 122784       iSortTab = iTab;
121611 122785       bSeq = 1;
121612 122786     }
121613         -  for(i=0, iCol=nKey+bSeq-1; i<nSortData; i++){
       122787  +  for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
       122788  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122789  +    if( aOutEx[i].bSorterRef ) continue;
       122790  +#endif
121614 122791       if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
121615 122792     }
121616         -  for(i=nSortData-1; i>=0; i--){
       122793  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122794  +  if( pSort->nDefer ){
       122795  +    int iKey = iCol+1;
       122796  +    int regKey = sqlite3GetTempRange(pParse, nRefKey);
       122797  +
       122798  +    for(i=0; i<pSort->nDefer; i++){
       122799  +      int iCsr = pSort->aDefer[i].iCsr;
       122800  +      Table *pTab = pSort->aDefer[i].pTab;
       122801  +      int nKey = pSort->aDefer[i].nKey;
       122802  +
       122803  +      sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
       122804  +      if( HasRowid(pTab) ){
       122805  +        sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
       122806  +        sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr, 
       122807  +            sqlite3VdbeCurrentAddr(v)+1, regKey);
       122808  +      }else{
       122809  +        int k;
       122810  +        int iJmp;
       122811  +        assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
       122812  +        for(k=0; k<nKey; k++){
       122813  +          sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
       122814  +        }
       122815  +        iJmp = sqlite3VdbeCurrentAddr(v);
       122816  +        sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
       122817  +        sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
       122818  +        sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
       122819  +      }
       122820  +    }
       122821  +    sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
       122822  +  }
       122823  +#endif
       122824  +  for(i=nColumn-1; i>=0; i--){
       122825  +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
       122826  +    if( aOutEx[i].bSorterRef ){
       122827  +      sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
       122828  +    }else
       122829  +#endif
       122830  +    {
121617 122831       int iRead;
121618 122832       if( aOutEx[i].u.x.iOrderByCol ){
121619 122833         iRead = aOutEx[i].u.x.iOrderByCol-1;
121620 122834       }else{
121621 122835         iRead = iCol--;
121622 122836       }
121623 122837       sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
121624 122838       VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
121625 122839     }
       122840  +  }
121626 122841     switch( eDest ){
121627 122842       case SRT_Table:
121628 122843       case SRT_EphemTab: {
121629 122844         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
121630 122845         sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
121631 122846         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
121632 122847         break;
................................................................................
121935 123150   #ifndef SQLITE_OMIT_EXPLAIN
121936 123151     /* If this is an EXPLAIN, skip this step */
121937 123152     if( pParse->explain ){
121938 123153       return;
121939 123154     }
121940 123155   #endif
121941 123156   
121942         -  if( pParse->colNamesSet || db->mallocFailed ) return;
       123157  +  if( pParse->colNamesSet ) return;
121943 123158     /* Column names are determined by the left-most term of a compound select */
121944 123159     while( pSelect->pPrior ) pSelect = pSelect->pPrior;
121945 123160     SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
121946 123161     pTabList = pSelect->pSrc;
121947 123162     pEList = pSelect->pEList;
121948 123163     assert( v!=0 );
121949 123164     assert( pTabList!=0 );
................................................................................
122462 123677     }
122463 123678   
122464 123679     /* Detach the ORDER BY clause from the compound SELECT */
122465 123680     p->pOrderBy = 0;
122466 123681   
122467 123682     /* Store the results of the setup-query in Queue. */
122468 123683     pSetup->pNext = 0;
       123684  +  ExplainQueryPlan((pParse, 1, "SETUP"));
122469 123685     rc = sqlite3Select(pParse, pSetup, &destQueue);
122470 123686     pSetup->pNext = p;
122471 123687     if( rc ) goto end_of_recursive_query;
122472 123688   
122473 123689     /* Find the next row in the Queue and output that row */
122474 123690     addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
122475 123691   
................................................................................
122496 123712     /* Execute the recursive SELECT taking the single row in Current as
122497 123713     ** the value for the recursive-table. Store the results in the Queue.
122498 123714     */
122499 123715     if( p->selFlags & SF_Aggregate ){
122500 123716       sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
122501 123717     }else{
122502 123718       p->pPrior = 0;
       123719  +    ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
122503 123720       sqlite3Select(pParse, p, &destQueue);
122504 123721       assert( p->pPrior==0 );
122505 123722       p->pPrior = pSetup;
122506 123723     }
122507 123724   
122508 123725     /* Keep running the loop until the Queue is empty */
122509 123726     sqlite3VdbeGoto(v, addrTop);
................................................................................
122541 123758   ** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
122542 123759   */
122543 123760   static int multiSelectValues(
122544 123761     Parse *pParse,        /* Parsing context */
122545 123762     Select *p,            /* The right-most of SELECTs to be coded */
122546 123763     SelectDest *pDest     /* What to do with query results */
122547 123764   ){
122548         -  Select *pPrior;
122549         -  Select *pRightmost = p;
122550 123765     int nRow = 1;
122551 123766     int rc = 0;
       123767  +  int bShowAll = p->pLimit==0;
122552 123768     assert( p->selFlags & SF_MultiValue );
122553 123769     do{
122554 123770       assert( p->selFlags & SF_Values );
122555 123771       assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
122556 123772       assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
122557 123773       if( p->pPrior==0 ) break;
122558 123774       assert( p->pPrior->pNext==p );
122559 123775       p = p->pPrior;
122560         -    nRow++;
       123776  +    nRow += bShowAll;
122561 123777     }while(1);
       123778  +  ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
       123779  +                    nRow==1 ? "" : "S"));
122562 123780     while( p ){
122563         -    pPrior = p->pPrior;
122564         -    p->pPrior = 0;
122565         -    rc = sqlite3Select(pParse, p, pDest);
122566         -    p->pPrior = pPrior;
122567         -    if( rc || pRightmost->pLimit ) break;
       123781  +    selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
       123782  +    if( !bShowAll ) break;
122568 123783       p->nSelectRow = nRow;
122569 123784       p = p->pNext;
122570 123785     }
122571 123786     return rc;
122572 123787   }
122573 123788   
122574 123789   /*
................................................................................
122609 123824   ){
122610 123825     int rc = SQLITE_OK;   /* Success code from a subroutine */
122611 123826     Select *pPrior;       /* Another SELECT immediately to our left */
122612 123827     Vdbe *v;              /* Generate code to this VDBE */
122613 123828     SelectDest dest;      /* Alternative data destination */
122614 123829     Select *pDelete = 0;  /* Chain of simple selects to delete */
122615 123830     sqlite3 *db;          /* Database connection */
122616         -#ifndef SQLITE_OMIT_EXPLAIN
122617         -  int iSub1 = 0;        /* EQP id of left-hand query */
122618         -  int iSub2 = 0;        /* EQP id of right-hand query */
122619         -#endif
122620 123831   
122621 123832     /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
122622 123833     ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
122623 123834     */
122624 123835     assert( p && p->pPrior );  /* Calling function guarantees this much */
122625 123836     assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
122626 123837     db = pParse->db;
................................................................................
122663 123874     }else
122664 123875   #endif
122665 123876   
122666 123877     /* Compound SELECTs that have an ORDER BY clause are handled separately.
122667 123878     */
122668 123879     if( p->pOrderBy ){
122669 123880       return multiSelectOrderBy(pParse, p, pDest);
122670         -  }else
       123881  +  }else{
       123882  +
       123883  +#ifndef SQLITE_OMIT_EXPLAIN
       123884  +    if( pPrior->pPrior==0 ){
       123885  +      ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
       123886  +      ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
       123887  +    }
       123888  +#endif
122671 123889   
122672 123890     /* Generate code for the left and right SELECT statements.
122673 123891     */
122674 123892     switch( p->op ){
122675 123893       case TK_ALL: {
122676 123894         int addr = 0;
122677 123895         int nLimit;
122678 123896         assert( !pPrior->pLimit );
122679 123897         pPrior->iLimit = p->iLimit;
122680 123898         pPrior->iOffset = p->iOffset;
122681 123899         pPrior->pLimit = p->pLimit;
122682         -      explainSetInteger(iSub1, pParse->iNextSelectId);
122683 123900         rc = sqlite3Select(pParse, pPrior, &dest);
122684 123901         p->pLimit = 0;
122685 123902         if( rc ){
122686 123903           goto multi_select_end;
122687 123904         }
122688 123905         p->pPrior = 0;
122689 123906         p->iLimit = pPrior->iLimit;
................................................................................
122692 123909           addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
122693 123910           VdbeComment((v, "Jump ahead if LIMIT reached"));
122694 123911           if( p->iOffset ){
122695 123912             sqlite3VdbeAddOp3(v, OP_OffsetLimit,
122696 123913                               p->iLimit, p->iOffset+1, p->iOffset);
122697 123914           }
122698 123915         }
122699         -      explainSetInteger(iSub2, pParse->iNextSelectId);
       123916  +        ExplainQueryPlan((pParse, 1, "UNION ALL"));
122700 123917         rc = sqlite3Select(pParse, p, &dest);
122701 123918         testcase( rc!=SQLITE_OK );
122702 123919         pDelete = p->pPrior;
122703 123920         p->pPrior = pPrior;
122704 123921         p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
122705 123922         if( pPrior->pLimit
122706 123923          && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
................................................................................
122711 123928         if( addr ){
122712 123929           sqlite3VdbeJumpHere(v, addr);
122713 123930         }
122714 123931         break;
122715 123932       }
122716 123933       case TK_EXCEPT:
122717 123934       case TK_UNION: {
122718         -      int unionTab;    /* Cursor number of the temporary table holding result */
       123935  +        int unionTab;    /* Cursor number of the temp table holding result */
122719 123936         u8 op = 0;       /* One of the SRT_ operations to apply to self */
122720 123937         int priorOp;     /* The SRT_ operation to apply to prior selects */
122721 123938         Expr *pLimit;    /* Saved values of p->nLimit  */
122722 123939         int addr;
122723 123940         SelectDest uniondest;
122724 123941   
122725 123942         testcase( p->op==TK_EXCEPT );
................................................................................
122744 123961           assert( p->pEList );
122745 123962         }
122746 123963   
122747 123964         /* Code the SELECT statements to our left
122748 123965         */
122749 123966         assert( !pPrior->pOrderBy );
122750 123967         sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
122751         -      explainSetInteger(iSub1, pParse->iNextSelectId);
122752 123968         rc = sqlite3Select(pParse, pPrior, &uniondest);
122753 123969         if( rc ){
122754 123970           goto multi_select_end;
122755 123971         }
122756 123972   
122757 123973         /* Code the current SELECT statement
122758 123974         */
................................................................................
122762 123978           assert( p->op==TK_UNION );
122763 123979           op = SRT_Union;
122764 123980         }
122765 123981         p->pPrior = 0;
122766 123982         pLimit = p->pLimit;
122767 123983         p->pLimit = 0;
122768 123984         uniondest.eDest = op;
122769         -      explainSetInteger(iSub2, pParse->iNextSelectId);
       123985  +        ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
       123986  +                          selectOpName(p->op)));
122770 123987         rc = sqlite3Select(pParse, p, &uniondest);
122771 123988         testcase( rc!=SQLITE_OK );
122772 123989         /* Query flattening in sqlite3Select() might refill p->pOrderBy.
122773 123990         ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
122774 123991         sqlite3ExprListDelete(db, p->pOrderBy);
122775 123992         pDelete = p->pPrior;
122776 123993         p->pPrior = pPrior;
................................................................................
122825 124042         p->addrOpenEphm[0] = addr;
122826 124043         findRightmost(p)->selFlags |= SF_UsesEphemeral;
122827 124044         assert( p->pEList );
122828 124045   
122829 124046         /* Code the SELECTs to our left into temporary table "tab1".
122830 124047         */
122831 124048         sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
122832         -      explainSetInteger(iSub1, pParse->iNextSelectId);
122833 124049         rc = sqlite3Select(pParse, pPrior, &intersectdest);
122834 124050         if( rc ){
122835 124051           goto multi_select_end;
122836 124052         }
122837 124053   
122838 124054         /* Code the current SELECT into temporary table "tab2"
122839 124055         */
................................................................................
122840 124056         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
122841 124057         assert( p->addrOpenEphm[1] == -1 );
122842 124058         p->addrOpenEphm[1] = addr;
122843 124059         p->pPrior = 0;
122844 124060         pLimit = p->pLimit;
122845 124061         p->pLimit = 0;
122846 124062         intersectdest.iSDParm = tab2;
122847         -      explainSetInteger(iSub2, pParse->iNextSelectId);
       124063  +        ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
       124064  +                          selectOpName(p->op)));
122848 124065         rc = sqlite3Select(pParse, p, &intersectdest);
122849 124066         testcase( rc!=SQLITE_OK );
122850 124067         pDelete = p->pPrior;
122851 124068         p->pPrior = pPrior;
122852         -      if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
       124069  +        if( p->nSelectRow>pPrior->nSelectRow ){
       124070  +          p->nSelectRow = pPrior->nSelectRow;
       124071  +        }
122853 124072         sqlite3ExprDelete(db, p->pLimit);
122854 124073         p->pLimit = pLimit;
122855 124074   
122856 124075         /* Generate code to take the intersection of the two temporary
122857 124076         ** tables.
122858 124077         */
122859 124078         assert( p->pEList );
122860 124079         iBreak = sqlite3VdbeMakeLabel(v);
122861 124080         iCont = sqlite3VdbeMakeLabel(v);
122862 124081         computeLimitRegisters(pParse, p, iBreak);
122863 124082         sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
122864 124083         r1 = sqlite3GetTempReg(pParse);
122865 124084         iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
122866         -      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
       124085  +        sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
       124086  +        VdbeCoverage(v);
122867 124087         sqlite3ReleaseTempReg(pParse, r1);
122868 124088         selectInnerLoop(pParse, p, tab1,
122869 124089                         0, 0, &dest, iCont, iBreak);
122870 124090         sqlite3VdbeResolveLabel(v, iCont);
122871 124091         sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
122872 124092         sqlite3VdbeResolveLabel(v, iBreak);
122873 124093         sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
122874 124094         sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
122875 124095         break;
122876 124096       }
122877 124097     }
122878 124098   
122879         -  explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
       124099  +  #ifndef SQLITE_OMIT_EXPLAIN
       124100  +    if( p->pNext==0 ){
       124101  +      ExplainQueryPlanPop(pParse);
       124102  +    }
       124103  +  #endif
       124104  +  }
122880 124105   
122881 124106     /* Compute collating sequences used by 
122882 124107     ** temporary tables needed to implement the compound select.
122883 124108     ** Attach the KeyInfo structure to all temporary tables.
122884 124109     **
122885 124110     ** This section is run by the right-most SELECT statement only.
122886 124111     ** SELECT statements to the left always skip this part.  The right-most
................................................................................
123211 124436     int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
123212 124437     KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
123213 124438     KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
123214 124439     sqlite3 *db;          /* Database connection */
123215 124440     ExprList *pOrderBy;   /* The ORDER BY clause */
123216 124441     int nOrderBy;         /* Number of terms in the ORDER BY clause */
123217 124442     int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
123218         -#ifndef SQLITE_OMIT_EXPLAIN
123219         -  int iSub1;            /* EQP id of left-hand query */
123220         -  int iSub2;            /* EQP id of right-hand query */
123221         -#endif
123222 124443   
123223 124444     assert( p->pOrderBy!=0 );
123224 124445     assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
123225 124446     db = pParse->db;
123226 124447     v = pParse->pVdbe;
123227 124448     assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
123228 124449     labelEnd = sqlite3VdbeMakeLabel(v);
................................................................................
123334 124555     regAddrA = ++pParse->nMem;
123335 124556     regAddrB = ++pParse->nMem;
123336 124557     regOutA = ++pParse->nMem;
123337 124558     regOutB = ++pParse->nMem;
123338 124559     sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
123339 124560     sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
123340 124561   
       124562  +  ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
       124563  +
123341 124564     /* Generate a coroutine to evaluate the SELECT statement to the
123342 124565     ** left of the compound operator - the "A" select.
123343 124566     */
123344 124567     addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
123345 124568     addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
123346 124569     VdbeComment((v, "left SELECT"));
123347 124570     pPrior->iLimit = regLimitA;
123348         -  explainSetInteger(iSub1, pParse->iNextSelectId);
       124571  +  ExplainQueryPlan((pParse, 1, "LEFT"));
123349 124572     sqlite3Select(pParse, pPrior, &destA);
123350 124573     sqlite3VdbeEndCoroutine(v, regAddrA);
123351 124574     sqlite3VdbeJumpHere(v, addr1);
123352 124575   
123353 124576     /* Generate a coroutine to evaluate the SELECT statement on 
123354 124577     ** the right - the "B" select
123355 124578     */
................................................................................
123356 124579     addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
123357 124580     addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
123358 124581     VdbeComment((v, "right SELECT"));
123359 124582     savedLimit = p->iLimit;
123360 124583     savedOffset = p->iOffset;
123361 124584     p->iLimit = regLimitB;
123362 124585     p->iOffset = 0;  
123363         -  explainSetInteger(iSub2, pParse->iNextSelectId);
       124586  +  ExplainQueryPlan((pParse, 1, "RIGHT"));
123364 124587     sqlite3Select(pParse, p, &destB);
123365 124588     p->iLimit = savedLimit;
123366 124589     p->iOffset = savedOffset;
123367 124590     sqlite3VdbeEndCoroutine(v, regAddrB);
123368 124591   
123369 124592     /* Generate a subroutine that outputs the current row of the A
123370 124593     ** select as the next output row of the compound select.
................................................................................
123468 124691       sqlite3SelectDelete(db, p->pPrior);
123469 124692     }
123470 124693     p->pPrior = pPrior;
123471 124694     pPrior->pNext = p;
123472 124695   
123473 124696     /*** TBD:  Insert subroutine calls to close cursors on incomplete
123474 124697     **** subqueries ****/
123475         -  explainComposite(pParse, p->op, iSub1, iSub2, 0);
       124698  +  ExplainQueryPlanPop(pParse);
123476 124699     return pParse->nErr!=0;
123477 124700   }
123478 124701   #endif
123479 124702   
123480 124703   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
123481 124704   
123482 124705   /* An instance of the SubstContext object describes an substitution edit
................................................................................
123955 125178       if( pNew==0 ){
123956 125179         p->pPrior = pPrior;
123957 125180       }else{
123958 125181         pNew->pPrior = pPrior;
123959 125182         if( pPrior ) pPrior->pNext = pNew;
123960 125183         pNew->pNext = p;
123961 125184         p->pPrior = pNew;
123962         -      SELECTTRACE(2,pParse,p,
123963         -         ("compound-subquery flattener creates %s.%p as peer\n",
123964         -         pNew->zSelName, pNew));
       125185  +      SELECTTRACE(2,pParse,p,("compound-subquery flattener"
       125186  +                              " creates %s.%p as peer\n",pNew->zSelName, pNew));
123965 125187       }
123966 125188       if( db->mallocFailed ) return 1;
123967 125189     }
123968 125190   
123969 125191     /* Begin flattening the iFrom-th entry of the FROM clause 
123970 125192     ** in the outer query.
123971 125193     */
................................................................................
125257 126479   static void explainSimpleCount(
125258 126480     Parse *pParse,                  /* Parse context */
125259 126481     Table *pTab,                    /* Table being queried */
125260 126482     Index *pIdx                     /* Index used to optimize scan, or NULL */
125261 126483   ){
125262 126484     if( pParse->explain==2 ){
125263 126485       int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
125264         -    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
       126486  +    sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
125265 126487           pTab->zName,
125266 126488           bCover ? " USING COVERING INDEX " : "",
125267 126489           bCover ? pIdx->zName : ""
125268 126490       );
125269         -    sqlite3VdbeAddOp4(
125270         -        pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
125271         -    );
125272 126491     }
125273 126492   }
125274 126493   #else
125275 126494   # define explainSimpleCount(a,b,c)
125276 126495   #endif
125277 126496   
125278 126497   /*
................................................................................
125477 126696     SortCtx sSort;         /* Info on how to code the ORDER BY clause */
125478 126697     AggInfo sAggInfo;      /* Information used by aggregate queries */
125479 126698     int iEnd;              /* Address of the end of the query */
125480 126699     sqlite3 *db;           /* The database connection */
125481 126700     ExprList *pMinMaxOrderBy = 0;  /* Added ORDER BY for min/max queries */
125482 126701     u8 minMaxFlag;                 /* Flag for min/max queries */
125483 126702   
125484         -#ifndef SQLITE_OMIT_EXPLAIN
125485         -  int iRestoreSelectId = pParse->iSelectId;
125486         -  pParse->iSelectId = pParse->iNextSelectId++;
125487         -#endif
125488         -
125489 126703     db = pParse->db;
       126704  +  v = sqlite3GetVdbe(pParse);
125490 126705     if( p==0 || db->mallocFailed || pParse->nErr ){
125491 126706       return 1;
125492 126707     }
125493 126708     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
125494 126709     memset(&sAggInfo, 0, sizeof(sAggInfo));
125495 126710   #if SELECTTRACE_ENABLED
125496         -  SELECTTRACE(1,pParse,p, ("begin processing:\n"));
       126711  +  SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
125497 126712     if( sqlite3SelectTrace & 0x100 ){
125498 126713       sqlite3TreeViewSelect(0, p, 0);
125499 126714     }
125500 126715   #endif
125501 126716   
125502 126717     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
125503 126718     assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
................................................................................
125520 126735     pTabList = p->pSrc;
125521 126736     if( pParse->nErr || db->mallocFailed ){
125522 126737       goto select_end;
125523 126738     }
125524 126739     assert( p->pEList!=0 );
125525 126740     isAgg = (p->selFlags & SF_Aggregate)!=0;
125526 126741   #if SELECTTRACE_ENABLED
125527         -  if( sqlite3SelectTrace & 0x100 ){
125528         -    SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
       126742  +  if( sqlite3SelectTrace & 0x104 ){
       126743  +    SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
125529 126744       sqlite3TreeViewSelect(0, p, 0);
125530 126745     }
125531 126746   #endif
125532 126747   
125533         -  /* Get a pointer the VDBE under construction, allocating a new VDBE if one
125534         -  ** does not already exist */
125535         -  v = sqlite3GetVdbe(pParse);
125536         -  if( v==0 ) goto select_end;
125537 126748     if( pDest->eDest==SRT_Output ){
125538 126749       generateColumnNames(pParse, p);
125539 126750     }
125540 126751   
125541 126752     /* Try to various optimizations (flattening subqueries, and strength
125542 126753     ** reduction of join operators) in the FROM clause up into the main query
125543 126754     */
................................................................................
125622 126833   
125623 126834   #ifndef SQLITE_OMIT_COMPOUND_SELECT
125624 126835     /* Handle compound SELECT statements using the separate multiSelect()
125625 126836     ** procedure.
125626 126837     */
125627 126838     if( p->pPrior ){
125628 126839       rc = multiSelect(pParse, p, pDest);
125629         -    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
125630 126840   #if SELECTTRACE_ENABLED
125631         -    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
       126841  +    SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
       126842  +    if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
       126843  +      sqlite3TreeViewSelect(0, p, 0);
       126844  +    }
125632 126845   #endif
       126846  +    if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
125633 126847       return rc;
125634 126848     }
125635 126849   #endif
125636 126850   
125637 126851     /* For each term in the FROM clause, do two things:
125638 126852     ** (1) Authorized unreferenced tables
125639 126853     ** (2) Generate code for all sub-queries
................................................................................
125737 126951         int addrTop = sqlite3VdbeCurrentAddr(v)+1;
125738 126952        
125739 126953         pItem->regReturn = ++pParse->nMem;
125740 126954         sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
125741 126955         VdbeComment((v, "%s", pItem->pTab->zName));
125742 126956         pItem->addrFillSub = addrTop;
125743 126957         sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
125744         -      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
       126958  +      ExplainQueryPlan((pParse, 1, "CO-ROUTINE 0x%p", pSub));
125745 126959         sqlite3Select(pParse, pSub, &dest);
125746 126960         pItem->pTab->nRowLogEst = pSub->nSelectRow;
125747 126961         pItem->fg.viaCoroutine = 1;
125748 126962         pItem->regResult = dest.iSdst;
125749 126963         sqlite3VdbeEndCoroutine(v, pItem->regReturn);
125750 126964         sqlite3VdbeJumpHere(v, addrTop-1);
125751 126965         sqlite3ClearTempRegCache(pParse);
................................................................................
125772 126986           VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
125773 126987         }else{
125774 126988           VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
125775 126989         }
125776 126990         pPrior = isSelfJoinView(pTabList, pItem);
125777 126991         if( pPrior ){
125778 126992           sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
125779         -        explainSetInteger(pItem->iSelectId, pPrior->iSelectId);
125780 126993           assert( pPrior->pSelect!=0 );
125781 126994           pSub->nSelectRow = pPrior->pSelect->nSelectRow;
125782 126995         }else{
125783 126996           sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
125784         -        explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
       126997  +        ExplainQueryPlan((pParse, 1, "MATERIALIZE 0x%p", pSub));
125785 126998           sqlite3Select(pParse, pSub, &dest);
125786 126999         }
125787 127000         pItem->pTab->nRowLogEst = pSub->nSelectRow;
125788 127001         if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
125789 127002         retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
125790 127003         VdbeComment((v, "end %s", pItem->pTab->zName));
125791 127004         sqlite3VdbeChangeP1(v, topAddr, retAddr);
................................................................................
126004 127217       /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
126005 127218       ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
126006 127219       ** SELECT statement.
126007 127220       */
126008 127221       memset(&sNC, 0, sizeof(sNC));
126009 127222       sNC.pParse = pParse;
126010 127223       sNC.pSrcList = pTabList;
126011         -    sNC.pAggInfo = &sAggInfo;
       127224  +    sNC.uNC.pAggInfo = &sAggInfo;
       127225  +    VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
126012 127226       sAggInfo.mnReg = pParse->nMem+1;
126013 127227       sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
126014 127228       sAggInfo.pGroupBy = pGroupBy;
126015 127229       sqlite3ExprAnalyzeAggList(&sNC, pEList);
126016 127230       sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
126017 127231       if( pHaving ){
126018 127232         if( pGroupBy ){
................................................................................
126393 127607   
126394 127608     /* If there is an ORDER BY clause, then we need to sort the results
126395 127609     ** and send them to the callback one by one.
126396 127610     */
126397 127611     if( sSort.pOrderBy ){
126398 127612       explainTempTable(pParse,
126399 127613                        sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
       127614  +    assert( p->pEList==pEList );
126400 127615       generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
126401 127616     }
126402 127617   
126403 127618     /* Jump here to skip this query
126404 127619     */
126405 127620     sqlite3VdbeResolveLabel(v, iEnd);
126406 127621   
................................................................................
126408 127623     ** set the return code to 1. Otherwise 0. */
126409 127624     rc = (pParse->nErr>0);
126410 127625   
126411 127626     /* Control jumps to here if an error is encountered above, or upon
126412 127627     ** successful coding of the SELECT.
126413 127628     */
126414 127629   select_end:
126415         -  explainSetInteger(pParse->iSelectId, iRestoreSelectId);
126416 127630     sqlite3ExprListDelete(db, pMinMaxOrderBy);
126417 127631     sqlite3DbFree(db, sAggInfo.aCol);
126418 127632     sqlite3DbFree(db, sAggInfo.aFunc);
126419 127633   #if SELECTTRACE_ENABLED
126420         -  SELECTTRACE(1,pParse,p,("end processing\n"));
       127634  +  SELECTTRACE(0x1,pParse,p,("end processing\n"));
       127635  +  if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
       127636  +    sqlite3TreeViewSelect(0, p, 0);
       127637  +  }
126421 127638   #endif
       127639  +  ExplainQueryPlanPop(pParse);
126422 127640     return rc;
126423 127641   }
126424 127642   
126425 127643   /************** End of select.c **********************************************/
126426 127644   /************** Begin file table.c *******************************************/
126427 127645   /*
126428 127646   ** 2001 September 15
................................................................................
126648 127866       TriggerStep * pTmp = pTriggerStep;
126649 127867       pTriggerStep = pTriggerStep->pNext;
126650 127868   
126651 127869       sqlite3ExprDelete(db, pTmp->pWhere);
126652 127870       sqlite3ExprListDelete(db, pTmp->pExprList);
126653 127871       sqlite3SelectDelete(db, pTmp->pSelect);
126654 127872       sqlite3IdListDelete(db, pTmp->pIdList);
       127873  +    sqlite3UpsertDelete(db, pTmp->pUpsert);
126655 127874       sqlite3DbFree(db, pTmp->zSpan);
126656 127875   
126657 127876       sqlite3DbFree(db, pTmp);
126658 127877     }
126659 127878   }
126660 127879   
126661 127880   /*
................................................................................
127039 128258   */
127040 128259   SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
127041 128260     sqlite3 *db,        /* The database connection */
127042 128261     Token *pTableName,  /* Name of the table into which we insert */
127043 128262     IdList *pColumn,    /* List of columns in pTableName to insert into */
127044 128263     Select *pSelect,    /* A SELECT statement that supplies values */
127045 128264     u8 orconf,          /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
       128265  +  Upsert *pUpsert,    /* ON CONFLICT clauses for upsert */
127046 128266     const char *zStart, /* Start of SQL text */
127047 128267     const char *zEnd    /* End of SQL text */
127048 128268   ){
127049 128269     TriggerStep *pTriggerStep;
127050 128270   
127051 128271     assert(pSelect != 0 || db->mallocFailed);
127052 128272   
127053 128273     pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName, zStart, zEnd);
127054 128274     if( pTriggerStep ){
127055 128275       pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
127056 128276       pTriggerStep->pIdList = pColumn;
       128277  +    pTriggerStep->pUpsert = pUpsert;
127057 128278       pTriggerStep->orconf = orconf;
127058 128279     }else{
       128280  +    testcase( pColumn );
127059 128281       sqlite3IdListDelete(db, pColumn);
       128282  +    testcase( pUpsert );
       128283  +    sqlite3UpsertDelete(db, pUpsert);
127060 128284     }
127061 128285     sqlite3SelectDelete(db, pSelect);
127062 128286   
127063 128287     return pTriggerStep;
127064 128288   }
127065 128289   
127066 128290   /*
................................................................................
127369 128593   
127370 128594       switch( pStep->op ){
127371 128595         case TK_UPDATE: {
127372 128596           sqlite3Update(pParse, 
127373 128597             targetSrcList(pParse, pStep),
127374 128598             sqlite3ExprListDup(db, pStep->pExprList, 0), 
127375 128599             sqlite3ExprDup(db, pStep->pWhere, 0), 
127376         -          pParse->eOrconf, 0, 0
       128600  +          pParse->eOrconf, 0, 0, 0
127377 128601           );
127378 128602           break;
127379 128603         }
127380 128604         case TK_INSERT: {
127381 128605           sqlite3Insert(pParse, 
127382 128606             targetSrcList(pParse, pStep),
127383 128607             sqlite3SelectDup(db, pStep->pSelect, 0), 
127384 128608             sqlite3IdListDup(db, pStep->pIdList), 
127385         -          pParse->eOrconf
       128609  +          pParse->eOrconf,
       128610  +          sqlite3UpsertDup(db, pStep->pUpsert)
127386 128611           );
127387 128612           break;
127388 128613         }
127389 128614         case TK_DELETE: {
127390 128615           sqlite3DeleteFrom(pParse, 
127391 128616             targetSrcList(pParse, pStep),
127392 128617             sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
................................................................................
127856 129081   SQLITE_PRIVATE void sqlite3Update(
127857 129082     Parse *pParse,         /* The parser context */
127858 129083     SrcList *pTabList,     /* The table in which we should change things */
127859 129084     ExprList *pChanges,    /* Things to be changed */
127860 129085     Expr *pWhere,          /* The WHERE clause.  May be null */
127861 129086     int onError,           /* How to handle constraint errors */
127862 129087     ExprList *pOrderBy,    /* ORDER BY clause. May be null */
127863         -  Expr *pLimit           /* LIMIT clause. May be null */
       129088  +  Expr *pLimit,          /* LIMIT clause. May be null */
       129089  +  Upsert *pUpsert        /* ON CONFLICT clause, or null */
127864 129090   ){
127865 129091     int i, j;              /* Loop counters */
127866 129092     Table *pTab;           /* The table to be updated */
127867 129093     int addrTop = 0;       /* VDBE instruction address of the start of the loop */
127868 129094     WhereInfo *pWInfo;     /* Information about the WHERE clause */
127869 129095     Vdbe *v;               /* The virtual database engine */
127870 129096     Index *pIdx;           /* For looping over indices */
................................................................................
127963 129189     }
127964 129190   
127965 129191     /* Allocate a cursors for the main database table and for all indices.
127966 129192     ** The index cursors might not be used, but if they are used they
127967 129193     ** need to occur right after the database cursor.  So go ahead and
127968 129194     ** allocate enough space, just in case.
127969 129195     */
127970         -  pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
       129196  +  iBaseCur = iDataCur = pParse->nTab++;
127971 129197     iIdxCur = iDataCur+1;
127972 129198     pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
       129199  +  testcase( pPk!=0 && pPk!=pTab->pIndex );
127973 129200     for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
127974         -    if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){
       129201  +    if( pPk==pIdx ){
127975 129202         iDataCur = pParse->nTab;
127976         -      pTabList->a[0].iCursor = iDataCur;
127977 129203       }
127978 129204       pParse->nTab++;
127979 129205     }
       129206  +  if( pUpsert ){
       129207  +    /* On an UPSERT, reuse the same cursors already opened by INSERT */
       129208  +    iDataCur = pUpsert->iDataCur;
       129209  +    iIdxCur = pUpsert->iIdxCur;
       129210  +    pParse->nTab = iBaseCur;
       129211  +  }
       129212  +  pTabList->a[0].iCursor = iDataCur;
127980 129213   
127981 129214     /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].  
127982 129215     ** Initialize aXRef[] and aToOpen[] to their default values.
127983 129216     */
127984 129217     aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
127985 129218     if( aXRef==0 ) goto update_cleanup;
127986 129219     aRegIdx = aXRef+pTab->nCol;
................................................................................
127989 129222     aToOpen[nIdx+1] = 0;
127990 129223     for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
127991 129224   
127992 129225     /* Initialize the name-context */
127993 129226     memset(&sNC, 0, sizeof(sNC));
127994 129227     sNC.pParse = pParse;
127995 129228     sNC.pSrcList = pTabList;
       129229  +  sNC.uNC.pUpsert = pUpsert;
       129230  +  sNC.ncFlags = NC_UUpsert;
127996 129231   
127997 129232     /* Resolve the column names in all the expressions of the
127998 129233     ** of the UPDATE statement.  Also find the column index
127999 129234     ** for each column to be updated in the pChanges array.  For each
128000 129235     ** column to be updated, make sure we have authorization to change
128001 129236     ** that column.
128002 129237     */
................................................................................
128092 129327       memset(aToOpen, 1, nIdx+1);
128093 129328     }
128094 129329   
128095 129330     /* Begin generating code. */
128096 129331     v = sqlite3GetVdbe(pParse);
128097 129332     if( v==0 ) goto update_cleanup;
128098 129333     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
128099         -  sqlite3BeginWriteOperation(pParse, 1, iDb);
       129334  +  sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
128100 129335   
128101 129336     /* Allocate required registers. */
128102 129337     if( !IsVirtual(pTab) ){
128103 129338       regRowSet = ++pParse->nMem;
128104 129339       regOldRowid = regNewRowid = ++pParse->nMem;
128105 129340       if( chngPk || pTrigger || hasFK ){
128106 129341         regOld = pParse->nMem + 1;
................................................................................
128143 129378     if( IsVirtual(pTab) ){
128144 129379       updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
128145 129380                          pWhere, onError);
128146 129381       goto update_cleanup;
128147 129382     }
128148 129383   #endif
128149 129384   
128150         -  /* Initialize the count of updated rows */
128151         -  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
       129385  +  /* Jump to labelBreak to abandon further processing of this UPDATE */
       129386  +  labelContinue = labelBreak = sqlite3VdbeMakeLabel(v);
       129387  +
       129388  +  /* Not an UPSERT.  Normal processing.  Begin by
       129389  +  ** initialize the count of updated rows */
       129390  +  if( (db->flags&SQLITE_CountRows)!=0
       129391  +   && !pParse->pTriggerTab
       129392  +   && !pParse->nested
       129393  +   && pUpsert==0
       129394  +  ){
128152 129395       regRowCount = ++pParse->nMem;
128153 129396       sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
128154 129397     }
128155 129398   
128156 129399     if( HasRowid(pTab) ){
128157 129400       sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
128158 129401     }else{
128159 129402       assert( pPk!=0 );
128160 129403       nPk = pPk->nKeyCol;
128161 129404       iPk = pParse->nMem+1;
128162 129405       pParse->nMem += nPk;
128163 129406       regKey = ++pParse->nMem;
       129407  +    if( pUpsert==0 ){
128164 129408       iEph = pParse->nTab++;
128165         -
128166 129409       sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
128167 129410       addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
128168 129411       sqlite3VdbeSetP4KeyInfo(pParse, pPk);
128169 129412     }
       129413  +  }
128170 129414   
       129415  +  if( pUpsert ){
       129416  +    /* If this is an UPSERT, then all cursors have already been opened by
       129417  +    ** the outer INSERT and the data cursor should be pointing at the row
       129418  +    ** that is to be updated.  So bypass the code that searches for the
       129419  +    ** row(s) to be updated.
       129420  +    */
       129421  +    pWInfo = 0;
       129422  +    eOnePass = ONEPASS_SINGLE;
       129423  +    sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL);
       129424  +  }else{
128171 129425     /* Begin the database scan. 
128172 129426     **
128173 129427     ** Do not consider a single-pass strategy for a multi-row update if
128174 129428     ** there are any triggers or foreign keys to process, or rows may
128175 129429     ** be deleted as a result of REPLACE conflict handling. Any of these
128176 129430     ** things might disturb a cursor being used to scan through the table
128177 129431     ** or index, causing a single-pass approach to malfunction.  */
................................................................................
128189 129443     **
128190 129444     **   UPDATE t1 SET b=b+1 WHERE b>?
128191 129445     **
128192 129446     ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
128193 129447     ** strategy that uses an index for which one or more columns are being
128194 129448     ** updated.  */
128195 129449     eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
       129450  +    if( eOnePass!=ONEPASS_SINGLE ){
       129451  +      sqlite3MultiWrite(pParse);
128196 129452     if( eOnePass==ONEPASS_MULTI ){
128197 129453       int iCur = aiCurOnePass[1];
128198 129454       if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
128199 129455         eOnePass = ONEPASS_OFF;
128200 129456       }
128201 129457       assert( iCur!=iDataCur || !HasRowid(pTab) );
128202 129458     }
       129459  +    }
       129460  +  }
128203 129461     
128204 129462     if( HasRowid(pTab) ){
128205 129463       /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
128206 129464       ** mode, write the rowid into the FIFO. In either of the one-pass modes,
128207 129465       ** leave it in register regOldRowid.  */
128208 129466       sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
128209 129467       if( eOnePass==ONEPASS_OFF ){
................................................................................
128216 129474       ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table 
128217 129475       ** is not required) and leave the PK fields in the array of registers.  */
128218 129476       for(i=0; i<nPk; i++){
128219 129477         assert( pPk->aiColumn[i]>=0 );
128220 129478         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
128221 129479       }
128222 129480       if( eOnePass ){
128223         -      sqlite3VdbeChangeToNoop(v, addrOpen);
       129481  +      if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
128224 129482         nKey = nPk;
128225 129483         regKey = iPk;
128226 129484       }else{
128227 129485         sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
128228 129486                           sqlite3IndexAffinityStr(db, pPk), nPk);
128229 129487         sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
128230 129488       }
128231 129489     }
128232 129490   
       129491  +  if( pUpsert==0 ){
128233 129492     if( eOnePass!=ONEPASS_MULTI ){
128234 129493       sqlite3WhereEnd(pWInfo);
128235 129494     }
128236 129495   
128237         -  labelBreak = sqlite3VdbeMakeLabel(v);
128238 129496     if( !isView ){
128239 129497       int addrOnce = 0;
128240 129498   
128241 129499       /* Open every index that needs updating. */
128242 129500       if( eOnePass!=ONEPASS_OFF ){
128243 129501         if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
128244 129502         if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
128245 129503       }
128246 129504   
128247 129505       if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
128248 129506         addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
128249 129507       }
128250         -    sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen,
128251         -                               0, 0);
       129508  +      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur,
       129509  +                                 aToOpen, 0, 0);
128252 129510       if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
128253 129511     }
128254 129512   
128255 129513     /* Top of the update loop */
128256 129514     if( eOnePass!=ONEPASS_OFF ){
128257 129515       if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
128258 129516         assert( pPk );
128259 129517         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
128260 129518         VdbeCoverageNeverTaken(v);
128261 129519       }
128262         -    if( eOnePass==ONEPASS_SINGLE ){
128263         -      labelContinue = labelBreak;
128264         -    }else{
       129520  +      if( eOnePass!=ONEPASS_SINGLE ){
128265 129521         labelContinue = sqlite3VdbeMakeLabel(v);
128266 129522       }
128267 129523       sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
128268 129524       VdbeCoverageIf(v, pPk==0);
128269 129525       VdbeCoverageIf(v, pPk!=0);
128270 129526     }else if( pPk ){
128271 129527       labelContinue = sqlite3VdbeMakeLabel(v);
................................................................................
128276 129532     }else{
128277 129533       labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
128278 129534                                regOldRowid);
128279 129535       VdbeCoverage(v);
128280 129536       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
128281 129537       VdbeCoverage(v);
128282 129538     }
       129539  +  }
128283 129540   
128284         -  /* If the record number will change, set register regNewRowid to
128285         -  ** contain the new value. If the record number is not being modified,
       129541  +  /* If the rowid value will change, set register regNewRowid to
       129542  +  ** contain the new value. If the rowid is not being modified,
128286 129543     ** then regNewRowid is the same register as regOldRowid, which is
128287 129544     ** already populated.  */
128288 129545     assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
128289 129546     if( chngRowid ){
128290 129547       sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
128291 129548       sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
128292 129549     }
................................................................................
128342 129599           ** into a register. This is done if there are no BEFORE triggers, or
128343 129600           ** if there are one or more BEFORE triggers that use this value via
128344 129601           ** a new.* reference in a trigger program.
128345 129602           */
128346 129603           testcase( i==31 );
128347 129604           testcase( i==32 );
128348 129605           sqlite3ExprCodeGetColumnToReg(pParse, pTab, i, iDataCur, regNew+i);
       129606  +        if( tmask & TRIGGER_BEFORE ){
       129607  +          /* This value will be recomputed in After-BEFORE-trigger-reload-loop
       129608  +          ** below, so make sure that it is not cached and reused.
       129609  +          ** Ticket d85fffd6ffe856092ed8daefa811b1e399706b28. */
       129610  +          sqlite3ExprCacheRemove(pParse, regNew+i, 1);
       129611  +        }
128349 129612         }else{
128350 129613           sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
128351 129614         }
128352 129615       }
128353 129616     }
128354 129617   
128355 129618     /* Fire any BEFORE UPDATE triggers. This happens before constraints are
................................................................................
128370 129633         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
128371 129634         VdbeCoverage(v);
128372 129635       }else{
128373 129636         sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
128374 129637         VdbeCoverage(v);
128375 129638       }
128376 129639   
128377         -    /* If it did not delete it, the row-trigger may still have modified 
       129640  +    /* After-BEFORE-trigger-reload-loop:
       129641  +    ** If it did not delete it, the BEFORE trigger may still have modified 
128378 129642       ** some of the columns of the row being updated. Load the values for 
128379         -    ** all columns not modified by the update statement into their 
128380         -    ** registers in case this has happened.
       129643  +    ** all columns not modified by the update statement into their registers
       129644  +    ** in case this has happened. Only unmodified columns are reloaded.
       129645  +    ** The values computed for modified columns use the values before the
       129646  +    ** BEFORE trigger runs.  See test case trigger1-18.0 (added 2018-04-26)
       129647  +    ** for an example.
128381 129648       */
128382 129649       for(i=0; i<pTab->nCol; i++){
128383 129650         if( aXRef[i]<0 && i!=pTab->iPKey ){
128384 129651           sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
128385 129652         }
128386 129653       }
128387 129654     }
................................................................................
128389 129656     if( !isView ){
128390 129657       int addr1 = 0;        /* Address of jump instruction */
128391 129658   
128392 129659       /* Do constraint checks. */
128393 129660       assert( regOldRowid>0 );
128394 129661       sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
128395 129662           regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
128396         -        aXRef);
       129663  +        aXRef, 0);
128397 129664   
128398 129665       /* Do FK constraint checks. */
128399 129666       if( hasFK ){
128400 129667         sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
128401 129668       }
128402 129669   
128403 129670       /* Delete the index entries associated with the current record.  */
................................................................................
128459 129726       if( hasFK ){
128460 129727         sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
128461 129728       }
128462 129729     }
128463 129730   
128464 129731     /* Increment the row counter 
128465 129732     */
128466         -  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
       129733  +  if( regRowCount ){
128467 129734       sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
128468 129735     }
128469 129736   
128470 129737     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
128471 129738         TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
128472 129739   
128473 129740     /* Repeat the above with the next record to be updated, until
................................................................................
128486 129753     }
128487 129754     sqlite3VdbeResolveLabel(v, labelBreak);
128488 129755   
128489 129756     /* Update the sqlite_sequence table by storing the content of the
128490 129757     ** maximum rowid counter values recorded while inserting into
128491 129758     ** autoincrement tables.
128492 129759     */
128493         -  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
       129760  +  if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
128494 129761       sqlite3AutoincrementEnd(pParse);
128495 129762     }
128496 129763   
128497 129764     /*
128498         -  ** Return the number of rows that were changed. If this routine is 
128499         -  ** generating code because of a call to sqlite3NestedParse(), do not
128500         -  ** invoke the callback function.
       129765  +  ** Return the number of rows that were changed, if we are tracking
       129766  +  ** that information.
128501 129767     */
128502         -  if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
       129768  +  if( regRowCount ){
128503 129769       sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
128504 129770       sqlite3VdbeSetNumCols(v, 1);
128505 129771       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
128506 129772     }
128507 129773   
128508 129774   update_cleanup:
128509 129775     sqlite3AuthContextPop(&sContext);
................................................................................
128567 129833     WhereInfo *pWInfo;
128568 129834     int nArg = 2 + pTab->nCol;      /* Number of arguments to VUpdate */
128569 129835     int regArg;                     /* First register in VUpdate arg array */
128570 129836     int regRec;                     /* Register in which to assemble record */
128571 129837     int regRowid;                   /* Register for ephem table rowid */
128572 129838     int iCsr = pSrc->a[0].iCursor;  /* Cursor used for virtual table scan */
128573 129839     int aDummy[2];                  /* Unused arg for sqlite3WhereOkOnePass() */
128574         -  int bOnePass;                   /* True to use onepass strategy */
       129840  +  int eOnePass;                   /* True to use onepass strategy */
128575 129841     int addr;                       /* Address of OP_OpenEphemeral */
128576 129842   
128577 129843     /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
128578 129844     ** create and open the ephemeral table in which the records created from
128579 129845     ** these arguments will be temporarily stored. */
128580 129846     assert( v );
128581 129847     ephemTab = pParse->nTab++;
................................................................................
128612 129878       assert( pPk!=0 );
128613 129879       assert( pPk->nKeyCol==1 );
128614 129880       iPk = pPk->aiColumn[0];
128615 129881       sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg);
128616 129882       sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1);
128617 129883     }
128618 129884   
128619         -  bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
       129885  +  eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
128620 129886   
128621         -  if( bOnePass ){
       129887  +  /* There is no ONEPASS_MULTI on virtual tables */
       129888  +  assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
       129889  +
       129890  +  if( eOnePass ){
128622 129891       /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
128623         -    ** above. Also, if this is a top-level parse (not a trigger), clear the
128624         -    ** multi-write flag so that the VM does not open a statement journal */
       129892  +    ** above. */
128625 129893       sqlite3VdbeChangeToNoop(v, addr);
128626         -    if( sqlite3IsToplevel(pParse) ){
128627         -      pParse->isMultiWrite = 0;
128628         -    }
       129894  +    sqlite3VdbeAddOp1(v, OP_Close, iCsr);
128629 129895     }else{
128630 129896       /* Create a record from the argument register contents and insert it into
128631 129897       ** the ephemeral table. */
       129898  +    sqlite3MultiWrite(pParse);
128632 129899       sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
128633 129900   #ifdef SQLITE_DEBUG
128634 129901       /* Signal an assert() within OP_MakeRecord that it is allowed to
128635 129902       ** accept no-change records with serial_type 10 */
128636 129903       sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
128637 129904   #endif
128638 129905       sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
128639 129906       sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
128640 129907     }
128641 129908   
128642 129909   
128643         -  if( bOnePass==0 ){
       129910  +  if( eOnePass==ONEPASS_OFF ){
128644 129911       /* End the virtual table scan */
128645 129912       sqlite3WhereEnd(pWInfo);
128646 129913   
128647 129914       /* Begin scannning through the ephemeral table. */
128648 129915       addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
128649 129916   
128650 129917       /* Extract arguments from the current row of the ephemeral table and 
................................................................................
128656 129923     sqlite3VtabMakeWritable(pParse, pTab);
128657 129924     sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB);
128658 129925     sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
128659 129926     sqlite3MayAbort(pParse);
128660 129927   
128661 129928     /* End of the ephemeral table scan. Or, if using the onepass strategy,
128662 129929     ** jump to here if the scan visited zero rows. */
128663         -  if( bOnePass==0 ){
       129930  +  if( eOnePass==ONEPASS_OFF ){
128664 129931       sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
128665 129932       sqlite3VdbeJumpHere(v, addr);
128666 129933       sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
128667 129934     }else{
128668 129935       sqlite3WhereEnd(pWInfo);
128669 129936     }
128670 129937   }
128671 129938   #endif /* SQLITE_OMIT_VIRTUALTABLE */
128672 129939   
128673 129940   /************** End of update.c **********************************************/
       129941  +/************** Begin file upsert.c ******************************************/
       129942  +/*
       129943  +** 2018-04-12
       129944  +**
       129945  +** The author disclaims copyright to this source code.  In place of
       129946  +** a legal notice, here is a blessing:
       129947  +**
       129948  +**    May you do good and not evil.
       129949  +**    May you find forgiveness for yourself and forgive others.
       129950  +**    May you share freely, never taking more than you give.
       129951  +**
       129952  +*************************************************************************
       129953  +** This file contains code to implement various aspects of UPSERT
       129954  +** processing and handling of the Upsert object.
       129955  +*/
       129956  +/* #include "sqliteInt.h" */
       129957  +
       129958  +#ifndef SQLITE_OMIT_UPSERT
       129959  +/*
       129960  +** Free a list of Upsert objects
       129961  +*/
       129962  +SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
       129963  +  if( p ){
       129964  +    sqlite3ExprListDelete(db, p->pUpsertTarget);
       129965  +    sqlite3ExprDelete(db, p->pUpsertTargetWhere);
       129966  +    sqlite3ExprListDelete(db, p->pUpsertSet);
       129967  +    sqlite3ExprDelete(db, p->pUpsertWhere);
       129968  +    sqlite3DbFree(db, p);
       129969  +  }
       129970  +}
       129971  +
       129972  +/*
       129973  +** Duplicate an Upsert object.
       129974  +*/
       129975  +SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
       129976  +  if( p==0 ) return 0;
       129977  +  return sqlite3UpsertNew(db,
       129978  +           sqlite3ExprListDup(db, p->pUpsertTarget, 0),
       129979  +           sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
       129980  +           sqlite3ExprListDup(db, p->pUpsertSet, 0),
       129981  +           sqlite3ExprDup(db, p->pUpsertWhere, 0)
       129982  +         );
       129983  +}
       129984  +
       129985  +/*
       129986  +** Create a new Upsert object.
       129987  +*/
       129988  +SQLITE_PRIVATE Upsert *sqlite3UpsertNew(
       129989  +  sqlite3 *db,           /* Determines which memory allocator to use */
       129990  +  ExprList *pTarget,     /* Target argument to ON CONFLICT, or NULL */
       129991  +  Expr *pTargetWhere,    /* Optional WHERE clause on the target */
       129992  +  ExprList *pSet,        /* UPDATE columns, or NULL for a DO NOTHING */
       129993  +  Expr *pWhere           /* WHERE clause for the ON CONFLICT UPDATE */
       129994  +){
       129995  +  Upsert *pNew;
       129996  +  pNew = sqlite3DbMallocRaw(db, sizeof(Upsert));
       129997  +  if( pNew==0 ){
       129998  +    sqlite3ExprListDelete(db, pTarget);
       129999  +    sqlite3ExprDelete(db, pTargetWhere);
       130000  +    sqlite3ExprListDelete(db, pSet);
       130001  +    sqlite3ExprDelete(db, pWhere);
       130002  +    return 0;
       130003  +  }else{
       130004  +    pNew->pUpsertTarget = pTarget;
       130005  +    pNew->pUpsertTargetWhere = pTargetWhere;
       130006  +    pNew->pUpsertSet = pSet;
       130007  +    pNew->pUpsertWhere = pWhere;
       130008  +    pNew->pUpsertIdx = 0;
       130009  +  }
       130010  +  return pNew;
       130011  +}
       130012  +
       130013  +/*
       130014  +** Analyze the ON CONFLICT clause described by pUpsert.  Resolve all
       130015  +** symbols in the conflict-target.
       130016  +**
       130017  +** Return SQLITE_OK if everything works, or an error code is something
       130018  +** is wrong.
       130019  +*/
       130020  +SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(
       130021  +  Parse *pParse,     /* The parsing context */
       130022  +  SrcList *pTabList, /* Table into which we are inserting */
       130023  +  Upsert *pUpsert    /* The ON CONFLICT clauses */
       130024  +){
       130025  +  Table *pTab;            /* That table into which we are inserting */
       130026  +  int rc;                 /* Result code */
       130027  +  int iCursor;            /* Cursor used by pTab */
       130028  +  Index *pIdx;            /* One of the indexes of pTab */
       130029  +  ExprList *pTarget;      /* The conflict-target clause */
       130030  +  Expr *pTerm;            /* One term of the conflict-target clause */
       130031  +  NameContext sNC;        /* Context for resolving symbolic names */
       130032  +  Expr sCol[2];           /* Index column converted into an Expr */
       130033  +
       130034  +  assert( pTabList->nSrc==1 );
       130035  +  assert( pTabList->a[0].pTab!=0 );
       130036  +  assert( pUpsert!=0 );
       130037  +  assert( pUpsert->pUpsertTarget!=0 );
       130038  +
       130039  +  /* Resolve all symbolic names in the conflict-target clause, which
       130040  +  ** includes both the list of columns and the optional partial-index
       130041  +  ** WHERE clause.
       130042  +  */
       130043  +  memset(&sNC, 0, sizeof(sNC));
       130044  +  sNC.pParse = pParse;
       130045  +  sNC.pSrcList = pTabList;
       130046  +  rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
       130047  +  if( rc ) return rc;
       130048  +  rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
       130049  +  if( rc ) return rc;
       130050  +
       130051  +  /* Check to see if the conflict target matches the rowid. */  
       130052  +  pTab = pTabList->a[0].pTab;
       130053  +  pTarget = pUpsert->pUpsertTarget;
       130054  +  iCursor = pTabList->a[0].iCursor;
       130055  +  if( HasRowid(pTab) 
       130056  +   && pTarget->nExpr==1
       130057  +   && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
       130058  +   && pTerm->iColumn==XN_ROWID
       130059  +  ){
       130060  +    /* The conflict-target is the rowid of the primary table */
       130061  +    assert( pUpsert->pUpsertIdx==0 );
       130062  +    return SQLITE_OK;
       130063  +  }
       130064  +
       130065  +  /* Initialize sCol[0..1] to be an expression parse tree for a
       130066  +  ** single column of an index.  The sCol[0] node will be the TK_COLLATE
       130067  +  ** operator and sCol[1] will be the TK_COLUMN operator.  Code below
       130068  +  ** will populate the specific collation and column number values
       130069  +  ** prior to comparing against the conflict-target expression.
       130070  +  */
       130071  +  memset(sCol, 0, sizeof(sCol));
       130072  +  sCol[0].op = TK_COLLATE;
       130073  +  sCol[0].pLeft = &sCol[1];
       130074  +  sCol[1].op = TK_COLUMN;
       130075  +  sCol[1].iTable = pTabList->a[0].iCursor;
       130076  +
       130077  +  /* Check for matches against other indexes */
       130078  +  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       130079  +    int ii, jj, nn;
       130080  +    if( !IsUniqueIndex(pIdx) ) continue;
       130081  +    if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
       130082  +    if( pIdx->pPartIdxWhere ){
       130083  +      if( pUpsert->pUpsertTargetWhere==0 ) continue;
       130084  +      if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
       130085  +                             pIdx->pPartIdxWhere, iCursor)!=0 ){
       130086  +        continue;
       130087  +      }
       130088  +    }
       130089  +    nn = pIdx->nKeyCol;
       130090  +    for(ii=0; ii<nn; ii++){
       130091  +      Expr *pExpr;
       130092  +      sCol[0].u.zToken = (char*)pIdx->azColl[ii];
       130093  +      if( pIdx->aiColumn[ii]==XN_EXPR ){
       130094  +        assert( pIdx->aColExpr!=0 );
       130095  +        assert( pIdx->aColExpr->nExpr>ii );
       130096  +        pExpr = pIdx->aColExpr->a[ii].pExpr;
       130097  +        if( pExpr->op!=TK_COLLATE ){
       130098  +          sCol[0].pLeft = pExpr;
       130099  +          pExpr = &sCol[0];
       130100  +        }
       130101  +      }else{
       130102  +        sCol[0].pLeft = &sCol[1];
       130103  +        sCol[1].iColumn = pIdx->aiColumn[ii];
       130104  +        pExpr = &sCol[0];
       130105  +      }
       130106  +      for(jj=0; jj<nn; jj++){
       130107  +        if( sqlite3ExprCompare(pParse, pTarget->a[jj].pExpr, pExpr,iCursor)<2 ){
       130108  +          break;  /* Column ii of the index matches column jj of target */
       130109  +        }
       130110  +      }
       130111  +      if( jj>=nn ){
       130112  +        /* The target contains no match for column jj of the index */
       130113  +        break;
       130114  +      }
       130115  +    }
       130116  +    if( ii<nn ){
       130117  +      /* Column ii of the index did not match any term of the conflict target.
       130118  +      ** Continue the search with the next index. */
       130119  +      continue;
       130120  +    }
       130121  +    pUpsert->pUpsertIdx = pIdx;
       130122  +    return SQLITE_OK;
       130123  +  }
       130124  +  sqlite3ErrorMsg(pParse, "ON CONFLICT clause does not match any "
       130125  +                          "PRIMARY KEY or UNIQUE constraint");
       130126  +  return SQLITE_ERROR;
       130127  +}
       130128  +
       130129  +/*
       130130  +** Generate bytecode that does an UPDATE as part of an upsert.
       130131  +**
       130132  +** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
       130133  +** In this case parameter iCur is a cursor open on the table b-tree that
       130134  +** currently points to the conflicting table row. Otherwise, if pIdx
       130135  +** is not NULL, then pIdx is the constraint that failed and iCur is a
       130136  +** cursor points to the conflicting row.
       130137  +*/
       130138  +SQLITE_PRIVATE void sqlite3UpsertDoUpdate(
       130139  +  Parse *pParse,        /* The parsing and code-generating context */
       130140  +  Upsert *pUpsert,      /* The ON CONFLICT clause for the upsert */
       130141  +  Table *pTab,          /* The table being updated */
       130142  +  Index *pIdx,          /* The UNIQUE constraint that failed */
       130143  +  int iCur              /* Cursor for pIdx (or pTab if pIdx==NULL) */
       130144  +){
       130145  +  Vdbe *v = pParse->pVdbe;
       130146  +  sqlite3 *db = pParse->db;
       130147  +  SrcList *pSrc;            /* FROM clause for the UPDATE */
       130148  +  int iDataCur = pUpsert->iDataCur;
       130149  +
       130150  +  assert( v!=0 );
       130151  +  VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
       130152  +  if( pIdx && iCur!=iDataCur ){
       130153  +    if( HasRowid(pTab) ){
       130154  +      int regRowid = sqlite3GetTempReg(pParse);
       130155  +      sqlite3VdbeAddOp2(v, OP_IdxRowid, iCur, regRowid);
       130156  +      sqlite3VdbeAddOp3(v, OP_SeekRowid, iDataCur, 0, regRowid);
       130157  +      VdbeCoverage(v);
       130158  +      sqlite3ReleaseTempReg(pParse, regRowid);
       130159  +    }else{
       130160  +      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
       130161  +      int nPk = pPk->nKeyCol;
       130162  +      int iPk = pParse->nMem+1;
       130163  +      int i;
       130164  +      pParse->nMem += nPk;
       130165  +      for(i=0; i<nPk; i++){
       130166  +        int k;
       130167  +        assert( pPk->aiColumn[i]>=0 );
       130168  +        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
       130169  +        sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
       130170  +        VdbeComment((v, "%s.%s", pIdx->zName,
       130171  +                    pTab->aCol[pPk->aiColumn[i]].zName));
       130172  +      }
       130173  +      sqlite3VdbeVerifyAbortable(v, OE_Abort);
       130174  +      i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
       130175  +      VdbeCoverage(v);
       130176  +      sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0, 
       130177  +            "corrupt database", P4_STATIC);
       130178  +      sqlite3VdbeJumpHere(v, i);
       130179  +    }
       130180  +  }
       130181  +  /* pUpsert does not own pUpsertSrc - the outer INSERT statement does.  So
       130182  +  ** we have to make a copy before passing it down into sqlite3Update() */
       130183  +  pSrc = sqlite3SrcListDup(db, pUpsert->pUpsertSrc, 0);
       130184  +  sqlite3Update(pParse, pSrc, pUpsert->pUpsertSet,
       130185  +      pUpsert->pUpsertWhere, OE_Abort, 0, 0, pUpsert);
       130186  +  pUpsert->pUpsertSet = 0;    /* Will have been deleted by sqlite3Update() */
       130187  +  pUpsert->pUpsertWhere = 0;  /* Will have been deleted by sqlite3Update() */
       130188  +  VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
       130189  +}
       130190  +
       130191  +#endif /* SQLITE_OMIT_UPSERT */
       130192  +
       130193  +/************** End of upsert.c **********************************************/
128674 130194   /************** Begin file vacuum.c ******************************************/
128675 130195   /*
128676 130196   ** 2003 April 6
128677 130197   **
128678 130198   ** The author disclaims copyright to this source code.  In place of
128679 130199   ** a legal notice, here is a blessing:
128680 130200   **
................................................................................
128709 130229   
128710 130230     /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
128711 130231     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
128712 130232     if( rc!=SQLITE_OK ) return rc;
128713 130233     while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
128714 130234       const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
128715 130235       assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
128716         -    assert( sqlite3_strnicmp(zSubSql,"SELECT",6)!=0 || CORRUPT_DB );
128717         -    if( zSubSql && zSubSql[0]!='S' ){
       130236  +    /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
       130237  +    ** or INSERT.  Historically there have been attacks that first
       130238  +    ** corrupt the sqlite_master.sql field with other kinds of statements
       130239  +    ** then run VACUUM to get those statements to execute at inappropriate
       130240  +    ** times. */
       130241  +    if( zSubSql
       130242  +     && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
       130243  +    ){
128718 130244         rc = execSql(db, pzErrMsg, zSubSql);
128719 130245         if( rc!=SQLITE_OK ) break;
128720 130246       }
128721 130247     }
128722 130248     assert( rc!=SQLITE_ROW );
128723 130249     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
128724 130250     if( rc ){
................................................................................
128923 130449         " WHERE type='table'AND name<>'sqlite_sequence'"
128924 130450         " AND coalesce(rootpage,1)>0",
128925 130451         zDbMain
128926 130452     );
128927 130453     if( rc!=SQLITE_OK ) goto end_of_vacuum;
128928 130454     rc = execSqlF(db, pzErrMsg,
128929 130455         "SELECT sql FROM \"%w\".sqlite_master"
128930         -      " WHERE type='index' AND length(sql)>10",
       130456  +      " WHERE type='index'",
128931 130457         zDbMain
128932 130458     );
128933 130459     if( rc!=SQLITE_OK ) goto end_of_vacuum;
128934 130460     db->init.iDb = 0;
128935 130461   
128936 130462     /* Loop through the tables in the main database. For each, do
128937 130463     ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
................................................................................
130093 131619     Table *pTab;
130094 131620     sqlite3_vtab *pVtab;
130095 131621     sqlite3_module *pMod;
130096 131622     void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
130097 131623     void *pArg = 0;
130098 131624     FuncDef *pNew;
130099 131625     int rc = 0;
130100         -  char *zLowerName;
130101         -  unsigned char *z;
130102         -
130103 131626   
130104 131627     /* Check to see the left operand is a column in a virtual table */
130105 131628     if( NEVER(pExpr==0) ) return pDef;
130106 131629     if( pExpr->op!=TK_COLUMN ) return pDef;
130107 131630     pTab = pExpr->pTab;
130108 131631     if( pTab==0 ) return pDef;
130109 131632     if( !IsVirtual(pTab) ) return pDef;
................................................................................
130110 131633     pVtab = sqlite3GetVTable(db, pTab)->pVtab;
130111 131634     assert( pVtab!=0 );
130112 131635     assert( pVtab->pModule!=0 );
130113 131636     pMod = (sqlite3_module *)pVtab->pModule;
130114 131637     if( pMod->xFindFunction==0 ) return pDef;
130115 131638    
130116 131639     /* Call the xFindFunction method on the virtual table implementation
130117         -  ** to see if the implementation wants to overload this function 
       131640  +  ** to see if the implementation wants to overload this function.
       131641  +  **
       131642  +  ** Though undocumented, we have historically always invoked xFindFunction
       131643  +  ** with an all lower-case function name.  Continue in this tradition to
       131644  +  ** avoid any chance of an incompatibility.
130118 131645     */
130119         -  zLowerName = sqlite3DbStrDup(db, pDef->zName);
130120         -  if( zLowerName ){
130121         -    for(z=(unsigned char*)zLowerName; *z; z++){
130122         -      *z = sqlite3UpperToLower[*z];
       131646  +#ifdef SQLITE_DEBUG
       131647  +  {
       131648  +    int i;
       131649  +    for(i=0; pDef->zName[i]; i++){
       131650  +      unsigned char x = (unsigned char)pDef->zName[i];
       131651  +      assert( x==sqlite3UpperToLower[x] );
130123 131652       }
130124         -    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg);
130125         -    sqlite3DbFree(db, zLowerName);
130126 131653     }
       131654  +#endif
       131655  +  rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
130127 131656     if( rc==0 ){
130128 131657       return pDef;
130129 131658     }
130130 131659   
130131 131660     /* Create a new ephemeral function definition for the overloaded
130132 131661     ** function */
130133 131662     pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
................................................................................
130779 132308   
130780 132309   /* wherecode.c: */
130781 132310   #ifndef SQLITE_OMIT_EXPLAIN
130782 132311   SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
130783 132312     Parse *pParse,                  /* Parse context */
130784 132313     SrcList *pTabList,              /* Table list this loop refers to */
130785 132314     WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
130786         -  int iLevel,                     /* Value for "level" column of output */
130787         -  int iFrom,                      /* Value for "from" column of output */
130788 132315     u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
130789 132316   );
130790 132317   #else
130791         -# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0
       132318  +# define sqlite3WhereExplainOneScan(u,v,w,x) 0
130792 132319   #endif /* SQLITE_OMIT_EXPLAIN */
130793 132320   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
130794 132321   SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
130795 132322     Vdbe *v,                        /* Vdbe to add scanstatus entry to */
130796 132323     SrcList *pSrclist,              /* FROM clause pLvl reads data from */
130797 132324     WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
130798 132325     int addrExplain                 /* Address of OP_Explain (or 0) */
................................................................................
130904 132431     int iTerm,                  /* Zero-based index of first term. */
130905 132432     int bAnd,                   /* Non-zero to append " AND " */
130906 132433     const char *zOp             /* Name of the operator */
130907 132434   ){
130908 132435     int i;
130909 132436   
130910 132437     assert( nTerm>=1 );
130911         -  if( bAnd ) sqlite3StrAccumAppend(pStr, " AND ", 5);
       132438  +  if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
130912 132439   
130913         -  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, "(", 1);
       132440  +  if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
130914 132441     for(i=0; i<nTerm; i++){
130915         -    if( i ) sqlite3StrAccumAppend(pStr, ",", 1);
130916         -    sqlite3StrAccumAppendAll(pStr, explainIndexColumnName(pIdx, iTerm+i));
       132442  +    if( i ) sqlite3_str_append(pStr, ",", 1);
       132443  +    sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
130917 132444     }
130918         -  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, ")", 1);
       132445  +  if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
130919 132446   
130920         -  sqlite3StrAccumAppend(pStr, zOp, 1);
       132447  +  sqlite3_str_append(pStr, zOp, 1);
130921 132448   
130922         -  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, "(", 1);
       132449  +  if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
130923 132450     for(i=0; i<nTerm; i++){
130924         -    if( i ) sqlite3StrAccumAppend(pStr, ",", 1);
130925         -    sqlite3StrAccumAppend(pStr, "?", 1);
       132451  +    if( i ) sqlite3_str_append(pStr, ",", 1);
       132452  +    sqlite3_str_append(pStr, "?", 1);
130926 132453     }
130927         -  if( nTerm>1 ) sqlite3StrAccumAppend(pStr, ")", 1);
       132454  +  if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
130928 132455   }
130929 132456   
130930 132457   /*
130931 132458   ** Argument pLevel describes a strategy for scanning table pTab. This 
130932 132459   ** function appends text to pStr that describes the subset of table
130933 132460   ** rows scanned by the strategy in the form of an SQL expression.
130934 132461   **
................................................................................
130944 132471   static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
130945 132472     Index *pIndex = pLoop->u.btree.pIndex;
130946 132473     u16 nEq = pLoop->u.btree.nEq;
130947 132474     u16 nSkip = pLoop->nSkip;
130948 132475     int i, j;
130949 132476   
130950 132477     if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
130951         -  sqlite3StrAccumAppend(pStr, " (", 2);
       132478  +  sqlite3_str_append(pStr, " (", 2);
130952 132479     for(i=0; i<nEq; i++){
130953 132480       const char *z = explainIndexColumnName(pIndex, i);
130954         -    if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
130955         -    sqlite3XPrintf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
       132481  +    if( i ) sqlite3_str_append(pStr, " AND ", 5);
       132482  +    sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
130956 132483     }
130957 132484   
130958 132485     j = i;
130959 132486     if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
130960 132487       explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
130961 132488       i = 1;
130962 132489     }
130963 132490     if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
130964 132491       explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
130965 132492     }
130966         -  sqlite3StrAccumAppend(pStr, ")", 1);
       132493  +  sqlite3_str_append(pStr, ")", 1);
130967 132494   }
130968 132495   
130969 132496   /*
130970 132497   ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
130971 132498   ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
130972 132499   ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode 
130973 132500   ** is added to the output to describe the table scan strategy in pLevel.
................................................................................
130975 132502   ** If an OP_Explain opcode is added to the VM, its address is returned.
130976 132503   ** Otherwise, if no OP_Explain is coded, zero is returned.
130977 132504   */
130978 132505   SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
130979 132506     Parse *pParse,                  /* Parse context */
130980 132507     SrcList *pTabList,              /* Table list this loop refers to */
130981 132508     WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
130982         -  int iLevel,                     /* Value for "level" column of output */
130983         -  int iFrom,                      /* Value for "from" column of output */
130984 132509     u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
130985 132510   ){
130986 132511     int ret = 0;
130987 132512   #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
130988 132513     if( sqlite3ParseToplevel(pParse)->explain==2 )
130989 132514   #endif
130990 132515     {
130991 132516       struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
130992 132517       Vdbe *v = pParse->pVdbe;      /* VM being constructed */
130993 132518       sqlite3 *db = pParse->db;     /* Database handle */
130994         -    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
130995 132519       int isSearch;                 /* True for a SEARCH. False for SCAN. */
130996 132520       WhereLoop *pLoop;             /* The controlling WhereLoop object */
130997 132521       u32 flags;                    /* Flags that describe this loop */
130998 132522       char *zMsg;                   /* Text to add to EQP output */
130999 132523       StrAccum str;                 /* EQP output string */
131000 132524       char zBuf[100];               /* Initial space for EQP output string */
131001 132525   
................................................................................
131004 132528       if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
131005 132529   
131006 132530       isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
131007 132531               || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
131008 132532               || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
131009 132533   
131010 132534       sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
131011         -    sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
       132535  +    sqlite3_str_appendall(&str, isSearch ? "SEARCH" : "SCAN");
131012 132536       if( pItem->pSelect ){
131013         -      sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId);
       132537  +      sqlite3_str_appendf(&str, " SUBQUERY 0x%p", pItem->pSelect);
131014 132538       }else{
131015         -      sqlite3XPrintf(&str, " TABLE %s", pItem->zName);
       132539  +      sqlite3_str_appendf(&str, " TABLE %s", pItem->zName);
131016 132540       }
131017 132541   
131018 132542       if( pItem->zAlias ){
131019         -      sqlite3XPrintf(&str, " AS %s", pItem->zAlias);
       132543  +      sqlite3_str_appendf(&str, " AS %s", pItem->zAlias);
131020 132544       }
131021 132545       if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
131022 132546         const char *zFmt = 0;
131023 132547         Index *pIdx;
131024 132548   
131025 132549         assert( pLoop->u.btree.pIndex!=0 );
131026 132550         pIdx = pLoop->u.btree.pIndex;
................................................................................
131035 132559           zFmt = "AUTOMATIC COVERING INDEX";
131036 132560         }else if( flags & WHERE_IDX_ONLY ){
131037 132561           zFmt = "COVERING INDEX %s";
131038 132562         }else{
131039 132563           zFmt = "INDEX %s";
131040 132564         }
131041 132565         if( zFmt ){
131042         -        sqlite3StrAccumAppend(&str, " USING ", 7);
131043         -        sqlite3XPrintf(&str, zFmt, pIdx->zName);
       132566  +        sqlite3_str_append(&str, " USING ", 7);
       132567  +        sqlite3_str_appendf(&str, zFmt, pIdx->zName);
131044 132568           explainIndexRange(&str, pLoop);
131045 132569         }
131046 132570       }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
131047 132571         const char *zRangeOp;
131048 132572         if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
131049 132573           zRangeOp = "=";
131050 132574         }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
................................................................................
131051 132575           zRangeOp = ">? AND rowid<";
131052 132576         }else if( flags&WHERE_BTM_LIMIT ){
131053 132577           zRangeOp = ">";
131054 132578         }else{
131055 132579           assert( flags&WHERE_TOP_LIMIT);
131056 132580           zRangeOp = "<";
131057 132581         }
131058         -      sqlite3XPrintf(&str, " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
       132582  +      sqlite3_str_appendf(&str, 
       132583  +          " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
131059 132584       }
131060 132585   #ifndef SQLITE_OMIT_VIRTUALTABLE
131061 132586       else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
131062         -      sqlite3XPrintf(&str, " VIRTUAL TABLE INDEX %d:%s",
       132587  +      sqlite3_str_appendf(&str, " VIRTUAL TABLE INDEX %d:%s",
131063 132588                     pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
131064 132589       }
131065 132590   #endif
131066 132591   #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
131067 132592       if( pLoop->nOut>=10 ){
131068         -      sqlite3XPrintf(&str, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
       132593  +      sqlite3_str_appendf(&str, " (~%llu rows)",
       132594  +             sqlite3LogEstToInt(pLoop->nOut));
131069 132595       }else{
131070         -      sqlite3StrAccumAppend(&str, " (~1 row)", 9);
       132596  +      sqlite3_str_append(&str, " (~1 row)", 9);
131071 132597       }
131072 132598   #endif
131073 132599       zMsg = sqlite3StrAccumFinish(&str);
131074         -    ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
       132600  +    ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
       132601  +                            pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
131075 132602     }
131076 132603     return ret;
131077 132604   }
131078 132605   #endif /* SQLITE_OMIT_EXPLAIN */
131079 132606   
131080 132607   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
131081 132608   /*
................................................................................
132068 133595     addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
132069 133596     addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
132070 133597   
132071 133598     /* If this is the right table of a LEFT OUTER JOIN, allocate and
132072 133599     ** initialize a memory cell that records if this table matches any
132073 133600     ** row of the left table of the join.
132074 133601     */
       133602  +  assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
       133603  +       || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
       133604  +  );
132075 133605     if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
132076 133606       pLevel->iLeftJoin = ++pParse->nMem;
132077 133607       sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
132078 133608       VdbeComment((v, "init LEFT JOIN no-match flag"));
132079 133609     }
132080 133610   
132081 133611     /* Compute a safe address to jump to if we discover that the table for
................................................................................
132601 134131         sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
132602 134132                              iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
132603 134133       }
132604 134134   
132605 134135       /* If pIdx is an index on one or more expressions, then look through
132606 134136       ** all the expressions in pWInfo and try to transform matching expressions
132607 134137       ** into reference to index columns.
       134138  +    **
       134139  +    ** Do not do this for the RHS of a LEFT JOIN. This is because the 
       134140  +    ** expression may be evaluated after OP_NullRow has been executed on
       134141  +    ** the cursor. In this case it is important to do the full evaluation,
       134142  +    ** as the result of the expression may not be NULL, even if all table
       134143  +    ** column values are.  https://www.sqlite.org/src/info/7fa8049685b50b5a
132608 134144       */
       134145  +    if( pLevel->iLeftJoin==0 ){
132609 134146       whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
132610         -
       134147  +    }
132611 134148   
132612 134149       /* Record the instruction used to terminate the loop. */
132613 134150       if( pLoop->wsFlags & WHERE_ONEROW ){
132614 134151         pLevel->op = OP_Noop;
132615 134152       }else if( bRev ){
132616 134153         pLevel->op = OP_Prev;
132617 134154       }else{
................................................................................
132759 134296       ** See ticket http://www.sqlite.org/src/info/f2369304e4
132760 134297       */
132761 134298       if( pWC->nTerm>1 ){
132762 134299         int iTerm;
132763 134300         for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
132764 134301           Expr *pExpr = pWC->a[iTerm].pExpr;
132765 134302           if( &pWC->a[iTerm] == pTerm ) continue;
132766         -        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
132767 134303           testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
132768 134304           testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
132769 134305           if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
132770 134306           if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
132771 134307           testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
132772 134308           pExpr = sqlite3ExprDup(db, pExpr, 0);
132773 134309           pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
................................................................................
132778 134314       }
132779 134315   
132780 134316       /* Run a separate WHERE clause for each term of the OR clause.  After
132781 134317       ** eliminating duplicates from other WHERE clauses, the action for each
132782 134318       ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
132783 134319       */
132784 134320       wctrlFlags =  WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
       134321  +    ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
132785 134322       for(ii=0; ii<pOrWc->nTerm; ii++){
132786 134323         WhereTerm *pOrTerm = &pOrWc->a[ii];
132787 134324         if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
132788 134325           WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
132789 134326           Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
132790 134327           int jmp1 = 0;                   /* Address of jump operation */
132791         -        if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
       134328  +        assert( (pTabItem[0].fg.jointype & JT_LEFT)==0 
       134329  +             || ExprHasProperty(pOrExpr, EP_FromJoin) 
       134330  +        );
       134331  +        if( pAndExpr ){
132792 134332             pAndExpr->pLeft = pOrExpr;
132793 134333             pOrExpr = pAndExpr;
132794 134334           }
132795 134335           /* Loop through table entries that match term pOrTerm. */
132796 134336           WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
132797 134337           pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
132798 134338                                         wctrlFlags, iCovCur);
132799 134339           assert( pSubWInfo || pParse->nErr || db->mallocFailed );
132800 134340           if( pSubWInfo ){
132801 134341             WhereLoop *pSubLoop;
132802 134342             int addrExplain = sqlite3WhereExplainOneScan(
132803         -              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
       134343  +              pParse, pOrTab, &pSubWInfo->a[0], 0
132804 134344             );
132805 134345             sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
132806 134346   
132807 134347             /* This is the sub-WHERE clause body.  First skip over
132808 134348             ** duplicate rows from prior sub-WHERE clauses, and record the
132809 134349             ** rowid (or PRIMARY KEY) for the current row so that the same
132810 134350             ** row will be skipped in subsequent sub-WHERE clauses.
................................................................................
132895 134435             }
132896 134436   
132897 134437             /* Finish the loop through table entries that match term pOrTerm. */
132898 134438             sqlite3WhereEnd(pSubWInfo);
132899 134439           }
132900 134440         }
132901 134441       }
       134442  +    ExplainQueryPlanPop(pParse);
132902 134443       pLevel->u.pCovidx = pCov;
132903 134444       if( pCov ) pLevel->iIdxCur = iCovCur;
132904 134445       if( pAndExpr ){
132905 134446         pAndExpr->pLeft = 0;
132906 134447         sqlite3ExprDelete(db, pAndExpr);
132907 134448       }
132908 134449       sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
................................................................................
132967 134508           testcase( pWInfo->untestedTerms==0
132968 134509               && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
132969 134510           pWInfo->untestedTerms = 1;
132970 134511           continue;
132971 134512         }
132972 134513         pE = pTerm->pExpr;
132973 134514         assert( pE!=0 );
132974         -      if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
       134515  +      if( (pTabItem->fg.jointype&JT_LEFT) && !ExprHasProperty(pE,EP_FromJoin) ){
132975 134516           continue;
132976 134517         }
132977 134518         
132978 134519         if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
132979 134520           iNext = 2;
132980 134521           continue;
132981 134522         }
................................................................................
133894 135435           testcase( idxNew==0 );
133895 135436           exprAnalyze(pSrc, pWC, idxNew);
133896 135437           pTerm = &pWC->a[idxTerm];
133897 135438           markTermAsChild(pWC, idxNew, idxTerm);
133898 135439         }else{
133899 135440           sqlite3ExprListDelete(db, pList);
133900 135441         }
133901         -      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 3 */
133902 135442       }
133903 135443     }
133904 135444   }
133905 135445   #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
133906 135446   
133907 135447   /*
133908 135448   ** We already know that pExpr is a binary operator where both operands are
................................................................................
137031 138571       }
137032 138572       if( pTerm->prereqRight & pNew->maskSelf ) continue;
137033 138573   
137034 138574       /* Do not allow the upper bound of a LIKE optimization range constraint
137035 138575       ** to mix with a lower range bound from some other source */
137036 138576       if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
137037 138577   
137038         -    /* Do not allow IS constraints from the WHERE clause to be used by the
       138578  +    /* Do not allow constraints from the WHERE clause to be used by the
137039 138579       ** right table of a LEFT JOIN.  Only constraints in the ON clause are
137040 138580       ** allowed */
137041 138581       if( (pSrc->fg.jointype & JT_LEFT)!=0
137042 138582        && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
137043         -     && (eOp & (WO_IS|WO_ISNULL))!=0
137044 138583       ){
137045         -      testcase( eOp & WO_IS );
137046         -      testcase( eOp & WO_ISNULL );
137047 138584         continue;
137048 138585       }
137049 138586   
137050 138587       if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
137051 138588         pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE;
137052 138589       }else{
137053 138590         pBuilder->bldFlags |= SQLITE_BLDF_INDEXED;
................................................................................
137476 139013           pNew->nSkip = 0;
137477 139014           pNew->u.btree.pIndex = 0;
137478 139015           pNew->nLTerm = 1;
137479 139016           pNew->aLTerm[0] = pTerm;
137480 139017           /* TUNING: One-time cost for computing the automatic index is
137481 139018           ** estimated to be X*N*log2(N) where N is the number of rows in
137482 139019           ** the table being indexed and where X is 7 (LogEst=28) for normal
137483         -        ** tables or 1.375 (LogEst=4) for views and subqueries.  The value
       139020  +        ** tables or 0.5 (LogEst=-10) for views and subqueries.  The value
137484 139021           ** of X is smaller for views and subqueries so that the query planner
137485 139022           ** will be more aggressive about generating automatic indexes for
137486 139023           ** those objects, since there is no opportunity to add schema
137487 139024           ** indexes on subqueries and views. */
137488         -        pNew->rSetup = rLogSize + rSize + 4;
       139025  +        pNew->rSetup = rLogSize + rSize;
137489 139026           if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
137490         -          pNew->rSetup += 24;
       139027  +          pNew->rSetup += 28;
       139028  +        }else{
       139029  +          pNew->rSetup -= 10;
137491 139030           }
137492 139031           ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
137493 139032           if( pNew->rSetup<0 ) pNew->rSetup = 0;
137494 139033           /* TUNING: Each index lookup yields 20 rows in the table.  This
137495 139034           ** is more than the usual guess of 10 rows, since we have no way
137496 139035           ** of knowing how selective the index will ultimately be.  It would
137497 139036           ** not be unreasonable to make this value much larger. */
................................................................................
138619 140158           LogEst rUnsorted;                 /* Unsorted cost of (pFrom+pWLoop) */
138620 140159           i8 isOrdered = pFrom->isOrdered;  /* isOrdered for (pFrom+pWLoop) */
138621 140160           Bitmask maskNew;                  /* Mask of src visited by (..) */
138622 140161           Bitmask revMask = 0;              /* Mask of rev-order loops for (..) */
138623 140162   
138624 140163           if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
138625 140164           if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
138626         -        if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<10 ){
       140165  +        if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
138627 140166             /* Do not use an automatic index if the this loop is expected
138628         -          ** to run less than 2 times. */
       140167  +          ** to run less than 1.25 times.  It is tempting to also exclude
       140168  +          ** automatic index usage on an outer loop, but sometimes an automatic
       140169  +          ** index is useful in the outer loop of a correlated subquery. */
138629 140170             assert( 10==sqlite3LogEst(2) );
138630 140171             continue;
138631 140172           }
       140173  +
138632 140174           /* At this point, pWLoop is a candidate to be the next loop. 
138633 140175           ** Compute its cost */
138634 140176           rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
138635 140177           rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
138636 140178           nOut = pFrom->nRow + pWLoop->nOut;
138637 140179           maskNew = pFrom->maskLoop | pWLoop->maskSelf;
138638 140180           if( isOrdered<0 ){
................................................................................
139206 140748     /* Special case: No FROM clause
139207 140749     */
139208 140750     if( nTabList==0 ){
139209 140751       if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
139210 140752       if( wctrlFlags & WHERE_WANT_DISTINCT ){
139211 140753         pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
139212 140754       }
       140755  +    ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
139213 140756     }else{
139214 140757       /* Assign a bit from the bitmask to every term in the FROM clause.
139215 140758       **
139216 140759       ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
139217 140760       **
139218 140761       ** The rule of the previous sentence ensures thta if X is the bitmask for
139219 140762       ** a table T, then X-1 is the bitmask for all other tables to the left of T.
................................................................................
139601 141144       if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
139602 141145         constructAutomaticIndex(pParse, &pWInfo->sWC,
139603 141146                   &pTabList->a[pLevel->iFrom], notReady, pLevel);
139604 141147         if( db->mallocFailed ) goto whereBeginError;
139605 141148       }
139606 141149   #endif
139607 141150       addrExplain = sqlite3WhereExplainOneScan(
139608         -        pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
       141151  +        pParse, pTabList, pLevel, wctrlFlags
139609 141152       );
139610 141153       pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
139611 141154       notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady);
139612 141155       pWInfo->iContinue = pLevel->addrCont;
139613 141156       if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){
139614 141157         sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
139615 141158       }
................................................................................
140033 141576   **                       This is typically a union of many types, one of
140034 141577   **                       which is sqlite3ParserTOKENTYPE.  The entry in the union
140035 141578   **                       for terminal symbols is called "yy0".
140036 141579   **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
140037 141580   **                       zero the stack is dynamically sized using realloc()
140038 141581   **    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
140039 141582   **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
       141583  +**    sqlite3ParserARG_PARAM     Code to pass %extra_argument as a subroutine parameter
140040 141584   **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
140041 141585   **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
       141586  +**    sqlite3ParserCTX_*         As sqlite3ParserARG_ except for %extra_context
140042 141587   **    YYERRORSYMBOL      is the code number of the error symbol.  If not
140043 141588   **                       defined, then do no error processing.
140044 141589   **    YYNSTATE           the combined number of states.
140045 141590   **    YYNRULE            the number of rules in the grammar
140046 141591   **    YYNTOKEN           Number of terminal symbols
140047 141592   **    YY_MAX_SHIFT       Maximum value for shift actions
140048 141593   **    YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
................................................................................
140054 141599   **    YY_MAX_REDUCE      Maximum value for reduce actions
140055 141600   */
140056 141601   #ifndef INTERFACE
140057 141602   # define INTERFACE 1
140058 141603   #endif
140059 141604   /************* Begin control #defines *****************************************/
140060 141605   #define YYCODETYPE unsigned char
140061         -#define YYNOCODE 253
       141606  +#define YYNOCODE 255
140062 141607   #define YYACTIONTYPE unsigned short int
140063         -#define YYWILDCARD 83
       141608  +#define YYWILDCARD 84
140064 141609   #define sqlite3ParserTOKENTYPE Token
140065 141610   typedef union {
140066 141611     int yyinit;
140067 141612     sqlite3ParserTOKENTYPE yy0;
140068         -  int yy4;
140069         -  struct TrigEvent yy90;
140070         -  TriggerStep* yy203;
140071         -  struct {int value; int mask;} yy215;
140072         -  SrcList* yy259;
140073         -  Expr* yy314;
140074         -  ExprList* yy322;
140075         -  const char* yy336;
140076         -  IdList* yy384;
140077         -  Select* yy387;
140078         -  With* yy451;
       141613  +  const char* yy36;
       141614  +  TriggerStep* yy47;
       141615  +  With* yy91;
       141616  +  struct {int value; int mask;} yy107;
       141617  +  Expr* yy182;
       141618  +  Upsert* yy198;
       141619  +  ExprList* yy232;
       141620  +  struct TrigEvent yy300;
       141621  +  Select* yy399;
       141622  +  SrcList* yy427;
       141623  +  int yy502;
       141624  +  IdList* yy510;
140079 141625   } YYMINORTYPE;
140080 141626   #ifndef YYSTACKDEPTH
140081 141627   #define YYSTACKDEPTH 100
140082 141628   #endif
140083         -#define sqlite3ParserARG_SDECL Parse *pParse;
140084         -#define sqlite3ParserARG_PDECL ,Parse *pParse
140085         -#define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
140086         -#define sqlite3ParserARG_STORE yypParser->pParse = pParse
       141629  +#define sqlite3ParserARG_SDECL
       141630  +#define sqlite3ParserARG_PDECL
       141631  +#define sqlite3ParserARG_PARAM
       141632  +#define sqlite3ParserARG_FETCH
       141633  +#define sqlite3ParserARG_STORE
       141634  +#define sqlite3ParserCTX_SDECL Parse *pParse;
       141635  +#define sqlite3ParserCTX_PDECL ,Parse *pParse
       141636  +#define sqlite3ParserCTX_PARAM ,pParse
       141637  +#define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
       141638  +#define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
140087 141639   #define YYFALLBACK 1
140088         -#define YYNSTATE             472
140089         -#define YYNRULE              333
140090         -#define YYNTOKEN             143
140091         -#define YY_MAX_SHIFT         471
140092         -#define YY_MIN_SHIFTREDUCE   681
140093         -#define YY_MAX_SHIFTREDUCE   1013
140094         -#define YY_ERROR_ACTION      1014
140095         -#define YY_ACCEPT_ACTION     1015
140096         -#define YY_NO_ACTION         1016
140097         -#define YY_MIN_REDUCE        1017
140098         -#define YY_MAX_REDUCE        1349
       141640  +#define YYNSTATE             490
       141641  +#define YYNRULE              341
       141642  +#define YYNTOKEN             145
       141643  +#define YY_MAX_SHIFT         489
       141644  +#define YY_MIN_SHIFTREDUCE   705
       141645  +#define YY_MAX_SHIFTREDUCE   1045
       141646  +#define YY_ERROR_ACTION      1046
       141647  +#define YY_ACCEPT_ACTION     1047
       141648  +#define YY_NO_ACTION         1048
       141649  +#define YY_MIN_REDUCE        1049
       141650  +#define YY_MAX_REDUCE        1389
140099 141651   /************* End control #defines *******************************************/
140100 141652   
140101 141653   /* Define the yytestcase() macro to be a no-op if is not already defined
140102 141654   ** otherwise.
140103 141655   **
140104 141656   ** Applications can choose to define yytestcase() in the %include section
140105 141657   ** to a macro that can assist in verifying code coverage.  For production
................................................................................
140157 141709   **  yy_shift_ofst[]    For each state, the offset into yy_action for
140158 141710   **                     shifting terminals.
140159 141711   **  yy_reduce_ofst[]   For each state, the offset into yy_action for
140160 141712   **                     shifting non-terminals after a reduce.
140161 141713   **  yy_default[]       Default action for each state.
140162 141714   **
140163 141715   *********** Begin parsing tables **********************************************/
140164         -#define YY_ACTTAB_COUNT (1566)
       141716  +#define YY_ACTTAB_COUNT (1657)
140165 141717   static const YYACTIONTYPE yy_action[] = {
140166         - /*     0 */  1169, 1015,  167,  167,    1,  168,  466, 1313,  466, 1083,
140167         - /*    10 */  1062,  466,   97,   94,  183, 1057,  466,  329, 1083,  342,
140168         - /*    20 */    97,   94,  183,  459,  459,  459,  436,   57,   57,   57,
140169         - /*    30 */    57,  807,   57,   57,  367,  367,  367,   57,   57,  808,
140170         - /*    40 */  1270, 1088, 1088,  104,  105,   95,  991,  991,  868,  871,
140171         - /*    50 */   860,  860,  102,  102,  103,  103,  103,  103,  233,  233,
140172         - /*    60 */   326, 1011,  449,  437,  449,  446,  351,  449,  461, 1142,
140173         - /*    70 */   463,  342,  449,  426, 1316,  209,  180,  742,   80,  299,
140174         - /*    80 */   857,  857,  869,  872,  101,  101,  101,  101,  100,  100,
140175         - /*    90 */    99,   99,   99,   98,  368,  104,  105,   95,  991,  991,
140176         - /*   100 */   868,  871,  860,  860,  102,  102,  103,  103,  103,  103,
140177         - /*   110 */    99,   99,   99,   98,  368,  355,   97,   94,  183,  228,
140178         - /*   120 */   106, 1012,  407,  342,  101,  101,  101,  101,  100,  100,
140179         - /*   130 */    99,   99,   99,   98,  368,  861,  101,  101,  101,  101,
140180         - /*   140 */   100,  100,   99,   99,   99,   98,  368,  104,  105,   95,
140181         - /*   150 */   991,  991,  868,  871,  860,  860,  102,  102,  103,  103,
140182         - /*   160 */   103,  103,  201,  368,  375,  420,  417,  416,  387,  273,
140183         - /*   170 */    65,   97,   94,  183,  168,  342,  415,  951, 1343,  396,
140184         - /*   180 */    66, 1343,  320,  959,  371,  970,  334,  340,  101,  101,
140185         - /*   190 */   101,  101,  100,  100,   99,   99,   99,   98,  368,  104,
140186         - /*   200 */   105,   95,  991,  991,  868,  871,  860,  860,  102,  102,
140187         - /*   210 */   103,  103,  103,  103,  373,  100,  100,   99,   99,   99,
140188         - /*   220 */    98,  368,  970,  971,  972,  201, 1100,  342,  420,  417,
140189         - /*   230 */   416,  287,  366,  365,  337,  970, 1162,  463,  949,  415,
140190         - /*   240 */   101,  101,  101,  101,  100,  100,   99,   99,   99,   98,
140191         - /*   250 */   368,  104,  105,   95,  991,  991,  868,  871,  860,  860,
140192         - /*   260 */   102,  102,  103,  103,  103,  103,  777,  241,  233,  233,
140193         - /*   270 */     9,  847,  970,  971,  972,  390,  998, 1141,  998,  342,
140194         - /*   280 */   463,  252,  829,  719,   98,  368,  840,  298,  338,  142,
140195         - /*   290 */   839,  339,  101,  101,  101,  101,  100,  100,   99,   99,
140196         - /*   300 */    99,   98,  368,  104,  105,   95,  991,  991,  868,  871,
140197         - /*   310 */   860,  860,  102,  102,  103,  103,  103,  103,  272,  466,
140198         - /*   320 */   392,  839,  839,  841,   97,   94,  183,  390, 1317,  253,
140199         - /*   330 */   456,  342,  125,  166,  807,  712,  208,  407,  386,  970,
140200         - /*   340 */    57,   57,  808,  238,  101,  101,  101,  101,  100,  100,
140201         - /*   350 */    99,   99,   99,   98,  368,  104,  105,   95,  991,  991,
140202         - /*   360 */   868,  871,  860,  860,  102,  102,  103,  103,  103,  103,
140203         - /*   370 */   466,  108,  466,  267,  465,  442,  970,  971,  972,  261,
140204         - /*   380 */   951, 1344,  909,  342, 1344,  142,  829,  848, 1292,  959,
140205         - /*   390 */   371,   55,   55,   57,   57,  242,  101,  101,  101,  101,
140206         - /*   400 */   100,  100,   99,   99,   99,   98,  368,  104,  105,   95,
140207         - /*   410 */   991,  991,  868,  871,  860,  860,  102,  102,  103,  103,
140208         - /*   420 */   103,  103,  272,  382,  262,  253,  456,  310,  364,  253,
140209         - /*   430 */   456,   86,  264,   84,  266,  342,  441,  176,  175,  834,
140210         - /*   440 */   464,  949,  767,  767,  332,  313, 1094,  396,  101,  101,
140211         - /*   450 */   101,  101,  100,  100,   99,   99,   99,   98,  368,  104,
140212         - /*   460 */   105,   95,  991,  991,  868,  871,  860,  860,  102,  102,
140213         - /*   470 */   103,  103,  103,  103,  227,  227,  233,  233,  233,  233,
140214         - /*   480 */   387,  273,  234,  234,  326,  950,  463,  342,  463,  298,
140215         - /*   490 */   463,  914,  914,  404,  463, 1037,  123,  265,   27,  970,
140216         - /*   500 */   101,  101,  101,  101,  100,  100,   99,   99,   99,   98,
140217         - /*   510 */   368,  104,  105,   95,  991,  991,  868,  871,  860,  860,
140218         - /*   520 */   102,  102,  103,  103,  103,  103,  435,  233,  233,  466,
140219         - /*   530 */   285,  686,  687,  688,  127,  271,  970,  971,  972,  463,
140220         - /*   540 */  1345,  327,  342,  407,  157, 1012,  988,   13,   13,  181,
140221         - /*   550 */    41,   41,  101,  101,  101,  101,  100,  100,   99,   99,
140222         - /*   560 */    99,   98,  368,  715,  794,  378,  104,  105,   95,  991,
140223         - /*   570 */   991,  868,  871,  860,  860,  102,  102,  103,  103,  103,
140224         - /*   580 */   103,  970,  378,  377,  346,  239,  847, 1086, 1086,  280,
140225         - /*   590 */  1169,  283,  204,  203,  202,  177,  298,  342,  407,  298,
140226         - /*   600 */   715,  840,  169,  299,  407,  839,   82,  101,  101,  101,
140227         - /*   610 */   101,  100,  100,   99,   99,   99,   98,  368,  970,  971,
140228         - /*   620 */   972,  104,  105,   95,  991,  991,  868,  871,  860,  860,
140229         - /*   630 */   102,  102,  103,  103,  103,  103,  839,  839,  841,  362,
140230         - /*   640 */   240,  124, 1169,  172,  126,  378, 1269, 1169, 1066,  342,
140231         - /*   650 */   253,  456,  407,  407,  407,  396,  352,  401,  407,  429,
140232         - /*   660 */   398,   85,  101,  101,  101,  101,  100,  100,   99,   99,
140233         - /*   670 */    99,   98,  368,  104,  105,   95,  991,  991,  868,  871,
140234         - /*   680 */   860,  860,  102,  102,  103,  103,  103,  103, 1169,  466,
140235         - /*   690 */   230,  233,  233,  792, 1235, 1095, 1091, 1293,    1,   77,
140236         - /*   700 */   278,  342,  205,  463,  974,  911, 1040,  348,  353,  911,
140237         - /*   710 */    42,   42,   79,  403,  101,  101,  101,  101,  100,  100,
140238         - /*   720 */    99,   99,   99,   98,  368,  104,   93,   95,  991,  991,
140239         - /*   730 */   868,  871,  860,  860,  102,  102,  103,  103,  103,  103,
140240         - /*   740 */   402,    9,  974,  243,  772,  458,  348,  232,  180,  771,
140241         - /*   750 */   946,  312,  342,  328,  363,  349,  143,  831,  389, 1278,
140242         - /*   760 */   211,  211,   21,  347,  432,  182,  101,  101,  101,  101,
140243         - /*   770 */   100,  100,   99,   99,   99,   98,  368,  105,   95,  991,
140244         - /*   780 */   991,  868,  871,  860,  860,  102,  102,  103,  103,  103,
140245         - /*   790 */   103,  792,  724,   22,  732,  731,  233,  233, 1239,  256,
140246         - /*   800 */   391,  274,  342,  211,   79,  360,  257,  413,  463,  397,
140247         - /*   810 */   207,  288,  260,  450,   79, 1239, 1241,  101,  101,  101,
140248         - /*   820 */   101,  100,  100,   99,   99,   99,   98,  368,   95,  991,
140249         - /*   830 */   991,  868,  871,  860,  860,  102,  102,  103,  103,  103,
140250         - /*   840 */   103,   91,  457,  296,    3,  233,  233,    5,  438,  212,
140251         - /*   850 */   331,  394,  739,  740,  295,  898,  894,  463,  460,  207,
140252         - /*   860 */   801, 1237,  722,  211,  698,  843, 1283,  101,  101,  101,
140253         - /*   870 */   101,  100,  100,   99,   99,   99,   98,  368, 1239,  380,
140254         - /*   880 */   357,  369,  233,  233,  989,  219,  236,  297,  423,  292,
140255         - /*   890 */   422,  206,  454,  898,  463,  970,   91,  457,  290,    3,
140256         - /*   900 */   722,  142,  268,  843,  847,  466, 1258,  149,  388,  425,
140257         - /*   910 */    88,   89,  769,  460,  930,   87,  447,   90,  369,  468,
140258         - /*   920 */   467,  385,  989,  839, 1257,  439,   57,   57,  395,  931,
140259         - /*   930 */  1065,  158,  970,  971,  972,  772,  369,  471, 1019,  399,
140260         - /*   940 */   771,  253,  456,  254,  932,  119,  891,  454,  233,  233,
140261         - /*   950 */     4,  970, 1096,  275,  839,  839,  841,  842,   19,  847,
140262         - /*   960 */   463,  449,  448,  163,  453,   88,   89,  776,  970, 1127,
140263         - /*   970 */   279,  930,   90,  369,  468,  467,   91,  457,  839,    3,
140264         - /*   980 */   235, 1064,  466, 1228,  233,  233,  931,  970,  970,  971,
140265         - /*   990 */   972,  970,  908,  460,  908,    2,  463,   81,  457,  212,
140266         - /*  1000 */     3,  932,  282,   10,   10,  970,  971,  972,  189,  839,
140267         - /*  1010 */   839,  841,  842,   19,  460,  284,  369,  354,  907,  286,
140268         - /*  1020 */   907,  753,  466, 1079,  970,  971,  972,  454,  970,  971,
140269         - /*  1030 */   972,  754,  970, 1063,  989,  372,  792,  369, 1118,  847,
140270         - /*  1040 */   291,  452,  466,   10,   10,   88,   89,  142,  454,  168,
140271         - /*  1050 */   300,  412,   90,  369,  468,  467,  793,  356,  839,  706,
140272         - /*  1060 */   847,  341,  121,   10,   10,  301,   88,   89,  379,  970,
140273         - /*  1070 */   971,  972,  989,   90,  369,  468,  467,  244,  205,  839,
140274         - /*  1080 */  1306,  245, 1135,  245,  250, 1168, 1114,  253,  456,  839,
140275         - /*  1090 */   839,  841,  842,   19, 1125,  237,  122,  451, 1174,  733,
140276         - /*  1100 */   324,  324,  323,  222,  321,  466, 1046,  695,  182,  225,
140277         - /*  1110 */   839,  839,  841,  842,   19,  103,  103,  103,  103,   96,
140278         - /*  1120 */   185,  466,  259, 1039, 1028,  170,   10,   10, 1027,  421,
140279         - /*  1130 */   258, 1029, 1300,  708,  792,  466,  408,  734,    8,  347,
140280         - /*  1140 */   444,  174,   12,   12,  290,  101,  101,  101,  101,  100,
140281         - /*  1150 */   100,   99,   99,   99,   98,  368,   32,   32,  466,  187,
140282         - /*  1160 */   466, 1111,  103,  103,  103,  103,  188,  466,  325,  138,
140283         - /*  1170 */   186,  708,  303,  305,  307,  358,  970,  270,  393,   43,
140284         - /*  1180 */    43,   44,   44, 1157,  333,  178,  418,  294,   45,   45,
140285         - /*  1190 */  1232,  318,  101,  101,  101,  101,  100,  100,   99,   99,
140286         - /*  1200 */    99,   98,  368,  381,  343,  366,  365,  466,  263,  253,
140287         - /*  1210 */   456,  466, 1062,  970,  971,  972, 1231,  997,  309,  466,
140288         - /*  1220 */   455,  466,  427,  466,  995,  173,  996, 1303,   46,   46,
140289         - /*  1230 */   145,  376,   37,   37, 1006, 1277,  466,  214, 1275,   64,
140290         - /*  1240 */    47,   47,   33,   33,   34,   34, 1003,   67,  466,  998,
140291         - /*  1250 */   350,  998,  466,  155,  233,  233,  466,   36,   36,   24,
140292         - /*  1260 */   140,   77, 1154,  466,  383,  466,  463,  428,  466,   48,
140293         - /*  1270 */    48,  466,  147,   49,   49,  466,  150,   50,   50,  466,
140294         - /*  1280 */   151,  152,  466,  384,   11,   11,   51,   51,  466,  110,
140295         - /*  1290 */   110,  153,   52,   52,  411,  466,   38,   38,  466,  191,
140296         - /*  1300 */    53,   53,  466,   54,   54,  466,  400,  466,  330,   39,
140297         - /*  1310 */    39,  466, 1164,  466,   25,  466,   56,   56,  466,  131,
140298         - /*  1320 */   131,   72,  466,  132,  132,  159,  133,  133,   61,   61,
140299         - /*  1330 */  1226,  195,   40,   40,  111,  111,   58,   58,  406,  112,
140300         - /*  1340 */   112,  466,  277,  113,  113,  466,  226,  466, 1246,  466,
140301         - /*  1350 */   197,  466,  164,  466,  409,  466,  198,  466,  199,  466,
140302         - /*  1360 */   335,  281,  109,  109,  466, 1030,  130,  130,  129,  129,
140303         - /*  1370 */   117,  117,  116,  116,  114,  114,  115,  115,   60,   60,
140304         - /*  1380 */    62,   62,  466,  359,  466,   59,   59,  424, 1082, 1081,
140305         - /*  1390 */  1080,  724, 1073, 1054,  336,  293, 1053, 1052, 1315,  431,
140306         - /*  1400 */   361,   76,  248,   31,   31,   35,   35, 1072,  249,  440,
140307         - /*  1410 */   302,  434,  213, 1122,    6,  311, 1212,  107,   83,  251,
140308         - /*  1420 */    78, 1123,  445,  220,  443, 1036,  304,   23, 1121,  469,
140309         - /*  1430 */   965,  221,  223, 1104,  314,  224,  344,  317,  315,  316,
140310         - /*  1440 */   470,  306, 1025, 1120,  308, 1262, 1020,  134,  120,  246,
140311         - /*  1450 */   682,  370,  171,  255, 1263,  135,  184, 1261, 1260,  374,
140312         - /*  1460 */   118,  906,  904,  827, 1050,  146,  136,  137,  148, 1049,
140313         - /*  1470 */    63, 1047,  756,  190,  269,  920,  154,  156,   68,   69,
140314         - /*  1480 */    70,   71,  139,  923,  192,  193,  144,  919,  345,  128,
140315         - /*  1490 */    14,  194,  276,  211, 1000,  405,  196,  161,  912,  160,
140316         - /*  1500 */    26,  697,  410,  295,  200,  289,  414,  162,  419,   73,
140317         - /*  1510 */    15,   16,  141,   74,   28,  247,  846,  845,  735,  874,
140318         - /*  1520 */   954,   75,  430,  955,   29,  433,  179,  229,  231,  800,
140319         - /*  1530 */   165,  795,   87,  210,  889,   79,  875,   17,  873,  877,
140320         - /*  1540 */   929,   18,  928,  216,  215,  878,   20,   30,  462,  844,
140321         - /*  1550 */   707,   92,  766,  770,    7,  322,  217,  218,  319, 1308,
140322         - /*  1560 */   960, 1016, 1016, 1016, 1016, 1307,
       141718  + /*     0 */   349,   99,   96,  185,   99,   96,  185,  233, 1047,    1,
       141719  + /*    10 */     1,  489,    2, 1051,  484,  477,  477,  477,  260,  351,
       141720  + /*    20 */   121, 1310, 1120, 1120, 1178, 1115, 1094, 1128,  380,  380,
       141721  + /*    30 */   380,  835,  454,  410, 1115,   59,   59, 1357,  425,  836,
       141722  + /*    40 */   710,  711,  712,  106,  107,   97, 1023, 1023,  900,  903,
       141723  + /*    50 */   892,  892,  104,  104,  105,  105,  105,  105,  346,  238,
       141724  + /*    60 */   238,   99,   96,  185,  238,  238,  889,  889,  901,  904,
       141725  + /*    70 */   460,  481,  351,   99,   96,  185,  481,  347, 1177,   82,
       141726  + /*    80 */   388,  214,  182,   23,  194,  103,  103,  103,  103,  102,
       141727  + /*    90 */   102,  101,  101,  101,  100,  381,  106,  107,   97, 1023,
       141728  + /*   100 */  1023,  900,  903,  892,  892,  104,  104,  105,  105,  105,
       141729  + /*   110 */   105,   10,  385,  484,   24,  484, 1333,  489,    2, 1051,
       141730  + /*   120 */   335, 1043,  108,  893,  260,  351,  121,   99,   96,  185,
       141731  + /*   130 */   100,  381,  386, 1128,   59,   59,   59,   59,  103,  103,
       141732  + /*   140 */   103,  103,  102,  102,  101,  101,  101,  100,  381,  106,
       141733  + /*   150 */   107,   97, 1023, 1023,  900,  903,  892,  892,  104,  104,
       141734  + /*   160 */   105,  105,  105,  105,  360,  238,  238,  170,  170,  467,
       141735  + /*   170 */   455,  467,  464,   67,  381,  329,  169,  481,  351,  343,
       141736  + /*   180 */   338,  400, 1044,   68,  101,  101,  101,  100,  381,  393,
       141737  + /*   190 */   194,  103,  103,  103,  103,  102,  102,  101,  101,  101,
       141738  + /*   200 */   100,  381,  106,  107,   97, 1023, 1023,  900,  903,  892,
       141739  + /*   210 */   892,  104,  104,  105,  105,  105,  105,  483,  385,  103,
       141740  + /*   220 */   103,  103,  103,  102,  102,  101,  101,  101,  100,  381,
       141741  + /*   230 */   268,  351,  946,  946,  422,  296,  102,  102,  101,  101,
       141742  + /*   240 */   101,  100,  381,  861,  103,  103,  103,  103,  102,  102,
       141743  + /*   250 */   101,  101,  101,  100,  381,  106,  107,   97, 1023, 1023,
       141744  + /*   260 */   900,  903,  892,  892,  104,  104,  105,  105,  105,  105,
       141745  + /*   270 */   484,  983, 1383,  206, 1353, 1383,  438,  435,  434,  281,
       141746  + /*   280 */   396,  269, 1089,  941,  351, 1002,  433,  861,  743,  401,
       141747  + /*   290 */   282,   57,   57,  482,  145,  791,  791,  103,  103,  103,
       141748  + /*   300 */   103,  102,  102,  101,  101,  101,  100,  381,  106,  107,
       141749  + /*   310 */    97, 1023, 1023,  900,  903,  892,  892,  104,  104,  105,
       141750  + /*   320 */   105,  105,  105,  281, 1002, 1003, 1004,  206,  879,  319,
       141751  + /*   330 */   438,  435,  434,  981,  259,  474,  360,  351, 1118, 1118,
       141752  + /*   340 */   433,  736,  379,  378,  872, 1002, 1356,  322,  871,  766,
       141753  + /*   350 */   103,  103,  103,  103,  102,  102,  101,  101,  101,  100,
       141754  + /*   360 */   381,  106,  107,   97, 1023, 1023,  900,  903,  892,  892,
       141755  + /*   370 */   104,  104,  105,  105,  105,  105,  484,  801,  484,  871,
       141756  + /*   380 */   871,  873,  401,  282, 1002, 1003, 1004, 1030,  360, 1030,
       141757  + /*   390 */   351,  983, 1384,  213,  880, 1384,  145,   59,   59,   59,
       141758  + /*   400 */    59, 1002,  244,  103,  103,  103,  103,  102,  102,  101,
       141759  + /*   410 */   101,  101,  100,  381,  106,  107,   97, 1023, 1023,  900,
       141760  + /*   420 */   903,  892,  892,  104,  104,  105,  105,  105,  105,  274,
       141761  + /*   430 */   484,  110,  467,  479,  467,  444,  259,  474,  232,  232,
       141762  + /*   440 */  1002, 1003, 1004,  351,  210,  335,  982,  866, 1385,  336,
       141763  + /*   450 */   481,   59,   59,  981,  245,  307,  103,  103,  103,  103,
       141764  + /*   460 */   102,  102,  101,  101,  101,  100,  381,  106,  107,   97,
       141765  + /*   470 */  1023, 1023,  900,  903,  892,  892,  104,  104,  105,  105,
       141766  + /*   480 */   105,  105,  453,  459,  484,  408,  377,  259,  474,  271,
       141767  + /*   490 */   183,  273,  209,  208,  207,  356,  351,  307,  178,  177,
       141768  + /*   500 */   127, 1006, 1098,   14,   14,   43,   43, 1044,  425,  103,
       141769  + /*   510 */   103,  103,  103,  102,  102,  101,  101,  101,  100,  381,
       141770  + /*   520 */   106,  107,   97, 1023, 1023,  900,  903,  892,  892,  104,
       141771  + /*   530 */   104,  105,  105,  105,  105,  294, 1132,  408,  160,  484,
       141772  + /*   540 */   408, 1006,  129,  962, 1209,  239,  239,  481,  307,  425,
       141773  + /*   550 */  1309, 1097,  351,  235,  243,  272,  820,  481,  963,  425,
       141774  + /*   560 */    11,   11,  103,  103,  103,  103,  102,  102,  101,  101,
       141775  + /*   570 */   101,  100,  381,  964,  362, 1002,  106,  107,   97, 1023,
       141776  + /*   580 */  1023,  900,  903,  892,  892,  104,  104,  105,  105,  105,
       141777  + /*   590 */   105, 1275,  161,  126,  777,  289, 1209,  292, 1072,  357,
       141778  + /*   600 */  1209, 1127,  476,  357,  778,  425,  247,  425,  351,  248,
       141779  + /*   610 */   414,  364,  414,  171, 1002, 1003, 1004,   84,  103,  103,
       141780  + /*   620 */   103,  103,  102,  102,  101,  101,  101,  100,  381, 1002,
       141781  + /*   630 */   184,  484,  106,  107,   97, 1023, 1023,  900,  903,  892,
       141782  + /*   640 */   892,  104,  104,  105,  105,  105,  105, 1123, 1209,  287,
       141783  + /*   650 */   484, 1209,   11,   11,  179,  820,  259,  474,  307,  237,
       141784  + /*   660 */   182,  351,  321,  365,  414,  308,  367,  366, 1002, 1003,
       141785  + /*   670 */  1004,   44,   44,   87,  103,  103,  103,  103,  102,  102,
       141786  + /*   680 */   101,  101,  101,  100,  381,  106,  107,   97, 1023, 1023,
       141787  + /*   690 */   900,  903,  892,  892,  104,  104,  105,  105,  105,  105,
       141788  + /*   700 */   246,  368,  280,  128,   10,  358,  146,  796,  835,  258,
       141789  + /*   710 */  1020,   88,  795,   86,  351,  421,  836,  943,  376,  348,
       141790  + /*   720 */   191,  943, 1318,  267,  308,  279,  456,  103,  103,  103,
       141791  + /*   730 */   103,  102,  102,  101,  101,  101,  100,  381,  106,   95,
       141792  + /*   740 */    97, 1023, 1023,  900,  903,  892,  892,  104,  104,  105,
       141793  + /*   750 */   105,  105,  105,  420,  249,  238,  238,  238,  238,   79,
       141794  + /*   760 */   375,  125,  305,   29,  262,  978,  351,  481,  337,  481,
       141795  + /*   770 */   756,  755,  304,  278,  415,   15,   81,  940, 1126,  940,
       141796  + /*   780 */   103,  103,  103,  103,  102,  102,  101,  101,  101,  100,
       141797  + /*   790 */   381,  107,   97, 1023, 1023,  900,  903,  892,  892,  104,
       141798  + /*   800 */   104,  105,  105,  105,  105,  457,  263,  484,  174,  484,
       141799  + /*   810 */   238,  238,  863,  407,  402,  216,  216,  351,  409,  193,
       141800  + /*   820 */   283,  216,  481,   81,  763,  764,  266,    5,   13,   13,
       141801  + /*   830 */    34,   34,  103,  103,  103,  103,  102,  102,  101,  101,
       141802  + /*   840 */   101,  100,  381,   97, 1023, 1023,  900,  903,  892,  892,
       141803  + /*   850 */   104,  104,  105,  105,  105,  105,   93,  475, 1002,    4,
       141804  + /*   860 */   403, 1002,  340,  431, 1002,  297,  212, 1277,   81,  746,
       141805  + /*   870 */  1163,  152,  926,  478,  166,  212,  757,  829,  930,  939,
       141806  + /*   880 */   216,  939,  858,  103,  103,  103,  103,  102,  102,  101,
       141807  + /*   890 */   101,  101,  100,  381,  238,  238,  382, 1002, 1003, 1004,
       141808  + /*   900 */  1002, 1003, 1004, 1002, 1003, 1004,  481,  439,  472,  746,
       141809  + /*   910 */   105,  105,  105,  105,   98,  758, 1162,  145,  930,  412,
       141810  + /*   920 */   879,  406,  793,   81,  395,   89,   90,   91,  105,  105,
       141811  + /*   930 */   105,  105, 1323,   92,  484,  382,  486,  485,  240,  275,
       141812  + /*   940 */   871,  103,  103,  103,  103,  102,  102,  101,  101,  101,
       141813  + /*   950 */   100,  381, 1096,  371,  355,   45,   45,  259,  474,  103,
       141814  + /*   960 */   103,  103,  103,  102,  102,  101,  101,  101,  100,  381,
       141815  + /*   970 */  1150,  871,  871,  873,  874,   21, 1332,  991,  384,  730,
       141816  + /*   980 */   722,  242,  123, 1298,  124,  875,  333,  333,  332,  227,
       141817  + /*   990 */   330,  991,  384,  719,  256,  242,  484,  391,  413, 1297,
       141818  + /*  1000 */   333,  333,  332,  227,  330,  748,  187,  719,  265,  470,
       141819  + /*  1010 */  1279, 1002,  484,  417,  391,  390,  264,   11,   11,  284,
       141820  + /*  1020 */   187,  732,  265,   93,  475,  875,    4, 1279, 1281,  419,
       141821  + /*  1030 */   264,  369,  416,   11,   11, 1159,  288,  484,  399, 1346,
       141822  + /*  1040 */   478,  379,  378,  291,  484,  293,  189,  250,  295, 1027,
       141823  + /*  1050 */  1002, 1003, 1004,  190, 1029, 1111,  140,  188,   11,   11,
       141824  + /*  1060 */   189,  732, 1028,  382,  923,   46,   46,  190, 1095,  230,
       141825  + /*  1070 */   140,  188,  462,   93,  475,  472,    4,  300,  309,  391,
       141826  + /*  1080 */   373,    6, 1069,  217,  739,  310, 1030,  879, 1030, 1171,
       141827  + /*  1090 */   478,  352, 1279,   90,   91,  800,  259,  474, 1208,  484,
       141828  + /*  1100 */    92, 1268,  382,  486,  485,  352, 1002,  871,  879,  426,
       141829  + /*  1110 */   259,  474,  172,  382,  238,  238, 1146,  170, 1021,  389,
       141830  + /*  1120 */    47,   47, 1157,  739,  872,  472,  481,  469,  871,  350,
       141831  + /*  1130 */  1214,   83,  475,  389,    4, 1078, 1071,  879,  871,  871,
       141832  + /*  1140 */   873,  874,   21,   90,   91, 1002, 1003, 1004,  478,  251,
       141833  + /*  1150 */    92,  251,  382,  486,  485,  443,  370,  871, 1021,  871,
       141834  + /*  1160 */   871,  873,  224,  241,  306,  441,  301,  440,  211, 1060,
       141835  + /*  1170 */   820,  382,  822,  447,  299, 1059,  484, 1061, 1143,  962,
       141836  + /*  1180 */   430,  796,  484,  472, 1340,  312,  795,  465,  871,  871,
       141837  + /*  1190 */   873,  874,   21,  314,  963,  879,  316,   59,   59, 1002,
       141838  + /*  1200 */     9,   90,   91,   48,   48,  238,  238,  210,   92,  964,
       141839  + /*  1210 */   382,  486,  485,  176,  334,  871,  242,  481, 1193,  238,
       141840  + /*  1220 */   238,  333,  333,  332,  227,  330,  394,  270,  719,  277,
       141841  + /*  1230 */   471,  481,  467,  466,  484,  145,  217, 1201, 1002, 1003,
       141842  + /*  1240 */  1004,  187,    3,  265,  184,  445,  871,  871,  873,  874,
       141843  + /*  1250 */    21,  264, 1337,  450, 1051,   39,   39,  392,  356,  260,
       141844  + /*  1260 */   342,  121,  468,  411,  436,  821,  180, 1094, 1128,  820,
       141845  + /*  1270 */   303, 1021, 1272, 1271,  299,  259,  474,  238,  238, 1002,
       141846  + /*  1280 */   473,  189,  484,  318,  327,  238,  238,  484,  190,  481,
       141847  + /*  1290 */   446,  140,  188, 1343,  238,  238, 1038,  481,  148,  175,
       141848  + /*  1300 */   238,  238,  484,   49,   49,  219,  481,  484,   35,   35,
       141849  + /*  1310 */  1317, 1021,  481,  484, 1035,  484, 1315,  484, 1002, 1003,
       141850  + /*  1320 */  1004,  484,   66,   36,   36,  194,  352,  484,   38,   38,
       141851  + /*  1330 */   484,  259,  474,   69,   50,   50,   51,   51,   52,   52,
       141852  + /*  1340 */   359,  484,   12,   12,  484, 1198,  484,  158,   53,   53,
       141853  + /*  1350 */   405,  112,  112,  385,  389,  484,   26,  484,  143,  484,
       141854  + /*  1360 */   150,  484,   54,   54,  397,   40,   40,   55,   55,  484,
       141855  + /*  1370 */    79,  484,  153, 1190,  484,  154,   56,   56,   41,   41,
       141856  + /*  1380 */    58,   58,  133,  133,  484,  398,  484,  429,  484,  155,
       141857  + /*  1390 */   134,  134,  135,  135,  484,   63,   63,  484,  341,  484,
       141858  + /*  1400 */   339,  484,  196,  484,  156,   42,   42,  113,  113,   60,
       141859  + /*  1410 */    60,  484,  404,  484,   27,  114,  114, 1204,  115,  115,
       141860  + /*  1420 */   111,  111,  132,  132,  131,  131, 1266,  418,  484,  162,
       141861  + /*  1430 */   484,  200,  119,  119,  118,  118,  484,   74,  424,  484,
       141862  + /*  1440 */  1286,  484,  231,  484,  202,  484,  167,  286,  427,  116,
       141863  + /*  1450 */   116,  117,  117,  290,  203,  442, 1062,   62,   62,  204,
       141864  + /*  1460 */    64,   64,   61,   61,   33,   33,   37,   37,  344,  372,
       141865  + /*  1470 */  1114, 1105,  748, 1113,  374, 1112,  254,  458, 1086,  255,
       141866  + /*  1480 */   345, 1085,  302, 1084, 1355,   78, 1154,  311, 1104,  449,
       141867  + /*  1490 */   452, 1155, 1153,  218,    7,  313,  315,  320, 1152,   85,
       141868  + /*  1500 */  1252,  317,  109,   80,  463,  225,  461, 1068,   25,  487,
       141869  + /*  1510 */   997,  323,  257,  226,  229,  228, 1136,  324,  325,  326,
       141870  + /*  1520 */   488,  136, 1057, 1052, 1302, 1303, 1301,  706, 1300,  137,
       141871  + /*  1530 */   122,  138,  383,  173, 1082,  261,  186,  252, 1081,   65,
       141872  + /*  1540 */   387,  120,  938,  936,  855,  353,  149, 1079,  139,  151,
       141873  + /*  1550 */   192,  780,  195,  276,  952,  157,  141,  361,   70,  363,
       141874  + /*  1560 */   859,  159,   71,   72,  142,   73,  955,  354,  147,  197,
       141875  + /*  1570 */   198,  951,  130,   16,  199,  285,  216, 1032,  201,  423,
       141876  + /*  1580 */   164,  944,  163,   28,  721,  428,  304,  165,  205,  759,
       141877  + /*  1590 */    75,  432,  298,   17,   18,  437,   76,  253,  878,  144,
       141878  + /*  1600 */   877,  906,   77,  986,   30,  448,  987,   31,  451,  181,
       141879  + /*  1610 */   234,  236,  168,  828,  823,   89,  910,  921,   81,  907,
       141880  + /*  1620 */   215,  905,  909,  961,  960,   19,  221,   20,  220,   22,
       141881  + /*  1630 */    32,  331,  876,  731,   94,  790,  794,    8,  992,  222,
       141882  + /*  1640 */   480,  328, 1048, 1048, 1048, 1048, 1048, 1048, 1048, 1048,
       141883  + /*  1650 */   223, 1048, 1048, 1048, 1048, 1348, 1347,
140323 141884   };
140324 141885   static const YYCODETYPE yy_lookahead[] = {
140325         - /*     0 */   152,  144,  145,  146,  147,  152,  152,  172,  152,  180,
140326         - /*    10 */   181,  152,  223,  224,  225,  180,  152,  164,  189,   19,
140327         - /*    20 */   223,  224,  225,  168,  169,  170,  163,  173,  174,  173,
140328         - /*    30 */   174,   31,  173,  174,  168,  169,  170,  173,  174,   39,
140329         - /*    40 */   243,  191,  192,   43,   44,   45,   46,   47,   48,   49,
140330         - /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  195,  196,
140331         - /*    60 */    22,   23,  208,  209,  208,  209,  218,  208,  209,  176,
140332         - /*    70 */   207,   19,  208,  209,   23,  212,  213,   26,   26,  152,
140333         - /*    80 */    46,   47,   48,   49,   84,   85,   86,   87,   88,   89,
140334         - /*    90 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
140335         - /*   100 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
140336         - /*   110 */    90,   91,   92,   93,   94,  188,  223,  224,  225,  171,
140337         - /*   120 */    68,   83,  152,   19,   84,   85,   86,   87,   88,   89,
140338         - /*   130 */    90,   91,   92,   93,   94,  101,   84,   85,   86,   87,
140339         - /*   140 */    88,   89,   90,   91,   92,   93,   94,   43,   44,   45,
140340         - /*   150 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
140341         - /*   160 */    56,   57,   99,   94,  194,  102,  103,  104,  109,  110,
140342         - /*   170 */    66,  223,  224,  225,  152,   19,  113,   22,   23,  152,
140343         - /*   180 */    24,   26,  160,    1,    2,   59,  164,  173,   84,   85,
140344         - /*   190 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   43,
140345         - /*   200 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
140346         - /*   210 */    54,   55,   56,   57,  244,   88,   89,   90,   91,   92,
140347         - /*   220 */    93,   94,   96,   97,   98,   99,  196,   19,  102,  103,
140348         - /*   230 */   104,   23,   88,   89,  173,   59,  163,  207,   83,  113,
140349         - /*   240 */    84,   85,   86,   87,   88,   89,   90,   91,   92,   93,
140350         - /*   250 */    94,   43,   44,   45,   46,   47,   48,   49,   50,   51,
140351         - /*   260 */    52,   53,   54,   55,   56,   57,   90,  240,  195,  196,
140352         - /*   270 */   171,   82,   96,   97,   98,  152,  132,  176,  134,   19,
140353         - /*   280 */   207,  200,   72,   23,   93,   94,   97,  152,  173,   79,
140354         - /*   290 */   101,  210,   84,   85,   86,   87,   88,   89,   90,   91,
140355         - /*   300 */    92,   93,   94,   43,   44,   45,   46,   47,   48,   49,
140356         - /*   310 */    50,   51,   52,   53,   54,   55,   56,   57,  108,  152,
140357         - /*   320 */   152,  132,  133,  134,  223,  224,  225,  152,  186,  119,
140358         - /*   330 */   120,   19,  197,  234,   31,   23,   26,  152,  239,   59,
140359         - /*   340 */   173,  174,   39,  220,   84,   85,   86,   87,   88,   89,
140360         - /*   350 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
140361         - /*   360 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
140362         - /*   370 */   152,   22,  152,   16,  152,  208,   96,   97,   98,  194,
140363         - /*   380 */    22,   23,   11,   19,   26,   79,   72,   23,    0,    1,
140364         - /*   390 */     2,  173,  174,  173,  174,  220,   84,   85,   86,   87,
140365         - /*   400 */    88,   89,   90,   91,   92,   93,   94,   43,   44,   45,
140366         - /*   410 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
140367         - /*   420 */    56,   57,  108,  109,  110,  119,  120,  152,  208,  119,
140368         - /*   430 */   120,  137,   75,  139,   77,   19,  152,   88,   89,   23,
140369         - /*   440 */   115,   83,  117,  118,  163,  227,  163,  152,   84,   85,
140370         - /*   450 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   43,
140371         - /*   460 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
140372         - /*   470 */    54,   55,   56,   57,  195,  196,  195,  196,  195,  196,
140373         - /*   480 */   109,  110,  195,  196,   22,   23,  207,   19,  207,  152,
140374         - /*   490 */   207,  108,  109,  110,  207,  163,   22,  140,   24,   59,
140375         - /*   500 */    84,   85,   86,   87,   88,   89,   90,   91,   92,   93,
140376         - /*   510 */    94,   43,   44,   45,   46,   47,   48,   49,   50,   51,
140377         - /*   520 */    52,   53,   54,   55,   56,   57,  152,  195,  196,  152,
140378         - /*   530 */    16,    7,    8,    9,  197,  240,   96,   97,   98,  207,
140379         - /*   540 */   249,  250,   19,  152,   22,   83,   26,  173,  174,  152,
140380         - /*   550 */   173,  174,   84,   85,   86,   87,   88,   89,   90,   91,
140381         - /*   560 */    92,   93,   94,   59,  124,  152,   43,   44,   45,   46,
140382         - /*   570 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
140383         - /*   580 */    57,   59,  169,  170,  157,  194,   82,  191,  192,   75,
140384         - /*   590 */   152,   77,  108,  109,  110,   26,  152,   19,  152,  152,
140385         - /*   600 */    96,   97,   24,  152,  152,  101,  138,   84,   85,   86,
140386         - /*   610 */    87,   88,   89,   90,   91,   92,   93,   94,   96,   97,
140387         - /*   620 */    98,   43,   44,   45,   46,   47,   48,   49,   50,   51,
140388         - /*   630 */    52,   53,   54,   55,   56,   57,  132,  133,  134,  188,
140389         - /*   640 */   194,  197,  152,  123,  197,  232,  194,  152,  182,   19,
140390         - /*   650 */   119,  120,  152,  152,  152,  152,  218,  230,  152,  163,
140391         - /*   660 */   233,  138,   84,   85,   86,   87,   88,   89,   90,   91,
140392         - /*   670 */    92,   93,   94,   43,   44,   45,   46,   47,   48,   49,
140393         - /*   680 */    50,   51,   52,   53,   54,   55,   56,   57,  152,  152,
140394         - /*   690 */    23,  195,  196,   26,  194,  194,  194,  146,  147,  130,
140395         - /*   700 */   194,   19,   46,  207,   59,   29,  166,  167,  218,   33,
140396         - /*   710 */   173,  174,   26,  218,   84,   85,   86,   87,   88,   89,
140397         - /*   720 */    90,   91,   92,   93,   94,   43,   44,   45,   46,   47,
140398         - /*   730 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
140399         - /*   740 */    64,  171,   97,  240,  116,  166,  167,  212,  213,  121,
140400         - /*   750 */    23,  152,   19,   26,  218,  247,  248,   23,   23,  152,
140401         - /*   760 */    26,   26,   22,  107,  163,   98,   84,   85,   86,   87,
140402         - /*   770 */    88,   89,   90,   91,   92,   93,   94,   44,   45,   46,
140403         - /*   780 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
140404         - /*   790 */    57,  124,  106,   53,  100,  101,  195,  196,  152,  152,
140405         - /*   800 */    23,   23,   19,   26,   26,   19,  152,   23,  207,  239,
140406         - /*   810 */    26,   23,  152,  163,   26,  169,  170,   84,   85,   86,
140407         - /*   820 */    87,   88,   89,   90,   91,   92,   93,   94,   45,   46,
140408         - /*   830 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
140409         - /*   840 */    57,   19,   20,  101,   22,  195,  196,   22,   19,   24,
140410         - /*   850 */   163,   19,    7,    8,  112,   59,   23,  207,   36,   26,
140411         - /*   860 */    23,  152,   59,   26,   21,   59,  152,   84,   85,   86,
140412         - /*   870 */    87,   88,   89,   90,   91,   92,   93,   94,  232,  221,
140413         - /*   880 */    94,   59,  195,  196,   59,   99,  100,  101,  102,  103,
140414         - /*   890 */   104,  105,   70,   97,  207,   59,   19,   20,  112,   22,
140415         - /*   900 */    97,   79,  152,   97,   82,  152,  152,   71,  221,   90,
140416         - /*   910 */    88,   89,   23,   36,   12,   26,  163,   95,   96,   97,
140417         - /*   920 */    98,   78,   97,  101,  152,   96,  173,  174,   96,   27,
140418         - /*   930 */   182,   22,   96,   97,   98,  116,   59,  148,  149,  152,
140419         - /*   940 */   121,  119,  120,  154,   42,  156,  103,   70,  195,  196,
140420         - /*   950 */    22,   59,  163,  152,  132,  133,  134,  135,  136,   82,
140421         - /*   960 */   207,  208,  209,   71,   62,   88,   89,   90,   59,  152,
140422         - /*   970 */   152,   12,   95,   96,   97,   98,   19,   20,  101,   22,
140423         - /*   980 */    22,  182,  152,  140,  195,  196,   27,   59,   96,   97,
140424         - /*   990 */    98,   59,  132,   36,  134,   22,  207,   19,   20,   24,
140425         - /*  1000 */    22,   42,  152,  173,  174,   96,   97,   98,  219,  132,
140426         - /*  1010 */   133,  134,  135,  136,   36,  152,   59,  187,  132,  152,
140427         - /*  1020 */   134,   62,  152,  152,   96,   97,   98,   70,   96,   97,
140428         - /*  1030 */    98,   72,   59,  152,   59,  246,   26,   59,  214,   82,
140429         - /*  1040 */   152,  192,  152,  173,  174,   88,   89,   79,   70,  152,
140430         - /*  1050 */   152,   19,   95,   96,   97,   98,  124,  187,  101,   23,
140431         - /*  1060 */    82,  164,   26,  173,  174,  152,   88,   89,  100,   96,
140432         - /*  1070 */    97,   98,   97,   95,   96,   97,   98,  187,   46,  101,
140433         - /*  1080 */   122,  184,  152,  186,  211,  152,  152,  119,  120,  132,
140434         - /*  1090 */   133,  134,  135,  136,  152,    5,   22,  152,  152,   35,
140435         - /*  1100 */    10,   11,   12,   13,   14,  152,  152,   17,   98,  235,
140436         - /*  1110 */   132,  133,  134,  135,  136,   54,   55,   56,   57,   58,
140437         - /*  1120 */    30,  152,   32,  152,  152,  198,  173,  174,  152,   65,
140438         - /*  1130 */    40,  152,  152,   59,  124,  152,  236,   73,  199,  107,
140439         - /*  1140 */   187,  171,  173,  174,  112,   84,   85,   86,   87,   88,
140440         - /*  1150 */    89,   90,   91,   92,   93,   94,  173,  174,  152,   69,
140441         - /*  1160 */   152,  211,   54,   55,   56,   57,   76,  152,  150,   79,
140442         - /*  1170 */    80,   97,  211,  211,  211,  111,   59,  241,  241,  173,
140443         - /*  1180 */   174,  173,  174,  202,  202,  185,  177,  176,  173,  174,
140444         - /*  1190 */   176,  201,   84,   85,   86,   87,   88,   89,   90,   91,
140445         - /*  1200 */    92,   93,   94,  215,  114,   88,   89,  152,  215,  119,
140446         - /*  1210 */   120,  152,  181,   96,   97,   98,  176,  100,  215,  152,
140447         - /*  1220 */   229,  152,  163,  152,  107,  199,  109,  155,  173,  174,
140448         - /*  1230 */   245,  141,  173,  174,   60,  159,  152,  122,  159,  242,
140449         - /*  1240 */   173,  174,  173,  174,  173,  174,   38,  242,  152,  132,
140450         - /*  1250 */   159,  134,  152,   22,  195,  196,  152,  173,  174,  222,
140451         - /*  1260 */    43,  130,  202,  152,   18,  152,  207,  208,  152,  173,
140452         - /*  1270 */   174,  152,  190,  173,  174,  152,  193,  173,  174,  152,
140453         - /*  1280 */   193,  193,  152,  159,  173,  174,  173,  174,  152,  173,
140454         - /*  1290 */   174,  193,  173,  174,   18,  152,  173,  174,  152,  158,
140455         - /*  1300 */   173,  174,  152,  173,  174,  152,  159,  152,  202,  173,
140456         - /*  1310 */   174,  152,  190,  152,  222,  152,  173,  174,  152,  173,
140457         - /*  1320 */   174,  137,  152,  173,  174,  190,  173,  174,  173,  174,
140458         - /*  1330 */   202,  158,  173,  174,  173,  174,  173,  174,   61,  173,
140459         - /*  1340 */   174,  152,  237,  173,  174,  152,  159,  152,  238,  152,
140460         - /*  1350 */   158,  152,   22,  152,  178,  152,  158,  152,  158,  152,
140461         - /*  1360 */   178,  159,  173,  174,  152,  159,  173,  174,  173,  174,
140462         - /*  1370 */   173,  174,  173,  174,  173,  174,  173,  174,  173,  174,
140463         - /*  1380 */   173,  174,  152,   63,  152,  173,  174,  107,  175,  175,
140464         - /*  1390 */   175,  106,  183,  175,  178,  175,  177,  175,  175,  178,
140465         - /*  1400 */    94,  107,  231,  173,  174,  173,  174,  183,  231,  125,
140466         - /*  1410 */   216,  178,  159,  217,   22,  159,  226,  129,  137,  228,
140467         - /*  1420 */   128,  217,  126,   25,  127,  162,  216,   26,  217,  161,
140468         - /*  1430 */    13,  153,  153,  206,  205,    6,  251,  202,  204,  203,
140469         - /*  1440 */   151,  216,  151,  217,  216,  171,  151,  165,  179,  179,
140470         - /*  1450 */     4,    3,   22,  142,  171,  165,   15,  171,  171,   81,
140471         - /*  1460 */    16,   23,   23,  120,  171,  131,  165,  111,  123,  171,
140472         - /*  1470 */   171,  171,   20,  125,   16,    1,  123,  131,   53,   53,
140473         - /*  1480 */    53,   53,  111,   96,   34,  122,  248,    1,  251,    5,
140474         - /*  1490 */    22,  107,  140,   26,   74,   41,  122,  107,   67,   67,
140475         - /*  1500 */    24,   20,   19,  112,  105,   23,   66,   22,   66,   22,
140476         - /*  1510 */    22,   22,   37,   22,   22,   66,   23,   23,   28,   23,
140477         - /*  1520 */    23,   26,   24,   23,   22,   24,  122,   23,   23,   96,
140478         - /*  1530 */    22,  124,   26,   34,   23,   26,   23,   34,   23,   23,
140479         - /*  1540 */    23,   34,   23,   22,   26,   11,   22,   22,   26,   23,
140480         - /*  1550 */    23,   22,  116,   23,   22,   15,  122,  122,   23,  122,
140481         - /*  1560 */     1,  252,  252,  252,  252,  122,  252,  252,  252,  252,
140482         - /*  1570 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140483         - /*  1580 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140484         - /*  1590 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140485         - /*  1600 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140486         - /*  1610 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140487         - /*  1620 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140488         - /*  1630 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140489         - /*  1640 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140490         - /*  1650 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140491         - /*  1660 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140492         - /*  1670 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140493         - /*  1680 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140494         - /*  1690 */   252,  252,  252,  252,  252,  252,  252,  252,  252,  252,
140495         - /*  1700 */   252,  252,  252,  252,  252,  252,  252,  252,  252,
140496         -};
140497         -#define YY_SHIFT_COUNT    (471)
       141886  + /*     0 */   174,  226,  227,  228,  226,  227,  228,  172,  145,  146,
       141887  + /*    10 */   147,  148,  149,  150,  153,  169,  170,  171,  155,   19,
       141888  + /*    20 */   157,  246,  192,  193,  177,  181,  182,  164,  169,  170,
       141889  + /*    30 */   171,   31,  164,  153,  190,  174,  175,  187,  153,   39,
       141890  + /*    40 */     7,    8,    9,   43,   44,   45,   46,   47,   48,   49,
       141891  + /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  174,  196,
       141892  + /*    60 */   197,  226,  227,  228,  196,  197,   46,   47,   48,   49,
       141893  + /*    70 */   209,  208,   19,  226,  227,  228,  208,  174,  177,   26,
       141894  + /*    80 */   195,  213,  214,   22,  221,   85,   86,   87,   88,   89,
       141895  + /*    90 */    90,   91,   92,   93,   94,   95,   43,   44,   45,   46,
       141896  + /*   100 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
       141897  + /*   110 */    57,  172,  249,  153,   53,  153,  147,  148,  149,  150,
       141898  + /*   120 */    22,   23,   69,  103,  155,   19,  157,  226,  227,  228,
       141899  + /*   130 */    94,   95,  247,  164,  174,  175,  174,  175,   85,   86,
       141900  + /*   140 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   43,
       141901  + /*   150 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
       141902  + /*   160 */    54,   55,   56,   57,  153,  196,  197,  153,  153,  209,
       141903  + /*   170 */   210,  209,  210,   67,   95,  161,  237,  208,   19,  165,
       141904  + /*   180 */   165,  242,   84,   24,   91,   92,   93,   94,   95,  223,
       141905  + /*   190 */   221,   85,   86,   87,   88,   89,   90,   91,   92,   93,
       141906  + /*   200 */    94,   95,   43,   44,   45,   46,   47,   48,   49,   50,
       141907  + /*   210 */    51,   52,   53,   54,   55,   56,   57,  153,  249,   85,
       141908  + /*   220 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
       141909  + /*   230 */   219,   19,  109,  110,  111,   23,   89,   90,   91,   92,
       141910  + /*   240 */    93,   94,   95,   73,   85,   86,   87,   88,   89,   90,
       141911  + /*   250 */    91,   92,   93,   94,   95,   43,   44,   45,   46,   47,
       141912  + /*   260 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
       141913  + /*   270 */   153,   22,   23,  101,  173,   26,  104,  105,  106,  109,
       141914  + /*   280 */   110,  111,  181,   11,   19,   59,  114,   73,   23,  110,
       141915  + /*   290 */   111,  174,  175,  116,   80,  118,  119,   85,   86,   87,
       141916  + /*   300 */    88,   89,   90,   91,   92,   93,   94,   95,   43,   44,
       141917  + /*   310 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
       141918  + /*   320 */    55,   56,   57,  109,   98,   99,  100,  101,   83,  153,
       141919  + /*   330 */   104,  105,  106,   84,  120,  121,  153,   19,  192,  193,
       141920  + /*   340 */   114,   23,   89,   90,   99,   59,   23,  230,  103,   26,
       141921  + /*   350 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
       141922  + /*   360 */    95,   43,   44,   45,   46,   47,   48,   49,   50,   51,
       141923  + /*   370 */    52,   53,   54,   55,   56,   57,  153,   91,  153,  134,
       141924  + /*   380 */   135,  136,  110,  111,   98,   99,  100,  134,  153,  136,
       141925  + /*   390 */    19,   22,   23,   26,   23,   26,   80,  174,  175,  174,
       141926  + /*   400 */   175,   59,  219,   85,   86,   87,   88,   89,   90,   91,
       141927  + /*   410 */    92,   93,   94,   95,   43,   44,   45,   46,   47,   48,
       141928  + /*   420 */    49,   50,   51,   52,   53,   54,   55,   56,   57,   16,
       141929  + /*   430 */   153,   22,  209,  210,  209,  210,  120,  121,  196,  197,
       141930  + /*   440 */    98,   99,  100,   19,   46,   22,   23,   23,  252,  253,
       141931  + /*   450 */   208,  174,  175,   84,  219,  153,   85,   86,   87,   88,
       141932  + /*   460 */    89,   90,   91,   92,   93,   94,   95,   43,   44,   45,
       141933  + /*   470 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
       141934  + /*   480 */    56,   57,  153,  153,  153,  153,  209,  120,  121,   76,
       141935  + /*   490 */   153,   78,  109,  110,  111,   97,   19,  153,   89,   90,
       141936  + /*   500 */   198,   59,  183,  174,  175,  174,  175,   84,  153,   85,
       141937  + /*   510 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
       141938  + /*   520 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
       141939  + /*   530 */    53,   54,   55,   56,   57,   16,  197,  153,   22,  153,
       141940  + /*   540 */   153,   99,  198,   12,  153,  196,  197,  208,  153,  153,
       141941  + /*   550 */   195,  183,   19,   23,  222,  142,   26,  208,   27,  153,
       141942  + /*   560 */   174,  175,   85,   86,   87,   88,   89,   90,   91,   92,
       141943  + /*   570 */    93,   94,   95,   42,  188,   59,   43,   44,   45,   46,
       141944  + /*   580 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
       141945  + /*   590 */    57,  195,   22,  198,   63,   76,  153,   78,  167,  168,
       141946  + /*   600 */   153,  195,  167,  168,   73,  153,  222,  153,   19,  222,
       141947  + /*   610 */   153,  220,  153,   24,   98,   99,  100,  140,   85,   86,
       141948  + /*   620 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   59,
       141949  + /*   630 */   100,  153,   43,   44,   45,   46,   47,   48,   49,   50,
       141950  + /*   640 */    51,   52,   53,   54,   55,   56,   57,  195,  153,  195,
       141951  + /*   650 */   153,  153,  174,  175,   26,  125,  120,  121,  153,  213,
       141952  + /*   660 */   214,   19,  153,  220,  153,  153,  188,  220,   98,   99,
       141953  + /*   670 */   100,  174,  175,  140,   85,   86,   87,   88,   89,   90,
       141954  + /*   680 */    91,   92,   93,   94,   95,   43,   44,   45,   46,   47,
       141955  + /*   690 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
       141956  + /*   700 */   243,  189,  243,  198,  172,  250,  251,  117,   31,  201,
       141957  + /*   710 */    26,  139,  122,  141,   19,  220,   39,   29,  220,  211,
       141958  + /*   720 */    24,   33,  153,  164,  153,  164,   19,   85,   86,   87,
       141959  + /*   730 */    88,   89,   90,   91,   92,   93,   94,   95,   43,   44,
       141960  + /*   740 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
       141961  + /*   750 */    55,   56,   57,   65,  243,  196,  197,  196,  197,  131,
       141962  + /*   760 */   189,   22,  103,   24,  153,   23,   19,  208,   26,  208,
       141963  + /*   770 */   102,  103,  113,   23,  242,   22,   26,  134,  164,  136,
       141964  + /*   780 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
       141965  + /*   790 */    95,   44,   45,   46,   47,   48,   49,   50,   51,   52,
       141966  + /*   800 */    53,   54,   55,   56,   57,   98,  153,  153,  124,  153,
       141967  + /*   810 */   196,  197,   23,   23,   61,   26,   26,   19,   23,  123,
       141968  + /*   820 */    23,   26,  208,   26,    7,    8,  153,   22,  174,  175,
       141969  + /*   830 */   174,  175,   85,   86,   87,   88,   89,   90,   91,   92,
       141970  + /*   840 */    93,   94,   95,   45,   46,   47,   48,   49,   50,   51,
       141971  + /*   850 */    52,   53,   54,   55,   56,   57,   19,   20,   59,   22,
       141972  + /*   860 */   111,   59,  164,   23,   59,   23,   26,  153,   26,   59,
       141973  + /*   870 */   153,   72,   23,   36,   72,   26,   35,   23,   59,  134,
       141974  + /*   880 */    26,  136,  133,   85,   86,   87,   88,   89,   90,   91,
       141975  + /*   890 */    92,   93,   94,   95,  196,  197,   59,   98,   99,  100,
       141976  + /*   900 */    98,   99,  100,   98,   99,  100,  208,   66,   71,   99,
       141977  + /*   910 */    54,   55,   56,   57,   58,   74,  153,   80,   99,   19,
       141978  + /*   920 */    83,  223,   23,   26,  153,   26,   89,   90,   54,   55,
       141979  + /*   930 */    56,   57,  153,   96,  153,   98,   99,  100,   22,  153,
       141980  + /*   940 */   103,   85,   86,   87,   88,   89,   90,   91,   92,   93,
       141981  + /*   950 */    94,   95,  183,  112,  158,  174,  175,  120,  121,   85,
       141982  + /*   960 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
       141983  + /*   970 */   215,  134,  135,  136,  137,  138,    0,    1,    2,   23,
       141984  + /*   980 */    21,    5,   26,  153,   22,   59,   10,   11,   12,   13,
       141985  + /*   990 */    14,    1,    2,   17,  212,    5,  153,  153,   98,  153,
       141986  + /*  1000 */    10,   11,   12,   13,   14,  108,   30,   17,   32,  193,
       141987  + /*  1010 */   153,   59,  153,  153,  170,  171,   40,  174,  175,  153,
       141988  + /*  1020 */    30,   59,   32,   19,   20,   99,   22,  170,  171,  233,
       141989  + /*  1030 */    40,  188,  236,  174,  175,  153,  153,  153,   79,  123,
       141990  + /*  1040 */    36,   89,   90,  153,  153,  153,   70,  188,  153,   97,
       141991  + /*  1050 */    98,   99,  100,   77,  102,  153,   80,   81,  174,  175,
       141992  + /*  1060 */    70,   99,  110,   59,  105,  174,  175,   77,  153,  238,
       141993  + /*  1070 */    80,   81,  188,   19,   20,   71,   22,  153,  153,  235,
       141994  + /*  1080 */    19,   22,  164,   24,   59,  153,  134,   83,  136,  153,
       141995  + /*  1090 */    36,  115,  235,   89,   90,   91,  120,  121,  153,  153,
       141996  + /*  1100 */    96,  142,   98,   99,  100,  115,   59,  103,   83,  239,
       141997  + /*  1110 */   120,  121,  199,   59,  196,  197,  153,  153,   59,  143,
       141998  + /*  1120 */   174,  175,  153,   98,   99,   71,  208,  153,  103,  165,
       141999  + /*  1130 */   153,   19,   20,  143,   22,  153,  153,   83,  134,  135,
       142000  + /*  1140 */   136,  137,  138,   89,   90,   98,   99,  100,   36,  185,
       142001  + /*  1150 */    96,  187,   98,   99,  100,   91,   95,  103,   99,  134,
       142002  + /*  1160 */   135,  136,  101,  102,  103,  104,  105,  106,  107,  153,
       142003  + /*  1170 */    26,   59,  125,  164,  113,  153,  153,  153,  212,   12,
       142004  + /*  1180 */    19,  117,  153,   71,  153,  212,  122,  164,  134,  135,
       142005  + /*  1190 */   136,  137,  138,  212,   27,   83,  212,  174,  175,   59,
       142006  + /*  1200 */   200,   89,   90,  174,  175,  196,  197,   46,   96,   42,
       142007  + /*  1210 */    98,   99,  100,  172,  151,  103,    5,  208,  203,  196,
       142008  + /*  1220 */   197,   10,   11,   12,   13,   14,  216,  216,   17,  244,
       142009  + /*  1230 */    63,  208,  209,  210,  153,   80,   24,  203,   98,   99,
       142010  + /*  1240 */   100,   30,   22,   32,  100,  164,  134,  135,  136,  137,
       142011  + /*  1250 */   138,   40,  148,  164,  150,  174,  175,  102,   97,  155,
       142012  + /*  1260 */   203,  157,  164,  244,  178,  125,  186,  182,  164,  125,
       142013  + /*  1270 */   177,   59,  177,  177,  113,  120,  121,  196,  197,   59,
       142014  + /*  1280 */   232,   70,  153,  216,  202,  196,  197,  153,   77,  208,
       142015  + /*  1290 */   209,   80,   81,  156,  196,  197,   60,  208,  248,  200,
       142016  + /*  1300 */   196,  197,  153,  174,  175,  123,  208,  153,  174,  175,
       142017  + /*  1310 */   160,   99,  208,  153,   38,  153,  160,  153,   98,   99,
       142018  + /*  1320 */   100,  153,  245,  174,  175,  221,  115,  153,  174,  175,
       142019  + /*  1330 */   153,  120,  121,  245,  174,  175,  174,  175,  174,  175,
       142020  + /*  1340 */   160,  153,  174,  175,  153,  225,  153,   22,  174,  175,
       142021  + /*  1350 */    97,  174,  175,  249,  143,  153,  224,  153,   43,  153,
       142022  + /*  1360 */   191,  153,  174,  175,   18,  174,  175,  174,  175,  153,
       142023  + /*  1370 */   131,  153,  194,  203,  153,  194,  174,  175,  174,  175,
       142024  + /*  1380 */   174,  175,  174,  175,  153,  160,  153,   18,  153,  194,
       142025  + /*  1390 */   174,  175,  174,  175,  153,  174,  175,  153,  225,  153,
       142026  + /*  1400 */   203,  153,  159,  153,  194,  174,  175,  174,  175,  174,
       142027  + /*  1410 */   175,  153,  203,  153,  224,  174,  175,  191,  174,  175,
       142028  + /*  1420 */   174,  175,  174,  175,  174,  175,  203,  160,  153,  191,
       142029  + /*  1430 */   153,  159,  174,  175,  174,  175,  153,  139,   62,  153,
       142030  + /*  1440 */   241,  153,  160,  153,  159,  153,   22,  240,  179,  174,
       142031  + /*  1450 */   175,  174,  175,  160,  159,   97,  160,  174,  175,  159,
       142032  + /*  1460 */   174,  175,  174,  175,  174,  175,  174,  175,  179,   64,
       142033  + /*  1470 */   176,  184,  108,  176,   95,  176,  234,  126,  176,  234,
       142034  + /*  1480 */   179,  178,  176,  176,  176,   97,  218,  217,  184,  179,
       142035  + /*  1490 */   179,  218,  218,  160,   22,  217,  217,  160,  218,  139,
       142036  + /*  1500 */   229,  217,  130,  129,  127,   25,  128,  163,   26,  162,
       142037  + /*  1510 */    13,  206,  231,  154,    6,  154,  207,  205,  204,  203,
       142038  + /*  1520 */   152,  166,  152,  152,  172,  172,  172,    4,  172,  166,
       142039  + /*  1530 */   180,  166,    3,   22,  172,  144,   15,  180,  172,  172,
       142040  + /*  1540 */    82,   16,   23,   23,  121,  254,  132,  172,  112,  124,
       142041  + /*  1550 */    24,   20,  126,   16,    1,  124,  112,   61,   53,   37,
       142042  + /*  1560 */   133,  132,   53,   53,  112,   53,   98,  254,  251,   34,
       142043  + /*  1570 */   123,    1,    5,   22,   97,  142,   26,   75,  123,   41,
       142044  + /*  1580 */    97,   68,   68,   24,   20,   19,  113,   22,  107,   28,
       142045  + /*  1590 */    22,   67,   23,   22,   22,   67,   22,   67,   23,   37,
       142046  + /*  1600 */    23,   23,   26,   23,   22,   24,   23,   22,   24,  123,
       142047  + /*  1610 */    23,   23,   22,   98,  125,   26,   11,   23,   26,   23,
       142048  + /*  1620 */    34,   23,   23,   23,   23,   34,   22,   34,   26,   22,
       142049  + /*  1630 */    22,   15,   23,   23,   22,  117,   23,   22,    1,  123,
       142050  + /*  1640 */    26,   23,  255,  255,  255,  255,  255,  255,  255,  255,
       142051  + /*  1650 */   123,  255,  255,  255,  255,  123,  123,  255,  255,  255,
       142052  + /*  1660 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142053  + /*  1670 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142054  + /*  1680 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142055  + /*  1690 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142056  + /*  1700 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142057  + /*  1710 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142058  + /*  1720 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142059  + /*  1730 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142060  + /*  1740 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142061  + /*  1750 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142062  + /*  1760 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142063  + /*  1770 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142064  + /*  1780 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142065  + /*  1790 */   255,  255,  255,  255,  255,  255,  255,  255,  255,  255,
       142066  + /*  1800 */   255,  255,
       142067  +};
       142068  +#define YY_SHIFT_COUNT    (489)
140498 142069   #define YY_SHIFT_MIN      (0)
140499         -#define YY_SHIFT_MAX      (1559)
       142070  +#define YY_SHIFT_MAX      (1637)
140500 142071   static const unsigned short int yy_shift_ofst[] = {
140501         - /*     0 */   182, 1090,  822,  822,  306,  957,  957,  957,  957,  210,
140502         - /*    10 */     0,    0,  104,  630,  957,  957,  957,  957,  957,  957,
140503         - /*    20 */   957, 1117, 1117,  126,  968,  306,  306,  306,  306,  306,
140504         - /*    30 */   306,   52,  156,  208,  260,  312,  364,  416,  468,  523,
140505         - /*    40 */   578,  630,  630,  630,  630,  630,  630,  630,  630,  630,
140506         - /*    50 */   630,  630,  630,  630,  630,  630,  630,  630,  682,  630,
140507         - /*    60 */   733,  783,  783,  877,  957,  957,  957,  957,  957,  957,
140508         - /*    70 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
140509         - /*    80 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
140510         - /*    90 */   957,  957,  957,  957,  957,  978,  957,  957,  957,  957,
140511         - /*   100 */   957,  957,  957,  957,  957,  957,  957,  957,  957, 1061,
140512         - /*   110 */  1108, 1108, 1108, 1108, 1108,   40,  127,   20,  280,  843,
140513         - /*   120 */  1032,  144,  144,  280,  310,  310,  310,  310,   59,  191,
140514         - /*   130 */    69, 1566, 1566, 1566,  786,  786,  786,  522,  836,  522,
140515         - /*   140 */   959,  959,  892,  155,  358,  280,  280,  280,  280,  280,
140516         - /*   150 */   280,  280,  280,  280,  280,  280,  280,  280,  280,  280,
140517         - /*   160 */   280,  280,  280,  280,  280,  280,  371,  388,  645,  645,
140518         - /*   170 */   531, 1566, 1566, 1566,  504,  189,  189,  909,   63,  176,
140519         - /*   180 */   928,  440,  932,  973,  280,  280,  280,  280,  280,  314,
140520         - /*   190 */   280,  280,  280,  280,  280,  280,  280,  280,  280,  280,
140521         - /*   200 */   280,  280, 1064, 1064, 1064,  280,  280,  280,  280,  667,
140522         - /*   210 */   280,  280,  280,  825,  280,  280,  902,  280,  280,  280,
140523         - /*   220 */   280,  280,  280,  280,  280,  383,  676,  325,  975,  975,
140524         - /*   230 */   975,  975, 1010,  325,  325,  819,  349,  524,  569,  829,
140525         - /*   240 */   829,  832,  569,  832,  686,   51,  656,  303,  303,  303,
140526         - /*   250 */   829,  294,  520,  628,  474, 1174, 1115, 1115, 1208, 1208,
140527         - /*   260 */  1115, 1231, 1217, 1131, 1246, 1246, 1246, 1246, 1115, 1276,
140528         - /*   270 */  1131, 1231, 1217, 1217, 1131, 1115, 1276, 1184, 1277, 1115,
140529         - /*   280 */  1276, 1330, 1115, 1276, 1115, 1276, 1330, 1280, 1280, 1280,
140530         - /*   290 */  1320, 1330, 1280, 1285, 1280, 1320, 1280, 1280, 1330, 1306,
140531         - /*   300 */  1306, 1330, 1284, 1294, 1284, 1294, 1284, 1294, 1284, 1294,
140532         - /*   310 */  1115, 1392, 1115, 1281, 1288, 1296, 1292, 1297, 1131, 1398,
140533         - /*   320 */  1401, 1417, 1417, 1429, 1429, 1429, 1566, 1566, 1566, 1566,
140534         - /*   330 */  1566, 1566, 1566, 1566, 1566, 1566, 1566, 1566, 1566, 1566,
140535         - /*   340 */  1566, 1566,   34,  357,   38,  462,  514,  484, 1074,  727,
140536         - /*   350 */   740,  734,  735,  777,  778,  784,  788,  803,  694,  845,
140537         - /*   360 */   742,  796,  833,  837,  889,  860,  886, 1036,  806,  958,
140538         - /*   370 */  1446, 1448, 1430, 1311, 1441, 1378, 1444, 1438, 1439, 1343,
140539         - /*   380 */  1334, 1356, 1345, 1452, 1348, 1458, 1474, 1353, 1346, 1425,
140540         - /*   390 */  1426, 1427, 1428, 1371, 1387, 1450, 1363, 1486, 1484, 1468,
140541         - /*   400 */  1384, 1352, 1431, 1467, 1432, 1420, 1454, 1374, 1390, 1476,
140542         - /*   410 */  1481, 1483, 1391, 1399, 1485, 1440, 1487, 1488, 1482, 1489,
140543         - /*   420 */  1442, 1490, 1491, 1449, 1475, 1493, 1494, 1496, 1495, 1497,
140544         - /*   430 */  1492, 1498, 1500, 1502, 1501, 1404, 1504, 1505, 1433, 1499,
140545         - /*   440 */  1508, 1407, 1506, 1503, 1509, 1507, 1511, 1513, 1515, 1506,
140546         - /*   450 */  1516, 1517, 1518, 1519, 1521, 1534, 1524, 1525, 1526, 1527,
140547         - /*   460 */  1529, 1530, 1532, 1522, 1436, 1434, 1435, 1437, 1443, 1535,
140548         - /*   470 */  1540, 1559,
140549         -};
140550         -#define YY_REDUCE_COUNT (341)
140551         -#define YY_REDUCE_MIN   (-211)
140552         -#define YY_REDUCE_MAX   (1301)
       142072  + /*     0 */   990,  976, 1211,  837,  837,  316, 1054, 1054, 1054, 1054,
       142073  + /*    10 */   214,    0,    0,  106,  642, 1054, 1054, 1054, 1054, 1054,
       142074  + /*    20 */  1054, 1054, 1054,  952,  952,  226, 1155,  316,  316,  316,
       142075  + /*    30 */   316,  316,  316,   53,  159,  212,  265,  318,  371,  424,
       142076  + /*    40 */   477,  533,  589,  642,  642,  642,  642,  642,  642,  642,
       142077  + /*    50 */   642,  642,  642,  642,  642,  642,  642,  642,  642,  642,
       142078  + /*    60 */   695,  642,  747,  798,  798, 1004, 1054, 1054, 1054, 1054,
       142079  + /*    70 */  1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054,
       142080  + /*    80 */  1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054,
       142081  + /*    90 */  1054, 1054, 1054, 1054, 1054, 1054, 1054, 1112, 1054, 1054,
       142082  + /*   100 */  1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054, 1054,
       142083  + /*   110 */  1054,  856,  874,  874,  874,  874,  874,  134,  147,   93,
       142084  + /*   120 */   342,  959, 1161,  253,  253,  342,  367,  367,  367,  367,
       142085  + /*   130 */   179,   36,   79, 1657, 1657, 1657, 1061, 1061, 1061,  516,
       142086  + /*   140 */   799,  516,  516,  531,  531,  802,  249,  369,  342,  342,
       142087  + /*   150 */   342,  342,  342,  342,  342,  342,  342,  342,  342,  342,
       142088  + /*   160 */   342,  342,  342,  342,  342,  342,  342,  342,  342,  272,
       142089  + /*   170 */   442,  442,  536, 1657, 1657, 1657, 1025,  245,  245,  570,
       142090  + /*   180 */   172,  286,  805, 1047, 1140, 1220,  342,  342,  342,  342,
       142091  + /*   190 */   342,  342,  342,  342,  170,  342,  342,  342,  342,  342,
       142092  + /*   200 */   342,  342,  342,  342,  342,  342,  342,  841,  841,  841,
       142093  + /*   210 */   342,  342,  342,  342,  530,  342,  342,  342, 1059,  342,
       142094  + /*   220 */   342, 1167,  342,  342,  342,  342,  342,  342,  342,  342,
       142095  + /*   230 */   123,  688,  177, 1212, 1212, 1212, 1212, 1144,  177,  177,
       142096  + /*   240 */  1064,  409,   33,  628,  707,  707,  900,  628,  628,  900,
       142097  + /*   250 */   897,  323,  398,  677,  677,  677,  707,  572,  684,  590,
       142098  + /*   260 */   739, 1236, 1182, 1182, 1276, 1276, 1182, 1253, 1325, 1315,
       142099  + /*   270 */  1239, 1346, 1346, 1346, 1346, 1182, 1369, 1239, 1239, 1253,
       142100  + /*   280 */  1325, 1315, 1315, 1239, 1182, 1369, 1298, 1376, 1182, 1369,
       142101  + /*   290 */  1424, 1182, 1369, 1182, 1369, 1424, 1358, 1358, 1358, 1405,
       142102  + /*   300 */  1424, 1358, 1364, 1358, 1405, 1358, 1358, 1424, 1379, 1379,
       142103  + /*   310 */  1424, 1351, 1388, 1351, 1388, 1351, 1388, 1351, 1388, 1182,
       142104  + /*   320 */  1472, 1182, 1360, 1372, 1377, 1374, 1378, 1239, 1480, 1482,
       142105  + /*   330 */  1497, 1497, 1508, 1508, 1508, 1657, 1657, 1657, 1657, 1657,
       142106  + /*   340 */  1657, 1657, 1657, 1657, 1657, 1657, 1657, 1657, 1657, 1657,
       142107  + /*   350 */  1657,   20,  413,   98,  423,  519,  383,  962,  742,   61,
       142108  + /*   360 */   696,  749,  750,  753,  789,  790,  795,  797,  840,  842,
       142109  + /*   370 */   810,  668,  817,  659,  819,  849,  854,  899,  643,  745,
       142110  + /*   380 */   956,  926,  916, 1523, 1529, 1511, 1391, 1521, 1458, 1525,
       142111  + /*   390 */  1519, 1520, 1423, 1414, 1436, 1526, 1425, 1531, 1426, 1537,
       142112  + /*   400 */  1553, 1431, 1427, 1444, 1496, 1522, 1429, 1505, 1509, 1510,
       142113  + /*   410 */  1512, 1452, 1468, 1535, 1447, 1570, 1567, 1551, 1477, 1433,
       142114  + /*   420 */  1513, 1550, 1514, 1502, 1538, 1455, 1483, 1559, 1564, 1566,
       142115  + /*   430 */  1473, 1481, 1565, 1524, 1568, 1571, 1569, 1572, 1528, 1561,
       142116  + /*   440 */  1574, 1530, 1562, 1575, 1577, 1578, 1576, 1580, 1582, 1581,
       142117  + /*   450 */  1583, 1585, 1584, 1486, 1587, 1588, 1515, 1586, 1590, 1489,
       142118  + /*   460 */  1589, 1591, 1592, 1593, 1594, 1596, 1598, 1589, 1599, 1600,
       142119  + /*   470 */  1602, 1601, 1604, 1605, 1607, 1608, 1609, 1610, 1612, 1613,
       142120  + /*   480 */  1615, 1614, 1518, 1516, 1527, 1532, 1533, 1618, 1616, 1637,
       142121  +};
       142122  +#define YY_REDUCE_COUNT (350)
       142123  +#define YY_REDUCE_MIN   (-225)
       142124  +#define YY_REDUCE_MAX   (1375)
140553 142125   static const short yy_reduce_ofst[] = {
140554         - /*     0 */  -143,  789,  753, 1059, -137, -146, -144, -141, -136,  687,
140555         - /*    10 */  -107,  101, -203,  -52,  830,  870,  890,  167,  953,  218,
140556         - /*    20 */   220,  413,  646,  897,   73,  281,  283,  332,  496,  601,
140557         - /*    30 */   650, -211, -211, -211, -211, -211, -211, -211, -211, -211,
140558         - /*    40 */  -211, -211, -211, -211, -211, -211, -211, -211, -211, -211,
140559         - /*    50 */  -211, -211, -211, -211, -211, -211, -211, -211, -211, -211,
140560         - /*    60 */  -211, -211, -211,  374,  377,  537,  969,  983, 1006, 1008,
140561         - /*    70 */  1015, 1055, 1067, 1069, 1071, 1084, 1096, 1100, 1104, 1111,
140562         - /*    80 */  1113, 1116, 1119, 1123, 1127, 1130, 1136, 1143, 1146, 1150,
140563         - /*    90 */  1153, 1155, 1159, 1161, 1163, 1166, 1170, 1189, 1193, 1195,
140564         - /*   100 */  1197, 1199, 1201, 1203, 1205, 1207, 1212, 1230, 1232, -211,
140565         - /*   110 */  -211, -211, -211, -211, -211, -211, -211, -211,  -30,  427,
140566         - /*   120 */  -171, -145, -134,   22,  279,  287,  279,  287,   99, -211,
140567         - /*   130 */  -211, -211, -211, -211, -165, -165, -165,  123,  135,  175,
140568         - /*   140 */  -150,  396,  337,  291,  291, -147,  185,  391,  446,  444,
140569         - /*   150 */   452,  500,  501,  502,   27, -152,  295,  438,  490,  503,
140570         - /*   160 */   495,  506,  -73,  447,  451,  536,  570,  551,  540,  579,
140571         - /*   170 */    30,  508,  535,   81,   14,   61,  115,  168,  142,  222,
140572         - /*   180 */   275,  284,  397,  599,  607,  647,  654,  660,  709,  658,
140573         - /*   190 */   714,  750,  754,  772,  787,  801,  817,  818,  850,  863,
140574         - /*   200 */   867,  871,  466,  748,  799,  881,  888,  898,  913,  824,
140575         - /*   210 */   930,  933,  934,  873,  942,  945,  849,  946,  222,  954,
140576         - /*   220 */   971,  972,  976,  979,  980,  900,  874,  927,  950,  961,
140577         - /*   230 */   962,  963,  824,  927,  927,  939,  970, 1018,  981,  988,
140578         - /*   240 */   993,  936,  982,  937, 1009, 1000, 1031, 1011, 1014, 1040,
140579         - /*   250 */  1003,  991,  990, 1026, 1072,  985, 1076, 1079,  997, 1005,
140580         - /*   260 */  1091, 1037, 1082, 1060, 1083, 1087, 1088, 1098, 1124, 1141,
140581         - /*   270 */  1106, 1092, 1122, 1135, 1128, 1147, 1173, 1110, 1105, 1187,
140582         - /*   280 */  1192, 1176, 1202, 1198, 1206, 1200, 1182, 1213, 1214, 1215,
140583         - /*   290 */  1209, 1216, 1218, 1219, 1220, 1224, 1222, 1223, 1221, 1171,
140584         - /*   300 */  1177, 1233, 1196, 1194, 1204, 1210, 1211, 1225, 1226, 1228,
140585         - /*   310 */  1253, 1190, 1256, 1191, 1227, 1229, 1234, 1236, 1235, 1263,
140586         - /*   320 */  1268, 1278, 1279, 1289, 1291, 1295, 1185, 1237, 1238, 1282,
140587         - /*   330 */  1274, 1283, 1286, 1287, 1290, 1269, 1270, 1293, 1298, 1299,
140588         - /*   340 */  1300, 1301,
       142126  + /*     0 */  -137,  -31, 1104, 1023, 1081, -132,  -40,  -38,  223,  225,
       142127  + /*    10 */   698, -153,  -99, -225, -165,  386,  478,  843,  859, -139,
       142128  + /*    20 */   884,  117,  277,  844,  857,  964,  559,  561,  614,  918,
       142129  + /*    30 */  1009, 1089, 1098, -222, -222, -222, -222, -222, -222, -222,
       142130  + /*    40 */  -222, -222, -222, -222, -222, -222, -222, -222, -222, -222,
       142131  + /*    50 */  -222, -222, -222, -222, -222, -222, -222, -222, -222, -222,
       142132  + /*    60 */  -222, -222, -222, -222, -222,  329,  331,  497,  654,  656,
       142133  + /*    70 */   781,  891,  946, 1029, 1129, 1134, 1149, 1154, 1160, 1162,
       142134  + /*    80 */  1164, 1168, 1174, 1177, 1188, 1191, 1193, 1202, 1204, 1206,
       142135  + /*    90 */  1208, 1216, 1218, 1221, 1231, 1233, 1235, 1241, 1244, 1246,
       142136  + /*   100 */  1248, 1250, 1258, 1260, 1275, 1277, 1283, 1286, 1288, 1290,
       142137  + /*   110 */  1292, -222, -222, -222, -222, -222, -222, -222, -222, -222,
       142138  + /*   120 */  -115,  796, -156, -154, -141,   14,  242,  349,  242,  349,
       142139  + /*   130 */   -61, -222, -222, -222, -222, -222,  101,  101,  101,  332,
       142140  + /*   140 */   302,  384,  387, -170,  146,  344,  196,  196,   15,   11,
       142141  + /*   150 */   183,  235,  395,  355,  396,  406,  452,  457,  391,  459,
       142142  + /*   160 */   443,  447,  511,  495,  454,  512,  505,  571,  498,  532,
       142143  + /*   170 */   431,  435,  339,  455,  446,  508, -174, -116,  -97, -120,
       142144  + /*   180 */  -150,   64,  176,  330,  337,  509,  569,  611,  653,  673,
       142145  + /*   190 */   714,  717,  763,  771,  -34,  779,  786,  830,  846,  860,
       142146  + /*   200 */   866,  882,  883,  890,  892,  895,  902,  319,  368,  769,
       142147  + /*   210 */   915,  924,  925,  932,  755,  936,  945,  963,  782,  969,
       142148  + /*   220 */   974,  816,  977,   64,  982,  983, 1016, 1022, 1024, 1031,
       142149  + /*   230 */   870,  831,  913,  966,  973,  981,  984,  755,  913,  913,
       142150  + /*   240 */  1000, 1041, 1063, 1015, 1010, 1011,  985, 1034, 1057, 1019,
       142151  + /*   250 */  1086, 1080, 1085, 1093, 1095, 1096, 1067, 1048, 1082, 1099,
       142152  + /*   260 */  1137, 1050, 1150, 1156, 1077, 1088, 1180, 1120, 1132, 1169,
       142153  + /*   270 */  1170, 1178, 1181, 1195, 1210, 1225, 1243, 1197, 1209, 1173,
       142154  + /*   280 */  1190, 1226, 1238, 1223, 1267, 1272, 1199, 1207, 1282, 1285,
       142155  + /*   290 */  1269, 1293, 1295, 1296, 1300, 1289, 1294, 1297, 1299, 1287,
       142156  + /*   300 */  1301, 1302, 1303, 1306, 1304, 1307, 1308, 1310, 1242, 1245,
       142157  + /*   310 */  1311, 1268, 1270, 1273, 1278, 1274, 1279, 1280, 1284, 1333,
       142158  + /*   320 */  1271, 1337, 1281, 1309, 1305, 1312, 1314, 1316, 1344, 1347,
       142159  + /*   330 */  1359, 1361, 1368, 1370, 1371, 1291, 1313, 1317, 1355, 1352,
       142160  + /*   340 */  1353, 1354, 1356, 1363, 1350, 1357, 1362, 1366, 1367, 1375,
       142161  + /*   350 */  1365,
140589 142162   };
140590 142163   static const YYACTIONTYPE yy_default[] = {
140591         - /*     0 */  1297, 1349, 1221, 1014, 1119, 1221, 1221, 1221, 1221, 1014,
140592         - /*    10 */  1145, 1145, 1272, 1045, 1014, 1014, 1014, 1014, 1014, 1220,
140593         - /*    20 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140594         - /*    30 */  1014, 1151, 1014, 1014, 1014, 1014, 1222, 1223, 1014, 1014,
140595         - /*    40 */  1014, 1271, 1273, 1161, 1160, 1159, 1158, 1254, 1132, 1156,
140596         - /*    50 */  1149, 1153, 1216, 1217, 1215, 1219, 1222, 1223, 1014, 1152,
140597         - /*    60 */  1186, 1200, 1185, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140598         - /*    70 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140599         - /*    80 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140600         - /*    90 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140601         - /*   100 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1194,
140602         - /*   110 */  1199, 1206, 1198, 1195, 1188, 1187, 1189, 1190, 1014, 1035,
140603         - /*   120 */  1084, 1014, 1014, 1014, 1289, 1288, 1014, 1014, 1045, 1191,
140604         - /*   130 */  1192, 1203, 1202, 1201, 1279, 1305, 1304, 1014, 1014, 1014,
140605         - /*   140 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140606         - /*   150 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140607         - /*   160 */  1014, 1014, 1014, 1014, 1014, 1014, 1045, 1297, 1041, 1041,
140608         - /*   170 */  1014, 1284, 1119, 1110, 1014, 1014, 1014, 1014, 1014, 1014,
140609         - /*   180 */  1014, 1014, 1014, 1014, 1014, 1276, 1274, 1014, 1236, 1014,
140610         - /*   190 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140611         - /*   200 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140612         - /*   210 */  1014, 1014, 1014, 1115, 1014, 1014, 1014, 1014, 1014, 1014,
140613         - /*   220 */  1014, 1014, 1014, 1014, 1299, 1014, 1249, 1098, 1115, 1115,
140614         - /*   230 */  1115, 1115, 1117, 1099, 1097, 1109, 1045, 1021, 1155, 1134,
140615         - /*   240 */  1134, 1338, 1155, 1338, 1059, 1319, 1056, 1145, 1145, 1145,
140616         - /*   250 */  1134, 1218, 1116, 1109, 1014, 1341, 1124, 1124, 1340, 1340,
140617         - /*   260 */  1124, 1166, 1087, 1155, 1093, 1093, 1093, 1093, 1124, 1032,
140618         - /*   270 */  1155, 1166, 1087, 1087, 1155, 1124, 1032, 1253, 1335, 1124,
140619         - /*   280 */  1032, 1229, 1124, 1032, 1124, 1032, 1229, 1085, 1085, 1085,
140620         - /*   290 */  1074, 1229, 1085, 1059, 1085, 1074, 1085, 1085, 1229, 1233,
140621         - /*   300 */  1233, 1229, 1138, 1133, 1138, 1133, 1138, 1133, 1138, 1133,
140622         - /*   310 */  1124, 1224, 1124, 1014, 1150, 1139, 1148, 1146, 1155, 1038,
140623         - /*   320 */  1077, 1302, 1302, 1298, 1298, 1298, 1346, 1346, 1284, 1314,
140624         - /*   330 */  1045, 1045, 1045, 1045, 1314, 1061, 1061, 1045, 1045, 1045,
140625         - /*   340 */  1045, 1314, 1014, 1014, 1014, 1014, 1014, 1014, 1309, 1014,
140626         - /*   350 */  1238, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140627         - /*   360 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1171,
140628         - /*   370 */  1014, 1017, 1281, 1014, 1014, 1280, 1014, 1014, 1014, 1014,
140629         - /*   380 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140630         - /*   390 */  1014, 1014, 1014, 1014, 1014, 1014, 1337, 1014, 1014, 1014,
140631         - /*   400 */  1014, 1014, 1014, 1252, 1251, 1014, 1014, 1126, 1014, 1014,
140632         - /*   410 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140633         - /*   420 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140634         - /*   430 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140635         - /*   440 */  1014, 1014, 1147, 1014, 1140, 1014, 1014, 1014, 1014, 1328,
140636         - /*   450 */  1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014,
140637         - /*   460 */  1014, 1014, 1014, 1323, 1101, 1173, 1014, 1172, 1176, 1014,
140638         - /*   470 */  1026, 1014,
       142164  + /*     0 */  1389, 1389, 1389, 1261, 1046, 1151, 1261, 1261, 1261, 1261,
       142165  + /*    10 */  1046, 1181, 1181, 1312, 1077, 1046, 1046, 1046, 1046, 1046,
       142166  + /*    20 */  1046, 1260, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142167  + /*    30 */  1046, 1046, 1046, 1187, 1046, 1046, 1046, 1046, 1262, 1263,
       142168  + /*    40 */  1046, 1046, 1046, 1311, 1313, 1197, 1196, 1195, 1194, 1294,
       142169  + /*    50 */  1168, 1192, 1185, 1189, 1256, 1257, 1255, 1259, 1262, 1263,
       142170  + /*    60 */  1046, 1188, 1226, 1240, 1225, 1046, 1046, 1046, 1046, 1046,
       142171  + /*    70 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142172  + /*    80 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142173  + /*    90 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142174  + /*   100 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142175  + /*   110 */  1046, 1234, 1239, 1246, 1238, 1235, 1228, 1227, 1229, 1230,
       142176  + /*   120 */  1046, 1067, 1116, 1046, 1046, 1046, 1329, 1328, 1046, 1046,
       142177  + /*   130 */  1077, 1231, 1232, 1243, 1242, 1241, 1319, 1345, 1344, 1046,
       142178  + /*   140 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142179  + /*   150 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142180  + /*   160 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1077,
       142181  + /*   170 */  1073, 1073, 1046, 1324, 1151, 1142, 1046, 1046, 1046, 1046,
       142182  + /*   180 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1316, 1314, 1046,
       142183  + /*   190 */  1276, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142184  + /*   200 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142185  + /*   210 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1147, 1046,
       142186  + /*   220 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1339,
       142187  + /*   230 */  1046, 1289, 1130, 1147, 1147, 1147, 1147, 1149, 1131, 1129,
       142188  + /*   240 */  1141, 1077, 1053, 1191, 1170, 1170, 1378, 1191, 1191, 1378,
       142189  + /*   250 */  1091, 1359, 1088, 1181, 1181, 1181, 1170, 1258, 1148, 1141,
       142190  + /*   260 */  1046, 1381, 1156, 1156, 1380, 1380, 1156, 1200, 1206, 1119,
       142191  + /*   270 */  1191, 1125, 1125, 1125, 1125, 1156, 1064, 1191, 1191, 1200,
       142192  + /*   280 */  1206, 1119, 1119, 1191, 1156, 1064, 1293, 1375, 1156, 1064,
       142193  + /*   290 */  1269, 1156, 1064, 1156, 1064, 1269, 1117, 1117, 1117, 1106,
       142194  + /*   300 */  1269, 1117, 1091, 1117, 1106, 1117, 1117, 1269, 1273, 1273,
       142195  + /*   310 */  1269, 1174, 1169, 1174, 1169, 1174, 1169, 1174, 1169, 1156,
       142196  + /*   320 */  1264, 1156, 1046, 1186, 1175, 1184, 1182, 1191, 1070, 1109,
       142197  + /*   330 */  1342, 1342, 1338, 1338, 1338, 1386, 1386, 1324, 1354, 1077,
       142198  + /*   340 */  1077, 1077, 1077, 1354, 1093, 1093, 1077, 1077, 1077, 1077,
       142199  + /*   350 */  1354, 1046, 1046, 1046, 1046, 1046, 1046, 1349, 1046, 1278,
       142200  + /*   360 */  1160, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142201  + /*   370 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142202  + /*   380 */  1046, 1046, 1211, 1046, 1049, 1321, 1046, 1046, 1320, 1046,
       142203  + /*   390 */  1046, 1046, 1046, 1046, 1046, 1161, 1046, 1046, 1046, 1046,
       142204  + /*   400 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142205  + /*   410 */  1046, 1046, 1046, 1046, 1377, 1046, 1046, 1046, 1046, 1046,
       142206  + /*   420 */  1046, 1292, 1291, 1046, 1046, 1158, 1046, 1046, 1046, 1046,
       142207  + /*   430 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142208  + /*   440 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142209  + /*   450 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142210  + /*   460 */  1183, 1046, 1176, 1046, 1046, 1046, 1046, 1368, 1046, 1046,
       142211  + /*   470 */  1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046, 1046,
       142212  + /*   480 */  1046, 1363, 1133, 1213, 1046, 1212, 1216, 1046, 1058, 1046,
140639 142213   };
140640 142214   /********** End of lemon-generated parsing tables *****************************/
140641 142215   
140642 142216   /* The next table maps tokens (terminal symbols) into fallback tokens.  
140643 142217   ** If a construct like the following:
140644 142218   ** 
140645 142219   **      %fallback ID X Y Z.
................................................................................
140712 142286       0,  /*         GT => nothing */
140713 142287       0,  /*         LE => nothing */
140714 142288       0,  /*         LT => nothing */
140715 142289       0,  /*         GE => nothing */
140716 142290       0,  /*     ESCAPE => nothing */
140717 142291       0,  /*         ID => nothing */
140718 142292      59,  /*   COLUMNKW => ID */
       142293  +   59,  /*         DO => ID */
140719 142294      59,  /*        FOR => ID */
140720 142295      59,  /*     IGNORE => ID */
140721 142296      59,  /*  INITIALLY => ID */
140722 142297      59,  /*    INSTEAD => ID */
140723 142298      59,  /*         NO => ID */
140724 142299      59,  /*        KEY => ID */
140725 142300      59,  /*         OF => ID */
................................................................................
140773 142348   #ifdef YYTRACKMAXSTACKDEPTH
140774 142349     int yyhwm;                    /* High-water mark of the stack */
140775 142350   #endif
140776 142351   #ifndef YYNOERRORRECOVERY
140777 142352     int yyerrcnt;                 /* Shifts left before out of the error */
140778 142353   #endif
140779 142354     sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
       142355  +  sqlite3ParserCTX_SDECL                /* A place to hold %extra_context */
140780 142356   #if YYSTACKDEPTH<=0
140781 142357     int yystksz;                  /* Current side of the stack */
140782 142358     yyStackEntry *yystack;        /* The parser's stack */
140783 142359     yyStackEntry yystk0;          /* First stack entry */
140784 142360   #else
140785 142361     yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
140786 142362     yyStackEntry *yystackEnd;            /* Last entry in the stack */
................................................................................
140881 142457     /*   54 */ "GT",
140882 142458     /*   55 */ "LE",
140883 142459     /*   56 */ "LT",
140884 142460     /*   57 */ "GE",
140885 142461     /*   58 */ "ESCAPE",
140886 142462     /*   59 */ "ID",
140887 142463     /*   60 */ "COLUMNKW",
140888         -  /*   61 */ "FOR",
140889         -  /*   62 */ "IGNORE",
140890         -  /*   63 */ "INITIALLY",
140891         -  /*   64 */ "INSTEAD",
140892         -  /*   65 */ "NO",
140893         -  /*   66 */ "KEY",
140894         -  /*   67 */ "OF",
140895         -  /*   68 */ "OFFSET",
140896         -  /*   69 */ "PRAGMA",
140897         -  /*   70 */ "RAISE",
140898         -  /*   71 */ "RECURSIVE",
140899         -  /*   72 */ "REPLACE",
140900         -  /*   73 */ "RESTRICT",
140901         -  /*   74 */ "ROW",
140902         -  /*   75 */ "TRIGGER",
140903         -  /*   76 */ "VACUUM",
140904         -  /*   77 */ "VIEW",
140905         -  /*   78 */ "VIRTUAL",
140906         -  /*   79 */ "WITH",
140907         -  /*   80 */ "REINDEX",
140908         -  /*   81 */ "RENAME",
140909         -  /*   82 */ "CTIME_KW",
140910         -  /*   83 */ "ANY",
140911         -  /*   84 */ "BITAND",
140912         -  /*   85 */ "BITOR",
140913         -  /*   86 */ "LSHIFT",
140914         -  /*   87 */ "RSHIFT",
140915         -  /*   88 */ "PLUS",
140916         -  /*   89 */ "MINUS",
140917         -  /*   90 */ "STAR",
140918         -  /*   91 */ "SLASH",
140919         -  /*   92 */ "REM",
140920         -  /*   93 */ "CONCAT",
140921         -  /*   94 */ "COLLATE",
140922         -  /*   95 */ "BITNOT",
140923         -  /*   96 */ "INDEXED",
140924         -  /*   97 */ "STRING",
140925         -  /*   98 */ "JOIN_KW",
140926         -  /*   99 */ "CONSTRAINT",
140927         -  /*  100 */ "DEFAULT",
140928         -  /*  101 */ "NULL",
140929         -  /*  102 */ "PRIMARY",
140930         -  /*  103 */ "UNIQUE",
140931         -  /*  104 */ "CHECK",
140932         -  /*  105 */ "REFERENCES",
140933         -  /*  106 */ "AUTOINCR",
140934         -  /*  107 */ "ON",
140935         -  /*  108 */ "INSERT",
140936         -  /*  109 */ "DELETE",
140937         -  /*  110 */ "UPDATE",
140938         -  /*  111 */ "SET",
140939         -  /*  112 */ "DEFERRABLE",
140940         -  /*  113 */ "FOREIGN",
140941         -  /*  114 */ "DROP",
140942         -  /*  115 */ "UNION",
140943         -  /*  116 */ "ALL",
140944         -  /*  117 */ "EXCEPT",
140945         -  /*  118 */ "INTERSECT",
140946         -  /*  119 */ "SELECT",
140947         -  /*  120 */ "VALUES",
140948         -  /*  121 */ "DISTINCT",
140949         -  /*  122 */ "DOT",
140950         -  /*  123 */ "FROM",
140951         -  /*  124 */ "JOIN",
140952         -  /*  125 */ "USING",
140953         -  /*  126 */ "ORDER",
140954         -  /*  127 */ "GROUP",
140955         -  /*  128 */ "HAVING",
140956         -  /*  129 */ "LIMIT",
140957         -  /*  130 */ "WHERE",
140958         -  /*  131 */ "INTO",
140959         -  /*  132 */ "FLOAT",
140960         -  /*  133 */ "BLOB",
140961         -  /*  134 */ "INTEGER",
140962         -  /*  135 */ "VARIABLE",
140963         -  /*  136 */ "CASE",
140964         -  /*  137 */ "WHEN",
140965         -  /*  138 */ "THEN",
140966         -  /*  139 */ "ELSE",
140967         -  /*  140 */ "INDEX",
140968         -  /*  141 */ "ALTER",
140969         -  /*  142 */ "ADD",
140970         -  /*  143 */ "error",
140971         -  /*  144 */ "input",
140972         -  /*  145 */ "cmdlist",
140973         -  /*  146 */ "ecmd",
140974         -  /*  147 */ "explain",
       142464  +  /*   61 */ "DO",
       142465  +  /*   62 */ "FOR",
       142466  +  /*   63 */ "IGNORE",
       142467  +  /*   64 */ "INITIALLY",
       142468  +  /*   65 */ "INSTEAD",
       142469  +  /*   66 */ "NO",
       142470  +  /*   67 */ "KEY",
       142471  +  /*   68 */ "OF",
       142472  +  /*   69 */ "OFFSET",
       142473  +  /*   70 */ "PRAGMA",
       142474  +  /*   71 */ "RAISE",
       142475  +  /*   72 */ "RECURSIVE",
       142476  +  /*   73 */ "REPLACE",
       142477  +  /*   74 */ "RESTRICT",
       142478  +  /*   75 */ "ROW",
       142479  +  /*   76 */ "TRIGGER",
       142480  +  /*   77 */ "VACUUM",
       142481  +  /*   78 */ "VIEW",
       142482  +  /*   79 */ "VIRTUAL",
       142483  +  /*   80 */ "WITH",
       142484  +  /*   81 */ "REINDEX",
       142485  +  /*   82 */ "RENAME",
       142486  +  /*   83 */ "CTIME_KW",
       142487  +  /*   84 */ "ANY",
       142488  +  /*   85 */ "BITAND",
       142489  +  /*   86 */ "BITOR",
       142490  +  /*   87 */ "LSHIFT",
       142491  +  /*   88 */ "RSHIFT",
       142492  +  /*   89 */ "PLUS",
       142493  +  /*   90 */ "MINUS",
       142494  +  /*   91 */ "STAR",
       142495  +  /*   92 */ "SLASH",
       142496  +  /*   93 */ "REM",
       142497  +  /*   94 */ "CONCAT",
       142498  +  /*   95 */ "COLLATE",
       142499  +  /*   96 */ "BITNOT",
       142500  +  /*   97 */ "ON",
       142501  +  /*   98 */ "INDEXED",
       142502  +  /*   99 */ "STRING",
       142503  +  /*  100 */ "JOIN_KW",
       142504  +  /*  101 */ "CONSTRAINT",
       142505  +  /*  102 */ "DEFAULT",
       142506  +  /*  103 */ "NULL",
       142507  +  /*  104 */ "PRIMARY",
       142508  +  /*  105 */ "UNIQUE",
       142509  +  /*  106 */ "CHECK",
       142510  +  /*  107 */ "REFERENCES",
       142511  +  /*  108 */ "AUTOINCR",
       142512  +  /*  109 */ "INSERT",
       142513  +  /*  110 */ "DELETE",
       142514  +  /*  111 */ "UPDATE",
       142515  +  /*  112 */ "SET",
       142516  +  /*  113 */ "DEFERRABLE",
       142517  +  /*  114 */ "FOREIGN",
       142518  +  /*  115 */ "DROP",
       142519  +  /*  116 */ "UNION",
       142520  +  /*  117 */ "ALL",
       142521  +  /*  118 */ "EXCEPT",
       142522  +  /*  119 */ "INTERSECT",
       142523  +  /*  120 */ "SELECT",
       142524  +  /*  121 */ "VALUES",
       142525  +  /*  122 */ "DISTINCT",
       142526  +  /*  123 */ "DOT",
       142527  +  /*  124 */ "FROM",
       142528  +  /*  125 */ "JOIN",
       142529  +  /*  126 */ "USING",
       142530  +  /*  127 */ "ORDER",
       142531  +  /*  128 */ "GROUP",
       142532  +  /*  129 */ "HAVING",
       142533  +  /*  130 */ "LIMIT",
       142534  +  /*  131 */ "WHERE",
       142535  +  /*  132 */ "INTO",
       142536  +  /*  133 */ "NOTHING",
       142537  +  /*  134 */ "FLOAT",
       142538  +  /*  135 */ "BLOB",
       142539  +  /*  136 */ "INTEGER",
       142540  +  /*  137 */ "VARIABLE",
       142541  +  /*  138 */ "CASE",
       142542  +  /*  139 */ "WHEN",
       142543  +  /*  140 */ "THEN",
       142544  +  /*  141 */ "ELSE",
       142545  +  /*  142 */ "INDEX",
       142546  +  /*  143 */ "ALTER",
       142547  +  /*  144 */ "ADD",
       142548  +  /*  145 */ "input",
       142549  +  /*  146 */ "cmdlist",
       142550  +  /*  147 */ "ecmd",
140975 142551     /*  148 */ "cmdx",
140976         -  /*  149 */ "cmd",
140977         -  /*  150 */ "transtype",
140978         -  /*  151 */ "trans_opt",
140979         -  /*  152 */ "nm",
140980         -  /*  153 */ "savepoint_opt",
140981         -  /*  154 */ "create_table",
140982         -  /*  155 */ "create_table_args",
140983         -  /*  156 */ "createkw",
140984         -  /*  157 */ "temp",
140985         -  /*  158 */ "ifnotexists",
140986         -  /*  159 */ "dbnm",
140987         -  /*  160 */ "columnlist",
140988         -  /*  161 */ "conslist_opt",
140989         -  /*  162 */ "table_options",
140990         -  /*  163 */ "select",
140991         -  /*  164 */ "columnname",
140992         -  /*  165 */ "carglist",
140993         -  /*  166 */ "typetoken",
140994         -  /*  167 */ "typename",
140995         -  /*  168 */ "signed",
140996         -  /*  169 */ "plus_num",
140997         -  /*  170 */ "minus_num",
140998         -  /*  171 */ "scanpt",
140999         -  /*  172 */ "ccons",
141000         -  /*  173 */ "term",
141001         -  /*  174 */ "expr",
141002         -  /*  175 */ "onconf",
141003         -  /*  176 */ "sortorder",
141004         -  /*  177 */ "autoinc",
141005         -  /*  178 */ "eidlist_opt",
141006         -  /*  179 */ "refargs",
141007         -  /*  180 */ "defer_subclause",
141008         -  /*  181 */ "refarg",
141009         -  /*  182 */ "refact",
141010         -  /*  183 */ "init_deferred_pred_opt",
141011         -  /*  184 */ "conslist",
141012         -  /*  185 */ "tconscomma",
141013         -  /*  186 */ "tcons",
141014         -  /*  187 */ "sortlist",
141015         -  /*  188 */ "eidlist",
141016         -  /*  189 */ "defer_subclause_opt",
141017         -  /*  190 */ "orconf",
141018         -  /*  191 */ "resolvetype",
141019         -  /*  192 */ "raisetype",
141020         -  /*  193 */ "ifexists",
141021         -  /*  194 */ "fullname",
141022         -  /*  195 */ "selectnowith",
141023         -  /*  196 */ "oneselect",
141024         -  /*  197 */ "wqlist",
141025         -  /*  198 */ "multiselect_op",
141026         -  /*  199 */ "distinct",
141027         -  /*  200 */ "selcollist",
141028         -  /*  201 */ "from",
141029         -  /*  202 */ "where_opt",
141030         -  /*  203 */ "groupby_opt",
141031         -  /*  204 */ "having_opt",
141032         -  /*  205 */ "orderby_opt",
141033         -  /*  206 */ "limit_opt",
141034         -  /*  207 */ "values",
141035         -  /*  208 */ "nexprlist",
141036         -  /*  209 */ "exprlist",
141037         -  /*  210 */ "sclp",
141038         -  /*  211 */ "as",
141039         -  /*  212 */ "seltablist",
141040         -  /*  213 */ "stl_prefix",
141041         -  /*  214 */ "joinop",
141042         -  /*  215 */ "indexed_opt",
141043         -  /*  216 */ "on_opt",
141044         -  /*  217 */ "using_opt",
141045         -  /*  218 */ "idlist",
141046         -  /*  219 */ "with",
141047         -  /*  220 */ "setlist",
141048         -  /*  221 */ "insert_cmd",
141049         -  /*  222 */ "idlist_opt",
141050         -  /*  223 */ "likeop",
141051         -  /*  224 */ "between_op",
141052         -  /*  225 */ "in_op",
141053         -  /*  226 */ "paren_exprlist",
141054         -  /*  227 */ "case_operand",
141055         -  /*  228 */ "case_exprlist",
141056         -  /*  229 */ "case_else",
141057         -  /*  230 */ "uniqueflag",
141058         -  /*  231 */ "collate",
141059         -  /*  232 */ "nmnum",
141060         -  /*  233 */ "trigger_decl",
141061         -  /*  234 */ "trigger_cmd_list",
141062         -  /*  235 */ "trigger_time",
141063         -  /*  236 */ "trigger_event",
141064         -  /*  237 */ "foreach_clause",
141065         -  /*  238 */ "when_clause",
141066         -  /*  239 */ "trigger_cmd",
141067         -  /*  240 */ "trnm",
141068         -  /*  241 */ "tridxby",
141069         -  /*  242 */ "database_kw_opt",
141070         -  /*  243 */ "key_opt",
141071         -  /*  244 */ "add_column_fullname",
141072         -  /*  245 */ "kwcolumn_opt",
141073         -  /*  246 */ "create_vtab",
141074         -  /*  247 */ "vtabarglist",
141075         -  /*  248 */ "vtabarg",
141076         -  /*  249 */ "vtabargtoken",
141077         -  /*  250 */ "lp",
141078         -  /*  251 */ "anylist",
       142552  +  /*  149 */ "explain",
       142553  +  /*  150 */ "cmd",
       142554  +  /*  151 */ "transtype",
       142555  +  /*  152 */ "trans_opt",
       142556  +  /*  153 */ "nm",
       142557  +  /*  154 */ "savepoint_opt",
       142558  +  /*  155 */ "create_table",
       142559  +  /*  156 */ "create_table_args",
       142560  +  /*  157 */ "createkw",
       142561  +  /*  158 */ "temp",
       142562  +  /*  159 */ "ifnotexists",
       142563  +  /*  160 */ "dbnm",
       142564  +  /*  161 */ "columnlist",
       142565  +  /*  162 */ "conslist_opt",
       142566  +  /*  163 */ "table_options",
       142567  +  /*  164 */ "select",
       142568  +  /*  165 */ "columnname",
       142569  +  /*  166 */ "carglist",
       142570  +  /*  167 */ "typetoken",
       142571  +  /*  168 */ "typename",
       142572  +  /*  169 */ "signed",
       142573  +  /*  170 */ "plus_num",
       142574  +  /*  171 */ "minus_num",
       142575  +  /*  172 */ "scanpt",
       142576  +  /*  173 */ "ccons",
       142577  +  /*  174 */ "term",
       142578  +  /*  175 */ "expr",
       142579  +  /*  176 */ "onconf",
       142580  +  /*  177 */ "sortorder",
       142581  +  /*  178 */ "autoinc",
       142582  +  /*  179 */ "eidlist_opt",
       142583  +  /*  180 */ "refargs",
       142584  +  /*  181 */ "defer_subclause",
       142585  +  /*  182 */ "refarg",
       142586  +  /*  183 */ "refact",
       142587  +  /*  184 */ "init_deferred_pred_opt",
       142588  +  /*  185 */ "conslist",
       142589  +  /*  186 */ "tconscomma",
       142590  +  /*  187 */ "tcons",
       142591  +  /*  188 */ "sortlist",
       142592  +  /*  189 */ "eidlist",
       142593  +  /*  190 */ "defer_subclause_opt",
       142594  +  /*  191 */ "orconf",
       142595  +  /*  192 */ "resolvetype",
       142596  +  /*  193 */ "raisetype",
       142597  +  /*  194 */ "ifexists",
       142598  +  /*  195 */ "fullname",
       142599  +  /*  196 */ "selectnowith",
       142600  +  /*  197 */ "oneselect",
       142601  +  /*  198 */ "wqlist",
       142602  +  /*  199 */ "multiselect_op",
       142603  +  /*  200 */ "distinct",
       142604  +  /*  201 */ "selcollist",
       142605  +  /*  202 */ "from",
       142606  +  /*  203 */ "where_opt",
       142607  +  /*  204 */ "groupby_opt",
       142608  +  /*  205 */ "having_opt",
       142609  +  /*  206 */ "orderby_opt",
       142610  +  /*  207 */ "limit_opt",
       142611  +  /*  208 */ "values",
       142612  +  /*  209 */ "nexprlist",
       142613  +  /*  210 */ "exprlist",
       142614  +  /*  211 */ "sclp",
       142615  +  /*  212 */ "as",
       142616  +  /*  213 */ "seltablist",
       142617  +  /*  214 */ "stl_prefix",
       142618  +  /*  215 */ "joinop",
       142619  +  /*  216 */ "indexed_opt",
       142620  +  /*  217 */ "on_opt",
       142621  +  /*  218 */ "using_opt",
       142622  +  /*  219 */ "xfullname",
       142623  +  /*  220 */ "idlist",
       142624  +  /*  221 */ "with",
       142625  +  /*  222 */ "setlist",
       142626  +  /*  223 */ "insert_cmd",
       142627  +  /*  224 */ "idlist_opt",
       142628  +  /*  225 */ "upsert",
       142629  +  /*  226 */ "likeop",
       142630  +  /*  227 */ "between_op",
       142631  +  /*  228 */ "in_op",
       142632  +  /*  229 */ "paren_exprlist",
       142633  +  /*  230 */ "case_operand",
       142634  +  /*  231 */ "case_exprlist",
       142635  +  /*  232 */ "case_else",
       142636  +  /*  233 */ "uniqueflag",
       142637  +  /*  234 */ "collate",
       142638  +  /*  235 */ "nmnum",
       142639  +  /*  236 */ "trigger_decl",
       142640  +  /*  237 */ "trigger_cmd_list",
       142641  +  /*  238 */ "trigger_time",
       142642  +  /*  239 */ "trigger_event",
       142643  +  /*  240 */ "foreach_clause",
       142644  +  /*  241 */ "when_clause",
       142645  +  /*  242 */ "trigger_cmd",
       142646  +  /*  243 */ "trnm",
       142647  +  /*  244 */ "tridxby",
       142648  +  /*  245 */ "database_kw_opt",
       142649  +  /*  246 */ "key_opt",
       142650  +  /*  247 */ "add_column_fullname",
       142651  +  /*  248 */ "kwcolumn_opt",
       142652  +  /*  249 */ "create_vtab",
       142653  +  /*  250 */ "vtabarglist",
       142654  +  /*  251 */ "vtabarg",
       142655  +  /*  252 */ "vtabargtoken",
       142656  +  /*  253 */ "lp",
       142657  +  /*  254 */ "anylist",
141079 142658   };
141080 142659   #endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
141081 142660   
141082 142661   #ifndef NDEBUG
141083 142662   /* For tracing reduce actions, the names of all rules are required.
141084 142663   */
141085 142664   static const char *const yyRuleName[] = {
................................................................................
141190 142769    /* 104 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt",
141191 142770    /* 105 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
141192 142771    /* 106 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
141193 142772    /* 107 */ "dbnm ::=",
141194 142773    /* 108 */ "dbnm ::= DOT nm",
141195 142774    /* 109 */ "fullname ::= nm",
141196 142775    /* 110 */ "fullname ::= nm DOT nm",
141197         - /* 111 */ "joinop ::= COMMA|JOIN",
141198         - /* 112 */ "joinop ::= JOIN_KW JOIN",
141199         - /* 113 */ "joinop ::= JOIN_KW nm JOIN",
141200         - /* 114 */ "joinop ::= JOIN_KW nm nm JOIN",
141201         - /* 115 */ "on_opt ::= ON expr",
141202         - /* 116 */ "on_opt ::=",
141203         - /* 117 */ "indexed_opt ::=",
141204         - /* 118 */ "indexed_opt ::= INDEXED BY nm",
141205         - /* 119 */ "indexed_opt ::= NOT INDEXED",
141206         - /* 120 */ "using_opt ::= USING LP idlist RP",
141207         - /* 121 */ "using_opt ::=",
141208         - /* 122 */ "orderby_opt ::=",
141209         - /* 123 */ "orderby_opt ::= ORDER BY sortlist",
141210         - /* 124 */ "sortlist ::= sortlist COMMA expr sortorder",
141211         - /* 125 */ "sortlist ::= expr sortorder",
141212         - /* 126 */ "sortorder ::= ASC",
141213         - /* 127 */ "sortorder ::= DESC",
141214         - /* 128 */ "sortorder ::=",
141215         - /* 129 */ "groupby_opt ::=",
141216         - /* 130 */ "groupby_opt ::= GROUP BY nexprlist",
141217         - /* 131 */ "having_opt ::=",
141218         - /* 132 */ "having_opt ::= HAVING expr",
141219         - /* 133 */ "limit_opt ::=",
141220         - /* 134 */ "limit_opt ::= LIMIT expr",
141221         - /* 135 */ "limit_opt ::= LIMIT expr OFFSET expr",
141222         - /* 136 */ "limit_opt ::= LIMIT expr COMMA expr",
141223         - /* 137 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt",
141224         - /* 138 */ "where_opt ::=",
141225         - /* 139 */ "where_opt ::= WHERE expr",
141226         - /* 140 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt",
141227         - /* 141 */ "setlist ::= setlist COMMA nm EQ expr",
141228         - /* 142 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
141229         - /* 143 */ "setlist ::= nm EQ expr",
141230         - /* 144 */ "setlist ::= LP idlist RP EQ expr",
141231         - /* 145 */ "cmd ::= with insert_cmd INTO fullname idlist_opt select",
141232         - /* 146 */ "cmd ::= with insert_cmd INTO fullname idlist_opt DEFAULT VALUES",
141233         - /* 147 */ "insert_cmd ::= INSERT orconf",
141234         - /* 148 */ "insert_cmd ::= REPLACE",
141235         - /* 149 */ "idlist_opt ::=",
141236         - /* 150 */ "idlist_opt ::= LP idlist RP",
141237         - /* 151 */ "idlist ::= idlist COMMA nm",
141238         - /* 152 */ "idlist ::= nm",
141239         - /* 153 */ "expr ::= LP expr RP",
141240         - /* 154 */ "expr ::= ID|INDEXED",
141241         - /* 155 */ "expr ::= JOIN_KW",
141242         - /* 156 */ "expr ::= nm DOT nm",
141243         - /* 157 */ "expr ::= nm DOT nm DOT nm",
141244         - /* 158 */ "term ::= NULL|FLOAT|BLOB",
141245         - /* 159 */ "term ::= STRING",
141246         - /* 160 */ "term ::= INTEGER",
141247         - /* 161 */ "expr ::= VARIABLE",
141248         - /* 162 */ "expr ::= expr COLLATE ID|STRING",
141249         - /* 163 */ "expr ::= CAST LP expr AS typetoken RP",
141250         - /* 164 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
141251         - /* 165 */ "expr ::= ID|INDEXED LP STAR RP",
141252         - /* 166 */ "term ::= CTIME_KW",
141253         - /* 167 */ "expr ::= LP nexprlist COMMA expr RP",
141254         - /* 168 */ "expr ::= expr AND expr",
141255         - /* 169 */ "expr ::= expr OR expr",
141256         - /* 170 */ "expr ::= expr LT|GT|GE|LE expr",
141257         - /* 171 */ "expr ::= expr EQ|NE expr",
141258         - /* 172 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
141259         - /* 173 */ "expr ::= expr PLUS|MINUS expr",
141260         - /* 174 */ "expr ::= expr STAR|SLASH|REM expr",
141261         - /* 175 */ "expr ::= expr CONCAT expr",
141262         - /* 176 */ "likeop ::= NOT LIKE_KW|MATCH",
141263         - /* 177 */ "expr ::= expr likeop expr",
141264         - /* 178 */ "expr ::= expr likeop expr ESCAPE expr",
141265         - /* 179 */ "expr ::= expr ISNULL|NOTNULL",
141266         - /* 180 */ "expr ::= expr NOT NULL",
141267         - /* 181 */ "expr ::= expr IS expr",
141268         - /* 182 */ "expr ::= expr IS NOT expr",
141269         - /* 183 */ "expr ::= NOT expr",
141270         - /* 184 */ "expr ::= BITNOT expr",
141271         - /* 185 */ "expr ::= MINUS expr",
141272         - /* 186 */ "expr ::= PLUS expr",
141273         - /* 187 */ "between_op ::= BETWEEN",
141274         - /* 188 */ "between_op ::= NOT BETWEEN",
141275         - /* 189 */ "expr ::= expr between_op expr AND expr",
141276         - /* 190 */ "in_op ::= IN",
141277         - /* 191 */ "in_op ::= NOT IN",
141278         - /* 192 */ "expr ::= expr in_op LP exprlist RP",
141279         - /* 193 */ "expr ::= LP select RP",
141280         - /* 194 */ "expr ::= expr in_op LP select RP",
141281         - /* 195 */ "expr ::= expr in_op nm dbnm paren_exprlist",
141282         - /* 196 */ "expr ::= EXISTS LP select RP",
141283         - /* 197 */ "expr ::= CASE case_operand case_exprlist case_else END",
141284         - /* 198 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
141285         - /* 199 */ "case_exprlist ::= WHEN expr THEN expr",
141286         - /* 200 */ "case_else ::= ELSE expr",
141287         - /* 201 */ "case_else ::=",
141288         - /* 202 */ "case_operand ::= expr",
141289         - /* 203 */ "case_operand ::=",
141290         - /* 204 */ "exprlist ::=",
141291         - /* 205 */ "nexprlist ::= nexprlist COMMA expr",
141292         - /* 206 */ "nexprlist ::= expr",
141293         - /* 207 */ "paren_exprlist ::=",
141294         - /* 208 */ "paren_exprlist ::= LP exprlist RP",
141295         - /* 209 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
141296         - /* 210 */ "uniqueflag ::= UNIQUE",
141297         - /* 211 */ "uniqueflag ::=",
141298         - /* 212 */ "eidlist_opt ::=",
141299         - /* 213 */ "eidlist_opt ::= LP eidlist RP",
141300         - /* 214 */ "eidlist ::= eidlist COMMA nm collate sortorder",
141301         - /* 215 */ "eidlist ::= nm collate sortorder",
141302         - /* 216 */ "collate ::=",
141303         - /* 217 */ "collate ::= COLLATE ID|STRING",
141304         - /* 218 */ "cmd ::= DROP INDEX ifexists fullname",
141305         - /* 219 */ "cmd ::= VACUUM",
141306         - /* 220 */ "cmd ::= VACUUM nm",
141307         - /* 221 */ "cmd ::= PRAGMA nm dbnm",
141308         - /* 222 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
141309         - /* 223 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
141310         - /* 224 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
141311         - /* 225 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
141312         - /* 226 */ "plus_num ::= PLUS INTEGER|FLOAT",
141313         - /* 227 */ "minus_num ::= MINUS INTEGER|FLOAT",
141314         - /* 228 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
141315         - /* 229 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
141316         - /* 230 */ "trigger_time ::= BEFORE|AFTER",
141317         - /* 231 */ "trigger_time ::= INSTEAD OF",
141318         - /* 232 */ "trigger_time ::=",
141319         - /* 233 */ "trigger_event ::= DELETE|INSERT",
141320         - /* 234 */ "trigger_event ::= UPDATE",
141321         - /* 235 */ "trigger_event ::= UPDATE OF idlist",
141322         - /* 236 */ "when_clause ::=",
141323         - /* 237 */ "when_clause ::= WHEN expr",
141324         - /* 238 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
141325         - /* 239 */ "trigger_cmd_list ::= trigger_cmd SEMI",
141326         - /* 240 */ "trnm ::= nm DOT nm",
141327         - /* 241 */ "tridxby ::= INDEXED BY nm",
141328         - /* 242 */ "tridxby ::= NOT INDEXED",
141329         - /* 243 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt",
141330         - /* 244 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select scanpt",
141331         - /* 245 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
141332         - /* 246 */ "trigger_cmd ::= scanpt select scanpt",
141333         - /* 247 */ "expr ::= RAISE LP IGNORE RP",
141334         - /* 248 */ "expr ::= RAISE LP raisetype COMMA nm RP",
141335         - /* 249 */ "raisetype ::= ROLLBACK",
141336         - /* 250 */ "raisetype ::= ABORT",
141337         - /* 251 */ "raisetype ::= FAIL",
141338         - /* 252 */ "cmd ::= DROP TRIGGER ifexists fullname",
141339         - /* 253 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
141340         - /* 254 */ "cmd ::= DETACH database_kw_opt expr",
141341         - /* 255 */ "key_opt ::=",
141342         - /* 256 */ "key_opt ::= KEY expr",
141343         - /* 257 */ "cmd ::= REINDEX",
141344         - /* 258 */ "cmd ::= REINDEX nm dbnm",
141345         - /* 259 */ "cmd ::= ANALYZE",
141346         - /* 260 */ "cmd ::= ANALYZE nm dbnm",
141347         - /* 261 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
141348         - /* 262 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
141349         - /* 263 */ "add_column_fullname ::= fullname",
141350         - /* 264 */ "cmd ::= create_vtab",
141351         - /* 265 */ "cmd ::= create_vtab LP vtabarglist RP",
141352         - /* 266 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
141353         - /* 267 */ "vtabarg ::=",
141354         - /* 268 */ "vtabargtoken ::= ANY",
141355         - /* 269 */ "vtabargtoken ::= lp anylist RP",
141356         - /* 270 */ "lp ::= LP",
141357         - /* 271 */ "with ::= WITH wqlist",
141358         - /* 272 */ "with ::= WITH RECURSIVE wqlist",
141359         - /* 273 */ "wqlist ::= nm eidlist_opt AS LP select RP",
141360         - /* 274 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
141361         - /* 275 */ "input ::= cmdlist",
141362         - /* 276 */ "cmdlist ::= cmdlist ecmd",
141363         - /* 277 */ "cmdlist ::= ecmd",
141364         - /* 278 */ "ecmd ::= SEMI",
141365         - /* 279 */ "ecmd ::= explain cmdx SEMI",
141366         - /* 280 */ "explain ::=",
141367         - /* 281 */ "trans_opt ::=",
141368         - /* 282 */ "trans_opt ::= TRANSACTION",
141369         - /* 283 */ "trans_opt ::= TRANSACTION nm",
141370         - /* 284 */ "savepoint_opt ::= SAVEPOINT",
141371         - /* 285 */ "savepoint_opt ::=",
141372         - /* 286 */ "cmd ::= create_table create_table_args",
141373         - /* 287 */ "columnlist ::= columnlist COMMA columnname carglist",
141374         - /* 288 */ "columnlist ::= columnname carglist",
141375         - /* 289 */ "nm ::= ID|INDEXED",
141376         - /* 290 */ "nm ::= STRING",
141377         - /* 291 */ "nm ::= JOIN_KW",
141378         - /* 292 */ "typetoken ::= typename",
141379         - /* 293 */ "typename ::= ID|STRING",
141380         - /* 294 */ "signed ::= plus_num",
141381         - /* 295 */ "signed ::= minus_num",
141382         - /* 296 */ "carglist ::= carglist ccons",
141383         - /* 297 */ "carglist ::=",
141384         - /* 298 */ "ccons ::= NULL onconf",
141385         - /* 299 */ "conslist_opt ::= COMMA conslist",
141386         - /* 300 */ "conslist ::= conslist tconscomma tcons",
141387         - /* 301 */ "conslist ::= tcons",
141388         - /* 302 */ "tconscomma ::=",
141389         - /* 303 */ "defer_subclause_opt ::= defer_subclause",
141390         - /* 304 */ "resolvetype ::= raisetype",
141391         - /* 305 */ "selectnowith ::= oneselect",
141392         - /* 306 */ "oneselect ::= values",
141393         - /* 307 */ "sclp ::= selcollist COMMA",
141394         - /* 308 */ "as ::= ID|STRING",
141395         - /* 309 */ "expr ::= term",
141396         - /* 310 */ "likeop ::= LIKE_KW|MATCH",
141397         - /* 311 */ "exprlist ::= nexprlist",
141398         - /* 312 */ "nmnum ::= plus_num",
141399         - /* 313 */ "nmnum ::= nm",
141400         - /* 314 */ "nmnum ::= ON",
141401         - /* 315 */ "nmnum ::= DELETE",
141402         - /* 316 */ "nmnum ::= DEFAULT",
141403         - /* 317 */ "plus_num ::= INTEGER|FLOAT",
141404         - /* 318 */ "foreach_clause ::=",
141405         - /* 319 */ "foreach_clause ::= FOR EACH ROW",
141406         - /* 320 */ "trnm ::= nm",
141407         - /* 321 */ "tridxby ::=",
141408         - /* 322 */ "database_kw_opt ::= DATABASE",
141409         - /* 323 */ "database_kw_opt ::=",
141410         - /* 324 */ "kwcolumn_opt ::=",
141411         - /* 325 */ "kwcolumn_opt ::= COLUMNKW",
141412         - /* 326 */ "vtabarglist ::= vtabarg",
141413         - /* 327 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
141414         - /* 328 */ "vtabarg ::= vtabarg vtabargtoken",
141415         - /* 329 */ "anylist ::=",
141416         - /* 330 */ "anylist ::= anylist LP anylist RP",
141417         - /* 331 */ "anylist ::= anylist ANY",
141418         - /* 332 */ "with ::=",
       142776  + /* 111 */ "xfullname ::= nm",
       142777  + /* 112 */ "xfullname ::= nm DOT nm",
       142778  + /* 113 */ "xfullname ::= nm DOT nm AS nm",
       142779  + /* 114 */ "xfullname ::= nm AS nm",
       142780  + /* 115 */ "joinop ::= COMMA|JOIN",
       142781  + /* 116 */ "joinop ::= JOIN_KW JOIN",
       142782  + /* 117 */ "joinop ::= JOIN_KW nm JOIN",
       142783  + /* 118 */ "joinop ::= JOIN_KW nm nm JOIN",
       142784  + /* 119 */ "on_opt ::= ON expr",
       142785  + /* 120 */ "on_opt ::=",
       142786  + /* 121 */ "indexed_opt ::=",
       142787  + /* 122 */ "indexed_opt ::= INDEXED BY nm",
       142788  + /* 123 */ "indexed_opt ::= NOT INDEXED",
       142789  + /* 124 */ "using_opt ::= USING LP idlist RP",
       142790  + /* 125 */ "using_opt ::=",
       142791  + /* 126 */ "orderby_opt ::=",
       142792  + /* 127 */ "orderby_opt ::= ORDER BY sortlist",
       142793  + /* 128 */ "sortlist ::= sortlist COMMA expr sortorder",
       142794  + /* 129 */ "sortlist ::= expr sortorder",
       142795  + /* 130 */ "sortorder ::= ASC",
       142796  + /* 131 */ "sortorder ::= DESC",
       142797  + /* 132 */ "sortorder ::=",
       142798  + /* 133 */ "groupby_opt ::=",
       142799  + /* 134 */ "groupby_opt ::= GROUP BY nexprlist",
       142800  + /* 135 */ "having_opt ::=",
       142801  + /* 136 */ "having_opt ::= HAVING expr",
       142802  + /* 137 */ "limit_opt ::=",
       142803  + /* 138 */ "limit_opt ::= LIMIT expr",
       142804  + /* 139 */ "limit_opt ::= LIMIT expr OFFSET expr",
       142805  + /* 140 */ "limit_opt ::= LIMIT expr COMMA expr",
       142806  + /* 141 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt",
       142807  + /* 142 */ "where_opt ::=",
       142808  + /* 143 */ "where_opt ::= WHERE expr",
       142809  + /* 144 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt",
       142810  + /* 145 */ "setlist ::= setlist COMMA nm EQ expr",
       142811  + /* 146 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
       142812  + /* 147 */ "setlist ::= nm EQ expr",
       142813  + /* 148 */ "setlist ::= LP idlist RP EQ expr",
       142814  + /* 149 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
       142815  + /* 150 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES",
       142816  + /* 151 */ "upsert ::=",
       142817  + /* 152 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt",
       142818  + /* 153 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING",
       142819  + /* 154 */ "upsert ::= ON CONFLICT DO NOTHING",
       142820  + /* 155 */ "insert_cmd ::= INSERT orconf",
       142821  + /* 156 */ "insert_cmd ::= REPLACE",
       142822  + /* 157 */ "idlist_opt ::=",
       142823  + /* 158 */ "idlist_opt ::= LP idlist RP",
       142824  + /* 159 */ "idlist ::= idlist COMMA nm",
       142825  + /* 160 */ "idlist ::= nm",
       142826  + /* 161 */ "expr ::= LP expr RP",
       142827  + /* 162 */ "expr ::= ID|INDEXED",
       142828  + /* 163 */ "expr ::= JOIN_KW",
       142829  + /* 164 */ "expr ::= nm DOT nm",
       142830  + /* 165 */ "expr ::= nm DOT nm DOT nm",
       142831  + /* 166 */ "term ::= NULL|FLOAT|BLOB",
       142832  + /* 167 */ "term ::= STRING",
       142833  + /* 168 */ "term ::= INTEGER",
       142834  + /* 169 */ "expr ::= VARIABLE",
       142835  + /* 170 */ "expr ::= expr COLLATE ID|STRING",
       142836  + /* 171 */ "expr ::= CAST LP expr AS typetoken RP",
       142837  + /* 172 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
       142838  + /* 173 */ "expr ::= ID|INDEXED LP STAR RP",
       142839  + /* 174 */ "term ::= CTIME_KW",
       142840  + /* 175 */ "expr ::= LP nexprlist COMMA expr RP",
       142841  + /* 176 */ "expr ::= expr AND expr",
       142842  + /* 177 */ "expr ::= expr OR expr",
       142843  + /* 178 */ "expr ::= expr LT|GT|GE|LE expr",
       142844  + /* 179 */ "expr ::= expr EQ|NE expr",
       142845  + /* 180 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
       142846  + /* 181 */ "expr ::= expr PLUS|MINUS expr",
       142847  + /* 182 */ "expr ::= expr STAR|SLASH|REM expr",
       142848  + /* 183 */ "expr ::= expr CONCAT expr",
       142849  + /* 184 */ "likeop ::= NOT LIKE_KW|MATCH",
       142850  + /* 185 */ "expr ::= expr likeop expr",
       142851  + /* 186 */ "expr ::= expr likeop expr ESCAPE expr",
       142852  + /* 187 */ "expr ::= expr ISNULL|NOTNULL",
       142853  + /* 188 */ "expr ::= expr NOT NULL",
       142854  + /* 189 */ "expr ::= expr IS expr",
       142855  + /* 190 */ "expr ::= expr IS NOT expr",
       142856  + /* 191 */ "expr ::= NOT expr",
       142857  + /* 192 */ "expr ::= BITNOT expr",
       142858  + /* 193 */ "expr ::= MINUS expr",
       142859  + /* 194 */ "expr ::= PLUS expr",
       142860  + /* 195 */ "between_op ::= BETWEEN",
       142861  + /* 196 */ "between_op ::= NOT BETWEEN",
       142862  + /* 197 */ "expr ::= expr between_op expr AND expr",
       142863  + /* 198 */ "in_op ::= IN",
       142864  + /* 199 */ "in_op ::= NOT IN",
       142865  + /* 200 */ "expr ::= expr in_op LP exprlist RP",
       142866  + /* 201 */ "expr ::= LP select RP",
       142867  + /* 202 */ "expr ::= expr in_op LP select RP",
       142868  + /* 203 */ "expr ::= expr in_op nm dbnm paren_exprlist",
       142869  + /* 204 */ "expr ::= EXISTS LP select RP",
       142870  + /* 205 */ "expr ::= CASE case_operand case_exprlist case_else END",
       142871  + /* 206 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
       142872  + /* 207 */ "case_exprlist ::= WHEN expr THEN expr",
       142873  + /* 208 */ "case_else ::= ELSE expr",
       142874  + /* 209 */ "case_else ::=",
       142875  + /* 210 */ "case_operand ::= expr",
       142876  + /* 211 */ "case_operand ::=",
       142877  + /* 212 */ "exprlist ::=",
       142878  + /* 213 */ "nexprlist ::= nexprlist COMMA expr",
       142879  + /* 214 */ "nexprlist ::= expr",
       142880  + /* 215 */ "paren_exprlist ::=",
       142881  + /* 216 */ "paren_exprlist ::= LP exprlist RP",
       142882  + /* 217 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
       142883  + /* 218 */ "uniqueflag ::= UNIQUE",
       142884  + /* 219 */ "uniqueflag ::=",
       142885  + /* 220 */ "eidlist_opt ::=",
       142886  + /* 221 */ "eidlist_opt ::= LP eidlist RP",
       142887  + /* 222 */ "eidlist ::= eidlist COMMA nm collate sortorder",
       142888  + /* 223 */ "eidlist ::= nm collate sortorder",
       142889  + /* 224 */ "collate ::=",
       142890  + /* 225 */ "collate ::= COLLATE ID|STRING",
       142891  + /* 226 */ "cmd ::= DROP INDEX ifexists fullname",
       142892  + /* 227 */ "cmd ::= VACUUM",
       142893  + /* 228 */ "cmd ::= VACUUM nm",
       142894  + /* 229 */ "cmd ::= PRAGMA nm dbnm",
       142895  + /* 230 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
       142896  + /* 231 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
       142897  + /* 232 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
       142898  + /* 233 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
       142899  + /* 234 */ "plus_num ::= PLUS INTEGER|FLOAT",
       142900  + /* 235 */ "minus_num ::= MINUS INTEGER|FLOAT",
       142901  + /* 236 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
       142902  + /* 237 */ &q